xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/amdgpu/amdgpu_virt.h (revision e4a580baf2598beeaae98d953ac7635b8700b80c)
1*e4a580baSriastradh /*	$NetBSD: amdgpu_virt.h,v 1.3 2021/12/19 10:59:01 riastradh Exp $	*/
24e390cabSriastradh 
34e390cabSriastradh /*
44e390cabSriastradh  * Copyright 2016 Advanced Micro Devices, Inc.
54e390cabSriastradh  *
64e390cabSriastradh  * Permission is hereby granted, free of charge, to any person obtaining a
74e390cabSriastradh  * copy of this software and associated documentation files (the "Software"),
84e390cabSriastradh  * to deal in the Software without restriction, including without limitation
94e390cabSriastradh  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
104e390cabSriastradh  * and/or sell copies of the Software, and to permit persons to whom the
114e390cabSriastradh  * Software is furnished to do so, subject to the following conditions:
124e390cabSriastradh  *
134e390cabSriastradh  * The above copyright notice and this permission notice shall be included in
144e390cabSriastradh  * all copies or substantial portions of the Software.
154e390cabSriastradh  *
164e390cabSriastradh  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
174e390cabSriastradh  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
184e390cabSriastradh  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
194e390cabSriastradh  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
204e390cabSriastradh  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
214e390cabSriastradh  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
224e390cabSriastradh  * OTHER DEALINGS IN THE SOFTWARE.
234e390cabSriastradh  *
244e390cabSriastradh  * Author: Monk.liu@amd.com
254e390cabSriastradh  */
264e390cabSriastradh #ifndef AMDGPU_VIRT_H
274e390cabSriastradh #define AMDGPU_VIRT_H
284e390cabSriastradh 
294e390cabSriastradh #define AMDGPU_SRIOV_CAPS_SRIOV_VBIOS  (1 << 0) /* vBIOS is sr-iov ready */
304e390cabSriastradh #define AMDGPU_SRIOV_CAPS_ENABLE_IOV   (1 << 1) /* sr-iov is enabled on this GPU */
314e390cabSriastradh #define AMDGPU_SRIOV_CAPS_IS_VF        (1 << 2) /* this GPU is a virtual function */
324e390cabSriastradh #define AMDGPU_PASSTHROUGH_MODE        (1 << 3) /* thw whole GPU is pass through for VM */
334e390cabSriastradh #define AMDGPU_SRIOV_CAPS_RUNTIME      (1 << 4) /* is out of full access mode */
344e390cabSriastradh 
354e390cabSriastradh struct amdgpu_mm_table {
364e390cabSriastradh 	struct amdgpu_bo	*bo;
374e390cabSriastradh 	uint32_t		*cpu_addr;
384e390cabSriastradh 	uint64_t		gpu_addr;
394e390cabSriastradh };
404e390cabSriastradh 
414e390cabSriastradh #define AMDGPU_VF_ERROR_ENTRY_SIZE    16
424e390cabSriastradh 
434e390cabSriastradh /* struct error_entry - amdgpu VF error information. */
444e390cabSriastradh struct amdgpu_vf_error_buffer {
454e390cabSriastradh 	struct mutex lock;
464e390cabSriastradh 	int read_count;
474e390cabSriastradh 	int write_count;
484e390cabSriastradh 	uint16_t code[AMDGPU_VF_ERROR_ENTRY_SIZE];
494e390cabSriastradh 	uint16_t flags[AMDGPU_VF_ERROR_ENTRY_SIZE];
504e390cabSriastradh 	uint64_t data[AMDGPU_VF_ERROR_ENTRY_SIZE];
514e390cabSriastradh };
524e390cabSriastradh 
534e390cabSriastradh /**
544e390cabSriastradh  * struct amdgpu_virt_ops - amdgpu device virt operations
554e390cabSriastradh  */
564e390cabSriastradh struct amdgpu_virt_ops {
574e390cabSriastradh 	int (*req_full_gpu)(struct amdgpu_device *adev, bool init);
584e390cabSriastradh 	int (*rel_full_gpu)(struct amdgpu_device *adev, bool init);
594e390cabSriastradh 	int (*reset_gpu)(struct amdgpu_device *adev);
604e390cabSriastradh 	int (*wait_reset)(struct amdgpu_device *adev);
614e390cabSriastradh 	void (*trans_msg)(struct amdgpu_device *adev, u32 req, u32 data1, u32 data2, u32 data3);
624e390cabSriastradh };
634e390cabSriastradh 
644e390cabSriastradh /*
654e390cabSriastradh  * Firmware Reserve Frame buffer
664e390cabSriastradh  */
674e390cabSriastradh struct amdgpu_virt_fw_reserve {
684e390cabSriastradh 	struct amd_sriov_msg_pf2vf_info_header *p_pf2vf;
694e390cabSriastradh 	struct amd_sriov_msg_vf2pf_info_header *p_vf2pf;
704e390cabSriastradh 	unsigned int checksum_key;
714e390cabSriastradh };
724e390cabSriastradh /*
734e390cabSriastradh  * Defination between PF and VF
744e390cabSriastradh  * Structures forcibly aligned to 4 to keep the same style as PF.
754e390cabSriastradh  */
764e390cabSriastradh #define AMDGIM_DATAEXCHANGE_OFFSET		(64 * 1024)
774e390cabSriastradh 
784e390cabSriastradh #define AMDGIM_GET_STRUCTURE_RESERVED_SIZE(total, u8, u16, u32, u64) \
794e390cabSriastradh 		(total - (((u8)+3) / 4 + ((u16)+1) / 2 + (u32) + (u64)*2))
804e390cabSriastradh 
814e390cabSriastradh enum AMDGIM_FEATURE_FLAG {
824e390cabSriastradh 	/* GIM supports feature of Error log collecting */
834e390cabSriastradh 	AMDGIM_FEATURE_ERROR_LOG_COLLECT = 0x1,
844e390cabSriastradh 	/* GIM supports feature of loading uCodes */
854e390cabSriastradh 	AMDGIM_FEATURE_GIM_LOAD_UCODES   = 0x2,
864e390cabSriastradh 	/* VRAM LOST by GIM */
874e390cabSriastradh 	AMDGIM_FEATURE_GIM_FLR_VRAMLOST = 0x4,
884e390cabSriastradh 	/* PP ONE VF MODE in GIM */
894e390cabSriastradh 	AMDGIM_FEATURE_PP_ONE_VF = (1 << 4),
904e390cabSriastradh };
914e390cabSriastradh 
924e390cabSriastradh struct amd_sriov_msg_pf2vf_info_header {
934e390cabSriastradh 	/* the total structure size in byte. */
944e390cabSriastradh 	uint32_t size;
954e390cabSriastradh 	/* version of this structure, written by the GIM */
964e390cabSriastradh 	uint32_t version;
974e390cabSriastradh 	/* reserved */
984e390cabSriastradh 	uint32_t reserved[2];
994e390cabSriastradh } __aligned(4);
1004e390cabSriastradh struct  amdgim_pf2vf_info_v1 {
1014e390cabSriastradh 	/* header contains size and version */
1024e390cabSriastradh 	struct amd_sriov_msg_pf2vf_info_header header;
1034e390cabSriastradh 	/* max_width * max_height */
1044e390cabSriastradh 	unsigned int uvd_enc_max_pixels_count;
1054e390cabSriastradh 	/* 16x16 pixels/sec, codec independent */
1064e390cabSriastradh 	unsigned int uvd_enc_max_bandwidth;
1074e390cabSriastradh 	/* max_width * max_height */
1084e390cabSriastradh 	unsigned int vce_enc_max_pixels_count;
1094e390cabSriastradh 	/* 16x16 pixels/sec, codec independent */
1104e390cabSriastradh 	unsigned int vce_enc_max_bandwidth;
1114e390cabSriastradh 	/* MEC FW position in kb from the start of visible frame buffer */
1124e390cabSriastradh 	unsigned int mecfw_kboffset;
1134e390cabSriastradh 	/* The features flags of the GIM driver supports. */
1144e390cabSriastradh 	unsigned int feature_flags;
1154e390cabSriastradh 	/* use private key from mailbox 2 to create chueksum */
1164e390cabSriastradh 	unsigned int checksum;
1174e390cabSriastradh } __aligned(4);
1184e390cabSriastradh 
1194e390cabSriastradh struct  amdgim_pf2vf_info_v2 {
1204e390cabSriastradh 	/* header contains size and version */
1214e390cabSriastradh 	struct amd_sriov_msg_pf2vf_info_header header;
1224e390cabSriastradh 	/* use private key from mailbox 2 to create chueksum */
1234e390cabSriastradh 	uint32_t checksum;
1244e390cabSriastradh 	/* The features flags of the GIM driver supports. */
1254e390cabSriastradh 	uint32_t feature_flags;
1264e390cabSriastradh 	/* max_width * max_height */
1274e390cabSriastradh 	uint32_t uvd_enc_max_pixels_count;
1284e390cabSriastradh 	/* 16x16 pixels/sec, codec independent */
1294e390cabSriastradh 	uint32_t uvd_enc_max_bandwidth;
1304e390cabSriastradh 	/* max_width * max_height */
1314e390cabSriastradh 	uint32_t vce_enc_max_pixels_count;
1324e390cabSriastradh 	/* 16x16 pixels/sec, codec independent */
1334e390cabSriastradh 	uint32_t vce_enc_max_bandwidth;
1344e390cabSriastradh 	/* MEC FW position in kb from the start of VF visible frame buffer */
1354e390cabSriastradh 	uint64_t mecfw_kboffset;
1364e390cabSriastradh 	/* MEC FW size in KB */
1374e390cabSriastradh 	uint32_t mecfw_ksize;
1384e390cabSriastradh 	/* UVD FW position in kb from the start of VF visible frame buffer */
1394e390cabSriastradh 	uint64_t uvdfw_kboffset;
1404e390cabSriastradh 	/* UVD FW size in KB */
1414e390cabSriastradh 	uint32_t uvdfw_ksize;
1424e390cabSriastradh 	/* VCE FW position in kb from the start of VF visible frame buffer */
1434e390cabSriastradh 	uint64_t vcefw_kboffset;
1444e390cabSriastradh 	/* VCE FW size in KB */
1454e390cabSriastradh 	uint32_t vcefw_ksize;
1464e390cabSriastradh 	uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 0, 0, (9 + sizeof(struct amd_sriov_msg_pf2vf_info_header)/sizeof(uint32_t)), 3)];
1474e390cabSriastradh } __aligned(4);
1484e390cabSriastradh 
1494e390cabSriastradh 
1504e390cabSriastradh struct amd_sriov_msg_vf2pf_info_header {
1514e390cabSriastradh 	/* the total structure size in byte. */
1524e390cabSriastradh 	uint32_t size;
1534e390cabSriastradh 	/*version of this structure, written by the guest */
1544e390cabSriastradh 	uint32_t version;
1554e390cabSriastradh 	/* reserved */
1564e390cabSriastradh 	uint32_t reserved[2];
1574e390cabSriastradh } __aligned(4);
1584e390cabSriastradh 
1594e390cabSriastradh struct amdgim_vf2pf_info_v1 {
1604e390cabSriastradh 	/* header contains size and version */
1614e390cabSriastradh 	struct amd_sriov_msg_vf2pf_info_header header;
1624e390cabSriastradh 	/* driver version */
1634e390cabSriastradh 	char driver_version[64];
1644e390cabSriastradh 	/* driver certification, 1=WHQL, 0=None */
1654e390cabSriastradh 	unsigned int driver_cert;
1664e390cabSriastradh 	/* guest OS type and version: need a define */
1674e390cabSriastradh 	unsigned int os_info;
1684e390cabSriastradh 	/* in the unit of 1M */
1694e390cabSriastradh 	unsigned int fb_usage;
1704e390cabSriastradh 	/* guest gfx engine usage percentage */
1714e390cabSriastradh 	unsigned int gfx_usage;
1724e390cabSriastradh 	/* guest gfx engine health percentage */
1734e390cabSriastradh 	unsigned int gfx_health;
1744e390cabSriastradh 	/* guest compute engine usage percentage */
1754e390cabSriastradh 	unsigned int compute_usage;
1764e390cabSriastradh 	/* guest compute engine health percentage */
1774e390cabSriastradh 	unsigned int compute_health;
1784e390cabSriastradh 	/* guest vce engine usage percentage. 0xffff means N/A. */
1794e390cabSriastradh 	unsigned int vce_enc_usage;
1804e390cabSriastradh 	/* guest vce engine health percentage. 0xffff means N/A. */
1814e390cabSriastradh 	unsigned int vce_enc_health;
1824e390cabSriastradh 	/* guest uvd engine usage percentage. 0xffff means N/A. */
1834e390cabSriastradh 	unsigned int uvd_enc_usage;
1844e390cabSriastradh 	/* guest uvd engine usage percentage. 0xffff means N/A. */
1854e390cabSriastradh 	unsigned int uvd_enc_health;
1864e390cabSriastradh 	unsigned int checksum;
1874e390cabSriastradh } __aligned(4);
1884e390cabSriastradh 
1894e390cabSriastradh struct amdgim_vf2pf_info_v2 {
1904e390cabSriastradh 	/* header contains size and version */
1914e390cabSriastradh 	struct amd_sriov_msg_vf2pf_info_header header;
1924e390cabSriastradh 	uint32_t checksum;
1934e390cabSriastradh 	/* driver version */
1944e390cabSriastradh 	uint8_t driver_version[64];
1954e390cabSriastradh 	/* driver certification, 1=WHQL, 0=None */
1964e390cabSriastradh 	uint32_t driver_cert;
1974e390cabSriastradh 	/* guest OS type and version: need a define */
1984e390cabSriastradh 	uint32_t os_info;
1994e390cabSriastradh 	/* in the unit of 1M */
2004e390cabSriastradh 	uint32_t fb_usage;
2014e390cabSriastradh 	/* guest gfx engine usage percentage */
2024e390cabSriastradh 	uint32_t gfx_usage;
2034e390cabSriastradh 	/* guest gfx engine health percentage */
2044e390cabSriastradh 	uint32_t gfx_health;
2054e390cabSriastradh 	/* guest compute engine usage percentage */
2064e390cabSriastradh 	uint32_t compute_usage;
2074e390cabSriastradh 	/* guest compute engine health percentage */
2084e390cabSriastradh 	uint32_t compute_health;
2094e390cabSriastradh 	/* guest vce engine usage percentage. 0xffff means N/A. */
2104e390cabSriastradh 	uint32_t vce_enc_usage;
2114e390cabSriastradh 	/* guest vce engine health percentage. 0xffff means N/A. */
2124e390cabSriastradh 	uint32_t vce_enc_health;
2134e390cabSriastradh 	/* guest uvd engine usage percentage. 0xffff means N/A. */
2144e390cabSriastradh 	uint32_t uvd_enc_usage;
2154e390cabSriastradh 	/* guest uvd engine usage percentage. 0xffff means N/A. */
2164e390cabSriastradh 	uint32_t uvd_enc_health;
2174e390cabSriastradh 	uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 64, 0, (12 + sizeof(struct amd_sriov_msg_vf2pf_info_header)/sizeof(uint32_t)), 0)];
2184e390cabSriastradh } __aligned(4);
2194e390cabSriastradh 
2204e390cabSriastradh #define AMDGPU_FW_VRAM_VF2PF_VER 2
2214e390cabSriastradh typedef struct amdgim_vf2pf_info_v2 amdgim_vf2pf_info ;
2224e390cabSriastradh 
2234e390cabSriastradh #define AMDGPU_FW_VRAM_VF2PF_WRITE(adev, field, val) \
2244e390cabSriastradh 	do { \
2254e390cabSriastradh 		((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field = (val); \
2264e390cabSriastradh 	} while (0)
2274e390cabSriastradh 
2284e390cabSriastradh #define AMDGPU_FW_VRAM_VF2PF_READ(adev, field, val) \
2294e390cabSriastradh 	do { \
2304e390cabSriastradh 		(*val) = ((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field; \
2314e390cabSriastradh 	} while (0)
2324e390cabSriastradh 
2334e390cabSriastradh #define AMDGPU_FW_VRAM_PF2VF_READ(adev, field, val) \
2344e390cabSriastradh 	do { \
2354e390cabSriastradh 		if (!adev->virt.fw_reserve.p_pf2vf) \
2364e390cabSriastradh 			*(val) = 0; \
2374e390cabSriastradh 		else { \
2384e390cabSriastradh 			if (adev->virt.fw_reserve.p_pf2vf->version == 1) \
2394e390cabSriastradh 				*(val) = ((struct amdgim_pf2vf_info_v1 *)adev->virt.fw_reserve.p_pf2vf)->field; \
2404e390cabSriastradh 			if (adev->virt.fw_reserve.p_pf2vf->version == 2) \
2414e390cabSriastradh 				*(val) = ((struct amdgim_pf2vf_info_v2 *)adev->virt.fw_reserve.p_pf2vf)->field; \
2424e390cabSriastradh 		} \
2434e390cabSriastradh 	} while (0)
2444e390cabSriastradh 
2454e390cabSriastradh /* GPU virtualization */
2464e390cabSriastradh struct amdgpu_virt {
2474e390cabSriastradh 	uint32_t			caps;
2484e390cabSriastradh 	struct amdgpu_bo		*csa_obj;
2494e390cabSriastradh 	void				*csa_cpu_addr;
2504e390cabSriastradh 	bool chained_ib_support;
2514e390cabSriastradh 	uint32_t			reg_val_offs;
2524e390cabSriastradh 	struct amdgpu_irq_src		ack_irq;
2534e390cabSriastradh 	struct amdgpu_irq_src		rcv_irq;
2544e390cabSriastradh 	struct work_struct		flr_work;
2554e390cabSriastradh 	struct amdgpu_mm_table		mm_table;
2564e390cabSriastradh 	const struct amdgpu_virt_ops	*ops;
2574e390cabSriastradh 	struct amdgpu_vf_error_buffer   vf_errors;
2584e390cabSriastradh 	struct amdgpu_virt_fw_reserve	fw_reserve;
2594e390cabSriastradh 	uint32_t gim_feature;
2604e390cabSriastradh 	uint32_t reg_access_mode;
2614e390cabSriastradh };
2624e390cabSriastradh 
2634e390cabSriastradh #define amdgpu_sriov_enabled(adev) \
2644e390cabSriastradh ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_ENABLE_IOV)
2654e390cabSriastradh 
2664e390cabSriastradh #define amdgpu_sriov_vf(adev) \
2674e390cabSriastradh ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_IS_VF)
2684e390cabSriastradh 
2694e390cabSriastradh #define amdgpu_sriov_bios(adev) \
2704e390cabSriastradh ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS)
2714e390cabSriastradh 
2724e390cabSriastradh #define amdgpu_sriov_runtime(adev) \
2734e390cabSriastradh ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_RUNTIME)
2744e390cabSriastradh 
2754e390cabSriastradh #define amdgpu_passthrough(adev) \
2764e390cabSriastradh ((adev)->virt.caps & AMDGPU_PASSTHROUGH_MODE)
2774e390cabSriastradh 
is_virtual_machine(void)2784e390cabSriastradh static inline bool is_virtual_machine(void)
2794e390cabSriastradh {
2804e390cabSriastradh #ifdef CONFIG_X86
281*e4a580baSriastradh #ifdef __NetBSD__
282*e4a580baSriastradh 	return false;		/* XXX */
283*e4a580baSriastradh #else
2844e390cabSriastradh 	return boot_cpu_has(X86_FEATURE_HYPERVISOR);
285*e4a580baSriastradh #endif
2864e390cabSriastradh #else
2874e390cabSriastradh 	return false;
2884e390cabSriastradh #endif
2894e390cabSriastradh }
2904e390cabSriastradh 
2914e390cabSriastradh #define amdgpu_sriov_is_pp_one_vf(adev) \
2924e390cabSriastradh 	((adev)->virt.gim_feature & AMDGIM_FEATURE_PP_ONE_VF)
2934e390cabSriastradh 
2944e390cabSriastradh bool amdgpu_virt_mmio_blocked(struct amdgpu_device *adev);
2954e390cabSriastradh void amdgpu_virt_init_setting(struct amdgpu_device *adev);
2964e390cabSriastradh void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev,
2974e390cabSriastradh 					uint32_t reg0, uint32_t rreg1,
2984e390cabSriastradh 					uint32_t ref, uint32_t mask);
2994e390cabSriastradh int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init);
3004e390cabSriastradh int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init);
3014e390cabSriastradh int amdgpu_virt_reset_gpu(struct amdgpu_device *adev);
3024e390cabSriastradh int amdgpu_virt_wait_reset(struct amdgpu_device *adev);
3034e390cabSriastradh int amdgpu_virt_alloc_mm_table(struct amdgpu_device *adev);
3044e390cabSriastradh void amdgpu_virt_free_mm_table(struct amdgpu_device *adev);
3054e390cabSriastradh int amdgpu_virt_fw_reserve_get_checksum(void *obj, unsigned long obj_size,
3064e390cabSriastradh 					unsigned int key,
3074e390cabSriastradh 					unsigned int chksum);
3084e390cabSriastradh void amdgpu_virt_init_data_exchange(struct amdgpu_device *adev);
3094e390cabSriastradh #endif
310