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