1 /* 2 * Copyright 2008 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 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 * Christian König 28 */ 29 #include <linux/seq_file.h> 30 #include <linux/slab.h> 31 #include <linux/uaccess.h> 32 #include <linux/debugfs.h> 33 34 #include <drm/amdgpu_drm.h> 35 #include "amdgpu.h" 36 #include "atom.h" 37 38 /* 39 * Rings 40 * Most engines on the GPU are fed via ring buffers. Ring 41 * buffers are areas of GPU accessible memory that the host 42 * writes commands into and the GPU reads commands out of. 43 * There is a rptr (read pointer) that determines where the 44 * GPU is currently reading, and a wptr (write pointer) 45 * which determines where the host has written. When the 46 * pointers are equal, the ring is idle. When the host 47 * writes commands to the ring buffer, it increments the 48 * wptr. The GPU then starts fetching commands and executes 49 * them until the pointers are equal again. 50 */ 51 52 /** 53 * amdgpu_ring_max_ibs - Return max IBs that fit in a single submission. 54 * 55 * @type: ring type for which to return the limit. 56 */ 57 unsigned int amdgpu_ring_max_ibs(enum amdgpu_ring_type type) 58 { 59 switch (type) { 60 case AMDGPU_RING_TYPE_GFX: 61 /* Need to keep at least 192 on GFX7+ for old radv. */ 62 return 192; 63 case AMDGPU_RING_TYPE_COMPUTE: 64 return 125; 65 case AMDGPU_RING_TYPE_VCN_JPEG: 66 return 16; 67 default: 68 return 49; 69 } 70 } 71 72 /** 73 * amdgpu_ring_alloc - allocate space on the ring buffer 74 * 75 * @ring: amdgpu_ring structure holding ring information 76 * @ndw: number of dwords to allocate in the ring buffer 77 * 78 * Allocate @ndw dwords in the ring buffer (all asics). 79 * Returns 0 on success, error on failure. 80 */ 81 int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned int ndw) 82 { 83 /* Align requested size with padding so unlock_commit can 84 * pad safely */ 85 ndw = (ndw + ring->funcs->align_mask) & ~ring->funcs->align_mask; 86 87 /* Make sure we aren't trying to allocate more space 88 * than the maximum for one submission 89 */ 90 if (WARN_ON_ONCE(ndw > ring->max_dw)) 91 return -ENOMEM; 92 93 ring->count_dw = ndw; 94 ring->wptr_old = ring->wptr; 95 96 if (ring->funcs->begin_use) 97 ring->funcs->begin_use(ring); 98 99 return 0; 100 } 101 102 /** 103 * amdgpu_ring_alloc_reemit - allocate space on the ring buffer for reemit 104 * 105 * @ring: amdgpu_ring structure holding ring information 106 * @ndw: number of dwords to allocate in the ring buffer 107 * 108 * Allocate @ndw dwords in the ring buffer (all asics). 109 * doesn't check the max_dw limit as we may be reemitting 110 * several submissions. 111 */ 112 static void amdgpu_ring_alloc_reemit(struct amdgpu_ring *ring, unsigned int ndw) 113 { 114 /* Align requested size with padding so unlock_commit can 115 * pad safely */ 116 ndw = (ndw + ring->funcs->align_mask) & ~ring->funcs->align_mask; 117 118 ring->count_dw = ndw; 119 ring->wptr_old = ring->wptr; 120 121 if (ring->funcs->begin_use) 122 ring->funcs->begin_use(ring); 123 } 124 125 /** amdgpu_ring_insert_nop - insert NOP packets 126 * 127 * @ring: amdgpu_ring structure holding ring information 128 * @count: the number of NOP packets to insert 129 * 130 * This is the generic insert_nop function for rings except SDMA 131 */ 132 void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count) 133 { 134 uint32_t occupied, chunk1, chunk2; 135 136 occupied = ring->wptr & ring->buf_mask; 137 chunk1 = ring->buf_mask + 1 - occupied; 138 chunk1 = (chunk1 >= count) ? count : chunk1; 139 chunk2 = count - chunk1; 140 141 if (chunk1) 142 memset32(&ring->ring[occupied], ring->funcs->nop, chunk1); 143 144 if (chunk2) 145 memset32(ring->ring, ring->funcs->nop, chunk2); 146 147 ring->wptr += count; 148 ring->wptr &= ring->ptr_mask; 149 ring->count_dw -= count; 150 } 151 152 /** 153 * amdgpu_ring_generic_pad_ib - pad IB with NOP packets 154 * 155 * @ring: amdgpu_ring structure holding ring information 156 * @ib: IB to add NOP packets to 157 * 158 * This is the generic pad_ib function for rings except SDMA 159 */ 160 void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib) 161 { 162 u32 align_mask = ring->funcs->align_mask; 163 u32 count = ib->length_dw & align_mask; 164 165 if (count) { 166 count = align_mask + 1 - count; 167 168 memset32(&ib->ptr[ib->length_dw], ring->funcs->nop, count); 169 170 ib->length_dw += count; 171 } 172 } 173 174 /** 175 * amdgpu_ring_commit - tell the GPU to execute the new 176 * commands on the ring buffer 177 * 178 * @ring: amdgpu_ring structure holding ring information 179 * 180 * Update the wptr (write pointer) to tell the GPU to 181 * execute new commands on the ring buffer (all asics). 182 */ 183 void amdgpu_ring_commit(struct amdgpu_ring *ring) 184 { 185 uint32_t count; 186 187 if (ring->count_dw < 0) 188 DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n"); 189 190 /* We pad to match fetch size */ 191 count = ring->funcs->align_mask + 1 - 192 (ring->wptr & ring->funcs->align_mask); 193 count &= ring->funcs->align_mask; 194 195 if (count != 0) 196 ring->funcs->insert_nop(ring, count); 197 198 mb(); 199 amdgpu_ring_set_wptr(ring); 200 201 if (ring->funcs->end_use) 202 ring->funcs->end_use(ring); 203 } 204 205 /** 206 * amdgpu_ring_undo - reset the wptr 207 * 208 * @ring: amdgpu_ring structure holding ring information 209 * 210 * Reset the driver's copy of the wptr (all asics). 211 */ 212 void amdgpu_ring_undo(struct amdgpu_ring *ring) 213 { 214 ring->wptr = ring->wptr_old; 215 216 if (ring->funcs->end_use) 217 ring->funcs->end_use(ring); 218 } 219 220 #define amdgpu_ring_get_gpu_addr(ring, offset) \ 221 (ring->adev->wb.gpu_addr + offset * 4) 222 223 #define amdgpu_ring_get_cpu_addr(ring, offset) \ 224 (&ring->adev->wb.wb[offset]) 225 226 /** 227 * amdgpu_ring_init - init driver ring struct. 228 * 229 * @adev: amdgpu_device pointer 230 * @ring: amdgpu_ring structure holding ring information 231 * @max_dw: maximum number of dw for ring alloc 232 * @irq_src: interrupt source to use for this ring 233 * @irq_type: interrupt type to use for this ring 234 * @hw_prio: ring priority (NORMAL/HIGH) 235 * @sched_score: optional score atomic shared with other schedulers 236 * 237 * Initialize the driver information for the selected ring (all asics). 238 * Returns 0 on success, error on failure. 239 */ 240 int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, 241 unsigned int max_dw, struct amdgpu_irq_src *irq_src, 242 unsigned int irq_type, unsigned int hw_prio, 243 atomic_t *sched_score) 244 { 245 int r; 246 int sched_hw_submission = amdgpu_sched_hw_submission; 247 u32 *num_sched; 248 u32 hw_ip; 249 unsigned int max_ibs_dw; 250 251 /* Set the hw submission limit higher for KIQ because 252 * it's used for a number of gfx/compute tasks by both 253 * KFD and KGD which may have outstanding fences and 254 * it doesn't really use the gpu scheduler anyway; 255 * KIQ tasks get submitted directly to the ring. 256 */ 257 if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ) 258 sched_hw_submission = max(sched_hw_submission, 256); 259 if (ring->funcs->type == AMDGPU_RING_TYPE_MES) 260 sched_hw_submission = 8; 261 else if (ring == &adev->sdma.instance[0].page) 262 sched_hw_submission = 256; 263 264 if (ring->adev == NULL) { 265 if (adev->num_rings >= AMDGPU_MAX_RINGS) 266 return -EINVAL; 267 268 ring->adev = adev; 269 ring->num_hw_submission = sched_hw_submission; 270 ring->sched_score = sched_score; 271 ring->vmid_wait = dma_fence_get_stub(); 272 273 ring->idx = adev->num_rings++; 274 adev->rings[ring->idx] = ring; 275 276 r = amdgpu_fence_driver_init_ring(ring); 277 if (r) 278 return r; 279 } 280 281 r = amdgpu_device_wb_get(adev, &ring->rptr_offs); 282 if (r) { 283 dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r); 284 return r; 285 } 286 287 r = amdgpu_device_wb_get(adev, &ring->wptr_offs); 288 if (r) { 289 dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r); 290 return r; 291 } 292 293 r = amdgpu_device_wb_get(adev, &ring->fence_offs); 294 if (r) { 295 dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r); 296 return r; 297 } 298 299 r = amdgpu_device_wb_get(adev, &ring->trail_fence_offs); 300 if (r) { 301 dev_err(adev->dev, "(%d) ring trail_fence_offs wb alloc failed\n", r); 302 return r; 303 } 304 305 r = amdgpu_device_wb_get(adev, &ring->cond_exe_offs); 306 if (r) { 307 dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r); 308 return r; 309 } 310 311 ring->fence_gpu_addr = 312 amdgpu_ring_get_gpu_addr(ring, ring->fence_offs); 313 ring->fence_cpu_addr = 314 amdgpu_ring_get_cpu_addr(ring, ring->fence_offs); 315 316 ring->rptr_gpu_addr = 317 amdgpu_ring_get_gpu_addr(ring, ring->rptr_offs); 318 ring->rptr_cpu_addr = 319 amdgpu_ring_get_cpu_addr(ring, ring->rptr_offs); 320 321 ring->wptr_gpu_addr = 322 amdgpu_ring_get_gpu_addr(ring, ring->wptr_offs); 323 ring->wptr_cpu_addr = 324 amdgpu_ring_get_cpu_addr(ring, ring->wptr_offs); 325 326 ring->trail_fence_gpu_addr = 327 amdgpu_ring_get_gpu_addr(ring, ring->trail_fence_offs); 328 ring->trail_fence_cpu_addr = 329 amdgpu_ring_get_cpu_addr(ring, ring->trail_fence_offs); 330 331 ring->cond_exe_gpu_addr = 332 amdgpu_ring_get_gpu_addr(ring, ring->cond_exe_offs); 333 ring->cond_exe_cpu_addr = 334 amdgpu_ring_get_cpu_addr(ring, ring->cond_exe_offs); 335 336 /* always set cond_exec_polling to CONTINUE */ 337 *ring->cond_exe_cpu_addr = 1; 338 339 if (ring->funcs->type != AMDGPU_RING_TYPE_CPER) { 340 r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type); 341 if (r) { 342 dev_err(adev->dev, "failed initializing fences (%d).\n", r); 343 return r; 344 } 345 346 max_ibs_dw = ring->funcs->emit_frame_size + 347 amdgpu_ring_max_ibs(ring->funcs->type) * ring->funcs->emit_ib_size; 348 max_ibs_dw = (max_ibs_dw + ring->funcs->align_mask) & ~ring->funcs->align_mask; 349 350 if (WARN_ON(max_ibs_dw > max_dw)) 351 max_dw = max_ibs_dw; 352 353 ring->ring_size = roundup_pow_of_two(max_dw * 4 * sched_hw_submission); 354 } else { 355 ring->ring_size = roundup_pow_of_two(max_dw * 4); 356 ring->count_dw = (ring->ring_size - 4) >> 2; 357 /* ring buffer is empty now */ 358 ring->wptr = *ring->rptr_cpu_addr = 0; 359 } 360 361 ring->buf_mask = (ring->ring_size / 4) - 1; 362 ring->ptr_mask = ring->funcs->support_64bit_ptrs ? 363 0xffffffffffffffff : ring->buf_mask; 364 /* Initialize cached_rptr to 0 */ 365 ring->cached_rptr = 0; 366 367 if (!ring->ring_backup) { 368 ring->ring_backup = kvzalloc(ring->ring_size, GFP_KERNEL); 369 if (!ring->ring_backup) 370 return -ENOMEM; 371 } 372 373 /* Allocate ring buffer */ 374 if (ring->ring_obj == NULL) { 375 r = amdgpu_bo_create_kernel(adev, ring->ring_size + ring->funcs->extra_bytes, 376 PAGE_SIZE, 377 AMDGPU_GEM_DOMAIN_GTT, 378 &ring->ring_obj, 379 &ring->gpu_addr, 380 (void **)&ring->ring); 381 if (r) { 382 dev_err(adev->dev, "(%d) ring create failed\n", r); 383 kvfree(ring->ring_backup); 384 return r; 385 } 386 amdgpu_ring_clear_ring(ring); 387 } 388 389 ring->max_dw = max_dw; 390 ring->hw_prio = hw_prio; 391 392 if (!ring->no_scheduler && ring->funcs->type < AMDGPU_HW_IP_NUM) { 393 hw_ip = ring->funcs->type; 394 num_sched = &adev->gpu_sched[hw_ip][hw_prio].num_scheds; 395 adev->gpu_sched[hw_ip][hw_prio].sched[(*num_sched)++] = 396 &ring->sched; 397 } 398 399 return 0; 400 } 401 402 /** 403 * amdgpu_ring_fini - tear down the driver ring struct. 404 * 405 * @ring: amdgpu_ring structure holding ring information 406 * 407 * Tear down the driver information for the selected ring (all asics). 408 */ 409 void amdgpu_ring_fini(struct amdgpu_ring *ring) 410 { 411 412 /* Not to finish a ring which is not initialized */ 413 if (!(ring->adev) || !(ring->adev->rings[ring->idx])) 414 return; 415 416 ring->sched.ready = false; 417 418 amdgpu_device_wb_free(ring->adev, ring->rptr_offs); 419 amdgpu_device_wb_free(ring->adev, ring->wptr_offs); 420 421 amdgpu_device_wb_free(ring->adev, ring->cond_exe_offs); 422 amdgpu_device_wb_free(ring->adev, ring->fence_offs); 423 424 amdgpu_bo_free_kernel(&ring->ring_obj, 425 &ring->gpu_addr, 426 (void **)&ring->ring); 427 kvfree(ring->ring_backup); 428 ring->ring_backup = NULL; 429 430 dma_fence_put(ring->vmid_wait); 431 ring->vmid_wait = NULL; 432 ring->me = 0; 433 } 434 435 /** 436 * amdgpu_ring_emit_reg_write_reg_wait_helper - ring helper 437 * 438 * @ring: ring to write to 439 * @reg0: register to write 440 * @reg1: register to wait on 441 * @ref: reference value to write/wait on 442 * @mask: mask to wait on 443 * 444 * Helper for rings that don't support write and wait in a 445 * single oneshot packet. 446 */ 447 void amdgpu_ring_emit_reg_write_reg_wait_helper(struct amdgpu_ring *ring, 448 uint32_t reg0, uint32_t reg1, 449 uint32_t ref, uint32_t mask) 450 { 451 amdgpu_ring_emit_wreg(ring, reg0, ref); 452 amdgpu_ring_emit_reg_wait(ring, reg1, mask, mask); 453 } 454 455 /** 456 * amdgpu_ring_soft_recovery - try to soft recover a ring lockup 457 * 458 * @ring: ring to try the recovery on 459 * @vmid: VMID we try to get going again 460 * @fence: timedout fence 461 * 462 * Tries to get a ring proceeding again when it is stuck. 463 */ 464 bool amdgpu_ring_soft_recovery(struct amdgpu_ring *ring, unsigned int vmid, 465 struct dma_fence *fence) 466 { 467 unsigned long flags; 468 ktime_t deadline; 469 bool ret; 470 471 if (unlikely(ring->adev->debug_disable_soft_recovery)) 472 return false; 473 474 deadline = ktime_add_us(ktime_get(), 10000); 475 476 if (amdgpu_sriov_vf(ring->adev) || !ring->funcs->soft_recovery || !fence) 477 return false; 478 479 spin_lock_irqsave(fence->lock, flags); 480 if (!dma_fence_is_signaled_locked(fence)) 481 dma_fence_set_error(fence, -ENODATA); 482 spin_unlock_irqrestore(fence->lock, flags); 483 484 while (!dma_fence_is_signaled(fence) && 485 ktime_to_ns(ktime_sub(deadline, ktime_get())) > 0) 486 ring->funcs->soft_recovery(ring, vmid); 487 488 ret = dma_fence_is_signaled(fence); 489 /* increment the counter only if soft reset worked */ 490 if (ret) 491 atomic_inc(&ring->adev->gpu_reset_counter); 492 493 return ret; 494 } 495 496 /* 497 * Debugfs info 498 */ 499 #if defined(CONFIG_DEBUG_FS) 500 501 /* Layout of file is 12 bytes consisting of 502 * - rptr 503 * - wptr 504 * - driver's copy of wptr 505 * 506 * followed by n-words of ring data 507 */ 508 static ssize_t amdgpu_debugfs_ring_read(struct file *f, char __user *buf, 509 size_t size, loff_t *pos) 510 { 511 struct amdgpu_ring *ring = file_inode(f)->i_private; 512 uint32_t value, result, early[3]; 513 uint64_t p; 514 loff_t i; 515 int r; 516 517 if (*pos & 3 || size & 3) 518 return -EINVAL; 519 520 result = 0; 521 522 if (*pos < 12) { 523 if (ring->funcs->type == AMDGPU_RING_TYPE_CPER) 524 mutex_lock(&ring->adev->cper.ring_lock); 525 526 early[0] = amdgpu_ring_get_rptr(ring) & ring->buf_mask; 527 early[1] = amdgpu_ring_get_wptr(ring) & ring->buf_mask; 528 early[2] = ring->wptr & ring->buf_mask; 529 for (i = *pos / 4; i < 3 && size; i++) { 530 r = put_user(early[i], (uint32_t *)buf); 531 if (r) { 532 result = r; 533 goto out; 534 } 535 buf += 4; 536 result += 4; 537 size -= 4; 538 *pos += 4; 539 } 540 } 541 542 if (ring->funcs->type != AMDGPU_RING_TYPE_CPER) { 543 while (size) { 544 if (*pos >= (ring->ring_size + 12)) 545 return result; 546 547 value = ring->ring[(*pos - 12)/4]; 548 r = put_user(value, (uint32_t *)buf); 549 if (r) 550 return r; 551 buf += 4; 552 result += 4; 553 size -= 4; 554 *pos += 4; 555 } 556 } else { 557 p = early[0]; 558 if (early[0] <= early[1]) 559 size = (early[1] - early[0]); 560 else 561 size = ring->ring_size - (early[0] - early[1]); 562 563 while (size) { 564 if (p == early[1]) 565 goto out; 566 567 value = ring->ring[p]; 568 r = put_user(value, (uint32_t *)buf); 569 if (r) { 570 result = r; 571 goto out; 572 } 573 574 buf += 4; 575 result += 4; 576 size--; 577 p++; 578 p &= ring->ptr_mask; 579 } 580 } 581 582 out: 583 if (ring->funcs->type == AMDGPU_RING_TYPE_CPER) 584 mutex_unlock(&ring->adev->cper.ring_lock); 585 586 return result; 587 } 588 589 static ssize_t amdgpu_debugfs_virt_ring_read(struct file *f, char __user *buf, 590 size_t size, loff_t *pos) 591 { 592 struct amdgpu_ring *ring = file_inode(f)->i_private; 593 594 if (*pos & 3 || size & 3) 595 return -EINVAL; 596 597 if (ring->funcs->type == AMDGPU_RING_TYPE_CPER) 598 amdgpu_virt_req_ras_cper_dump(ring->adev, false); 599 600 return amdgpu_debugfs_ring_read(f, buf, size, pos); 601 } 602 603 static const struct file_operations amdgpu_debugfs_ring_fops = { 604 .owner = THIS_MODULE, 605 .read = amdgpu_debugfs_ring_read, 606 .llseek = default_llseek 607 }; 608 609 static const struct file_operations amdgpu_debugfs_virt_ring_fops = { 610 .owner = THIS_MODULE, 611 .read = amdgpu_debugfs_virt_ring_read, 612 .llseek = default_llseek 613 }; 614 615 static ssize_t amdgpu_debugfs_mqd_read(struct file *f, char __user *buf, 616 size_t size, loff_t *pos) 617 { 618 struct amdgpu_ring *ring = file_inode(f)->i_private; 619 ssize_t bytes = min_t(ssize_t, ring->mqd_size - *pos, size); 620 void *from = ((u8 *)ring->mqd_ptr) + *pos; 621 622 if (*pos > ring->mqd_size) 623 return 0; 624 625 if (copy_to_user(buf, from, bytes)) 626 return -EFAULT; 627 628 *pos += bytes; 629 return bytes; 630 } 631 632 static const struct file_operations amdgpu_debugfs_mqd_fops = { 633 .owner = THIS_MODULE, 634 .read = amdgpu_debugfs_mqd_read, 635 .llseek = default_llseek 636 }; 637 638 static int amdgpu_debugfs_ring_error(void *data, u64 val) 639 { 640 struct amdgpu_ring *ring = data; 641 642 amdgpu_fence_driver_set_error(ring, val); 643 return 0; 644 } 645 646 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(amdgpu_debugfs_error_fops, NULL, 647 amdgpu_debugfs_ring_error, "%lld\n"); 648 649 #endif 650 651 void amdgpu_debugfs_ring_init(struct amdgpu_device *adev, 652 struct amdgpu_ring *ring) 653 { 654 #if defined(CONFIG_DEBUG_FS) 655 struct drm_minor *minor = adev_to_drm(adev)->primary; 656 struct dentry *root = minor->debugfs_root; 657 char name[32]; 658 659 sprintf(name, "amdgpu_ring_%s", ring->name); 660 if (amdgpu_sriov_vf(adev)) 661 debugfs_create_file_size(name, S_IFREG | 0444, root, ring, 662 &amdgpu_debugfs_virt_ring_fops, 663 ring->ring_size + 12); 664 else 665 debugfs_create_file_size(name, S_IFREG | 0444, root, ring, 666 &amdgpu_debugfs_ring_fops, 667 ring->ring_size + 12); 668 669 if (ring->mqd_obj) { 670 sprintf(name, "amdgpu_mqd_%s", ring->name); 671 debugfs_create_file_size(name, S_IFREG | 0444, root, ring, 672 &amdgpu_debugfs_mqd_fops, 673 ring->mqd_size); 674 } 675 676 sprintf(name, "amdgpu_error_%s", ring->name); 677 debugfs_create_file(name, 0200, root, ring, 678 &amdgpu_debugfs_error_fops); 679 680 #endif 681 } 682 683 /** 684 * amdgpu_ring_test_helper - tests ring and set sched readiness status 685 * 686 * @ring: ring to try the recovery on 687 * 688 * Tests ring and set sched readiness status 689 * 690 * Returns 0 on success, error on failure. 691 */ 692 int amdgpu_ring_test_helper(struct amdgpu_ring *ring) 693 { 694 struct amdgpu_device *adev = ring->adev; 695 int r; 696 697 r = amdgpu_ring_test_ring(ring); 698 if (r) 699 DRM_DEV_ERROR(adev->dev, "ring %s test failed (%d)\n", 700 ring->name, r); 701 else 702 DRM_DEV_DEBUG(adev->dev, "ring test on %s succeeded\n", 703 ring->name); 704 705 ring->sched.ready = !r; 706 707 return r; 708 } 709 710 static void amdgpu_ring_to_mqd_prop(struct amdgpu_ring *ring, 711 struct amdgpu_mqd_prop *prop) 712 { 713 struct amdgpu_device *adev = ring->adev; 714 bool is_high_prio_compute = ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE && 715 amdgpu_gfx_is_high_priority_compute_queue(adev, ring); 716 bool is_high_prio_gfx = ring->funcs->type == AMDGPU_RING_TYPE_GFX && 717 amdgpu_gfx_is_high_priority_graphics_queue(adev, ring); 718 719 memset(prop, 0, sizeof(*prop)); 720 721 prop->mqd_gpu_addr = ring->mqd_gpu_addr; 722 prop->hqd_base_gpu_addr = ring->gpu_addr; 723 prop->rptr_gpu_addr = ring->rptr_gpu_addr; 724 prop->wptr_gpu_addr = ring->wptr_gpu_addr; 725 prop->queue_size = ring->ring_size; 726 prop->eop_gpu_addr = ring->eop_gpu_addr; 727 prop->use_doorbell = ring->use_doorbell; 728 prop->doorbell_index = ring->doorbell_index; 729 prop->kernel_queue = true; 730 731 /* map_queues packet doesn't need activate the queue, 732 * so only kiq need set this field. 733 */ 734 prop->hqd_active = ring->funcs->type == AMDGPU_RING_TYPE_KIQ; 735 736 prop->allow_tunneling = is_high_prio_compute; 737 if (is_high_prio_compute || is_high_prio_gfx) { 738 prop->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_HIGH; 739 prop->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MAXIMUM; 740 } 741 } 742 743 int amdgpu_ring_init_mqd(struct amdgpu_ring *ring) 744 { 745 struct amdgpu_device *adev = ring->adev; 746 struct amdgpu_mqd *mqd_mgr; 747 struct amdgpu_mqd_prop prop; 748 749 amdgpu_ring_to_mqd_prop(ring, &prop); 750 751 ring->wptr = 0; 752 753 if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ) 754 mqd_mgr = &adev->mqds[AMDGPU_HW_IP_COMPUTE]; 755 else 756 mqd_mgr = &adev->mqds[ring->funcs->type]; 757 758 return mqd_mgr->init_mqd(adev, ring->mqd_ptr, &prop); 759 } 760 761 void amdgpu_ring_ib_begin(struct amdgpu_ring *ring) 762 { 763 if (ring->is_sw_ring) 764 amdgpu_sw_ring_ib_begin(ring); 765 } 766 767 void amdgpu_ring_ib_end(struct amdgpu_ring *ring) 768 { 769 if (ring->is_sw_ring) 770 amdgpu_sw_ring_ib_end(ring); 771 } 772 773 void amdgpu_ring_ib_on_emit_cntl(struct amdgpu_ring *ring) 774 { 775 if (ring->is_sw_ring) 776 amdgpu_sw_ring_ib_mark_offset(ring, AMDGPU_MUX_OFFSET_TYPE_CONTROL); 777 } 778 779 void amdgpu_ring_ib_on_emit_ce(struct amdgpu_ring *ring) 780 { 781 if (ring->is_sw_ring) 782 amdgpu_sw_ring_ib_mark_offset(ring, AMDGPU_MUX_OFFSET_TYPE_CE); 783 } 784 785 void amdgpu_ring_ib_on_emit_de(struct amdgpu_ring *ring) 786 { 787 if (ring->is_sw_ring) 788 amdgpu_sw_ring_ib_mark_offset(ring, AMDGPU_MUX_OFFSET_TYPE_DE); 789 } 790 791 bool amdgpu_ring_sched_ready(struct amdgpu_ring *ring) 792 { 793 if (!ring) 794 return false; 795 796 if (ring->no_scheduler || !drm_sched_wqueue_ready(&ring->sched)) 797 return false; 798 799 return true; 800 } 801 802 void amdgpu_ring_reset_helper_begin(struct amdgpu_ring *ring, 803 struct amdgpu_fence *guilty_fence) 804 { 805 /* Stop the scheduler to prevent anybody else from touching the ring buffer. */ 806 drm_sched_wqueue_stop(&ring->sched); 807 /* back up the non-guilty commands */ 808 amdgpu_ring_backup_unprocessed_commands(ring, guilty_fence); 809 } 810 811 int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring, 812 struct amdgpu_fence *guilty_fence) 813 { 814 unsigned int i; 815 int r; 816 817 /* verify that the ring is functional */ 818 r = amdgpu_ring_test_ring(ring); 819 if (r) 820 return r; 821 822 /* signal the guilty fence and set an error on all fences from the context */ 823 if (guilty_fence) 824 amdgpu_fence_driver_guilty_force_completion(guilty_fence); 825 /* Re-emit the non-guilty commands */ 826 if (ring->ring_backup_entries_to_copy) { 827 amdgpu_ring_alloc_reemit(ring, ring->ring_backup_entries_to_copy); 828 for (i = 0; i < ring->ring_backup_entries_to_copy; i++) 829 amdgpu_ring_write(ring, ring->ring_backup[i]); 830 amdgpu_ring_commit(ring); 831 } 832 /* Start the scheduler again */ 833 drm_sched_wqueue_start(&ring->sched); 834 return 0; 835 } 836 837 bool amdgpu_ring_is_reset_type_supported(struct amdgpu_ring *ring, 838 u32 reset_type) 839 { 840 switch (ring->funcs->type) { 841 case AMDGPU_RING_TYPE_GFX: 842 if (ring->adev->gfx.gfx_supported_reset & reset_type) 843 return true; 844 break; 845 case AMDGPU_RING_TYPE_COMPUTE: 846 if (ring->adev->gfx.compute_supported_reset & reset_type) 847 return true; 848 break; 849 case AMDGPU_RING_TYPE_SDMA: 850 if (ring->adev->sdma.supported_reset & reset_type) 851 return true; 852 break; 853 case AMDGPU_RING_TYPE_VCN_DEC: 854 case AMDGPU_RING_TYPE_VCN_ENC: 855 if (ring->adev->vcn.supported_reset & reset_type) 856 return true; 857 break; 858 case AMDGPU_RING_TYPE_VCN_JPEG: 859 if (ring->adev->jpeg.supported_reset & reset_type) 860 return true; 861 break; 862 default: 863 break; 864 } 865 return false; 866 } 867