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 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 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 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 } 190 else 191 dma_fence_init(fence, &amdgpu_fence_ops, 192 &ring->fence_drv.lock, 193 adev->fence_context + ring->idx, seq); 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 /* This function can't be called concurrently anyway, otherwise 216 * emitting the fence would mess up the hardware ring buffer. 217 */ 218 rcu_assign_pointer(*ptr, dma_fence_get(fence)); 219 220 *f = fence; 221 222 return 0; 223 } 224 225 /** 226 * amdgpu_fence_emit_polling - emit a fence on the requeste ring 227 * 228 * @ring: ring the fence is associated with 229 * @s: resulting sequence number 230 * @timeout: the timeout for waiting in usecs 231 * 232 * Emits a fence command on the requested ring (all asics). 233 * Used For polling fence. 234 * Returns 0 on success, -ENOMEM on failure. 235 */ 236 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s, 237 uint32_t timeout) 238 { 239 uint32_t seq; 240 signed long r; 241 242 if (!s) 243 return -EINVAL; 244 245 seq = ++ring->fence_drv.sync_seq; 246 r = amdgpu_fence_wait_polling(ring, 247 seq - ring->fence_drv.num_fences_mask, 248 timeout); 249 if (r < 1) 250 return -ETIMEDOUT; 251 252 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr, 253 seq, 0); 254 255 *s = seq; 256 257 return 0; 258 } 259 260 /** 261 * amdgpu_fence_schedule_fallback - schedule fallback check 262 * 263 * @ring: pointer to struct amdgpu_ring 264 * 265 * Start a timer as fallback to our interrupts. 266 */ 267 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring) 268 { 269 mod_timer(&ring->fence_drv.fallback_timer, 270 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT); 271 } 272 273 /** 274 * amdgpu_fence_process - check for fence activity 275 * 276 * @ring: pointer to struct amdgpu_ring 277 * 278 * Checks the current fence value and calculates the last 279 * signalled fence value. Wakes the fence queue if the 280 * sequence number has increased. 281 * 282 * Returns true if fence was processed 283 */ 284 bool amdgpu_fence_process(struct amdgpu_ring *ring) 285 { 286 struct amdgpu_fence_driver *drv = &ring->fence_drv; 287 struct amdgpu_device *adev = ring->adev; 288 uint32_t seq, last_seq; 289 290 do { 291 last_seq = atomic_read(&ring->fence_drv.last_seq); 292 seq = amdgpu_fence_read(ring); 293 294 } while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq); 295 296 if (del_timer(&ring->fence_drv.fallback_timer) && 297 seq != ring->fence_drv.sync_seq) 298 amdgpu_fence_schedule_fallback(ring); 299 300 if (unlikely(seq == last_seq)) 301 return false; 302 303 last_seq &= drv->num_fences_mask; 304 seq &= drv->num_fences_mask; 305 306 do { 307 struct dma_fence *fence, **ptr; 308 309 ++last_seq; 310 last_seq &= drv->num_fences_mask; 311 ptr = &drv->fences[last_seq]; 312 313 /* There is always exactly one thread signaling this fence slot */ 314 fence = rcu_dereference_protected(*ptr, 1); 315 RCU_INIT_POINTER(*ptr, NULL); 316 317 if (!fence) 318 continue; 319 320 dma_fence_signal(fence); 321 dma_fence_put(fence); 322 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev); 323 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev); 324 } while (last_seq != seq); 325 326 return true; 327 } 328 329 /** 330 * amdgpu_fence_fallback - fallback for hardware interrupts 331 * 332 * @t: timer context used to obtain the pointer to ring structure 333 * 334 * Checks for fence activity. 335 */ 336 static void amdgpu_fence_fallback(void *arg) 337 { 338 struct amdgpu_ring *ring = arg; 339 340 if (amdgpu_fence_process(ring)) 341 DRM_WARN("Fence fallback timer expired on ring %s\n", ring->name); 342 } 343 344 /** 345 * amdgpu_fence_wait_empty - wait for all fences to signal 346 * 347 * @ring: ring index the fence is associated with 348 * 349 * Wait for all fences on the requested ring to signal (all asics). 350 * Returns 0 if the fences have passed, error for all other cases. 351 */ 352 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring) 353 { 354 uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq); 355 struct dma_fence *fence, **ptr; 356 int r; 357 358 if (!seq) 359 return 0; 360 361 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask]; 362 rcu_read_lock(); 363 fence = rcu_dereference(*ptr); 364 if (!fence || !dma_fence_get_rcu(fence)) { 365 rcu_read_unlock(); 366 return 0; 367 } 368 rcu_read_unlock(); 369 370 r = dma_fence_wait(fence, false); 371 dma_fence_put(fence); 372 return r; 373 } 374 375 /** 376 * amdgpu_fence_wait_polling - busy wait for givn sequence number 377 * 378 * @ring: ring index the fence is associated with 379 * @wait_seq: sequence number to wait 380 * @timeout: the timeout for waiting in usecs 381 * 382 * Wait for all fences on the requested ring to signal (all asics). 383 * Returns left time if no timeout, 0 or minus if timeout. 384 */ 385 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring, 386 uint32_t wait_seq, 387 signed long timeout) 388 { 389 uint32_t seq; 390 391 do { 392 seq = amdgpu_fence_read(ring); 393 udelay(5); 394 timeout -= 5; 395 } while ((int32_t)(wait_seq - seq) > 0 && timeout > 0); 396 397 return timeout > 0 ? timeout : 0; 398 } 399 /** 400 * amdgpu_fence_count_emitted - get the count of emitted fences 401 * 402 * @ring: ring the fence is associated with 403 * 404 * Get the number of fences emitted on the requested ring (all asics). 405 * Returns the number of emitted fences on the ring. Used by the 406 * dynpm code to ring track activity. 407 */ 408 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring) 409 { 410 uint64_t emitted; 411 412 /* We are not protected by ring lock when reading the last sequence 413 * but it's ok to report slightly wrong fence count here. 414 */ 415 emitted = 0x100000000ull; 416 emitted -= atomic_read(&ring->fence_drv.last_seq); 417 emitted += READ_ONCE(ring->fence_drv.sync_seq); 418 return lower_32_bits(emitted); 419 } 420 421 /** 422 * amdgpu_fence_driver_start_ring - make the fence driver 423 * ready for use on the requested ring. 424 * 425 * @ring: ring to start the fence driver on 426 * @irq_src: interrupt source to use for this ring 427 * @irq_type: interrupt type to use for this ring 428 * 429 * Make the fence driver ready for processing (all asics). 430 * Not all asics have all rings, so each asic will only 431 * start the fence driver on the rings it has. 432 * Returns 0 for success, errors for failure. 433 */ 434 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring, 435 struct amdgpu_irq_src *irq_src, 436 unsigned irq_type) 437 { 438 struct amdgpu_device *adev = ring->adev; 439 uint64_t index; 440 441 if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) { 442 ring->fence_drv.cpu_addr = ring->fence_cpu_addr; 443 ring->fence_drv.gpu_addr = ring->fence_gpu_addr; 444 } else { 445 /* put fence directly behind firmware */ 446 index = roundup2(adev->uvd.fw->size, 8); 447 ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index; 448 ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index; 449 } 450 amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq)); 451 452 ring->fence_drv.irq_src = irq_src; 453 ring->fence_drv.irq_type = irq_type; 454 ring->fence_drv.initialized = true; 455 456 DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr 0x%016llx\n", 457 ring->name, ring->fence_drv.gpu_addr); 458 return 0; 459 } 460 461 /** 462 * amdgpu_fence_driver_init_ring - init the fence driver 463 * for the requested ring. 464 * 465 * @ring: ring to init the fence driver on 466 * 467 * Init the fence driver for the requested ring (all asics). 468 * Helper function for amdgpu_fence_driver_init(). 469 */ 470 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring) 471 { 472 struct amdgpu_device *adev = ring->adev; 473 474 if (!adev) 475 return -EINVAL; 476 477 if (!is_power_of_2(ring->num_hw_submission)) 478 return -EINVAL; 479 480 ring->fence_drv.cpu_addr = NULL; 481 ring->fence_drv.gpu_addr = 0; 482 ring->fence_drv.sync_seq = 0; 483 atomic_set(&ring->fence_drv.last_seq, 0); 484 ring->fence_drv.initialized = false; 485 486 #ifdef __linux__ 487 timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0); 488 #else 489 timeout_set(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 490 ring); 491 #endif 492 493 ring->fence_drv.num_fences_mask = ring->num_hw_submission * 2 - 1; 494 mtx_init(&ring->fence_drv.lock, IPL_TTY); 495 ring->fence_drv.fences = kcalloc(ring->num_hw_submission * 2, sizeof(void *), 496 GFP_KERNEL); 497 498 if (!ring->fence_drv.fences) 499 return -ENOMEM; 500 501 return 0; 502 } 503 504 /** 505 * amdgpu_fence_driver_sw_init - init the fence driver 506 * for all possible rings. 507 * 508 * @adev: amdgpu device pointer 509 * 510 * Init the fence driver for all possible rings (all asics). 511 * Not all asics have all rings, so each asic will only 512 * start the fence driver on the rings it has using 513 * amdgpu_fence_driver_start_ring(). 514 * Returns 0 for success. 515 */ 516 int amdgpu_fence_driver_sw_init(struct amdgpu_device *adev) 517 { 518 return 0; 519 } 520 521 /** 522 * amdgpu_fence_driver_hw_fini - tear down the fence driver 523 * for all possible rings. 524 * 525 * @adev: amdgpu device pointer 526 * 527 * Tear down the fence driver for all possible rings (all asics). 528 */ 529 void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev) 530 { 531 int i, r; 532 533 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 534 struct amdgpu_ring *ring = adev->rings[i]; 535 536 if (!ring || !ring->fence_drv.initialized) 537 continue; 538 539 /* You can't wait for HW to signal if it's gone */ 540 if (!drm_dev_is_unplugged(adev_to_drm(adev))) 541 r = amdgpu_fence_wait_empty(ring); 542 else 543 r = -ENODEV; 544 /* no need to trigger GPU reset as we are unloading */ 545 if (r) 546 amdgpu_fence_driver_force_completion(ring); 547 548 if (ring->fence_drv.irq_src) 549 amdgpu_irq_put(adev, ring->fence_drv.irq_src, 550 ring->fence_drv.irq_type); 551 552 del_timer_sync(&ring->fence_drv.fallback_timer); 553 } 554 } 555 556 /* Will either stop and flush handlers for amdgpu interrupt or reanble it */ 557 void amdgpu_fence_driver_isr_toggle(struct amdgpu_device *adev, bool stop) 558 { 559 STUB(); 560 #ifdef notyet 561 int i; 562 563 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 564 struct amdgpu_ring *ring = adev->rings[i]; 565 566 if (!ring || !ring->fence_drv.initialized || !ring->fence_drv.irq_src) 567 continue; 568 569 if (stop) 570 disable_irq(adev->irq.irq); 571 else 572 enable_irq(adev->irq.irq); 573 } 574 #endif 575 } 576 577 void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev) 578 { 579 unsigned int i, j; 580 581 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 582 struct amdgpu_ring *ring = adev->rings[i]; 583 584 if (!ring || !ring->fence_drv.initialized) 585 continue; 586 587 if (!ring->no_scheduler) 588 drm_sched_fini(&ring->sched); 589 590 for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j) 591 dma_fence_put(ring->fence_drv.fences[j]); 592 kfree(ring->fence_drv.fences); 593 ring->fence_drv.fences = NULL; 594 ring->fence_drv.initialized = false; 595 } 596 } 597 598 /** 599 * amdgpu_fence_driver_hw_init - enable the fence driver 600 * for all possible rings. 601 * 602 * @adev: amdgpu device pointer 603 * 604 * Enable the fence driver for all possible rings (all asics). 605 * Not all asics have all rings, so each asic will only 606 * start the fence driver on the rings it has using 607 * amdgpu_fence_driver_start_ring(). 608 * Returns 0 for success. 609 */ 610 void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev) 611 { 612 int i; 613 614 for (i = 0; i < AMDGPU_MAX_RINGS; i++) { 615 struct amdgpu_ring *ring = adev->rings[i]; 616 if (!ring || !ring->fence_drv.initialized) 617 continue; 618 619 /* enable the interrupt */ 620 if (ring->fence_drv.irq_src) 621 amdgpu_irq_get(adev, ring->fence_drv.irq_src, 622 ring->fence_drv.irq_type); 623 } 624 } 625 626 /** 627 * amdgpu_fence_driver_clear_job_fences - clear job embedded fences of ring 628 * 629 * @ring: fence of the ring to be cleared 630 * 631 */ 632 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring) 633 { 634 int i; 635 struct dma_fence *old, **ptr; 636 637 for (i = 0; i <= ring->fence_drv.num_fences_mask; i++) { 638 ptr = &ring->fence_drv.fences[i]; 639 old = rcu_dereference_protected(*ptr, 1); 640 if (old && old->ops == &amdgpu_job_fence_ops) { 641 RCU_INIT_POINTER(*ptr, NULL); 642 dma_fence_put(old); 643 } 644 } 645 } 646 647 /** 648 * amdgpu_fence_driver_force_completion - force signal latest fence of ring 649 * 650 * @ring: fence of the ring to signal 651 * 652 */ 653 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring) 654 { 655 amdgpu_fence_write(ring, ring->fence_drv.sync_seq); 656 amdgpu_fence_process(ring); 657 } 658 659 /* 660 * Common fence implementation 661 */ 662 663 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence) 664 { 665 return "amdgpu"; 666 } 667 668 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f) 669 { 670 return (const char *)to_amdgpu_fence(f)->ring->name; 671 } 672 673 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f) 674 { 675 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence); 676 677 return (const char *)to_amdgpu_ring(job->base.sched)->name; 678 } 679 680 /** 681 * amdgpu_fence_enable_signaling - enable signalling on fence 682 * @f: fence 683 * 684 * This function is called with fence_queue lock held, and adds a callback 685 * to fence_queue that checks if this fence is signaled, and if so it 686 * signals the fence and removes itself. 687 */ 688 static bool amdgpu_fence_enable_signaling(struct dma_fence *f) 689 { 690 if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer)) 691 amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring); 692 693 return true; 694 } 695 696 /** 697 * amdgpu_job_fence_enable_signaling - enable signalling on job fence 698 * @f: fence 699 * 700 * This is the simliar function with amdgpu_fence_enable_signaling above, it 701 * only handles the job embedded fence. 702 */ 703 static bool amdgpu_job_fence_enable_signaling(struct dma_fence *f) 704 { 705 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence); 706 707 if (!timer_pending(&to_amdgpu_ring(job->base.sched)->fence_drv.fallback_timer)) 708 amdgpu_fence_schedule_fallback(to_amdgpu_ring(job->base.sched)); 709 710 return true; 711 } 712 713 /** 714 * amdgpu_fence_free - free up the fence memory 715 * 716 * @rcu: RCU callback head 717 * 718 * Free up the fence memory after the RCU grace period. 719 */ 720 static void amdgpu_fence_free(struct rcu_head *rcu) 721 { 722 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu); 723 724 /* free fence_slab if it's separated fence*/ 725 #ifdef __linux__ 726 kmem_cache_free(amdgpu_fence_slab, to_amdgpu_fence(f)); 727 #else 728 pool_put(&amdgpu_fence_slab, to_amdgpu_fence(f)); 729 #endif 730 } 731 732 /** 733 * amdgpu_job_fence_free - free up the job with embedded fence 734 * 735 * @rcu: RCU callback head 736 * 737 * Free up the job with embedded fence after the RCU grace period. 738 */ 739 static void amdgpu_job_fence_free(struct rcu_head *rcu) 740 { 741 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu); 742 743 /* free job if fence has a parent job */ 744 kfree(container_of(f, struct amdgpu_job, hw_fence)); 745 } 746 747 /** 748 * amdgpu_fence_release - callback that fence can be freed 749 * 750 * @f: fence 751 * 752 * This function is called when the reference count becomes zero. 753 * It just RCU schedules freeing up the fence. 754 */ 755 static void amdgpu_fence_release(struct dma_fence *f) 756 { 757 call_rcu(&f->rcu, amdgpu_fence_free); 758 } 759 760 /** 761 * amdgpu_job_fence_release - callback that job embedded fence can be freed 762 * 763 * @f: fence 764 * 765 * This is the simliar function with amdgpu_fence_release above, it 766 * only handles the job embedded fence. 767 */ 768 static void amdgpu_job_fence_release(struct dma_fence *f) 769 { 770 call_rcu(&f->rcu, amdgpu_job_fence_free); 771 } 772 773 static const struct dma_fence_ops amdgpu_fence_ops = { 774 .get_driver_name = amdgpu_fence_get_driver_name, 775 .get_timeline_name = amdgpu_fence_get_timeline_name, 776 .enable_signaling = amdgpu_fence_enable_signaling, 777 .release = amdgpu_fence_release, 778 }; 779 780 static const struct dma_fence_ops amdgpu_job_fence_ops = { 781 .get_driver_name = amdgpu_fence_get_driver_name, 782 .get_timeline_name = amdgpu_job_fence_get_timeline_name, 783 .enable_signaling = amdgpu_job_fence_enable_signaling, 784 .release = amdgpu_job_fence_release, 785 }; 786 787 /* 788 * Fence debugfs 789 */ 790 #if defined(CONFIG_DEBUG_FS) 791 static int amdgpu_debugfs_fence_info_show(struct seq_file *m, void *unused) 792 { 793 struct amdgpu_device *adev = (struct amdgpu_device *)m->private; 794 int i; 795 796 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 797 struct amdgpu_ring *ring = adev->rings[i]; 798 if (!ring || !ring->fence_drv.initialized) 799 continue; 800 801 amdgpu_fence_process(ring); 802 803 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name); 804 seq_printf(m, "Last signaled fence 0x%08x\n", 805 atomic_read(&ring->fence_drv.last_seq)); 806 seq_printf(m, "Last emitted 0x%08x\n", 807 ring->fence_drv.sync_seq); 808 809 if (ring->funcs->type == AMDGPU_RING_TYPE_GFX || 810 ring->funcs->type == AMDGPU_RING_TYPE_SDMA) { 811 seq_printf(m, "Last signaled trailing fence 0x%08x\n", 812 le32_to_cpu(*ring->trail_fence_cpu_addr)); 813 seq_printf(m, "Last emitted 0x%08x\n", 814 ring->trail_seq); 815 } 816 817 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX) 818 continue; 819 820 /* set in CP_VMID_PREEMPT and preemption occurred */ 821 seq_printf(m, "Last preempted 0x%08x\n", 822 le32_to_cpu(*(ring->fence_drv.cpu_addr + 2))); 823 /* set in CP_VMID_RESET and reset occurred */ 824 seq_printf(m, "Last reset 0x%08x\n", 825 le32_to_cpu(*(ring->fence_drv.cpu_addr + 4))); 826 /* Both preemption and reset occurred */ 827 seq_printf(m, "Last both 0x%08x\n", 828 le32_to_cpu(*(ring->fence_drv.cpu_addr + 6))); 829 } 830 return 0; 831 } 832 833 /* 834 * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover 835 * 836 * Manually trigger a gpu reset at the next fence wait. 837 */ 838 static int gpu_recover_get(void *data, u64 *val) 839 { 840 struct amdgpu_device *adev = (struct amdgpu_device *)data; 841 struct drm_device *dev = adev_to_drm(adev); 842 int r; 843 844 r = pm_runtime_get_sync(dev->dev); 845 if (r < 0) { 846 pm_runtime_put_autosuspend(dev->dev); 847 return 0; 848 } 849 850 if (amdgpu_reset_domain_schedule(adev->reset_domain, &adev->reset_work)) 851 flush_work(&adev->reset_work); 852 853 *val = atomic_read(&adev->reset_domain->reset_res); 854 855 pm_runtime_mark_last_busy(dev->dev); 856 pm_runtime_put_autosuspend(dev->dev); 857 858 return 0; 859 } 860 861 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_fence_info); 862 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_gpu_recover_fops, gpu_recover_get, NULL, 863 "%lld\n"); 864 865 static void amdgpu_debugfs_reset_work(struct work_struct *work) 866 { 867 struct amdgpu_device *adev = container_of(work, struct amdgpu_device, 868 reset_work); 869 870 struct amdgpu_reset_context reset_context; 871 memset(&reset_context, 0, sizeof(reset_context)); 872 873 reset_context.method = AMD_RESET_METHOD_NONE; 874 reset_context.reset_req_dev = adev; 875 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 876 877 amdgpu_device_gpu_recover(adev, NULL, &reset_context); 878 } 879 880 #endif 881 882 void amdgpu_debugfs_fence_init(struct amdgpu_device *adev) 883 { 884 #if defined(CONFIG_DEBUG_FS) 885 struct drm_minor *minor = adev_to_drm(adev)->primary; 886 struct dentry *root = minor->debugfs_root; 887 888 debugfs_create_file("amdgpu_fence_info", 0444, root, adev, 889 &amdgpu_debugfs_fence_info_fops); 890 891 if (!amdgpu_sriov_vf(adev)) { 892 893 INIT_WORK(&adev->reset_work, amdgpu_debugfs_reset_work); 894 debugfs_create_file("amdgpu_gpu_recover", 0444, root, adev, 895 &amdgpu_debugfs_gpu_recover_fops); 896 } 897 #endif 898 } 899 900