xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_virt.h (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2016 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  * Author: Monk.liu@amd.com
23b843c749SSergey Zigachev  */
24b843c749SSergey Zigachev #ifndef AMDGPU_VIRT_H
25b843c749SSergey Zigachev #define AMDGPU_VIRT_H
26b843c749SSergey Zigachev 
27b843c749SSergey Zigachev #define AMDGPU_SRIOV_CAPS_SRIOV_VBIOS  (1 << 0) /* vBIOS is sr-iov ready */
28b843c749SSergey Zigachev #define AMDGPU_SRIOV_CAPS_ENABLE_IOV   (1 << 1) /* sr-iov is enabled on this GPU */
29b843c749SSergey Zigachev #define AMDGPU_SRIOV_CAPS_IS_VF        (1 << 2) /* this GPU is a virtual function */
30b843c749SSergey Zigachev #define AMDGPU_PASSTHROUGH_MODE        (1 << 3) /* thw whole GPU is pass through for VM */
31b843c749SSergey Zigachev #define AMDGPU_SRIOV_CAPS_RUNTIME      (1 << 4) /* is out of full access mode */
32b843c749SSergey Zigachev 
33b843c749SSergey Zigachev struct amdgpu_mm_table {
34b843c749SSergey Zigachev 	struct amdgpu_bo	*bo;
35b843c749SSergey Zigachev 	uint32_t		*cpu_addr;
36b843c749SSergey Zigachev 	uint64_t		gpu_addr;
37b843c749SSergey Zigachev };
38b843c749SSergey Zigachev 
39b843c749SSergey Zigachev #define AMDGPU_VF_ERROR_ENTRY_SIZE    16
40b843c749SSergey Zigachev 
41b843c749SSergey Zigachev /* struct error_entry - amdgpu VF error information. */
42b843c749SSergey Zigachev struct amdgpu_vf_error_buffer {
43*78973132SSergey Zigachev 	struct lock lock;
44b843c749SSergey Zigachev 	int read_count;
45b843c749SSergey Zigachev 	int write_count;
46b843c749SSergey Zigachev 	uint16_t code[AMDGPU_VF_ERROR_ENTRY_SIZE];
47b843c749SSergey Zigachev 	uint16_t flags[AMDGPU_VF_ERROR_ENTRY_SIZE];
48b843c749SSergey Zigachev 	uint64_t data[AMDGPU_VF_ERROR_ENTRY_SIZE];
49b843c749SSergey Zigachev };
50b843c749SSergey Zigachev 
51b843c749SSergey Zigachev /**
52b843c749SSergey Zigachev  * struct amdgpu_virt_ops - amdgpu device virt operations
53b843c749SSergey Zigachev  */
54b843c749SSergey Zigachev struct amdgpu_virt_ops {
55b843c749SSergey Zigachev 	int (*req_full_gpu)(struct amdgpu_device *adev, bool init);
56b843c749SSergey Zigachev 	int (*rel_full_gpu)(struct amdgpu_device *adev, bool init);
57b843c749SSergey Zigachev 	int (*reset_gpu)(struct amdgpu_device *adev);
58b843c749SSergey Zigachev 	int (*wait_reset)(struct amdgpu_device *adev);
59b843c749SSergey Zigachev 	void (*trans_msg)(struct amdgpu_device *adev, u32 req, u32 data1, u32 data2, u32 data3);
60b843c749SSergey Zigachev };
61b843c749SSergey Zigachev 
62b843c749SSergey Zigachev /*
63b843c749SSergey Zigachev  * Firmware Reserve Frame buffer
64b843c749SSergey Zigachev  */
65b843c749SSergey Zigachev struct amdgpu_virt_fw_reserve {
66b843c749SSergey Zigachev 	struct amdgim_pf2vf_info_header *p_pf2vf;
67b843c749SSergey Zigachev 	struct amdgim_vf2pf_info_header *p_vf2pf;
68b843c749SSergey Zigachev 	unsigned int checksum_key;
69b843c749SSergey Zigachev };
70b843c749SSergey Zigachev /*
71b843c749SSergey Zigachev  * Defination between PF and VF
72b843c749SSergey Zigachev  * Structures forcibly aligned to 4 to keep the same style as PF.
73b843c749SSergey Zigachev  */
74b843c749SSergey Zigachev #define AMDGIM_DATAEXCHANGE_OFFSET		(64 * 1024)
75b843c749SSergey Zigachev 
76b843c749SSergey Zigachev #define AMDGIM_GET_STRUCTURE_RESERVED_SIZE(total, u8, u16, u32, u64) \
77b843c749SSergey Zigachev 		(total - (((u8)+3) / 4 + ((u16)+1) / 2 + (u32) + (u64)*2))
78b843c749SSergey Zigachev 
79b843c749SSergey Zigachev enum AMDGIM_FEATURE_FLAG {
80b843c749SSergey Zigachev 	/* GIM supports feature of Error log collecting */
81b843c749SSergey Zigachev 	AMDGIM_FEATURE_ERROR_LOG_COLLECT = 0x1,
82b843c749SSergey Zigachev 	/* GIM supports feature of loading uCodes */
83b843c749SSergey Zigachev 	AMDGIM_FEATURE_GIM_LOAD_UCODES   = 0x2,
84b843c749SSergey Zigachev 	/* VRAM LOST by GIM */
85b843c749SSergey Zigachev 	AMDGIM_FEATURE_GIM_FLR_VRAMLOST = 0x4,
86b843c749SSergey Zigachev };
87b843c749SSergey Zigachev 
88b843c749SSergey Zigachev struct amdgim_pf2vf_info_header {
89b843c749SSergey Zigachev 	/* the total structure size in byte. */
90b843c749SSergey Zigachev 	uint32_t size;
91b843c749SSergey Zigachev 	/* version of this structure, written by the GIM */
92b843c749SSergey Zigachev 	uint32_t version;
93b843c749SSergey Zigachev } __aligned(4);
94b843c749SSergey Zigachev struct  amdgim_pf2vf_info_v1 {
95b843c749SSergey Zigachev 	/* header contains size and version */
96b843c749SSergey Zigachev 	struct amdgim_pf2vf_info_header header;
97b843c749SSergey Zigachev 	/* max_width * max_height */
98b843c749SSergey Zigachev 	unsigned int uvd_enc_max_pixels_count;
99b843c749SSergey Zigachev 	/* 16x16 pixels/sec, codec independent */
100b843c749SSergey Zigachev 	unsigned int uvd_enc_max_bandwidth;
101b843c749SSergey Zigachev 	/* max_width * max_height */
102b843c749SSergey Zigachev 	unsigned int vce_enc_max_pixels_count;
103b843c749SSergey Zigachev 	/* 16x16 pixels/sec, codec independent */
104b843c749SSergey Zigachev 	unsigned int vce_enc_max_bandwidth;
105b843c749SSergey Zigachev 	/* MEC FW position in kb from the start of visible frame buffer */
106b843c749SSergey Zigachev 	unsigned int mecfw_kboffset;
107b843c749SSergey Zigachev 	/* The features flags of the GIM driver supports. */
108b843c749SSergey Zigachev 	unsigned int feature_flags;
109b843c749SSergey Zigachev 	/* use private key from mailbox 2 to create chueksum */
110b843c749SSergey Zigachev 	unsigned int checksum;
111b843c749SSergey Zigachev } __aligned(4);
112b843c749SSergey Zigachev 
113b843c749SSergey Zigachev struct  amdgim_pf2vf_info_v2 {
114b843c749SSergey Zigachev 	/* header contains size and version */
115b843c749SSergey Zigachev 	struct amdgim_pf2vf_info_header header;
116b843c749SSergey Zigachev 	/* use private key from mailbox 2 to create chueksum */
117b843c749SSergey Zigachev 	uint32_t checksum;
118b843c749SSergey Zigachev 	/* The features flags of the GIM driver supports. */
119b843c749SSergey Zigachev 	uint32_t feature_flags;
120b843c749SSergey Zigachev 	/* max_width * max_height */
121b843c749SSergey Zigachev 	uint32_t uvd_enc_max_pixels_count;
122b843c749SSergey Zigachev 	/* 16x16 pixels/sec, codec independent */
123b843c749SSergey Zigachev 	uint32_t uvd_enc_max_bandwidth;
124b843c749SSergey Zigachev 	/* max_width * max_height */
125b843c749SSergey Zigachev 	uint32_t vce_enc_max_pixels_count;
126b843c749SSergey Zigachev 	/* 16x16 pixels/sec, codec independent */
127b843c749SSergey Zigachev 	uint32_t vce_enc_max_bandwidth;
128b843c749SSergey Zigachev 	/* MEC FW position in kb from the start of VF visible frame buffer */
129b843c749SSergey Zigachev 	uint64_t mecfw_kboffset;
130b843c749SSergey Zigachev 	/* MEC FW size in KB */
131b843c749SSergey Zigachev 	uint32_t mecfw_ksize;
132b843c749SSergey Zigachev 	/* UVD FW position in kb from the start of VF visible frame buffer */
133b843c749SSergey Zigachev 	uint64_t uvdfw_kboffset;
134b843c749SSergey Zigachev 	/* UVD FW size in KB */
135b843c749SSergey Zigachev 	uint32_t uvdfw_ksize;
136b843c749SSergey Zigachev 	/* VCE FW position in kb from the start of VF visible frame buffer */
137b843c749SSergey Zigachev 	uint64_t vcefw_kboffset;
138b843c749SSergey Zigachev 	/* VCE FW size in KB */
139b843c749SSergey Zigachev 	uint32_t vcefw_ksize;
140b843c749SSergey Zigachev 	uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 0, 0, (9 + sizeof(struct amdgim_pf2vf_info_header)/sizeof(uint32_t)), 3)];
141b843c749SSergey Zigachev } __aligned(4);
142b843c749SSergey Zigachev 
143b843c749SSergey Zigachev 
144b843c749SSergey Zigachev struct amdgim_vf2pf_info_header {
145b843c749SSergey Zigachev 	/* the total structure size in byte. */
146b843c749SSergey Zigachev 	uint32_t size;
147b843c749SSergey Zigachev 	/*version of this structure, written by the guest */
148b843c749SSergey Zigachev 	uint32_t version;
149b843c749SSergey Zigachev } __aligned(4);
150b843c749SSergey Zigachev 
151b843c749SSergey Zigachev struct amdgim_vf2pf_info_v1 {
152b843c749SSergey Zigachev 	/* header contains size and version */
153b843c749SSergey Zigachev 	struct amdgim_vf2pf_info_header header;
154b843c749SSergey Zigachev 	/* driver version */
155b843c749SSergey Zigachev 	char driver_version[64];
156b843c749SSergey Zigachev 	/* driver certification, 1=WHQL, 0=None */
157b843c749SSergey Zigachev 	unsigned int driver_cert;
158b843c749SSergey Zigachev 	/* guest OS type and version: need a define */
159b843c749SSergey Zigachev 	unsigned int os_info;
160b843c749SSergey Zigachev 	/* in the unit of 1M */
161b843c749SSergey Zigachev 	unsigned int fb_usage;
162b843c749SSergey Zigachev 	/* guest gfx engine usage percentage */
163b843c749SSergey Zigachev 	unsigned int gfx_usage;
164b843c749SSergey Zigachev 	/* guest gfx engine health percentage */
165b843c749SSergey Zigachev 	unsigned int gfx_health;
166b843c749SSergey Zigachev 	/* guest compute engine usage percentage */
167b843c749SSergey Zigachev 	unsigned int compute_usage;
168b843c749SSergey Zigachev 	/* guest compute engine health percentage */
169b843c749SSergey Zigachev 	unsigned int compute_health;
170b843c749SSergey Zigachev 	/* guest vce engine usage percentage. 0xffff means N/A. */
171b843c749SSergey Zigachev 	unsigned int vce_enc_usage;
172b843c749SSergey Zigachev 	/* guest vce engine health percentage. 0xffff means N/A. */
173b843c749SSergey Zigachev 	unsigned int vce_enc_health;
174b843c749SSergey Zigachev 	/* guest uvd engine usage percentage. 0xffff means N/A. */
175b843c749SSergey Zigachev 	unsigned int uvd_enc_usage;
176b843c749SSergey Zigachev 	/* guest uvd engine usage percentage. 0xffff means N/A. */
177b843c749SSergey Zigachev 	unsigned int uvd_enc_health;
178b843c749SSergey Zigachev 	unsigned int checksum;
179b843c749SSergey Zigachev } __aligned(4);
180b843c749SSergey Zigachev 
181b843c749SSergey Zigachev struct amdgim_vf2pf_info_v2 {
182b843c749SSergey Zigachev 	/* header contains size and version */
183b843c749SSergey Zigachev 	struct amdgim_vf2pf_info_header header;
184b843c749SSergey Zigachev 	uint32_t checksum;
185b843c749SSergey Zigachev 	/* driver version */
186b843c749SSergey Zigachev 	uint8_t driver_version[64];
187b843c749SSergey Zigachev 	/* driver certification, 1=WHQL, 0=None */
188b843c749SSergey Zigachev 	uint32_t driver_cert;
189b843c749SSergey Zigachev 	/* guest OS type and version: need a define */
190b843c749SSergey Zigachev 	uint32_t os_info;
191b843c749SSergey Zigachev 	/* in the unit of 1M */
192b843c749SSergey Zigachev 	uint32_t fb_usage;
193b843c749SSergey Zigachev 	/* guest gfx engine usage percentage */
194b843c749SSergey Zigachev 	uint32_t gfx_usage;
195b843c749SSergey Zigachev 	/* guest gfx engine health percentage */
196b843c749SSergey Zigachev 	uint32_t gfx_health;
197b843c749SSergey Zigachev 	/* guest compute engine usage percentage */
198b843c749SSergey Zigachev 	uint32_t compute_usage;
199b843c749SSergey Zigachev 	/* guest compute engine health percentage */
200b843c749SSergey Zigachev 	uint32_t compute_health;
201b843c749SSergey Zigachev 	/* guest vce engine usage percentage. 0xffff means N/A. */
202b843c749SSergey Zigachev 	uint32_t vce_enc_usage;
203b843c749SSergey Zigachev 	/* guest vce engine health percentage. 0xffff means N/A. */
204b843c749SSergey Zigachev 	uint32_t vce_enc_health;
205b843c749SSergey Zigachev 	/* guest uvd engine usage percentage. 0xffff means N/A. */
206b843c749SSergey Zigachev 	uint32_t uvd_enc_usage;
207b843c749SSergey Zigachev 	/* guest uvd engine usage percentage. 0xffff means N/A. */
208b843c749SSergey Zigachev 	uint32_t uvd_enc_health;
209b843c749SSergey Zigachev 	uint32_t reserved[AMDGIM_GET_STRUCTURE_RESERVED_SIZE(256, 64, 0, (12 + sizeof(struct amdgim_vf2pf_info_header)/sizeof(uint32_t)), 0)];
210b843c749SSergey Zigachev } __aligned(4);
211b843c749SSergey Zigachev 
212b843c749SSergey Zigachev #define AMDGPU_FW_VRAM_VF2PF_VER 2
213b843c749SSergey Zigachev typedef struct amdgim_vf2pf_info_v2 amdgim_vf2pf_info ;
214b843c749SSergey Zigachev 
215b843c749SSergey Zigachev #define AMDGPU_FW_VRAM_VF2PF_WRITE(adev, field, val) \
216b843c749SSergey Zigachev 	do { \
217b843c749SSergey Zigachev 		((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field = (val); \
218b843c749SSergey Zigachev 	} while (0)
219b843c749SSergey Zigachev 
220b843c749SSergey Zigachev #define AMDGPU_FW_VRAM_VF2PF_READ(adev, field, val) \
221b843c749SSergey Zigachev 	do { \
222b843c749SSergey Zigachev 		(*val) = ((amdgim_vf2pf_info *)adev->virt.fw_reserve.p_vf2pf)->field; \
223b843c749SSergey Zigachev 	} while (0)
224b843c749SSergey Zigachev 
225b843c749SSergey Zigachev #define AMDGPU_FW_VRAM_PF2VF_READ(adev, field, val) \
226b843c749SSergey Zigachev 	do { \
227b843c749SSergey Zigachev 		if (!adev->virt.fw_reserve.p_pf2vf) \
228b843c749SSergey Zigachev 			*(val) = 0; \
229b843c749SSergey Zigachev 		else { \
230b843c749SSergey Zigachev 			if (adev->virt.fw_reserve.p_pf2vf->version == 1) \
231b843c749SSergey Zigachev 				*(val) = ((struct amdgim_pf2vf_info_v1 *)adev->virt.fw_reserve.p_pf2vf)->field; \
232b843c749SSergey Zigachev 			if (adev->virt.fw_reserve.p_pf2vf->version == 2) \
233b843c749SSergey Zigachev 				*(val) = ((struct amdgim_pf2vf_info_v2 *)adev->virt.fw_reserve.p_pf2vf)->field; \
234b843c749SSergey Zigachev 		} \
235b843c749SSergey Zigachev 	} while (0)
236b843c749SSergey Zigachev 
237b843c749SSergey Zigachev /* GPU virtualization */
238b843c749SSergey Zigachev struct amdgpu_virt {
239b843c749SSergey Zigachev 	uint32_t			caps;
240b843c749SSergey Zigachev 	struct amdgpu_bo		*csa_obj;
241b843c749SSergey Zigachev 	uint64_t			csa_vmid0_addr;
242b843c749SSergey Zigachev 	bool chained_ib_support;
243b843c749SSergey Zigachev 	uint32_t			reg_val_offs;
244b843c749SSergey Zigachev 	struct amdgpu_irq_src		ack_irq;
245b843c749SSergey Zigachev 	struct amdgpu_irq_src		rcv_irq;
246b843c749SSergey Zigachev 	struct work_struct		flr_work;
247b843c749SSergey Zigachev 	struct amdgpu_mm_table		mm_table;
248b843c749SSergey Zigachev 	const struct amdgpu_virt_ops	*ops;
249b843c749SSergey Zigachev 	struct amdgpu_vf_error_buffer   vf_errors;
250b843c749SSergey Zigachev 	struct amdgpu_virt_fw_reserve	fw_reserve;
251b843c749SSergey Zigachev 	uint32_t gim_feature;
252b843c749SSergey Zigachev };
253b843c749SSergey Zigachev 
254b843c749SSergey Zigachev #define AMDGPU_CSA_SIZE		(8 * 1024)
255b843c749SSergey Zigachev 
256b843c749SSergey Zigachev #define amdgpu_sriov_enabled(adev) \
257b843c749SSergey Zigachev ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_ENABLE_IOV)
258b843c749SSergey Zigachev 
259b843c749SSergey Zigachev #define amdgpu_sriov_vf(adev) \
260b843c749SSergey Zigachev ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_IS_VF)
261b843c749SSergey Zigachev 
262b843c749SSergey Zigachev #define amdgpu_sriov_bios(adev) \
263b843c749SSergey Zigachev ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS)
264b843c749SSergey Zigachev 
265b843c749SSergey Zigachev #define amdgpu_sriov_runtime(adev) \
266b843c749SSergey Zigachev ((adev)->virt.caps & AMDGPU_SRIOV_CAPS_RUNTIME)
267b843c749SSergey Zigachev 
268b843c749SSergey Zigachev #define amdgpu_passthrough(adev) \
269b843c749SSergey Zigachev ((adev)->virt.caps & AMDGPU_PASSTHROUGH_MODE)
270b843c749SSergey Zigachev 
is_virtual_machine(void)271b843c749SSergey Zigachev static inline bool is_virtual_machine(void)
272b843c749SSergey Zigachev {
273b843c749SSergey Zigachev #ifdef CONFIG_X86
274b843c749SSergey Zigachev 	return boot_cpu_has(X86_FEATURE_HYPERVISOR);
275b843c749SSergey Zigachev #else
276b843c749SSergey Zigachev 	return false;
277b843c749SSergey Zigachev #endif
278b843c749SSergey Zigachev }
279b843c749SSergey Zigachev 
280b843c749SSergey Zigachev struct amdgpu_vm;
281b843c749SSergey Zigachev 
282b843c749SSergey Zigachev uint64_t amdgpu_csa_vaddr(struct amdgpu_device *adev);
283b843c749SSergey Zigachev bool amdgpu_virt_mmio_blocked(struct amdgpu_device *adev);
284b843c749SSergey Zigachev int amdgpu_allocate_static_csa(struct amdgpu_device *adev);
285b843c749SSergey Zigachev int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm,
286b843c749SSergey Zigachev 			  struct amdgpu_bo_va **bo_va);
287b843c749SSergey Zigachev void amdgpu_free_static_csa(struct amdgpu_device *adev);
288b843c749SSergey Zigachev void amdgpu_virt_init_setting(struct amdgpu_device *adev);
289b843c749SSergey Zigachev uint32_t amdgpu_virt_kiq_rreg(struct amdgpu_device *adev, uint32_t reg);
290b843c749SSergey Zigachev void amdgpu_virt_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v);
291b843c749SSergey Zigachev int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init);
292b843c749SSergey Zigachev int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init);
293b843c749SSergey Zigachev int amdgpu_virt_reset_gpu(struct amdgpu_device *adev);
294b843c749SSergey Zigachev int amdgpu_virt_wait_reset(struct amdgpu_device *adev);
295b843c749SSergey Zigachev int amdgpu_virt_alloc_mm_table(struct amdgpu_device *adev);
296b843c749SSergey Zigachev void amdgpu_virt_free_mm_table(struct amdgpu_device *adev);
297b843c749SSergey Zigachev int amdgpu_virt_fw_reserve_get_checksum(void *obj, unsigned long obj_size,
298b843c749SSergey Zigachev 					unsigned int key,
299b843c749SSergey Zigachev 					unsigned int chksum);
300b843c749SSergey Zigachev void amdgpu_virt_init_data_exchange(struct amdgpu_device *adev);
301b843c749SSergey Zigachev 
302b843c749SSergey Zigachev #endif
303