1 /*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26 #include <linux/firmware.h>
27 #include <linux/pm_runtime.h>
28
29 #include "amdgpu.h"
30 #include "amdgpu_gfx.h"
31 #include "amdgpu_rlc.h"
32 #include "amdgpu_ras.h"
33 #include "amdgpu_reset.h"
34 #include "amdgpu_xcp.h"
35 #include "amdgpu_xgmi.h"
36
37 /* delay 0.1 second to enable gfx off feature */
38 #define GFX_OFF_DELAY_ENABLE msecs_to_jiffies(100)
39
40 #define GFX_OFF_NO_DELAY 0
41
42 /*
43 * GPU GFX IP block helpers function.
44 */
45
amdgpu_gfx_mec_queue_to_bit(struct amdgpu_device * adev,int mec,int pipe,int queue)46 int amdgpu_gfx_mec_queue_to_bit(struct amdgpu_device *adev, int mec,
47 int pipe, int queue)
48 {
49 int bit = 0;
50
51 bit += mec * adev->gfx.mec.num_pipe_per_mec
52 * adev->gfx.mec.num_queue_per_pipe;
53 bit += pipe * adev->gfx.mec.num_queue_per_pipe;
54 bit += queue;
55
56 return bit;
57 }
58
amdgpu_queue_mask_bit_to_mec_queue(struct amdgpu_device * adev,int bit,int * mec,int * pipe,int * queue)59 void amdgpu_queue_mask_bit_to_mec_queue(struct amdgpu_device *adev, int bit,
60 int *mec, int *pipe, int *queue)
61 {
62 *queue = bit % adev->gfx.mec.num_queue_per_pipe;
63 *pipe = (bit / adev->gfx.mec.num_queue_per_pipe)
64 % adev->gfx.mec.num_pipe_per_mec;
65 *mec = (bit / adev->gfx.mec.num_queue_per_pipe)
66 / adev->gfx.mec.num_pipe_per_mec;
67
68 }
69
amdgpu_gfx_is_mec_queue_enabled(struct amdgpu_device * adev,int xcc_id,int mec,int pipe,int queue)70 bool amdgpu_gfx_is_mec_queue_enabled(struct amdgpu_device *adev,
71 int xcc_id, int mec, int pipe, int queue)
72 {
73 return test_bit(amdgpu_gfx_mec_queue_to_bit(adev, mec, pipe, queue),
74 adev->gfx.mec_bitmap[xcc_id].queue_bitmap);
75 }
76
amdgpu_gfx_me_queue_to_bit(struct amdgpu_device * adev,int me,int pipe,int queue)77 int amdgpu_gfx_me_queue_to_bit(struct amdgpu_device *adev,
78 int me, int pipe, int queue)
79 {
80 int bit = 0;
81
82 bit += me * adev->gfx.me.num_pipe_per_me
83 * adev->gfx.me.num_queue_per_pipe;
84 bit += pipe * adev->gfx.me.num_queue_per_pipe;
85 bit += queue;
86
87 return bit;
88 }
89
amdgpu_gfx_is_me_queue_enabled(struct amdgpu_device * adev,int me,int pipe,int queue)90 bool amdgpu_gfx_is_me_queue_enabled(struct amdgpu_device *adev,
91 int me, int pipe, int queue)
92 {
93 return test_bit(amdgpu_gfx_me_queue_to_bit(adev, me, pipe, queue),
94 adev->gfx.me.queue_bitmap);
95 }
96
97 /**
98 * amdgpu_gfx_parse_disable_cu - Parse the disable_cu module parameter
99 *
100 * @mask: array in which the per-shader array disable masks will be stored
101 * @max_se: number of SEs
102 * @max_sh: number of SHs
103 *
104 * The bitmask of CUs to be disabled in the shader array determined by se and
105 * sh is stored in mask[se * max_sh + sh].
106 */
amdgpu_gfx_parse_disable_cu(unsigned int * mask,unsigned int max_se,unsigned int max_sh)107 void amdgpu_gfx_parse_disable_cu(unsigned int *mask, unsigned int max_se, unsigned int max_sh)
108 {
109 unsigned int se, sh, cu;
110 const char *p;
111
112 memset(mask, 0, sizeof(*mask) * max_se * max_sh);
113
114 if (!amdgpu_disable_cu || !*amdgpu_disable_cu)
115 return;
116
117 p = amdgpu_disable_cu;
118 for (;;) {
119 char *next;
120 int ret = sscanf(p, "%u.%u.%u", &se, &sh, &cu);
121
122 if (ret < 3) {
123 DRM_ERROR("amdgpu: could not parse disable_cu\n");
124 return;
125 }
126
127 if (se < max_se && sh < max_sh && cu < 16) {
128 DRM_INFO("amdgpu: disabling CU %u.%u.%u\n", se, sh, cu);
129 mask[se * max_sh + sh] |= 1u << cu;
130 } else {
131 DRM_ERROR("amdgpu: disable_cu %u.%u.%u is out of range\n",
132 se, sh, cu);
133 }
134
135 next = strchr(p, ',');
136 if (!next)
137 break;
138 p = next + 1;
139 }
140 }
141
amdgpu_gfx_is_graphics_multipipe_capable(struct amdgpu_device * adev)142 static bool amdgpu_gfx_is_graphics_multipipe_capable(struct amdgpu_device *adev)
143 {
144 return amdgpu_async_gfx_ring && adev->gfx.me.num_pipe_per_me > 1;
145 }
146
amdgpu_gfx_is_compute_multipipe_capable(struct amdgpu_device * adev)147 static bool amdgpu_gfx_is_compute_multipipe_capable(struct amdgpu_device *adev)
148 {
149 if (amdgpu_compute_multipipe != -1) {
150 DRM_INFO("amdgpu: forcing compute pipe policy %d\n",
151 amdgpu_compute_multipipe);
152 return amdgpu_compute_multipipe == 1;
153 }
154
155 if (amdgpu_ip_version(adev, GC_HWIP, 0) > IP_VERSION(9, 0, 0))
156 return true;
157
158 /* FIXME: spreading the queues across pipes causes perf regressions
159 * on POLARIS11 compute workloads */
160 if (adev->asic_type == CHIP_POLARIS11)
161 return false;
162
163 return adev->gfx.mec.num_mec > 1;
164 }
165
amdgpu_gfx_is_high_priority_graphics_queue(struct amdgpu_device * adev,struct amdgpu_ring * ring)166 bool amdgpu_gfx_is_high_priority_graphics_queue(struct amdgpu_device *adev,
167 struct amdgpu_ring *ring)
168 {
169 int queue = ring->queue;
170 int pipe = ring->pipe;
171
172 /* Policy: use pipe1 queue0 as high priority graphics queue if we
173 * have more than one gfx pipe.
174 */
175 if (amdgpu_gfx_is_graphics_multipipe_capable(adev) &&
176 adev->gfx.num_gfx_rings > 1 && pipe == 1 && queue == 0) {
177 int me = ring->me;
178 int bit;
179
180 bit = amdgpu_gfx_me_queue_to_bit(adev, me, pipe, queue);
181 if (ring == &adev->gfx.gfx_ring[bit])
182 return true;
183 }
184
185 return false;
186 }
187
amdgpu_gfx_is_high_priority_compute_queue(struct amdgpu_device * adev,struct amdgpu_ring * ring)188 bool amdgpu_gfx_is_high_priority_compute_queue(struct amdgpu_device *adev,
189 struct amdgpu_ring *ring)
190 {
191 /* Policy: use 1st queue as high priority compute queue if we
192 * have more than one compute queue.
193 */
194 if (adev->gfx.num_compute_rings > 1 &&
195 ring == &adev->gfx.compute_ring[0])
196 return true;
197
198 return false;
199 }
200
amdgpu_gfx_compute_queue_acquire(struct amdgpu_device * adev)201 void amdgpu_gfx_compute_queue_acquire(struct amdgpu_device *adev)
202 {
203 int i, j, queue, pipe;
204 bool multipipe_policy = amdgpu_gfx_is_compute_multipipe_capable(adev);
205 int max_queues_per_mec = min(adev->gfx.mec.num_pipe_per_mec *
206 adev->gfx.mec.num_queue_per_pipe,
207 adev->gfx.num_compute_rings);
208 int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
209
210 if (multipipe_policy) {
211 /* policy: make queues evenly cross all pipes on MEC1 only
212 * for multiple xcc, just use the original policy for simplicity */
213 for (j = 0; j < num_xcc; j++) {
214 for (i = 0; i < max_queues_per_mec; i++) {
215 pipe = i % adev->gfx.mec.num_pipe_per_mec;
216 queue = (i / adev->gfx.mec.num_pipe_per_mec) %
217 adev->gfx.mec.num_queue_per_pipe;
218
219 set_bit(pipe * adev->gfx.mec.num_queue_per_pipe + queue,
220 adev->gfx.mec_bitmap[j].queue_bitmap);
221 }
222 }
223 } else {
224 /* policy: amdgpu owns all queues in the given pipe */
225 for (j = 0; j < num_xcc; j++) {
226 for (i = 0; i < max_queues_per_mec; ++i)
227 set_bit(i, adev->gfx.mec_bitmap[j].queue_bitmap);
228 }
229 }
230
231 for (j = 0; j < num_xcc; j++) {
232 dev_dbg(adev->dev, "mec queue bitmap weight=%d\n",
233 bitmap_weight(adev->gfx.mec_bitmap[j].queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES));
234 }
235 }
236
amdgpu_gfx_graphics_queue_acquire(struct amdgpu_device * adev)237 void amdgpu_gfx_graphics_queue_acquire(struct amdgpu_device *adev)
238 {
239 int i, queue, pipe;
240 bool multipipe_policy = amdgpu_gfx_is_graphics_multipipe_capable(adev);
241 int max_queues_per_me = adev->gfx.me.num_pipe_per_me *
242 adev->gfx.me.num_queue_per_pipe;
243
244 if (multipipe_policy) {
245 /* policy: amdgpu owns the first queue per pipe at this stage
246 * will extend to mulitple queues per pipe later */
247 for (i = 0; i < max_queues_per_me; i++) {
248 pipe = i % adev->gfx.me.num_pipe_per_me;
249 queue = (i / adev->gfx.me.num_pipe_per_me) %
250 adev->gfx.me.num_queue_per_pipe;
251
252 set_bit(pipe * adev->gfx.me.num_queue_per_pipe + queue,
253 adev->gfx.me.queue_bitmap);
254 }
255 } else {
256 for (i = 0; i < max_queues_per_me; ++i)
257 set_bit(i, adev->gfx.me.queue_bitmap);
258 }
259
260 /* update the number of active graphics rings */
261 adev->gfx.num_gfx_rings =
262 bitmap_weight(adev->gfx.me.queue_bitmap, AMDGPU_MAX_GFX_QUEUES);
263 }
264
amdgpu_gfx_kiq_acquire(struct amdgpu_device * adev,struct amdgpu_ring * ring,int xcc_id)265 static int amdgpu_gfx_kiq_acquire(struct amdgpu_device *adev,
266 struct amdgpu_ring *ring, int xcc_id)
267 {
268 int queue_bit;
269 int mec, pipe, queue;
270
271 queue_bit = adev->gfx.mec.num_mec
272 * adev->gfx.mec.num_pipe_per_mec
273 * adev->gfx.mec.num_queue_per_pipe;
274
275 while (--queue_bit >= 0) {
276 if (test_bit(queue_bit, adev->gfx.mec_bitmap[xcc_id].queue_bitmap))
277 continue;
278
279 amdgpu_queue_mask_bit_to_mec_queue(adev, queue_bit, &mec, &pipe, &queue);
280
281 /*
282 * 1. Using pipes 2/3 from MEC 2 seems cause problems.
283 * 2. It must use queue id 0, because CGPG_IDLE/SAVE/LOAD/RUN
284 * only can be issued on queue 0.
285 */
286 if ((mec == 1 && pipe > 1) || queue != 0)
287 continue;
288
289 ring->me = mec + 1;
290 ring->pipe = pipe;
291 ring->queue = queue;
292
293 return 0;
294 }
295
296 dev_err(adev->dev, "Failed to find a queue for KIQ\n");
297 return -EINVAL;
298 }
299
amdgpu_gfx_kiq_init_ring(struct amdgpu_device * adev,int xcc_id)300 int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev, int xcc_id)
301 {
302 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
303 struct amdgpu_irq_src *irq = &kiq->irq;
304 struct amdgpu_ring *ring = &kiq->ring;
305 int r = 0;
306
307 spin_lock_init(&kiq->ring_lock);
308
309 ring->adev = NULL;
310 ring->ring_obj = NULL;
311 ring->use_doorbell = true;
312 ring->xcc_id = xcc_id;
313 ring->vm_hub = AMDGPU_GFXHUB(xcc_id);
314 ring->doorbell_index =
315 (adev->doorbell_index.kiq +
316 xcc_id * adev->doorbell_index.xcc_doorbell_range)
317 << 1;
318
319 r = amdgpu_gfx_kiq_acquire(adev, ring, xcc_id);
320 if (r)
321 return r;
322
323 ring->eop_gpu_addr = kiq->eop_gpu_addr;
324 ring->no_scheduler = true;
325 snprintf(ring->name, sizeof(ring->name), "kiq_%hhu.%hhu.%hhu.%hhu",
326 (unsigned char)xcc_id, (unsigned char)ring->me,
327 (unsigned char)ring->pipe, (unsigned char)ring->queue);
328 r = amdgpu_ring_init(adev, ring, 1024, irq, AMDGPU_CP_KIQ_IRQ_DRIVER0,
329 AMDGPU_RING_PRIO_DEFAULT, NULL);
330 if (r)
331 dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
332
333 return r;
334 }
335
amdgpu_gfx_kiq_free_ring(struct amdgpu_ring * ring)336 void amdgpu_gfx_kiq_free_ring(struct amdgpu_ring *ring)
337 {
338 amdgpu_ring_fini(ring);
339 }
340
amdgpu_gfx_kiq_fini(struct amdgpu_device * adev,int xcc_id)341 void amdgpu_gfx_kiq_fini(struct amdgpu_device *adev, int xcc_id)
342 {
343 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
344
345 amdgpu_bo_free_kernel(&kiq->eop_obj, &kiq->eop_gpu_addr, NULL);
346 }
347
amdgpu_gfx_kiq_init(struct amdgpu_device * adev,unsigned int hpd_size,int xcc_id)348 int amdgpu_gfx_kiq_init(struct amdgpu_device *adev,
349 unsigned int hpd_size, int xcc_id)
350 {
351 int r;
352 u32 *hpd;
353 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
354
355 r = amdgpu_bo_create_kernel(adev, hpd_size, PAGE_SIZE,
356 AMDGPU_GEM_DOMAIN_GTT, &kiq->eop_obj,
357 &kiq->eop_gpu_addr, (void **)&hpd);
358 if (r) {
359 dev_warn(adev->dev, "failed to create KIQ bo (%d).\n", r);
360 return r;
361 }
362
363 memset(hpd, 0, hpd_size);
364
365 r = amdgpu_bo_reserve(kiq->eop_obj, true);
366 if (unlikely(r != 0))
367 dev_warn(adev->dev, "(%d) reserve kiq eop bo failed\n", r);
368 amdgpu_bo_kunmap(kiq->eop_obj);
369 amdgpu_bo_unreserve(kiq->eop_obj);
370
371 return 0;
372 }
373
374 /* create MQD for each compute/gfx queue */
amdgpu_gfx_mqd_sw_init(struct amdgpu_device * adev,unsigned int mqd_size,int xcc_id)375 int amdgpu_gfx_mqd_sw_init(struct amdgpu_device *adev,
376 unsigned int mqd_size, int xcc_id)
377 {
378 int r, i, j;
379 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
380 struct amdgpu_ring *ring = &kiq->ring;
381 u32 domain = AMDGPU_GEM_DOMAIN_GTT;
382
383 #if !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
384 /* Only enable on gfx10 and 11 for now to avoid changing behavior on older chips */
385 if (amdgpu_ip_version(adev, GC_HWIP, 0) >= IP_VERSION(10, 0, 0))
386 domain |= AMDGPU_GEM_DOMAIN_VRAM;
387 #endif
388
389 /* create MQD for KIQ */
390 if (!adev->enable_mes_kiq && !ring->mqd_obj) {
391 /* originaly the KIQ MQD is put in GTT domain, but for SRIOV VRAM domain is a must
392 * otherwise hypervisor trigger SAVE_VF fail after driver unloaded which mean MQD
393 * deallocated and gart_unbind, to strict diverage we decide to use VRAM domain for
394 * KIQ MQD no matter SRIOV or Bare-metal
395 */
396 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
397 AMDGPU_GEM_DOMAIN_VRAM |
398 AMDGPU_GEM_DOMAIN_GTT,
399 &ring->mqd_obj,
400 &ring->mqd_gpu_addr,
401 &ring->mqd_ptr);
402 if (r) {
403 dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
404 return r;
405 }
406
407 /* prepare MQD backup */
408 kiq->mqd_backup = kzalloc(mqd_size, GFP_KERNEL);
409 if (!kiq->mqd_backup) {
410 dev_warn(adev->dev,
411 "no memory to create MQD backup for ring %s\n", ring->name);
412 return -ENOMEM;
413 }
414 }
415
416 if (adev->asic_type >= CHIP_NAVI10 && amdgpu_async_gfx_ring) {
417 /* create MQD for each KGQ */
418 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
419 ring = &adev->gfx.gfx_ring[i];
420 if (!ring->mqd_obj) {
421 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
422 domain, &ring->mqd_obj,
423 &ring->mqd_gpu_addr, &ring->mqd_ptr);
424 if (r) {
425 dev_warn(adev->dev, "failed to create ring mqd bo (%d)", r);
426 return r;
427 }
428
429 ring->mqd_size = mqd_size;
430 /* prepare MQD backup */
431 adev->gfx.me.mqd_backup[i] = kzalloc(mqd_size, GFP_KERNEL);
432 if (!adev->gfx.me.mqd_backup[i]) {
433 dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
434 return -ENOMEM;
435 }
436 }
437 }
438 }
439
440 /* create MQD for each KCQ */
441 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
442 j = i + xcc_id * adev->gfx.num_compute_rings;
443 ring = &adev->gfx.compute_ring[j];
444 if (!ring->mqd_obj) {
445 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
446 domain, &ring->mqd_obj,
447 &ring->mqd_gpu_addr, &ring->mqd_ptr);
448 if (r) {
449 dev_warn(adev->dev, "failed to create ring mqd bo (%d)", r);
450 return r;
451 }
452
453 ring->mqd_size = mqd_size;
454 /* prepare MQD backup */
455 adev->gfx.mec.mqd_backup[j] = kzalloc(mqd_size, GFP_KERNEL);
456 if (!adev->gfx.mec.mqd_backup[j]) {
457 dev_warn(adev->dev, "no memory to create MQD backup for ring %s\n", ring->name);
458 return -ENOMEM;
459 }
460 }
461 }
462
463 return 0;
464 }
465
amdgpu_gfx_mqd_sw_fini(struct amdgpu_device * adev,int xcc_id)466 void amdgpu_gfx_mqd_sw_fini(struct amdgpu_device *adev, int xcc_id)
467 {
468 struct amdgpu_ring *ring = NULL;
469 int i, j;
470 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
471
472 if (adev->asic_type >= CHIP_NAVI10 && amdgpu_async_gfx_ring) {
473 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
474 ring = &adev->gfx.gfx_ring[i];
475 kfree(adev->gfx.me.mqd_backup[i]);
476 amdgpu_bo_free_kernel(&ring->mqd_obj,
477 &ring->mqd_gpu_addr,
478 &ring->mqd_ptr);
479 }
480 }
481
482 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
483 j = i + xcc_id * adev->gfx.num_compute_rings;
484 ring = &adev->gfx.compute_ring[j];
485 kfree(adev->gfx.mec.mqd_backup[j]);
486 amdgpu_bo_free_kernel(&ring->mqd_obj,
487 &ring->mqd_gpu_addr,
488 &ring->mqd_ptr);
489 }
490
491 ring = &kiq->ring;
492 kfree(kiq->mqd_backup);
493 amdgpu_bo_free_kernel(&ring->mqd_obj,
494 &ring->mqd_gpu_addr,
495 &ring->mqd_ptr);
496 }
497
amdgpu_gfx_disable_kcq(struct amdgpu_device * adev,int xcc_id)498 int amdgpu_gfx_disable_kcq(struct amdgpu_device *adev, int xcc_id)
499 {
500 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
501 struct amdgpu_ring *kiq_ring = &kiq->ring;
502 int i, r = 0;
503 int j;
504
505 if (adev->enable_mes) {
506 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
507 j = i + xcc_id * adev->gfx.num_compute_rings;
508 amdgpu_mes_unmap_legacy_queue(adev,
509 &adev->gfx.compute_ring[j],
510 RESET_QUEUES, 0, 0);
511 }
512 return 0;
513 }
514
515 if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
516 return -EINVAL;
517
518 if (!kiq_ring->sched.ready || amdgpu_in_reset(adev))
519 return 0;
520
521 spin_lock(&kiq->ring_lock);
522 if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size *
523 adev->gfx.num_compute_rings)) {
524 spin_unlock(&kiq->ring_lock);
525 return -ENOMEM;
526 }
527
528 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
529 j = i + xcc_id * adev->gfx.num_compute_rings;
530 kiq->pmf->kiq_unmap_queues(kiq_ring,
531 &adev->gfx.compute_ring[j],
532 RESET_QUEUES, 0, 0);
533 }
534 /* Submit unmap queue packet */
535 amdgpu_ring_commit(kiq_ring);
536 /*
537 * Ring test will do a basic scratch register change check. Just run
538 * this to ensure that unmap queues that is submitted before got
539 * processed successfully before returning.
540 */
541 r = amdgpu_ring_test_helper(kiq_ring);
542
543 spin_unlock(&kiq->ring_lock);
544
545 return r;
546 }
547
amdgpu_gfx_disable_kgq(struct amdgpu_device * adev,int xcc_id)548 int amdgpu_gfx_disable_kgq(struct amdgpu_device *adev, int xcc_id)
549 {
550 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
551 struct amdgpu_ring *kiq_ring = &kiq->ring;
552 int i, r = 0;
553 int j;
554
555 if (adev->enable_mes) {
556 if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
557 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
558 j = i + xcc_id * adev->gfx.num_gfx_rings;
559 amdgpu_mes_unmap_legacy_queue(adev,
560 &adev->gfx.gfx_ring[j],
561 PREEMPT_QUEUES, 0, 0);
562 }
563 }
564 return 0;
565 }
566
567 if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
568 return -EINVAL;
569
570 if (!adev->gfx.kiq[0].ring.sched.ready || amdgpu_in_reset(adev))
571 return 0;
572
573 if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
574 spin_lock(&kiq->ring_lock);
575 if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size *
576 adev->gfx.num_gfx_rings)) {
577 spin_unlock(&kiq->ring_lock);
578 return -ENOMEM;
579 }
580
581 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
582 j = i + xcc_id * adev->gfx.num_gfx_rings;
583 kiq->pmf->kiq_unmap_queues(kiq_ring,
584 &adev->gfx.gfx_ring[j],
585 PREEMPT_QUEUES, 0, 0);
586 }
587 /* Submit unmap queue packet */
588 amdgpu_ring_commit(kiq_ring);
589
590 /*
591 * Ring test will do a basic scratch register change check.
592 * Just run this to ensure that unmap queues that is submitted
593 * before got processed successfully before returning.
594 */
595 r = amdgpu_ring_test_helper(kiq_ring);
596 spin_unlock(&kiq->ring_lock);
597 }
598
599 return r;
600 }
601
amdgpu_queue_mask_bit_to_set_resource_bit(struct amdgpu_device * adev,int queue_bit)602 int amdgpu_queue_mask_bit_to_set_resource_bit(struct amdgpu_device *adev,
603 int queue_bit)
604 {
605 int mec, pipe, queue;
606 int set_resource_bit = 0;
607
608 amdgpu_queue_mask_bit_to_mec_queue(adev, queue_bit, &mec, &pipe, &queue);
609
610 set_resource_bit = mec * 4 * 8 + pipe * 8 + queue;
611
612 return set_resource_bit;
613 }
614
amdgpu_gfx_mes_enable_kcq(struct amdgpu_device * adev,int xcc_id)615 static int amdgpu_gfx_mes_enable_kcq(struct amdgpu_device *adev, int xcc_id)
616 {
617 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
618 struct amdgpu_ring *kiq_ring = &kiq->ring;
619 uint64_t queue_mask = ~0ULL;
620 int r, i, j;
621
622 amdgpu_device_flush_hdp(adev, NULL);
623
624 if (!adev->enable_uni_mes) {
625 spin_lock(&kiq->ring_lock);
626 r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->set_resources_size);
627 if (r) {
628 dev_err(adev->dev, "Failed to lock KIQ (%d).\n", r);
629 spin_unlock(&kiq->ring_lock);
630 return r;
631 }
632
633 kiq->pmf->kiq_set_resources(kiq_ring, queue_mask);
634 r = amdgpu_ring_test_helper(kiq_ring);
635 spin_unlock(&kiq->ring_lock);
636 if (r)
637 dev_err(adev->dev, "KIQ failed to set resources\n");
638 }
639
640 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
641 j = i + xcc_id * adev->gfx.num_compute_rings;
642 r = amdgpu_mes_map_legacy_queue(adev,
643 &adev->gfx.compute_ring[j]);
644 if (r) {
645 dev_err(adev->dev, "failed to map compute queue\n");
646 return r;
647 }
648 }
649
650 return 0;
651 }
652
amdgpu_gfx_enable_kcq(struct amdgpu_device * adev,int xcc_id)653 int amdgpu_gfx_enable_kcq(struct amdgpu_device *adev, int xcc_id)
654 {
655 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
656 struct amdgpu_ring *kiq_ring = &kiq->ring;
657 uint64_t queue_mask = 0;
658 int r, i, j;
659
660 if (adev->mes.enable_legacy_queue_map)
661 return amdgpu_gfx_mes_enable_kcq(adev, xcc_id);
662
663 if (!kiq->pmf || !kiq->pmf->kiq_map_queues || !kiq->pmf->kiq_set_resources)
664 return -EINVAL;
665
666 for (i = 0; i < AMDGPU_MAX_COMPUTE_QUEUES; ++i) {
667 if (!test_bit(i, adev->gfx.mec_bitmap[xcc_id].queue_bitmap))
668 continue;
669
670 /* This situation may be hit in the future if a new HW
671 * generation exposes more than 64 queues. If so, the
672 * definition of queue_mask needs updating */
673 if (WARN_ON(i > (sizeof(queue_mask)*8))) {
674 DRM_ERROR("Invalid KCQ enabled: %d\n", i);
675 break;
676 }
677
678 queue_mask |= (1ull << amdgpu_queue_mask_bit_to_set_resource_bit(adev, i));
679 }
680
681 amdgpu_device_flush_hdp(adev, NULL);
682
683 DRM_INFO("kiq ring mec %d pipe %d q %d\n", kiq_ring->me, kiq_ring->pipe,
684 kiq_ring->queue);
685
686 spin_lock(&kiq->ring_lock);
687 r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->map_queues_size *
688 adev->gfx.num_compute_rings +
689 kiq->pmf->set_resources_size);
690 if (r) {
691 DRM_ERROR("Failed to lock KIQ (%d).\n", r);
692 spin_unlock(&kiq->ring_lock);
693 return r;
694 }
695
696 kiq->pmf->kiq_set_resources(kiq_ring, queue_mask);
697 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
698 j = i + xcc_id * adev->gfx.num_compute_rings;
699 kiq->pmf->kiq_map_queues(kiq_ring,
700 &adev->gfx.compute_ring[j]);
701 }
702 /* Submit map queue packet */
703 amdgpu_ring_commit(kiq_ring);
704 /*
705 * Ring test will do a basic scratch register change check. Just run
706 * this to ensure that map queues that is submitted before got
707 * processed successfully before returning.
708 */
709 r = amdgpu_ring_test_helper(kiq_ring);
710 spin_unlock(&kiq->ring_lock);
711 if (r)
712 DRM_ERROR("KCQ enable failed\n");
713
714 return r;
715 }
716
amdgpu_gfx_enable_kgq(struct amdgpu_device * adev,int xcc_id)717 int amdgpu_gfx_enable_kgq(struct amdgpu_device *adev, int xcc_id)
718 {
719 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
720 struct amdgpu_ring *kiq_ring = &kiq->ring;
721 int r, i, j;
722
723 if (!kiq->pmf || !kiq->pmf->kiq_map_queues)
724 return -EINVAL;
725
726 amdgpu_device_flush_hdp(adev, NULL);
727
728 if (adev->mes.enable_legacy_queue_map) {
729 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
730 j = i + xcc_id * adev->gfx.num_gfx_rings;
731 r = amdgpu_mes_map_legacy_queue(adev,
732 &adev->gfx.gfx_ring[j]);
733 if (r) {
734 DRM_ERROR("failed to map gfx queue\n");
735 return r;
736 }
737 }
738
739 return 0;
740 }
741
742 spin_lock(&kiq->ring_lock);
743 /* No need to map kcq on the slave */
744 if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
745 r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->map_queues_size *
746 adev->gfx.num_gfx_rings);
747 if (r) {
748 DRM_ERROR("Failed to lock KIQ (%d).\n", r);
749 spin_unlock(&kiq->ring_lock);
750 return r;
751 }
752
753 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
754 j = i + xcc_id * adev->gfx.num_gfx_rings;
755 kiq->pmf->kiq_map_queues(kiq_ring,
756 &adev->gfx.gfx_ring[j]);
757 }
758 }
759 /* Submit map queue packet */
760 amdgpu_ring_commit(kiq_ring);
761 /*
762 * Ring test will do a basic scratch register change check. Just run
763 * this to ensure that map queues that is submitted before got
764 * processed successfully before returning.
765 */
766 r = amdgpu_ring_test_helper(kiq_ring);
767 spin_unlock(&kiq->ring_lock);
768 if (r)
769 DRM_ERROR("KGQ enable failed\n");
770
771 return r;
772 }
773
amdgpu_gfx_do_off_ctrl(struct amdgpu_device * adev,bool enable,bool no_delay)774 static void amdgpu_gfx_do_off_ctrl(struct amdgpu_device *adev, bool enable,
775 bool no_delay)
776 {
777 unsigned long delay = GFX_OFF_DELAY_ENABLE;
778
779 if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
780 return;
781
782 mutex_lock(&adev->gfx.gfx_off_mutex);
783
784 if (enable) {
785 /* If the count is already 0, it means there's an imbalance bug somewhere.
786 * Note that the bug may be in a different caller than the one which triggers the
787 * WARN_ON_ONCE.
788 */
789 if (WARN_ON_ONCE(adev->gfx.gfx_off_req_count == 0))
790 goto unlock;
791
792 adev->gfx.gfx_off_req_count--;
793
794 if (adev->gfx.gfx_off_req_count == 0 &&
795 !adev->gfx.gfx_off_state) {
796 /* If going to s2idle, no need to wait */
797 if (no_delay) {
798 if (!amdgpu_dpm_set_powergating_by_smu(adev,
799 AMD_IP_BLOCK_TYPE_GFX, true, 0))
800 adev->gfx.gfx_off_state = true;
801 } else {
802 schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
803 delay);
804 }
805 }
806 } else {
807 if (adev->gfx.gfx_off_req_count == 0) {
808 cancel_delayed_work_sync(&adev->gfx.gfx_off_delay_work);
809
810 if (adev->gfx.gfx_off_state &&
811 !amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, false, 0)) {
812 adev->gfx.gfx_off_state = false;
813
814 if (adev->gfx.funcs->init_spm_golden) {
815 dev_dbg(adev->dev,
816 "GFXOFF is disabled, re-init SPM golden settings\n");
817 amdgpu_gfx_init_spm_golden(adev);
818 }
819 }
820 }
821
822 adev->gfx.gfx_off_req_count++;
823 }
824
825 unlock:
826 mutex_unlock(&adev->gfx.gfx_off_mutex);
827 }
828
829 /* amdgpu_gfx_off_ctrl - Handle gfx off feature enable/disable
830 *
831 * @adev: amdgpu_device pointer
832 * @bool enable true: enable gfx off feature, false: disable gfx off feature
833 *
834 * 1. gfx off feature will be enabled by gfx ip after gfx cg pg enabled.
835 * 2. other client can send request to disable gfx off feature, the request should be honored.
836 * 3. other client can cancel their request of disable gfx off feature
837 * 4. other client should not send request to enable gfx off feature before disable gfx off feature.
838 *
839 * gfx off allow will be delayed by GFX_OFF_DELAY_ENABLE ms.
840 */
amdgpu_gfx_off_ctrl(struct amdgpu_device * adev,bool enable)841 void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
842 {
843 /* If going to s2idle, no need to wait */
844 bool no_delay = adev->in_s0ix ? true : false;
845
846 amdgpu_gfx_do_off_ctrl(adev, enable, no_delay);
847 }
848
849 /* amdgpu_gfx_off_ctrl_immediate - Handle gfx off feature enable/disable
850 *
851 * @adev: amdgpu_device pointer
852 * @bool enable true: enable gfx off feature, false: disable gfx off feature
853 *
854 * 1. gfx off feature will be enabled by gfx ip after gfx cg pg enabled.
855 * 2. other client can send request to disable gfx off feature, the request should be honored.
856 * 3. other client can cancel their request of disable gfx off feature
857 * 4. other client should not send request to enable gfx off feature before disable gfx off feature.
858 *
859 * gfx off allow will be issued immediately.
860 */
amdgpu_gfx_off_ctrl_immediate(struct amdgpu_device * adev,bool enable)861 void amdgpu_gfx_off_ctrl_immediate(struct amdgpu_device *adev, bool enable)
862 {
863 amdgpu_gfx_do_off_ctrl(adev, enable, true);
864 }
865
amdgpu_set_gfx_off_residency(struct amdgpu_device * adev,bool value)866 int amdgpu_set_gfx_off_residency(struct amdgpu_device *adev, bool value)
867 {
868 int r = 0;
869
870 mutex_lock(&adev->gfx.gfx_off_mutex);
871
872 r = amdgpu_dpm_set_residency_gfxoff(adev, value);
873
874 mutex_unlock(&adev->gfx.gfx_off_mutex);
875
876 return r;
877 }
878
amdgpu_get_gfx_off_residency(struct amdgpu_device * adev,u32 * value)879 int amdgpu_get_gfx_off_residency(struct amdgpu_device *adev, u32 *value)
880 {
881 int r = 0;
882
883 mutex_lock(&adev->gfx.gfx_off_mutex);
884
885 r = amdgpu_dpm_get_residency_gfxoff(adev, value);
886
887 mutex_unlock(&adev->gfx.gfx_off_mutex);
888
889 return r;
890 }
891
amdgpu_get_gfx_off_entrycount(struct amdgpu_device * adev,u64 * value)892 int amdgpu_get_gfx_off_entrycount(struct amdgpu_device *adev, u64 *value)
893 {
894 int r = 0;
895
896 mutex_lock(&adev->gfx.gfx_off_mutex);
897
898 r = amdgpu_dpm_get_entrycount_gfxoff(adev, value);
899
900 mutex_unlock(&adev->gfx.gfx_off_mutex);
901
902 return r;
903 }
904
amdgpu_get_gfx_off_status(struct amdgpu_device * adev,uint32_t * value)905 int amdgpu_get_gfx_off_status(struct amdgpu_device *adev, uint32_t *value)
906 {
907
908 int r = 0;
909
910 mutex_lock(&adev->gfx.gfx_off_mutex);
911
912 r = amdgpu_dpm_get_status_gfxoff(adev, value);
913
914 mutex_unlock(&adev->gfx.gfx_off_mutex);
915
916 return r;
917 }
918
amdgpu_gfx_ras_late_init(struct amdgpu_device * adev,struct ras_common_if * ras_block)919 int amdgpu_gfx_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
920 {
921 int r;
922
923 if (amdgpu_ras_is_supported(adev, ras_block->block)) {
924 if (!amdgpu_persistent_edc_harvesting_supported(adev)) {
925 r = amdgpu_ras_reset_error_status(adev, AMDGPU_RAS_BLOCK__GFX);
926 if (r)
927 return r;
928 }
929
930 r = amdgpu_ras_block_late_init(adev, ras_block);
931 if (r)
932 return r;
933
934 if (amdgpu_sriov_vf(adev))
935 return r;
936
937 if (adev->gfx.cp_ecc_error_irq.funcs) {
938 r = amdgpu_irq_get(adev, &adev->gfx.cp_ecc_error_irq, 0);
939 if (r)
940 goto late_fini;
941 }
942 } else {
943 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
944 }
945
946 return 0;
947 late_fini:
948 amdgpu_ras_block_late_fini(adev, ras_block);
949 return r;
950 }
951
amdgpu_gfx_ras_sw_init(struct amdgpu_device * adev)952 int amdgpu_gfx_ras_sw_init(struct amdgpu_device *adev)
953 {
954 int err = 0;
955 struct amdgpu_gfx_ras *ras = NULL;
956
957 /* adev->gfx.ras is NULL, which means gfx does not
958 * support ras function, then do nothing here.
959 */
960 if (!adev->gfx.ras)
961 return 0;
962
963 ras = adev->gfx.ras;
964
965 err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
966 if (err) {
967 dev_err(adev->dev, "Failed to register gfx ras block!\n");
968 return err;
969 }
970
971 strcpy(ras->ras_block.ras_comm.name, "gfx");
972 ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__GFX;
973 ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
974 adev->gfx.ras_if = &ras->ras_block.ras_comm;
975
976 /* If not define special ras_late_init function, use gfx default ras_late_init */
977 if (!ras->ras_block.ras_late_init)
978 ras->ras_block.ras_late_init = amdgpu_gfx_ras_late_init;
979
980 /* If not defined special ras_cb function, use default ras_cb */
981 if (!ras->ras_block.ras_cb)
982 ras->ras_block.ras_cb = amdgpu_gfx_process_ras_data_cb;
983
984 return 0;
985 }
986
amdgpu_gfx_poison_consumption_handler(struct amdgpu_device * adev,struct amdgpu_iv_entry * entry)987 int amdgpu_gfx_poison_consumption_handler(struct amdgpu_device *adev,
988 struct amdgpu_iv_entry *entry)
989 {
990 if (adev->gfx.ras && adev->gfx.ras->poison_consumption_handler)
991 return adev->gfx.ras->poison_consumption_handler(adev, entry);
992
993 return 0;
994 }
995
amdgpu_gfx_process_ras_data_cb(struct amdgpu_device * adev,void * err_data,struct amdgpu_iv_entry * entry)996 int amdgpu_gfx_process_ras_data_cb(struct amdgpu_device *adev,
997 void *err_data,
998 struct amdgpu_iv_entry *entry)
999 {
1000 /* TODO ue will trigger an interrupt.
1001 *
1002 * When “Full RAS” is enabled, the per-IP interrupt sources should
1003 * be disabled and the driver should only look for the aggregated
1004 * interrupt via sync flood
1005 */
1006 if (!amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__GFX)) {
1007 kgd2kfd_set_sram_ecc_flag(adev->kfd.dev);
1008 if (adev->gfx.ras && adev->gfx.ras->ras_block.hw_ops &&
1009 adev->gfx.ras->ras_block.hw_ops->query_ras_error_count)
1010 adev->gfx.ras->ras_block.hw_ops->query_ras_error_count(adev, err_data);
1011 amdgpu_ras_reset_gpu(adev);
1012 }
1013 return AMDGPU_RAS_SUCCESS;
1014 }
1015
amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)1016 int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
1017 struct amdgpu_irq_src *source,
1018 struct amdgpu_iv_entry *entry)
1019 {
1020 struct ras_common_if *ras_if = adev->gfx.ras_if;
1021 struct ras_dispatch_if ih_data = {
1022 .entry = entry,
1023 };
1024
1025 if (!ras_if)
1026 return 0;
1027
1028 ih_data.head = *ras_if;
1029
1030 DRM_ERROR("CP ECC ERROR IRQ\n");
1031 amdgpu_ras_interrupt_dispatch(adev, &ih_data);
1032 return 0;
1033 }
1034
amdgpu_gfx_ras_error_func(struct amdgpu_device * adev,void * ras_error_status,void (* func)(struct amdgpu_device * adev,void * ras_error_status,int xcc_id))1035 void amdgpu_gfx_ras_error_func(struct amdgpu_device *adev,
1036 void *ras_error_status,
1037 void (*func)(struct amdgpu_device *adev, void *ras_error_status,
1038 int xcc_id))
1039 {
1040 int i;
1041 int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
1042 uint32_t xcc_mask = GENMASK(num_xcc - 1, 0);
1043 struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
1044
1045 if (err_data) {
1046 err_data->ue_count = 0;
1047 err_data->ce_count = 0;
1048 }
1049
1050 for_each_inst(i, xcc_mask)
1051 func(adev, ras_error_status, i);
1052 }
1053
amdgpu_kiq_rreg(struct amdgpu_device * adev,uint32_t reg,uint32_t xcc_id)1054 uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg, uint32_t xcc_id)
1055 {
1056 signed long r, cnt = 0;
1057 unsigned long flags;
1058 uint32_t seq, reg_val_offs = 0, value = 0;
1059 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
1060 struct amdgpu_ring *ring = &kiq->ring;
1061
1062 if (amdgpu_device_skip_hw_access(adev))
1063 return 0;
1064
1065 if (adev->mes.ring[0].sched.ready)
1066 return amdgpu_mes_rreg(adev, reg);
1067
1068 BUG_ON(!ring->funcs->emit_rreg);
1069
1070 spin_lock_irqsave(&kiq->ring_lock, flags);
1071 if (amdgpu_device_wb_get(adev, ®_val_offs)) {
1072 pr_err("critical bug! too many kiq readers\n");
1073 goto failed_unlock;
1074 }
1075 r = amdgpu_ring_alloc(ring, 32);
1076 if (r)
1077 goto failed_unlock;
1078
1079 amdgpu_ring_emit_rreg(ring, reg, reg_val_offs);
1080 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
1081 if (r)
1082 goto failed_undo;
1083
1084 amdgpu_ring_commit(ring);
1085 spin_unlock_irqrestore(&kiq->ring_lock, flags);
1086
1087 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1088
1089 /* don't wait anymore for gpu reset case because this way may
1090 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
1091 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
1092 * never return if we keep waiting in virt_kiq_rreg, which cause
1093 * gpu_recover() hang there.
1094 *
1095 * also don't wait anymore for IRQ context
1096 * */
1097 if (r < 1 && (amdgpu_in_reset(adev) || in_interrupt()))
1098 goto failed_kiq_read;
1099
1100 might_sleep();
1101 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
1102 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
1103 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1104 }
1105
1106 if (cnt > MAX_KIQ_REG_TRY)
1107 goto failed_kiq_read;
1108
1109 mb();
1110 value = adev->wb.wb[reg_val_offs];
1111 amdgpu_device_wb_free(adev, reg_val_offs);
1112 return value;
1113
1114 failed_undo:
1115 amdgpu_ring_undo(ring);
1116 failed_unlock:
1117 spin_unlock_irqrestore(&kiq->ring_lock, flags);
1118 failed_kiq_read:
1119 if (reg_val_offs)
1120 amdgpu_device_wb_free(adev, reg_val_offs);
1121 dev_err(adev->dev, "failed to read reg:%x\n", reg);
1122 return ~0;
1123 }
1124
amdgpu_kiq_wreg(struct amdgpu_device * adev,uint32_t reg,uint32_t v,uint32_t xcc_id)1125 void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v, uint32_t xcc_id)
1126 {
1127 signed long r, cnt = 0;
1128 unsigned long flags;
1129 uint32_t seq;
1130 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
1131 struct amdgpu_ring *ring = &kiq->ring;
1132
1133 BUG_ON(!ring->funcs->emit_wreg);
1134
1135 if (amdgpu_device_skip_hw_access(adev))
1136 return;
1137
1138 if (adev->mes.ring[0].sched.ready) {
1139 amdgpu_mes_wreg(adev, reg, v);
1140 return;
1141 }
1142
1143 spin_lock_irqsave(&kiq->ring_lock, flags);
1144 r = amdgpu_ring_alloc(ring, 32);
1145 if (r)
1146 goto failed_unlock;
1147
1148 amdgpu_ring_emit_wreg(ring, reg, v);
1149 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
1150 if (r)
1151 goto failed_undo;
1152
1153 amdgpu_ring_commit(ring);
1154 spin_unlock_irqrestore(&kiq->ring_lock, flags);
1155
1156 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1157
1158 /* don't wait anymore for gpu reset case because this way may
1159 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
1160 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
1161 * never return if we keep waiting in virt_kiq_rreg, which cause
1162 * gpu_recover() hang there.
1163 *
1164 * also don't wait anymore for IRQ context
1165 * */
1166 if (r < 1 && (amdgpu_in_reset(adev) || in_interrupt()))
1167 goto failed_kiq_write;
1168
1169 might_sleep();
1170 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
1171
1172 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
1173 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1174 }
1175
1176 if (cnt > MAX_KIQ_REG_TRY)
1177 goto failed_kiq_write;
1178
1179 return;
1180
1181 failed_undo:
1182 amdgpu_ring_undo(ring);
1183 failed_unlock:
1184 spin_unlock_irqrestore(&kiq->ring_lock, flags);
1185 failed_kiq_write:
1186 dev_err(adev->dev, "failed to write reg:%x\n", reg);
1187 }
1188
amdgpu_gfx_get_num_kcq(struct amdgpu_device * adev)1189 int amdgpu_gfx_get_num_kcq(struct amdgpu_device *adev)
1190 {
1191 if (amdgpu_num_kcq == -1) {
1192 return 8;
1193 } else if (amdgpu_num_kcq > 8 || amdgpu_num_kcq < 0) {
1194 dev_warn(adev->dev, "set kernel compute queue number to 8 due to invalid parameter provided by user\n");
1195 return 8;
1196 }
1197 return amdgpu_num_kcq;
1198 }
1199
amdgpu_gfx_cp_init_microcode(struct amdgpu_device * adev,uint32_t ucode_id)1200 void amdgpu_gfx_cp_init_microcode(struct amdgpu_device *adev,
1201 uint32_t ucode_id)
1202 {
1203 const struct gfx_firmware_header_v1_0 *cp_hdr;
1204 const struct gfx_firmware_header_v2_0 *cp_hdr_v2_0;
1205 struct amdgpu_firmware_info *info = NULL;
1206 const struct firmware *ucode_fw;
1207 unsigned int fw_size;
1208
1209 switch (ucode_id) {
1210 case AMDGPU_UCODE_ID_CP_PFP:
1211 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1212 adev->gfx.pfp_fw->data;
1213 adev->gfx.pfp_fw_version =
1214 le32_to_cpu(cp_hdr->header.ucode_version);
1215 adev->gfx.pfp_feature_version =
1216 le32_to_cpu(cp_hdr->ucode_feature_version);
1217 ucode_fw = adev->gfx.pfp_fw;
1218 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1219 break;
1220 case AMDGPU_UCODE_ID_CP_RS64_PFP:
1221 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1222 adev->gfx.pfp_fw->data;
1223 adev->gfx.pfp_fw_version =
1224 le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1225 adev->gfx.pfp_feature_version =
1226 le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1227 ucode_fw = adev->gfx.pfp_fw;
1228 fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1229 break;
1230 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
1231 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
1232 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1233 adev->gfx.pfp_fw->data;
1234 ucode_fw = adev->gfx.pfp_fw;
1235 fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1236 break;
1237 case AMDGPU_UCODE_ID_CP_ME:
1238 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1239 adev->gfx.me_fw->data;
1240 adev->gfx.me_fw_version =
1241 le32_to_cpu(cp_hdr->header.ucode_version);
1242 adev->gfx.me_feature_version =
1243 le32_to_cpu(cp_hdr->ucode_feature_version);
1244 ucode_fw = adev->gfx.me_fw;
1245 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1246 break;
1247 case AMDGPU_UCODE_ID_CP_RS64_ME:
1248 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1249 adev->gfx.me_fw->data;
1250 adev->gfx.me_fw_version =
1251 le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1252 adev->gfx.me_feature_version =
1253 le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1254 ucode_fw = adev->gfx.me_fw;
1255 fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1256 break;
1257 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
1258 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
1259 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1260 adev->gfx.me_fw->data;
1261 ucode_fw = adev->gfx.me_fw;
1262 fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1263 break;
1264 case AMDGPU_UCODE_ID_CP_CE:
1265 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1266 adev->gfx.ce_fw->data;
1267 adev->gfx.ce_fw_version =
1268 le32_to_cpu(cp_hdr->header.ucode_version);
1269 adev->gfx.ce_feature_version =
1270 le32_to_cpu(cp_hdr->ucode_feature_version);
1271 ucode_fw = adev->gfx.ce_fw;
1272 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1273 break;
1274 case AMDGPU_UCODE_ID_CP_MEC1:
1275 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1276 adev->gfx.mec_fw->data;
1277 adev->gfx.mec_fw_version =
1278 le32_to_cpu(cp_hdr->header.ucode_version);
1279 adev->gfx.mec_feature_version =
1280 le32_to_cpu(cp_hdr->ucode_feature_version);
1281 ucode_fw = adev->gfx.mec_fw;
1282 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
1283 le32_to_cpu(cp_hdr->jt_size) * 4;
1284 break;
1285 case AMDGPU_UCODE_ID_CP_MEC1_JT:
1286 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1287 adev->gfx.mec_fw->data;
1288 ucode_fw = adev->gfx.mec_fw;
1289 fw_size = le32_to_cpu(cp_hdr->jt_size) * 4;
1290 break;
1291 case AMDGPU_UCODE_ID_CP_MEC2:
1292 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1293 adev->gfx.mec2_fw->data;
1294 adev->gfx.mec2_fw_version =
1295 le32_to_cpu(cp_hdr->header.ucode_version);
1296 adev->gfx.mec2_feature_version =
1297 le32_to_cpu(cp_hdr->ucode_feature_version);
1298 ucode_fw = adev->gfx.mec2_fw;
1299 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
1300 le32_to_cpu(cp_hdr->jt_size) * 4;
1301 break;
1302 case AMDGPU_UCODE_ID_CP_MEC2_JT:
1303 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1304 adev->gfx.mec2_fw->data;
1305 ucode_fw = adev->gfx.mec2_fw;
1306 fw_size = le32_to_cpu(cp_hdr->jt_size) * 4;
1307 break;
1308 case AMDGPU_UCODE_ID_CP_RS64_MEC:
1309 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1310 adev->gfx.mec_fw->data;
1311 adev->gfx.mec_fw_version =
1312 le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1313 adev->gfx.mec_feature_version =
1314 le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1315 ucode_fw = adev->gfx.mec_fw;
1316 fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1317 break;
1318 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
1319 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
1320 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
1321 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
1322 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1323 adev->gfx.mec_fw->data;
1324 ucode_fw = adev->gfx.mec_fw;
1325 fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1326 break;
1327 default:
1328 dev_err(adev->dev, "Invalid ucode id %u\n", ucode_id);
1329 return;
1330 }
1331
1332 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1333 info = &adev->firmware.ucode[ucode_id];
1334 info->ucode_id = ucode_id;
1335 info->fw = ucode_fw;
1336 adev->firmware.fw_size += ALIGN(fw_size, PAGE_SIZE);
1337 }
1338 }
1339
amdgpu_gfx_is_master_xcc(struct amdgpu_device * adev,int xcc_id)1340 bool amdgpu_gfx_is_master_xcc(struct amdgpu_device *adev, int xcc_id)
1341 {
1342 return !(xcc_id % (adev->gfx.num_xcc_per_xcp ?
1343 adev->gfx.num_xcc_per_xcp : 1));
1344 }
1345
amdgpu_gfx_get_current_compute_partition(struct device * dev,struct device_attribute * addr,char * buf)1346 static ssize_t amdgpu_gfx_get_current_compute_partition(struct device *dev,
1347 struct device_attribute *addr,
1348 char *buf)
1349 {
1350 struct drm_device *ddev = dev_get_drvdata(dev);
1351 struct amdgpu_device *adev = drm_to_adev(ddev);
1352 int mode;
1353
1354 mode = amdgpu_xcp_query_partition_mode(adev->xcp_mgr,
1355 AMDGPU_XCP_FL_NONE);
1356
1357 return sysfs_emit(buf, "%s\n", amdgpu_gfx_compute_mode_desc(mode));
1358 }
1359
amdgpu_gfx_set_compute_partition(struct device * dev,struct device_attribute * addr,const char * buf,size_t count)1360 static ssize_t amdgpu_gfx_set_compute_partition(struct device *dev,
1361 struct device_attribute *addr,
1362 const char *buf, size_t count)
1363 {
1364 struct drm_device *ddev = dev_get_drvdata(dev);
1365 struct amdgpu_device *adev = drm_to_adev(ddev);
1366 enum amdgpu_gfx_partition mode;
1367 int ret = 0, num_xcc;
1368
1369 num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1370 if (num_xcc % 2 != 0)
1371 return -EINVAL;
1372
1373 if (!strncasecmp("SPX", buf, strlen("SPX"))) {
1374 mode = AMDGPU_SPX_PARTITION_MODE;
1375 } else if (!strncasecmp("DPX", buf, strlen("DPX"))) {
1376 /*
1377 * DPX mode needs AIDs to be in multiple of 2.
1378 * Each AID connects 2 XCCs.
1379 */
1380 if (num_xcc%4)
1381 return -EINVAL;
1382 mode = AMDGPU_DPX_PARTITION_MODE;
1383 } else if (!strncasecmp("TPX", buf, strlen("TPX"))) {
1384 if (num_xcc != 6)
1385 return -EINVAL;
1386 mode = AMDGPU_TPX_PARTITION_MODE;
1387 } else if (!strncasecmp("QPX", buf, strlen("QPX"))) {
1388 if (num_xcc != 8)
1389 return -EINVAL;
1390 mode = AMDGPU_QPX_PARTITION_MODE;
1391 } else if (!strncasecmp("CPX", buf, strlen("CPX"))) {
1392 mode = AMDGPU_CPX_PARTITION_MODE;
1393 } else {
1394 return -EINVAL;
1395 }
1396
1397 ret = amdgpu_xcp_switch_partition_mode(adev->xcp_mgr, mode);
1398
1399 if (ret)
1400 return ret;
1401
1402 return count;
1403 }
1404
1405 static const char *xcp_desc[] = {
1406 [AMDGPU_SPX_PARTITION_MODE] = "SPX",
1407 [AMDGPU_DPX_PARTITION_MODE] = "DPX",
1408 [AMDGPU_TPX_PARTITION_MODE] = "TPX",
1409 [AMDGPU_QPX_PARTITION_MODE] = "QPX",
1410 [AMDGPU_CPX_PARTITION_MODE] = "CPX",
1411 };
1412
amdgpu_gfx_get_available_compute_partition(struct device * dev,struct device_attribute * addr,char * buf)1413 static ssize_t amdgpu_gfx_get_available_compute_partition(struct device *dev,
1414 struct device_attribute *addr,
1415 char *buf)
1416 {
1417 struct drm_device *ddev = dev_get_drvdata(dev);
1418 struct amdgpu_device *adev = drm_to_adev(ddev);
1419 struct amdgpu_xcp_mgr *xcp_mgr = adev->xcp_mgr;
1420 int size = 0, mode;
1421 char *sep = "";
1422
1423 if (!xcp_mgr || !xcp_mgr->avail_xcp_modes)
1424 return sysfs_emit(buf, "Not supported\n");
1425
1426 for_each_inst(mode, xcp_mgr->avail_xcp_modes) {
1427 size += sysfs_emit_at(buf, size, "%s%s", sep, xcp_desc[mode]);
1428 sep = ", ";
1429 }
1430
1431 size += sysfs_emit_at(buf, size, "\n");
1432
1433 return size;
1434 }
1435
amdgpu_gfx_run_cleaner_shader_job(struct amdgpu_ring * ring)1436 static int amdgpu_gfx_run_cleaner_shader_job(struct amdgpu_ring *ring)
1437 {
1438 struct amdgpu_device *adev = ring->adev;
1439 struct drm_gpu_scheduler *sched = &ring->sched;
1440 struct drm_sched_entity entity;
1441 static atomic_t counter;
1442 struct dma_fence *f;
1443 struct amdgpu_job *job;
1444 struct amdgpu_ib *ib;
1445 void *owner;
1446 int i, r;
1447
1448 /* Initialize the scheduler entity */
1449 r = drm_sched_entity_init(&entity, DRM_SCHED_PRIORITY_NORMAL,
1450 &sched, 1, NULL);
1451 if (r) {
1452 dev_err(adev->dev, "Failed setting up GFX kernel entity.\n");
1453 goto err;
1454 }
1455
1456 /*
1457 * Use some unique dummy value as the owner to make sure we execute
1458 * the cleaner shader on each submission. The value just need to change
1459 * for each submission and is otherwise meaningless.
1460 */
1461 owner = (void *)(unsigned long)atomic_inc_return(&counter);
1462
1463 r = amdgpu_job_alloc_with_ib(ring->adev, &entity, owner,
1464 64, 0, &job);
1465 if (r)
1466 goto err;
1467
1468 job->enforce_isolation = true;
1469
1470 ib = &job->ibs[0];
1471 for (i = 0; i <= ring->funcs->align_mask; ++i)
1472 ib->ptr[i] = ring->funcs->nop;
1473 ib->length_dw = ring->funcs->align_mask + 1;
1474
1475 f = amdgpu_job_submit(job);
1476
1477 r = dma_fence_wait(f, false);
1478 if (r)
1479 goto err;
1480
1481 dma_fence_put(f);
1482
1483 /* Clean up the scheduler entity */
1484 drm_sched_entity_destroy(&entity);
1485 return 0;
1486
1487 err:
1488 return r;
1489 }
1490
amdgpu_gfx_run_cleaner_shader(struct amdgpu_device * adev,int xcp_id)1491 static int amdgpu_gfx_run_cleaner_shader(struct amdgpu_device *adev, int xcp_id)
1492 {
1493 int num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1494 struct amdgpu_ring *ring;
1495 int num_xcc_to_clear;
1496 int i, r, xcc_id;
1497
1498 if (adev->gfx.num_xcc_per_xcp)
1499 num_xcc_to_clear = adev->gfx.num_xcc_per_xcp;
1500 else
1501 num_xcc_to_clear = 1;
1502
1503 for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
1504 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
1505 ring = &adev->gfx.compute_ring[i + xcc_id * adev->gfx.num_compute_rings];
1506 if ((ring->xcp_id == xcp_id) && ring->sched.ready) {
1507 r = amdgpu_gfx_run_cleaner_shader_job(ring);
1508 if (r)
1509 return r;
1510 num_xcc_to_clear--;
1511 break;
1512 }
1513 }
1514 }
1515
1516 if (num_xcc_to_clear)
1517 return -ENOENT;
1518
1519 return 0;
1520 }
1521
1522 /**
1523 * amdgpu_gfx_set_run_cleaner_shader - Execute the AMDGPU GFX Cleaner Shader
1524 * @dev: The device structure
1525 * @attr: The device attribute structure
1526 * @buf: The buffer containing the input data
1527 * @count: The size of the input data
1528 *
1529 * Provides the sysfs interface to manually run a cleaner shader, which is
1530 * used to clear the GPU state between different tasks. Writing a value to the
1531 * 'run_cleaner_shader' sysfs file triggers the cleaner shader execution.
1532 * The value written corresponds to the partition index on multi-partition
1533 * devices. On single-partition devices, the value should be '0'.
1534 *
1535 * The cleaner shader clears the Local Data Store (LDS) and General Purpose
1536 * Registers (GPRs) to ensure data isolation between GPU workloads.
1537 *
1538 * Return: The number of bytes written to the sysfs file.
1539 */
amdgpu_gfx_set_run_cleaner_shader(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1540 static ssize_t amdgpu_gfx_set_run_cleaner_shader(struct device *dev,
1541 struct device_attribute *attr,
1542 const char *buf,
1543 size_t count)
1544 {
1545 struct drm_device *ddev = dev_get_drvdata(dev);
1546 struct amdgpu_device *adev = drm_to_adev(ddev);
1547 int ret;
1548 long value;
1549
1550 if (amdgpu_in_reset(adev))
1551 return -EPERM;
1552 if (adev->in_suspend && !adev->in_runpm)
1553 return -EPERM;
1554
1555 ret = kstrtol(buf, 0, &value);
1556
1557 if (ret)
1558 return -EINVAL;
1559
1560 if (value < 0)
1561 return -EINVAL;
1562
1563 if (adev->xcp_mgr) {
1564 if (value >= adev->xcp_mgr->num_xcps)
1565 return -EINVAL;
1566 } else {
1567 if (value > 1)
1568 return -EINVAL;
1569 }
1570
1571 ret = pm_runtime_get_sync(ddev->dev);
1572 if (ret < 0) {
1573 pm_runtime_put_autosuspend(ddev->dev);
1574 return ret;
1575 }
1576
1577 ret = amdgpu_gfx_run_cleaner_shader(adev, value);
1578
1579 pm_runtime_mark_last_busy(ddev->dev);
1580 pm_runtime_put_autosuspend(ddev->dev);
1581
1582 if (ret)
1583 return ret;
1584
1585 return count;
1586 }
1587
1588 /**
1589 * amdgpu_gfx_get_enforce_isolation - Query AMDGPU GFX Enforce Isolation Settings
1590 * @dev: The device structure
1591 * @attr: The device attribute structure
1592 * @buf: The buffer to store the output data
1593 *
1594 * Provides the sysfs read interface to get the current settings of the 'enforce_isolation'
1595 * feature for each GPU partition. Reading from the 'enforce_isolation'
1596 * sysfs file returns the isolation settings for all partitions, where '0'
1597 * indicates disabled and '1' indicates enabled.
1598 *
1599 * Return: The number of bytes read from the sysfs file.
1600 */
amdgpu_gfx_get_enforce_isolation(struct device * dev,struct device_attribute * attr,char * buf)1601 static ssize_t amdgpu_gfx_get_enforce_isolation(struct device *dev,
1602 struct device_attribute *attr,
1603 char *buf)
1604 {
1605 struct drm_device *ddev = dev_get_drvdata(dev);
1606 struct amdgpu_device *adev = drm_to_adev(ddev);
1607 int i;
1608 ssize_t size = 0;
1609
1610 if (adev->xcp_mgr) {
1611 for (i = 0; i < adev->xcp_mgr->num_xcps; i++) {
1612 size += sysfs_emit_at(buf, size, "%u", adev->enforce_isolation[i]);
1613 if (i < (adev->xcp_mgr->num_xcps - 1))
1614 size += sysfs_emit_at(buf, size, " ");
1615 }
1616 buf[size++] = '\n';
1617 } else {
1618 size = sysfs_emit_at(buf, 0, "%u\n", adev->enforce_isolation[0]);
1619 }
1620
1621 return size;
1622 }
1623
1624 /**
1625 * amdgpu_gfx_set_enforce_isolation - Control AMDGPU GFX Enforce Isolation
1626 * @dev: The device structure
1627 * @attr: The device attribute structure
1628 * @buf: The buffer containing the input data
1629 * @count: The size of the input data
1630 *
1631 * This function allows control over the 'enforce_isolation' feature, which
1632 * serializes access to the graphics engine. Writing '1' or '0' to the
1633 * 'enforce_isolation' sysfs file enables or disables process isolation for
1634 * each partition. The input should specify the setting for all partitions.
1635 *
1636 * Return: The number of bytes written to the sysfs file.
1637 */
amdgpu_gfx_set_enforce_isolation(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1638 static ssize_t amdgpu_gfx_set_enforce_isolation(struct device *dev,
1639 struct device_attribute *attr,
1640 const char *buf, size_t count)
1641 {
1642 struct drm_device *ddev = dev_get_drvdata(dev);
1643 struct amdgpu_device *adev = drm_to_adev(ddev);
1644 long partition_values[MAX_XCP] = {0};
1645 int ret, i, num_partitions;
1646 const char *input_buf = buf;
1647
1648 for (i = 0; i < (adev->xcp_mgr ? adev->xcp_mgr->num_xcps : 1); i++) {
1649 ret = sscanf(input_buf, "%ld", &partition_values[i]);
1650 if (ret <= 0)
1651 break;
1652
1653 /* Move the pointer to the next value in the string */
1654 input_buf = strchr(input_buf, ' ');
1655 if (input_buf) {
1656 input_buf++;
1657 } else {
1658 i++;
1659 break;
1660 }
1661 }
1662 num_partitions = i;
1663
1664 if (adev->xcp_mgr && num_partitions != adev->xcp_mgr->num_xcps)
1665 return -EINVAL;
1666
1667 if (!adev->xcp_mgr && num_partitions != 1)
1668 return -EINVAL;
1669
1670 for (i = 0; i < num_partitions; i++) {
1671 if (partition_values[i] != 0 && partition_values[i] != 1)
1672 return -EINVAL;
1673 }
1674
1675 mutex_lock(&adev->enforce_isolation_mutex);
1676 for (i = 0; i < num_partitions; i++)
1677 adev->enforce_isolation[i] = partition_values[i];
1678 mutex_unlock(&adev->enforce_isolation_mutex);
1679
1680 amdgpu_mes_update_enforce_isolation(adev);
1681
1682 return count;
1683 }
1684
amdgpu_gfx_get_gfx_reset_mask(struct device * dev,struct device_attribute * attr,char * buf)1685 static ssize_t amdgpu_gfx_get_gfx_reset_mask(struct device *dev,
1686 struct device_attribute *attr,
1687 char *buf)
1688 {
1689 struct drm_device *ddev = dev_get_drvdata(dev);
1690 struct amdgpu_device *adev = drm_to_adev(ddev);
1691
1692 if (!adev)
1693 return -ENODEV;
1694
1695 return amdgpu_show_reset_mask(buf, adev->gfx.gfx_supported_reset);
1696 }
1697
amdgpu_gfx_get_compute_reset_mask(struct device * dev,struct device_attribute * attr,char * buf)1698 static ssize_t amdgpu_gfx_get_compute_reset_mask(struct device *dev,
1699 struct device_attribute *attr,
1700 char *buf)
1701 {
1702 struct drm_device *ddev = dev_get_drvdata(dev);
1703 struct amdgpu_device *adev = drm_to_adev(ddev);
1704
1705 if (!adev)
1706 return -ENODEV;
1707
1708 return amdgpu_show_reset_mask(buf, adev->gfx.compute_supported_reset);
1709 }
1710
1711 static DEVICE_ATTR(run_cleaner_shader, 0200,
1712 NULL, amdgpu_gfx_set_run_cleaner_shader);
1713
1714 static DEVICE_ATTR(enforce_isolation, 0644,
1715 amdgpu_gfx_get_enforce_isolation,
1716 amdgpu_gfx_set_enforce_isolation);
1717
1718 static DEVICE_ATTR(current_compute_partition, 0644,
1719 amdgpu_gfx_get_current_compute_partition,
1720 amdgpu_gfx_set_compute_partition);
1721
1722 static DEVICE_ATTR(available_compute_partition, 0444,
1723 amdgpu_gfx_get_available_compute_partition, NULL);
1724 static DEVICE_ATTR(gfx_reset_mask, 0444,
1725 amdgpu_gfx_get_gfx_reset_mask, NULL);
1726
1727 static DEVICE_ATTR(compute_reset_mask, 0444,
1728 amdgpu_gfx_get_compute_reset_mask, NULL);
1729
amdgpu_gfx_sysfs_xcp_init(struct amdgpu_device * adev)1730 static int amdgpu_gfx_sysfs_xcp_init(struct amdgpu_device *adev)
1731 {
1732 struct amdgpu_xcp_mgr *xcp_mgr = adev->xcp_mgr;
1733 bool xcp_switch_supported;
1734 int r;
1735
1736 if (!xcp_mgr)
1737 return 0;
1738
1739 xcp_switch_supported =
1740 (xcp_mgr->funcs && xcp_mgr->funcs->switch_partition_mode);
1741
1742 if (!xcp_switch_supported)
1743 dev_attr_current_compute_partition.attr.mode &=
1744 ~(S_IWUSR | S_IWGRP | S_IWOTH);
1745
1746 r = device_create_file(adev->dev, &dev_attr_current_compute_partition);
1747 if (r)
1748 return r;
1749
1750 if (xcp_switch_supported)
1751 r = device_create_file(adev->dev,
1752 &dev_attr_available_compute_partition);
1753
1754 return r;
1755 }
1756
amdgpu_gfx_sysfs_xcp_fini(struct amdgpu_device * adev)1757 static void amdgpu_gfx_sysfs_xcp_fini(struct amdgpu_device *adev)
1758 {
1759 struct amdgpu_xcp_mgr *xcp_mgr = adev->xcp_mgr;
1760 bool xcp_switch_supported;
1761
1762 if (!xcp_mgr)
1763 return;
1764
1765 xcp_switch_supported =
1766 (xcp_mgr->funcs && xcp_mgr->funcs->switch_partition_mode);
1767 device_remove_file(adev->dev, &dev_attr_current_compute_partition);
1768
1769 if (xcp_switch_supported)
1770 device_remove_file(adev->dev,
1771 &dev_attr_available_compute_partition);
1772 }
1773
amdgpu_gfx_sysfs_isolation_shader_init(struct amdgpu_device * adev)1774 static int amdgpu_gfx_sysfs_isolation_shader_init(struct amdgpu_device *adev)
1775 {
1776 int r;
1777
1778 r = device_create_file(adev->dev, &dev_attr_enforce_isolation);
1779 if (r)
1780 return r;
1781 if (adev->gfx.enable_cleaner_shader)
1782 r = device_create_file(adev->dev, &dev_attr_run_cleaner_shader);
1783
1784 return r;
1785 }
1786
amdgpu_gfx_sysfs_isolation_shader_fini(struct amdgpu_device * adev)1787 static void amdgpu_gfx_sysfs_isolation_shader_fini(struct amdgpu_device *adev)
1788 {
1789 device_remove_file(adev->dev, &dev_attr_enforce_isolation);
1790 if (adev->gfx.enable_cleaner_shader)
1791 device_remove_file(adev->dev, &dev_attr_run_cleaner_shader);
1792 }
1793
amdgpu_gfx_sysfs_reset_mask_init(struct amdgpu_device * adev)1794 static int amdgpu_gfx_sysfs_reset_mask_init(struct amdgpu_device *adev)
1795 {
1796 int r = 0;
1797
1798 if (!amdgpu_gpu_recovery)
1799 return r;
1800
1801 if (adev->gfx.num_gfx_rings) {
1802 r = device_create_file(adev->dev, &dev_attr_gfx_reset_mask);
1803 if (r)
1804 return r;
1805 }
1806
1807 if (adev->gfx.num_compute_rings) {
1808 r = device_create_file(adev->dev, &dev_attr_compute_reset_mask);
1809 if (r)
1810 return r;
1811 }
1812
1813 return r;
1814 }
1815
amdgpu_gfx_sysfs_reset_mask_fini(struct amdgpu_device * adev)1816 static void amdgpu_gfx_sysfs_reset_mask_fini(struct amdgpu_device *adev)
1817 {
1818 if (!amdgpu_gpu_recovery)
1819 return;
1820
1821 if (adev->gfx.num_gfx_rings)
1822 device_remove_file(adev->dev, &dev_attr_gfx_reset_mask);
1823
1824 if (adev->gfx.num_compute_rings)
1825 device_remove_file(adev->dev, &dev_attr_compute_reset_mask);
1826 }
1827
amdgpu_gfx_sysfs_init(struct amdgpu_device * adev)1828 int amdgpu_gfx_sysfs_init(struct amdgpu_device *adev)
1829 {
1830 int r;
1831
1832 r = amdgpu_gfx_sysfs_xcp_init(adev);
1833 if (r) {
1834 dev_err(adev->dev, "failed to create xcp sysfs files");
1835 return r;
1836 }
1837
1838 r = amdgpu_gfx_sysfs_isolation_shader_init(adev);
1839 if (r)
1840 dev_err(adev->dev, "failed to create isolation sysfs files");
1841
1842 r = amdgpu_gfx_sysfs_reset_mask_init(adev);
1843 if (r)
1844 dev_err(adev->dev, "failed to create reset mask sysfs files");
1845
1846 return r;
1847 }
1848
amdgpu_gfx_sysfs_fini(struct amdgpu_device * adev)1849 void amdgpu_gfx_sysfs_fini(struct amdgpu_device *adev)
1850 {
1851 if (adev->dev->kobj.sd) {
1852 amdgpu_gfx_sysfs_xcp_fini(adev);
1853 amdgpu_gfx_sysfs_isolation_shader_fini(adev);
1854 amdgpu_gfx_sysfs_reset_mask_fini(adev);
1855 }
1856 }
1857
amdgpu_gfx_cleaner_shader_sw_init(struct amdgpu_device * adev,unsigned int cleaner_shader_size)1858 int amdgpu_gfx_cleaner_shader_sw_init(struct amdgpu_device *adev,
1859 unsigned int cleaner_shader_size)
1860 {
1861 if (!adev->gfx.enable_cleaner_shader)
1862 return -EOPNOTSUPP;
1863
1864 return amdgpu_bo_create_kernel(adev, cleaner_shader_size, PAGE_SIZE,
1865 AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT,
1866 &adev->gfx.cleaner_shader_obj,
1867 &adev->gfx.cleaner_shader_gpu_addr,
1868 (void **)&adev->gfx.cleaner_shader_cpu_ptr);
1869 }
1870
amdgpu_gfx_cleaner_shader_sw_fini(struct amdgpu_device * adev)1871 void amdgpu_gfx_cleaner_shader_sw_fini(struct amdgpu_device *adev)
1872 {
1873 if (!adev->gfx.enable_cleaner_shader)
1874 return;
1875
1876 amdgpu_bo_free_kernel(&adev->gfx.cleaner_shader_obj,
1877 &adev->gfx.cleaner_shader_gpu_addr,
1878 (void **)&adev->gfx.cleaner_shader_cpu_ptr);
1879 }
1880
amdgpu_gfx_cleaner_shader_init(struct amdgpu_device * adev,unsigned int cleaner_shader_size,const void * cleaner_shader_ptr)1881 void amdgpu_gfx_cleaner_shader_init(struct amdgpu_device *adev,
1882 unsigned int cleaner_shader_size,
1883 const void *cleaner_shader_ptr)
1884 {
1885 if (!adev->gfx.enable_cleaner_shader)
1886 return;
1887
1888 if (adev->gfx.cleaner_shader_cpu_ptr && cleaner_shader_ptr)
1889 memcpy_toio(adev->gfx.cleaner_shader_cpu_ptr, cleaner_shader_ptr,
1890 cleaner_shader_size);
1891 }
1892
1893 /**
1894 * amdgpu_gfx_kfd_sch_ctrl - Control the KFD scheduler from the KGD (Graphics Driver)
1895 * @adev: amdgpu_device pointer
1896 * @idx: Index of the scheduler to control
1897 * @enable: Whether to enable or disable the KFD scheduler
1898 *
1899 * This function is used to control the KFD (Kernel Fusion Driver) scheduler
1900 * from the KGD. It is part of the cleaner shader feature. This function plays
1901 * a key role in enforcing process isolation on the GPU.
1902 *
1903 * The function uses a reference count mechanism (kfd_sch_req_count) to keep
1904 * track of the number of requests to enable the KFD scheduler. When a request
1905 * to enable the KFD scheduler is made, the reference count is decremented.
1906 * When the reference count reaches zero, a delayed work is scheduled to
1907 * enforce isolation after a delay of GFX_SLICE_PERIOD.
1908 *
1909 * When a request to disable the KFD scheduler is made, the function first
1910 * checks if the reference count is zero. If it is, it cancels the delayed work
1911 * for enforcing isolation and checks if the KFD scheduler is active. If the
1912 * KFD scheduler is active, it sends a request to stop the KFD scheduler and
1913 * sets the KFD scheduler state to inactive. Then, it increments the reference
1914 * count.
1915 *
1916 * The function is synchronized using the kfd_sch_mutex to ensure that the KFD
1917 * scheduler state and reference count are updated atomically.
1918 *
1919 * Note: If the reference count is already zero when a request to enable the
1920 * KFD scheduler is made, it means there's an imbalance bug somewhere. The
1921 * function triggers a warning in this case.
1922 */
amdgpu_gfx_kfd_sch_ctrl(struct amdgpu_device * adev,u32 idx,bool enable)1923 static void amdgpu_gfx_kfd_sch_ctrl(struct amdgpu_device *adev, u32 idx,
1924 bool enable)
1925 {
1926 mutex_lock(&adev->gfx.kfd_sch_mutex);
1927
1928 if (enable) {
1929 /* If the count is already 0, it means there's an imbalance bug somewhere.
1930 * Note that the bug may be in a different caller than the one which triggers the
1931 * WARN_ON_ONCE.
1932 */
1933 if (WARN_ON_ONCE(adev->gfx.kfd_sch_req_count[idx] == 0)) {
1934 dev_err(adev->dev, "Attempted to enable KFD scheduler when reference count is already zero\n");
1935 goto unlock;
1936 }
1937
1938 adev->gfx.kfd_sch_req_count[idx]--;
1939
1940 if (adev->gfx.kfd_sch_req_count[idx] == 0 &&
1941 adev->gfx.kfd_sch_inactive[idx]) {
1942 schedule_delayed_work(&adev->gfx.enforce_isolation[idx].work,
1943 msecs_to_jiffies(adev->gfx.enforce_isolation_time[idx]));
1944 }
1945 } else {
1946 if (adev->gfx.kfd_sch_req_count[idx] == 0) {
1947 cancel_delayed_work_sync(&adev->gfx.enforce_isolation[idx].work);
1948 if (!adev->gfx.kfd_sch_inactive[idx]) {
1949 amdgpu_amdkfd_stop_sched(adev, idx);
1950 adev->gfx.kfd_sch_inactive[idx] = true;
1951 }
1952 }
1953
1954 adev->gfx.kfd_sch_req_count[idx]++;
1955 }
1956
1957 unlock:
1958 mutex_unlock(&adev->gfx.kfd_sch_mutex);
1959 }
1960
1961 /**
1962 * amdgpu_gfx_enforce_isolation_handler - work handler for enforcing shader isolation
1963 *
1964 * @work: work_struct.
1965 *
1966 * This function is the work handler for enforcing shader isolation on AMD GPUs.
1967 * It counts the number of emitted fences for each GFX and compute ring. If there
1968 * are any fences, it schedules the `enforce_isolation_work` to be run after a
1969 * delay of `GFX_SLICE_PERIOD`. If there are no fences, it signals the Kernel Fusion
1970 * Driver (KFD) to resume the runqueue. The function is synchronized using the
1971 * `enforce_isolation_mutex`.
1972 */
amdgpu_gfx_enforce_isolation_handler(struct work_struct * work)1973 void amdgpu_gfx_enforce_isolation_handler(struct work_struct *work)
1974 {
1975 struct amdgpu_isolation_work *isolation_work =
1976 container_of(work, struct amdgpu_isolation_work, work.work);
1977 struct amdgpu_device *adev = isolation_work->adev;
1978 u32 i, idx, fences = 0;
1979
1980 if (isolation_work->xcp_id == AMDGPU_XCP_NO_PARTITION)
1981 idx = 0;
1982 else
1983 idx = isolation_work->xcp_id;
1984
1985 if (idx >= MAX_XCP)
1986 return;
1987
1988 mutex_lock(&adev->enforce_isolation_mutex);
1989 for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i) {
1990 if (isolation_work->xcp_id == adev->gfx.gfx_ring[i].xcp_id)
1991 fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
1992 }
1993 for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i) {
1994 if (isolation_work->xcp_id == adev->gfx.compute_ring[i].xcp_id)
1995 fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
1996 }
1997 if (fences) {
1998 /* we've already had our timeslice, so let's wrap this up */
1999 schedule_delayed_work(&adev->gfx.enforce_isolation[idx].work,
2000 msecs_to_jiffies(1));
2001 } else {
2002 /* Tell KFD to resume the runqueue */
2003 if (adev->kfd.init_complete) {
2004 WARN_ON_ONCE(!adev->gfx.kfd_sch_inactive[idx]);
2005 WARN_ON_ONCE(adev->gfx.kfd_sch_req_count[idx]);
2006 amdgpu_amdkfd_start_sched(adev, idx);
2007 adev->gfx.kfd_sch_inactive[idx] = false;
2008 }
2009 }
2010 mutex_unlock(&adev->enforce_isolation_mutex);
2011 }
2012
2013 /**
2014 * amdgpu_gfx_enforce_isolation_wait_for_kfd - Manage KFD wait period for process isolation
2015 * @adev: amdgpu_device pointer
2016 * @idx: Index of the GPU partition
2017 *
2018 * When kernel submissions come in, the jobs are given a time slice and once
2019 * that time slice is up, if there are KFD user queues active, kernel
2020 * submissions are blocked until KFD has had its time slice. Once the KFD time
2021 * slice is up, KFD user queues are preempted and kernel submissions are
2022 * unblocked and allowed to run again.
2023 */
2024 static void
amdgpu_gfx_enforce_isolation_wait_for_kfd(struct amdgpu_device * adev,u32 idx)2025 amdgpu_gfx_enforce_isolation_wait_for_kfd(struct amdgpu_device *adev,
2026 u32 idx)
2027 {
2028 unsigned long cjiffies;
2029 bool wait = false;
2030
2031 mutex_lock(&adev->enforce_isolation_mutex);
2032 if (adev->enforce_isolation[idx]) {
2033 /* set the initial values if nothing is set */
2034 if (!adev->gfx.enforce_isolation_jiffies[idx]) {
2035 adev->gfx.enforce_isolation_jiffies[idx] = jiffies;
2036 adev->gfx.enforce_isolation_time[idx] = GFX_SLICE_PERIOD_MS;
2037 }
2038 /* Make sure KFD gets a chance to run */
2039 if (amdgpu_amdkfd_compute_active(adev, idx)) {
2040 cjiffies = jiffies;
2041 if (time_after(cjiffies, adev->gfx.enforce_isolation_jiffies[idx])) {
2042 cjiffies -= adev->gfx.enforce_isolation_jiffies[idx];
2043 if ((jiffies_to_msecs(cjiffies) >= GFX_SLICE_PERIOD_MS)) {
2044 /* if our time is up, let KGD work drain before scheduling more */
2045 wait = true;
2046 /* reset the timer period */
2047 adev->gfx.enforce_isolation_time[idx] = GFX_SLICE_PERIOD_MS;
2048 } else {
2049 /* set the timer period to what's left in our time slice */
2050 adev->gfx.enforce_isolation_time[idx] =
2051 GFX_SLICE_PERIOD_MS - jiffies_to_msecs(cjiffies);
2052 }
2053 } else {
2054 /* if jiffies wrap around we will just wait a little longer */
2055 adev->gfx.enforce_isolation_jiffies[idx] = jiffies;
2056 }
2057 } else {
2058 /* if there is no KFD work, then set the full slice period */
2059 adev->gfx.enforce_isolation_jiffies[idx] = jiffies;
2060 adev->gfx.enforce_isolation_time[idx] = GFX_SLICE_PERIOD_MS;
2061 }
2062 }
2063 mutex_unlock(&adev->enforce_isolation_mutex);
2064
2065 if (wait)
2066 msleep(GFX_SLICE_PERIOD_MS);
2067 }
2068
2069 /**
2070 * amdgpu_gfx_enforce_isolation_ring_begin_use - Begin use of a ring with enforced isolation
2071 * @ring: Pointer to the amdgpu_ring structure
2072 *
2073 * Ring begin_use helper implementation for gfx which serializes access to the
2074 * gfx IP between kernel submission IOCTLs and KFD user queues when isolation
2075 * enforcement is enabled. The kernel submission IOCTLs and KFD user queues
2076 * each get a time slice when both are active.
2077 */
amdgpu_gfx_enforce_isolation_ring_begin_use(struct amdgpu_ring * ring)2078 void amdgpu_gfx_enforce_isolation_ring_begin_use(struct amdgpu_ring *ring)
2079 {
2080 struct amdgpu_device *adev = ring->adev;
2081 u32 idx;
2082 bool sched_work = false;
2083
2084 if (!adev->gfx.enable_cleaner_shader)
2085 return;
2086
2087 if (ring->xcp_id == AMDGPU_XCP_NO_PARTITION)
2088 idx = 0;
2089 else
2090 idx = ring->xcp_id;
2091
2092 if (idx >= MAX_XCP)
2093 return;
2094
2095 /* Don't submit more work until KFD has had some time */
2096 amdgpu_gfx_enforce_isolation_wait_for_kfd(adev, idx);
2097
2098 mutex_lock(&adev->enforce_isolation_mutex);
2099 if (adev->enforce_isolation[idx]) {
2100 if (adev->kfd.init_complete)
2101 sched_work = true;
2102 }
2103 mutex_unlock(&adev->enforce_isolation_mutex);
2104
2105 if (sched_work)
2106 amdgpu_gfx_kfd_sch_ctrl(adev, idx, false);
2107 }
2108
2109 /**
2110 * amdgpu_gfx_enforce_isolation_ring_end_use - End use of a ring with enforced isolation
2111 * @ring: Pointer to the amdgpu_ring structure
2112 *
2113 * Ring end_use helper implementation for gfx which serializes access to the
2114 * gfx IP between kernel submission IOCTLs and KFD user queues when isolation
2115 * enforcement is enabled. The kernel submission IOCTLs and KFD user queues
2116 * each get a time slice when both are active.
2117 */
amdgpu_gfx_enforce_isolation_ring_end_use(struct amdgpu_ring * ring)2118 void amdgpu_gfx_enforce_isolation_ring_end_use(struct amdgpu_ring *ring)
2119 {
2120 struct amdgpu_device *adev = ring->adev;
2121 u32 idx;
2122 bool sched_work = false;
2123
2124 if (!adev->gfx.enable_cleaner_shader)
2125 return;
2126
2127 if (ring->xcp_id == AMDGPU_XCP_NO_PARTITION)
2128 idx = 0;
2129 else
2130 idx = ring->xcp_id;
2131
2132 if (idx >= MAX_XCP)
2133 return;
2134
2135 mutex_lock(&adev->enforce_isolation_mutex);
2136 if (adev->enforce_isolation[idx]) {
2137 if (adev->kfd.init_complete)
2138 sched_work = true;
2139 }
2140 mutex_unlock(&adev->enforce_isolation_mutex);
2141
2142 if (sched_work)
2143 amdgpu_gfx_kfd_sch_ctrl(adev, idx, true);
2144 }
2145
amdgpu_gfx_profile_idle_work_handler(struct work_struct * work)2146 void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)
2147 {
2148 struct amdgpu_device *adev =
2149 container_of(work, struct amdgpu_device, gfx.idle_work.work);
2150 enum PP_SMC_POWER_PROFILE profile;
2151 u32 i, fences = 0;
2152 int r;
2153
2154 if (adev->gfx.num_gfx_rings)
2155 profile = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
2156 else
2157 profile = PP_SMC_POWER_PROFILE_COMPUTE;
2158
2159 for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i)
2160 fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
2161 for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i)
2162 fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
2163 if (!fences && !atomic_read(&adev->gfx.total_submission_cnt)) {
2164 mutex_lock(&adev->gfx.workload_profile_mutex);
2165 if (adev->gfx.workload_profile_active) {
2166 r = amdgpu_dpm_switch_power_profile(adev, profile, false);
2167 if (r)
2168 dev_warn(adev->dev, "(%d) failed to disable %s power profile mode\n", r,
2169 profile == PP_SMC_POWER_PROFILE_FULLSCREEN3D ?
2170 "fullscreen 3D" : "compute");
2171 adev->gfx.workload_profile_active = false;
2172 }
2173 mutex_unlock(&adev->gfx.workload_profile_mutex);
2174 } else {
2175 schedule_delayed_work(&adev->gfx.idle_work, GFX_PROFILE_IDLE_TIMEOUT);
2176 }
2177 }
2178
amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring * ring)2179 void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)
2180 {
2181 struct amdgpu_device *adev = ring->adev;
2182 enum PP_SMC_POWER_PROFILE profile;
2183 int r;
2184
2185 if (adev->gfx.num_gfx_rings)
2186 profile = PP_SMC_POWER_PROFILE_FULLSCREEN3D;
2187 else
2188 profile = PP_SMC_POWER_PROFILE_COMPUTE;
2189
2190 atomic_inc(&adev->gfx.total_submission_cnt);
2191
2192 cancel_delayed_work_sync(&adev->gfx.idle_work);
2193
2194 /* We can safely return early here because we've cancelled the
2195 * the delayed work so there is no one else to set it to false
2196 * and we don't care if someone else sets it to true.
2197 */
2198 if (adev->gfx.workload_profile_active)
2199 return;
2200
2201 mutex_lock(&adev->gfx.workload_profile_mutex);
2202 if (!adev->gfx.workload_profile_active) {
2203 r = amdgpu_dpm_switch_power_profile(adev, profile, true);
2204 if (r)
2205 dev_warn(adev->dev, "(%d) failed to disable %s power profile mode\n", r,
2206 profile == PP_SMC_POWER_PROFILE_FULLSCREEN3D ?
2207 "fullscreen 3D" : "compute");
2208 adev->gfx.workload_profile_active = true;
2209 }
2210 mutex_unlock(&adev->gfx.workload_profile_mutex);
2211 }
2212
amdgpu_gfx_profile_ring_end_use(struct amdgpu_ring * ring)2213 void amdgpu_gfx_profile_ring_end_use(struct amdgpu_ring *ring)
2214 {
2215 atomic_dec(&ring->adev->gfx.total_submission_cnt);
2216
2217 schedule_delayed_work(&ring->adev->gfx.idle_work, GFX_PROFILE_IDLE_TIMEOUT);
2218 }
2219
2220 /*
2221 * debugfs for to enable/disable gfx job submission to specific core.
2222 */
2223 #if defined(CONFIG_DEBUG_FS)
amdgpu_debugfs_gfx_sched_mask_set(void * data,u64 val)2224 static int amdgpu_debugfs_gfx_sched_mask_set(void *data, u64 val)
2225 {
2226 struct amdgpu_device *adev = (struct amdgpu_device *)data;
2227 u32 i;
2228 u64 mask = 0;
2229 struct amdgpu_ring *ring;
2230
2231 if (!adev)
2232 return -ENODEV;
2233
2234 mask = (1ULL << adev->gfx.num_gfx_rings) - 1;
2235 if ((val & mask) == 0)
2236 return -EINVAL;
2237
2238 for (i = 0; i < adev->gfx.num_gfx_rings; ++i) {
2239 ring = &adev->gfx.gfx_ring[i];
2240 if (val & (1 << i))
2241 ring->sched.ready = true;
2242 else
2243 ring->sched.ready = false;
2244 }
2245 /* publish sched.ready flag update effective immediately across smp */
2246 smp_rmb();
2247 return 0;
2248 }
2249
amdgpu_debugfs_gfx_sched_mask_get(void * data,u64 * val)2250 static int amdgpu_debugfs_gfx_sched_mask_get(void *data, u64 *val)
2251 {
2252 struct amdgpu_device *adev = (struct amdgpu_device *)data;
2253 u32 i;
2254 u64 mask = 0;
2255 struct amdgpu_ring *ring;
2256
2257 if (!adev)
2258 return -ENODEV;
2259 for (i = 0; i < adev->gfx.num_gfx_rings; ++i) {
2260 ring = &adev->gfx.gfx_ring[i];
2261 if (ring->sched.ready)
2262 mask |= 1ULL << i;
2263 }
2264
2265 *val = mask;
2266 return 0;
2267 }
2268
2269 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_gfx_sched_mask_fops,
2270 amdgpu_debugfs_gfx_sched_mask_get,
2271 amdgpu_debugfs_gfx_sched_mask_set, "%llx\n");
2272
2273 #endif
2274
amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device * adev)2275 void amdgpu_debugfs_gfx_sched_mask_init(struct amdgpu_device *adev)
2276 {
2277 #if defined(CONFIG_DEBUG_FS)
2278 struct drm_minor *minor = adev_to_drm(adev)->primary;
2279 struct dentry *root = minor->debugfs_root;
2280 char name[32];
2281
2282 if (!(adev->gfx.num_gfx_rings > 1))
2283 return;
2284 sprintf(name, "amdgpu_gfx_sched_mask");
2285 debugfs_create_file(name, 0600, root, adev,
2286 &amdgpu_debugfs_gfx_sched_mask_fops);
2287 #endif
2288 }
2289
2290 /*
2291 * debugfs for to enable/disable compute job submission to specific core.
2292 */
2293 #if defined(CONFIG_DEBUG_FS)
amdgpu_debugfs_compute_sched_mask_set(void * data,u64 val)2294 static int amdgpu_debugfs_compute_sched_mask_set(void *data, u64 val)
2295 {
2296 struct amdgpu_device *adev = (struct amdgpu_device *)data;
2297 u32 i;
2298 u64 mask = 0;
2299 struct amdgpu_ring *ring;
2300
2301 if (!adev)
2302 return -ENODEV;
2303
2304 mask = (1ULL << adev->gfx.num_compute_rings) - 1;
2305 if ((val & mask) == 0)
2306 return -EINVAL;
2307
2308 for (i = 0; i < adev->gfx.num_compute_rings; ++i) {
2309 ring = &adev->gfx.compute_ring[i];
2310 if (val & (1 << i))
2311 ring->sched.ready = true;
2312 else
2313 ring->sched.ready = false;
2314 }
2315
2316 /* publish sched.ready flag update effective immediately across smp */
2317 smp_rmb();
2318 return 0;
2319 }
2320
amdgpu_debugfs_compute_sched_mask_get(void * data,u64 * val)2321 static int amdgpu_debugfs_compute_sched_mask_get(void *data, u64 *val)
2322 {
2323 struct amdgpu_device *adev = (struct amdgpu_device *)data;
2324 u32 i;
2325 u64 mask = 0;
2326 struct amdgpu_ring *ring;
2327
2328 if (!adev)
2329 return -ENODEV;
2330 for (i = 0; i < adev->gfx.num_compute_rings; ++i) {
2331 ring = &adev->gfx.compute_ring[i];
2332 if (ring->sched.ready)
2333 mask |= 1ULL << i;
2334 }
2335
2336 *val = mask;
2337 return 0;
2338 }
2339
2340 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_compute_sched_mask_fops,
2341 amdgpu_debugfs_compute_sched_mask_get,
2342 amdgpu_debugfs_compute_sched_mask_set, "%llx\n");
2343
2344 #endif
2345
amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device * adev)2346 void amdgpu_debugfs_compute_sched_mask_init(struct amdgpu_device *adev)
2347 {
2348 #if defined(CONFIG_DEBUG_FS)
2349 struct drm_minor *minor = adev_to_drm(adev)->primary;
2350 struct dentry *root = minor->debugfs_root;
2351 char name[32];
2352
2353 if (!(adev->gfx.num_compute_rings > 1))
2354 return;
2355 sprintf(name, "amdgpu_compute_sched_mask");
2356 debugfs_create_file(name, 0600, root, adev,
2357 &amdgpu_debugfs_compute_sched_mask_fops);
2358 #endif
2359 }
2360