2 * Copyright 2014 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
26 #include <linux/firmware.h>
27 #include <linux/pm_runtime.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"
37 /* delay 0.1 second to enable gfx off feature */
38 #define GFX_OFF_DELAY_ENABLE msecs_to_jiffies(100)
40 #define GFX_OFF_NO_DELAY 0
43 * GPU GFX IP block helpers function.
46 int amdgpu_gfx_mec_queue_to_bit(struct amdgpu_device *adev, int mec,
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;
59 void amdgpu_queue_mask_bit_to_mec_queue(struct amdgpu_device *adev, int bit,
60 int *mec, int *pipe, int *queue)
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;
70 bool amdgpu_gfx_is_mec_queue_enabled(struct amdgpu_device *adev,
71 int xcc_id, int mec, int pipe, int queue)
73 return test_bit(amdgpu_gfx_mec_queue_to_bit(adev, mec, pipe, queue),
74 adev->gfx.mec_bitmap[xcc_id].queue_bitmap);
77 int amdgpu_gfx_me_queue_to_bit(struct amdgpu_device *adev,
78 int me, int pipe, int queue)
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;
90 bool amdgpu_gfx_is_me_queue_enabled(struct amdgpu_device *adev,
91 int me, int pipe, int queue)
93 return test_bit(amdgpu_gfx_me_queue_to_bit(adev, me, pipe, queue),
94 adev->gfx.me.queue_bitmap);
98 * amdgpu_gfx_parse_disable_cu - Parse the disable_cu module parameter
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
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].
107 void amdgpu_gfx_parse_disable_cu(unsigned int *mask, unsigned int max_se, unsigned int max_sh)
109 unsigned int se, sh, cu;
112 memset(mask, 0, sizeof(*mask) * max_se * max_sh);
114 if (!amdgpu_disable_cu || !*amdgpu_disable_cu)
117 p = amdgpu_disable_cu;
120 int ret = sscanf(p, "%u.%u.%u", &se, &sh, &cu);
123 DRM_ERROR("amdgpu: could not parse disable_cu\n");
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;
131 DRM_ERROR("amdgpu: disable_cu %u.%u.%u is out of range\n",
135 next = strchr(p, ',');
142 static bool amdgpu_gfx_is_graphics_multipipe_capable(struct amdgpu_device *adev)
144 return amdgpu_async_gfx_ring && adev->gfx.me.num_pipe_per_me > 1;
147 static bool amdgpu_gfx_is_compute_multipipe_capable(struct amdgpu_device *adev)
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;
155 if (amdgpu_ip_version(adev, GC_HWIP, 0) > IP_VERSION(9, 0, 0))
158 /* FIXME: spreading the queues across pipes causes perf regressions
159 * on POLARIS11 compute workloads */
160 if (adev->asic_type == CHIP_POLARIS11)
163 return adev->gfx.mec.num_mec > 1;
166 bool amdgpu_gfx_is_high_priority_graphics_queue(struct amdgpu_device *adev,
167 struct amdgpu_ring *ring)
169 int queue = ring->queue;
170 int pipe = ring->pipe;
172 /* Policy: use pipe1 queue0 as high priority graphics queue if we
173 * have more than one gfx pipe.
175 if (amdgpu_gfx_is_graphics_multipipe_capable(adev) &&
176 adev->gfx.num_gfx_rings > 1 && pipe == 1 && queue == 0) {
180 bit = amdgpu_gfx_me_queue_to_bit(adev, me, pipe, queue);
181 if (ring == &adev->gfx.gfx_ring[bit])
188 bool amdgpu_gfx_is_high_priority_compute_queue(struct amdgpu_device *adev,
189 struct amdgpu_ring *ring)
191 /* Policy: use 1st queue as high priority compute queue if we
192 * have more than one compute queue.
194 if (adev->gfx.num_compute_rings > 1 &&
195 ring == &adev->gfx.compute_ring[0])
201 void amdgpu_gfx_compute_queue_acquire(struct amdgpu_device *adev)
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;
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;
219 set_bit(pipe * adev->gfx.mec.num_queue_per_pipe + queue,
220 adev->gfx.mec_bitmap[j].queue_bitmap);
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);
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));
237 void amdgpu_gfx_graphics_queue_acquire(struct amdgpu_device *adev)
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;
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;
252 set_bit(pipe * adev->gfx.me.num_queue_per_pipe + queue,
253 adev->gfx.me.queue_bitmap);
256 for (i = 0; i < max_queues_per_me; ++i)
257 set_bit(i, adev->gfx.me.queue_bitmap);
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);
265 static int amdgpu_gfx_kiq_acquire(struct amdgpu_device *adev,
266 struct amdgpu_ring *ring, int xcc_id)
269 int mec, pipe, queue;
271 queue_bit = adev->gfx.mec.num_mec
272 * adev->gfx.mec.num_pipe_per_mec
273 * adev->gfx.mec.num_queue_per_pipe;
275 while (--queue_bit >= 0) {
276 if (test_bit(queue_bit, adev->gfx.mec_bitmap[xcc_id].queue_bitmap))
279 amdgpu_queue_mask_bit_to_mec_queue(adev, queue_bit, &mec, &pipe, &queue);
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.
286 if ((mec == 1 && pipe > 1) || queue != 0)
296 dev_err(adev->dev, "Failed to find a queue for KIQ\n");
300 int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev, int xcc_id)
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;
307 spin_lock_init(&kiq->ring_lock);
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)
319 r = amdgpu_gfx_kiq_acquire(adev, ring, xcc_id);
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);
331 dev_warn(adev->dev, "(%d) failed to init kiq ring\n", r);
336 void amdgpu_gfx_kiq_free_ring(struct amdgpu_ring *ring)
338 amdgpu_ring_fini(ring);
341 void amdgpu_gfx_kiq_fini(struct amdgpu_device *adev, int xcc_id)
343 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
345 amdgpu_bo_free_kernel(&kiq->eop_obj, &kiq->eop_gpu_addr, NULL);
348 int amdgpu_gfx_kiq_init(struct amdgpu_device *adev,
349 unsigned int hpd_size, int xcc_id)
353 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
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);
359 dev_warn(adev->dev, "failed to create KIQ bo (%d).\n", r);
363 memset(hpd, 0, hpd_size);
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);
374 /* create MQD for each compute/gfx queue */
375 int amdgpu_gfx_mqd_sw_init(struct amdgpu_device *adev,
376 unsigned int mqd_size, int xcc_id)
379 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
380 struct amdgpu_ring *ring = &kiq->ring;
381 u32 domain = AMDGPU_GEM_DOMAIN_GTT;
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;
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
396 r = amdgpu_bo_create_kernel(adev, mqd_size, PAGE_SIZE,
397 AMDGPU_GEM_DOMAIN_VRAM |
398 AMDGPU_GEM_DOMAIN_GTT,
403 dev_warn(adev->dev, "failed to create ring mqd ob (%d)", r);
407 /* prepare MQD backup */
408 kiq->mqd_backup = kzalloc(mqd_size, GFP_KERNEL);
409 if (!kiq->mqd_backup) {
411 "no memory to create MQD backup for ring %s\n", ring->name);
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);
425 dev_warn(adev->dev, "failed to create ring mqd bo (%d)", r);
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);
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);
449 dev_warn(adev->dev, "failed to create ring mqd bo (%d)", r);
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);
466 void amdgpu_gfx_mqd_sw_fini(struct amdgpu_device *adev, int xcc_id)
468 struct amdgpu_ring *ring = NULL;
470 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
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,
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,
492 kfree(kiq->mqd_backup);
493 amdgpu_bo_free_kernel(&ring->mqd_obj,
498 int amdgpu_gfx_disable_kcq(struct amdgpu_device *adev, int xcc_id)
500 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
501 struct amdgpu_ring *kiq_ring = &kiq->ring;
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],
515 if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
518 spin_lock(&kiq->ring_lock);
519 if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size *
520 adev->gfx.num_compute_rings)) {
521 spin_unlock(&kiq->ring_lock);
525 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
526 j = i + xcc_id * adev->gfx.num_compute_rings;
527 kiq->pmf->kiq_unmap_queues(kiq_ring,
528 &adev->gfx.compute_ring[j],
533 * This is workaround: only skip kiq_ring test
534 * during ras recovery in suspend stage for gfx9.4.3
536 if ((amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 3) ||
537 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 4)) &&
538 amdgpu_ras_in_recovery(adev)) {
539 spin_unlock(&kiq->ring_lock);
543 if (kiq_ring->sched.ready && !adev->job_hang)
544 r = amdgpu_ring_test_helper(kiq_ring);
545 spin_unlock(&kiq->ring_lock);
550 int amdgpu_gfx_disable_kgq(struct amdgpu_device *adev, int xcc_id)
552 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
553 struct amdgpu_ring *kiq_ring = &kiq->ring;
557 if (adev->enable_mes) {
558 if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
559 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
560 j = i + xcc_id * adev->gfx.num_gfx_rings;
561 amdgpu_mes_unmap_legacy_queue(adev,
562 &adev->gfx.gfx_ring[j],
563 PREEMPT_QUEUES, 0, 0);
569 if (!kiq->pmf || !kiq->pmf->kiq_unmap_queues)
572 spin_lock(&kiq->ring_lock);
573 if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
574 if (amdgpu_ring_alloc(kiq_ring, kiq->pmf->unmap_queues_size *
575 adev->gfx.num_gfx_rings)) {
576 spin_unlock(&kiq->ring_lock);
580 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
581 j = i + xcc_id * adev->gfx.num_gfx_rings;
582 kiq->pmf->kiq_unmap_queues(kiq_ring,
583 &adev->gfx.gfx_ring[j],
584 PREEMPT_QUEUES, 0, 0);
588 if (adev->gfx.kiq[0].ring.sched.ready && !adev->job_hang)
589 r = amdgpu_ring_test_helper(kiq_ring);
590 spin_unlock(&kiq->ring_lock);
595 int amdgpu_queue_mask_bit_to_set_resource_bit(struct amdgpu_device *adev,
598 int mec, pipe, queue;
599 int set_resource_bit = 0;
601 amdgpu_queue_mask_bit_to_mec_queue(adev, queue_bit, &mec, &pipe, &queue);
603 set_resource_bit = mec * 4 * 8 + pipe * 8 + queue;
605 return set_resource_bit;
608 static int amdgpu_gfx_mes_enable_kcq(struct amdgpu_device *adev, int xcc_id)
610 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
611 struct amdgpu_ring *kiq_ring = &kiq->ring;
612 uint64_t queue_mask = ~0ULL;
615 amdgpu_device_flush_hdp(adev, NULL);
617 if (!adev->enable_uni_mes) {
618 spin_lock(&kiq->ring_lock);
619 r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->set_resources_size);
621 dev_err(adev->dev, "Failed to lock KIQ (%d).\n", r);
622 spin_unlock(&kiq->ring_lock);
626 kiq->pmf->kiq_set_resources(kiq_ring, queue_mask);
627 r = amdgpu_ring_test_helper(kiq_ring);
628 spin_unlock(&kiq->ring_lock);
630 dev_err(adev->dev, "KIQ failed to set resources\n");
633 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
634 j = i + xcc_id * adev->gfx.num_compute_rings;
635 r = amdgpu_mes_map_legacy_queue(adev,
636 &adev->gfx.compute_ring[j]);
638 dev_err(adev->dev, "failed to map compute queue\n");
646 int amdgpu_gfx_enable_kcq(struct amdgpu_device *adev, int xcc_id)
648 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
649 struct amdgpu_ring *kiq_ring = &kiq->ring;
650 uint64_t queue_mask = 0;
653 if (adev->mes.enable_legacy_queue_map)
654 return amdgpu_gfx_mes_enable_kcq(adev, xcc_id);
656 if (!kiq->pmf || !kiq->pmf->kiq_map_queues || !kiq->pmf->kiq_set_resources)
659 for (i = 0; i < AMDGPU_MAX_COMPUTE_QUEUES; ++i) {
660 if (!test_bit(i, adev->gfx.mec_bitmap[xcc_id].queue_bitmap))
663 /* This situation may be hit in the future if a new HW
664 * generation exposes more than 64 queues. If so, the
665 * definition of queue_mask needs updating */
666 if (WARN_ON(i > (sizeof(queue_mask)*8))) {
667 DRM_ERROR("Invalid KCQ enabled: %d\n", i);
671 queue_mask |= (1ull << amdgpu_queue_mask_bit_to_set_resource_bit(adev, i));
674 amdgpu_device_flush_hdp(adev, NULL);
676 DRM_INFO("kiq ring mec %d pipe %d q %d\n", kiq_ring->me, kiq_ring->pipe,
679 spin_lock(&kiq->ring_lock);
680 r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->map_queues_size *
681 adev->gfx.num_compute_rings +
682 kiq->pmf->set_resources_size);
684 DRM_ERROR("Failed to lock KIQ (%d).\n", r);
685 spin_unlock(&kiq->ring_lock);
689 kiq->pmf->kiq_set_resources(kiq_ring, queue_mask);
690 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
691 j = i + xcc_id * adev->gfx.num_compute_rings;
692 kiq->pmf->kiq_map_queues(kiq_ring,
693 &adev->gfx.compute_ring[j]);
696 r = amdgpu_ring_test_helper(kiq_ring);
697 spin_unlock(&kiq->ring_lock);
699 DRM_ERROR("KCQ enable failed\n");
704 int amdgpu_gfx_enable_kgq(struct amdgpu_device *adev, int xcc_id)
706 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
707 struct amdgpu_ring *kiq_ring = &kiq->ring;
710 if (!kiq->pmf || !kiq->pmf->kiq_map_queues)
713 amdgpu_device_flush_hdp(adev, NULL);
715 if (adev->mes.enable_legacy_queue_map) {
716 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
717 j = i + xcc_id * adev->gfx.num_gfx_rings;
718 r = amdgpu_mes_map_legacy_queue(adev,
719 &adev->gfx.gfx_ring[j]);
721 DRM_ERROR("failed to map gfx queue\n");
729 spin_lock(&kiq->ring_lock);
730 /* No need to map kcq on the slave */
731 if (amdgpu_gfx_is_master_xcc(adev, xcc_id)) {
732 r = amdgpu_ring_alloc(kiq_ring, kiq->pmf->map_queues_size *
733 adev->gfx.num_gfx_rings);
735 DRM_ERROR("Failed to lock KIQ (%d).\n", r);
736 spin_unlock(&kiq->ring_lock);
740 for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
741 j = i + xcc_id * adev->gfx.num_gfx_rings;
742 kiq->pmf->kiq_map_queues(kiq_ring,
743 &adev->gfx.gfx_ring[j]);
747 r = amdgpu_ring_test_helper(kiq_ring);
748 spin_unlock(&kiq->ring_lock);
750 DRM_ERROR("KGQ enable failed\n");
755 /* amdgpu_gfx_off_ctrl - Handle gfx off feature enable/disable
757 * @adev: amdgpu_device pointer
758 * @bool enable true: enable gfx off feature, false: disable gfx off feature
760 * 1. gfx off feature will be enabled by gfx ip after gfx cg gp enabled.
761 * 2. other client can send request to disable gfx off feature, the request should be honored.
762 * 3. other client can cancel their request of disable gfx off feature
763 * 4. other client should not send request to enable gfx off feature before disable gfx off feature.
766 void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
768 unsigned long delay = GFX_OFF_DELAY_ENABLE;
770 if (!(adev->pm.pp_feature & PP_GFXOFF_MASK))
773 mutex_lock(&adev->gfx.gfx_off_mutex);
776 /* If the count is already 0, it means there's an imbalance bug somewhere.
777 * Note that the bug may be in a different caller than the one which triggers the
780 if (WARN_ON_ONCE(adev->gfx.gfx_off_req_count == 0))
783 adev->gfx.gfx_off_req_count--;
785 if (adev->gfx.gfx_off_req_count == 0 &&
786 !adev->gfx.gfx_off_state) {
787 /* If going to s2idle, no need to wait */
789 if (!amdgpu_dpm_set_powergating_by_smu(adev,
790 AMD_IP_BLOCK_TYPE_GFX, true))
791 adev->gfx.gfx_off_state = true;
793 schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
798 if (adev->gfx.gfx_off_req_count == 0) {
799 cancel_delayed_work_sync(&adev->gfx.gfx_off_delay_work);
801 if (adev->gfx.gfx_off_state &&
802 !amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, false)) {
803 adev->gfx.gfx_off_state = false;
805 if (adev->gfx.funcs->init_spm_golden) {
807 "GFXOFF is disabled, re-init SPM golden settings\n");
808 amdgpu_gfx_init_spm_golden(adev);
813 adev->gfx.gfx_off_req_count++;
817 mutex_unlock(&adev->gfx.gfx_off_mutex);
820 int amdgpu_set_gfx_off_residency(struct amdgpu_device *adev, bool value)
824 mutex_lock(&adev->gfx.gfx_off_mutex);
826 r = amdgpu_dpm_set_residency_gfxoff(adev, value);
828 mutex_unlock(&adev->gfx.gfx_off_mutex);
833 int amdgpu_get_gfx_off_residency(struct amdgpu_device *adev, u32 *value)
837 mutex_lock(&adev->gfx.gfx_off_mutex);
839 r = amdgpu_dpm_get_residency_gfxoff(adev, value);
841 mutex_unlock(&adev->gfx.gfx_off_mutex);
846 int amdgpu_get_gfx_off_entrycount(struct amdgpu_device *adev, u64 *value)
850 mutex_lock(&adev->gfx.gfx_off_mutex);
852 r = amdgpu_dpm_get_entrycount_gfxoff(adev, value);
854 mutex_unlock(&adev->gfx.gfx_off_mutex);
859 int amdgpu_get_gfx_off_status(struct amdgpu_device *adev, uint32_t *value)
864 mutex_lock(&adev->gfx.gfx_off_mutex);
866 r = amdgpu_dpm_get_status_gfxoff(adev, value);
868 mutex_unlock(&adev->gfx.gfx_off_mutex);
873 int amdgpu_gfx_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
877 if (amdgpu_ras_is_supported(adev, ras_block->block)) {
878 if (!amdgpu_persistent_edc_harvesting_supported(adev)) {
879 r = amdgpu_ras_reset_error_status(adev, AMDGPU_RAS_BLOCK__GFX);
884 r = amdgpu_ras_block_late_init(adev, ras_block);
888 if (adev->gfx.cp_ecc_error_irq.funcs) {
889 r = amdgpu_irq_get(adev, &adev->gfx.cp_ecc_error_irq, 0);
894 amdgpu_ras_feature_enable_on_boot(adev, ras_block, 0);
899 amdgpu_ras_block_late_fini(adev, ras_block);
903 int amdgpu_gfx_ras_sw_init(struct amdgpu_device *adev)
906 struct amdgpu_gfx_ras *ras = NULL;
908 /* adev->gfx.ras is NULL, which means gfx does not
909 * support ras function, then do nothing here.
916 err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
918 dev_err(adev->dev, "Failed to register gfx ras block!\n");
922 strcpy(ras->ras_block.ras_comm.name, "gfx");
923 ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__GFX;
924 ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
925 adev->gfx.ras_if = &ras->ras_block.ras_comm;
927 /* If not define special ras_late_init function, use gfx default ras_late_init */
928 if (!ras->ras_block.ras_late_init)
929 ras->ras_block.ras_late_init = amdgpu_gfx_ras_late_init;
931 /* If not defined special ras_cb function, use default ras_cb */
932 if (!ras->ras_block.ras_cb)
933 ras->ras_block.ras_cb = amdgpu_gfx_process_ras_data_cb;
938 int amdgpu_gfx_poison_consumption_handler(struct amdgpu_device *adev,
939 struct amdgpu_iv_entry *entry)
941 if (adev->gfx.ras && adev->gfx.ras->poison_consumption_handler)
942 return adev->gfx.ras->poison_consumption_handler(adev, entry);
947 int amdgpu_gfx_process_ras_data_cb(struct amdgpu_device *adev,
949 struct amdgpu_iv_entry *entry)
951 /* TODO ue will trigger an interrupt.
953 * When “Full RAS” is enabled, the per-IP interrupt sources should
954 * be disabled and the driver should only look for the aggregated
955 * interrupt via sync flood
957 if (!amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__GFX)) {
958 kgd2kfd_set_sram_ecc_flag(adev->kfd.dev);
959 if (adev->gfx.ras && adev->gfx.ras->ras_block.hw_ops &&
960 adev->gfx.ras->ras_block.hw_ops->query_ras_error_count)
961 adev->gfx.ras->ras_block.hw_ops->query_ras_error_count(adev, err_data);
962 amdgpu_ras_reset_gpu(adev);
964 return AMDGPU_RAS_SUCCESS;
967 int amdgpu_gfx_cp_ecc_error_irq(struct amdgpu_device *adev,
968 struct amdgpu_irq_src *source,
969 struct amdgpu_iv_entry *entry)
971 struct ras_common_if *ras_if = adev->gfx.ras_if;
972 struct ras_dispatch_if ih_data = {
979 ih_data.head = *ras_if;
981 DRM_ERROR("CP ECC ERROR IRQ\n");
982 amdgpu_ras_interrupt_dispatch(adev, &ih_data);
986 void amdgpu_gfx_ras_error_func(struct amdgpu_device *adev,
987 void *ras_error_status,
988 void (*func)(struct amdgpu_device *adev, void *ras_error_status,
992 int num_xcc = adev->gfx.xcc_mask ? NUM_XCC(adev->gfx.xcc_mask) : 1;
993 uint32_t xcc_mask = GENMASK(num_xcc - 1, 0);
994 struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
997 err_data->ue_count = 0;
998 err_data->ce_count = 0;
1001 for_each_inst(i, xcc_mask)
1002 func(adev, ras_error_status, i);
1005 uint32_t amdgpu_kiq_rreg(struct amdgpu_device *adev, uint32_t reg, uint32_t xcc_id)
1007 signed long r, cnt = 0;
1008 unsigned long flags;
1009 uint32_t seq, reg_val_offs = 0, value = 0;
1010 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
1011 struct amdgpu_ring *ring = &kiq->ring;
1013 if (amdgpu_device_skip_hw_access(adev))
1016 if (adev->mes.ring[0].sched.ready)
1017 return amdgpu_mes_rreg(adev, reg);
1019 BUG_ON(!ring->funcs->emit_rreg);
1021 spin_lock_irqsave(&kiq->ring_lock, flags);
1022 if (amdgpu_device_wb_get(adev, ®_val_offs)) {
1023 pr_err("critical bug! too many kiq readers\n");
1026 r = amdgpu_ring_alloc(ring, 32);
1030 amdgpu_ring_emit_rreg(ring, reg, reg_val_offs);
1031 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
1035 amdgpu_ring_commit(ring);
1036 spin_unlock_irqrestore(&kiq->ring_lock, flags);
1038 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1040 /* don't wait anymore for gpu reset case because this way may
1041 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
1042 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
1043 * never return if we keep waiting in virt_kiq_rreg, which cause
1044 * gpu_recover() hang there.
1046 * also don't wait anymore for IRQ context
1048 if (r < 1 && (amdgpu_in_reset(adev) || in_interrupt()))
1049 goto failed_kiq_read;
1052 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
1053 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
1054 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1057 if (cnt > MAX_KIQ_REG_TRY)
1058 goto failed_kiq_read;
1061 value = adev->wb.wb[reg_val_offs];
1062 amdgpu_device_wb_free(adev, reg_val_offs);
1066 amdgpu_ring_undo(ring);
1068 spin_unlock_irqrestore(&kiq->ring_lock, flags);
1071 amdgpu_device_wb_free(adev, reg_val_offs);
1072 dev_err(adev->dev, "failed to read reg:%x\n", reg);
1076 void amdgpu_kiq_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v, uint32_t xcc_id)
1078 signed long r, cnt = 0;
1079 unsigned long flags;
1081 struct amdgpu_kiq *kiq = &adev->gfx.kiq[xcc_id];
1082 struct amdgpu_ring *ring = &kiq->ring;
1084 BUG_ON(!ring->funcs->emit_wreg);
1086 if (amdgpu_device_skip_hw_access(adev))
1089 if (adev->mes.ring[0].sched.ready) {
1090 amdgpu_mes_wreg(adev, reg, v);
1094 spin_lock_irqsave(&kiq->ring_lock, flags);
1095 r = amdgpu_ring_alloc(ring, 32);
1099 amdgpu_ring_emit_wreg(ring, reg, v);
1100 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
1104 amdgpu_ring_commit(ring);
1105 spin_unlock_irqrestore(&kiq->ring_lock, flags);
1107 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1109 /* don't wait anymore for gpu reset case because this way may
1110 * block gpu_recover() routine forever, e.g. this virt_kiq_rreg
1111 * is triggered in TTM and ttm_bo_lock_delayed_workqueue() will
1112 * never return if we keep waiting in virt_kiq_rreg, which cause
1113 * gpu_recover() hang there.
1115 * also don't wait anymore for IRQ context
1117 if (r < 1 && (amdgpu_in_reset(adev) || in_interrupt()))
1118 goto failed_kiq_write;
1121 while (r < 1 && cnt++ < MAX_KIQ_REG_TRY) {
1123 msleep(MAX_KIQ_REG_BAILOUT_INTERVAL);
1124 r = amdgpu_fence_wait_polling(ring, seq, MAX_KIQ_REG_WAIT);
1127 if (cnt > MAX_KIQ_REG_TRY)
1128 goto failed_kiq_write;
1133 amdgpu_ring_undo(ring);
1135 spin_unlock_irqrestore(&kiq->ring_lock, flags);
1137 dev_err(adev->dev, "failed to write reg:%x\n", reg);
1140 int amdgpu_gfx_get_num_kcq(struct amdgpu_device *adev)
1142 if (amdgpu_num_kcq == -1) {
1144 } else if (amdgpu_num_kcq > 8 || amdgpu_num_kcq < 0) {
1145 dev_warn(adev->dev, "set kernel compute queue number to 8 due to invalid parameter provided by user\n");
1148 return amdgpu_num_kcq;
1151 void amdgpu_gfx_cp_init_microcode(struct amdgpu_device *adev,
1154 const struct gfx_firmware_header_v1_0 *cp_hdr;
1155 const struct gfx_firmware_header_v2_0 *cp_hdr_v2_0;
1156 struct amdgpu_firmware_info *info = NULL;
1157 const struct firmware *ucode_fw;
1158 unsigned int fw_size;
1161 case AMDGPU_UCODE_ID_CP_PFP:
1162 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1163 adev->gfx.pfp_fw->data;
1164 adev->gfx.pfp_fw_version =
1165 le32_to_cpu(cp_hdr->header.ucode_version);
1166 adev->gfx.pfp_feature_version =
1167 le32_to_cpu(cp_hdr->ucode_feature_version);
1168 ucode_fw = adev->gfx.pfp_fw;
1169 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1171 case AMDGPU_UCODE_ID_CP_RS64_PFP:
1172 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1173 adev->gfx.pfp_fw->data;
1174 adev->gfx.pfp_fw_version =
1175 le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1176 adev->gfx.pfp_feature_version =
1177 le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1178 ucode_fw = adev->gfx.pfp_fw;
1179 fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1181 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
1182 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
1183 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1184 adev->gfx.pfp_fw->data;
1185 ucode_fw = adev->gfx.pfp_fw;
1186 fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1188 case AMDGPU_UCODE_ID_CP_ME:
1189 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1190 adev->gfx.me_fw->data;
1191 adev->gfx.me_fw_version =
1192 le32_to_cpu(cp_hdr->header.ucode_version);
1193 adev->gfx.me_feature_version =
1194 le32_to_cpu(cp_hdr->ucode_feature_version);
1195 ucode_fw = adev->gfx.me_fw;
1196 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1198 case AMDGPU_UCODE_ID_CP_RS64_ME:
1199 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1200 adev->gfx.me_fw->data;
1201 adev->gfx.me_fw_version =
1202 le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1203 adev->gfx.me_feature_version =
1204 le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1205 ucode_fw = adev->gfx.me_fw;
1206 fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1208 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
1209 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
1210 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1211 adev->gfx.me_fw->data;
1212 ucode_fw = adev->gfx.me_fw;
1213 fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1215 case AMDGPU_UCODE_ID_CP_CE:
1216 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1217 adev->gfx.ce_fw->data;
1218 adev->gfx.ce_fw_version =
1219 le32_to_cpu(cp_hdr->header.ucode_version);
1220 adev->gfx.ce_feature_version =
1221 le32_to_cpu(cp_hdr->ucode_feature_version);
1222 ucode_fw = adev->gfx.ce_fw;
1223 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
1225 case AMDGPU_UCODE_ID_CP_MEC1:
1226 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1227 adev->gfx.mec_fw->data;
1228 adev->gfx.mec_fw_version =
1229 le32_to_cpu(cp_hdr->header.ucode_version);
1230 adev->gfx.mec_feature_version =
1231 le32_to_cpu(cp_hdr->ucode_feature_version);
1232 ucode_fw = adev->gfx.mec_fw;
1233 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
1234 le32_to_cpu(cp_hdr->jt_size) * 4;
1236 case AMDGPU_UCODE_ID_CP_MEC1_JT:
1237 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1238 adev->gfx.mec_fw->data;
1239 ucode_fw = adev->gfx.mec_fw;
1240 fw_size = le32_to_cpu(cp_hdr->jt_size) * 4;
1242 case AMDGPU_UCODE_ID_CP_MEC2:
1243 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1244 adev->gfx.mec2_fw->data;
1245 adev->gfx.mec2_fw_version =
1246 le32_to_cpu(cp_hdr->header.ucode_version);
1247 adev->gfx.mec2_feature_version =
1248 le32_to_cpu(cp_hdr->ucode_feature_version);
1249 ucode_fw = adev->gfx.mec2_fw;
1250 fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
1251 le32_to_cpu(cp_hdr->jt_size) * 4;
1253 case AMDGPU_UCODE_ID_CP_MEC2_JT:
1254 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
1255 adev->gfx.mec2_fw->data;
1256 ucode_fw = adev->gfx.mec2_fw;
1257 fw_size = le32_to_cpu(cp_hdr->jt_size) * 4;
1259 case AMDGPU_UCODE_ID_CP_RS64_MEC:
1260 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1261 adev->gfx.mec_fw->data;
1262 adev->gfx.mec_fw_version =
1263 le32_to_cpu(cp_hdr_v2_0->header.ucode_version);
1264 adev->gfx.mec_feature_version =
1265 le32_to_cpu(cp_hdr_v2_0->ucode_feature_version);
1266 ucode_fw = adev->gfx.mec_fw;
1267 fw_size = le32_to_cpu(cp_hdr_v2_0->ucode_size_bytes);
1269 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
1270 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
1271 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
1272 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
1273 cp_hdr_v2_0 = (const struct gfx_firmware_header_v2_0 *)
1274 adev->gfx.mec_fw->data;
1275 ucode_fw = adev->gfx.mec_fw;
1276 fw_size = le32_to_cpu(cp_hdr_v2_0->data_size_bytes);
1279 dev_err(adev->dev, "Invalid ucode id %u\n", ucode_id);
1283 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1284 info = &adev->firmware.ucode[ucode_id];
1285 info->ucode_id = ucode_id;
1286 info->fw = ucode_fw;
1287 adev->firmware.fw_size += ALIGN(fw_size, PAGE_SIZE);
1291 bool amdgpu_gfx_is_master_xcc(struct amdgpu_device *adev, int xcc_id)
1293 return !(xcc_id % (adev->gfx.num_xcc_per_xcp ?
1294 adev->gfx.num_xcc_per_xcp : 1));
1297 static ssize_t amdgpu_gfx_get_current_compute_partition(struct device *dev,
1298 struct device_attribute *addr,
1301 struct drm_device *ddev = dev_get_drvdata(dev);
1302 struct amdgpu_device *adev = drm_to_adev(ddev);
1305 mode = amdgpu_xcp_query_partition_mode(adev->xcp_mgr,
1306 AMDGPU_XCP_FL_NONE);
1308 return sysfs_emit(buf, "%s\n", amdgpu_gfx_compute_mode_desc(mode));
1311 static ssize_t amdgpu_gfx_set_compute_partition(struct device *dev,
1312 struct device_attribute *addr,
1313 const char *buf, size_t count)
1315 struct drm_device *ddev = dev_get_drvdata(dev);
1316 struct amdgpu_device *adev = drm_to_adev(ddev);
1317 enum amdgpu_gfx_partition mode;
1318 int ret = 0, num_xcc;
1320 num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1321 if (num_xcc % 2 != 0)
1324 if (!strncasecmp("SPX", buf, strlen("SPX"))) {
1325 mode = AMDGPU_SPX_PARTITION_MODE;
1326 } else if (!strncasecmp("DPX", buf, strlen("DPX"))) {
1328 * DPX mode needs AIDs to be in multiple of 2.
1329 * Each AID connects 2 XCCs.
1333 mode = AMDGPU_DPX_PARTITION_MODE;
1334 } else if (!strncasecmp("TPX", buf, strlen("TPX"))) {
1337 mode = AMDGPU_TPX_PARTITION_MODE;
1338 } else if (!strncasecmp("QPX", buf, strlen("QPX"))) {
1341 mode = AMDGPU_QPX_PARTITION_MODE;
1342 } else if (!strncasecmp("CPX", buf, strlen("CPX"))) {
1343 mode = AMDGPU_CPX_PARTITION_MODE;
1348 ret = amdgpu_xcp_switch_partition_mode(adev->xcp_mgr, mode);
1356 static const char *xcp_desc[] = {
1357 [AMDGPU_SPX_PARTITION_MODE] = "SPX",
1358 [AMDGPU_DPX_PARTITION_MODE] = "DPX",
1359 [AMDGPU_TPX_PARTITION_MODE] = "TPX",
1360 [AMDGPU_QPX_PARTITION_MODE] = "QPX",
1361 [AMDGPU_CPX_PARTITION_MODE] = "CPX",
1364 static ssize_t amdgpu_gfx_get_available_compute_partition(struct device *dev,
1365 struct device_attribute *addr,
1368 struct drm_device *ddev = dev_get_drvdata(dev);
1369 struct amdgpu_device *adev = drm_to_adev(ddev);
1370 struct amdgpu_xcp_mgr *xcp_mgr = adev->xcp_mgr;
1374 if (!xcp_mgr || !xcp_mgr->avail_xcp_modes)
1375 return sysfs_emit(buf, "Not supported\n");
1377 for_each_inst(mode, xcp_mgr->avail_xcp_modes) {
1378 size += sysfs_emit_at(buf, size, "%s%s", sep, xcp_desc[mode]);
1382 size += sysfs_emit_at(buf, size, "\n");
1387 static int amdgpu_gfx_run_cleaner_shader_job(struct amdgpu_ring *ring)
1389 struct amdgpu_device *adev = ring->adev;
1390 struct drm_gpu_scheduler *sched = &ring->sched;
1391 struct drm_sched_entity entity;
1392 struct dma_fence *f;
1393 struct amdgpu_job *job;
1394 struct amdgpu_ib *ib;
1397 /* Initialize the scheduler entity */
1398 r = drm_sched_entity_init(&entity, DRM_SCHED_PRIORITY_NORMAL,
1401 dev_err(adev->dev, "Failed setting up GFX kernel entity.\n");
1405 r = amdgpu_job_alloc_with_ib(ring->adev, &entity, NULL,
1411 job->enforce_isolation = true;
1414 for (i = 0; i <= ring->funcs->align_mask; ++i)
1415 ib->ptr[i] = ring->funcs->nop;
1416 ib->length_dw = ring->funcs->align_mask + 1;
1418 f = amdgpu_job_submit(job);
1420 r = dma_fence_wait(f, false);
1426 /* Clean up the scheduler entity */
1427 drm_sched_entity_destroy(&entity);
1434 static int amdgpu_gfx_run_cleaner_shader(struct amdgpu_device *adev, int xcp_id)
1436 int num_xcc = NUM_XCC(adev->gfx.xcc_mask);
1437 struct amdgpu_ring *ring;
1438 int num_xcc_to_clear;
1441 if (adev->gfx.num_xcc_per_xcp)
1442 num_xcc_to_clear = adev->gfx.num_xcc_per_xcp;
1444 num_xcc_to_clear = 1;
1446 for (xcc_id = 0; xcc_id < num_xcc; xcc_id++) {
1447 for (i = 0; i < adev->gfx.num_compute_rings; i++) {
1448 ring = &adev->gfx.compute_ring[i + xcc_id * adev->gfx.num_compute_rings];
1449 if ((ring->xcp_id == xcp_id) && ring->sched.ready) {
1450 r = amdgpu_gfx_run_cleaner_shader_job(ring);
1459 if (num_xcc_to_clear)
1465 static ssize_t amdgpu_gfx_set_run_cleaner_shader(struct device *dev,
1466 struct device_attribute *attr,
1470 struct drm_device *ddev = dev_get_drvdata(dev);
1471 struct amdgpu_device *adev = drm_to_adev(ddev);
1475 if (amdgpu_in_reset(adev))
1477 if (adev->in_suspend && !adev->in_runpm)
1480 ret = kstrtol(buf, 0, &value);
1488 if (adev->xcp_mgr) {
1489 if (value >= adev->xcp_mgr->num_xcps)
1496 ret = pm_runtime_get_sync(ddev->dev);
1498 pm_runtime_put_autosuspend(ddev->dev);
1502 ret = amdgpu_gfx_run_cleaner_shader(adev, value);
1504 pm_runtime_mark_last_busy(ddev->dev);
1505 pm_runtime_put_autosuspend(ddev->dev);
1513 static ssize_t amdgpu_gfx_get_enforce_isolation(struct device *dev,
1514 struct device_attribute *attr,
1517 struct drm_device *ddev = dev_get_drvdata(dev);
1518 struct amdgpu_device *adev = drm_to_adev(ddev);
1522 if (adev->xcp_mgr) {
1523 for (i = 0; i < adev->xcp_mgr->num_xcps; i++) {
1524 size += sysfs_emit_at(buf, size, "%u", adev->enforce_isolation[i]);
1525 if (i < (adev->xcp_mgr->num_xcps - 1))
1526 size += sysfs_emit_at(buf, size, " ");
1530 size = sysfs_emit_at(buf, 0, "%u\n", adev->enforce_isolation[0]);
1536 static ssize_t amdgpu_gfx_set_enforce_isolation(struct device *dev,
1537 struct device_attribute *attr,
1538 const char *buf, size_t count)
1540 struct drm_device *ddev = dev_get_drvdata(dev);
1541 struct amdgpu_device *adev = drm_to_adev(ddev);
1542 long partition_values[MAX_XCP] = {0};
1543 int ret, i, num_partitions;
1544 const char *input_buf = buf;
1546 for (i = 0; i < (adev->xcp_mgr ? adev->xcp_mgr->num_xcps : 1); i++) {
1547 ret = sscanf(input_buf, "%ld", &partition_values[i]);
1551 /* Move the pointer to the next value in the string */
1552 input_buf = strchr(input_buf, ' ');
1562 if (adev->xcp_mgr && num_partitions != adev->xcp_mgr->num_xcps)
1565 if (!adev->xcp_mgr && num_partitions != 1)
1568 for (i = 0; i < num_partitions; i++) {
1569 if (partition_values[i] != 0 && partition_values[i] != 1)
1573 mutex_lock(&adev->enforce_isolation_mutex);
1575 for (i = 0; i < num_partitions; i++) {
1576 if (adev->enforce_isolation[i] && !partition_values[i]) {
1577 /* Going from enabled to disabled */
1578 amdgpu_vmid_free_reserved(adev, AMDGPU_GFXHUB(i));
1579 } else if (!adev->enforce_isolation[i] && partition_values[i]) {
1580 /* Going from disabled to enabled */
1581 amdgpu_vmid_alloc_reserved(adev, AMDGPU_GFXHUB(i));
1583 adev->enforce_isolation[i] = partition_values[i];
1586 mutex_unlock(&adev->enforce_isolation_mutex);
1591 static DEVICE_ATTR(run_cleaner_shader, 0200,
1592 NULL, amdgpu_gfx_set_run_cleaner_shader);
1594 static DEVICE_ATTR(enforce_isolation, 0644,
1595 amdgpu_gfx_get_enforce_isolation,
1596 amdgpu_gfx_set_enforce_isolation);
1598 static DEVICE_ATTR(current_compute_partition, 0644,
1599 amdgpu_gfx_get_current_compute_partition,
1600 amdgpu_gfx_set_compute_partition);
1602 static DEVICE_ATTR(available_compute_partition, 0444,
1603 amdgpu_gfx_get_available_compute_partition, NULL);
1605 int amdgpu_gfx_sysfs_init(struct amdgpu_device *adev)
1607 struct amdgpu_xcp_mgr *xcp_mgr = adev->xcp_mgr;
1608 bool xcp_switch_supported;
1614 xcp_switch_supported =
1615 (xcp_mgr->funcs && xcp_mgr->funcs->switch_partition_mode);
1617 if (!xcp_switch_supported)
1618 dev_attr_current_compute_partition.attr.mode &=
1619 ~(S_IWUSR | S_IWGRP | S_IWOTH);
1621 r = device_create_file(adev->dev, &dev_attr_current_compute_partition);
1625 if (xcp_switch_supported)
1626 r = device_create_file(adev->dev,
1627 &dev_attr_available_compute_partition);
1632 void amdgpu_gfx_sysfs_fini(struct amdgpu_device *adev)
1634 struct amdgpu_xcp_mgr *xcp_mgr = adev->xcp_mgr;
1635 bool xcp_switch_supported;
1640 xcp_switch_supported =
1641 (xcp_mgr->funcs && xcp_mgr->funcs->switch_partition_mode);
1642 device_remove_file(adev->dev, &dev_attr_current_compute_partition);
1644 if (xcp_switch_supported)
1645 device_remove_file(adev->dev,
1646 &dev_attr_available_compute_partition);
1649 int amdgpu_gfx_sysfs_isolation_shader_init(struct amdgpu_device *adev)
1653 r = device_create_file(adev->dev, &dev_attr_enforce_isolation);
1657 r = device_create_file(adev->dev, &dev_attr_run_cleaner_shader);
1664 void amdgpu_gfx_sysfs_isolation_shader_fini(struct amdgpu_device *adev)
1666 device_remove_file(adev->dev, &dev_attr_enforce_isolation);
1667 device_remove_file(adev->dev, &dev_attr_run_cleaner_shader);
1670 int amdgpu_gfx_cleaner_shader_sw_init(struct amdgpu_device *adev,
1671 unsigned int cleaner_shader_size)
1673 if (!adev->gfx.enable_cleaner_shader)
1676 return amdgpu_bo_create_kernel(adev, cleaner_shader_size, PAGE_SIZE,
1677 AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT,
1678 &adev->gfx.cleaner_shader_obj,
1679 &adev->gfx.cleaner_shader_gpu_addr,
1680 (void **)&adev->gfx.cleaner_shader_cpu_ptr);
1683 void amdgpu_gfx_cleaner_shader_sw_fini(struct amdgpu_device *adev)
1685 if (!adev->gfx.enable_cleaner_shader)
1688 amdgpu_bo_free_kernel(&adev->gfx.cleaner_shader_obj,
1689 &adev->gfx.cleaner_shader_gpu_addr,
1690 (void **)&adev->gfx.cleaner_shader_cpu_ptr);
1693 void amdgpu_gfx_cleaner_shader_init(struct amdgpu_device *adev,
1694 unsigned int cleaner_shader_size,
1695 const void *cleaner_shader_ptr)
1697 if (!adev->gfx.enable_cleaner_shader)
1700 if (adev->gfx.cleaner_shader_cpu_ptr && cleaner_shader_ptr)
1701 memcpy_toio(adev->gfx.cleaner_shader_cpu_ptr, cleaner_shader_ptr,
1702 cleaner_shader_size);
1706 * amdgpu_gfx_kfd_sch_ctrl - Control the KFD scheduler from the KGD (Graphics Driver)
1707 * @adev: amdgpu_device pointer
1708 * @idx: Index of the scheduler to control
1709 * @enable: Whether to enable or disable the KFD scheduler
1711 * This function is used to control the KFD (Kernel Fusion Driver) scheduler
1712 * from the KGD. It is part of the cleaner shader feature. This function plays
1713 * a key role in enforcing process isolation on the GPU.
1715 * The function uses a reference count mechanism (kfd_sch_req_count) to keep
1716 * track of the number of requests to enable the KFD scheduler. When a request
1717 * to enable the KFD scheduler is made, the reference count is decremented.
1718 * When the reference count reaches zero, a delayed work is scheduled to
1719 * enforce isolation after a delay of GFX_SLICE_PERIOD.
1721 * When a request to disable the KFD scheduler is made, the function first
1722 * checks if the reference count is zero. If it is, it cancels the delayed work
1723 * for enforcing isolation and checks if the KFD scheduler is active. If the
1724 * KFD scheduler is active, it sends a request to stop the KFD scheduler and
1725 * sets the KFD scheduler state to inactive. Then, it increments the reference
1728 * The function is synchronized using the kfd_sch_mutex to ensure that the KFD
1729 * scheduler state and reference count are updated atomically.
1731 * Note: If the reference count is already zero when a request to enable the
1732 * KFD scheduler is made, it means there's an imbalance bug somewhere. The
1733 * function triggers a warning in this case.
1735 static void amdgpu_gfx_kfd_sch_ctrl(struct amdgpu_device *adev, u32 idx,
1738 mutex_lock(&adev->gfx.kfd_sch_mutex);
1741 /* If the count is already 0, it means there's an imbalance bug somewhere.
1742 * Note that the bug may be in a different caller than the one which triggers the
1745 if (WARN_ON_ONCE(adev->gfx.kfd_sch_req_count[idx] == 0)) {
1746 dev_err(adev->dev, "Attempted to enable KFD scheduler when reference count is already zero\n");
1750 adev->gfx.kfd_sch_req_count[idx]--;
1752 if (adev->gfx.kfd_sch_req_count[idx] == 0 &&
1753 adev->gfx.kfd_sch_inactive[idx]) {
1754 schedule_delayed_work(&adev->gfx.enforce_isolation[idx].work,
1755 msecs_to_jiffies(adev->gfx.enforce_isolation_time[idx]));
1758 if (adev->gfx.kfd_sch_req_count[idx] == 0) {
1759 cancel_delayed_work_sync(&adev->gfx.enforce_isolation[idx].work);
1760 if (!adev->gfx.kfd_sch_inactive[idx]) {
1761 amdgpu_amdkfd_stop_sched(adev, idx);
1762 adev->gfx.kfd_sch_inactive[idx] = true;
1766 adev->gfx.kfd_sch_req_count[idx]++;
1770 mutex_unlock(&adev->gfx.kfd_sch_mutex);
1774 * amdgpu_gfx_enforce_isolation_handler - work handler for enforcing shader isolation
1776 * @work: work_struct.
1778 * This function is the work handler for enforcing shader isolation on AMD GPUs.
1779 * It counts the number of emitted fences for each GFX and compute ring. If there
1780 * are any fences, it schedules the `enforce_isolation_work` to be run after a
1781 * delay of `GFX_SLICE_PERIOD`. If there are no fences, it signals the Kernel Fusion
1782 * Driver (KFD) to resume the runqueue. The function is synchronized using the
1783 * `enforce_isolation_mutex`.
1785 void amdgpu_gfx_enforce_isolation_handler(struct work_struct *work)
1787 struct amdgpu_isolation_work *isolation_work =
1788 container_of(work, struct amdgpu_isolation_work, work.work);
1789 struct amdgpu_device *adev = isolation_work->adev;
1790 u32 i, idx, fences = 0;
1792 if (isolation_work->xcp_id == AMDGPU_XCP_NO_PARTITION)
1795 idx = isolation_work->xcp_id;
1800 mutex_lock(&adev->enforce_isolation_mutex);
1801 for (i = 0; i < AMDGPU_MAX_GFX_RINGS; ++i) {
1802 if (isolation_work->xcp_id == adev->gfx.gfx_ring[i].xcp_id)
1803 fences += amdgpu_fence_count_emitted(&adev->gfx.gfx_ring[i]);
1805 for (i = 0; i < (AMDGPU_MAX_COMPUTE_RINGS * AMDGPU_MAX_GC_INSTANCES); ++i) {
1806 if (isolation_work->xcp_id == adev->gfx.compute_ring[i].xcp_id)
1807 fences += amdgpu_fence_count_emitted(&adev->gfx.compute_ring[i]);
1810 /* we've already had our timeslice, so let's wrap this up */
1811 schedule_delayed_work(&adev->gfx.enforce_isolation[idx].work,
1812 msecs_to_jiffies(1));
1814 /* Tell KFD to resume the runqueue */
1815 if (adev->kfd.init_complete) {
1816 WARN_ON_ONCE(!adev->gfx.kfd_sch_inactive[idx]);
1817 WARN_ON_ONCE(adev->gfx.kfd_sch_req_count[idx]);
1818 amdgpu_amdkfd_start_sched(adev, idx);
1819 adev->gfx.kfd_sch_inactive[idx] = false;
1822 mutex_unlock(&adev->enforce_isolation_mutex);
1826 amdgpu_gfx_enforce_isolation_wait_for_kfd(struct amdgpu_device *adev,
1829 unsigned long cjiffies;
1832 mutex_lock(&adev->enforce_isolation_mutex);
1833 if (adev->enforce_isolation[idx]) {
1834 /* set the initial values if nothing is set */
1835 if (!adev->gfx.enforce_isolation_jiffies[idx]) {
1836 adev->gfx.enforce_isolation_jiffies[idx] = jiffies;
1837 adev->gfx.enforce_isolation_time[idx] = GFX_SLICE_PERIOD_MS;
1839 /* Make sure KFD gets a chance to run */
1840 if (amdgpu_amdkfd_compute_active(adev, idx)) {
1842 if (time_after(cjiffies, adev->gfx.enforce_isolation_jiffies[idx])) {
1843 cjiffies -= adev->gfx.enforce_isolation_jiffies[idx];
1844 if ((jiffies_to_msecs(cjiffies) >= GFX_SLICE_PERIOD_MS)) {
1845 /* if our time is up, let KGD work drain before scheduling more */
1847 /* reset the timer period */
1848 adev->gfx.enforce_isolation_time[idx] = GFX_SLICE_PERIOD_MS;
1850 /* set the timer period to what's left in our time slice */
1851 adev->gfx.enforce_isolation_time[idx] =
1852 GFX_SLICE_PERIOD_MS - jiffies_to_msecs(cjiffies);
1855 /* if jiffies wrap around we will just wait a little longer */
1856 adev->gfx.enforce_isolation_jiffies[idx] = jiffies;
1859 /* if there is no KFD work, then set the full slice period */
1860 adev->gfx.enforce_isolation_jiffies[idx] = jiffies;
1861 adev->gfx.enforce_isolation_time[idx] = GFX_SLICE_PERIOD_MS;
1864 mutex_unlock(&adev->enforce_isolation_mutex);
1867 msleep(GFX_SLICE_PERIOD_MS);
1870 void amdgpu_gfx_enforce_isolation_ring_begin_use(struct amdgpu_ring *ring)
1872 struct amdgpu_device *adev = ring->adev;
1875 if (!adev->gfx.enable_cleaner_shader)
1878 if (ring->xcp_id == AMDGPU_XCP_NO_PARTITION)
1886 /* Don't submit more work until KFD has had some time */
1887 amdgpu_gfx_enforce_isolation_wait_for_kfd(adev, idx);
1889 mutex_lock(&adev->enforce_isolation_mutex);
1890 if (adev->enforce_isolation[idx]) {
1891 if (adev->kfd.init_complete)
1892 amdgpu_gfx_kfd_sch_ctrl(adev, idx, false);
1894 mutex_unlock(&adev->enforce_isolation_mutex);
1897 void amdgpu_gfx_enforce_isolation_ring_end_use(struct amdgpu_ring *ring)
1899 struct amdgpu_device *adev = ring->adev;
1902 if (!adev->gfx.enable_cleaner_shader)
1905 if (ring->xcp_id == AMDGPU_XCP_NO_PARTITION)
1913 mutex_lock(&adev->enforce_isolation_mutex);
1914 if (adev->enforce_isolation[idx]) {
1915 if (adev->kfd.init_complete)
1916 amdgpu_gfx_kfd_sch_ctrl(adev, idx, true);
1918 mutex_unlock(&adev->enforce_isolation_mutex);