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
28 * Jerome Glisse <glisse@freedesktop.org>
31 * $FreeBSD: head/sys/dev/drm2/radeon/radeon_fence.c 254885 2013-08-25 19:37:15Z dumbbell $
35 #include "radeon_reg.h"
38 #include "radeon_trace.h"
39 #endif /* DUMBBELL_WIP */
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. Whether
48 * we use a scratch register or memory location depends on the asic
49 * and whether writeback is enabled.
53 * radeon_fence_write - write a fence value
55 * @rdev: radeon_device pointer
56 * @seq: sequence number to write
57 * @ring: ring index the fence is associated with
59 * Writes a fence value to memory or a scratch register (all asics).
61 static void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
63 struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
64 if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
65 *drv->cpu_addr = cpu_to_le32(seq);
67 WREG32(drv->scratch_reg, seq);
72 * radeon_fence_read - read a fence value
74 * @rdev: radeon_device pointer
75 * @ring: ring index the fence is associated with
77 * Reads a fence value from memory or a scratch register (all asics).
78 * Returns the value of the fence read from memory or register.
80 static u32 radeon_fence_read(struct radeon_device *rdev, int ring)
82 struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
85 if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
86 seq = le32_to_cpu(*drv->cpu_addr);
88 seq = RREG32(drv->scratch_reg);
94 * radeon_fence_emit - emit a fence on the requested ring
96 * @rdev: radeon_device pointer
97 * @fence: radeon fence object
98 * @ring: ring index the fence is associated with
100 * Emits a fence command on the requested ring (all asics).
101 * Returns 0 on success, -ENOMEM on failure.
103 int radeon_fence_emit(struct radeon_device *rdev,
104 struct radeon_fence **fence,
107 /* we are protected by the ring emission mutex */
108 *fence = kmalloc(sizeof(struct radeon_fence), M_DRM,
110 if ((*fence) == NULL) {
113 refcount_init(&((*fence)->kref), 1);
114 (*fence)->rdev = rdev;
115 (*fence)->seq = ++rdev->fence_drv[ring].sync_seq[ring];
116 (*fence)->ring = ring;
117 radeon_fence_ring_emit(rdev, ring, *fence);
122 * radeon_fence_process - process a fence
124 * @rdev: radeon_device pointer
125 * @ring: ring index the fence is associated with
127 * Checks the current fence value and wakes the fence queue
128 * if the sequence number has increased (all asics).
130 void radeon_fence_process(struct radeon_device *rdev, int ring)
132 uint64_t seq, last_seq, last_emitted;
133 unsigned count_loop = 0;
136 /* Note there is a scenario here for an infinite loop but it's
137 * very unlikely to happen. For it to happen, the current polling
138 * process need to be interrupted by another process and another
139 * process needs to update the last_seq btw the atomic read and
140 * xchg of the current process.
142 * More over for this to go in infinite loop there need to be
143 * continuously new fence signaled ie radeon_fence_read needs
144 * to return a different value each time for both the currently
145 * polling process and the other process that xchg the last_seq
146 * btw atomic read and xchg of the current process. And the
147 * value the other process set as last seq must be higher than
148 * the seq value we just read. Which means that current process
149 * need to be interrupted after radeon_fence_read and before
152 * To be even more safe we count the number of time we loop and
153 * we bail after 10 loop just accepting the fact that we might
154 * have temporarly set the last_seq not to the true real last
155 * seq but to an older one.
157 last_seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
159 last_emitted = rdev->fence_drv[ring].sync_seq[ring];
160 seq = radeon_fence_read(rdev, ring);
161 seq |= last_seq & 0xffffffff00000000LL;
162 if (seq < last_seq) {
164 seq |= last_emitted & 0xffffffff00000000LL;
167 if (seq <= last_seq || seq > last_emitted) {
170 /* If we loop over we don't want to return without
171 * checking if a fence is signaled as it means that the
172 * seq we just read is different from the previous on.
176 if ((count_loop++) > 10) {
177 /* We looped over too many time leave with the
178 * fact that we might have set an older fence
179 * seq then the current real last seq as signaled
184 } while (atomic64_xchg(&rdev->fence_drv[ring].last_seq, seq) > seq);
187 rdev->fence_drv[ring].last_activity = jiffies;
188 cv_broadcast(&rdev->fence_queue);
193 * radeon_fence_destroy - destroy a fence
197 * Frees the fence object (all asics).
199 static void radeon_fence_destroy(struct radeon_fence *fence)
202 drm_free(fence, M_DRM);
206 * radeon_fence_seq_signaled - check if a fence sequeuce number has signaled
208 * @rdev: radeon device pointer
209 * @seq: sequence number
210 * @ring: ring index the fence is associated with
212 * Check if the last singled fence sequnce number is >= the requested
213 * sequence number (all asics).
214 * Returns true if the fence has signaled (current fence value
215 * is >= requested value) or false if it has not (current fence
216 * value is < the requested value. Helper function for
217 * radeon_fence_signaled().
219 static bool radeon_fence_seq_signaled(struct radeon_device *rdev,
220 u64 seq, unsigned ring)
222 if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
225 /* poll new last sequence at least once */
226 radeon_fence_process(rdev, ring);
227 if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
234 * radeon_fence_signaled - check if a fence has signaled
236 * @fence: radeon fence object
238 * Check if the requested fence has signaled (all asics).
239 * Returns true if the fence has signaled or false if it has not.
241 bool radeon_fence_signaled(struct radeon_fence *fence)
246 if (fence->seq == RADEON_FENCE_SIGNALED_SEQ) {
249 if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
250 fence->seq = RADEON_FENCE_SIGNALED_SEQ;
257 * radeon_fence_wait_seq - wait for a specific sequence number
259 * @rdev: radeon device pointer
260 * @target_seq: sequence number we want to wait for
261 * @ring: ring index the fence is associated with
262 * @intr: use interruptable sleep
263 * @lock_ring: whether the ring should be locked or not
265 * Wait for the requested sequence number to be written (all asics).
266 * @intr selects whether to use interruptable (true) or non-interruptable
267 * (false) sleep when waiting for the sequence number. Helper function
268 * for radeon_fence_wait(), et al.
269 * Returns 0 if the sequence number has passed, error for all other cases.
270 * -EDEADLK is returned when a GPU lockup has been detected and the ring is
271 * marked as not ready so no further jobs get scheduled until a successful
274 static int radeon_fence_wait_seq(struct radeon_device *rdev, u64 target_seq,
275 unsigned ring, bool intr, bool lock_ring)
277 unsigned long timeout, last_activity;
280 bool signaled, fence_queue_locked;
283 while (target_seq > atomic64_read(&rdev->fence_drv[ring].last_seq)) {
284 if (!rdev->ring[ring].ready) {
288 timeout = jiffies - RADEON_FENCE_JIFFIES_TIMEOUT;
289 if (time_after(rdev->fence_drv[ring].last_activity, timeout)) {
290 /* the normal case, timeout is somewhere before last_activity */
291 timeout = rdev->fence_drv[ring].last_activity - timeout;
293 /* either jiffies wrapped around, or no fence was signaled in the last 500ms
294 * anyway we will just wait for the minimum amount and then check for a lockup
298 seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
299 /* Save current last activity valuee, used to check for GPU lockups */
300 last_activity = rdev->fence_drv[ring].last_activity;
302 radeon_irq_kms_sw_irq_get(rdev, ring);
303 fence_queue_locked = false;
305 while (!(signaled = radeon_fence_seq_signaled(rdev,
306 target_seq, ring))) {
307 if (!fence_queue_locked) {
308 lockmgr(&rdev->fence_queue_mtx, LK_EXCLUSIVE);
309 fence_queue_locked = true;
312 r = cv_timedwait_sig(&rdev->fence_queue,
313 &rdev->fence_queue_mtx,
316 r = cv_timedwait(&rdev->fence_queue,
317 &rdev->fence_queue_mtx,
321 if (r == EWOULDBLOCK) {
323 radeon_fence_seq_signaled(
324 rdev, target_seq, ring);
329 if (fence_queue_locked) {
330 lockmgr(&rdev->fence_queue_mtx, LK_RELEASE);
332 radeon_irq_kms_sw_irq_put(rdev, ring);
333 if (unlikely(r == EINTR || r == ERESTART)) {
337 if (unlikely(!signaled)) {
339 /* we were interrupted for some reason and fence
340 * isn't signaled yet, resume waiting */
346 /* check if sequence value has changed since last_activity */
347 if (seq != atomic64_read(&rdev->fence_drv[ring].last_seq)) {
352 lockmgr(&rdev->ring_lock, LK_EXCLUSIVE);
355 /* test if somebody else has already decided that this is a lockup */
356 if (last_activity != rdev->fence_drv[ring].last_activity) {
358 lockmgr(&rdev->ring_lock, LK_RELEASE);
363 if (radeon_ring_is_lockup(rdev, ring, &rdev->ring[ring])) {
364 /* good news we believe it's a lockup */
365 dev_warn(rdev->dev, "GPU lockup (waiting for 0x%016jx last fence id 0x%016jx)\n",
366 (uintmax_t)target_seq, (uintmax_t)seq);
368 /* change last activity so nobody else think there is a lockup */
369 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
370 rdev->fence_drv[i].last_activity = jiffies;
373 /* mark the ring as not ready any more */
374 rdev->ring[ring].ready = false;
376 lockmgr(&rdev->ring_lock, LK_RELEASE);
382 lockmgr(&rdev->ring_lock, LK_RELEASE);
390 * radeon_fence_wait - wait for a fence to signal
392 * @fence: radeon fence object
393 * @intr: use interruptable sleep
395 * Wait for the requested fence to signal (all asics).
396 * @intr selects whether to use interruptable (true) or non-interruptable
397 * (false) sleep when waiting for the fence.
398 * Returns 0 if the fence has passed, error for all other cases.
400 int radeon_fence_wait(struct radeon_fence *fence, bool intr)
405 DRM_ERROR("Querying an invalid fence : %p !\n", fence);
409 r = radeon_fence_wait_seq(fence->rdev, fence->seq,
410 fence->ring, intr, true);
414 fence->seq = RADEON_FENCE_SIGNALED_SEQ;
418 static bool radeon_fence_any_seq_signaled(struct radeon_device *rdev, u64 *seq)
422 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
423 if (seq[i] && radeon_fence_seq_signaled(rdev, seq[i], i)) {
431 * radeon_fence_wait_any_seq - wait for a sequence number on any ring
433 * @rdev: radeon device pointer
434 * @target_seq: sequence number(s) we want to wait for
435 * @intr: use interruptable sleep
437 * Wait for the requested sequence number(s) to be written by any ring
438 * (all asics). Sequnce number array is indexed by ring id.
439 * @intr selects whether to use interruptable (true) or non-interruptable
440 * (false) sleep when waiting for the sequence number. Helper function
441 * for radeon_fence_wait_any(), et al.
442 * Returns 0 if the sequence number has passed, error for all other cases.
444 static int radeon_fence_wait_any_seq(struct radeon_device *rdev,
445 u64 *target_seq, bool intr)
447 unsigned long timeout, last_activity, tmp;
448 unsigned i, ring = RADEON_NUM_RINGS;
449 bool signaled, fence_queue_locked;
452 for (i = 0, last_activity = 0; i < RADEON_NUM_RINGS; ++i) {
453 if (!target_seq[i]) {
457 /* use the most recent one as indicator */
458 if (time_after(rdev->fence_drv[i].last_activity, last_activity)) {
459 last_activity = rdev->fence_drv[i].last_activity;
462 /* For lockup detection just pick the lowest ring we are
463 * actively waiting for
470 /* nothing to wait for ? */
471 if (ring == RADEON_NUM_RINGS) {
475 while (!radeon_fence_any_seq_signaled(rdev, target_seq)) {
476 timeout = jiffies - RADEON_FENCE_JIFFIES_TIMEOUT;
477 if (time_after(last_activity, timeout)) {
478 /* the normal case, timeout is somewhere before last_activity */
479 timeout = last_activity - timeout;
481 /* either jiffies wrapped around, or no fence was signaled in the last 500ms
482 * anyway we will just wait for the minimum amount and then check for a lockup
487 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
489 radeon_irq_kms_sw_irq_get(rdev, i);
492 fence_queue_locked = false;
494 while (!(signaled = radeon_fence_any_seq_signaled(rdev,
496 if (!fence_queue_locked) {
497 lockmgr(&rdev->fence_queue_mtx, LK_EXCLUSIVE);
498 fence_queue_locked = true;
501 r = cv_timedwait_sig(&rdev->fence_queue,
502 &rdev->fence_queue_mtx,
505 r = cv_timedwait(&rdev->fence_queue,
506 &rdev->fence_queue_mtx,
510 if (r == EWOULDBLOCK) {
512 radeon_fence_any_seq_signaled(
518 if (fence_queue_locked) {
519 lockmgr(&rdev->fence_queue_mtx, LK_RELEASE);
521 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
523 radeon_irq_kms_sw_irq_put(rdev, i);
526 if (unlikely(r == EINTR || r == ERESTART)) {
530 if (unlikely(!signaled)) {
532 /* we were interrupted for some reason and fence
533 * isn't signaled yet, resume waiting */
539 lockmgr(&rdev->ring_lock, LK_EXCLUSIVE);
540 for (i = 0, tmp = 0; i < RADEON_NUM_RINGS; ++i) {
541 if (time_after(rdev->fence_drv[i].last_activity, tmp)) {
542 tmp = rdev->fence_drv[i].last_activity;
545 /* test if somebody else has already decided that this is a lockup */
546 if (last_activity != tmp) {
548 lockmgr(&rdev->ring_lock, LK_RELEASE);
552 if (radeon_ring_is_lockup(rdev, ring, &rdev->ring[ring])) {
553 /* good news we believe it's a lockup */
554 dev_warn(rdev->dev, "GPU lockup (waiting for 0x%016jx)\n",
555 (uintmax_t)target_seq[ring]);
557 /* change last activity so nobody else think there is a lockup */
558 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
559 rdev->fence_drv[i].last_activity = jiffies;
562 /* mark the ring as not ready any more */
563 rdev->ring[ring].ready = false;
564 lockmgr(&rdev->ring_lock, LK_RELEASE);
567 lockmgr(&rdev->ring_lock, LK_RELEASE);
574 * radeon_fence_wait_any - wait for a fence to signal on any ring
576 * @rdev: radeon device pointer
577 * @fences: radeon fence object(s)
578 * @intr: use interruptable sleep
580 * Wait for any requested fence to signal (all asics). Fence
581 * array is indexed by ring id. @intr selects whether to use
582 * interruptable (true) or non-interruptable (false) sleep when
583 * waiting for the fences. Used by the suballocator.
584 * Returns 0 if any fence has passed, error for all other cases.
586 int radeon_fence_wait_any(struct radeon_device *rdev,
587 struct radeon_fence **fences,
590 uint64_t seq[RADEON_NUM_RINGS];
594 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
601 if (fences[i]->seq == RADEON_FENCE_SIGNALED_SEQ) {
602 /* something was allready signaled */
606 seq[i] = fences[i]->seq;
609 r = radeon_fence_wait_any_seq(rdev, seq, intr);
617 * radeon_fence_wait_next_locked - wait for the next fence to signal
619 * @rdev: radeon device pointer
620 * @ring: ring index the fence is associated with
622 * Wait for the next fence on the requested ring to signal (all asics).
623 * Returns 0 if the next fence has passed, error for all other cases.
624 * Caller must hold ring lock.
626 int radeon_fence_wait_next_locked(struct radeon_device *rdev, int ring)
630 seq = atomic64_read(&rdev->fence_drv[ring].last_seq) + 1ULL;
631 if (seq >= rdev->fence_drv[ring].sync_seq[ring]) {
632 /* nothing to wait for, last_seq is
633 already the last emited fence */
636 return radeon_fence_wait_seq(rdev, seq, ring, false, false);
640 * radeon_fence_wait_empty_locked - wait for all fences to signal
642 * @rdev: radeon device pointer
643 * @ring: ring index the fence is associated with
645 * Wait for all fences on the requested ring to signal (all asics).
646 * Returns 0 if the fences have passed, error for all other cases.
647 * Caller must hold ring lock.
649 int radeon_fence_wait_empty_locked(struct radeon_device *rdev, int ring)
651 uint64_t seq = rdev->fence_drv[ring].sync_seq[ring];
654 r = radeon_fence_wait_seq(rdev, seq, ring, false, false);
659 dev_err(rdev->dev, "error waiting for ring[%d] to become idle (%d)\n",
666 * radeon_fence_ref - take a ref on a fence
668 * @fence: radeon fence object
670 * Take a reference on a fence (all asics).
673 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
675 refcount_acquire(&fence->kref);
680 * radeon_fence_unref - remove a ref on a fence
682 * @fence: radeon fence object
684 * Remove a reference on a fence (all asics).
686 void radeon_fence_unref(struct radeon_fence **fence)
688 struct radeon_fence *tmp = *fence;
692 if (refcount_release(&tmp->kref)) {
693 radeon_fence_destroy(tmp);
699 * radeon_fence_count_emitted - get the count of emitted fences
701 * @rdev: radeon device pointer
702 * @ring: ring index the fence is associated with
704 * Get the number of fences emitted on the requested ring (all asics).
705 * Returns the number of emitted fences on the ring. Used by the
706 * dynpm code to ring track activity.
708 unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
712 /* We are not protected by ring lock when reading the last sequence
713 * but it's ok to report slightly wrong fence count here.
715 radeon_fence_process(rdev, ring);
716 emitted = rdev->fence_drv[ring].sync_seq[ring]
717 - atomic64_read(&rdev->fence_drv[ring].last_seq);
718 /* to avoid 32bits warp around */
719 if (emitted > 0x10000000) {
720 emitted = 0x10000000;
722 return (unsigned)emitted;
726 * radeon_fence_need_sync - do we need a semaphore
728 * @fence: radeon fence object
729 * @dst_ring: which ring to check against
731 * Check if the fence needs to be synced against another ring
732 * (all asics). If so, we need to emit a semaphore.
733 * Returns true if we need to sync with another ring, false if
736 bool radeon_fence_need_sync(struct radeon_fence *fence, int dst_ring)
738 struct radeon_fence_driver *fdrv;
744 if (fence->ring == dst_ring) {
748 /* we are protected by the ring mutex */
749 fdrv = &fence->rdev->fence_drv[dst_ring];
750 if (fence->seq <= fdrv->sync_seq[fence->ring]) {
758 * radeon_fence_note_sync - record the sync point
760 * @fence: radeon fence object
761 * @dst_ring: which ring to check against
763 * Note the sequence number at which point the fence will
764 * be synced with the requested ring (all asics).
766 void radeon_fence_note_sync(struct radeon_fence *fence, int dst_ring)
768 struct radeon_fence_driver *dst, *src;
775 if (fence->ring == dst_ring) {
779 /* we are protected by the ring mutex */
780 src = &fence->rdev->fence_drv[fence->ring];
781 dst = &fence->rdev->fence_drv[dst_ring];
782 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
786 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
791 * radeon_fence_driver_start_ring - make the fence driver
792 * ready for use on the requested ring.
794 * @rdev: radeon device pointer
795 * @ring: ring index to start the fence driver on
797 * Make the fence driver ready for processing (all asics).
798 * Not all asics have all rings, so each asic will only
799 * start the fence driver on the rings it has.
800 * Returns 0 for success, errors for failure.
802 int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
807 radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
808 if (rdev->wb.use_event || !radeon_ring_supports_scratch_reg(rdev, &rdev->ring[ring])) {
809 rdev->fence_drv[ring].scratch_reg = 0;
810 index = R600_WB_EVENT_OFFSET + ring * 4;
812 r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
814 dev_err(rdev->dev, "fence failed to get scratch register\n");
817 index = RADEON_WB_SCRATCH_OFFSET +
818 rdev->fence_drv[ring].scratch_reg -
819 rdev->scratch.reg_base;
821 rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
822 rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
823 radeon_fence_write(rdev, atomic64_read(&rdev->fence_drv[ring].last_seq), ring);
824 rdev->fence_drv[ring].initialized = true;
825 dev_info(rdev->dev, "fence driver on ring %d use gpu addr 0x%016jx and cpu addr 0x%p\n",
826 ring, (uintmax_t)rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
831 * radeon_fence_driver_init_ring - init the fence driver
832 * for the requested ring.
834 * @rdev: radeon device pointer
835 * @ring: ring index to start the fence driver on
837 * Init the fence driver for the requested ring (all asics).
838 * Helper function for radeon_fence_driver_init().
840 static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
844 rdev->fence_drv[ring].scratch_reg = -1;
845 rdev->fence_drv[ring].cpu_addr = NULL;
846 rdev->fence_drv[ring].gpu_addr = 0;
847 for (i = 0; i < RADEON_NUM_RINGS; ++i)
848 rdev->fence_drv[ring].sync_seq[i] = 0;
849 atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
850 rdev->fence_drv[ring].last_activity = jiffies;
851 rdev->fence_drv[ring].initialized = false;
855 * radeon_fence_driver_init - init the fence driver
856 * for all possible rings.
858 * @rdev: radeon device pointer
860 * Init the fence driver for all possible rings (all asics).
861 * Not all asics have all rings, so each asic will only
862 * start the fence driver on the rings it has using
863 * radeon_fence_driver_start_ring().
864 * Returns 0 for success.
866 int radeon_fence_driver_init(struct radeon_device *rdev)
870 lockinit(&rdev->fence_queue_mtx,
871 "drm__radeon_device__fence_queue_mtx", 0, LK_CANRECURSE);
872 cv_init(&rdev->fence_queue, "drm__radeon_device__fence_queue");
873 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
874 radeon_fence_driver_init_ring(rdev, ring);
876 if (radeon_debugfs_fence_init(rdev)) {
877 dev_err(rdev->dev, "fence debugfs file creation failed\n");
883 * radeon_fence_driver_fini - tear down the fence driver
884 * for all possible rings.
886 * @rdev: radeon device pointer
888 * Tear down the fence driver for all possible rings (all asics).
890 void radeon_fence_driver_fini(struct radeon_device *rdev)
894 lockmgr(&rdev->ring_lock, LK_EXCLUSIVE);
895 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
896 if (!rdev->fence_drv[ring].initialized)
898 r = radeon_fence_wait_empty_locked(rdev, ring);
900 /* no need to trigger GPU reset as we are unloading */
901 radeon_fence_driver_force_completion(rdev);
903 cv_broadcast(&rdev->fence_queue);
904 radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
905 rdev->fence_drv[ring].initialized = false;
906 cv_destroy(&rdev->fence_queue);
908 lockmgr(&rdev->ring_lock, LK_RELEASE);
912 * radeon_fence_driver_force_completion - force all fence waiter to complete
914 * @rdev: radeon device pointer
916 * In case of GPU reset failure make sure no process keep waiting on fence
917 * that will never complete.
919 void radeon_fence_driver_force_completion(struct radeon_device *rdev)
923 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
924 if (!rdev->fence_drv[ring].initialized)
926 radeon_fence_write(rdev, rdev->fence_drv[ring].sync_seq[ring], ring);
934 #if defined(CONFIG_DEBUG_FS)
935 static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
937 struct drm_info_node *node = (struct drm_info_node *)m->private;
938 struct drm_device *dev = node->minor->dev;
939 struct radeon_device *rdev = dev->dev_private;
942 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
943 if (!rdev->fence_drv[i].initialized)
946 seq_printf(m, "--- ring %d ---\n", i);
947 seq_printf(m, "Last signaled fence 0x%016llx\n",
948 (unsigned long long)atomic_load_acq_64(&rdev->fence_drv[i].last_seq));
949 seq_printf(m, "Last emitted 0x%016llx\n",
950 rdev->fence_drv[i].sync_seq[i]);
952 for (j = 0; j < RADEON_NUM_RINGS; ++j) {
953 if (i != j && rdev->fence_drv[j].initialized)
954 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
955 j, rdev->fence_drv[i].sync_seq[j]);
961 static struct drm_info_list radeon_debugfs_fence_list[] = {
962 {"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
966 int radeon_debugfs_fence_init(struct radeon_device *rdev)
968 #if defined(CONFIG_DEBUG_FS)
969 return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 1);