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