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 amdgpu_ring_fill(ring, ring->funcs->nop, count); 121 } 122 123 /** 124 * amdgpu_ring_generic_pad_ib - pad IB with NOP packets 125 * 126 * @ring: amdgpu_ring structure holding ring information 127 * @ib: IB to add NOP packets to 128 * 129 * This is the generic pad_ib function for rings except SDMA 130 */ 131 void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib) 132 { 133 u32 align_mask = ring->funcs->align_mask; 134 u32 count = ib->length_dw & align_mask; 135 136 if (count) { 137 count = align_mask + 1 - count; 138 139 memset32(&ib->ptr[ib->length_dw], ring->funcs->nop, count); 140 141 ib->length_dw += count; 142 } 143 } 144 145 /** 146 * amdgpu_ring_pad_to_align - pad the ring to the fetch alignment 147 * @ring: amdgpu_ring structure holding ring information 148 */ 149 void amdgpu_ring_pad_to_align(struct amdgpu_ring *ring) 150 { 151 u32 count; 152 153 count = ring->funcs->align_mask + 1 - 154 (ring->wptr & ring->funcs->align_mask); 155 count &= ring->funcs->align_mask; 156 157 if (count != 0) 158 ring->funcs->insert_nop(ring, count); 159 } 160 161 /** 162 * amdgpu_ring_commit - tell the GPU to execute the new 163 * commands on the ring buffer 164 * 165 * @ring: amdgpu_ring structure holding ring information 166 * 167 * Update the wptr (write pointer) to tell the GPU to 168 * execute new commands on the ring buffer (all asics). 169 */ 170 void amdgpu_ring_commit(struct amdgpu_ring *ring) 171 { 172 if (ring->count_dw < 0) 173 drm_err(adev_to_drm(ring->adev), "writing more dwords to the ring than expected!\n"); 174 175 /* We pad to match fetch size */ 176 amdgpu_ring_pad_to_align(ring); 177 178 mb(); 179 amdgpu_ring_set_wptr(ring); 180 181 if (ring->funcs->end_use) 182 ring->funcs->end_use(ring); 183 } 184 185 /** 186 * amdgpu_ring_undo - reset the wptr 187 * 188 * @ring: amdgpu_ring structure holding ring information 189 * 190 * Reset the driver's copy of the wptr (all asics). 191 */ 192 void amdgpu_ring_undo(struct amdgpu_ring *ring) 193 { 194 ring->wptr = ring->wptr_old; 195 196 if (ring->funcs->end_use) 197 ring->funcs->end_use(ring); 198 } 199 200 #define amdgpu_ring_get_gpu_addr(ring, offset) \ 201 (ring->adev->wb.gpu_addr + offset * 4) 202 203 #define amdgpu_ring_get_cpu_addr(ring, offset) \ 204 (&ring->adev->wb.wb[offset]) 205 206 /** 207 * amdgpu_ring_init - init driver ring struct. 208 * 209 * @adev: amdgpu_device pointer 210 * @ring: amdgpu_ring structure holding ring information 211 * @max_dw: maximum number of dw for ring alloc 212 * @irq_src: interrupt source to use for this ring 213 * @irq_type: interrupt type to use for this ring 214 * @hw_prio: ring priority (NORMAL/HIGH) 215 * @sched_score: optional score atomic shared with other schedulers 216 * 217 * Initialize the driver information for the selected ring (all asics). 218 * Returns 0 on success, error on failure. 219 */ 220 int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring, 221 unsigned int max_dw, struct amdgpu_irq_src *irq_src, 222 unsigned int irq_type, unsigned int hw_prio, 223 atomic_t *sched_score) 224 { 225 int r; 226 int sched_hw_submission = amdgpu_sched_hw_submission; 227 u32 *num_sched; 228 u32 hw_ip; 229 unsigned int max_ibs_dw; 230 231 /* Set the hw submission limit higher for KIQ because 232 * it's used for a number of gfx/compute tasks by both 233 * KFD and KGD which may have outstanding fences and 234 * it doesn't really use the gpu scheduler anyway; 235 * KIQ tasks get submitted directly to the ring. 236 */ 237 if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ) 238 sched_hw_submission = max(sched_hw_submission, 256); 239 if (ring->funcs->type == AMDGPU_RING_TYPE_MES) 240 sched_hw_submission = 8; 241 else if (ring == &adev->sdma.instance[0].page) 242 sched_hw_submission = 256; 243 244 if (ring->adev == NULL) { 245 if (adev->num_rings >= AMDGPU_MAX_RINGS) 246 return -EINVAL; 247 248 ring->adev = adev; 249 ring->num_hw_submission = sched_hw_submission; 250 ring->sched_score = sched_score; 251 252 ring->idx = adev->num_rings++; 253 adev->rings[ring->idx] = ring; 254 255 r = amdgpu_fence_driver_init_ring(ring); 256 if (r) 257 return r; 258 } 259 260 r = amdgpu_wb_get(adev, &ring->rptr_offs); 261 if (r) { 262 dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r); 263 return r; 264 } 265 266 r = amdgpu_wb_get(adev, &ring->wptr_offs); 267 if (r) { 268 dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r); 269 return r; 270 } 271 272 r = amdgpu_wb_get(adev, &ring->fence_offs); 273 if (r) { 274 dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r); 275 return r; 276 } 277 278 r = amdgpu_wb_get(adev, &ring->trail_fence_offs); 279 if (r) { 280 dev_err(adev->dev, "(%d) ring trail_fence_offs wb alloc failed\n", r); 281 return r; 282 } 283 284 r = amdgpu_wb_get(adev, &ring->cond_exe_offs); 285 if (r) { 286 dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r); 287 return r; 288 } 289 290 ring->fence_gpu_addr = 291 amdgpu_ring_get_gpu_addr(ring, ring->fence_offs); 292 ring->fence_cpu_addr = 293 amdgpu_ring_get_cpu_addr(ring, ring->fence_offs); 294 295 ring->rptr_gpu_addr = 296 amdgpu_ring_get_gpu_addr(ring, ring->rptr_offs); 297 ring->rptr_cpu_addr = 298 amdgpu_ring_get_cpu_addr(ring, ring->rptr_offs); 299 300 ring->wptr_gpu_addr = 301 amdgpu_ring_get_gpu_addr(ring, ring->wptr_offs); 302 ring->wptr_cpu_addr = 303 amdgpu_ring_get_cpu_addr(ring, ring->wptr_offs); 304 305 ring->trail_fence_gpu_addr = 306 amdgpu_ring_get_gpu_addr(ring, ring->trail_fence_offs); 307 ring->trail_fence_cpu_addr = 308 amdgpu_ring_get_cpu_addr(ring, ring->trail_fence_offs); 309 310 ring->cond_exe_gpu_addr = 311 amdgpu_ring_get_gpu_addr(ring, ring->cond_exe_offs); 312 ring->cond_exe_cpu_addr = 313 amdgpu_ring_get_cpu_addr(ring, ring->cond_exe_offs); 314 315 /* always set cond_exec_polling to CONTINUE */ 316 *ring->cond_exe_cpu_addr = 1; 317 318 if (ring->funcs->type != AMDGPU_RING_TYPE_CPER) { 319 r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type); 320 if (r) { 321 dev_err(adev->dev, "failed initializing fences (%d).\n", r); 322 return r; 323 } 324 325 max_ibs_dw = ring->funcs->emit_frame_size + 326 amdgpu_ring_max_ibs(ring->funcs->type) * ring->funcs->emit_ib_size; 327 max_ibs_dw = (max_ibs_dw + ring->funcs->align_mask) & ~ring->funcs->align_mask; 328 329 if (WARN_ON(max_ibs_dw > max_dw)) 330 max_dw = max_ibs_dw; 331 332 ring->ring_size = roundup_pow_of_two(max_dw * 4 * sched_hw_submission); 333 } else { 334 ring->ring_size = roundup_pow_of_two(max_dw * 4); 335 ring->count_dw = (ring->ring_size - 4) >> 2; 336 /* ring buffer is empty now */ 337 ring->wptr = *ring->rptr_cpu_addr = 0; 338 } 339 340 ring->buf_mask = (ring->ring_size / 4) - 1; 341 ring->ptr_mask = ring->funcs->support_64bit_ptrs ? 342 0xffffffffffffffff : ring->buf_mask; 343 344 if (!ring->ring_backup) { 345 ring->ring_backup = kvzalloc(ring->ring_size, GFP_KERNEL); 346 if (!ring->ring_backup) 347 return -ENOMEM; 348 } 349 350 /* Allocate ring buffer */ 351 if (ring->ring_obj == NULL) { 352 r = amdgpu_bo_create_kernel(adev, ring->ring_size + ring->funcs->extra_bytes, 353 PAGE_SIZE, 354 AMDGPU_GEM_DOMAIN_GTT, 355 &ring->ring_obj, 356 &ring->gpu_addr, 357 (void **)&ring->ring); 358 if (r) { 359 dev_err(adev->dev, "(%d) ring create failed\n", r); 360 kvfree(ring->ring_backup); 361 return r; 362 } 363 amdgpu_ring_clear_ring(ring); 364 } 365 366 ring->max_dw = max_dw; 367 ring->hw_prio = hw_prio; 368 ring->vmid_wait = dma_fence_get_stub(); 369 370 if (!ring->no_scheduler && ring->funcs->type < AMDGPU_HW_IP_NUM) { 371 hw_ip = ring->funcs->type; 372 num_sched = &adev->gpu_sched[hw_ip][hw_prio].num_scheds; 373 adev->gpu_sched[hw_ip][hw_prio].sched[(*num_sched)++] = 374 &ring->sched; 375 } 376 377 return 0; 378 } 379 380 /** 381 * amdgpu_ring_fini - tear down the driver ring struct. 382 * 383 * @ring: amdgpu_ring structure holding ring information 384 * 385 * Tear down the driver information for the selected ring (all asics). 386 */ 387 void amdgpu_ring_fini(struct amdgpu_ring *ring) 388 { 389 390 /* Not to finish a ring which is not initialized */ 391 if (!(ring->adev) || !(ring->adev->rings[ring->idx])) 392 return; 393 394 ring->sched.ready = false; 395 396 amdgpu_wb_free(ring->adev, ring->rptr_offs); 397 amdgpu_wb_free(ring->adev, ring->wptr_offs); 398 399 amdgpu_wb_free(ring->adev, ring->cond_exe_offs); 400 amdgpu_wb_free(ring->adev, ring->fence_offs); 401 402 amdgpu_bo_free_kernel(&ring->ring_obj, 403 &ring->gpu_addr, 404 (void **)&ring->ring); 405 kvfree(ring->ring_backup); 406 ring->ring_backup = NULL; 407 408 dma_fence_put(ring->vmid_wait); 409 ring->vmid_wait = NULL; 410 ring->me = 0; 411 } 412 413 /** 414 * amdgpu_ring_emit_reg_write_reg_wait_helper - ring helper 415 * 416 * @ring: ring to write to 417 * @reg0: register to write 418 * @reg1: register to wait on 419 * @ref: reference value to write/wait on 420 * @mask: mask to wait on 421 * 422 * Helper for rings that don't support write and wait in a 423 * single oneshot packet. 424 */ 425 void amdgpu_ring_emit_reg_write_reg_wait_helper(struct amdgpu_ring *ring, 426 uint32_t reg0, uint32_t reg1, 427 uint32_t ref, uint32_t mask) 428 { 429 amdgpu_ring_emit_wreg(ring, reg0, ref); 430 amdgpu_ring_emit_reg_wait(ring, reg1, mask, mask); 431 } 432 433 /** 434 * amdgpu_ring_soft_recovery - try to soft recover a ring lockup 435 * 436 * @ring: ring to try the recovery on 437 * @vmid: VMID we try to get going again 438 * @fence: timedout fence 439 * 440 * Tries to get a ring proceeding again when it is stuck. 441 */ 442 bool amdgpu_ring_soft_recovery(struct amdgpu_ring *ring, unsigned int vmid, 443 struct dma_fence *fence) 444 { 445 unsigned long flags; 446 ktime_t deadline; 447 bool ret; 448 449 deadline = ktime_add_us(ktime_get(), 10000); 450 451 if (amdgpu_sriov_vf(ring->adev) || !ring->funcs->soft_recovery || !fence) 452 return false; 453 454 dma_fence_lock_irqsave(fence, flags); 455 if (!dma_fence_is_signaled_locked(fence)) 456 dma_fence_set_error(fence, -ENODATA); 457 dma_fence_unlock_irqrestore(fence, flags); 458 459 while (!dma_fence_is_signaled(fence) && 460 ktime_to_ns(ktime_sub(deadline, ktime_get())) > 0) 461 ring->funcs->soft_recovery(ring, vmid); 462 463 ret = dma_fence_is_signaled(fence); 464 /* increment the counter only if soft reset worked */ 465 if (ret) 466 atomic_inc(&ring->adev->gpu_reset_counter); 467 468 return ret; 469 } 470 471 /* 472 * Debugfs info 473 */ 474 #if defined(CONFIG_DEBUG_FS) 475 476 static ssize_t amdgpu_ras_cper_debugfs_read(struct file *f, char __user *buf, 477 size_t size, loff_t *offset) 478 { 479 const u8 ring_header_size = 12; 480 struct amdgpu_ring *ring = file_inode(f)->i_private; 481 struct ras_cmd_cper_snapshot_req *snapshot_req __free(kfree) = 482 kzalloc_obj(struct ras_cmd_cper_snapshot_req); 483 struct ras_cmd_cper_snapshot_rsp *snapshot_rsp __free(kfree) = 484 kzalloc_obj(struct ras_cmd_cper_snapshot_rsp); 485 struct ras_cmd_cper_record_req *record_req __free(kfree) = 486 kzalloc_obj(struct ras_cmd_cper_record_req); 487 struct ras_cmd_cper_record_rsp *record_rsp __free(kfree) = 488 kzalloc_obj(struct ras_cmd_cper_record_rsp); 489 u32 *ring_header __free(kfree) = 490 kzalloc(ring_header_size, GFP_KERNEL); 491 char __user *data_buf = buf; 492 size_t data_size = size; 493 u32 total_cper_num; 494 u64 start_cper_id; 495 u64 cper_offset; 496 size_t chunk_size; 497 size_t total_data_size = 0; 498 bool read_header; 499 int r; 500 501 if (!snapshot_req || !snapshot_rsp || !record_req || !record_rsp || 502 !ring_header) 503 return -ENOMEM; 504 505 read_header = !(*offset); 506 cper_offset = read_header ? 0 : *offset - 1; 507 508 if (read_header) { 509 /* Need at least 12 bytes for the header on the first read */ 510 if (size < ring_header_size) 511 return -EINVAL; 512 data_buf += ring_header_size; 513 data_size -= ring_header_size; 514 } 515 516 r = amdgpu_ras_mgr_handle_ras_cmd(ring->adev, 517 RAS_CMD__GET_CPER_SNAPSHOT, 518 snapshot_req, sizeof(struct ras_cmd_cper_snapshot_req), 519 snapshot_rsp, sizeof(struct ras_cmd_cper_snapshot_rsp)); 520 if (r) 521 return -EINVAL; 522 523 if (!snapshot_rsp->total_cper_num) { 524 if (!read_header) 525 return 0; 526 527 if (copy_to_user(buf, ring_header, ring_header_size)) 528 return -EFAULT; 529 530 *offset = 1; 531 return ring_header_size; 532 } 533 534 start_cper_id = snapshot_rsp->start_cper_id; 535 total_cper_num = snapshot_rsp->total_cper_num; 536 if (read_header && !data_size) { 537 if (copy_to_user(buf, ring_header, ring_header_size)) 538 return -EFAULT; 539 540 *offset = cper_offset + 1; 541 return ring_header_size; 542 } 543 544 if (!data_size) 545 return 0; 546 547 while (data_size && cper_offset < total_cper_num) { 548 memset(record_req, 0, sizeof(*record_req)); 549 memset(record_rsp, 0, sizeof(*record_rsp)); 550 chunk_size = min_t(size_t, data_size, RAS_CMD_MAX_CPER_BUF_SZ); 551 552 record_req->buf_ptr = (u64)(uintptr_t)data_buf; 553 record_req->buf_size = chunk_size; 554 record_req->cper_start_id = start_cper_id + cper_offset; 555 record_req->cper_num = total_cper_num - cper_offset; 556 r = amdgpu_ras_mgr_handle_ras_cmd(ring->adev, 557 RAS_CMD__GET_CPER_RECORD, 558 record_req, 559 sizeof(struct ras_cmd_cper_record_req), 560 record_rsp, 561 sizeof(struct ras_cmd_cper_record_rsp)); 562 if (r) 563 return -EINVAL; 564 565 if (!record_rsp->real_data_size || !record_rsp->real_cper_num) 566 break; 567 if (record_rsp->real_data_size > data_size) 568 return -EIO; 569 570 data_buf += record_rsp->real_data_size; 571 data_size -= record_rsp->real_data_size; 572 total_data_size += record_rsp->real_data_size; 573 cper_offset += record_rsp->real_cper_num; 574 } 575 576 if (read_header) { 577 ring_header[1] = total_data_size >> 2; 578 ring_header[2] = ring_header[1]; 579 580 if (copy_to_user(buf, ring_header, ring_header_size)) 581 return -EFAULT; 582 } 583 584 r = read_header ? total_data_size + ring_header_size : total_data_size; 585 *offset = cper_offset + 1; 586 587 return r; 588 } 589 590 /* Layout of file is 12 bytes consisting of 591 * - rptr 592 * - wptr 593 * - driver's copy of wptr 594 * 595 * followed by n-words of ring data 596 */ 597 static ssize_t amdgpu_debugfs_ring_read(struct file *f, char __user *buf, 598 size_t size, loff_t *pos) 599 { 600 struct amdgpu_ring *ring = file_inode(f)->i_private; 601 u32 value, result, early[3] = { 0 }; 602 uint64_t p; 603 u32 avail_dw, start_dw, read_dw; 604 loff_t i; 605 int r; 606 607 if (ring->funcs->type == AMDGPU_RING_TYPE_CPER && amdgpu_uniras_enabled(ring->adev)) 608 return amdgpu_ras_cper_debugfs_read(f, buf, size, pos); 609 610 if (*pos & 3 || size & 3) 611 return -EINVAL; 612 613 result = 0; 614 615 if (ring->funcs->type == AMDGPU_RING_TYPE_CPER) 616 mutex_lock(&ring->adev->cper.ring_lock); 617 618 if (*pos < 12) { 619 early[0] = amdgpu_ring_get_rptr(ring) & ring->buf_mask; 620 early[1] = amdgpu_ring_get_wptr(ring) & ring->buf_mask; 621 early[2] = ring->wptr & ring->buf_mask; 622 for (i = *pos / 4; i < 3 && size; i++) { 623 r = put_user(early[i], (uint32_t *)buf); 624 if (r) { 625 result = r; 626 goto out; 627 } 628 buf += 4; 629 result += 4; 630 size -= 4; 631 *pos += 4; 632 } 633 } 634 635 if (ring->funcs->type != AMDGPU_RING_TYPE_CPER) { 636 while (size) { 637 if (*pos >= (ring->ring_size + 12)) 638 return result; 639 640 value = ring->ring[(*pos - 12)/4]; 641 r = put_user(value, (uint32_t *)buf); 642 if (r) 643 return r; 644 buf += 4; 645 result += 4; 646 size -= 4; 647 *pos += 4; 648 } 649 } else { 650 early[0] = amdgpu_ring_get_rptr(ring) & ring->buf_mask; 651 early[1] = amdgpu_ring_get_wptr(ring) & ring->buf_mask; 652 653 p = early[0]; 654 if (early[0] <= early[1]) 655 avail_dw = early[1] - early[0]; 656 else 657 avail_dw = ring->buf_mask + 1 - (early[0] - early[1]); 658 659 start_dw = (*pos > 12) ? ((*pos - 12) >> 2) : 0; 660 if (start_dw >= avail_dw) 661 goto out; 662 663 p = (p + start_dw) & ring->ptr_mask; 664 avail_dw -= start_dw; 665 read_dw = min_t(u32, avail_dw, size >> 2); 666 667 while (read_dw) { 668 if (p == early[1]) 669 goto out; 670 671 value = ring->ring[p]; 672 r = put_user(value, (uint32_t *)buf); 673 if (r) { 674 result = r; 675 goto out; 676 } 677 678 buf += 4; 679 result += 4; 680 read_dw--; 681 p++; 682 p &= ring->ptr_mask; 683 *pos += 4; 684 } 685 } 686 687 out: 688 if (ring->funcs->type == AMDGPU_RING_TYPE_CPER) 689 mutex_unlock(&ring->adev->cper.ring_lock); 690 691 return result; 692 } 693 694 static ssize_t amdgpu_debugfs_virt_ring_read(struct file *f, char __user *buf, 695 size_t size, loff_t *pos) 696 { 697 struct amdgpu_ring *ring = file_inode(f)->i_private; 698 699 if (*pos & 3 || size & 3) 700 return -EINVAL; 701 702 if (ring->funcs->type == AMDGPU_RING_TYPE_CPER) 703 amdgpu_virt_req_ras_cper_dump(ring->adev, false); 704 705 return amdgpu_debugfs_ring_read(f, buf, size, pos); 706 } 707 708 static const struct file_operations amdgpu_debugfs_ring_fops = { 709 .owner = THIS_MODULE, 710 .read = amdgpu_debugfs_ring_read, 711 .llseek = default_llseek 712 }; 713 714 static const struct file_operations amdgpu_debugfs_virt_ring_fops = { 715 .owner = THIS_MODULE, 716 .read = amdgpu_debugfs_virt_ring_read, 717 .llseek = default_llseek 718 }; 719 720 static ssize_t amdgpu_debugfs_mqd_read(struct file *f, char __user *buf, 721 size_t size, loff_t *pos) 722 { 723 struct amdgpu_ring *ring = file_inode(f)->i_private; 724 ssize_t bytes = min_t(ssize_t, ring->mqd_size - *pos, size); 725 void *from = ((u8 *)ring->mqd_ptr) + *pos; 726 727 if (*pos > ring->mqd_size) 728 return 0; 729 730 if (copy_to_user(buf, from, bytes)) 731 return -EFAULT; 732 733 *pos += bytes; 734 return bytes; 735 } 736 737 static const struct file_operations amdgpu_debugfs_mqd_fops = { 738 .owner = THIS_MODULE, 739 .read = amdgpu_debugfs_mqd_read, 740 .llseek = default_llseek 741 }; 742 743 static int amdgpu_debugfs_ring_error(void *data, u64 val) 744 { 745 struct amdgpu_ring *ring = data; 746 747 amdgpu_fence_driver_set_error(ring, val); 748 return 0; 749 } 750 751 DEFINE_DEBUGFS_ATTRIBUTE_SIGNED(amdgpu_debugfs_error_fops, NULL, 752 amdgpu_debugfs_ring_error, "%lld\n"); 753 754 #endif 755 756 void amdgpu_debugfs_ring_init(struct amdgpu_device *adev, 757 struct amdgpu_ring *ring) 758 { 759 #if defined(CONFIG_DEBUG_FS) 760 struct drm_minor *minor = adev_to_drm(adev)->primary; 761 struct dentry *root = minor->debugfs_root; 762 char name[32]; 763 764 sprintf(name, "amdgpu_ring_%s", ring->name); 765 if (amdgpu_sriov_vf(adev)) 766 debugfs_create_file_size(name, S_IFREG | 0444, root, ring, 767 &amdgpu_debugfs_virt_ring_fops, 768 ring->ring_size + 12); 769 else 770 debugfs_create_file_size(name, S_IFREG | 0444, root, ring, 771 &amdgpu_debugfs_ring_fops, 772 ring->ring_size + 12); 773 774 if (ring->mqd_obj) { 775 sprintf(name, "amdgpu_mqd_%s", ring->name); 776 debugfs_create_file_size(name, S_IFREG | 0444, root, ring, 777 &amdgpu_debugfs_mqd_fops, 778 ring->mqd_size); 779 } 780 781 sprintf(name, "amdgpu_error_%s", ring->name); 782 debugfs_create_file(name, 0200, root, ring, 783 &amdgpu_debugfs_error_fops); 784 785 #endif 786 } 787 788 /** 789 * amdgpu_ring_test_helper - tests ring and set sched readiness status 790 * 791 * @ring: ring to try the recovery on 792 * 793 * Tests ring and set sched readiness status 794 * 795 * Returns 0 on success, error on failure. 796 */ 797 int amdgpu_ring_test_helper(struct amdgpu_ring *ring) 798 { 799 struct amdgpu_device *adev = ring->adev; 800 int r; 801 802 r = amdgpu_ring_test_ring(ring); 803 if (r) 804 DRM_DEV_ERROR(adev->dev, "ring %s test failed (%d)\n", 805 ring->name, r); 806 else 807 DRM_DEV_DEBUG(adev->dev, "ring test on %s succeeded\n", 808 ring->name); 809 810 ring->sched.ready = !r; 811 812 return r; 813 } 814 815 static void amdgpu_ring_to_mqd_prop(struct amdgpu_ring *ring, 816 struct amdgpu_mqd_prop *prop) 817 { 818 struct amdgpu_device *adev = ring->adev; 819 bool is_high_prio_compute = ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE && 820 amdgpu_gfx_is_high_priority_compute_queue(adev, ring); 821 bool is_high_prio_gfx = ring->funcs->type == AMDGPU_RING_TYPE_GFX && 822 amdgpu_gfx_is_high_priority_graphics_queue(adev, ring); 823 824 memset(prop, 0, sizeof(*prop)); 825 826 prop->mqd_gpu_addr = ring->mqd_gpu_addr; 827 prop->hqd_base_gpu_addr = ring->gpu_addr; 828 prop->rptr_gpu_addr = ring->rptr_gpu_addr; 829 prop->wptr_gpu_addr = ring->wptr_gpu_addr; 830 prop->queue_size = ring->ring_size; 831 prop->eop_gpu_addr = ring->eop_gpu_addr; 832 prop->use_doorbell = ring->use_doorbell; 833 prop->doorbell_index = ring->doorbell_index; 834 prop->kernel_queue = true; 835 836 /* map_queues packet doesn't need activate the queue, 837 * so only kiq need set this field. 838 */ 839 prop->hqd_active = ring->funcs->type == AMDGPU_RING_TYPE_KIQ; 840 841 prop->allow_tunneling = is_high_prio_compute; 842 if (is_high_prio_compute || is_high_prio_gfx) { 843 prop->hqd_pipe_priority = AMDGPU_GFX_PIPE_PRIO_HIGH; 844 prop->hqd_queue_priority = AMDGPU_GFX_QUEUE_PRIORITY_MAXIMUM; 845 } 846 } 847 848 int amdgpu_ring_init_mqd(struct amdgpu_ring *ring) 849 { 850 struct amdgpu_device *adev = ring->adev; 851 struct amdgpu_mqd *mqd_mgr; 852 struct amdgpu_mqd_prop prop; 853 854 amdgpu_ring_to_mqd_prop(ring, &prop); 855 856 ring->wptr = 0; 857 858 if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ) 859 mqd_mgr = &adev->mqds[AMDGPU_HW_IP_COMPUTE]; 860 else 861 mqd_mgr = &adev->mqds[ring->funcs->type]; 862 863 return mqd_mgr->init_mqd(adev, ring->mqd_ptr, &prop); 864 } 865 866 void amdgpu_ring_ib_begin(struct amdgpu_ring *ring) 867 { 868 if (ring->is_sw_ring) 869 amdgpu_sw_ring_ib_begin(ring); 870 } 871 872 void amdgpu_ring_ib_end(struct amdgpu_ring *ring) 873 { 874 if (ring->is_sw_ring) 875 amdgpu_sw_ring_ib_end(ring); 876 } 877 878 void amdgpu_ring_ib_on_emit_cntl(struct amdgpu_ring *ring) 879 { 880 if (ring->is_sw_ring) 881 amdgpu_sw_ring_ib_mark_offset(ring, AMDGPU_MUX_OFFSET_TYPE_CONTROL); 882 } 883 884 void amdgpu_ring_ib_on_emit_ce(struct amdgpu_ring *ring) 885 { 886 if (ring->is_sw_ring) 887 amdgpu_sw_ring_ib_mark_offset(ring, AMDGPU_MUX_OFFSET_TYPE_CE); 888 } 889 890 void amdgpu_ring_ib_on_emit_de(struct amdgpu_ring *ring) 891 { 892 if (ring->is_sw_ring) 893 amdgpu_sw_ring_ib_mark_offset(ring, AMDGPU_MUX_OFFSET_TYPE_DE); 894 } 895 896 bool amdgpu_ring_sched_ready(struct amdgpu_ring *ring) 897 { 898 if (!ring) 899 return false; 900 901 if (ring->no_scheduler || !drm_sched_wqueue_ready(&ring->sched)) 902 return false; 903 904 return true; 905 } 906 907 void amdgpu_ring_reset_helper_begin(struct amdgpu_ring *ring, 908 struct amdgpu_fence *guilty_fence) 909 { 910 /* back up the non-guilty commands */ 911 amdgpu_ring_backup_unprocessed_commands(ring, guilty_fence); 912 } 913 914 int amdgpu_ring_reset_helper_end(struct amdgpu_ring *ring, 915 struct amdgpu_fence *guilty_fence) 916 { 917 int r; 918 919 /* verify that the ring is functional */ 920 r = amdgpu_ring_test_ring(ring); 921 if (r) 922 return r; 923 924 /* set an error on all fences from the context and reemit */ 925 amdgpu_ring_set_fence_errors_and_reemit(ring, guilty_fence); 926 927 return 0; 928 } 929 930 /** 931 * amdgpu_multi_ring_reset_helper_begin() - Prepare multiple rings for a reset. 932 * 933 * @ring_type_mask: Bitmask of affected ring types 934 * @guilty_ring: The ring which is guilty of causing a reset. 935 * @guilty_fence: The fence which didn't signal on the guilty ring. 936 * 937 * Useful when performing a GPU reset method that affects 938 * multiple rings at the same time, such as an IP block soft 939 * reset. For example, a GFX IP block soft reset will affect 940 * every graphics and compute queue. 941 * 942 * This function should be called before such a reset. 943 * 944 * Prepare the affected rings before the reset, make sure to 945 * minimize collateral damage, and backup the contents of 946 * the rings. Then the caller can call the actual HW specific 947 * reset function. 948 * 949 * After the reset is complete, the caller should then call 950 * amdgpu_multi_ring_reset_helper_end() to restore the rings. 951 */ 952 void amdgpu_multi_ring_reset_helper_begin(const u32 ring_type_mask, 953 struct amdgpu_ring *guilty_ring, 954 struct amdgpu_fence *guilty_fence) 955 { 956 struct amdgpu_device *adev = guilty_ring->adev; 957 struct amdgpu_fence *ring_guilty_fence; 958 struct amdgpu_ring *ring; 959 bool rings_busy; 960 int i; 961 u32 t; 962 963 for (i = 0; i < adev->num_rings; ++i) { 964 ring = adev->rings[i]; 965 966 if (!(BIT(ring->funcs->type) & ring_type_mask)) 967 continue; 968 969 /* Don't accept new submissions on the ring. */ 970 if (amdgpu_ring_sched_ready(ring) && !drm_sched_is_stopped(&ring->sched)) 971 drm_sched_wqueue_stop(&ring->sched); 972 973 /* 974 * Clear the preempt condition to stop the ring 975 * from starting its next submission. This ensures 976 * that only the currently executing submission 977 * can be rejected because of the reset and helps 978 * minimize collateral damage. 979 */ 980 if (ring->funcs->init_cond_exec) 981 amdgpu_ring_set_preempt_cond_exec(ring, false); 982 } 983 984 /* Flush HDP cache so the GPU can see the updated COND_EXEC values */ 985 amdgpu_device_flush_hdp(adev, NULL); 986 987 /* 988 * Give some time for non-guilty rings to finish their 989 * current submission, to try to minimize collateral damage. 990 * 991 * Note that this is just a best effort, but really there 992 * is no way to really know which ring is actually responsible 993 * because different rings may share resources, eg. a compute 994 * ring may hog shader engines, causing a graphics ring to hang. 995 */ 996 for (t = 0; t < adev->usec_timeout; t += 10000) { 997 rings_busy = false; 998 999 /* Check if any of the non-guilty rings are busy */ 1000 for (i = 0; i < adev->num_rings; ++i) { 1001 ring = adev->rings[i]; 1002 1003 if (!(BIT(ring->funcs->type) & ring_type_mask)) 1004 continue; 1005 1006 if (ring == guilty_ring) 1007 continue; 1008 1009 rings_busy |= 1010 atomic_read(&ring->fence_drv.last_seq) != 1011 READ_ONCE(ring->fence_drv.sync_seq); 1012 } 1013 1014 if (!rings_busy) 1015 break; 1016 1017 mdelay(10); 1018 } 1019 1020 for (i = 0; i < adev->num_rings; ++i) { 1021 ring = adev->rings[i]; 1022 1023 if (!(BIT(ring->funcs->type) & ring_type_mask)) 1024 continue; 1025 1026 /* 1027 * Find guilty fences, ie. the fences that didn't signal 1028 * on each ring. At this point there is no way to know 1029 * which one is really responsible for the hang, and no 1030 * way to save any of them, so we treat all of them as guilty. 1031 */ 1032 ring_guilty_fence = 1033 ring == guilty_ring ? guilty_fence : 1034 amdgpu_ring_find_guilty_fence(ring); 1035 1036 /* 1037 * Backup current contents of the ring. 1038 * The helper takes care to only reemit unsignalled fences 1039 * so we don't have to worry about that here. 1040 */ 1041 amdgpu_ring_reset_helper_begin(ring, ring_guilty_fence); 1042 } 1043 } 1044 1045 /** 1046 * amdgpu_multi_ring_reset_helper_end() - Prepare multiple rings for a reset. 1047 * 1048 * @ring_type_mask: Bitmask of affected ring types 1049 * @guilty_ring: The ring which is guilty of causing a reset. 1050 * @ret: Return code from the reset function. 1051 * 1052 * After calling amdgpu_multi_ring_reset_helper_begin() 1053 * and executing the actual reset method, call this 1054 * function to restore normal operation. 1055 * 1056 * In case the reset failed, this function should still 1057 * be called to restore preemption state, but it won't attempt to 1058 * fully restore the ring contents. 1059 */ 1060 int amdgpu_multi_ring_reset_helper_end(const u32 ring_type_mask, 1061 struct amdgpu_ring *guilty_ring, int ret) 1062 { 1063 struct amdgpu_device *adev = guilty_ring->adev; 1064 struct amdgpu_ring *ring; 1065 int i, r; 1066 1067 /* Set preempt condition, rings are now allowed to execute submissions */ 1068 for (i = 0; i < adev->num_rings; ++i) { 1069 ring = adev->rings[i]; 1070 1071 if (!(BIT(ring->funcs->type) & ring_type_mask)) 1072 continue; 1073 1074 if (ring->funcs->init_cond_exec) 1075 amdgpu_ring_set_preempt_cond_exec(ring, true); 1076 } 1077 1078 /* Flush HDP cache so the GPU can see the updated COND_EXEC values */ 1079 amdgpu_device_flush_hdp(adev, NULL); 1080 1081 /* If the reset was unsuccessful, return without restoring anything else. */ 1082 if (ret) 1083 return ret; 1084 1085 /* Restore contents of all rings */ 1086 for (i = 0; i < adev->num_rings; ++i) { 1087 ring = adev->rings[i]; 1088 1089 if (!(BIT(ring->funcs->type) & ring_type_mask)) 1090 continue; 1091 1092 /* Restore contents of the ring */ 1093 r = amdgpu_ring_reset_helper_end(ring, ring->guilty_fence); 1094 if (r) { 1095 dev_err(adev->dev, 1096 "Failed to recover ring %s after soft reset\n", 1097 ring->name); 1098 return r; 1099 } 1100 } 1101 1102 /* Accept submissions on all rings again */ 1103 for (i = 0; i < adev->num_rings; ++i) { 1104 ring = adev->rings[i]; 1105 1106 if (!(BIT(ring->funcs->type) & ring_type_mask)) 1107 continue; 1108 1109 if (!amdgpu_ring_sched_ready(ring)) 1110 continue; 1111 1112 drm_sched_wqueue_start(&ring->sched); 1113 } 1114 1115 return 0; 1116 } 1117 1118 bool amdgpu_ring_is_reset_type_supported(struct amdgpu_ring *ring, 1119 u32 reset_type) 1120 { 1121 switch (ring->funcs->type) { 1122 case AMDGPU_RING_TYPE_GFX: 1123 if (ring->adev->gfx.gfx_supported_reset & reset_type) 1124 return true; 1125 break; 1126 case AMDGPU_RING_TYPE_COMPUTE: 1127 if (ring->adev->gfx.compute_supported_reset & reset_type) 1128 return true; 1129 break; 1130 case AMDGPU_RING_TYPE_SDMA: 1131 if (ring->adev->sdma.supported_reset & reset_type) 1132 return true; 1133 break; 1134 case AMDGPU_RING_TYPE_VCN_DEC: 1135 case AMDGPU_RING_TYPE_VCN_ENC: 1136 if (ring->adev->vcn.supported_reset & reset_type) 1137 return true; 1138 break; 1139 case AMDGPU_RING_TYPE_VCN_JPEG: 1140 if (ring->adev->jpeg.supported_reset & reset_type) 1141 return true; 1142 break; 1143 default: 1144 break; 1145 } 1146 return false; 1147 } 1148