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