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, (*fence)->seq, false);
132 trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq);
137 * amdgpu_fence_check_signaled - callback from fence_queue
139 * this function is called with fence_queue lock held, which is also used
140 * for the fence locking itself, so unlocked variants are used for
141 * fence_signal, and remove_wait_queue.
143 static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
145 struct amdgpu_fence *fence;
146 struct amdgpu_device *adev;
150 fence = container_of(wait, struct amdgpu_fence, fence_wake);
151 adev = fence->ring->adev;
154 * We cannot use amdgpu_fence_process here because we're already
155 * in the waitqueue, in a call from wake_up_all.
157 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
158 if (seq >= fence->seq) {
159 ret = fence_signal_locked(&fence->base);
161 FENCE_TRACE(&fence->base, "signaled from irq context\n");
163 FENCE_TRACE(&fence->base, "was already signaled\n");
165 amdgpu_irq_put(adev, fence->ring->fence_drv.irq_src,
166 fence->ring->fence_drv.irq_type);
167 __remove_wait_queue(&adev->fence_queue, &fence->fence_wake);
168 fence_put(&fence->base);
170 FENCE_TRACE(&fence->base, "pending\n");
175 * amdgpu_fence_activity - check for fence activity
177 * @ring: pointer to struct amdgpu_ring
179 * Checks the current fence value and calculates the last
180 * signalled fence value. Returns true if activity occured
181 * on the ring, and the fence_queue should be waken up.
183 static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
185 uint64_t seq, last_seq, last_emitted;
186 unsigned count_loop = 0;
189 /* Note there is a scenario here for an infinite loop but it's
190 * very unlikely to happen. For it to happen, the current polling
191 * process need to be interrupted by another process and another
192 * process needs to update the last_seq btw the atomic read and
193 * xchg of the current process.
195 * More over for this to go in infinite loop there need to be
196 * continuously new fence signaled ie amdgpu_fence_read needs
197 * to return a different value each time for both the currently
198 * polling process and the other process that xchg the last_seq
199 * btw atomic read and xchg of the current process. And the
200 * value the other process set as last seq must be higher than
201 * the seq value we just read. Which means that current process
202 * need to be interrupted after amdgpu_fence_read and before
205 * To be even more safe we count the number of time we loop and
206 * we bail after 10 loop just accepting the fact that we might
207 * have temporarly set the last_seq not to the true real last
208 * seq but to an older one.
210 last_seq = atomic64_read(&ring->fence_drv.last_seq);
212 last_emitted = ring->fence_drv.sync_seq[ring->idx];
213 seq = amdgpu_fence_read(ring);
214 seq |= last_seq & 0xffffffff00000000LL;
215 if (seq < last_seq) {
217 seq |= last_emitted & 0xffffffff00000000LL;
220 if (seq <= last_seq || seq > last_emitted) {
223 /* If we loop over we don't want to return without
224 * checking if a fence is signaled as it means that the
225 * seq we just read is different from the previous on.
229 if ((count_loop++) > 10) {
230 /* We looped over too many time leave with the
231 * fact that we might have set an older fence
232 * seq then the current real last seq as signaled
237 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
239 if (seq < last_emitted)
240 amdgpu_fence_schedule_check(ring);
246 * amdgpu_fence_check_lockup - check for hardware lockup
248 * @work: delayed work item
250 * Checks for fence activity and if there is none probe
251 * the hardware if a lockup occured.
253 static void amdgpu_fence_check_lockup(struct work_struct *work)
255 struct amdgpu_fence_driver *fence_drv;
256 struct amdgpu_ring *ring;
258 fence_drv = container_of(work, struct amdgpu_fence_driver,
260 ring = fence_drv->ring;
262 if (!down_read_trylock(&ring->adev->exclusive_lock)) {
263 /* just reschedule the check if a reset is going on */
264 amdgpu_fence_schedule_check(ring);
268 if (fence_drv->delayed_irq && ring->adev->ddev->irq_enabled) {
269 fence_drv->delayed_irq = false;
270 amdgpu_irq_update(ring->adev, fence_drv->irq_src,
271 fence_drv->irq_type);
274 if (amdgpu_fence_activity(ring))
275 wake_up_all(&ring->adev->fence_queue);
276 else if (amdgpu_ring_is_lockup(ring)) {
277 /* good news we believe it's a lockup */
278 dev_warn(ring->adev->dev, "GPU lockup (current fence id "
279 "0x%016llx last fence id 0x%016llx on ring %d)\n",
280 (uint64_t)atomic64_read(&fence_drv->last_seq),
281 fence_drv->sync_seq[ring->idx], ring->idx);
283 /* remember that we need an reset */
284 ring->adev->needs_reset = true;
285 wake_up_all(&ring->adev->fence_queue);
287 up_read(&ring->adev->exclusive_lock);
291 * amdgpu_fence_process - process a fence
293 * @adev: amdgpu_device pointer
294 * @ring: ring index the fence is associated with
296 * Checks the current fence value and wakes the fence queue
297 * if the sequence number has increased (all asics).
299 void amdgpu_fence_process(struct amdgpu_ring *ring)
301 uint64_t seq, last_seq, last_emitted;
302 unsigned count_loop = 0;
305 /* Note there is a scenario here for an infinite loop but it's
306 * very unlikely to happen. For it to happen, the current polling
307 * process need to be interrupted by another process and another
308 * process needs to update the last_seq btw the atomic read and
309 * xchg of the current process.
311 * More over for this to go in infinite loop there need to be
312 * continuously new fence signaled ie amdgpu_fence_read needs
313 * to return a different value each time for both the currently
314 * polling process and the other process that xchg the last_seq
315 * btw atomic read and xchg of the current process. And the
316 * value the other process set as last seq must be higher than
317 * the seq value we just read. Which means that current process
318 * need to be interrupted after amdgpu_fence_read and before
321 * To be even more safe we count the number of time we loop and
322 * we bail after 10 loop just accepting the fact that we might
323 * have temporarly set the last_seq not to the true real last
324 * seq but to an older one.
326 last_seq = atomic64_read(&ring->fence_drv.last_seq);
328 last_emitted = ring->fence_drv.sync_seq[ring->idx];
329 seq = amdgpu_fence_read(ring);
330 seq |= last_seq & 0xffffffff00000000LL;
331 if (seq < last_seq) {
333 seq |= last_emitted & 0xffffffff00000000LL;
336 if (seq <= last_seq || seq > last_emitted) {
339 /* If we loop over we don't want to return without
340 * checking if a fence is signaled as it means that the
341 * seq we just read is different from the previous on.
345 if ((count_loop++) > 10) {
346 /* We looped over too many time leave with the
347 * fact that we might have set an older fence
348 * seq then the current real last seq as signaled
353 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
356 wake_up_all(&ring->adev->fence_queue);
360 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
362 * @ring: ring the fence is associated with
363 * @seq: sequence number
365 * Check if the last signaled fence sequnce number is >= the requested
366 * sequence number (all asics).
367 * Returns true if the fence has signaled (current fence value
368 * is >= requested value) or false if it has not (current fence
369 * value is < the requested value. Helper function for
370 * amdgpu_fence_signaled().
372 static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
374 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
377 /* poll new last sequence at least once */
378 amdgpu_fence_process(ring);
379 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
385 static bool amdgpu_fence_is_signaled(struct fence *f)
387 struct amdgpu_fence *fence = to_amdgpu_fence(f);
388 struct amdgpu_ring *ring = fence->ring;
389 struct amdgpu_device *adev = ring->adev;
391 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
394 if (down_read_trylock(&adev->exclusive_lock)) {
395 amdgpu_fence_process(ring);
396 up_read(&adev->exclusive_lock);
398 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
405 * amdgpu_fence_enable_signaling - enable signalling on fence
408 * This function is called with fence_queue lock held, and adds a callback
409 * to fence_queue that checks if this fence is signaled, and if so it
410 * signals the fence and removes itself.
412 static bool amdgpu_fence_enable_signaling(struct fence *f)
414 struct amdgpu_fence *fence = to_amdgpu_fence(f);
415 struct amdgpu_ring *ring = fence->ring;
416 struct amdgpu_device *adev = ring->adev;
418 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
421 if (down_read_trylock(&adev->exclusive_lock)) {
422 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
423 ring->fence_drv.irq_type);
424 if (amdgpu_fence_activity(ring))
425 wake_up_all_locked(&adev->fence_queue);
427 /* did fence get signaled after we enabled the sw irq? */
428 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) {
429 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
430 ring->fence_drv.irq_type);
431 up_read(&adev->exclusive_lock);
435 up_read(&adev->exclusive_lock);
437 /* we're probably in a lockup, lets not fiddle too much */
438 if (amdgpu_irq_get_delayed(adev, ring->fence_drv.irq_src,
439 ring->fence_drv.irq_type))
440 ring->fence_drv.delayed_irq = true;
441 amdgpu_fence_schedule_check(ring);
444 fence->fence_wake.flags = 0;
445 fence->fence_wake.private = NULL;
446 fence->fence_wake.func = amdgpu_fence_check_signaled;
447 __add_wait_queue(&adev->fence_queue, &fence->fence_wake);
449 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
454 * amdgpu_fence_signaled - check if a fence has signaled
456 * @fence: amdgpu fence object
458 * Check if the requested fence has signaled (all asics).
459 * Returns true if the fence has signaled or false if it has not.
461 bool amdgpu_fence_signaled(struct amdgpu_fence *fence)
466 if (amdgpu_fence_seq_signaled(fence->ring, fence->seq)) {
467 if (!fence_signal(&fence->base))
468 FENCE_TRACE(&fence->base, "signaled from amdgpu_fence_signaled\n");
476 * amdgpu_fence_any_seq_signaled - check if any sequence number is signaled
478 * @adev: amdgpu device pointer
479 * @seq: sequence numbers
481 * Check if the last signaled fence sequnce number is >= the requested
482 * sequence number (all asics).
483 * Returns true if any has signaled (current value is >= requested value)
484 * or false if it has not. Helper function for amdgpu_fence_wait_seq.
486 static bool amdgpu_fence_any_seq_signaled(struct amdgpu_device *adev, u64 *seq)
490 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
491 if (!adev->rings[i] || !seq[i])
494 if (amdgpu_fence_seq_signaled(adev->rings[i], seq[i]))
502 * amdgpu_fence_wait_seq_timeout - wait for a specific sequence numbers
504 * @adev: amdgpu device pointer
505 * @target_seq: sequence number(s) we want to wait for
506 * @intr: use interruptable sleep
507 * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
509 * Wait for the requested sequence number(s) to be written by any ring
510 * (all asics). Sequnce number array is indexed by ring id.
511 * @intr selects whether to use interruptable (true) or non-interruptable
512 * (false) sleep when waiting for the sequence number. Helper function
513 * for amdgpu_fence_wait_*().
514 * Returns remaining time if the sequence number has passed, 0 when
515 * the wait timeout, or an error for all other cases.
516 * -EDEADLK is returned when a GPU lockup has been detected.
518 long amdgpu_fence_wait_seq_timeout(struct amdgpu_device *adev, u64 *target_seq,
519 bool intr, long timeout)
521 uint64_t last_seq[AMDGPU_MAX_RINGS];
526 return amdgpu_fence_any_seq_signaled(adev, target_seq);
529 while (!amdgpu_fence_any_seq_signaled(adev, target_seq)) {
531 /* Save current sequence values, used to check for GPU lockups */
532 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
533 struct amdgpu_ring *ring = adev->rings[i];
535 if (!ring || !target_seq[i])
538 last_seq[i] = atomic64_read(&ring->fence_drv.last_seq);
539 trace_amdgpu_fence_wait_begin(adev->ddev, i, target_seq[i]);
540 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
541 ring->fence_drv.irq_type);
545 r = wait_event_interruptible_timeout(adev->fence_queue, (
546 (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
547 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
549 r = wait_event_timeout(adev->fence_queue, (
550 (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
551 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
554 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
555 struct amdgpu_ring *ring = adev->rings[i];
557 if (!ring || !target_seq[i])
560 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
561 ring->fence_drv.irq_type);
562 trace_amdgpu_fence_wait_end(adev->ddev, i, target_seq[i]);
568 if (unlikely(!signaled)) {
570 if (adev->needs_reset)
573 /* we were interrupted for some reason and fence
574 * isn't signaled yet, resume waiting */
578 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
579 struct amdgpu_ring *ring = adev->rings[i];
581 if (!ring || !target_seq[i])
584 if (last_seq[i] != atomic64_read(&ring->fence_drv.last_seq))
588 if (i != AMDGPU_MAX_RINGS)
591 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
592 if (!adev->rings[i] || !target_seq[i])
595 if (amdgpu_ring_is_lockup(adev->rings[i]))
599 if (i < AMDGPU_MAX_RINGS) {
600 /* good news we believe it's a lockup */
601 dev_warn(adev->dev, "GPU lockup (waiting for "
602 "0x%016llx last fence id 0x%016llx on"
604 target_seq[i], last_seq[i], i);
606 /* remember that we need an reset */
607 adev->needs_reset = true;
608 wake_up_all(&adev->fence_queue);
612 if (timeout < MAX_SCHEDULE_TIMEOUT) {
613 timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
624 * amdgpu_fence_wait - wait for a fence to signal
626 * @fence: amdgpu fence object
627 * @intr: use interruptable sleep
629 * Wait for the requested fence to signal (all asics).
630 * @intr selects whether to use interruptable (true) or non-interruptable
631 * (false) sleep when waiting for the fence.
632 * Returns 0 if the fence has passed, error for all other cases.
634 int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr)
636 uint64_t seq[AMDGPU_MAX_RINGS] = {};
639 seq[fence->ring->idx] = fence->seq;
640 r = amdgpu_fence_wait_seq_timeout(fence->ring->adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
645 r = fence_signal(&fence->base);
647 FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
652 * amdgpu_fence_wait_any - wait for a fence to signal on any ring
654 * @adev: amdgpu device pointer
655 * @fences: amdgpu fence object(s)
656 * @intr: use interruptable sleep
658 * Wait for any requested fence to signal (all asics). Fence
659 * array is indexed by ring id. @intr selects whether to use
660 * interruptable (true) or non-interruptable (false) sleep when
661 * waiting for the fences. Used by the suballocator.
662 * Returns 0 if any fence has passed, error for all other cases.
664 int amdgpu_fence_wait_any(struct amdgpu_device *adev,
665 struct amdgpu_fence **fences,
668 uint64_t seq[AMDGPU_MAX_RINGS];
669 unsigned i, num_rings = 0;
672 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
679 seq[i] = fences[i]->seq;
683 /* nothing to wait for ? */
687 r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
695 * amdgpu_fence_wait_next - wait for the next fence to signal
697 * @adev: amdgpu device pointer
698 * @ring: ring index the fence is associated with
700 * Wait for the next fence on the requested ring to signal (all asics).
701 * Returns 0 if the next fence has passed, error for all other cases.
702 * Caller must hold ring lock.
704 int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
706 uint64_t seq[AMDGPU_MAX_RINGS] = {};
709 seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
710 if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) {
711 /* nothing to wait for, last_seq is
712 already the last emited fence */
715 r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT);
722 * amdgpu_fence_wait_empty - wait for all fences to signal
724 * @adev: amdgpu device pointer
725 * @ring: ring index the fence is associated with
727 * Wait for all fences on the requested ring to signal (all asics).
728 * Returns 0 if the fences have passed, error for all other cases.
729 * Caller must hold ring lock.
731 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
733 struct amdgpu_device *adev = ring->adev;
734 uint64_t seq[AMDGPU_MAX_RINGS] = {};
737 seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx];
741 r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT);
746 dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
753 * amdgpu_fence_ref - take a ref on a fence
755 * @fence: amdgpu fence object
757 * Take a reference on a fence (all asics).
760 struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
762 fence_get(&fence->base);
767 * amdgpu_fence_unref - remove a ref on a fence
769 * @fence: amdgpu fence object
771 * Remove a reference on a fence (all asics).
773 void amdgpu_fence_unref(struct amdgpu_fence **fence)
775 struct amdgpu_fence *tmp = *fence;
779 fence_put(&tmp->base);
783 * amdgpu_fence_count_emitted - get the count of emitted fences
785 * @ring: ring the fence is associated with
787 * Get the number of fences emitted on the requested ring (all asics).
788 * Returns the number of emitted fences on the ring. Used by the
789 * dynpm code to ring track activity.
791 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
795 /* We are not protected by ring lock when reading the last sequence
796 * but it's ok to report slightly wrong fence count here.
798 amdgpu_fence_process(ring);
799 emitted = ring->fence_drv.sync_seq[ring->idx]
800 - atomic64_read(&ring->fence_drv.last_seq);
801 /* to avoid 32bits warp around */
802 if (emitted > 0x10000000)
803 emitted = 0x10000000;
805 return (unsigned)emitted;
809 * amdgpu_fence_need_sync - do we need a semaphore
811 * @fence: amdgpu fence object
812 * @dst_ring: which ring to check against
814 * Check if the fence needs to be synced against another ring
815 * (all asics). If so, we need to emit a semaphore.
816 * Returns true if we need to sync with another ring, false if
819 bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
820 struct amdgpu_ring *dst_ring)
822 struct amdgpu_fence_driver *fdrv;
827 if (fence->ring == dst_ring)
830 /* we are protected by the ring mutex */
831 fdrv = &dst_ring->fence_drv;
832 if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
839 * amdgpu_fence_note_sync - record the sync point
841 * @fence: amdgpu fence object
842 * @dst_ring: which ring to check against
844 * Note the sequence number at which point the fence will
845 * be synced with the requested ring (all asics).
847 void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
848 struct amdgpu_ring *dst_ring)
850 struct amdgpu_fence_driver *dst, *src;
856 if (fence->ring == dst_ring)
859 /* we are protected by the ring mutex */
860 src = &fence->ring->fence_drv;
861 dst = &dst_ring->fence_drv;
862 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
863 if (i == dst_ring->idx)
866 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
871 * amdgpu_fence_driver_start_ring - make the fence driver
872 * ready for use on the requested ring.
874 * @ring: ring to start the fence driver on
875 * @irq_src: interrupt source to use for this ring
876 * @irq_type: interrupt type to use for this ring
878 * Make the fence driver ready for processing (all asics).
879 * Not all asics have all rings, so each asic will only
880 * start the fence driver on the rings it has.
881 * Returns 0 for success, errors for failure.
883 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
884 struct amdgpu_irq_src *irq_src,
887 struct amdgpu_device *adev = ring->adev;
890 if (ring != &adev->uvd.ring) {
891 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
892 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
894 /* put fence directly behind firmware */
895 index = ALIGN(adev->uvd.fw->size, 8);
896 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
897 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
899 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
900 ring->fence_drv.initialized = true;
901 ring->fence_drv.irq_src = irq_src;
902 ring->fence_drv.irq_type = irq_type;
903 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
904 "cpu addr 0x%p\n", ring->idx,
905 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
910 * amdgpu_fence_driver_init_ring - init the fence driver
911 * for the requested ring.
913 * @ring: ring to init the fence driver on
915 * Init the fence driver for the requested ring (all asics).
916 * Helper function for amdgpu_fence_driver_init().
918 void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
922 ring->fence_drv.cpu_addr = NULL;
923 ring->fence_drv.gpu_addr = 0;
924 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
925 ring->fence_drv.sync_seq[i] = 0;
927 atomic64_set(&ring->fence_drv.last_seq, 0);
928 ring->fence_drv.initialized = false;
930 INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
931 amdgpu_fence_check_lockup);
932 ring->fence_drv.ring = ring;
936 * amdgpu_fence_driver_init - init the fence driver
937 * for all possible rings.
939 * @adev: amdgpu device pointer
941 * Init the fence driver for all possible rings (all asics).
942 * Not all asics have all rings, so each asic will only
943 * start the fence driver on the rings it has using
944 * amdgpu_fence_driver_start_ring().
945 * Returns 0 for success.
947 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
949 init_waitqueue_head(&adev->fence_queue);
950 if (amdgpu_debugfs_fence_init(adev))
951 dev_err(adev->dev, "fence debugfs file creation failed\n");
957 * amdgpu_fence_driver_fini - tear down the fence driver
958 * for all possible rings.
960 * @adev: amdgpu device pointer
962 * Tear down the fence driver for all possible rings (all asics).
964 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
968 mutex_lock(&adev->ring_lock);
969 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
970 struct amdgpu_ring *ring = adev->rings[i];
971 if (!ring || !ring->fence_drv.initialized)
973 r = amdgpu_fence_wait_empty(ring);
975 /* no need to trigger GPU reset as we are unloading */
976 amdgpu_fence_driver_force_completion(adev);
978 wake_up_all(&adev->fence_queue);
979 ring->fence_drv.initialized = false;
981 mutex_unlock(&adev->ring_lock);
985 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
987 * @adev: amdgpu device pointer
989 * In case of GPU reset failure make sure no process keep waiting on fence
990 * that will never complete.
992 void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
996 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
997 struct amdgpu_ring *ring = adev->rings[i];
998 if (!ring || !ring->fence_drv.initialized)
1001 amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
1009 #if defined(CONFIG_DEBUG_FS)
1010 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
1012 struct drm_info_node *node = (struct drm_info_node *)m->private;
1013 struct drm_device *dev = node->minor->dev;
1014 struct amdgpu_device *adev = dev->dev_private;
1017 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1018 struct amdgpu_ring *ring = adev->rings[i];
1019 if (!ring || !ring->fence_drv.initialized)
1022 amdgpu_fence_process(ring);
1024 seq_printf(m, "--- ring %d ---\n", i);
1025 seq_printf(m, "Last signaled fence 0x%016llx\n",
1026 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
1027 seq_printf(m, "Last emitted 0x%016llx\n",
1028 ring->fence_drv.sync_seq[i]);
1030 for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
1031 struct amdgpu_ring *other = adev->rings[j];
1032 if (i != j && other && other->fence_drv.initialized)
1033 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
1034 j, ring->fence_drv.sync_seq[j]);
1040 static struct drm_info_list amdgpu_debugfs_fence_list[] = {
1041 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
1045 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
1047 #if defined(CONFIG_DEBUG_FS)
1048 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
1054 static const char *amdgpu_fence_get_driver_name(struct fence *fence)
1059 static const char *amdgpu_fence_get_timeline_name(struct fence *f)
1061 struct amdgpu_fence *fence = to_amdgpu_fence(f);
1062 return (const char *)fence->ring->name;
1065 static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
1067 return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
1070 struct amdgpu_wait_cb {
1071 struct fence_cb base;
1072 struct task_struct *task;
1075 static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
1077 struct amdgpu_wait_cb *wait =
1078 container_of(cb, struct amdgpu_wait_cb, base);
1079 wake_up_process(wait->task);
1082 static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
1085 struct amdgpu_fence *fence = to_amdgpu_fence(f);
1086 struct amdgpu_device *adev = fence->ring->adev;
1087 struct amdgpu_wait_cb cb;
1091 if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb))
1096 set_current_state(TASK_INTERRUPTIBLE);
1098 set_current_state(TASK_UNINTERRUPTIBLE);
1101 * amdgpu_test_signaled must be called after
1102 * set_current_state to prevent a race with wake_up_process
1104 if (amdgpu_test_signaled(fence))
1107 if (adev->needs_reset) {
1112 t = schedule_timeout(t);
1114 if (t > 0 && intr && signal_pending(current))
1118 __set_current_state(TASK_RUNNING);
1119 fence_remove_callback(f, &cb.base);
1124 const struct fence_ops amdgpu_fence_ops = {
1125 .get_driver_name = amdgpu_fence_get_driver_name,
1126 .get_timeline_name = amdgpu_fence_get_timeline_name,
1127 .enable_signaling = amdgpu_fence_enable_signaling,
1128 .signaled = amdgpu_fence_is_signaled,
1129 .wait = amdgpu_fence_default_wait,