2 * Copyright 2009 Jerome Glisse.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
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>
39 #include <drm/drm_drv.h>
41 #include "amdgpu_trace.h"
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 the relevant GPU caches have been flushed.
53 struct dma_fence base;
56 struct amdgpu_ring *ring;
59 static struct kmem_cache *amdgpu_fence_slab;
61 int amdgpu_fence_slab_init(void)
63 amdgpu_fence_slab = kmem_cache_create(
64 "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
65 SLAB_HWCACHE_ALIGN, NULL);
66 if (!amdgpu_fence_slab)
71 void amdgpu_fence_slab_fini(void)
74 kmem_cache_destroy(amdgpu_fence_slab);
79 static const struct dma_fence_ops amdgpu_fence_ops;
80 static const struct dma_fence_ops amdgpu_job_fence_ops;
81 static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)
83 struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
85 if (__f->base.ops == &amdgpu_fence_ops ||
86 __f->base.ops == &amdgpu_job_fence_ops)
93 * amdgpu_fence_write - write a fence value
95 * @ring: ring the fence is associated with
96 * @seq: sequence number to write
98 * Writes a fence value to memory (all asics).
100 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
102 struct amdgpu_fence_driver *drv = &ring->fence_drv;
105 *drv->cpu_addr = cpu_to_le32(seq);
109 * amdgpu_fence_read - read a fence value
111 * @ring: ring the fence is associated with
113 * Reads a fence value from memory (all asics).
114 * Returns the value of the fence read from memory.
116 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
118 struct amdgpu_fence_driver *drv = &ring->fence_drv;
122 seq = le32_to_cpu(*drv->cpu_addr);
124 seq = atomic_read(&drv->last_seq);
130 * amdgpu_fence_emit - emit a fence on the requested ring
132 * @ring: ring the fence is associated with
133 * @f: resulting fence object
134 * @job: job the fence is embedded in
135 * @flags: flags to pass into the subordinate .emit_fence() call
137 * Emits a fence command on the requested ring (all asics).
138 * Returns 0 on success, -ENOMEM on failure.
140 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f, struct amdgpu_job *job,
143 struct amdgpu_device *adev = ring->adev;
144 struct dma_fence *fence;
145 struct amdgpu_fence *am_fence;
146 struct dma_fence __rcu **ptr;
151 /* create a sperate hw fence */
152 am_fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_ATOMIC);
153 if (am_fence == NULL)
155 fence = &am_fence->base;
156 am_fence->ring = ring;
158 /* take use of job-embedded fence */
159 fence = &job->hw_fence;
162 seq = ++ring->fence_drv.sync_seq;
163 if (job && job->job_run_counter) {
164 /* reinit seq for resubmitted jobs */
168 dma_fence_init(fence, &amdgpu_job_fence_ops,
169 &ring->fence_drv.lock,
170 adev->fence_context + ring->idx, seq);
172 dma_fence_init(fence, &amdgpu_fence_ops,
173 &ring->fence_drv.lock,
174 adev->fence_context + ring->idx, seq);
177 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
178 seq, flags | AMDGPU_FENCE_FLAG_INT);
179 pm_runtime_get_noresume(adev_to_drm(adev)->dev);
180 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
181 if (unlikely(rcu_dereference_protected(*ptr, 1))) {
182 struct dma_fence *old;
185 old = dma_fence_get_rcu_safe(ptr);
189 r = dma_fence_wait(old, false);
196 /* This function can't be called concurrently anyway, otherwise
197 * emitting the fence would mess up the hardware ring buffer.
199 rcu_assign_pointer(*ptr, dma_fence_get(fence));
207 * amdgpu_fence_emit_polling - emit a fence on the requeste ring
209 * @ring: ring the fence is associated with
210 * @s: resulting sequence number
211 * @timeout: the timeout for waiting in usecs
213 * Emits a fence command on the requested ring (all asics).
214 * Used For polling fence.
215 * Returns 0 on success, -ENOMEM on failure.
217 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s,
226 seq = ++ring->fence_drv.sync_seq;
227 r = amdgpu_fence_wait_polling(ring,
228 seq - ring->fence_drv.num_fences_mask,
233 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
242 * amdgpu_fence_schedule_fallback - schedule fallback check
244 * @ring: pointer to struct amdgpu_ring
246 * Start a timer as fallback to our interrupts.
248 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
250 mod_timer(&ring->fence_drv.fallback_timer,
251 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
255 * amdgpu_fence_process - check for fence activity
257 * @ring: pointer to struct amdgpu_ring
259 * Checks the current fence value and calculates the last
260 * signalled fence value. Wakes the fence queue if the
261 * sequence number has increased.
263 * Returns true if fence was processed
265 bool amdgpu_fence_process(struct amdgpu_ring *ring)
267 struct amdgpu_fence_driver *drv = &ring->fence_drv;
268 struct amdgpu_device *adev = ring->adev;
269 uint32_t seq, last_seq;
272 last_seq = atomic_read(&ring->fence_drv.last_seq);
273 seq = amdgpu_fence_read(ring);
275 } while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq);
277 if (del_timer(&ring->fence_drv.fallback_timer) &&
278 seq != ring->fence_drv.sync_seq)
279 amdgpu_fence_schedule_fallback(ring);
281 if (unlikely(seq == last_seq))
284 last_seq &= drv->num_fences_mask;
285 seq &= drv->num_fences_mask;
288 struct dma_fence *fence, **ptr;
291 last_seq &= drv->num_fences_mask;
292 ptr = &drv->fences[last_seq];
294 /* There is always exactly one thread signaling this fence slot */
295 fence = rcu_dereference_protected(*ptr, 1);
296 RCU_INIT_POINTER(*ptr, NULL);
301 dma_fence_signal(fence);
302 dma_fence_put(fence);
303 pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
304 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
305 } while (last_seq != seq);
311 * amdgpu_fence_fallback - fallback for hardware interrupts
313 * @t: timer context used to obtain the pointer to ring structure
315 * Checks for fence activity.
317 static void amdgpu_fence_fallback(struct timer_list *t)
319 struct amdgpu_ring *ring = from_timer(ring, t,
320 fence_drv.fallback_timer);
322 if (amdgpu_fence_process(ring))
323 DRM_WARN("Fence fallback timer expired on ring %s\n", ring->name);
327 * amdgpu_fence_wait_empty - wait for all fences to signal
329 * @ring: ring index the fence is associated with
331 * Wait for all fences on the requested ring to signal (all asics).
332 * Returns 0 if the fences have passed, error for all other cases.
334 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
336 uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq);
337 struct dma_fence *fence, **ptr;
343 ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
345 fence = rcu_dereference(*ptr);
346 if (!fence || !dma_fence_get_rcu(fence)) {
352 r = dma_fence_wait(fence, false);
353 dma_fence_put(fence);
358 * amdgpu_fence_wait_polling - busy wait for givn sequence number
360 * @ring: ring index the fence is associated with
361 * @wait_seq: sequence number to wait
362 * @timeout: the timeout for waiting in usecs
364 * Wait for all fences on the requested ring to signal (all asics).
365 * Returns left time if no timeout, 0 or minus if timeout.
367 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
374 seq = amdgpu_fence_read(ring);
377 } while ((int32_t)(wait_seq - seq) > 0 && timeout > 0);
379 return timeout > 0 ? timeout : 0;
382 * amdgpu_fence_count_emitted - get the count of emitted fences
384 * @ring: ring the fence is associated with
386 * Get the number of fences emitted on the requested ring (all asics).
387 * Returns the number of emitted fences on the ring. Used by the
388 * dynpm code to ring track activity.
390 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
394 /* We are not protected by ring lock when reading the last sequence
395 * but it's ok to report slightly wrong fence count here.
397 amdgpu_fence_process(ring);
398 emitted = 0x100000000ull;
399 emitted -= atomic_read(&ring->fence_drv.last_seq);
400 emitted += READ_ONCE(ring->fence_drv.sync_seq);
401 return lower_32_bits(emitted);
405 * amdgpu_fence_driver_start_ring - make the fence driver
406 * ready for use on the requested ring.
408 * @ring: ring to start the fence driver on
409 * @irq_src: interrupt source to use for this ring
410 * @irq_type: interrupt type to use for this ring
412 * Make the fence driver ready for processing (all asics).
413 * Not all asics have all rings, so each asic will only
414 * start the fence driver on the rings it has.
415 * Returns 0 for success, errors for failure.
417 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
418 struct amdgpu_irq_src *irq_src,
421 struct amdgpu_device *adev = ring->adev;
424 if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) {
425 ring->fence_drv.cpu_addr = ring->fence_cpu_addr;
426 ring->fence_drv.gpu_addr = ring->fence_gpu_addr;
428 /* put fence directly behind firmware */
429 index = ALIGN(adev->uvd.fw->size, 8);
430 ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index;
431 ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index;
433 amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq));
435 ring->fence_drv.irq_src = irq_src;
436 ring->fence_drv.irq_type = irq_type;
437 ring->fence_drv.initialized = true;
439 DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr 0x%016llx\n",
440 ring->name, ring->fence_drv.gpu_addr);
445 * amdgpu_fence_driver_init_ring - init the fence driver
446 * for the requested ring.
448 * @ring: ring to init the fence driver on
450 * Init the fence driver for the requested ring (all asics).
451 * Helper function for amdgpu_fence_driver_init().
453 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
455 struct amdgpu_device *adev = ring->adev;
460 if (!is_power_of_2(ring->num_hw_submission))
463 ring->fence_drv.cpu_addr = NULL;
464 ring->fence_drv.gpu_addr = 0;
465 ring->fence_drv.sync_seq = 0;
466 atomic_set(&ring->fence_drv.last_seq, 0);
467 ring->fence_drv.initialized = false;
469 timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
471 ring->fence_drv.num_fences_mask = ring->num_hw_submission * 2 - 1;
472 spin_lock_init(&ring->fence_drv.lock);
473 ring->fence_drv.fences = kcalloc(ring->num_hw_submission * 2, sizeof(void *),
476 if (!ring->fence_drv.fences)
483 * amdgpu_fence_driver_sw_init - init the fence driver
484 * for all possible rings.
486 * @adev: amdgpu device pointer
488 * Init the fence driver for all possible rings (all asics).
489 * Not all asics have all rings, so each asic will only
490 * start the fence driver on the rings it has using
491 * amdgpu_fence_driver_start_ring().
492 * Returns 0 for success.
494 int amdgpu_fence_driver_sw_init(struct amdgpu_device *adev)
500 * amdgpu_fence_driver_hw_fini - tear down the fence driver
501 * for all possible rings.
503 * @adev: amdgpu device pointer
505 * Tear down the fence driver for all possible rings (all asics).
507 void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev)
511 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
512 struct amdgpu_ring *ring = adev->rings[i];
514 if (!ring || !ring->fence_drv.initialized)
517 /* You can't wait for HW to signal if it's gone */
518 if (!drm_dev_is_unplugged(adev_to_drm(adev)))
519 r = amdgpu_fence_wait_empty(ring);
522 /* no need to trigger GPU reset as we are unloading */
524 amdgpu_fence_driver_force_completion(ring);
526 if (ring->fence_drv.irq_src)
527 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
528 ring->fence_drv.irq_type);
530 del_timer_sync(&ring->fence_drv.fallback_timer);
534 void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev)
538 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
539 struct amdgpu_ring *ring = adev->rings[i];
541 if (!ring || !ring->fence_drv.initialized)
544 if (!ring->no_scheduler)
545 drm_sched_fini(&ring->sched);
547 for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
548 dma_fence_put(ring->fence_drv.fences[j]);
549 kfree(ring->fence_drv.fences);
550 ring->fence_drv.fences = NULL;
551 ring->fence_drv.initialized = false;
556 * amdgpu_fence_driver_hw_init - enable the fence driver
557 * for all possible rings.
559 * @adev: amdgpu device pointer
561 * Enable the fence driver for all possible rings (all asics).
562 * Not all asics have all rings, so each asic will only
563 * start the fence driver on the rings it has using
564 * amdgpu_fence_driver_start_ring().
565 * Returns 0 for success.
567 void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev)
571 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
572 struct amdgpu_ring *ring = adev->rings[i];
573 if (!ring || !ring->fence_drv.initialized)
576 /* enable the interrupt */
577 if (ring->fence_drv.irq_src)
578 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
579 ring->fence_drv.irq_type);
584 * amdgpu_fence_driver_clear_job_fences - clear job embedded fences of ring
586 * @ring: fence of the ring to be cleared
589 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring)
592 struct dma_fence *old, **ptr;
594 for (i = 0; i <= ring->fence_drv.num_fences_mask; i++) {
595 ptr = &ring->fence_drv.fences[i];
596 old = rcu_dereference_protected(*ptr, 1);
597 if (old && old->ops == &amdgpu_job_fence_ops)
598 RCU_INIT_POINTER(*ptr, NULL);
603 * amdgpu_fence_driver_force_completion - force signal latest fence of ring
605 * @ring: fence of the ring to signal
608 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
610 amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
611 amdgpu_fence_process(ring);
615 * Common fence implementation
618 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
623 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
625 return (const char *)to_amdgpu_fence(f)->ring->name;
628 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
630 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
632 return (const char *)to_amdgpu_ring(job->base.sched)->name;
636 * amdgpu_fence_enable_signaling - enable signalling on fence
639 * This function is called with fence_queue lock held, and adds a callback
640 * to fence_queue that checks if this fence is signaled, and if so it
641 * signals the fence and removes itself.
643 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
645 if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
646 amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
652 * amdgpu_job_fence_enable_signaling - enable signalling on job fence
655 * This is the simliar function with amdgpu_fence_enable_signaling above, it
656 * only handles the job embedded fence.
658 static bool amdgpu_job_fence_enable_signaling(struct dma_fence *f)
660 struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
662 if (!timer_pending(&to_amdgpu_ring(job->base.sched)->fence_drv.fallback_timer))
663 amdgpu_fence_schedule_fallback(to_amdgpu_ring(job->base.sched));
669 * amdgpu_fence_free - free up the fence memory
671 * @rcu: RCU callback head
673 * Free up the fence memory after the RCU grace period.
675 static void amdgpu_fence_free(struct rcu_head *rcu)
677 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
679 /* free fence_slab if it's separated fence*/
680 kmem_cache_free(amdgpu_fence_slab, to_amdgpu_fence(f));
684 * amdgpu_job_fence_free - free up the job with embedded fence
686 * @rcu: RCU callback head
688 * Free up the job with embedded fence after the RCU grace period.
690 static void amdgpu_job_fence_free(struct rcu_head *rcu)
692 struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
694 /* free job if fence has a parent job */
695 kfree(container_of(f, struct amdgpu_job, hw_fence));
699 * amdgpu_fence_release - callback that fence can be freed
703 * This function is called when the reference count becomes zero.
704 * It just RCU schedules freeing up the fence.
706 static void amdgpu_fence_release(struct dma_fence *f)
708 call_rcu(&f->rcu, amdgpu_fence_free);
712 * amdgpu_job_fence_release - callback that job embedded fence can be freed
716 * This is the simliar function with amdgpu_fence_release above, it
717 * only handles the job embedded fence.
719 static void amdgpu_job_fence_release(struct dma_fence *f)
721 call_rcu(&f->rcu, amdgpu_job_fence_free);
724 static const struct dma_fence_ops amdgpu_fence_ops = {
725 .get_driver_name = amdgpu_fence_get_driver_name,
726 .get_timeline_name = amdgpu_fence_get_timeline_name,
727 .enable_signaling = amdgpu_fence_enable_signaling,
728 .release = amdgpu_fence_release,
731 static const struct dma_fence_ops amdgpu_job_fence_ops = {
732 .get_driver_name = amdgpu_fence_get_driver_name,
733 .get_timeline_name = amdgpu_job_fence_get_timeline_name,
734 .enable_signaling = amdgpu_job_fence_enable_signaling,
735 .release = amdgpu_job_fence_release,
741 #if defined(CONFIG_DEBUG_FS)
742 static int amdgpu_debugfs_fence_info_show(struct seq_file *m, void *unused)
744 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
747 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
748 struct amdgpu_ring *ring = adev->rings[i];
749 if (!ring || !ring->fence_drv.initialized)
752 amdgpu_fence_process(ring);
754 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
755 seq_printf(m, "Last signaled fence 0x%08x\n",
756 atomic_read(&ring->fence_drv.last_seq));
757 seq_printf(m, "Last emitted 0x%08x\n",
758 ring->fence_drv.sync_seq);
760 if (ring->funcs->type == AMDGPU_RING_TYPE_GFX ||
761 ring->funcs->type == AMDGPU_RING_TYPE_SDMA) {
762 seq_printf(m, "Last signaled trailing fence 0x%08x\n",
763 le32_to_cpu(*ring->trail_fence_cpu_addr));
764 seq_printf(m, "Last emitted 0x%08x\n",
768 if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
771 /* set in CP_VMID_PREEMPT and preemption occurred */
772 seq_printf(m, "Last preempted 0x%08x\n",
773 le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
774 /* set in CP_VMID_RESET and reset occurred */
775 seq_printf(m, "Last reset 0x%08x\n",
776 le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
777 /* Both preemption and reset occurred */
778 seq_printf(m, "Last both 0x%08x\n",
779 le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
785 * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
787 * Manually trigger a gpu reset at the next fence wait.
789 static int gpu_recover_get(void *data, u64 *val)
791 struct amdgpu_device *adev = (struct amdgpu_device *)data;
792 struct drm_device *dev = adev_to_drm(adev);
795 r = pm_runtime_get_sync(dev->dev);
797 pm_runtime_put_autosuspend(dev->dev);
801 *val = amdgpu_device_gpu_recover(adev, NULL);
803 pm_runtime_mark_last_busy(dev->dev);
804 pm_runtime_put_autosuspend(dev->dev);
809 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_fence_info);
810 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_gpu_recover_fops, gpu_recover_get, NULL,
815 void amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
817 #if defined(CONFIG_DEBUG_FS)
818 struct drm_minor *minor = adev_to_drm(adev)->primary;
819 struct dentry *root = minor->debugfs_root;
821 debugfs_create_file("amdgpu_fence_info", 0444, root, adev,
822 &amdgpu_debugfs_fence_info_fops);
824 if (!amdgpu_sriov_vf(adev))
825 debugfs_create_file("amdgpu_gpu_recover", 0444, root, adev,
826 &amdgpu_debugfs_gpu_recover_fops);