1 /* 2 * Copyright 2009 Jerome Glisse. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 19 * USE OR OTHER DEALINGS IN THE SOFTWARE. 20 * 21 * The above copyright notice and this permission notice (including the 22 * next paragraph) shall be included in all copies or substantial portions 23 * of the Software. 24 * 25 */ 26 /* 27 * Authors: 28 * Jerome Glisse <glisse@freedesktop.org> 29 * Dave Airlie 30 */ 31 #include <linux/seq_file.h> 32 #include <linux/atomic.h> 33 #include <linux/wait.h> 34 #include <linux/kref.h> 35 #include <linux/slab.h> 36 #include <linux/firmware.h> 37 #include <linux/pm_runtime.h> 38 39 #include <drm/drm_drv.h> 40 #include "amdgpu.h" 41 #include "amdgpu_trace.h" 42 #include "amdgpu_reset.h" 43 44 /* 45 * Fences mark an event in the GPUs pipeline and are used 46 * for GPU/CPU synchronization. When the fence is written, 47 * it is expected that all buffers associated with that fence 48 * are no longer in use by the associated ring on the GPU and 49 * that the relevant GPU caches have been flushed. 50 */ 51 52 struct amdgpu_fence { 53 struct dma_fence base; 54 55 /* RB, DMA, etc. */ 56 struct amdgpu_ring *ring; 57 ktime_t start_timestamp; 58 }; 59 60 static struct pool amdgpu_fence_slab; 61 62 int amdgpu_fence_slab_init(void) 63 { 64 #ifdef __linux__ 65 amdgpu_fence_slab = kmem_cache_create( 66 "amdgpu_fence", sizeof(struct amdgpu_fence), 0, 67 SLAB_HWCACHE_ALIGN, NULL); 68 if (!amdgpu_fence_slab) 69 return -ENOMEM; 70 #else 71 pool_init(&amdgpu_fence_slab, sizeof(struct amdgpu_fence), 72 CACHELINESIZE, IPL_TTY, 0, "amdgpu_fence", NULL); 73 #endif 74 return 0; 75 } 76 77 void amdgpu_fence_slab_fini(void) 78 { 79 rcu_barrier(); 80 #ifdef __linux__ 81 kmem_cache_destroy(amdgpu_fence_slab); 82 #else 83 pool_destroy(&amdgpu_fence_slab); 84 #endif 85 } 86 /* 87 * Cast helper 88 */ 89 static const struct dma_fence_ops amdgpu_fence_ops; 90 static const struct dma_fence_ops amdgpu_job_fence_ops; 91 static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f) 92 { 93 struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base); 94 95 if (__f->base.ops == &amdgpu_fence_ops || 96 __f->base.ops == &amdgpu_job_fence_ops) 97 return __f; 98 99 return NULL; 100 } 101 102 /** 103 * amdgpu_fence_write - write a fence value 104 * 105 * @ring: ring the fence is associated with 106 * @seq: sequence number to write 107 * 108 * Writes a fence value to memory (all asics). 109 */ 110 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq) 111 { 112 struct amdgpu_fence_driver *drv = &ring->fence_drv; 113 114 if (drv->cpu_addr) 115 *drv->cpu_addr = cpu_to_le32(seq); 116 } 117 118 /** 119 * amdgpu_fence_read - read a fence value 120 * 121 * @ring: ring the fence is associated with 122 * 123 * Reads a fence value from memory (all asics). 124 * Returns the value of the fence read from memory. 125 */ 126 static u32 amdgpu_fence_read(struct amdgpu_ring *ring) 127 { 128 struct amdgpu_fence_driver *drv = &ring->fence_drv; 129 u32 seq = 0; 130 131 if (drv->cpu_addr) 132 seq = le32_to_cpu(*drv->cpu_addr); 133 else 134 seq = atomic_read(&drv->last_seq); 135 136 return seq; 137 } 138 139 /** 140 * amdgpu_fence_emit - emit a fence on the requested ring 141 * 142 * @ring: ring the fence is associated with 143 * @f: resulting fence object 144 * @job: job the fence is embedded in 145 * @flags: flags to pass into the subordinate .emit_fence() call 146 * 147 * Emits a fence command on the requested ring (all asics). 148 * Returns 0 on success, -ENOMEM on failure. 149 */ 150 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f, struct amdgpu_job *job, 151 unsigned int flags) 152 { 153 struct amdgpu_device *adev = ring->adev; 154 struct dma_fence *fence; 155 struct amdgpu_fence *am_fence; 156 struct dma_fence __rcu **ptr; 157 uint32_t seq; 158 int r; 159 160 if (job == NULL) { 161 /* create a sperate hw fence */ 162 #ifdef __linux__ 163 am_fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_ATOMIC); 164 #else 165 am_fence = pool_get(&amdgpu_fence_slab, PR_NOWAIT); 166 #endif 167 if (am_fence == NULL) 168 return -ENOMEM; 169 fence = &am_fence->base; 170 am_fence->ring = ring; 171 } else { 172 /* take use of job-embedded fence */ 173 fence = &job->hw_fence; 174 } 175 176 seq = ++ring->fence_drv.sync_seq; 177 if (job && job->job_run_counter) { 178 /* reinit seq for resubmitted jobs */ 179 fence->seqno = seq; 180 /* TO be inline with external fence creation and other drivers */ 181 dma_fence_get(fence); 182 } else { 183 if (job) { 184 dma_fence_init(fence, &amdgpu_job_fence_ops, 185 &ring->fence_drv.lock, 186 adev->fence_context + ring->idx, seq); 187 /* Against remove in amdgpu_job_{free, free_cb} */ 188 dma_fence_get(fence); 189 } else { 190 dma_fence_init(fence, &amdgpu_fence_ops, 191 &ring->fence_drv.lock, 192 adev->fence_context + ring->idx, seq); 193 } 194 } 195 196 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr, 197 seq, flags | AMDGPU_FENCE_FLAG_INT); 198 pm_runtime_get_noresume(adev_to_drm(adev)->dev); 199 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask]; 200 if (unlikely(rcu_dereference_protected(*ptr, 1))) { 201 struct dma_fence *old; 202 203 rcu_read_lock(); 204 old = dma_fence_get_rcu_safe(ptr); 205 rcu_read_unlock(); 206 207 if (old) { 208 r = dma_fence_wait(old, false); 209 dma_fence_put(old); 210 if (r) 211 return r; 212 } 213 } 214 215 to_amdgpu_fence(fence)->start_timestamp = ktime_get(); 216 217 /* This function can't be called concurrently anyway, otherwise 218 * emitting the fence would mess up the hardware ring buffer. 219 */ 220 rcu_assign_pointer(*ptr, dma_fence_get(fence)); 221 222 *f = fence; 223 224 return 0; 225 } 226 227 /** 228 * amdgpu_fence_emit_polling - emit a fence on the requeste ring 229 * 230 * @ring: ring the fence is associated with 231 * @s: resulting sequence number 232 * @timeout: the timeout for waiting in usecs 233 * 234 * Emits a fence command on the requested ring (all asics). 235 * Used For polling fence. 236 * Returns 0 on success, -ENOMEM on failure. 237 */ 238 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s, 239 uint32_t timeout) 240 { 241 uint32_t seq; 242 signed long r; 243 244 if (!s) 245 return -EINVAL; 246 247 seq = ++ring->fence_drv.sync_seq; 248 r = amdgpu_fence_wait_polling(ring, 249 seq - ring->fence_drv.num_fences_mask, 250 timeout); 251 if (r < 1) 252 return -ETIMEDOUT; 253 254 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr, 255 seq, 0); 256 257 *s = seq; 258 259 return 0; 260 } 261 262 /** 263 * amdgpu_fence_schedule_fallback - schedule fallback check 264 * 265 * @ring: pointer to struct amdgpu_ring 266 * 267 * Start a timer as fallback to our interrupts. 268 */ 269 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring) 270 { 271 mod_timer(&ring->fence_drv.fallback_timer, 272 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT); 273 } 274 275 /** 276 * amdgpu_fence_process - check for fence activity 277 * 278 * @ring: pointer to struct amdgpu_ring 279 * 280 * Checks the current fence value and calculates the last 281 * signalled fence value. Wakes the fence queue if the 282 * sequence number has increased. 283 * 284 * Returns true if fence was processed 285 */ 286 bool amdgpu_fence_process(struct amdgpu_ring *ring) 287 { 288 struct amdgpu_fence_driver *drv = &ring->fence_drv; 289 struct amdgpu_device *adev = ring->adev; 290 uint32_t seq, last_seq; 291 292 do { 293 last_seq = atomic_read(&ring->fence_drv.last_seq); 294 seq = amdgpu_fence_read(ring); 295 296 } while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq); 297 298 if (del_timer(&ring->fence_drv.fallback_timer) && 299 seq != ring->fence_drv.sync_seq) 300 amdgpu_fence_schedule_fallback(ring); 301 302 if (unlikely(seq == last_seq)) 303 return false; 304 305 last_seq &= drv->num_fences_mask; 306 seq &= drv->num_fences_mask; 307 308 do { 309 struct dma_fence *fence, **ptr; 310 311 ++last_seq; 312 last_seq &= drv->num_fences_mask; 313 ptr = &drv->fences[last_seq]; 314 315 /* There is always exactly one thread signaling this fence slot */ 316 fence = rcu_dereference_protected(*ptr, 1); 317 RCU_INIT_POINTER(*ptr, NULL); 318 319 if (!fence) 320 continue; 321 322 dma_fence_signal(fence); 323 dma_fence_put(fence); 324 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 325 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 326 } while (last_seq != seq); 327 328 return true; 329 } 330 331 /** 332 * amdgpu_fence_fallback - fallback for hardware interrupts 333 * 334 * @t: timer context used to obtain the pointer to ring structure 335 * 336 * Checks for fence activity. 337 */ 338 static void amdgpu_fence_fallback(void *arg) 339 { 340 struct amdgpu_ring *ring = arg; 341 342 if (amdgpu_fence_process(ring)) 343 DRM_WARN("Fence fallback timer expired on ring %s\n", ring->name); 344 } 345 346 /** 347 * amdgpu_fence_wait_empty - wait for all fences to signal 348 * 349 * @ring: ring index the fence is associated with 350 * 351 * Wait for all fences on the requested ring to signal (all asics). 352 * Returns 0 if the fences have passed, error for all other cases. 353 */ 354 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring) 355 { 356 uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq); 357 struct dma_fence *fence, **ptr; 358 int r; 359 360 if (!seq) 361 return 0; 362 363 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask]; 364 rcu_read_lock(); 365 fence = rcu_dereference(*ptr); 366 if (!fence || !dma_fence_get_rcu(fence)) { 367 rcu_read_unlock(); 368 return 0; 369 } 370 rcu_read_unlock(); 371 372 r = dma_fence_wait(fence, false); 373 dma_fence_put(fence); 374 return r; 375 } 376 377 /** 378 * amdgpu_fence_wait_polling - busy wait for givn sequence number 379 * 380 * @ring: ring index the fence is associated with 381 * @wait_seq: sequence number to wait 382 * @timeout: the timeout for waiting in usecs 383 * 384 * Wait for all fences on the requested ring to signal (all asics). 385 * Returns left time if no timeout, 0 or minus if timeout. 386 */ 387 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring, 388 uint32_t wait_seq, 389 signed long timeout) 390 { 391 392 while ((int32_t)(wait_seq - amdgpu_fence_read(ring)) > 0 && timeout > 0) { 393 udelay(2); 394 timeout -= 2; 395 } 396 return timeout > 0 ? timeout : 0; 397 } 398 /** 399 * amdgpu_fence_count_emitted - get the count of emitted fences 400 * 401 * @ring: ring the fence is associated with 402 * 403 * Get the number of fences emitted on the requested ring (all asics). 404 * Returns the number of emitted fences on the ring. Used by the 405 * dynpm code to ring track activity. 406 */ 407 unsigned int amdgpu_fence_count_emitted(struct amdgpu_ring *ring) 408 { 409 uint64_t emitted; 410 411 /* We are not protected by ring lock when reading the last sequence 412 * but it's ok to report slightly wrong fence count here. 413 */ 414 emitted = 0x100000000ull; 415 emitted -= atomic_read(&ring->fence_drv.last_seq); 416 emitted += READ_ONCE(ring->fence_drv.sync_seq); 417 return lower_32_bits(emitted); 418 } 419 420 /** 421 * amdgpu_fence_last_unsignaled_time_us - the time fence emitted until now 422 * @ring: ring the fence is associated with 423 * 424 * Find the earliest fence unsignaled until now, calculate the time delta 425 * between the time fence emitted and now. 426 */ 427 u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring) 428 { 429 struct amdgpu_fence_driver *drv = &ring->fence_drv; 430 struct dma_fence *fence; 431 uint32_t last_seq, sync_seq; 432 433 last_seq = atomic_read(&ring->fence_drv.last_seq); 434 sync_seq = READ_ONCE(ring->fence_drv.sync_seq); 435 if (last_seq == sync_seq) 436 return 0; 437 438 ++last_seq; 439 last_seq &= drv->num_fences_mask; 440 fence = drv->fences[last_seq]; 441 if (!fence) 442 return 0; 443 444 return ktime_us_delta(ktime_get(), 445 to_amdgpu_fence(fence)->start_timestamp); 446 } 447 448 /** 449 * amdgpu_fence_update_start_timestamp - update the timestamp of the fence 450 * @ring: ring the fence is associated with 451 * @seq: the fence seq number to update. 452 * @timestamp: the start timestamp to update. 453 * 454 * The function called at the time the fence and related ib is about to 455 * resubmit to gpu in MCBP scenario. Thus we do not consider race condition 456 * with amdgpu_fence_process to modify the same fence. 457 */ 458 void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq, ktime_t timestamp) 459 { 460 struct amdgpu_fence_driver *drv = &ring->fence_drv; 461 struct dma_fence *fence; 462 463 seq &= drv->num_fences_mask; 464 fence = drv->fences[seq]; 465 if (!fence) 466 return; 467 468 to_amdgpu_fence(fence)->start_timestamp = timestamp; 469 } 470 471 /** 472 * amdgpu_fence_driver_start_ring - make the fence driver 473 * ready for use on the requested ring. 474 * 475 * @ring: ring to start the fence driver on 476 * @irq_src: interrupt source to use for this ring 477 * @irq_type: interrupt type to use for this ring 478 * 479 * Make the fence driver ready for processing (all asics). 480 * Not all asics have all rings, so each asic will only 481 * start the fence driver on the rings it has. 482 * Returns 0 for success, errors for failure. 483 */ 484 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring, 485 struct amdgpu_irq_src *irq_src, 486 unsigned int irq_type) 487 { 488 struct amdgpu_device *adev = ring->adev; 489 uint64_t index; 490 491 if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) { 492 ring->fence_drv.cpu_addr = ring->fence_cpu_addr; 493 ring->fence_drv.gpu_addr = ring->fence_gpu_addr; 494 } else { 495 /* put fence directly behind firmware */ 496 index = ALIGN(adev->uvd.fw->size, 8); 497 ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index; 498 ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index; 499 } 500 amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq)); 501 502 ring->fence_drv.irq_src = irq_src; 503 ring->fence_drv.irq_type = irq_type; 504 ring->fence_drv.initialized = true; 505 506 DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr 0x%016llx\n", 507 ring->name, ring->fence_drv.gpu_addr); 508 return 0; 509 } 510 511 /** 512 * amdgpu_fence_driver_init_ring - init the fence driver 513 * for the requested ring. 514 * 515 * @ring: ring to init the fence driver on 516 * 517 * Init the fence driver for the requested ring (all asics). 518 * Helper function for amdgpu_fence_driver_init(). 519 */ 520 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring) 521 { 522 struct amdgpu_device *adev = ring->adev; 523 524 if (!adev) 525 return -EINVAL; 526 527 if (!is_power_of_2(ring->num_hw_submission)) 528 return -EINVAL; 529 530 ring->fence_drv.cpu_addr = NULL; 531 ring->fence_drv.gpu_addr = 0; 532 ring->fence_drv.sync_seq = 0; 533 atomic_set(&ring->fence_drv.last_seq, 0); 534 ring->fence_drv.initialized = false; 535 536 #ifdef __linux__ 537 timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0); 538 #else 539 timeout_set(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 540 ring); 541 #endif 542 543 ring->fence_drv.num_fences_mask = ring->num_hw_submission * 2 - 1; 544 mtx_init(&ring->fence_drv.lock, IPL_TTY); 545 ring->fence_drv.fences = kcalloc(ring->num_hw_submission * 2, sizeof(void *), 546 GFP_KERNEL); 547 548 if (!ring->fence_drv.fences) 549 return -ENOMEM; 550 551 return 0; 552 } 553 554 /** 555 * amdgpu_fence_driver_sw_init - init the fence driver 556 * for all possible rings. 557 * 558 * @adev: amdgpu device pointer 559 * 560 * Init the fence driver for all possible rings (all asics). 561 * Not all asics have all rings, so each asic will only 562 * start the fence driver on the rings it has using 563 * amdgpu_fence_driver_start_ring(). 564 * Returns 0 for success. 565 */ 566 int amdgpu_fence_driver_sw_init(struct amdgpu_device *adev) 567 { 568 return 0; 569 } 570 571 /** 572 * amdgpu_fence_need_ring_interrupt_restore - helper function to check whether 573 * fence driver interrupts need to be restored. 574 * 575 * @ring: ring that to be checked 576 * 577 * Interrupts for rings that belong to GFX IP don't need to be restored 578 * when the target power state is s0ix. 579 * 580 * Return true if need to restore interrupts, false otherwise. 581 */ 582 static bool amdgpu_fence_need_ring_interrupt_restore(struct amdgpu_ring *ring) 583 { 584 struct amdgpu_device *adev = ring->adev; 585 bool is_gfx_power_domain = false; 586 587 switch (ring->funcs->type) { 588 case AMDGPU_RING_TYPE_SDMA: 589 /* SDMA 5.x+ is part of GFX power domain so it's covered by GFXOFF */ 590 if (adev->ip_versions[SDMA0_HWIP][0] >= IP_VERSION(5, 0, 0)) 591 is_gfx_power_domain = true; 592 break; 593 case AMDGPU_RING_TYPE_GFX: 594 case AMDGPU_RING_TYPE_COMPUTE: 595 case AMDGPU_RING_TYPE_KIQ: 596 case AMDGPU_RING_TYPE_MES: 597 is_gfx_power_domain = true; 598 break; 599 default: 600 break; 601 } 602 603 return !(adev->in_s0ix && is_gfx_power_domain); 604 } 605 606 /** 607 * amdgpu_fence_driver_hw_fini - tear down the fence driver 608 * for all possible rings. 609 * 610 * @adev: amdgpu device pointer 611 * 612 * Tear down the fence driver for all possible rings (all asics). 613 */ 614 void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev) 615 { 616 int i, r; 617 618 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 619 struct amdgpu_ring *ring = adev->rings[i]; 620 621 if (!ring || !ring->fence_drv.initialized) 622 continue; 623 624 /* You can't wait for HW to signal if it's gone */ 625 if (!drm_dev_is_unplugged(adev_to_drm(adev))) 626 r = amdgpu_fence_wait_empty(ring); 627 else 628 r = -ENODEV; 629 /* no need to trigger GPU reset as we are unloading */ 630 if (r) 631 amdgpu_fence_driver_force_completion(ring); 632 633 if (!drm_dev_is_unplugged(adev_to_drm(adev)) && 634 ring->fence_drv.irq_src && 635 amdgpu_fence_need_ring_interrupt_restore(ring)) 636 amdgpu_irq_put(adev, ring->fence_drv.irq_src, 637 ring->fence_drv.irq_type); 638 639 del_timer_sync(&ring->fence_drv.fallback_timer); 640 } 641 } 642 643 /* Will either stop and flush handlers for amdgpu interrupt or reanble it */ 644 void amdgpu_fence_driver_isr_toggle(struct amdgpu_device *adev, bool stop) 645 { 646 STUB(); 647 #ifdef notyet 648 int i; 649 650 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 651 struct amdgpu_ring *ring = adev->rings[i]; 652 653 if (!ring || !ring->fence_drv.initialized || !ring->fence_drv.irq_src) 654 continue; 655 656 if (stop) 657 disable_irq(adev->irq.irq); 658 else 659 enable_irq(adev->irq.irq); 660 } 661 #endif 662 } 663 664 void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev) 665 { 666 unsigned int i, j; 667 668 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 669 struct amdgpu_ring *ring = adev->rings[i]; 670 671 if (!ring || !ring->fence_drv.initialized) 672 continue; 673 674 /* 675 * Notice we check for sched.ops since there's some 676 * override on the meaning of sched.ready by amdgpu. 677 * The natural check would be sched.ready, which is 678 * set as drm_sched_init() finishes... 679 */ 680 if (ring->sched.ops) 681 drm_sched_fini(&ring->sched); 682 683 for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j) 684 dma_fence_put(ring->fence_drv.fences[j]); 685 kfree(ring->fence_drv.fences); 686 ring->fence_drv.fences = NULL; 687 ring->fence_drv.initialized = false; 688 } 689 } 690 691 /** 692 * amdgpu_fence_driver_hw_init - enable the fence driver 693 * for all possible rings. 694 * 695 * @adev: amdgpu device pointer 696 * 697 * Enable the fence driver for all possible rings (all asics). 698 * Not all asics have all rings, so each asic will only 699 * start the fence driver on the rings it has using 700 * amdgpu_fence_driver_start_ring(). 701 * Returns 0 for success. 702 */ 703 void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev) 704 { 705 int i; 706 707 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 708 struct amdgpu_ring *ring = adev->rings[i]; 709 710 if (!ring || !ring->fence_drv.initialized) 711 continue; 712 713 /* enable the interrupt */ 714 if (ring->fence_drv.irq_src && 715 amdgpu_fence_need_ring_interrupt_restore(ring)) 716 amdgpu_irq_get(adev, ring->fence_drv.irq_src, 717 ring->fence_drv.irq_type); 718 } 719 } 720 721 /** 722 * amdgpu_fence_driver_clear_job_fences - clear job embedded fences of ring 723 * 724 * @ring: fence of the ring to be cleared 725 * 726 */ 727 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring) 728 { 729 int i; 730 struct dma_fence *old, **ptr; 731 732 for (i = 0; i <= ring->fence_drv.num_fences_mask; i++) { 733 ptr = &ring->fence_drv.fences[i]; 734 old = rcu_dereference_protected(*ptr, 1); 735 if (old && old->ops == &amdgpu_job_fence_ops) { 736 struct amdgpu_job *job; 737 738 /* For non-scheduler bad job, i.e. failed ib test, we need to signal 739 * it right here or we won't be able to track them in fence_drv 740 * and they will remain unsignaled during sa_bo free. 741 */ 742 job = container_of(old, struct amdgpu_job, hw_fence); 743 if (!job->base.s_fence && !dma_fence_is_signaled(old)) 744 dma_fence_signal(old); 745 RCU_INIT_POINTER(*ptr, NULL); 746 dma_fence_put(old); 747 } 748 } 749 } 750 751 /** 752 * amdgpu_fence_driver_set_error - set error code on fences 753 * @ring: the ring which contains the fences 754 * @error: the error code to set 755 * 756 * Set an error code to all the fences pending on the ring. 757 */ 758 void amdgpu_fence_driver_set_error(struct amdgpu_ring *ring, int error) 759 { 760 struct amdgpu_fence_driver *drv = &ring->fence_drv; 761 unsigned long flags; 762 763 spin_lock_irqsave(&drv->lock, flags); 764 for (unsigned int i = 0; i <= drv->num_fences_mask; ++i) { 765 struct dma_fence *fence; 766 767 fence = rcu_dereference_protected(drv->fences[i], 768 lockdep_is_held(&drv->lock)); 769 if (fence && !dma_fence_is_signaled_locked(fence)) 770 dma_fence_set_error(fence, error); 771 } 772 spin_unlock_irqrestore(&drv->lock, flags); 773 } 774 775 /** 776 * amdgpu_fence_driver_force_completion - force signal latest fence of ring 777 * 778 * @ring: fence of the ring to signal 779 * 780 */ 781 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) 782 { 783 amdgpu_fence_driver_set_error(ring, -ECANCELED); 784 amdgpu_fence_write(ring, ring->fence_drv.sync_seq); 785 amdgpu_fence_process(ring); 786 } 787 788 /* 789 * Common fence implementation 790 */ 791 792 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence) 793 { 794 return "amdgpu"; 795 } 796 797 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f) 798 { 799 return (const char *)to_amdgpu_fence(f)->ring->name; 800 } 801 802 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f) 803 { 804 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence); 805 806 return (const char *)to_amdgpu_ring(job->base.sched)->name; 807 } 808 809 /** 810 * amdgpu_fence_enable_signaling - enable signalling on fence 811 * @f: fence 812 * 813 * This function is called with fence_queue lock held, and adds a callback 814 * to fence_queue that checks if this fence is signaled, and if so it 815 * signals the fence and removes itself. 816 */ 817 static bool amdgpu_fence_enable_signaling(struct dma_fence *f) 818 { 819 if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer)) 820 amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring); 821 822 return true; 823 } 824 825 /** 826 * amdgpu_job_fence_enable_signaling - enable signalling on job fence 827 * @f: fence 828 * 829 * This is the simliar function with amdgpu_fence_enable_signaling above, it 830 * only handles the job embedded fence. 831 */ 832 static bool amdgpu_job_fence_enable_signaling(struct dma_fence *f) 833 { 834 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence); 835 836 if (!timer_pending(&to_amdgpu_ring(job->base.sched)->fence_drv.fallback_timer)) 837 amdgpu_fence_schedule_fallback(to_amdgpu_ring(job->base.sched)); 838 839 return true; 840 } 841 842 /** 843 * amdgpu_fence_free - free up the fence memory 844 * 845 * @rcu: RCU callback head 846 * 847 * Free up the fence memory after the RCU grace period. 848 */ 849 static void amdgpu_fence_free(struct rcu_head *rcu) 850 { 851 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu); 852 853 /* free fence_slab if it's separated fence*/ 854 #ifdef __linux__ 855 kmem_cache_free(amdgpu_fence_slab, to_amdgpu_fence(f)); 856 #else 857 pool_put(&amdgpu_fence_slab, to_amdgpu_fence(f)); 858 #endif 859 } 860 861 /** 862 * amdgpu_job_fence_free - free up the job with embedded fence 863 * 864 * @rcu: RCU callback head 865 * 866 * Free up the job with embedded fence after the RCU grace period. 867 */ 868 static void amdgpu_job_fence_free(struct rcu_head *rcu) 869 { 870 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu); 871 872 /* free job if fence has a parent job */ 873 kfree(container_of(f, struct amdgpu_job, hw_fence)); 874 } 875 876 /** 877 * amdgpu_fence_release - callback that fence can be freed 878 * 879 * @f: fence 880 * 881 * This function is called when the reference count becomes zero. 882 * It just RCU schedules freeing up the fence. 883 */ 884 static void amdgpu_fence_release(struct dma_fence *f) 885 { 886 call_rcu(&f->rcu, amdgpu_fence_free); 887 } 888 889 /** 890 * amdgpu_job_fence_release - callback that job embedded fence can be freed 891 * 892 * @f: fence 893 * 894 * This is the simliar function with amdgpu_fence_release above, it 895 * only handles the job embedded fence. 896 */ 897 static void amdgpu_job_fence_release(struct dma_fence *f) 898 { 899 call_rcu(&f->rcu, amdgpu_job_fence_free); 900 } 901 902 static const struct dma_fence_ops amdgpu_fence_ops = { 903 .get_driver_name = amdgpu_fence_get_driver_name, 904 .get_timeline_name = amdgpu_fence_get_timeline_name, 905 .enable_signaling = amdgpu_fence_enable_signaling, 906 .release = amdgpu_fence_release, 907 }; 908 909 static const struct dma_fence_ops amdgpu_job_fence_ops = { 910 .get_driver_name = amdgpu_fence_get_driver_name, 911 .get_timeline_name = amdgpu_job_fence_get_timeline_name, 912 .enable_signaling = amdgpu_job_fence_enable_signaling, 913 .release = amdgpu_job_fence_release, 914 }; 915 916 /* 917 * Fence debugfs 918 */ 919 #if defined(CONFIG_DEBUG_FS) 920 static int amdgpu_debugfs_fence_info_show(struct seq_file *m, void *unused) 921 { 922 struct amdgpu_device *adev = m->private; 923 int i; 924 925 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 926 struct amdgpu_ring *ring = adev->rings[i]; 927 928 if (!ring || !ring->fence_drv.initialized) 929 continue; 930 931 amdgpu_fence_process(ring); 932 933 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name); 934 seq_printf(m, "Last signaled fence 0x%08x\n", 935 atomic_read(&ring->fence_drv.last_seq)); 936 seq_printf(m, "Last emitted 0x%08x\n", 937 ring->fence_drv.sync_seq); 938 939 if (ring->funcs->type == AMDGPU_RING_TYPE_GFX || 940 ring->funcs->type == AMDGPU_RING_TYPE_SDMA) { 941 seq_printf(m, "Last signaled trailing fence 0x%08x\n", 942 le32_to_cpu(*ring->trail_fence_cpu_addr)); 943 seq_printf(m, "Last emitted 0x%08x\n", 944 ring->trail_seq); 945 } 946 947 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX) 948 continue; 949 950 /* set in CP_VMID_PREEMPT and preemption occurred */ 951 seq_printf(m, "Last preempted 0x%08x\n", 952 le32_to_cpu(*(ring->fence_drv.cpu_addr + 2))); 953 /* set in CP_VMID_RESET and reset occurred */ 954 seq_printf(m, "Last reset 0x%08x\n", 955 le32_to_cpu(*(ring->fence_drv.cpu_addr + 4))); 956 /* Both preemption and reset occurred */ 957 seq_printf(m, "Last both 0x%08x\n", 958 le32_to_cpu(*(ring->fence_drv.cpu_addr + 6))); 959 } 960 return 0; 961 } 962 963 /* 964 * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover 965 * 966 * Manually trigger a gpu reset at the next fence wait. 967 */ 968 static int gpu_recover_get(void *data, u64 *val) 969 { 970 struct amdgpu_device *adev = (struct amdgpu_device *)data; 971 struct drm_device *dev = adev_to_drm(adev); 972 int r; 973 974 r = pm_runtime_get_sync(dev->dev); 975 if (r < 0) { 976 pm_runtime_put_autosuspend(dev->dev); 977 return 0; 978 } 979 980 if (amdgpu_reset_domain_schedule(adev->reset_domain, &adev->reset_work)) 981 flush_work(&adev->reset_work); 982 983 *val = atomic_read(&adev->reset_domain->reset_res); 984 985 pm_runtime_mark_last_busy(dev->dev); 986 pm_runtime_put_autosuspend(dev->dev); 987 988 return 0; 989 } 990 991 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_fence_info); 992 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_gpu_recover_fops, gpu_recover_get, NULL, 993 "%lld\n"); 994 995 static void amdgpu_debugfs_reset_work(struct work_struct *work) 996 { 997 struct amdgpu_device *adev = container_of(work, struct amdgpu_device, 998 reset_work); 999 1000 struct amdgpu_reset_context reset_context; 1001 1002 memset(&reset_context, 0, sizeof(reset_context)); 1003 1004 reset_context.method = AMD_RESET_METHOD_NONE; 1005 reset_context.reset_req_dev = adev; 1006 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 1007 1008 amdgpu_device_gpu_recover(adev, NULL, &reset_context); 1009 } 1010 1011 #endif 1012 1013 void amdgpu_debugfs_fence_init(struct amdgpu_device *adev) 1014 { 1015 #if defined(CONFIG_DEBUG_FS) 1016 struct drm_minor *minor = adev_to_drm(adev)->primary; 1017 struct dentry *root = minor->debugfs_root; 1018 1019 debugfs_create_file("amdgpu_fence_info", 0444, root, adev, 1020 &amdgpu_debugfs_fence_info_fops); 1021 1022 if (!amdgpu_sriov_vf(adev)) { 1023 1024 INIT_WORK(&adev->reset_work, amdgpu_debugfs_reset_work); 1025 debugfs_create_file("amdgpu_gpu_recover", 0444, root, adev, 1026 &amdgpu_debugfs_gpu_recover_fops); 1027 } 1028 #endif 1029 } 1030 1031