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