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>
39 #include "amdgpu_trace.h"
43 * Fences mark an event in the GPUs pipeline and are used
44 * for GPU/CPU synchronization. When the fence is written,
45 * it is expected that all buffers associated with that fence
46 * are no longer in use by the associated ring on the GPU and
47 * that the the relevant GPU caches have been flushed.
51 * amdgpu_fence_write - write a fence value
53 * @ring: ring the fence is associated with
54 * @seq: sequence number to write
56 * Writes a fence value to memory (all asics).
58 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
60 struct amdgpu_fence_driver *drv = &ring->fence_drv;
63 *drv->cpu_addr = cpu_to_le32(seq);
67 * amdgpu_fence_read - read a fence value
69 * @ring: ring the fence is associated with
71 * Reads a fence value from memory (all asics).
72 * Returns the value of the fence read from memory.
74 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
76 struct amdgpu_fence_driver *drv = &ring->fence_drv;
80 seq = le32_to_cpu(*drv->cpu_addr);
82 seq = lower_32_bits(atomic64_read(&drv->last_seq));
88 * amdgpu_fence_schedule_check - schedule lockup check
90 * @ring: pointer to struct amdgpu_ring
92 * Queues a delayed work item to check for lockups.
94 static void amdgpu_fence_schedule_check(struct amdgpu_ring *ring)
97 * Do not reset the timer here with mod_delayed_work,
98 * this can livelock in an interaction with TTM delayed destroy.
100 queue_delayed_work(system_power_efficient_wq,
101 &ring->fence_drv.lockup_work,
102 AMDGPU_FENCE_JIFFIES_TIMEOUT);
106 * amdgpu_fence_emit - emit a fence on the requested ring
108 * @ring: ring the fence is associated with
109 * @owner: creator of the fence
110 * @fence: amdgpu fence object
112 * Emits a fence command on the requested ring (all asics).
113 * Returns 0 on success, -ENOMEM on failure.
115 int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner,
116 struct amdgpu_fence **fence)
118 struct amdgpu_device *adev = ring->adev;
120 /* we are protected by the ring emission mutex */
121 *fence = kmalloc(sizeof(struct amdgpu_fence), GFP_KERNEL);
122 if ((*fence) == NULL) {
125 (*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx];
126 (*fence)->ring = ring;
127 (*fence)->owner = owner;
128 fence_init(&(*fence)->base, &amdgpu_fence_ops,
129 &adev->fence_queue.lock, adev->fence_context + ring->idx,
131 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
133 AMDGPU_FENCE_FLAG_INT);
134 trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq);
139 * amdgpu_fence_check_signaled - callback from fence_queue
141 * this function is called with fence_queue lock held, which is also used
142 * for the fence locking itself, so unlocked variants are used for
143 * fence_signal, and remove_wait_queue.
145 static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
147 struct amdgpu_fence *fence;
148 struct amdgpu_device *adev;
152 fence = container_of(wait, struct amdgpu_fence, fence_wake);
153 adev = fence->ring->adev;
156 * We cannot use amdgpu_fence_process here because we're already
157 * in the waitqueue, in a call from wake_up_all.
159 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
160 if (seq >= fence->seq) {
161 ret = fence_signal_locked(&fence->base);
163 FENCE_TRACE(&fence->base, "signaled from irq context\n");
165 FENCE_TRACE(&fence->base, "was already signaled\n");
167 amdgpu_irq_put(adev, fence->ring->fence_drv.irq_src,
168 fence->ring->fence_drv.irq_type);
169 __remove_wait_queue(&adev->fence_queue, &fence->fence_wake);
170 fence_put(&fence->base);
172 FENCE_TRACE(&fence->base, "pending\n");
177 * amdgpu_fence_activity - check for fence activity
179 * @ring: pointer to struct amdgpu_ring
181 * Checks the current fence value and calculates the last
182 * signalled fence value. Returns true if activity occured
183 * on the ring, and the fence_queue should be waken up.
185 static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
187 uint64_t seq, last_seq, last_emitted;
188 unsigned count_loop = 0;
191 /* Note there is a scenario here for an infinite loop but it's
192 * very unlikely to happen. For it to happen, the current polling
193 * process need to be interrupted by another process and another
194 * process needs to update the last_seq btw the atomic read and
195 * xchg of the current process.
197 * More over for this to go in infinite loop there need to be
198 * continuously new fence signaled ie amdgpu_fence_read needs
199 * to return a different value each time for both the currently
200 * polling process and the other process that xchg the last_seq
201 * btw atomic read and xchg of the current process. And the
202 * value the other process set as last seq must be higher than
203 * the seq value we just read. Which means that current process
204 * need to be interrupted after amdgpu_fence_read and before
207 * To be even more safe we count the number of time we loop and
208 * we bail after 10 loop just accepting the fact that we might
209 * have temporarly set the last_seq not to the true real last
210 * seq but to an older one.
212 last_seq = atomic64_read(&ring->fence_drv.last_seq);
214 last_emitted = ring->fence_drv.sync_seq[ring->idx];
215 seq = amdgpu_fence_read(ring);
216 seq |= last_seq & 0xffffffff00000000LL;
217 if (seq < last_seq) {
219 seq |= last_emitted & 0xffffffff00000000LL;
222 if (seq <= last_seq || seq > last_emitted) {
225 /* If we loop over we don't want to return without
226 * checking if a fence is signaled as it means that the
227 * seq we just read is different from the previous on.
231 if ((count_loop++) > 10) {
232 /* We looped over too many time leave with the
233 * fact that we might have set an older fence
234 * seq then the current real last seq as signaled
239 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
241 if (seq < last_emitted)
242 amdgpu_fence_schedule_check(ring);
248 * amdgpu_fence_check_lockup - check for hardware lockup
250 * @work: delayed work item
252 * Checks for fence activity and if there is none probe
253 * the hardware if a lockup occured.
255 static void amdgpu_fence_check_lockup(struct work_struct *work)
257 struct amdgpu_fence_driver *fence_drv;
258 struct amdgpu_ring *ring;
260 fence_drv = container_of(work, struct amdgpu_fence_driver,
262 ring = fence_drv->ring;
264 if (!down_read_trylock(&ring->adev->exclusive_lock)) {
265 /* just reschedule the check if a reset is going on */
266 amdgpu_fence_schedule_check(ring);
270 if (fence_drv->delayed_irq && ring->adev->ddev->irq_enabled) {
271 fence_drv->delayed_irq = false;
272 amdgpu_irq_update(ring->adev, fence_drv->irq_src,
273 fence_drv->irq_type);
276 if (amdgpu_fence_activity(ring))
277 wake_up_all(&ring->adev->fence_queue);
278 else if (amdgpu_ring_is_lockup(ring)) {
279 /* good news we believe it's a lockup */
280 dev_warn(ring->adev->dev, "GPU lockup (current fence id "
281 "0x%016llx last fence id 0x%016llx on ring %d)\n",
282 (uint64_t)atomic64_read(&fence_drv->last_seq),
283 fence_drv->sync_seq[ring->idx], ring->idx);
285 /* remember that we need an reset */
286 ring->adev->needs_reset = true;
287 wake_up_all(&ring->adev->fence_queue);
289 up_read(&ring->adev->exclusive_lock);
293 * amdgpu_fence_process - process a fence
295 * @adev: amdgpu_device pointer
296 * @ring: ring index the fence is associated with
298 * Checks the current fence value and wakes the fence queue
299 * if the sequence number has increased (all asics).
301 void amdgpu_fence_process(struct amdgpu_ring *ring)
303 uint64_t seq, last_seq, last_emitted;
304 unsigned count_loop = 0;
307 /* Note there is a scenario here for an infinite loop but it's
308 * very unlikely to happen. For it to happen, the current polling
309 * process need to be interrupted by another process and another
310 * process needs to update the last_seq btw the atomic read and
311 * xchg of the current process.
313 * More over for this to go in infinite loop there need to be
314 * continuously new fence signaled ie amdgpu_fence_read needs
315 * to return a different value each time for both the currently
316 * polling process and the other process that xchg the last_seq
317 * btw atomic read and xchg of the current process. And the
318 * value the other process set as last seq must be higher than
319 * the seq value we just read. Which means that current process
320 * need to be interrupted after amdgpu_fence_read and before
323 * To be even more safe we count the number of time we loop and
324 * we bail after 10 loop just accepting the fact that we might
325 * have temporarly set the last_seq not to the true real last
326 * seq but to an older one.
328 last_seq = atomic64_read(&ring->fence_drv.last_seq);
330 last_emitted = ring->fence_drv.sync_seq[ring->idx];
331 seq = amdgpu_fence_read(ring);
332 seq |= last_seq & 0xffffffff00000000LL;
333 if (seq < last_seq) {
335 seq |= last_emitted & 0xffffffff00000000LL;
338 if (seq <= last_seq || seq > last_emitted) {
341 /* If we loop over we don't want to return without
342 * checking if a fence is signaled as it means that the
343 * seq we just read is different from the previous on.
347 if ((count_loop++) > 10) {
348 /* We looped over too many time leave with the
349 * fact that we might have set an older fence
350 * seq then the current real last seq as signaled
355 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
358 wake_up_all(&ring->adev->fence_queue);
362 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
364 * @ring: ring the fence is associated with
365 * @seq: sequence number
367 * Check if the last signaled fence sequnce number is >= the requested
368 * sequence number (all asics).
369 * Returns true if the fence has signaled (current fence value
370 * is >= requested value) or false if it has not (current fence
371 * value is < the requested value. Helper function for
372 * amdgpu_fence_signaled().
374 static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
376 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
379 /* poll new last sequence at least once */
380 amdgpu_fence_process(ring);
381 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
387 static bool amdgpu_fence_is_signaled(struct fence *f)
389 struct amdgpu_fence *fence = to_amdgpu_fence(f);
390 struct amdgpu_ring *ring = fence->ring;
391 struct amdgpu_device *adev = ring->adev;
393 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
396 if (down_read_trylock(&adev->exclusive_lock)) {
397 amdgpu_fence_process(ring);
398 up_read(&adev->exclusive_lock);
400 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
407 * amdgpu_fence_enable_signaling - enable signalling on fence
410 * This function is called with fence_queue lock held, and adds a callback
411 * to fence_queue that checks if this fence is signaled, and if so it
412 * signals the fence and removes itself.
414 static bool amdgpu_fence_enable_signaling(struct fence *f)
416 struct amdgpu_fence *fence = to_amdgpu_fence(f);
417 struct amdgpu_ring *ring = fence->ring;
418 struct amdgpu_device *adev = ring->adev;
420 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
423 if (down_read_trylock(&adev->exclusive_lock)) {
424 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
425 ring->fence_drv.irq_type);
426 if (amdgpu_fence_activity(ring))
427 wake_up_all_locked(&adev->fence_queue);
429 /* did fence get signaled after we enabled the sw irq? */
430 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) {
431 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
432 ring->fence_drv.irq_type);
433 up_read(&adev->exclusive_lock);
437 up_read(&adev->exclusive_lock);
439 /* we're probably in a lockup, lets not fiddle too much */
440 if (amdgpu_irq_get_delayed(adev, ring->fence_drv.irq_src,
441 ring->fence_drv.irq_type))
442 ring->fence_drv.delayed_irq = true;
443 amdgpu_fence_schedule_check(ring);
446 fence->fence_wake.flags = 0;
447 fence->fence_wake.private = NULL;
448 fence->fence_wake.func = amdgpu_fence_check_signaled;
449 __add_wait_queue(&adev->fence_queue, &fence->fence_wake);
451 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
456 * amdgpu_fence_signaled - check if a fence has signaled
458 * @fence: amdgpu fence object
460 * Check if the requested fence has signaled (all asics).
461 * Returns true if the fence has signaled or false if it has not.
463 bool amdgpu_fence_signaled(struct amdgpu_fence *fence)
468 if (amdgpu_fence_seq_signaled(fence->ring, fence->seq)) {
469 if (!fence_signal(&fence->base))
470 FENCE_TRACE(&fence->base, "signaled from amdgpu_fence_signaled\n");
478 * amdgpu_fence_any_seq_signaled - check if any sequence number is signaled
480 * @adev: amdgpu device pointer
481 * @seq: sequence numbers
483 * Check if the last signaled fence sequnce number is >= the requested
484 * sequence number (all asics).
485 * Returns true if any has signaled (current value is >= requested value)
486 * or false if it has not. Helper function for amdgpu_fence_wait_seq.
488 static bool amdgpu_fence_any_seq_signaled(struct amdgpu_device *adev, u64 *seq)
492 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
493 if (!adev->rings[i] || !seq[i])
496 if (amdgpu_fence_seq_signaled(adev->rings[i], seq[i]))
504 * amdgpu_fence_wait_seq_timeout - wait for a specific sequence numbers
506 * @adev: amdgpu device pointer
507 * @target_seq: sequence number(s) we want to wait for
508 * @intr: use interruptable sleep
509 * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
511 * Wait for the requested sequence number(s) to be written by any ring
512 * (all asics). Sequnce number array is indexed by ring id.
513 * @intr selects whether to use interruptable (true) or non-interruptable
514 * (false) sleep when waiting for the sequence number. Helper function
515 * for amdgpu_fence_wait_*().
516 * Returns remaining time if the sequence number has passed, 0 when
517 * the wait timeout, or an error for all other cases.
518 * -EDEADLK is returned when a GPU lockup has been detected.
520 long amdgpu_fence_wait_seq_timeout(struct amdgpu_device *adev, u64 *target_seq,
521 bool intr, long timeout)
523 uint64_t last_seq[AMDGPU_MAX_RINGS];
528 return amdgpu_fence_any_seq_signaled(adev, target_seq);
531 while (!amdgpu_fence_any_seq_signaled(adev, target_seq)) {
533 /* Save current sequence values, used to check for GPU lockups */
534 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
535 struct amdgpu_ring *ring = adev->rings[i];
537 if (!ring || !target_seq[i])
540 last_seq[i] = atomic64_read(&ring->fence_drv.last_seq);
541 trace_amdgpu_fence_wait_begin(adev->ddev, i, target_seq[i]);
542 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
543 ring->fence_drv.irq_type);
547 r = wait_event_interruptible_timeout(adev->fence_queue, (
548 (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
549 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
551 r = wait_event_timeout(adev->fence_queue, (
552 (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
553 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
556 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
557 struct amdgpu_ring *ring = adev->rings[i];
559 if (!ring || !target_seq[i])
562 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
563 ring->fence_drv.irq_type);
564 trace_amdgpu_fence_wait_end(adev->ddev, i, target_seq[i]);
570 if (unlikely(!signaled)) {
572 if (adev->needs_reset)
575 /* we were interrupted for some reason and fence
576 * isn't signaled yet, resume waiting */
580 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
581 struct amdgpu_ring *ring = adev->rings[i];
583 if (!ring || !target_seq[i])
586 if (last_seq[i] != atomic64_read(&ring->fence_drv.last_seq))
590 if (i != AMDGPU_MAX_RINGS)
593 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
594 if (!adev->rings[i] || !target_seq[i])
597 if (amdgpu_ring_is_lockup(adev->rings[i]))
601 if (i < AMDGPU_MAX_RINGS) {
602 /* good news we believe it's a lockup */
603 dev_warn(adev->dev, "GPU lockup (waiting for "
604 "0x%016llx last fence id 0x%016llx on"
606 target_seq[i], last_seq[i], i);
608 /* remember that we need an reset */
609 adev->needs_reset = true;
610 wake_up_all(&adev->fence_queue);
614 if (timeout < MAX_SCHEDULE_TIMEOUT) {
615 timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
626 * amdgpu_fence_wait - wait for a fence to signal
628 * @fence: amdgpu fence object
629 * @intr: use interruptable sleep
631 * Wait for the requested fence to signal (all asics).
632 * @intr selects whether to use interruptable (true) or non-interruptable
633 * (false) sleep when waiting for the fence.
634 * Returns 0 if the fence has passed, error for all other cases.
636 int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr)
638 uint64_t seq[AMDGPU_MAX_RINGS] = {};
641 seq[fence->ring->idx] = fence->seq;
642 r = amdgpu_fence_wait_seq_timeout(fence->ring->adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
647 r = fence_signal(&fence->base);
649 FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
654 * amdgpu_fence_wait_any - wait for a fence to signal on any ring
656 * @adev: amdgpu device pointer
657 * @fences: amdgpu fence object(s)
658 * @intr: use interruptable sleep
660 * Wait for any requested fence to signal (all asics). Fence
661 * array is indexed by ring id. @intr selects whether to use
662 * interruptable (true) or non-interruptable (false) sleep when
663 * waiting for the fences. Used by the suballocator.
664 * Returns 0 if any fence has passed, error for all other cases.
666 int amdgpu_fence_wait_any(struct amdgpu_device *adev,
667 struct amdgpu_fence **fences,
670 uint64_t seq[AMDGPU_MAX_RINGS];
671 unsigned i, num_rings = 0;
674 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
681 seq[i] = fences[i]->seq;
685 /* nothing to wait for ? */
689 r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
697 * amdgpu_fence_wait_next - wait for the next fence to signal
699 * @adev: amdgpu device pointer
700 * @ring: ring index the fence is associated with
702 * Wait for the next fence on the requested ring to signal (all asics).
703 * Returns 0 if the next fence has passed, error for all other cases.
704 * Caller must hold ring lock.
706 int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
708 uint64_t seq[AMDGPU_MAX_RINGS] = {};
711 seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
712 if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) {
713 /* nothing to wait for, last_seq is
714 already the last emited fence */
717 r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT);
724 * amdgpu_fence_wait_empty - wait for all fences to signal
726 * @adev: amdgpu device pointer
727 * @ring: ring index the fence is associated with
729 * Wait for all fences on the requested ring to signal (all asics).
730 * Returns 0 if the fences have passed, error for all other cases.
731 * Caller must hold ring lock.
733 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
735 struct amdgpu_device *adev = ring->adev;
736 uint64_t seq[AMDGPU_MAX_RINGS] = {};
739 seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx];
743 r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT);
748 dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
755 * amdgpu_fence_ref - take a ref on a fence
757 * @fence: amdgpu fence object
759 * Take a reference on a fence (all asics).
762 struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
764 fence_get(&fence->base);
769 * amdgpu_fence_unref - remove a ref on a fence
771 * @fence: amdgpu fence object
773 * Remove a reference on a fence (all asics).
775 void amdgpu_fence_unref(struct amdgpu_fence **fence)
777 struct amdgpu_fence *tmp = *fence;
781 fence_put(&tmp->base);
785 * amdgpu_fence_count_emitted - get the count of emitted fences
787 * @ring: ring the fence is associated with
789 * Get the number of fences emitted on the requested ring (all asics).
790 * Returns the number of emitted fences on the ring. Used by the
791 * dynpm code to ring track activity.
793 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
797 /* We are not protected by ring lock when reading the last sequence
798 * but it's ok to report slightly wrong fence count here.
800 amdgpu_fence_process(ring);
801 emitted = ring->fence_drv.sync_seq[ring->idx]
802 - atomic64_read(&ring->fence_drv.last_seq);
803 /* to avoid 32bits warp around */
804 if (emitted > 0x10000000)
805 emitted = 0x10000000;
807 return (unsigned)emitted;
811 * amdgpu_fence_need_sync - do we need a semaphore
813 * @fence: amdgpu fence object
814 * @dst_ring: which ring to check against
816 * Check if the fence needs to be synced against another ring
817 * (all asics). If so, we need to emit a semaphore.
818 * Returns true if we need to sync with another ring, false if
821 bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
822 struct amdgpu_ring *dst_ring)
824 struct amdgpu_fence_driver *fdrv;
829 if (fence->ring == dst_ring)
832 /* we are protected by the ring mutex */
833 fdrv = &dst_ring->fence_drv;
834 if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
841 * amdgpu_fence_note_sync - record the sync point
843 * @fence: amdgpu fence object
844 * @dst_ring: which ring to check against
846 * Note the sequence number at which point the fence will
847 * be synced with the requested ring (all asics).
849 void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
850 struct amdgpu_ring *dst_ring)
852 struct amdgpu_fence_driver *dst, *src;
858 if (fence->ring == dst_ring)
861 /* we are protected by the ring mutex */
862 src = &fence->ring->fence_drv;
863 dst = &dst_ring->fence_drv;
864 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
865 if (i == dst_ring->idx)
868 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
873 * amdgpu_fence_driver_start_ring - make the fence driver
874 * ready for use on the requested ring.
876 * @ring: ring to start the fence driver on
877 * @irq_src: interrupt source to use for this ring
878 * @irq_type: interrupt type to use for this ring
880 * Make the fence driver ready for processing (all asics).
881 * Not all asics have all rings, so each asic will only
882 * start the fence driver on the rings it has.
883 * Returns 0 for success, errors for failure.
885 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
886 struct amdgpu_irq_src *irq_src,
889 struct amdgpu_device *adev = ring->adev;
892 if (ring != &adev->uvd.ring) {
893 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
894 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
896 /* put fence directly behind firmware */
897 index = ALIGN(adev->uvd.fw->size, 8);
898 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
899 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
901 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
902 ring->fence_drv.initialized = true;
903 ring->fence_drv.irq_src = irq_src;
904 ring->fence_drv.irq_type = irq_type;
905 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
906 "cpu addr 0x%p\n", ring->idx,
907 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
912 * amdgpu_fence_driver_init_ring - init the fence driver
913 * for the requested ring.
915 * @ring: ring to init the fence driver on
917 * Init the fence driver for the requested ring (all asics).
918 * Helper function for amdgpu_fence_driver_init().
920 void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
924 ring->fence_drv.cpu_addr = NULL;
925 ring->fence_drv.gpu_addr = 0;
926 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
927 ring->fence_drv.sync_seq[i] = 0;
929 atomic64_set(&ring->fence_drv.last_seq, 0);
930 ring->fence_drv.initialized = false;
932 INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
933 amdgpu_fence_check_lockup);
934 ring->fence_drv.ring = ring;
938 * amdgpu_fence_driver_init - init the fence driver
939 * for all possible rings.
941 * @adev: amdgpu device pointer
943 * Init the fence driver for all possible rings (all asics).
944 * Not all asics have all rings, so each asic will only
945 * start the fence driver on the rings it has using
946 * amdgpu_fence_driver_start_ring().
947 * Returns 0 for success.
949 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
951 init_waitqueue_head(&adev->fence_queue);
952 if (amdgpu_debugfs_fence_init(adev))
953 dev_err(adev->dev, "fence debugfs file creation failed\n");
959 * amdgpu_fence_driver_fini - tear down the fence driver
960 * for all possible rings.
962 * @adev: amdgpu device pointer
964 * Tear down the fence driver for all possible rings (all asics).
966 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
970 mutex_lock(&adev->ring_lock);
971 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
972 struct amdgpu_ring *ring = adev->rings[i];
973 if (!ring || !ring->fence_drv.initialized)
975 r = amdgpu_fence_wait_empty(ring);
977 /* no need to trigger GPU reset as we are unloading */
978 amdgpu_fence_driver_force_completion(adev);
980 wake_up_all(&adev->fence_queue);
981 ring->fence_drv.initialized = false;
983 mutex_unlock(&adev->ring_lock);
987 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
989 * @adev: amdgpu device pointer
991 * In case of GPU reset failure make sure no process keep waiting on fence
992 * that will never complete.
994 void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
998 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
999 struct amdgpu_ring *ring = adev->rings[i];
1000 if (!ring || !ring->fence_drv.initialized)
1003 amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
1011 #if defined(CONFIG_DEBUG_FS)
1012 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
1014 struct drm_info_node *node = (struct drm_info_node *)m->private;
1015 struct drm_device *dev = node->minor->dev;
1016 struct amdgpu_device *adev = dev->dev_private;
1019 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1020 struct amdgpu_ring *ring = adev->rings[i];
1021 if (!ring || !ring->fence_drv.initialized)
1024 amdgpu_fence_process(ring);
1026 seq_printf(m, "--- ring %d ---\n", i);
1027 seq_printf(m, "Last signaled fence 0x%016llx\n",
1028 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
1029 seq_printf(m, "Last emitted 0x%016llx\n",
1030 ring->fence_drv.sync_seq[i]);
1032 for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
1033 struct amdgpu_ring *other = adev->rings[j];
1034 if (i != j && other && other->fence_drv.initialized)
1035 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
1036 j, ring->fence_drv.sync_seq[j]);
1042 static struct drm_info_list amdgpu_debugfs_fence_list[] = {
1043 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
1047 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
1049 #if defined(CONFIG_DEBUG_FS)
1050 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
1056 static const char *amdgpu_fence_get_driver_name(struct fence *fence)
1061 static const char *amdgpu_fence_get_timeline_name(struct fence *f)
1063 struct amdgpu_fence *fence = to_amdgpu_fence(f);
1064 return (const char *)fence->ring->name;
1067 static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
1069 return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
1072 struct amdgpu_wait_cb {
1073 struct fence_cb base;
1074 struct task_struct *task;
1077 static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
1079 struct amdgpu_wait_cb *wait =
1080 container_of(cb, struct amdgpu_wait_cb, base);
1081 wake_up_process(wait->task);
1084 static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
1087 struct amdgpu_fence *fence = to_amdgpu_fence(f);
1088 struct amdgpu_device *adev = fence->ring->adev;
1089 struct amdgpu_wait_cb cb;
1093 if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb))
1098 set_current_state(TASK_INTERRUPTIBLE);
1100 set_current_state(TASK_UNINTERRUPTIBLE);
1103 * amdgpu_test_signaled must be called after
1104 * set_current_state to prevent a race with wake_up_process
1106 if (amdgpu_test_signaled(fence))
1109 if (adev->needs_reset) {
1114 t = schedule_timeout(t);
1116 if (t > 0 && intr && signal_pending(current))
1120 __set_current_state(TASK_RUNNING);
1121 fence_remove_callback(f, &cb.base);
1126 const struct fence_ops amdgpu_fence_ops = {
1127 .get_driver_name = amdgpu_fence_get_driver_name,
1128 .get_timeline_name = amdgpu_fence_get_timeline_name,
1129 .enable_signaling = amdgpu_fence_enable_signaling,
1130 .signaled = amdgpu_fence_is_signaled,
1131 .wait = amdgpu_fence_default_wait,