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>
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <dev/drm2/drmP.h>
36 #include "radeon_reg.h"
39 #include "radeon_trace.h"
40 #endif /* FREEBSD_WIP */
44 * Fences mark an event in the GPUs pipeline and are used
45 * for GPU/CPU synchronization. When the fence is written,
46 * it is expected that all buffers associated with that fence
47 * are no longer in use by the associated ring on the GPU and
48 * that the relevant GPU caches have been flushed. Whether
49 * we use a scratch register or memory location depends on the asic
50 * and whether writeback is enabled.
54 * radeon_fence_write - write a fence value
56 * @rdev: radeon_device pointer
57 * @seq: sequence number to write
58 * @ring: ring index the fence is associated with
60 * Writes a fence value to memory or a scratch register (all asics).
62 static void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
64 struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
65 if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
66 *drv->cpu_addr = cpu_to_le32(seq);
68 WREG32(drv->scratch_reg, seq);
73 * radeon_fence_read - read a fence value
75 * @rdev: radeon_device pointer
76 * @ring: ring index the fence is associated with
78 * Reads a fence value from memory or a scratch register (all asics).
79 * Returns the value of the fence read from memory or register.
81 static u32 radeon_fence_read(struct radeon_device *rdev, int ring)
83 struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
86 if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
87 seq = le32_to_cpu(*drv->cpu_addr);
89 seq = RREG32(drv->scratch_reg);
95 * radeon_fence_emit - emit a fence on the requested ring
97 * @rdev: radeon_device pointer
98 * @fence: radeon fence object
99 * @ring: ring index the fence is associated with
101 * Emits a fence command on the requested ring (all asics).
102 * Returns 0 on success, -ENOMEM on failure.
104 int radeon_fence_emit(struct radeon_device *rdev,
105 struct radeon_fence **fence,
108 /* we are protected by the ring emission mutex */
109 *fence = malloc(sizeof(struct radeon_fence), DRM_MEM_DRIVER, M_NOWAIT);
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);
118 CTR2(KTR_DRM, "radeon fence: emit (ring=%d, seq=%d)", ring, (*fence)->seq);
123 * radeon_fence_process - process a fence
125 * @rdev: radeon_device pointer
126 * @ring: ring index the fence is associated with
128 * Checks the current fence value and wakes the fence queue
129 * if the sequence number has increased (all asics).
131 void radeon_fence_process(struct radeon_device *rdev, int ring)
133 uint64_t seq, last_seq, last_emitted;
134 unsigned count_loop = 0;
137 /* Note there is a scenario here for an infinite loop but it's
138 * very unlikely to happen. For it to happen, the current polling
139 * process need to be interrupted by another process and another
140 * process needs to update the last_seq btw the atomic read and
141 * xchg of the current process.
143 * More over for this to go in infinite loop there need to be
144 * continuously new fence signaled ie radeon_fence_read needs
145 * to return a different value each time for both the currently
146 * polling process and the other process that xchg the last_seq
147 * btw atomic read and xchg of the current process. And the
148 * value the other process set as last seq must be higher than
149 * the seq value we just read. Which means that current process
150 * need to be interrupted after radeon_fence_read and before
153 * To be even more safe we count the number of time we loop and
154 * we bail after 10 loop just accepting the fact that we might
155 * have temporarly set the last_seq not to the true real last
156 * seq but to an older one.
158 last_seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
160 last_emitted = rdev->fence_drv[ring].sync_seq[ring];
161 seq = radeon_fence_read(rdev, ring);
162 seq |= last_seq & 0xffffffff00000000LL;
163 if (seq < last_seq) {
165 seq |= last_emitted & 0xffffffff00000000LL;
168 if (seq <= last_seq || seq > last_emitted) {
171 /* If we loop over we don't want to return without
172 * checking if a fence is signaled as it means that the
173 * seq we just read is different from the previous on.
177 if ((count_loop++) > 10) {
178 /* We looped over too many time leave with the
179 * fact that we might have set an older fence
180 * seq then the current real last seq as signaled
185 } while (atomic64_xchg(&rdev->fence_drv[ring].last_seq, seq) > seq);
188 rdev->fence_drv[ring].last_activity = jiffies;
189 cv_broadcast(&rdev->fence_queue);
194 * radeon_fence_destroy - destroy a fence
198 * Frees the fence object (all asics).
200 static void radeon_fence_destroy(struct radeon_fence *fence)
203 free(fence, DRM_MEM_DRIVER);
207 * radeon_fence_seq_signaled - check if a fence sequeuce number has signaled
209 * @rdev: radeon device pointer
210 * @seq: sequence number
211 * @ring: ring index the fence is associated with
213 * Check if the last singled fence sequnce number is >= the requested
214 * sequence number (all asics).
215 * Returns true if the fence has signaled (current fence value
216 * is >= requested value) or false if it has not (current fence
217 * value is < the requested value. Helper function for
218 * radeon_fence_signaled().
220 static bool radeon_fence_seq_signaled(struct radeon_device *rdev,
221 u64 seq, unsigned ring)
223 if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
226 /* poll new last sequence at least once */
227 radeon_fence_process(rdev, ring);
228 if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
235 * radeon_fence_signaled - check if a fence has signaled
237 * @fence: radeon fence object
239 * Check if the requested fence has signaled (all asics).
240 * Returns true if the fence has signaled or false if it has not.
242 bool radeon_fence_signaled(struct radeon_fence *fence)
247 if (fence->seq == RADEON_FENCE_SIGNALED_SEQ) {
250 if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
251 fence->seq = RADEON_FENCE_SIGNALED_SEQ;
258 * radeon_fence_wait_seq - wait for a specific sequence number
260 * @rdev: radeon device pointer
261 * @target_seq: sequence number we want to wait for
262 * @ring: ring index the fence is associated with
263 * @intr: use interruptable sleep
264 * @lock_ring: whether the ring should be locked or not
266 * Wait for the requested sequence number to be written (all asics).
267 * @intr selects whether to use interruptable (true) or non-interruptable
268 * (false) sleep when waiting for the sequence number. Helper function
269 * for radeon_fence_wait(), et al.
270 * Returns 0 if the sequence number has passed, error for all other cases.
271 * -EDEADLK is returned when a GPU lockup has been detected and the ring is
272 * marked as not ready so no further jobs get scheduled until a successful
275 static int radeon_fence_wait_seq(struct radeon_device *rdev, u64 target_seq,
276 unsigned ring, bool intr, bool lock_ring)
278 unsigned long timeout, last_activity;
281 bool signaled, fence_queue_locked;
284 while (target_seq > atomic64_read(&rdev->fence_drv[ring].last_seq)) {
285 if (!rdev->ring[ring].ready) {
289 timeout = jiffies - RADEON_FENCE_JIFFIES_TIMEOUT;
290 if (time_after(rdev->fence_drv[ring].last_activity, timeout)) {
291 /* the normal case, timeout is somewhere before last_activity */
292 timeout = rdev->fence_drv[ring].last_activity - timeout;
294 /* either jiffies wrapped around, or no fence was signaled in the last 500ms
295 * anyway we will just wait for the minimum amount and then check for a lockup
299 seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
300 /* Save current last activity valuee, used to check for GPU lockups */
301 last_activity = rdev->fence_drv[ring].last_activity;
303 CTR2(KTR_DRM, "radeon fence: wait begin (ring=%d, seq=%d)",
306 radeon_irq_kms_sw_irq_get(rdev, ring);
307 fence_queue_locked = false;
309 while (!(signaled = radeon_fence_seq_signaled(rdev,
310 target_seq, ring))) {
311 if (!fence_queue_locked) {
312 mtx_lock(&rdev->fence_queue_mtx);
313 fence_queue_locked = true;
316 r = cv_timedwait_sig(&rdev->fence_queue,
317 &rdev->fence_queue_mtx,
320 r = cv_timedwait(&rdev->fence_queue,
321 &rdev->fence_queue_mtx,
327 if (r == EWOULDBLOCK) {
329 radeon_fence_seq_signaled(
330 rdev, target_seq, ring);
335 if (fence_queue_locked) {
336 mtx_unlock(&rdev->fence_queue_mtx);
338 radeon_irq_kms_sw_irq_put(rdev, ring);
339 if (unlikely(r == ERESTARTSYS)) {
342 CTR2(KTR_DRM, "radeon fence: wait end (ring=%d, seq=%d)",
345 if (unlikely(!signaled)) {
347 /* we were interrupted for some reason and fence
348 * isn't signaled yet, resume waiting */
354 /* check if sequence value has changed since last_activity */
355 if (seq != atomic64_read(&rdev->fence_drv[ring].last_seq)) {
360 sx_xlock(&rdev->ring_lock);
363 /* test if somebody else has already decided that this is a lockup */
364 if (last_activity != rdev->fence_drv[ring].last_activity) {
366 sx_xunlock(&rdev->ring_lock);
371 if (radeon_ring_is_lockup(rdev, ring, &rdev->ring[ring])) {
372 /* good news we believe it's a lockup */
373 dev_warn(rdev->dev, "GPU lockup (waiting for 0x%016jx last fence id 0x%016jx)\n",
374 (uintmax_t)target_seq, (uintmax_t)seq);
376 /* change last activity so nobody else think there is a lockup */
377 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
378 rdev->fence_drv[i].last_activity = jiffies;
381 /* mark the ring as not ready any more */
382 rdev->ring[ring].ready = false;
384 sx_xunlock(&rdev->ring_lock);
390 sx_xunlock(&rdev->ring_lock);
398 * radeon_fence_wait - wait for a fence to signal
400 * @fence: radeon fence object
401 * @intr: use interruptable sleep
403 * Wait for the requested fence to signal (all asics).
404 * @intr selects whether to use interruptable (true) or non-interruptable
405 * (false) sleep when waiting for the fence.
406 * Returns 0 if the fence has passed, error for all other cases.
408 int radeon_fence_wait(struct radeon_fence *fence, bool intr)
413 DRM_ERROR("Querying an invalid fence : %p !\n", fence);
417 r = radeon_fence_wait_seq(fence->rdev, fence->seq,
418 fence->ring, intr, true);
422 fence->seq = RADEON_FENCE_SIGNALED_SEQ;
426 static bool radeon_fence_any_seq_signaled(struct radeon_device *rdev, u64 *seq)
430 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
431 if (seq[i] && radeon_fence_seq_signaled(rdev, seq[i], i)) {
439 * radeon_fence_wait_any_seq - wait for a sequence number on any ring
441 * @rdev: radeon device pointer
442 * @target_seq: sequence number(s) we want to wait for
443 * @intr: use interruptable sleep
445 * Wait for the requested sequence number(s) to be written by any ring
446 * (all asics). Sequnce number array is indexed by ring id.
447 * @intr selects whether to use interruptable (true) or non-interruptable
448 * (false) sleep when waiting for the sequence number. Helper function
449 * for radeon_fence_wait_any(), et al.
450 * Returns 0 if the sequence number has passed, error for all other cases.
452 static int radeon_fence_wait_any_seq(struct radeon_device *rdev,
453 u64 *target_seq, bool intr)
455 unsigned long timeout, last_activity, tmp;
456 unsigned i, ring = RADEON_NUM_RINGS;
457 bool signaled, fence_queue_locked;
460 for (i = 0, last_activity = 0; i < RADEON_NUM_RINGS; ++i) {
461 if (!target_seq[i]) {
465 /* use the most recent one as indicator */
466 if (time_after(rdev->fence_drv[i].last_activity, last_activity)) {
467 last_activity = rdev->fence_drv[i].last_activity;
470 /* For lockup detection just pick the lowest ring we are
471 * actively waiting for
478 /* nothing to wait for ? */
479 if (ring == RADEON_NUM_RINGS) {
483 while (!radeon_fence_any_seq_signaled(rdev, target_seq)) {
484 timeout = jiffies - RADEON_FENCE_JIFFIES_TIMEOUT;
485 if (time_after(last_activity, timeout)) {
486 /* the normal case, timeout is somewhere before last_activity */
487 timeout = last_activity - timeout;
489 /* either jiffies wrapped around, or no fence was signaled in the last 500ms
490 * anyway we will just wait for the minimum amount and then check for a lockup
495 CTR2(KTR_DRM, "radeon fence: wait begin (ring=%d, target_seq=%d)",
496 ring, target_seq[ring]);
497 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
499 radeon_irq_kms_sw_irq_get(rdev, i);
502 fence_queue_locked = false;
504 while (!(signaled = radeon_fence_any_seq_signaled(rdev,
506 if (!fence_queue_locked) {
507 mtx_lock(&rdev->fence_queue_mtx);
508 fence_queue_locked = true;
511 r = cv_timedwait_sig(&rdev->fence_queue,
512 &rdev->fence_queue_mtx,
515 r = cv_timedwait(&rdev->fence_queue,
516 &rdev->fence_queue_mtx,
522 if (r == EWOULDBLOCK) {
524 radeon_fence_any_seq_signaled(
530 if (fence_queue_locked) {
531 mtx_unlock(&rdev->fence_queue_mtx);
533 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
535 radeon_irq_kms_sw_irq_put(rdev, i);
538 if (unlikely(r == ERESTARTSYS)) {
541 CTR2(KTR_DRM, "radeon fence: wait end (ring=%d, target_seq=%d)",
542 ring, target_seq[ring]);
544 if (unlikely(!signaled)) {
546 /* we were interrupted for some reason and fence
547 * isn't signaled yet, resume waiting */
553 sx_xlock(&rdev->ring_lock);
554 for (i = 0, tmp = 0; i < RADEON_NUM_RINGS; ++i) {
555 if (time_after(rdev->fence_drv[i].last_activity, tmp)) {
556 tmp = rdev->fence_drv[i].last_activity;
559 /* test if somebody else has already decided that this is a lockup */
560 if (last_activity != tmp) {
562 sx_xunlock(&rdev->ring_lock);
566 if (radeon_ring_is_lockup(rdev, ring, &rdev->ring[ring])) {
567 /* good news we believe it's a lockup */
568 dev_warn(rdev->dev, "GPU lockup (waiting for 0x%016jx)\n",
569 (uintmax_t)target_seq[ring]);
571 /* change last activity so nobody else think there is a lockup */
572 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
573 rdev->fence_drv[i].last_activity = jiffies;
576 /* mark the ring as not ready any more */
577 rdev->ring[ring].ready = false;
578 sx_xunlock(&rdev->ring_lock);
581 sx_xunlock(&rdev->ring_lock);
588 * radeon_fence_wait_any - wait for a fence to signal on any ring
590 * @rdev: radeon device pointer
591 * @fences: radeon fence object(s)
592 * @intr: use interruptable sleep
594 * Wait for any requested fence to signal (all asics). Fence
595 * array is indexed by ring id. @intr selects whether to use
596 * interruptable (true) or non-interruptable (false) sleep when
597 * waiting for the fences. Used by the suballocator.
598 * Returns 0 if any fence has passed, error for all other cases.
600 int radeon_fence_wait_any(struct radeon_device *rdev,
601 struct radeon_fence **fences,
604 uint64_t seq[RADEON_NUM_RINGS];
608 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
615 if (fences[i]->seq == RADEON_FENCE_SIGNALED_SEQ) {
616 /* something was already signaled */
620 seq[i] = fences[i]->seq;
623 r = radeon_fence_wait_any_seq(rdev, seq, intr);
631 * radeon_fence_wait_next_locked - wait for the next fence to signal
633 * @rdev: radeon device pointer
634 * @ring: ring index the fence is associated with
636 * Wait for the next fence on the requested ring to signal (all asics).
637 * Returns 0 if the next fence has passed, error for all other cases.
638 * Caller must hold ring lock.
640 int radeon_fence_wait_next_locked(struct radeon_device *rdev, int ring)
644 seq = atomic64_read(&rdev->fence_drv[ring].last_seq) + 1ULL;
645 if (seq >= rdev->fence_drv[ring].sync_seq[ring]) {
646 /* nothing to wait for, last_seq is
647 already the last emitted fence */
650 return radeon_fence_wait_seq(rdev, seq, ring, false, false);
654 * radeon_fence_wait_empty_locked - wait for all fences to signal
656 * @rdev: radeon device pointer
657 * @ring: ring index the fence is associated with
659 * Wait for all fences on the requested ring to signal (all asics).
660 * Returns 0 if the fences have passed, error for all other cases.
661 * Caller must hold ring lock.
663 int radeon_fence_wait_empty_locked(struct radeon_device *rdev, int ring)
665 uint64_t seq = rdev->fence_drv[ring].sync_seq[ring];
668 r = radeon_fence_wait_seq(rdev, seq, ring, false, false);
673 dev_err(rdev->dev, "error waiting for ring[%d] to become idle (%d)\n",
680 * radeon_fence_ref - take a ref on a fence
682 * @fence: radeon fence object
684 * Take a reference on a fence (all asics).
687 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
689 refcount_acquire(&fence->kref);
694 * radeon_fence_unref - remove a ref on a fence
696 * @fence: radeon fence object
698 * Remove a reference on a fence (all asics).
700 void radeon_fence_unref(struct radeon_fence **fence)
702 struct radeon_fence *tmp = *fence;
706 if (refcount_release(&tmp->kref)) {
707 radeon_fence_destroy(tmp);
713 * radeon_fence_count_emitted - get the count of emitted fences
715 * @rdev: radeon device pointer
716 * @ring: ring index the fence is associated with
718 * Get the number of fences emitted on the requested ring (all asics).
719 * Returns the number of emitted fences on the ring. Used by the
720 * dynpm code to ring track activity.
722 unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
726 /* We are not protected by ring lock when reading the last sequence
727 * but it's ok to report slightly wrong fence count here.
729 radeon_fence_process(rdev, ring);
730 emitted = rdev->fence_drv[ring].sync_seq[ring]
731 - atomic64_read(&rdev->fence_drv[ring].last_seq);
732 /* to avoid 32bits warp around */
733 if (emitted > 0x10000000) {
734 emitted = 0x10000000;
736 return (unsigned)emitted;
740 * radeon_fence_need_sync - do we need a semaphore
742 * @fence: radeon fence object
743 * @dst_ring: which ring to check against
745 * Check if the fence needs to be synced against another ring
746 * (all asics). If so, we need to emit a semaphore.
747 * Returns true if we need to sync with another ring, false if
750 bool radeon_fence_need_sync(struct radeon_fence *fence, int dst_ring)
752 struct radeon_fence_driver *fdrv;
758 if (fence->ring == dst_ring) {
762 /* we are protected by the ring mutex */
763 fdrv = &fence->rdev->fence_drv[dst_ring];
764 if (fence->seq <= fdrv->sync_seq[fence->ring]) {
772 * radeon_fence_note_sync - record the sync point
774 * @fence: radeon fence object
775 * @dst_ring: which ring to check against
777 * Note the sequence number at which point the fence will
778 * be synced with the requested ring (all asics).
780 void radeon_fence_note_sync(struct radeon_fence *fence, int dst_ring)
782 struct radeon_fence_driver *dst, *src;
789 if (fence->ring == dst_ring) {
793 /* we are protected by the ring mutex */
794 src = &fence->rdev->fence_drv[fence->ring];
795 dst = &fence->rdev->fence_drv[dst_ring];
796 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
800 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
805 * radeon_fence_driver_start_ring - make the fence driver
806 * ready for use on the requested ring.
808 * @rdev: radeon device pointer
809 * @ring: ring index to start the fence driver on
811 * Make the fence driver ready for processing (all asics).
812 * Not all asics have all rings, so each asic will only
813 * start the fence driver on the rings it has.
814 * Returns 0 for success, errors for failure.
816 int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
821 radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
822 if (rdev->wb.use_event || !radeon_ring_supports_scratch_reg(rdev, &rdev->ring[ring])) {
823 rdev->fence_drv[ring].scratch_reg = 0;
824 index = R600_WB_EVENT_OFFSET + ring * 4;
826 r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
828 dev_err(rdev->dev, "fence failed to get scratch register\n");
831 index = RADEON_WB_SCRATCH_OFFSET +
832 rdev->fence_drv[ring].scratch_reg -
833 rdev->scratch.reg_base;
835 rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
836 rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
837 radeon_fence_write(rdev, atomic64_read(&rdev->fence_drv[ring].last_seq), ring);
838 rdev->fence_drv[ring].initialized = true;
839 dev_info(rdev->dev, "fence driver on ring %d use gpu addr 0x%016jx and cpu addr 0x%p\n",
840 ring, (uintmax_t)rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
845 * radeon_fence_driver_init_ring - init the fence driver
846 * for the requested ring.
848 * @rdev: radeon device pointer
849 * @ring: ring index to start the fence driver on
851 * Init the fence driver for the requested ring (all asics).
852 * Helper function for radeon_fence_driver_init().
854 static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
858 rdev->fence_drv[ring].scratch_reg = -1;
859 rdev->fence_drv[ring].cpu_addr = NULL;
860 rdev->fence_drv[ring].gpu_addr = 0;
861 for (i = 0; i < RADEON_NUM_RINGS; ++i)
862 rdev->fence_drv[ring].sync_seq[i] = 0;
863 atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
864 rdev->fence_drv[ring].last_activity = jiffies;
865 rdev->fence_drv[ring].initialized = false;
869 * radeon_fence_driver_init - init the fence driver
870 * for all possible rings.
872 * @rdev: radeon device pointer
874 * Init the fence driver for all possible rings (all asics).
875 * Not all asics have all rings, so each asic will only
876 * start the fence driver on the rings it has using
877 * radeon_fence_driver_start_ring().
878 * Returns 0 for success.
880 int radeon_fence_driver_init(struct radeon_device *rdev)
884 mtx_init(&rdev->fence_queue_mtx,
885 "drm__radeon_device__fence_queue_mtx", NULL, MTX_DEF);
886 cv_init(&rdev->fence_queue, "drm__radeon_device__fence_queue");
887 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
888 radeon_fence_driver_init_ring(rdev, ring);
890 if (radeon_debugfs_fence_init(rdev)) {
891 dev_err(rdev->dev, "fence debugfs file creation failed\n");
897 * radeon_fence_driver_fini - tear down the fence driver
898 * for all possible rings.
900 * @rdev: radeon device pointer
902 * Tear down the fence driver for all possible rings (all asics).
904 void radeon_fence_driver_fini(struct radeon_device *rdev)
908 sx_xlock(&rdev->ring_lock);
909 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
910 if (!rdev->fence_drv[ring].initialized)
912 r = radeon_fence_wait_empty_locked(rdev, ring);
914 /* no need to trigger GPU reset as we are unloading */
915 radeon_fence_driver_force_completion(rdev);
917 cv_broadcast(&rdev->fence_queue);
918 radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
919 rdev->fence_drv[ring].initialized = false;
920 cv_destroy(&rdev->fence_queue);
922 sx_xunlock(&rdev->ring_lock);
926 * radeon_fence_driver_force_completion - force all fence waiter to complete
928 * @rdev: radeon device pointer
930 * In case of GPU reset failure make sure no process keep waiting on fence
931 * that will never complete.
933 void radeon_fence_driver_force_completion(struct radeon_device *rdev)
937 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
938 if (!rdev->fence_drv[ring].initialized)
940 radeon_fence_write(rdev, rdev->fence_drv[ring].sync_seq[ring], ring);
948 #if defined(CONFIG_DEBUG_FS)
949 static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
951 struct drm_info_node *node = (struct drm_info_node *)m->private;
952 struct drm_device *dev = node->minor->dev;
953 struct radeon_device *rdev = dev->dev_private;
956 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
957 if (!rdev->fence_drv[i].initialized)
960 seq_printf(m, "--- ring %d ---\n", i);
961 seq_printf(m, "Last signaled fence 0x%016llx\n",
962 (unsigned long long)atomic64_read(&rdev->fence_drv[i].last_seq));
963 seq_printf(m, "Last emitted 0x%016llx\n",
964 rdev->fence_drv[i].sync_seq[i]);
966 for (j = 0; j < RADEON_NUM_RINGS; ++j) {
967 if (i != j && rdev->fence_drv[j].initialized)
968 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
969 j, rdev->fence_drv[i].sync_seq[j]);
975 static struct drm_info_list radeon_debugfs_fence_list[] = {
976 {"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
980 int radeon_debugfs_fence_init(struct radeon_device *rdev)
982 #if defined(CONFIG_DEBUG_FS)
983 return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 1);