This source file includes following definitions.
- amdgpu_virt_mmio_blocked
- amdgpu_virt_init_setting
- amdgpu_virt_kiq_rreg
- amdgpu_virt_kiq_wreg
- amdgpu_virt_kiq_reg_write_reg_wait
- amdgpu_virt_request_full_gpu
- amdgpu_virt_release_full_gpu
- amdgpu_virt_reset_gpu
- amdgpu_virt_wait_reset
- amdgpu_virt_alloc_mm_table
- amdgpu_virt_free_mm_table
- amdgpu_virt_fw_reserve_get_checksum
- amdgpu_virt_init_data_exchange
- parse_clk
- amdgpu_virt_get_sclk
- amdgpu_virt_get_mclk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #include <linux/module.h>
25
26 #include <drm/drm_drv.h>
27
28 #include "amdgpu.h"
29
30 bool amdgpu_virt_mmio_blocked(struct amdgpu_device *adev)
31 {
32
33
34
35 return RREG32_NO_KIQ(0xc040) == 0xffffffff;
36 }
37
38 void amdgpu_virt_init_setting(struct amdgpu_device *adev)
39 {
40
41 adev->mode_info.num_crtc = 1;
42 adev->enable_virtual_display = true;
43 adev->ddev->driver->driver_features &= ~DRIVER_ATOMIC;
44 adev->cg_flags = 0;
45 adev->pg_flags = 0;
46 }
47
48 uint32_t amdgpu_virt_kiq_rreg(struct amdgpu_device *adev, uint32_t reg)
49 {
50 signed long r, cnt = 0;
51 unsigned long flags;
52 uint32_t seq;
53 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
54 struct amdgpu_ring *ring = &kiq->ring;
55
56 BUG_ON(!ring->funcs->emit_rreg);
57
58 spin_lock_irqsave(&kiq->ring_lock, flags);
59 amdgpu_ring_alloc(ring, 32);
60 amdgpu_ring_emit_rreg(ring, reg);
61 amdgpu_fence_emit_polling(ring, &seq);
62 amdgpu_ring_commit(ring);
63 spin_unlock_irqrestore(&kiq->ring_lock, flags);
64
65 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
66
67
68
69
70
71
72
73
74
75 if (r < 1 && (adev->in_gpu_reset || in_interrupt()))
76 goto failed_kiq_read;
77
78 might_sleep();
79 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
80 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
81 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
82 }
83
84 if (cnt > MAX_KIQ_REG_TRY)
85 goto failed_kiq_read;
86
87 return adev->wb.wb[adev->virt.reg_val_offs];
88
89 failed_kiq_read:
90 pr_err("failed to read reg:%x\n", reg);
91 return ~0;
92 }
93
94 void amdgpu_virt_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
95 {
96 signed long r, cnt = 0;
97 unsigned long flags;
98 uint32_t seq;
99 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
100 struct amdgpu_ring *ring = &kiq->ring;
101
102 BUG_ON(!ring->funcs->emit_wreg);
103
104 spin_lock_irqsave(&kiq->ring_lock, flags);
105 amdgpu_ring_alloc(ring, 32);
106 amdgpu_ring_emit_wreg(ring, reg, v);
107 amdgpu_fence_emit_polling(ring, &seq);
108 amdgpu_ring_commit(ring);
109 spin_unlock_irqrestore(&kiq->ring_lock, flags);
110
111 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
112
113
114
115
116
117
118
119
120
121 if (r < 1 && (adev->in_gpu_reset || in_interrupt()))
122 goto failed_kiq_write;
123
124 might_sleep();
125 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
126
127 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
128 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
129 }
130
131 if (cnt > MAX_KIQ_REG_TRY)
132 goto failed_kiq_write;
133
134 return;
135
136 failed_kiq_write:
137 pr_err("failed to write reg:%x\n", reg);
138 }
139
140 void amdgpu_virt_kiq_reg_write_reg_wait(struct amdgpu_device *adev,
141 uint32_t reg0, uint32_t reg1,
142 uint32_t ref, uint32_t mask)
143 {
144 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
145 struct amdgpu_ring *ring = &kiq->ring;
146 signed long r, cnt = 0;
147 unsigned long flags;
148 uint32_t seq;
149
150 spin_lock_irqsave(&kiq->ring_lock, flags);
151 amdgpu_ring_alloc(ring, 32);
152 amdgpu_ring_emit_reg_write_reg_wait(ring, reg0, reg1,
153 ref, mask);
154 amdgpu_fence_emit_polling(ring, &seq);
155 amdgpu_ring_commit(ring);
156 spin_unlock_irqrestore(&kiq->ring_lock, flags);
157
158 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
159
160
161 if (r < 1 && in_interrupt())
162 goto failed_kiq;
163
164 might_sleep();
165 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
166
167 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
168 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
169 }
170
171 if (cnt > MAX_KIQ_REG_TRY)
172 goto failed_kiq;
173
174 return;
175
176 failed_kiq:
177 pr_err("failed to write reg %x wait reg %x\n", reg0, reg1);
178 }
179
180
181
182
183
184
185
186
187 int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init)
188 {
189 struct amdgpu_virt *virt = &adev->virt;
190 int r;
191
192 if (virt->ops && virt->ops->req_full_gpu) {
193 r = virt->ops->req_full_gpu(adev, init);
194 if (r)
195 return r;
196
197 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
198 }
199
200 return 0;
201 }
202
203
204
205
206
207
208
209
210 int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init)
211 {
212 struct amdgpu_virt *virt = &adev->virt;
213 int r;
214
215 if (virt->ops && virt->ops->rel_full_gpu) {
216 r = virt->ops->rel_full_gpu(adev, init);
217 if (r)
218 return r;
219
220 adev->virt.caps |= AMDGPU_SRIOV_CAPS_RUNTIME;
221 }
222 return 0;
223 }
224
225
226
227
228
229
230
231 int amdgpu_virt_reset_gpu(struct amdgpu_device *adev)
232 {
233 struct amdgpu_virt *virt = &adev->virt;
234 int r;
235
236 if (virt->ops && virt->ops->reset_gpu) {
237 r = virt->ops->reset_gpu(adev);
238 if (r)
239 return r;
240
241 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
242 }
243
244 return 0;
245 }
246
247
248
249
250
251
252
253 int amdgpu_virt_wait_reset(struct amdgpu_device *adev)
254 {
255 struct amdgpu_virt *virt = &adev->virt;
256
257 if (!virt->ops || !virt->ops->wait_reset)
258 return -EINVAL;
259
260 return virt->ops->wait_reset(adev);
261 }
262
263
264
265
266
267
268
269 int amdgpu_virt_alloc_mm_table(struct amdgpu_device *adev)
270 {
271 int r;
272
273 if (!amdgpu_sriov_vf(adev) || adev->virt.mm_table.gpu_addr)
274 return 0;
275
276 r = amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
277 AMDGPU_GEM_DOMAIN_VRAM,
278 &adev->virt.mm_table.bo,
279 &adev->virt.mm_table.gpu_addr,
280 (void *)&adev->virt.mm_table.cpu_addr);
281 if (r) {
282 DRM_ERROR("failed to alloc mm table and error = %d.\n", r);
283 return r;
284 }
285
286 memset((void *)adev->virt.mm_table.cpu_addr, 0, PAGE_SIZE);
287 DRM_INFO("MM table gpu addr = 0x%llx, cpu addr = %p.\n",
288 adev->virt.mm_table.gpu_addr,
289 adev->virt.mm_table.cpu_addr);
290 return 0;
291 }
292
293
294
295
296
297
298 void amdgpu_virt_free_mm_table(struct amdgpu_device *adev)
299 {
300 if (!amdgpu_sriov_vf(adev) || !adev->virt.mm_table.gpu_addr)
301 return;
302
303 amdgpu_bo_free_kernel(&adev->virt.mm_table.bo,
304 &adev->virt.mm_table.gpu_addr,
305 (void *)&adev->virt.mm_table.cpu_addr);
306 adev->virt.mm_table.gpu_addr = 0;
307 }
308
309
310 int amdgpu_virt_fw_reserve_get_checksum(void *obj,
311 unsigned long obj_size,
312 unsigned int key,
313 unsigned int chksum)
314 {
315 unsigned int ret = key;
316 unsigned long i = 0;
317 unsigned char *pos;
318
319 pos = (char *)obj;
320
321 for (i = 0; i < obj_size; ++i)
322 ret += *(pos + i);
323
324 pos = (char *)&chksum;
325 for (i = 0; i < sizeof(chksum); ++i)
326 ret -= *(pos + i);
327 return ret;
328 }
329
330 void amdgpu_virt_init_data_exchange(struct amdgpu_device *adev)
331 {
332 uint32_t pf2vf_size = 0;
333 uint32_t checksum = 0;
334 uint32_t checkval;
335 char *str;
336
337 adev->virt.fw_reserve.p_pf2vf = NULL;
338 adev->virt.fw_reserve.p_vf2pf = NULL;
339
340 if (adev->fw_vram_usage.va != NULL) {
341 adev->virt.fw_reserve.p_pf2vf =
342 (struct amd_sriov_msg_pf2vf_info_header *)(
343 adev->fw_vram_usage.va + AMDGIM_DATAEXCHANGE_OFFSET);
344 AMDGPU_FW_VRAM_PF2VF_READ(adev, header.size, &pf2vf_size);
345 AMDGPU_FW_VRAM_PF2VF_READ(adev, checksum, &checksum);
346 AMDGPU_FW_VRAM_PF2VF_READ(adev, feature_flags, &adev->virt.gim_feature);
347
348
349 if (pf2vf_size > 0 && pf2vf_size < 4096) {
350 checkval = amdgpu_virt_fw_reserve_get_checksum(
351 adev->virt.fw_reserve.p_pf2vf, pf2vf_size,
352 adev->virt.fw_reserve.checksum_key, checksum);
353 if (checkval == checksum) {
354 adev->virt.fw_reserve.p_vf2pf =
355 ((void *)adev->virt.fw_reserve.p_pf2vf +
356 pf2vf_size);
357 memset((void *)adev->virt.fw_reserve.p_vf2pf, 0,
358 sizeof(amdgim_vf2pf_info));
359 AMDGPU_FW_VRAM_VF2PF_WRITE(adev, header.version,
360 AMDGPU_FW_VRAM_VF2PF_VER);
361 AMDGPU_FW_VRAM_VF2PF_WRITE(adev, header.size,
362 sizeof(amdgim_vf2pf_info));
363 AMDGPU_FW_VRAM_VF2PF_READ(adev, driver_version,
364 &str);
365 #ifdef MODULE
366 if (THIS_MODULE->version != NULL)
367 strcpy(str, THIS_MODULE->version);
368 else
369 #endif
370 strcpy(str, "N/A");
371 AMDGPU_FW_VRAM_VF2PF_WRITE(adev, driver_cert,
372 0);
373 AMDGPU_FW_VRAM_VF2PF_WRITE(adev, checksum,
374 amdgpu_virt_fw_reserve_get_checksum(
375 adev->virt.fw_reserve.p_vf2pf,
376 pf2vf_size,
377 adev->virt.fw_reserve.checksum_key, 0));
378 }
379 }
380 }
381 }
382
383 static uint32_t parse_clk(char *buf, bool min)
384 {
385 char *ptr = buf;
386 uint32_t clk = 0;
387
388 do {
389 ptr = strchr(ptr, ':');
390 if (!ptr)
391 break;
392 ptr+=2;
393 if (kstrtou32(ptr, 10, &clk))
394 return 0;
395 } while (!min);
396
397 return clk * 100;
398 }
399
400 uint32_t amdgpu_virt_get_sclk(struct amdgpu_device *adev, bool lowest)
401 {
402 char *buf = NULL;
403 uint32_t clk = 0;
404
405 buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
406 if (!buf)
407 return -ENOMEM;
408
409 adev->virt.ops->get_pp_clk(adev, PP_SCLK, buf);
410 clk = parse_clk(buf, lowest);
411
412 kfree(buf);
413
414 return clk;
415 }
416
417 uint32_t amdgpu_virt_get_mclk(struct amdgpu_device *adev, bool lowest)
418 {
419 char *buf = NULL;
420 uint32_t clk = 0;
421
422 buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
423 if (!buf)
424 return -ENOMEM;
425
426 adev->virt.ops->get_pp_clk(adev, PP_MCLK, buf);
427 clk = parse_clk(buf, lowest);
428
429 kfree(buf);
430
431 return clk;
432 }