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 46 * Fences mark an event in the GPUs pipeline and are used 47 * for GPU/CPU synchronization. When the fence is written, 48 * it is expected that all buffers associated with that fence 49 * are no longer in use by the associated ring on the GPU and 50 * that the the relevant GPU caches have been flushed. 51 */ 52 53 struct amdgpu_fence { 54 struct dma_fence base; 55 56 /* RB, DMA, etc. */ 57 struct amdgpu_ring *ring; 58 }; 59 60 static struct kmem_cache *amdgpu_fence_slab; 61 62 int amdgpu_fence_slab_init(void) 63 { 64 amdgpu_fence_slab = kmem_cache_create( 65 "amdgpu_fence", sizeof(struct amdgpu_fence), 0, 66 SLAB_HWCACHE_ALIGN, NULL); 67 if (!amdgpu_fence_slab) 68 return -ENOMEM; 69 return 0; 70 } 71 72 void amdgpu_fence_slab_fini(void) 73 { 74 rcu_barrier(); 75 kmem_cache_destroy(amdgpu_fence_slab); 76 } 77 /* 78 * Cast helper 79 */ 80 static const struct dma_fence_ops amdgpu_fence_ops; 81 static const struct dma_fence_ops amdgpu_job_fence_ops; 82 static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f) 83 { 84 struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base); 85 86 if (__f->base.ops == &amdgpu_fence_ops || 87 __f->base.ops == &amdgpu_job_fence_ops) 88 return __f; 89 90 return NULL; 91 } 92 93 /** 94 * amdgpu_fence_write - write a fence value 95 * 96 * @ring: ring the fence is associated with 97 * @seq: sequence number to write 98 * 99 * Writes a fence value to memory (all asics). 100 */ 101 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq) 102 { 103 struct amdgpu_fence_driver *drv = &ring->fence_drv; 104 105 if (drv->cpu_addr) 106 *drv->cpu_addr = cpu_to_le32(seq); 107 } 108 109 /** 110 * amdgpu_fence_read - read a fence value 111 * 112 * @ring: ring the fence is associated with 113 * 114 * Reads a fence value from memory (all asics). 115 * Returns the value of the fence read from memory. 116 */ 117 static u32 amdgpu_fence_read(struct amdgpu_ring *ring) 118 { 119 struct amdgpu_fence_driver *drv = &ring->fence_drv; 120 u32 seq = 0; 121 122 if (drv->cpu_addr) 123 seq = le32_to_cpu(*drv->cpu_addr); 124 else 125 seq = atomic_read(&drv->last_seq); 126 127 return seq; 128 } 129 130 /** 131 * amdgpu_fence_emit - emit a fence on the requested ring 132 * 133 * @ring: ring the fence is associated with 134 * @f: resulting fence object 135 * @job: job the fence is embedded in 136 * @flags: flags to pass into the subordinate .emit_fence() call 137 * 138 * Emits a fence command on the requested ring (all asics). 139 * Returns 0 on success, -ENOMEM on failure. 140 */ 141 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f, struct amdgpu_job *job, 142 unsigned flags) 143 { 144 struct amdgpu_device *adev = ring->adev; 145 struct dma_fence *fence; 146 struct amdgpu_fence *am_fence; 147 struct dma_fence __rcu **ptr; 148 uint32_t seq; 149 int r; 150 151 if (job == NULL) { 152 /* create a sperate hw fence */ 153 am_fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_ATOMIC); 154 if (am_fence == NULL) 155 return -ENOMEM; 156 fence = &am_fence->base; 157 am_fence->ring = ring; 158 } else { 159 /* take use of job-embedded fence */ 160 fence = &job->hw_fence; 161 } 162 163 seq = ++ring->fence_drv.sync_seq; 164 if (job && job->job_run_counter) { 165 /* reinit seq for resubmitted jobs */ 166 fence->seqno = seq; 167 } else { 168 if (job) 169 dma_fence_init(fence, &amdgpu_job_fence_ops, 170 &ring->fence_drv.lock, 171 adev->fence_context + ring->idx, seq); 172 else 173 dma_fence_init(fence, &amdgpu_fence_ops, 174 &ring->fence_drv.lock, 175 adev->fence_context + ring->idx, seq); 176 } 177 178 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr, 179 seq, flags | AMDGPU_FENCE_FLAG_INT); 180 pm_runtime_get_noresume(adev_to_drm(adev)->dev); 181 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask]; 182 if (unlikely(rcu_dereference_protected(*ptr, 1))) { 183 struct dma_fence *old; 184 185 rcu_read_lock(); 186 old = dma_fence_get_rcu_safe(ptr); 187 rcu_read_unlock(); 188 189 if (old) { 190 r = dma_fence_wait(old, false); 191 dma_fence_put(old); 192 if (r) 193 return r; 194 } 195 } 196 197 /* This function can't be called concurrently anyway, otherwise 198 * emitting the fence would mess up the hardware ring buffer. 199 */ 200 rcu_assign_pointer(*ptr, dma_fence_get(fence)); 201 202 *f = fence; 203 204 return 0; 205 } 206 207 /** 208 * amdgpu_fence_emit_polling - emit a fence on the requeste ring 209 * 210 * @ring: ring the fence is associated with 211 * @s: resulting sequence number 212 * @timeout: the timeout for waiting in usecs 213 * 214 * Emits a fence command on the requested ring (all asics). 215 * Used For polling fence. 216 * Returns 0 on success, -ENOMEM on failure. 217 */ 218 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s, 219 uint32_t timeout) 220 { 221 uint32_t seq; 222 signed long r; 223 224 if (!s) 225 return -EINVAL; 226 227 seq = ++ring->fence_drv.sync_seq; 228 r = amdgpu_fence_wait_polling(ring, 229 seq - ring->fence_drv.num_fences_mask, 230 timeout); 231 if (r < 1) 232 return -ETIMEDOUT; 233 234 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr, 235 seq, 0); 236 237 *s = seq; 238 239 return 0; 240 } 241 242 /** 243 * amdgpu_fence_schedule_fallback - schedule fallback check 244 * 245 * @ring: pointer to struct amdgpu_ring 246 * 247 * Start a timer as fallback to our interrupts. 248 */ 249 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring) 250 { 251 mod_timer(&ring->fence_drv.fallback_timer, 252 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT); 253 } 254 255 /** 256 * amdgpu_fence_process - check for fence activity 257 * 258 * @ring: pointer to struct amdgpu_ring 259 * 260 * Checks the current fence value and calculates the last 261 * signalled fence value. Wakes the fence queue if the 262 * sequence number has increased. 263 * 264 * Returns true if fence was processed 265 */ 266 bool amdgpu_fence_process(struct amdgpu_ring *ring) 267 { 268 struct amdgpu_fence_driver *drv = &ring->fence_drv; 269 struct amdgpu_device *adev = ring->adev; 270 uint32_t seq, last_seq; 271 272 do { 273 last_seq = atomic_read(&ring->fence_drv.last_seq); 274 seq = amdgpu_fence_read(ring); 275 276 } while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq); 277 278 if (del_timer(&ring->fence_drv.fallback_timer) && 279 seq != ring->fence_drv.sync_seq) 280 amdgpu_fence_schedule_fallback(ring); 281 282 if (unlikely(seq == last_seq)) 283 return false; 284 285 last_seq &= drv->num_fences_mask; 286 seq &= drv->num_fences_mask; 287 288 do { 289 struct dma_fence *fence, **ptr; 290 291 ++last_seq; 292 last_seq &= drv->num_fences_mask; 293 ptr = &drv->fences[last_seq]; 294 295 /* There is always exactly one thread signaling this fence slot */ 296 fence = rcu_dereference_protected(*ptr, 1); 297 RCU_INIT_POINTER(*ptr, NULL); 298 299 if (!fence) 300 continue; 301 302 dma_fence_signal(fence); 303 dma_fence_put(fence); 304 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 305 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 306 } while (last_seq != seq); 307 308 return true; 309 } 310 311 /** 312 * amdgpu_fence_fallback - fallback for hardware interrupts 313 * 314 * @t: timer context used to obtain the pointer to ring structure 315 * 316 * Checks for fence activity. 317 */ 318 static void amdgpu_fence_fallback(struct timer_list *t) 319 { 320 struct amdgpu_ring *ring = from_timer(ring, t, 321 fence_drv.fallback_timer); 322 323 if (amdgpu_fence_process(ring)) 324 DRM_WARN("Fence fallback timer expired on ring %s\n", ring->name); 325 } 326 327 /** 328 * amdgpu_fence_wait_empty - wait for all fences to signal 329 * 330 * @ring: ring index the fence is associated with 331 * 332 * Wait for all fences on the requested ring to signal (all asics). 333 * Returns 0 if the fences have passed, error for all other cases. 334 */ 335 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring) 336 { 337 uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq); 338 struct dma_fence *fence, **ptr; 339 int r; 340 341 if (!seq) 342 return 0; 343 344 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask]; 345 rcu_read_lock(); 346 fence = rcu_dereference(*ptr); 347 if (!fence || !dma_fence_get_rcu(fence)) { 348 rcu_read_unlock(); 349 return 0; 350 } 351 rcu_read_unlock(); 352 353 r = dma_fence_wait(fence, false); 354 dma_fence_put(fence); 355 return r; 356 } 357 358 /** 359 * amdgpu_fence_wait_polling - busy wait for givn sequence number 360 * 361 * @ring: ring index the fence is associated with 362 * @wait_seq: sequence number to wait 363 * @timeout: the timeout for waiting in usecs 364 * 365 * Wait for all fences on the requested ring to signal (all asics). 366 * Returns left time if no timeout, 0 or minus if timeout. 367 */ 368 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring, 369 uint32_t wait_seq, 370 signed long timeout) 371 { 372 uint32_t seq; 373 374 do { 375 seq = amdgpu_fence_read(ring); 376 udelay(5); 377 timeout -= 5; 378 } while ((int32_t)(wait_seq - seq) > 0 && timeout > 0); 379 380 return timeout > 0 ? timeout : 0; 381 } 382 /** 383 * amdgpu_fence_count_emitted - get the count of emitted fences 384 * 385 * @ring: ring the fence is associated with 386 * 387 * Get the number of fences emitted on the requested ring (all asics). 388 * Returns the number of emitted fences on the ring. Used by the 389 * dynpm code to ring track activity. 390 */ 391 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring) 392 { 393 uint64_t emitted; 394 395 /* We are not protected by ring lock when reading the last sequence 396 * but it's ok to report slightly wrong fence count here. 397 */ 398 amdgpu_fence_process(ring); 399 emitted = 0x100000000ull; 400 emitted -= atomic_read(&ring->fence_drv.last_seq); 401 emitted += READ_ONCE(ring->fence_drv.sync_seq); 402 return lower_32_bits(emitted); 403 } 404 405 /** 406 * amdgpu_fence_driver_start_ring - make the fence driver 407 * ready for use on the requested ring. 408 * 409 * @ring: ring to start the fence driver on 410 * @irq_src: interrupt source to use for this ring 411 * @irq_type: interrupt type to use for this ring 412 * 413 * Make the fence driver ready for processing (all asics). 414 * Not all asics have all rings, so each asic will only 415 * start the fence driver on the rings it has. 416 * Returns 0 for success, errors for failure. 417 */ 418 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring, 419 struct amdgpu_irq_src *irq_src, 420 unsigned irq_type) 421 { 422 struct amdgpu_device *adev = ring->adev; 423 uint64_t index; 424 425 if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) { 426 ring->fence_drv.cpu_addr = ring->fence_cpu_addr; 427 ring->fence_drv.gpu_addr = ring->fence_gpu_addr; 428 } else { 429 /* put fence directly behind firmware */ 430 index = ALIGN(adev->uvd.fw->size, 8); 431 ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index; 432 ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index; 433 } 434 amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq)); 435 436 ring->fence_drv.irq_src = irq_src; 437 ring->fence_drv.irq_type = irq_type; 438 ring->fence_drv.initialized = true; 439 440 DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr 0x%016llx\n", 441 ring->name, ring->fence_drv.gpu_addr); 442 return 0; 443 } 444 445 /** 446 * amdgpu_fence_driver_init_ring - init the fence driver 447 * for the requested ring. 448 * 449 * @ring: ring to init the fence driver on 450 * 451 * Init the fence driver for the requested ring (all asics). 452 * Helper function for amdgpu_fence_driver_init(). 453 */ 454 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring) 455 { 456 struct amdgpu_device *adev = ring->adev; 457 458 if (!adev) 459 return -EINVAL; 460 461 if (!is_power_of_2(ring->num_hw_submission)) 462 return -EINVAL; 463 464 ring->fence_drv.cpu_addr = NULL; 465 ring->fence_drv.gpu_addr = 0; 466 ring->fence_drv.sync_seq = 0; 467 atomic_set(&ring->fence_drv.last_seq, 0); 468 ring->fence_drv.initialized = false; 469 470 timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0); 471 472 ring->fence_drv.num_fences_mask = ring->num_hw_submission * 2 - 1; 473 spin_lock_init(&ring->fence_drv.lock); 474 ring->fence_drv.fences = kcalloc(ring->num_hw_submission * 2, sizeof(void *), 475 GFP_KERNEL); 476 477 if (!ring->fence_drv.fences) 478 return -ENOMEM; 479 480 return 0; 481 } 482 483 /** 484 * amdgpu_fence_driver_sw_init - init the fence driver 485 * for all possible rings. 486 * 487 * @adev: amdgpu device pointer 488 * 489 * Init the fence driver for all possible rings (all asics). 490 * Not all asics have all rings, so each asic will only 491 * start the fence driver on the rings it has using 492 * amdgpu_fence_driver_start_ring(). 493 * Returns 0 for success. 494 */ 495 int amdgpu_fence_driver_sw_init(struct amdgpu_device *adev) 496 { 497 return 0; 498 } 499 500 /** 501 * amdgpu_fence_driver_hw_fini - tear down the fence driver 502 * for all possible rings. 503 * 504 * @adev: amdgpu device pointer 505 * 506 * Tear down the fence driver for all possible rings (all asics). 507 */ 508 void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev) 509 { 510 int i, r; 511 512 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 513 struct amdgpu_ring *ring = adev->rings[i]; 514 515 if (!ring || !ring->fence_drv.initialized) 516 continue; 517 518 /* You can't wait for HW to signal if it's gone */ 519 if (!drm_dev_is_unplugged(adev_to_drm(adev))) 520 r = amdgpu_fence_wait_empty(ring); 521 else 522 r = -ENODEV; 523 /* no need to trigger GPU reset as we are unloading */ 524 if (r) 525 amdgpu_fence_driver_force_completion(ring); 526 527 if (ring->fence_drv.irq_src) 528 amdgpu_irq_put(adev, ring->fence_drv.irq_src, 529 ring->fence_drv.irq_type); 530 531 del_timer_sync(&ring->fence_drv.fallback_timer); 532 } 533 } 534 535 void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev) 536 { 537 unsigned int i, j; 538 539 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 540 struct amdgpu_ring *ring = adev->rings[i]; 541 542 if (!ring || !ring->fence_drv.initialized) 543 continue; 544 545 if (!ring->no_scheduler) 546 drm_sched_fini(&ring->sched); 547 548 for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j) 549 dma_fence_put(ring->fence_drv.fences[j]); 550 kfree(ring->fence_drv.fences); 551 ring->fence_drv.fences = NULL; 552 ring->fence_drv.initialized = false; 553 } 554 } 555 556 /** 557 * amdgpu_fence_driver_hw_init - enable the fence driver 558 * for all possible rings. 559 * 560 * @adev: amdgpu device pointer 561 * 562 * Enable the fence driver for all possible rings (all asics). 563 * Not all asics have all rings, so each asic will only 564 * start the fence driver on the rings it has using 565 * amdgpu_fence_driver_start_ring(). 566 * Returns 0 for success. 567 */ 568 void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev) 569 { 570 int i; 571 572 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 573 struct amdgpu_ring *ring = adev->rings[i]; 574 if (!ring || !ring->fence_drv.initialized) 575 continue; 576 577 /* enable the interrupt */ 578 if (ring->fence_drv.irq_src) 579 amdgpu_irq_get(adev, ring->fence_drv.irq_src, 580 ring->fence_drv.irq_type); 581 } 582 } 583 584 /** 585 * amdgpu_fence_driver_clear_job_fences - clear job embedded fences of ring 586 * 587 * @ring: fence of the ring to be cleared 588 * 589 */ 590 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring) 591 { 592 int i; 593 struct dma_fence *old, **ptr; 594 595 for (i = 0; i <= ring->fence_drv.num_fences_mask; i++) { 596 ptr = &ring->fence_drv.fences[i]; 597 old = rcu_dereference_protected(*ptr, 1); 598 if (old && old->ops == &amdgpu_job_fence_ops) 599 RCU_INIT_POINTER(*ptr, NULL); 600 } 601 } 602 603 /** 604 * amdgpu_fence_driver_force_completion - force signal latest fence of ring 605 * 606 * @ring: fence of the ring to signal 607 * 608 */ 609 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) 610 { 611 amdgpu_fence_write(ring, ring->fence_drv.sync_seq); 612 amdgpu_fence_process(ring); 613 } 614 615 /* 616 * Common fence implementation 617 */ 618 619 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence) 620 { 621 return "amdgpu"; 622 } 623 624 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f) 625 { 626 return (const char *)to_amdgpu_fence(f)->ring->name; 627 } 628 629 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f) 630 { 631 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence); 632 633 return (const char *)to_amdgpu_ring(job->base.sched)->name; 634 } 635 636 /** 637 * amdgpu_fence_enable_signaling - enable signalling on fence 638 * @f: fence 639 * 640 * This function is called with fence_queue lock held, and adds a callback 641 * to fence_queue that checks if this fence is signaled, and if so it 642 * signals the fence and removes itself. 643 */ 644 static bool amdgpu_fence_enable_signaling(struct dma_fence *f) 645 { 646 if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer)) 647 amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring); 648 649 return true; 650 } 651 652 /** 653 * amdgpu_job_fence_enable_signaling - enable signalling on job fence 654 * @f: fence 655 * 656 * This is the simliar function with amdgpu_fence_enable_signaling above, it 657 * only handles the job embedded fence. 658 */ 659 static bool amdgpu_job_fence_enable_signaling(struct dma_fence *f) 660 { 661 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence); 662 663 if (!timer_pending(&to_amdgpu_ring(job->base.sched)->fence_drv.fallback_timer)) 664 amdgpu_fence_schedule_fallback(to_amdgpu_ring(job->base.sched)); 665 666 return true; 667 } 668 669 /** 670 * amdgpu_fence_free - free up the fence memory 671 * 672 * @rcu: RCU callback head 673 * 674 * Free up the fence memory after the RCU grace period. 675 */ 676 static void amdgpu_fence_free(struct rcu_head *rcu) 677 { 678 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu); 679 680 /* free fence_slab if it's separated fence*/ 681 kmem_cache_free(amdgpu_fence_slab, to_amdgpu_fence(f)); 682 } 683 684 /** 685 * amdgpu_job_fence_free - free up the job with embedded fence 686 * 687 * @rcu: RCU callback head 688 * 689 * Free up the job with embedded fence after the RCU grace period. 690 */ 691 static void amdgpu_job_fence_free(struct rcu_head *rcu) 692 { 693 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu); 694 695 /* free job if fence has a parent job */ 696 kfree(container_of(f, struct amdgpu_job, hw_fence)); 697 } 698 699 /** 700 * amdgpu_fence_release - callback that fence can be freed 701 * 702 * @f: fence 703 * 704 * This function is called when the reference count becomes zero. 705 * It just RCU schedules freeing up the fence. 706 */ 707 static void amdgpu_fence_release(struct dma_fence *f) 708 { 709 call_rcu(&f->rcu, amdgpu_fence_free); 710 } 711 712 /** 713 * amdgpu_job_fence_release - callback that job embedded fence can be freed 714 * 715 * @f: fence 716 * 717 * This is the simliar function with amdgpu_fence_release above, it 718 * only handles the job embedded fence. 719 */ 720 static void amdgpu_job_fence_release(struct dma_fence *f) 721 { 722 call_rcu(&f->rcu, amdgpu_job_fence_free); 723 } 724 725 static const struct dma_fence_ops amdgpu_fence_ops = { 726 .get_driver_name = amdgpu_fence_get_driver_name, 727 .get_timeline_name = amdgpu_fence_get_timeline_name, 728 .enable_signaling = amdgpu_fence_enable_signaling, 729 .release = amdgpu_fence_release, 730 }; 731 732 static const struct dma_fence_ops amdgpu_job_fence_ops = { 733 .get_driver_name = amdgpu_fence_get_driver_name, 734 .get_timeline_name = amdgpu_job_fence_get_timeline_name, 735 .enable_signaling = amdgpu_job_fence_enable_signaling, 736 .release = amdgpu_job_fence_release, 737 }; 738 739 /* 740 * Fence debugfs 741 */ 742 #if defined(CONFIG_DEBUG_FS) 743 static int amdgpu_debugfs_fence_info_show(struct seq_file *m, void *unused) 744 { 745 struct amdgpu_device *adev = (struct amdgpu_device *)m->private; 746 int i; 747 748 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 749 struct amdgpu_ring *ring = adev->rings[i]; 750 if (!ring || !ring->fence_drv.initialized) 751 continue; 752 753 amdgpu_fence_process(ring); 754 755 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name); 756 seq_printf(m, "Last signaled fence 0x%08x\n", 757 atomic_read(&ring->fence_drv.last_seq)); 758 seq_printf(m, "Last emitted 0x%08x\n", 759 ring->fence_drv.sync_seq); 760 761 if (ring->funcs->type == AMDGPU_RING_TYPE_GFX || 762 ring->funcs->type == AMDGPU_RING_TYPE_SDMA) { 763 seq_printf(m, "Last signaled trailing fence 0x%08x\n", 764 le32_to_cpu(*ring->trail_fence_cpu_addr)); 765 seq_printf(m, "Last emitted 0x%08x\n", 766 ring->trail_seq); 767 } 768 769 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX) 770 continue; 771 772 /* set in CP_VMID_PREEMPT and preemption occurred */ 773 seq_printf(m, "Last preempted 0x%08x\n", 774 le32_to_cpu(*(ring->fence_drv.cpu_addr + 2))); 775 /* set in CP_VMID_RESET and reset occurred */ 776 seq_printf(m, "Last reset 0x%08x\n", 777 le32_to_cpu(*(ring->fence_drv.cpu_addr + 4))); 778 /* Both preemption and reset occurred */ 779 seq_printf(m, "Last both 0x%08x\n", 780 le32_to_cpu(*(ring->fence_drv.cpu_addr + 6))); 781 } 782 return 0; 783 } 784 785 /* 786 * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover 787 * 788 * Manually trigger a gpu reset at the next fence wait. 789 */ 790 static int gpu_recover_get(void *data, u64 *val) 791 { 792 struct amdgpu_device *adev = (struct amdgpu_device *)data; 793 struct drm_device *dev = adev_to_drm(adev); 794 int r; 795 796 r = pm_runtime_get_sync(dev->dev); 797 if (r < 0) { 798 pm_runtime_put_autosuspend(dev->dev); 799 return 0; 800 } 801 802 if (amdgpu_reset_domain_schedule(adev->reset_domain, &adev->reset_work)) 803 flush_work(&adev->reset_work); 804 805 *val = atomic_read(&adev->reset_domain->reset_res); 806 807 pm_runtime_mark_last_busy(dev->dev); 808 pm_runtime_put_autosuspend(dev->dev); 809 810 return 0; 811 } 812 813 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_fence_info); 814 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_gpu_recover_fops, gpu_recover_get, NULL, 815 "%lld\n"); 816 817 static void amdgpu_debugfs_reset_work(struct work_struct *work) 818 { 819 struct amdgpu_device *adev = container_of(work, struct amdgpu_device, 820 reset_work); 821 822 amdgpu_device_gpu_recover(adev, NULL); 823 } 824 825 #endif 826 827 void amdgpu_debugfs_fence_init(struct amdgpu_device *adev) 828 { 829 #if defined(CONFIG_DEBUG_FS) 830 struct drm_minor *minor = adev_to_drm(adev)->primary; 831 struct dentry *root = minor->debugfs_root; 832 833 debugfs_create_file("amdgpu_fence_info", 0444, root, adev, 834 &amdgpu_debugfs_fence_info_fops); 835 836 if (!amdgpu_sriov_vf(adev)) { 837 838 INIT_WORK(&adev->reset_work, amdgpu_debugfs_reset_work); 839 debugfs_create_file("amdgpu_gpu_recover", 0444, root, adev, 840 &amdgpu_debugfs_gpu_recover_fops); 841 } 842 #endif 843 } 844 845