xref: /dflybsd-src/sys/dev/drm/radeon/radeon_fence.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot  * Copyright 2009 Jerome Glisse.
3926deccbSFrançois Tigeot  * All Rights Reserved.
4926deccbSFrançois Tigeot  *
5926deccbSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
6926deccbSFrançois Tigeot  * copy of this software and associated documentation files (the
7926deccbSFrançois Tigeot  * "Software"), to deal in the Software without restriction, including
8926deccbSFrançois Tigeot  * without limitation the rights to use, copy, modify, merge, publish,
9926deccbSFrançois Tigeot  * distribute, sub license, and/or sell copies of the Software, and to
10926deccbSFrançois Tigeot  * permit persons to whom the Software is furnished to do so, subject to
11926deccbSFrançois Tigeot  * the following conditions:
12926deccbSFrançois Tigeot  *
13926deccbSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14926deccbSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15926deccbSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16926deccbSFrançois Tigeot  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17926deccbSFrançois Tigeot  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18926deccbSFrançois Tigeot  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19926deccbSFrançois Tigeot  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20926deccbSFrançois Tigeot  *
21926deccbSFrançois Tigeot  * The above copyright notice and this permission notice (including the
22926deccbSFrançois Tigeot  * next paragraph) shall be included in all copies or substantial portions
23926deccbSFrançois Tigeot  * of the Software.
24926deccbSFrançois Tigeot  *
25926deccbSFrançois Tigeot  */
26926deccbSFrançois Tigeot /*
27926deccbSFrançois Tigeot  * Authors:
28926deccbSFrançois Tigeot  *    Jerome Glisse <glisse@freedesktop.org>
29926deccbSFrançois Tigeot  *    Dave Airlie
30926deccbSFrançois Tigeot  */
311cfef1a5SFrançois Tigeot #include <linux/seq_file.h>
321cfef1a5SFrançois Tigeot #include <linux/atomic.h>
331cfef1a5SFrançois Tigeot #include <linux/wait.h>
341cfef1a5SFrançois Tigeot #include <linux/kref.h>
351cfef1a5SFrançois Tigeot #include <linux/firmware.h>
36926deccbSFrançois Tigeot #include <drm/drmP.h>
37926deccbSFrançois Tigeot #include "radeon_reg.h"
38926deccbSFrançois Tigeot #include "radeon.h"
39926deccbSFrançois Tigeot #include "radeon_trace.h"
40926deccbSFrançois Tigeot 
41926deccbSFrançois Tigeot /*
42926deccbSFrançois Tigeot  * Fences
43926deccbSFrançois Tigeot  * Fences mark an event in the GPUs pipeline and are used
44926deccbSFrançois Tigeot  * for GPU/CPU synchronization.  When the fence is written,
45926deccbSFrançois Tigeot  * it is expected that all buffers associated with that fence
46926deccbSFrançois Tigeot  * are no longer in use by the associated ring on the GPU and
47926deccbSFrançois Tigeot  * that the the relevant GPU caches have been flushed.  Whether
48926deccbSFrançois Tigeot  * we use a scratch register or memory location depends on the asic
49926deccbSFrançois Tigeot  * and whether writeback is enabled.
50926deccbSFrançois Tigeot  */
51926deccbSFrançois Tigeot 
52926deccbSFrançois Tigeot /**
53926deccbSFrançois Tigeot  * radeon_fence_write - write a fence value
54926deccbSFrançois Tigeot  *
55926deccbSFrançois Tigeot  * @rdev: radeon_device pointer
56926deccbSFrançois Tigeot  * @seq: sequence number to write
57926deccbSFrançois Tigeot  * @ring: ring index the fence is associated with
58926deccbSFrançois Tigeot  *
59926deccbSFrançois Tigeot  * Writes a fence value to memory or a scratch register (all asics).
60926deccbSFrançois Tigeot  */
radeon_fence_write(struct radeon_device * rdev,u32 seq,int ring)61926deccbSFrançois Tigeot static void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
62926deccbSFrançois Tigeot {
63926deccbSFrançois Tigeot 	struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
64926deccbSFrançois Tigeot 	if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
65f43cf1b1SMichael Neumann 		if (drv->cpu_addr) {
66926deccbSFrançois Tigeot 			*drv->cpu_addr = cpu_to_le32(seq);
67f43cf1b1SMichael Neumann 		}
68926deccbSFrançois Tigeot 	} else {
69926deccbSFrançois Tigeot 		WREG32(drv->scratch_reg, seq);
70926deccbSFrançois Tigeot 	}
71926deccbSFrançois Tigeot }
72926deccbSFrançois Tigeot 
73926deccbSFrançois Tigeot /**
74926deccbSFrançois Tigeot  * radeon_fence_read - read a fence value
75926deccbSFrançois Tigeot  *
76926deccbSFrançois Tigeot  * @rdev: radeon_device pointer
77926deccbSFrançois Tigeot  * @ring: ring index the fence is associated with
78926deccbSFrançois Tigeot  *
79926deccbSFrançois Tigeot  * Reads a fence value from memory or a scratch register (all asics).
80926deccbSFrançois Tigeot  * Returns the value of the fence read from memory or register.
81926deccbSFrançois Tigeot  */
radeon_fence_read(struct radeon_device * rdev,int ring)82926deccbSFrançois Tigeot static u32 radeon_fence_read(struct radeon_device *rdev, int ring)
83926deccbSFrançois Tigeot {
84926deccbSFrançois Tigeot 	struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
85926deccbSFrançois Tigeot 	u32 seq = 0;
86926deccbSFrançois Tigeot 
87926deccbSFrançois Tigeot 	if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
88f43cf1b1SMichael Neumann 		if (drv->cpu_addr) {
89926deccbSFrançois Tigeot 			seq = le32_to_cpu(*drv->cpu_addr);
90926deccbSFrançois Tigeot 		} else {
91f43cf1b1SMichael Neumann 			seq = lower_32_bits(atomic64_read(&drv->last_seq));
92f43cf1b1SMichael Neumann 		}
93f43cf1b1SMichael Neumann 	} else {
94926deccbSFrançois Tigeot 		seq = RREG32(drv->scratch_reg);
95926deccbSFrançois Tigeot 	}
96926deccbSFrançois Tigeot 	return seq;
97926deccbSFrançois Tigeot }
98926deccbSFrançois Tigeot 
99926deccbSFrançois Tigeot /**
100591d5043SFrançois Tigeot  * radeon_fence_schedule_check - schedule lockup check
101591d5043SFrançois Tigeot  *
102591d5043SFrançois Tigeot  * @rdev: radeon_device pointer
103591d5043SFrançois Tigeot  * @ring: ring index we should work with
104591d5043SFrançois Tigeot  *
105591d5043SFrançois Tigeot  * Queues a delayed work item to check for lockups.
106591d5043SFrançois Tigeot  */
radeon_fence_schedule_check(struct radeon_device * rdev,int ring)107591d5043SFrançois Tigeot static void radeon_fence_schedule_check(struct radeon_device *rdev, int ring)
108591d5043SFrançois Tigeot {
109591d5043SFrançois Tigeot 	/*
110591d5043SFrançois Tigeot 	 * Do not reset the timer here with mod_delayed_work,
111591d5043SFrançois Tigeot 	 * this can livelock in an interaction with TTM delayed destroy.
112591d5043SFrançois Tigeot 	 */
113591d5043SFrançois Tigeot 	queue_delayed_work(system_power_efficient_wq,
114591d5043SFrançois Tigeot 			   &rdev->fence_drv[ring].lockup_work,
115591d5043SFrançois Tigeot 			   RADEON_FENCE_JIFFIES_TIMEOUT);
116591d5043SFrançois Tigeot }
117591d5043SFrançois Tigeot 
118591d5043SFrançois Tigeot /**
119926deccbSFrançois Tigeot  * radeon_fence_emit - emit a fence on the requested ring
120926deccbSFrançois Tigeot  *
121926deccbSFrançois Tigeot  * @rdev: radeon_device pointer
122926deccbSFrançois Tigeot  * @fence: radeon fence object
123926deccbSFrançois Tigeot  * @ring: ring index the fence is associated with
124926deccbSFrançois Tigeot  *
125926deccbSFrançois Tigeot  * Emits a fence command on the requested ring (all asics).
126926deccbSFrançois Tigeot  * Returns 0 on success, -ENOMEM on failure.
127926deccbSFrançois Tigeot  */
radeon_fence_emit(struct radeon_device * rdev,struct radeon_fence ** fence,int ring)128926deccbSFrançois Tigeot int radeon_fence_emit(struct radeon_device *rdev,
129926deccbSFrançois Tigeot 		      struct radeon_fence **fence,
130926deccbSFrançois Tigeot 		      int ring)
131926deccbSFrançois Tigeot {
132d78d3a22SFrançois Tigeot 	u64 seq;
1331cfef1a5SFrançois Tigeot 
134926deccbSFrançois Tigeot 	/* we are protected by the ring emission mutex */
1351cfef1a5SFrançois Tigeot 	*fence = kmalloc(sizeof(struct radeon_fence), M_DRM, M_WAITOK);
136926deccbSFrançois Tigeot 	if ((*fence) == NULL) {
137926deccbSFrançois Tigeot 		return -ENOMEM;
138926deccbSFrançois Tigeot 	}
139926deccbSFrançois Tigeot 	(*fence)->rdev = rdev;
140d78d3a22SFrançois Tigeot 	(*fence)->seq = seq = ++rdev->fence_drv[ring].sync_seq[ring];
141926deccbSFrançois Tigeot 	(*fence)->ring = ring;
1427dcf36dcSFrançois Tigeot 	(*fence)->is_vm_update = false;
1436559babbSFrançois Tigeot 	dma_fence_init(&(*fence)->base, &radeon_fence_ops,
1446559babbSFrançois Tigeot 		       &rdev->fence_queue.lock,
1456559babbSFrançois Tigeot 		       rdev->fence_context + ring,
1466559babbSFrançois Tigeot 		       seq);
147926deccbSFrançois Tigeot 	radeon_fence_ring_emit(rdev, ring, *fence);
148c6f73aabSFrançois Tigeot 	trace_radeon_fence_emit(rdev->ddev, ring, (*fence)->seq);
149591d5043SFrançois Tigeot 	radeon_fence_schedule_check(rdev, ring);
150926deccbSFrançois Tigeot 	return 0;
151926deccbSFrançois Tigeot }
152926deccbSFrançois Tigeot 
153926deccbSFrançois Tigeot /**
1541cfef1a5SFrançois Tigeot  * radeon_fence_check_signaled - callback from fence_queue
1551cfef1a5SFrançois Tigeot  *
1561cfef1a5SFrançois Tigeot  * this function is called with fence_queue lock held, which is also used
1571cfef1a5SFrançois Tigeot  * for the fence locking itself, so unlocked variants are used for
1581cfef1a5SFrançois Tigeot  * fence_signal, and remove_wait_queue.
1591cfef1a5SFrançois Tigeot  */
radeon_fence_check_signaled(wait_queue_entry_t * wait,unsigned mode,int flags,void * key)160*3f2dd94aSFrançois Tigeot static int radeon_fence_check_signaled(wait_queue_entry_t *wait, unsigned mode, int flags, void *key)
1611cfef1a5SFrançois Tigeot {
1621cfef1a5SFrançois Tigeot 	struct radeon_fence *fence;
1631cfef1a5SFrançois Tigeot 	u64 seq;
1641cfef1a5SFrançois Tigeot 
1651cfef1a5SFrançois Tigeot 	fence = container_of(wait, struct radeon_fence, fence_wake);
1661cfef1a5SFrançois Tigeot 
1671cfef1a5SFrançois Tigeot 	/*
1681cfef1a5SFrançois Tigeot 	 * We cannot use radeon_fence_process here because we're already
1691cfef1a5SFrançois Tigeot 	 * in the waitqueue, in a call from wake_up_all.
1701cfef1a5SFrançois Tigeot 	 */
1711cfef1a5SFrançois Tigeot 	seq = atomic64_read(&fence->rdev->fence_drv[fence->ring].last_seq);
1721cfef1a5SFrançois Tigeot 	if (seq >= fence->seq) {
1736559babbSFrançois Tigeot 		int ret = dma_fence_signal_locked(&fence->base);
1741cfef1a5SFrançois Tigeot 
1751cfef1a5SFrançois Tigeot 		if (!ret)
1766559babbSFrançois Tigeot 			DMA_FENCE_TRACE(&fence->base, "signaled from irq context\n");
1771cfef1a5SFrançois Tigeot 		else
1786559babbSFrançois Tigeot 			DMA_FENCE_TRACE(&fence->base, "was already signaled\n");
1791cfef1a5SFrançois Tigeot 
1801cfef1a5SFrançois Tigeot 		radeon_irq_kms_sw_irq_put(fence->rdev, fence->ring);
1811cfef1a5SFrançois Tigeot 		__remove_wait_queue(&fence->rdev->fence_queue, &fence->fence_wake);
1826559babbSFrançois Tigeot 		dma_fence_put(&fence->base);
1831cfef1a5SFrançois Tigeot 	} else
1846559babbSFrançois Tigeot 		DMA_FENCE_TRACE(&fence->base, "pending\n");
1851cfef1a5SFrançois Tigeot 	return 0;
1861cfef1a5SFrançois Tigeot }
1871cfef1a5SFrançois Tigeot 
1881cfef1a5SFrançois Tigeot /**
189591d5043SFrançois Tigeot  * radeon_fence_activity - check for fence activity
190926deccbSFrançois Tigeot  *
191926deccbSFrançois Tigeot  * @rdev: radeon_device pointer
192926deccbSFrançois Tigeot  * @ring: ring index the fence is associated with
193926deccbSFrançois Tigeot  *
194591d5043SFrançois Tigeot  * Checks the current fence value and calculates the last
195591d5043SFrançois Tigeot  * signalled fence value. Returns true if activity occured
196591d5043SFrançois Tigeot  * on the ring, and the fence_queue should be waken up.
197926deccbSFrançois Tigeot  */
radeon_fence_activity(struct radeon_device * rdev,int ring)198591d5043SFrançois Tigeot static bool radeon_fence_activity(struct radeon_device *rdev, int ring)
199926deccbSFrançois Tigeot {
200926deccbSFrançois Tigeot 	uint64_t seq, last_seq, last_emitted;
201926deccbSFrançois Tigeot 	unsigned count_loop = 0;
202926deccbSFrançois Tigeot 	bool wake = false;
203926deccbSFrançois Tigeot 
204926deccbSFrançois Tigeot 	/* Note there is a scenario here for an infinite loop but it's
205926deccbSFrançois Tigeot 	 * very unlikely to happen. For it to happen, the current polling
206926deccbSFrançois Tigeot 	 * process need to be interrupted by another process and another
207926deccbSFrançois Tigeot 	 * process needs to update the last_seq btw the atomic read and
208926deccbSFrançois Tigeot 	 * xchg of the current process.
209926deccbSFrançois Tigeot 	 *
210926deccbSFrançois Tigeot 	 * More over for this to go in infinite loop there need to be
211926deccbSFrançois Tigeot 	 * continuously new fence signaled ie radeon_fence_read needs
212926deccbSFrançois Tigeot 	 * to return a different value each time for both the currently
213926deccbSFrançois Tigeot 	 * polling process and the other process that xchg the last_seq
214926deccbSFrançois Tigeot 	 * btw atomic read and xchg of the current process. And the
215926deccbSFrançois Tigeot 	 * value the other process set as last seq must be higher than
216926deccbSFrançois Tigeot 	 * the seq value we just read. Which means that current process
217926deccbSFrançois Tigeot 	 * need to be interrupted after radeon_fence_read and before
218926deccbSFrançois Tigeot 	 * atomic xchg.
219926deccbSFrançois Tigeot 	 *
220926deccbSFrançois Tigeot 	 * To be even more safe we count the number of time we loop and
221926deccbSFrançois Tigeot 	 * we bail after 10 loop just accepting the fact that we might
222926deccbSFrançois Tigeot 	 * have temporarly set the last_seq not to the true real last
223926deccbSFrançois Tigeot 	 * seq but to an older one.
224926deccbSFrançois Tigeot 	 */
225e3b244c9SFrançois Tigeot 	last_seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
226926deccbSFrançois Tigeot 	do {
227926deccbSFrançois Tigeot 		last_emitted = rdev->fence_drv[ring].sync_seq[ring];
228926deccbSFrançois Tigeot 		seq = radeon_fence_read(rdev, ring);
229926deccbSFrançois Tigeot 		seq |= last_seq & 0xffffffff00000000LL;
230926deccbSFrançois Tigeot 		if (seq < last_seq) {
231926deccbSFrançois Tigeot 			seq &= 0xffffffff;
232926deccbSFrançois Tigeot 			seq |= last_emitted & 0xffffffff00000000LL;
233926deccbSFrançois Tigeot 		}
234926deccbSFrançois Tigeot 
235926deccbSFrançois Tigeot 		if (seq <= last_seq || seq > last_emitted) {
236926deccbSFrançois Tigeot 			break;
237926deccbSFrançois Tigeot 		}
238926deccbSFrançois Tigeot 		/* If we loop over we don't want to return without
239926deccbSFrançois Tigeot 		 * checking if a fence is signaled as it means that the
240926deccbSFrançois Tigeot 		 * seq we just read is different from the previous on.
241926deccbSFrançois Tigeot 		 */
242926deccbSFrançois Tigeot 		wake = true;
243926deccbSFrançois Tigeot 		last_seq = seq;
244926deccbSFrançois Tigeot 		if ((count_loop++) > 10) {
245926deccbSFrançois Tigeot 			/* We looped over too many time leave with the
246926deccbSFrançois Tigeot 			 * fact that we might have set an older fence
247926deccbSFrançois Tigeot 			 * seq then the current real last seq as signaled
248926deccbSFrançois Tigeot 			 * by the hw.
249926deccbSFrançois Tigeot 			 */
250926deccbSFrançois Tigeot 			break;
251926deccbSFrançois Tigeot 		}
252926deccbSFrançois Tigeot 	} while (atomic64_xchg(&rdev->fence_drv[ring].last_seq, seq) > seq);
253926deccbSFrançois Tigeot 
254591d5043SFrançois Tigeot 	if (seq < last_emitted)
255591d5043SFrançois Tigeot 		radeon_fence_schedule_check(rdev, ring);
256591d5043SFrançois Tigeot 
257591d5043SFrançois Tigeot 	return wake;
258591d5043SFrançois Tigeot }
259591d5043SFrançois Tigeot 
260591d5043SFrançois Tigeot /**
261591d5043SFrançois Tigeot  * radeon_fence_check_lockup - check for hardware lockup
262591d5043SFrançois Tigeot  *
263591d5043SFrançois Tigeot  * @work: delayed work item
264591d5043SFrançois Tigeot  *
265591d5043SFrançois Tigeot  * Checks for fence activity and if there is none probe
266591d5043SFrançois Tigeot  * the hardware if a lockup occured.
267591d5043SFrançois Tigeot  */
radeon_fence_check_lockup(struct work_struct * work)268591d5043SFrançois Tigeot static void radeon_fence_check_lockup(struct work_struct *work)
269591d5043SFrançois Tigeot {
270591d5043SFrançois Tigeot 	struct radeon_fence_driver *fence_drv;
271591d5043SFrançois Tigeot 	struct radeon_device *rdev;
272591d5043SFrançois Tigeot 	int ring;
273591d5043SFrançois Tigeot 
274591d5043SFrançois Tigeot 	fence_drv = container_of(work, struct radeon_fence_driver,
275591d5043SFrançois Tigeot 				 lockup_work.work);
276591d5043SFrançois Tigeot 	rdev = fence_drv->rdev;
277591d5043SFrançois Tigeot 	ring = fence_drv - &rdev->fence_drv[0];
278591d5043SFrançois Tigeot 
2791cfef1a5SFrançois Tigeot 	if (!down_read_trylock(&rdev->exclusive_lock)) {
280591d5043SFrançois Tigeot 		/* just reschedule the check if a reset is going on */
281591d5043SFrançois Tigeot 		radeon_fence_schedule_check(rdev, ring);
282591d5043SFrançois Tigeot 		return;
283591d5043SFrançois Tigeot 	}
284591d5043SFrançois Tigeot 
2851cfef1a5SFrançois Tigeot 	if (fence_drv->delayed_irq && rdev->ddev->irq_enabled) {
2861cfef1a5SFrançois Tigeot 		unsigned long irqflags;
2871cfef1a5SFrançois Tigeot 
2881cfef1a5SFrançois Tigeot 		fence_drv->delayed_irq = false;
2891cfef1a5SFrançois Tigeot 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
2901cfef1a5SFrançois Tigeot 		radeon_irq_set(rdev);
2911cfef1a5SFrançois Tigeot 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
2921cfef1a5SFrançois Tigeot 	}
2931cfef1a5SFrançois Tigeot 
294591d5043SFrançois Tigeot 	if (radeon_fence_activity(rdev, ring))
295591d5043SFrançois Tigeot 		wake_up_all(&rdev->fence_queue);
296591d5043SFrançois Tigeot 
297591d5043SFrançois Tigeot 	else if (radeon_ring_is_lockup(rdev, ring, &rdev->ring[ring])) {
298591d5043SFrançois Tigeot 
299591d5043SFrançois Tigeot 		/* good news we believe it's a lockup */
300591d5043SFrançois Tigeot 		dev_warn(rdev->dev, "GPU lockup (current fence id "
301591d5043SFrançois Tigeot 			 "0x%016lx last fence id 0x%016lx on ring %d)\n",
302591d5043SFrançois Tigeot 			 (uint64_t)atomic64_read(&fence_drv->last_seq),
303591d5043SFrançois Tigeot 			 fence_drv->sync_seq[ring], ring);
304591d5043SFrançois Tigeot 
305591d5043SFrançois Tigeot 		/* remember that we need an reset */
306591d5043SFrançois Tigeot 		rdev->needs_reset = true;
307591d5043SFrançois Tigeot 		wake_up_all(&rdev->fence_queue);
308591d5043SFrançois Tigeot 	}
3091cfef1a5SFrançois Tigeot 	up_read(&rdev->exclusive_lock);
310591d5043SFrançois Tigeot }
311591d5043SFrançois Tigeot 
312591d5043SFrançois Tigeot /**
313591d5043SFrançois Tigeot  * radeon_fence_process - process a fence
314591d5043SFrançois Tigeot  *
315591d5043SFrançois Tigeot  * @rdev: radeon_device pointer
316591d5043SFrançois Tigeot  * @ring: ring index the fence is associated with
317591d5043SFrançois Tigeot  *
318591d5043SFrançois Tigeot  * Checks the current fence value and wakes the fence queue
319591d5043SFrançois Tigeot  * if the sequence number has increased (all asics).
320591d5043SFrançois Tigeot  */
radeon_fence_process(struct radeon_device * rdev,int ring)321591d5043SFrançois Tigeot void radeon_fence_process(struct radeon_device *rdev, int ring)
322591d5043SFrançois Tigeot {
323591d5043SFrançois Tigeot 	if (radeon_fence_activity(rdev, ring))
3240d5a30c1SFrançois Tigeot 		wake_up_all(&rdev->fence_queue);
325926deccbSFrançois Tigeot }
326926deccbSFrançois Tigeot 
327926deccbSFrançois Tigeot /**
328c6f73aabSFrançois Tigeot  * radeon_fence_seq_signaled - check if a fence sequence number has signaled
329926deccbSFrançois Tigeot  *
330926deccbSFrançois Tigeot  * @rdev: radeon device pointer
331926deccbSFrançois Tigeot  * @seq: sequence number
332926deccbSFrançois Tigeot  * @ring: ring index the fence is associated with
333926deccbSFrançois Tigeot  *
334c6f73aabSFrançois Tigeot  * Check if the last signaled fence sequnce number is >= the requested
335926deccbSFrançois Tigeot  * sequence number (all asics).
336926deccbSFrançois Tigeot  * Returns true if the fence has signaled (current fence value
337926deccbSFrançois Tigeot  * is >= requested value) or false if it has not (current fence
338926deccbSFrançois Tigeot  * value is < the requested value.  Helper function for
339926deccbSFrançois Tigeot  * radeon_fence_signaled().
340926deccbSFrançois Tigeot  */
radeon_fence_seq_signaled(struct radeon_device * rdev,u64 seq,unsigned ring)341926deccbSFrançois Tigeot static bool radeon_fence_seq_signaled(struct radeon_device *rdev,
342926deccbSFrançois Tigeot 				      u64 seq, unsigned ring)
343926deccbSFrançois Tigeot {
344e3b244c9SFrançois Tigeot 	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
345926deccbSFrançois Tigeot 		return true;
346926deccbSFrançois Tigeot 	}
347926deccbSFrançois Tigeot 	/* poll new last sequence at least once */
348926deccbSFrançois Tigeot 	radeon_fence_process(rdev, ring);
349e3b244c9SFrançois Tigeot 	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
350926deccbSFrançois Tigeot 		return true;
351926deccbSFrançois Tigeot 	}
352926deccbSFrançois Tigeot 	return false;
353926deccbSFrançois Tigeot }
354926deccbSFrançois Tigeot 
radeon_fence_is_signaled(struct dma_fence * f)3556559babbSFrançois Tigeot static bool radeon_fence_is_signaled(struct dma_fence *f)
3561cfef1a5SFrançois Tigeot {
3571cfef1a5SFrançois Tigeot 	struct radeon_fence *fence = to_radeon_fence(f);
3581cfef1a5SFrançois Tigeot 	struct radeon_device *rdev = fence->rdev;
3591cfef1a5SFrançois Tigeot 	unsigned ring = fence->ring;
3601cfef1a5SFrançois Tigeot 	u64 seq = fence->seq;
3611cfef1a5SFrançois Tigeot 
3621cfef1a5SFrançois Tigeot 	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
3631cfef1a5SFrançois Tigeot 		return true;
3641cfef1a5SFrançois Tigeot 	}
3651cfef1a5SFrançois Tigeot 
3661cfef1a5SFrançois Tigeot 	if (down_read_trylock(&rdev->exclusive_lock)) {
3671cfef1a5SFrançois Tigeot 		radeon_fence_process(rdev, ring);
3681cfef1a5SFrançois Tigeot 		up_read(&rdev->exclusive_lock);
3691cfef1a5SFrançois Tigeot 
3701cfef1a5SFrançois Tigeot 		if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
3711cfef1a5SFrançois Tigeot 			return true;
3721cfef1a5SFrançois Tigeot 		}
3731cfef1a5SFrançois Tigeot 	}
3741cfef1a5SFrançois Tigeot 	return false;
3751cfef1a5SFrançois Tigeot }
3761cfef1a5SFrançois Tigeot 
3771cfef1a5SFrançois Tigeot /**
3781cfef1a5SFrançois Tigeot  * radeon_fence_enable_signaling - enable signalling on fence
3791cfef1a5SFrançois Tigeot  * @fence: fence
3801cfef1a5SFrançois Tigeot  *
3811cfef1a5SFrançois Tigeot  * This function is called with fence_queue lock held, and adds a callback
3821cfef1a5SFrançois Tigeot  * to fence_queue that checks if this fence is signaled, and if so it
3831cfef1a5SFrançois Tigeot  * signals the fence and removes itself.
3841cfef1a5SFrançois Tigeot  */
radeon_fence_enable_signaling(struct dma_fence * f)3856559babbSFrançois Tigeot static bool radeon_fence_enable_signaling(struct dma_fence *f)
3861cfef1a5SFrançois Tigeot {
3871cfef1a5SFrançois Tigeot 	struct radeon_fence *fence = to_radeon_fence(f);
3881cfef1a5SFrançois Tigeot 	struct radeon_device *rdev = fence->rdev;
3891cfef1a5SFrançois Tigeot 
3901cfef1a5SFrançois Tigeot 	if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq)
3911cfef1a5SFrançois Tigeot 		return false;
3921cfef1a5SFrançois Tigeot 
3931cfef1a5SFrançois Tigeot 	if (down_read_trylock(&rdev->exclusive_lock)) {
3941cfef1a5SFrançois Tigeot 		radeon_irq_kms_sw_irq_get(rdev, fence->ring);
3951cfef1a5SFrançois Tigeot 
3961cfef1a5SFrançois Tigeot 		if (radeon_fence_activity(rdev, fence->ring))
3971cfef1a5SFrançois Tigeot 			wake_up_all_locked(&rdev->fence_queue);
3981cfef1a5SFrançois Tigeot 
3991cfef1a5SFrançois Tigeot 		/* did fence get signaled after we enabled the sw irq? */
4001cfef1a5SFrançois Tigeot 		if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq) {
4011cfef1a5SFrançois Tigeot 			radeon_irq_kms_sw_irq_put(rdev, fence->ring);
4021cfef1a5SFrançois Tigeot 			up_read(&rdev->exclusive_lock);
4031cfef1a5SFrançois Tigeot 			return false;
4041cfef1a5SFrançois Tigeot 		}
4051cfef1a5SFrançois Tigeot 
4061cfef1a5SFrançois Tigeot 		up_read(&rdev->exclusive_lock);
4071cfef1a5SFrançois Tigeot 	} else {
4081cfef1a5SFrançois Tigeot 		/* we're probably in a lockup, lets not fiddle too much */
4091cfef1a5SFrançois Tigeot 		if (radeon_irq_kms_sw_irq_get_delayed(rdev, fence->ring))
4101cfef1a5SFrançois Tigeot 			rdev->fence_drv[fence->ring].delayed_irq = true;
4111cfef1a5SFrançois Tigeot 		radeon_fence_schedule_check(rdev, fence->ring);
4121cfef1a5SFrançois Tigeot 	}
4131cfef1a5SFrançois Tigeot 
4141cfef1a5SFrançois Tigeot 	fence->fence_wake.flags = 0;
4151cfef1a5SFrançois Tigeot 	fence->fence_wake.private = NULL;
4161cfef1a5SFrançois Tigeot 	fence->fence_wake.func = radeon_fence_check_signaled;
4171cfef1a5SFrançois Tigeot 	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
4186559babbSFrançois Tigeot 	dma_fence_get(f);
4191cfef1a5SFrançois Tigeot 
4206559babbSFrançois Tigeot 	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", fence->ring);
4211cfef1a5SFrançois Tigeot 	return true;
4221cfef1a5SFrançois Tigeot }
4231cfef1a5SFrançois Tigeot 
424926deccbSFrançois Tigeot /**
425926deccbSFrançois Tigeot  * radeon_fence_signaled - check if a fence has signaled
426926deccbSFrançois Tigeot  *
427926deccbSFrançois Tigeot  * @fence: radeon fence object
428926deccbSFrançois Tigeot  *
429926deccbSFrançois Tigeot  * Check if the requested fence has signaled (all asics).
430926deccbSFrançois Tigeot  * Returns true if the fence has signaled or false if it has not.
431926deccbSFrançois Tigeot  */
radeon_fence_signaled(struct radeon_fence * fence)432926deccbSFrançois Tigeot bool radeon_fence_signaled(struct radeon_fence *fence)
433926deccbSFrançois Tigeot {
434591d5043SFrançois Tigeot 	if (!fence)
435926deccbSFrançois Tigeot 		return true;
4361cfef1a5SFrançois Tigeot 
4371cfef1a5SFrançois Tigeot 	if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
4381cfef1a5SFrançois Tigeot 		int ret;
4391cfef1a5SFrançois Tigeot 
4406559babbSFrançois Tigeot 		ret = dma_fence_signal(&fence->base);
4411cfef1a5SFrançois Tigeot 		if (!ret)
4426559babbSFrançois Tigeot 			DMA_FENCE_TRACE(&fence->base, "signaled from radeon_fence_signaled\n");
443926deccbSFrançois Tigeot 		return true;
4441cfef1a5SFrançois Tigeot 	}
445926deccbSFrançois Tigeot 	return false;
446926deccbSFrançois Tigeot }
447926deccbSFrançois Tigeot 
448926deccbSFrançois Tigeot /**
449c6f73aabSFrançois Tigeot  * radeon_fence_any_seq_signaled - check if any sequence number is signaled
450926deccbSFrançois Tigeot  *
451926deccbSFrançois Tigeot  * @rdev: radeon device pointer
452c6f73aabSFrançois Tigeot  * @seq: sequence numbers
453926deccbSFrançois Tigeot  *
454c6f73aabSFrançois Tigeot  * Check if the last signaled fence sequnce number is >= the requested
455c6f73aabSFrançois Tigeot  * sequence number (all asics).
456c6f73aabSFrançois Tigeot  * Returns true if any has signaled (current value is >= requested value)
457c6f73aabSFrançois Tigeot  * or false if it has not. Helper function for radeon_fence_wait_seq.
458c6f73aabSFrançois Tigeot  */
radeon_fence_any_seq_signaled(struct radeon_device * rdev,u64 * seq)459c6f73aabSFrançois Tigeot static bool radeon_fence_any_seq_signaled(struct radeon_device *rdev, u64 *seq)
460c6f73aabSFrançois Tigeot {
461c6f73aabSFrançois Tigeot 	unsigned i;
462c6f73aabSFrançois Tigeot 
463c6f73aabSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
464c6f73aabSFrançois Tigeot 		if (seq[i] && radeon_fence_seq_signaled(rdev, seq[i], i))
465c6f73aabSFrançois Tigeot 			return true;
466c6f73aabSFrançois Tigeot 	}
467c6f73aabSFrançois Tigeot 	return false;
468c6f73aabSFrançois Tigeot }
469c6f73aabSFrançois Tigeot 
470c6f73aabSFrançois Tigeot /**
471591d5043SFrançois Tigeot  * radeon_fence_wait_seq_timeout - wait for a specific sequence numbers
472c6f73aabSFrançois Tigeot  *
473c6f73aabSFrançois Tigeot  * @rdev: radeon device pointer
474c6f73aabSFrançois Tigeot  * @target_seq: sequence number(s) we want to wait for
475c6f73aabSFrançois Tigeot  * @intr: use interruptable sleep
476591d5043SFrançois Tigeot  * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
477c6f73aabSFrançois Tigeot  *
478c6f73aabSFrançois Tigeot  * Wait for the requested sequence number(s) to be written by any ring
479c6f73aabSFrançois Tigeot  * (all asics).  Sequnce number array is indexed by ring id.
480926deccbSFrançois Tigeot  * @intr selects whether to use interruptable (true) or non-interruptable
481926deccbSFrançois Tigeot  * (false) sleep when waiting for the sequence number.  Helper function
482c6f73aabSFrançois Tigeot  * for radeon_fence_wait_*().
483591d5043SFrançois Tigeot  * Returns remaining time if the sequence number has passed, 0 when
484591d5043SFrançois Tigeot  * the wait timeout, or an error for all other cases.
485c6f73aabSFrançois Tigeot  * -EDEADLK is returned when a GPU lockup has been detected.
486926deccbSFrançois Tigeot  */
radeon_fence_wait_seq_timeout(struct radeon_device * rdev,u64 * target_seq,bool intr,long timeout)4871cfef1a5SFrançois Tigeot static long radeon_fence_wait_seq_timeout(struct radeon_device *rdev,
488591d5043SFrançois Tigeot 					  u64 *target_seq, bool intr,
4891cfef1a5SFrançois Tigeot 					  long timeout)
490926deccbSFrançois Tigeot {
491591d5043SFrançois Tigeot 	long r;
492591d5043SFrançois Tigeot 	int i;
493926deccbSFrançois Tigeot 
494591d5043SFrançois Tigeot 	if (radeon_fence_any_seq_signaled(rdev, target_seq))
495591d5043SFrançois Tigeot 		return timeout;
496c6f73aabSFrançois Tigeot 
497591d5043SFrançois Tigeot 	/* enable IRQs and tracing */
498c6f73aabSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
499c6f73aabSFrançois Tigeot 		if (!target_seq[i])
500c6f73aabSFrançois Tigeot 			continue;
501c6f73aabSFrançois Tigeot 
502c6f73aabSFrançois Tigeot 		trace_radeon_fence_wait_begin(rdev->ddev, i, target_seq[i]);
503c6f73aabSFrançois Tigeot 		radeon_irq_kms_sw_irq_get(rdev, i);
504926deccbSFrançois Tigeot 	}
505926deccbSFrançois Tigeot 
506926deccbSFrançois Tigeot 	if (intr) {
507c6f73aabSFrançois Tigeot 		r = wait_event_interruptible_timeout(rdev->fence_queue, (
508591d5043SFrançois Tigeot 			radeon_fence_any_seq_signaled(rdev, target_seq)
509591d5043SFrançois Tigeot 			 || rdev->needs_reset), timeout);
510926deccbSFrançois Tigeot 	} else {
511c6f73aabSFrançois Tigeot 		r = wait_event_timeout(rdev->fence_queue, (
512591d5043SFrançois Tigeot 			radeon_fence_any_seq_signaled(rdev, target_seq)
513591d5043SFrançois Tigeot 			 || rdev->needs_reset), timeout);
514926deccbSFrançois Tigeot 	}
515c6f73aabSFrançois Tigeot 
516591d5043SFrançois Tigeot 	if (rdev->needs_reset)
517591d5043SFrançois Tigeot 		r = -EDEADLK;
518591d5043SFrançois Tigeot 
519c6f73aabSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
520c6f73aabSFrançois Tigeot 		if (!target_seq[i])
521c6f73aabSFrançois Tigeot 			continue;
522c6f73aabSFrançois Tigeot 
523c6f73aabSFrançois Tigeot 		radeon_irq_kms_sw_irq_put(rdev, i);
524c6f73aabSFrançois Tigeot 		trace_radeon_fence_wait_end(rdev->ddev, i, target_seq[i]);
525c6f73aabSFrançois Tigeot 	}
526c6f73aabSFrançois Tigeot 
5270d5a30c1SFrançois Tigeot 	return r;
528926deccbSFrançois Tigeot }
529926deccbSFrançois Tigeot 
530926deccbSFrançois Tigeot /**
531d78d3a22SFrançois Tigeot  * radeon_fence_wait_timeout - wait for a fence to signal with timeout
532d78d3a22SFrançois Tigeot  *
533d78d3a22SFrançois Tigeot  * @fence: radeon fence object
534d78d3a22SFrançois Tigeot  * @intr: use interruptible sleep
535d78d3a22SFrançois Tigeot  *
536d78d3a22SFrançois Tigeot  * Wait for the requested fence to signal (all asics).
537d78d3a22SFrançois Tigeot  * @intr selects whether to use interruptable (true) or non-interruptable
538d78d3a22SFrançois Tigeot  * (false) sleep when waiting for the fence.
539d78d3a22SFrançois Tigeot  * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
540d78d3a22SFrançois Tigeot  * Returns remaining time if the sequence number has passed, 0 when
541d78d3a22SFrançois Tigeot  * the wait timeout, or an error for all other cases.
542d78d3a22SFrançois Tigeot  */
radeon_fence_wait_timeout(struct radeon_fence * fence,bool intr,long timeout)543d78d3a22SFrançois Tigeot long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeout)
544d78d3a22SFrançois Tigeot {
545d78d3a22SFrançois Tigeot 	u64 seq[RADEON_NUM_RINGS] = {};
546d78d3a22SFrançois Tigeot 	long r;
547d78d3a22SFrançois Tigeot 	int r_sig;
548d78d3a22SFrançois Tigeot 
549d78d3a22SFrançois Tigeot 	/*
550d78d3a22SFrançois Tigeot 	 * This function should not be called on !radeon fences.
551d78d3a22SFrançois Tigeot 	 * If this is the case, it would mean this function can
552d78d3a22SFrançois Tigeot 	 * also be called on radeon fences belonging to another card.
553d78d3a22SFrançois Tigeot 	 * exclusive_lock is not held in that case.
554d78d3a22SFrançois Tigeot 	 */
555d78d3a22SFrançois Tigeot 	if (WARN_ON_ONCE(!to_radeon_fence(&fence->base)))
5566559babbSFrançois Tigeot 		return dma_fence_wait(&fence->base, intr);
557d78d3a22SFrançois Tigeot 
558d78d3a22SFrançois Tigeot 	seq[fence->ring] = fence->seq;
559d78d3a22SFrançois Tigeot 	r = radeon_fence_wait_seq_timeout(fence->rdev, seq, intr, timeout);
560d78d3a22SFrançois Tigeot 	if (r <= 0) {
561d78d3a22SFrançois Tigeot 		return r;
562d78d3a22SFrançois Tigeot 	}
563d78d3a22SFrançois Tigeot 
5646559babbSFrançois Tigeot 	r_sig = dma_fence_signal(&fence->base);
565d78d3a22SFrançois Tigeot 	if (!r_sig)
5666559babbSFrançois Tigeot 		DMA_FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
567d78d3a22SFrançois Tigeot 	return r;
568d78d3a22SFrançois Tigeot }
569d78d3a22SFrançois Tigeot 
570d78d3a22SFrançois Tigeot /**
571926deccbSFrançois Tigeot  * radeon_fence_wait - wait for a fence to signal
572926deccbSFrançois Tigeot  *
573926deccbSFrançois Tigeot  * @fence: radeon fence object
574591d5043SFrançois Tigeot  * @intr: use interruptible sleep
575926deccbSFrançois Tigeot  *
576926deccbSFrançois Tigeot  * Wait for the requested fence to signal (all asics).
577926deccbSFrançois Tigeot  * @intr selects whether to use interruptable (true) or non-interruptable
578926deccbSFrançois Tigeot  * (false) sleep when waiting for the fence.
579926deccbSFrançois Tigeot  * Returns 0 if the fence has passed, error for all other cases.
580926deccbSFrançois Tigeot  */
radeon_fence_wait(struct radeon_fence * fence,bool intr)581926deccbSFrançois Tigeot int radeon_fence_wait(struct radeon_fence *fence, bool intr)
582926deccbSFrançois Tigeot {
583d78d3a22SFrançois Tigeot 	long r = radeon_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
584d78d3a22SFrançois Tigeot 	if (r > 0) {
585d78d3a22SFrançois Tigeot 		return 0;
586d78d3a22SFrançois Tigeot 	} else {
5870d5a30c1SFrançois Tigeot 		return r;
588591d5043SFrançois Tigeot 	}
589926deccbSFrançois Tigeot }
590926deccbSFrançois Tigeot 
591926deccbSFrançois Tigeot /**
592926deccbSFrançois Tigeot  * radeon_fence_wait_any - wait for a fence to signal on any ring
593926deccbSFrançois Tigeot  *
594926deccbSFrançois Tigeot  * @rdev: radeon device pointer
595926deccbSFrançois Tigeot  * @fences: radeon fence object(s)
596926deccbSFrançois Tigeot  * @intr: use interruptable sleep
597926deccbSFrançois Tigeot  *
598926deccbSFrançois Tigeot  * Wait for any requested fence to signal (all asics).  Fence
599926deccbSFrançois Tigeot  * array is indexed by ring id.  @intr selects whether to use
600926deccbSFrançois Tigeot  * interruptable (true) or non-interruptable (false) sleep when
601926deccbSFrançois Tigeot  * waiting for the fences. Used by the suballocator.
602926deccbSFrançois Tigeot  * Returns 0 if any fence has passed, error for all other cases.
603926deccbSFrançois Tigeot  */
radeon_fence_wait_any(struct radeon_device * rdev,struct radeon_fence ** fences,bool intr)604926deccbSFrançois Tigeot int radeon_fence_wait_any(struct radeon_device *rdev,
605926deccbSFrançois Tigeot 			  struct radeon_fence **fences,
606926deccbSFrançois Tigeot 			  bool intr)
607926deccbSFrançois Tigeot {
608f77dbd6cSFrançois Tigeot 	u64 seq[RADEON_NUM_RINGS];
609c6f73aabSFrançois Tigeot 	unsigned i, num_rings = 0;
6101cfef1a5SFrançois Tigeot 	long r;
611926deccbSFrançois Tigeot 
612926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
613926deccbSFrançois Tigeot 		seq[i] = 0;
614926deccbSFrançois Tigeot 
615926deccbSFrançois Tigeot 		if (!fences[i]) {
616926deccbSFrançois Tigeot 			continue;
617926deccbSFrançois Tigeot 		}
618926deccbSFrançois Tigeot 
619c6f73aabSFrançois Tigeot 		seq[i] = fences[i]->seq;
620c6f73aabSFrançois Tigeot 		++num_rings;
621926deccbSFrançois Tigeot 	}
622926deccbSFrançois Tigeot 
623c6f73aabSFrançois Tigeot 	/* nothing to wait for ? */
624c6f73aabSFrançois Tigeot 	if (num_rings == 0)
625c6f73aabSFrançois Tigeot 		return -ENOENT;
626926deccbSFrançois Tigeot 
6271cfef1a5SFrançois Tigeot 	r = radeon_fence_wait_seq_timeout(rdev, seq, intr, MAX_SCHEDULE_TIMEOUT);
628591d5043SFrançois Tigeot 	if (r < 0) {
629926deccbSFrançois Tigeot 		return r;
630926deccbSFrançois Tigeot 	}
631926deccbSFrançois Tigeot 	return 0;
632926deccbSFrançois Tigeot }
633926deccbSFrançois Tigeot 
634926deccbSFrançois Tigeot /**
635c6f73aabSFrançois Tigeot  * radeon_fence_wait_next - wait for the next fence to signal
636926deccbSFrançois Tigeot  *
637926deccbSFrançois Tigeot  * @rdev: radeon device pointer
638926deccbSFrançois Tigeot  * @ring: ring index the fence is associated with
639926deccbSFrançois Tigeot  *
640926deccbSFrançois Tigeot  * Wait for the next fence on the requested ring to signal (all asics).
641926deccbSFrançois Tigeot  * Returns 0 if the next fence has passed, error for all other cases.
642926deccbSFrançois Tigeot  * Caller must hold ring lock.
643926deccbSFrançois Tigeot  */
radeon_fence_wait_next(struct radeon_device * rdev,int ring)644c6f73aabSFrançois Tigeot int radeon_fence_wait_next(struct radeon_device *rdev, int ring)
645926deccbSFrançois Tigeot {
6461cfef1a5SFrançois Tigeot 	u64 seq[RADEON_NUM_RINGS] = {};
6471cfef1a5SFrançois Tigeot 	long r;
648926deccbSFrançois Tigeot 
649c6f73aabSFrançois Tigeot 	seq[ring] = atomic64_read(&rdev->fence_drv[ring].last_seq) + 1ULL;
650c6f73aabSFrançois Tigeot 	if (seq[ring] >= rdev->fence_drv[ring].sync_seq[ring]) {
651926deccbSFrançois Tigeot 		/* nothing to wait for, last_seq is
652926deccbSFrançois Tigeot 		   already the last emited fence */
653926deccbSFrançois Tigeot 		return -ENOENT;
654926deccbSFrançois Tigeot 	}
6551cfef1a5SFrançois Tigeot 	r = radeon_fence_wait_seq_timeout(rdev, seq, false, MAX_SCHEDULE_TIMEOUT);
656591d5043SFrançois Tigeot 	if (r < 0)
657591d5043SFrançois Tigeot 		return r;
658591d5043SFrançois Tigeot 	return 0;
659926deccbSFrançois Tigeot }
660926deccbSFrançois Tigeot 
661926deccbSFrançois Tigeot /**
662c6f73aabSFrançois Tigeot  * radeon_fence_wait_empty - wait for all fences to signal
663926deccbSFrançois Tigeot  *
664926deccbSFrançois Tigeot  * @rdev: radeon device pointer
665926deccbSFrançois Tigeot  * @ring: ring index the fence is associated with
666926deccbSFrançois Tigeot  *
667926deccbSFrançois Tigeot  * Wait for all fences on the requested ring to signal (all asics).
668926deccbSFrançois Tigeot  * Returns 0 if the fences have passed, error for all other cases.
669926deccbSFrançois Tigeot  * Caller must hold ring lock.
670926deccbSFrançois Tigeot  */
radeon_fence_wait_empty(struct radeon_device * rdev,int ring)671c6f73aabSFrançois Tigeot int radeon_fence_wait_empty(struct radeon_device *rdev, int ring)
672926deccbSFrançois Tigeot {
673f77dbd6cSFrançois Tigeot 	u64 seq[RADEON_NUM_RINGS] = {};
6741cfef1a5SFrançois Tigeot 	long r;
675926deccbSFrançois Tigeot 
676c6f73aabSFrançois Tigeot 	seq[ring] = rdev->fence_drv[ring].sync_seq[ring];
677c6f73aabSFrançois Tigeot 	if (!seq[ring])
678c6f73aabSFrançois Tigeot 		return 0;
679c6f73aabSFrançois Tigeot 
6801cfef1a5SFrançois Tigeot 	r = radeon_fence_wait_seq_timeout(rdev, seq, false, MAX_SCHEDULE_TIMEOUT);
681591d5043SFrançois Tigeot 	if (r < 0) {
682c6f73aabSFrançois Tigeot 		if (r == -EDEADLK)
683926deccbSFrançois Tigeot 			return -EDEADLK;
684c6f73aabSFrançois Tigeot 
6851cfef1a5SFrançois Tigeot 		dev_err(rdev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
686926deccbSFrançois Tigeot 			ring, r);
687926deccbSFrançois Tigeot 	}
688926deccbSFrançois Tigeot 	return 0;
689926deccbSFrançois Tigeot }
690926deccbSFrançois Tigeot 
691926deccbSFrançois Tigeot /**
692926deccbSFrançois Tigeot  * radeon_fence_ref - take a ref on a fence
693926deccbSFrançois Tigeot  *
694926deccbSFrançois Tigeot  * @fence: radeon fence object
695926deccbSFrançois Tigeot  *
696926deccbSFrançois Tigeot  * Take a reference on a fence (all asics).
697926deccbSFrançois Tigeot  * Returns the fence.
698926deccbSFrançois Tigeot  */
radeon_fence_ref(struct radeon_fence * fence)699926deccbSFrançois Tigeot struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
700926deccbSFrançois Tigeot {
7016559babbSFrançois Tigeot 	dma_fence_get(&fence->base);
702926deccbSFrançois Tigeot 	return fence;
703926deccbSFrançois Tigeot }
704926deccbSFrançois Tigeot 
705926deccbSFrançois Tigeot /**
706926deccbSFrançois Tigeot  * radeon_fence_unref - remove a ref on a fence
707926deccbSFrançois Tigeot  *
708926deccbSFrançois Tigeot  * @fence: radeon fence object
709926deccbSFrançois Tigeot  *
710926deccbSFrançois Tigeot  * Remove a reference on a fence (all asics).
711926deccbSFrançois Tigeot  */
radeon_fence_unref(struct radeon_fence ** fence)712926deccbSFrançois Tigeot void radeon_fence_unref(struct radeon_fence **fence)
713926deccbSFrançois Tigeot {
714926deccbSFrançois Tigeot 	struct radeon_fence *tmp = *fence;
715926deccbSFrançois Tigeot 
716926deccbSFrançois Tigeot 	*fence = NULL;
717926deccbSFrançois Tigeot 	if (tmp) {
7186559babbSFrançois Tigeot 		dma_fence_put(&tmp->base);
719926deccbSFrançois Tigeot 	}
720926deccbSFrançois Tigeot }
721926deccbSFrançois Tigeot 
722926deccbSFrançois Tigeot /**
723926deccbSFrançois Tigeot  * radeon_fence_count_emitted - get the count of emitted fences
724926deccbSFrançois Tigeot  *
725926deccbSFrançois Tigeot  * @rdev: radeon device pointer
726926deccbSFrançois Tigeot  * @ring: ring index the fence is associated with
727926deccbSFrançois Tigeot  *
728926deccbSFrançois Tigeot  * Get the number of fences emitted on the requested ring (all asics).
729926deccbSFrançois Tigeot  * Returns the number of emitted fences on the ring.  Used by the
730926deccbSFrançois Tigeot  * dynpm code to ring track activity.
731926deccbSFrançois Tigeot  */
radeon_fence_count_emitted(struct radeon_device * rdev,int ring)732926deccbSFrançois Tigeot unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
733926deccbSFrançois Tigeot {
734926deccbSFrançois Tigeot 	uint64_t emitted;
735926deccbSFrançois Tigeot 
736926deccbSFrançois Tigeot 	/* We are not protected by ring lock when reading the last sequence
737926deccbSFrançois Tigeot 	 * but it's ok to report slightly wrong fence count here.
738926deccbSFrançois Tigeot 	 */
739926deccbSFrançois Tigeot 	radeon_fence_process(rdev, ring);
740926deccbSFrançois Tigeot 	emitted = rdev->fence_drv[ring].sync_seq[ring]
741e3b244c9SFrançois Tigeot 		- atomic64_read(&rdev->fence_drv[ring].last_seq);
742926deccbSFrançois Tigeot 	/* to avoid 32bits warp around */
743926deccbSFrançois Tigeot 	if (emitted > 0x10000000) {
744926deccbSFrançois Tigeot 		emitted = 0x10000000;
745926deccbSFrançois Tigeot 	}
746926deccbSFrançois Tigeot 	return (unsigned)emitted;
747926deccbSFrançois Tigeot }
748926deccbSFrançois Tigeot 
749926deccbSFrançois Tigeot /**
750926deccbSFrançois Tigeot  * radeon_fence_need_sync - do we need a semaphore
751926deccbSFrançois Tigeot  *
752926deccbSFrançois Tigeot  * @fence: radeon fence object
753926deccbSFrançois Tigeot  * @dst_ring: which ring to check against
754926deccbSFrançois Tigeot  *
755926deccbSFrançois Tigeot  * Check if the fence needs to be synced against another ring
756926deccbSFrançois Tigeot  * (all asics).  If so, we need to emit a semaphore.
757926deccbSFrançois Tigeot  * Returns true if we need to sync with another ring, false if
758926deccbSFrançois Tigeot  * not.
759926deccbSFrançois Tigeot  */
radeon_fence_need_sync(struct radeon_fence * fence,int dst_ring)760926deccbSFrançois Tigeot bool radeon_fence_need_sync(struct radeon_fence *fence, int dst_ring)
761926deccbSFrançois Tigeot {
762926deccbSFrançois Tigeot 	struct radeon_fence_driver *fdrv;
763926deccbSFrançois Tigeot 
764926deccbSFrançois Tigeot 	if (!fence) {
765926deccbSFrançois Tigeot 		return false;
766926deccbSFrançois Tigeot 	}
767926deccbSFrançois Tigeot 
768926deccbSFrançois Tigeot 	if (fence->ring == dst_ring) {
769926deccbSFrançois Tigeot 		return false;
770926deccbSFrançois Tigeot 	}
771926deccbSFrançois Tigeot 
772926deccbSFrançois Tigeot 	/* we are protected by the ring mutex */
773926deccbSFrançois Tigeot 	fdrv = &fence->rdev->fence_drv[dst_ring];
774926deccbSFrançois Tigeot 	if (fence->seq <= fdrv->sync_seq[fence->ring]) {
775926deccbSFrançois Tigeot 		return false;
776926deccbSFrançois Tigeot 	}
777926deccbSFrançois Tigeot 
778926deccbSFrançois Tigeot 	return true;
779926deccbSFrançois Tigeot }
780926deccbSFrançois Tigeot 
781926deccbSFrançois Tigeot /**
782926deccbSFrançois Tigeot  * radeon_fence_note_sync - record the sync point
783926deccbSFrançois Tigeot  *
784926deccbSFrançois Tigeot  * @fence: radeon fence object
785926deccbSFrançois Tigeot  * @dst_ring: which ring to check against
786926deccbSFrançois Tigeot  *
787926deccbSFrançois Tigeot  * Note the sequence number at which point the fence will
788926deccbSFrançois Tigeot  * be synced with the requested ring (all asics).
789926deccbSFrançois Tigeot  */
radeon_fence_note_sync(struct radeon_fence * fence,int dst_ring)790926deccbSFrançois Tigeot void radeon_fence_note_sync(struct radeon_fence *fence, int dst_ring)
791926deccbSFrançois Tigeot {
792926deccbSFrançois Tigeot 	struct radeon_fence_driver *dst, *src;
793926deccbSFrançois Tigeot 	unsigned i;
794926deccbSFrançois Tigeot 
795926deccbSFrançois Tigeot 	if (!fence) {
796926deccbSFrançois Tigeot 		return;
797926deccbSFrançois Tigeot 	}
798926deccbSFrançois Tigeot 
799926deccbSFrançois Tigeot 	if (fence->ring == dst_ring) {
800926deccbSFrançois Tigeot 		return;
801926deccbSFrançois Tigeot 	}
802926deccbSFrançois Tigeot 
803926deccbSFrançois Tigeot 	/* we are protected by the ring mutex */
804926deccbSFrançois Tigeot 	src = &fence->rdev->fence_drv[fence->ring];
805926deccbSFrançois Tigeot 	dst = &fence->rdev->fence_drv[dst_ring];
806926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
807926deccbSFrançois Tigeot 		if (i == dst_ring) {
808926deccbSFrançois Tigeot 			continue;
809926deccbSFrançois Tigeot 		}
810926deccbSFrançois Tigeot 		dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
811926deccbSFrançois Tigeot 	}
812926deccbSFrançois Tigeot }
813926deccbSFrançois Tigeot 
814926deccbSFrançois Tigeot /**
815926deccbSFrançois Tigeot  * radeon_fence_driver_start_ring - make the fence driver
816926deccbSFrançois Tigeot  * ready for use on the requested ring.
817926deccbSFrançois Tigeot  *
818926deccbSFrançois Tigeot  * @rdev: radeon device pointer
819926deccbSFrançois Tigeot  * @ring: ring index to start the fence driver on
820926deccbSFrançois Tigeot  *
821926deccbSFrançois Tigeot  * Make the fence driver ready for processing (all asics).
822926deccbSFrançois Tigeot  * Not all asics have all rings, so each asic will only
823926deccbSFrançois Tigeot  * start the fence driver on the rings it has.
824926deccbSFrançois Tigeot  * Returns 0 for success, errors for failure.
825926deccbSFrançois Tigeot  */
radeon_fence_driver_start_ring(struct radeon_device * rdev,int ring)826926deccbSFrançois Tigeot int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
827926deccbSFrançois Tigeot {
828926deccbSFrançois Tigeot 	uint64_t index;
829926deccbSFrançois Tigeot 	int r;
830926deccbSFrançois Tigeot 
831926deccbSFrançois Tigeot 	radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
832926deccbSFrançois Tigeot 	if (rdev->wb.use_event || !radeon_ring_supports_scratch_reg(rdev, &rdev->ring[ring])) {
833926deccbSFrançois Tigeot 		rdev->fence_drv[ring].scratch_reg = 0;
834f43cf1b1SMichael Neumann 		if (ring != R600_RING_TYPE_UVD_INDEX) {
835926deccbSFrançois Tigeot 			index = R600_WB_EVENT_OFFSET + ring * 4;
836f43cf1b1SMichael Neumann 			rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
837f43cf1b1SMichael Neumann 			rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr +
838f43cf1b1SMichael Neumann 							 index;
839f43cf1b1SMichael Neumann 
840f43cf1b1SMichael Neumann 		} else {
841f43cf1b1SMichael Neumann 			/* put fence directly behind firmware */
842f43cf1b1SMichael Neumann 			index = ALIGN(rdev->uvd_fw->datasize, 8);
843f43cf1b1SMichael Neumann 			rdev->fence_drv[ring].cpu_addr = (void*)((uint8_t*)rdev->uvd.cpu_addr + index);
844f43cf1b1SMichael Neumann 			rdev->fence_drv[ring].gpu_addr = rdev->uvd.gpu_addr + index;
845f43cf1b1SMichael Neumann 		}
846f43cf1b1SMichael Neumann 
847926deccbSFrançois Tigeot 	} else {
848926deccbSFrançois Tigeot 		r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
849926deccbSFrançois Tigeot 		if (r) {
850926deccbSFrançois Tigeot 			dev_err(rdev->dev, "fence failed to get scratch register\n");
851926deccbSFrançois Tigeot 			return r;
852926deccbSFrançois Tigeot 		}
853926deccbSFrançois Tigeot 		index = RADEON_WB_SCRATCH_OFFSET +
854926deccbSFrançois Tigeot 			rdev->fence_drv[ring].scratch_reg -
855926deccbSFrançois Tigeot 			rdev->scratch.reg_base;
856926deccbSFrançois Tigeot 		rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
857926deccbSFrançois Tigeot 		rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
858f43cf1b1SMichael Neumann 	}
859e3b244c9SFrançois Tigeot 	radeon_fence_write(rdev, atomic64_read(&rdev->fence_drv[ring].last_seq), ring);
860926deccbSFrançois Tigeot 	rdev->fence_drv[ring].initialized = true;
8611cfef1a5SFrançois Tigeot 	dev_info(rdev->dev, "fence driver on ring %d use gpu addr 0x%016lx and cpu addr 0x%p\n",
8624cd92098Szrj 		 ring, rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
863926deccbSFrançois Tigeot 	return 0;
864926deccbSFrançois Tigeot }
865926deccbSFrançois Tigeot 
866926deccbSFrançois Tigeot /**
867926deccbSFrançois Tigeot  * radeon_fence_driver_init_ring - init the fence driver
868926deccbSFrançois Tigeot  * for the requested ring.
869926deccbSFrançois Tigeot  *
870926deccbSFrançois Tigeot  * @rdev: radeon device pointer
871926deccbSFrançois Tigeot  * @ring: ring index to start the fence driver on
872926deccbSFrançois Tigeot  *
873926deccbSFrançois Tigeot  * Init the fence driver for the requested ring (all asics).
874926deccbSFrançois Tigeot  * Helper function for radeon_fence_driver_init().
875926deccbSFrançois Tigeot  */
radeon_fence_driver_init_ring(struct radeon_device * rdev,int ring)876926deccbSFrançois Tigeot static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
877926deccbSFrançois Tigeot {
878926deccbSFrançois Tigeot 	int i;
879926deccbSFrançois Tigeot 
880926deccbSFrançois Tigeot 	rdev->fence_drv[ring].scratch_reg = -1;
881926deccbSFrançois Tigeot 	rdev->fence_drv[ring].cpu_addr = NULL;
882926deccbSFrançois Tigeot 	rdev->fence_drv[ring].gpu_addr = 0;
883926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i)
884926deccbSFrançois Tigeot 		rdev->fence_drv[ring].sync_seq[i] = 0;
885e3b244c9SFrançois Tigeot 	atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
886926deccbSFrançois Tigeot 	rdev->fence_drv[ring].initialized = false;
887591d5043SFrançois Tigeot 	INIT_DELAYED_WORK(&rdev->fence_drv[ring].lockup_work,
888591d5043SFrançois Tigeot 			  radeon_fence_check_lockup);
889591d5043SFrançois Tigeot 	rdev->fence_drv[ring].rdev = rdev;
890926deccbSFrançois Tigeot }
891926deccbSFrançois Tigeot 
892926deccbSFrançois Tigeot /**
893926deccbSFrançois Tigeot  * radeon_fence_driver_init - init the fence driver
894926deccbSFrançois Tigeot  * for all possible rings.
895926deccbSFrançois Tigeot  *
896926deccbSFrançois Tigeot  * @rdev: radeon device pointer
897926deccbSFrançois Tigeot  *
898926deccbSFrançois Tigeot  * Init the fence driver for all possible rings (all asics).
899926deccbSFrançois Tigeot  * Not all asics have all rings, so each asic will only
900926deccbSFrançois Tigeot  * start the fence driver on the rings it has using
901926deccbSFrançois Tigeot  * radeon_fence_driver_start_ring().
902926deccbSFrançois Tigeot  * Returns 0 for success.
903926deccbSFrançois Tigeot  */
radeon_fence_driver_init(struct radeon_device * rdev)904926deccbSFrançois Tigeot int radeon_fence_driver_init(struct radeon_device *rdev)
905926deccbSFrançois Tigeot {
906926deccbSFrançois Tigeot 	int ring;
907926deccbSFrançois Tigeot 
9080d5a30c1SFrançois Tigeot 	init_waitqueue_head(&rdev->fence_queue);
909926deccbSFrançois Tigeot 	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
910926deccbSFrançois Tigeot 		radeon_fence_driver_init_ring(rdev, ring);
911926deccbSFrançois Tigeot 	}
912926deccbSFrançois Tigeot 	if (radeon_debugfs_fence_init(rdev)) {
913926deccbSFrançois Tigeot 		dev_err(rdev->dev, "fence debugfs file creation failed\n");
914926deccbSFrançois Tigeot 	}
915926deccbSFrançois Tigeot 	return 0;
916926deccbSFrançois Tigeot }
917926deccbSFrançois Tigeot 
918926deccbSFrançois Tigeot /**
919926deccbSFrançois Tigeot  * radeon_fence_driver_fini - tear down the fence driver
920926deccbSFrançois Tigeot  * for all possible rings.
921926deccbSFrançois Tigeot  *
922926deccbSFrançois Tigeot  * @rdev: radeon device pointer
923926deccbSFrançois Tigeot  *
924926deccbSFrançois Tigeot  * Tear down the fence driver for all possible rings (all asics).
925926deccbSFrançois Tigeot  */
radeon_fence_driver_fini(struct radeon_device * rdev)926926deccbSFrançois Tigeot void radeon_fence_driver_fini(struct radeon_device *rdev)
927926deccbSFrançois Tigeot {
928926deccbSFrançois Tigeot 	int ring, r;
929926deccbSFrançois Tigeot 
9301cfef1a5SFrançois Tigeot 	mutex_lock(&rdev->ring_lock);
931926deccbSFrançois Tigeot 	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
932926deccbSFrançois Tigeot 		if (!rdev->fence_drv[ring].initialized)
933926deccbSFrançois Tigeot 			continue;
934c6f73aabSFrançois Tigeot 		r = radeon_fence_wait_empty(rdev, ring);
935926deccbSFrançois Tigeot 		if (r) {
936926deccbSFrançois Tigeot 			/* no need to trigger GPU reset as we are unloading */
937c6f73aabSFrançois Tigeot 			radeon_fence_driver_force_completion(rdev, ring);
938926deccbSFrançois Tigeot 		}
939591d5043SFrançois Tigeot 		cancel_delayed_work_sync(&rdev->fence_drv[ring].lockup_work);
9400d5a30c1SFrançois Tigeot 		wake_up_all(&rdev->fence_queue);
941926deccbSFrançois Tigeot 		radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
942926deccbSFrançois Tigeot 		rdev->fence_drv[ring].initialized = false;
943926deccbSFrançois Tigeot 	}
9441cfef1a5SFrançois Tigeot 	mutex_unlock(&rdev->ring_lock);
945926deccbSFrançois Tigeot }
946926deccbSFrançois Tigeot 
947926deccbSFrançois Tigeot /**
948926deccbSFrançois Tigeot  * radeon_fence_driver_force_completion - force all fence waiter to complete
949926deccbSFrançois Tigeot  *
950926deccbSFrançois Tigeot  * @rdev: radeon device pointer
951c6f73aabSFrançois Tigeot  * @ring: the ring to complete
952926deccbSFrançois Tigeot  *
953926deccbSFrançois Tigeot  * In case of GPU reset failure make sure no process keep waiting on fence
954926deccbSFrançois Tigeot  * that will never complete.
955926deccbSFrançois Tigeot  */
radeon_fence_driver_force_completion(struct radeon_device * rdev,int ring)956c6f73aabSFrançois Tigeot void radeon_fence_driver_force_completion(struct radeon_device *rdev, int ring)
957926deccbSFrançois Tigeot {
958591d5043SFrançois Tigeot 	if (rdev->fence_drv[ring].initialized) {
959926deccbSFrançois Tigeot 		radeon_fence_write(rdev, rdev->fence_drv[ring].sync_seq[ring], ring);
960591d5043SFrançois Tigeot 		cancel_delayed_work_sync(&rdev->fence_drv[ring].lockup_work);
961591d5043SFrançois Tigeot 	}
962926deccbSFrançois Tigeot }
963926deccbSFrançois Tigeot 
964926deccbSFrançois Tigeot 
965926deccbSFrançois Tigeot /*
966926deccbSFrançois Tigeot  * Fence debugfs
967926deccbSFrançois Tigeot  */
968926deccbSFrançois Tigeot #if defined(CONFIG_DEBUG_FS)
radeon_debugfs_fence_info(struct seq_file * m,void * data)969926deccbSFrançois Tigeot static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
970926deccbSFrançois Tigeot {
971926deccbSFrançois Tigeot 	struct drm_info_node *node = (struct drm_info_node *)m->private;
972926deccbSFrançois Tigeot 	struct drm_device *dev = node->minor->dev;
973926deccbSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
974926deccbSFrançois Tigeot 	int i, j;
975926deccbSFrançois Tigeot 
976926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
977926deccbSFrançois Tigeot 		if (!rdev->fence_drv[i].initialized)
978926deccbSFrançois Tigeot 			continue;
979926deccbSFrançois Tigeot 
980c6f73aabSFrançois Tigeot 		radeon_fence_process(rdev, i);
981c6f73aabSFrançois Tigeot 
982926deccbSFrançois Tigeot 		seq_printf(m, "--- ring %d ---\n", i);
983926deccbSFrançois Tigeot 		seq_printf(m, "Last signaled fence 0x%016llx\n",
9841cfef1a5SFrançois Tigeot 			   (unsigned long long)atomic64_read(&rdev->fence_drv[i].last_seq));
985926deccbSFrançois Tigeot 		seq_printf(m, "Last emitted        0x%016llx\n",
986926deccbSFrançois Tigeot 			   rdev->fence_drv[i].sync_seq[i]);
987926deccbSFrançois Tigeot 
988926deccbSFrançois Tigeot 		for (j = 0; j < RADEON_NUM_RINGS; ++j) {
989926deccbSFrançois Tigeot 			if (i != j && rdev->fence_drv[j].initialized)
990926deccbSFrançois Tigeot 				seq_printf(m, "Last sync to ring %d 0x%016llx\n",
991926deccbSFrançois Tigeot 					   j, rdev->fence_drv[i].sync_seq[j]);
992926deccbSFrançois Tigeot 		}
993926deccbSFrançois Tigeot 	}
994926deccbSFrançois Tigeot 	return 0;
995926deccbSFrançois Tigeot }
996926deccbSFrançois Tigeot 
997c6f73aabSFrançois Tigeot /**
998c6f73aabSFrançois Tigeot  * radeon_debugfs_gpu_reset - manually trigger a gpu reset
999c6f73aabSFrançois Tigeot  *
1000c6f73aabSFrançois Tigeot  * Manually trigger a gpu reset at the next fence wait.
1001c6f73aabSFrançois Tigeot  */
radeon_debugfs_gpu_reset(struct seq_file * m,void * data)1002c6f73aabSFrançois Tigeot static int radeon_debugfs_gpu_reset(struct seq_file *m, void *data)
1003c6f73aabSFrançois Tigeot {
1004c6f73aabSFrançois Tigeot 	struct drm_info_node *node = (struct drm_info_node *) m->private;
1005c6f73aabSFrançois Tigeot 	struct drm_device *dev = node->minor->dev;
1006c6f73aabSFrançois Tigeot 	struct radeon_device *rdev = dev->dev_private;
1007c6f73aabSFrançois Tigeot 
1008c6f73aabSFrançois Tigeot 	down_read(&rdev->exclusive_lock);
1009c6f73aabSFrançois Tigeot 	seq_printf(m, "%d\n", rdev->needs_reset);
1010c6f73aabSFrançois Tigeot 	rdev->needs_reset = true;
1011591d5043SFrançois Tigeot 	wake_up_all(&rdev->fence_queue);
1012c6f73aabSFrançois Tigeot 	up_read(&rdev->exclusive_lock);
1013c6f73aabSFrançois Tigeot 
1014c6f73aabSFrançois Tigeot 	return 0;
1015c6f73aabSFrançois Tigeot }
1016c6f73aabSFrançois Tigeot 
1017926deccbSFrançois Tigeot static struct drm_info_list radeon_debugfs_fence_list[] = {
1018926deccbSFrançois Tigeot 	{"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
1019c6f73aabSFrançois Tigeot 	{"radeon_gpu_reset", &radeon_debugfs_gpu_reset, 0, NULL}
1020926deccbSFrançois Tigeot };
1021926deccbSFrançois Tigeot #endif
1022926deccbSFrançois Tigeot 
radeon_debugfs_fence_init(struct radeon_device * rdev)1023926deccbSFrançois Tigeot int radeon_debugfs_fence_init(struct radeon_device *rdev)
1024926deccbSFrançois Tigeot {
1025926deccbSFrançois Tigeot #if defined(CONFIG_DEBUG_FS)
1026c6f73aabSFrançois Tigeot 	return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 2);
1027926deccbSFrançois Tigeot #else
1028926deccbSFrançois Tigeot 	return 0;
1029926deccbSFrançois Tigeot #endif
1030926deccbSFrançois Tigeot }
10311cfef1a5SFrançois Tigeot 
radeon_fence_get_driver_name(struct dma_fence * fence)10326559babbSFrançois Tigeot static const char *radeon_fence_get_driver_name(struct dma_fence *fence)
10331cfef1a5SFrançois Tigeot {
10341cfef1a5SFrançois Tigeot 	return "radeon";
10351cfef1a5SFrançois Tigeot }
10361cfef1a5SFrançois Tigeot 
radeon_fence_get_timeline_name(struct dma_fence * f)10376559babbSFrançois Tigeot static const char *radeon_fence_get_timeline_name(struct dma_fence *f)
10381cfef1a5SFrançois Tigeot {
10391cfef1a5SFrançois Tigeot 	struct radeon_fence *fence = to_radeon_fence(f);
10401cfef1a5SFrançois Tigeot 	switch (fence->ring) {
10411cfef1a5SFrançois Tigeot 	case RADEON_RING_TYPE_GFX_INDEX: return "radeon.gfx";
10421cfef1a5SFrançois Tigeot 	case CAYMAN_RING_TYPE_CP1_INDEX: return "radeon.cp1";
10431cfef1a5SFrançois Tigeot 	case CAYMAN_RING_TYPE_CP2_INDEX: return "radeon.cp2";
10441cfef1a5SFrançois Tigeot 	case R600_RING_TYPE_DMA_INDEX: return "radeon.dma";
10451cfef1a5SFrançois Tigeot 	case CAYMAN_RING_TYPE_DMA1_INDEX: return "radeon.dma1";
10461cfef1a5SFrançois Tigeot 	case R600_RING_TYPE_UVD_INDEX: return "radeon.uvd";
10471cfef1a5SFrançois Tigeot 	case TN_RING_TYPE_VCE1_INDEX: return "radeon.vce1";
10481cfef1a5SFrançois Tigeot 	case TN_RING_TYPE_VCE2_INDEX: return "radeon.vce2";
10491cfef1a5SFrançois Tigeot 	default: WARN_ON_ONCE(1); return "radeon.unk";
10501cfef1a5SFrançois Tigeot 	}
10511cfef1a5SFrançois Tigeot }
10521cfef1a5SFrançois Tigeot 
radeon_test_signaled(struct radeon_fence * fence)10531cfef1a5SFrançois Tigeot static inline bool radeon_test_signaled(struct radeon_fence *fence)
10541cfef1a5SFrançois Tigeot {
10556559babbSFrançois Tigeot 	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
10561cfef1a5SFrançois Tigeot }
10571cfef1a5SFrançois Tigeot 
10587dcf36dcSFrançois Tigeot struct radeon_wait_cb {
10596559babbSFrançois Tigeot 	struct dma_fence_cb base;
10607dcf36dcSFrançois Tigeot 	struct task_struct *task;
10617dcf36dcSFrançois Tigeot };
10627dcf36dcSFrançois Tigeot 
10637dcf36dcSFrançois Tigeot static void
radeon_fence_wait_cb(struct dma_fence * fence,struct dma_fence_cb * cb)10646559babbSFrançois Tigeot radeon_fence_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
10657dcf36dcSFrançois Tigeot {
10667dcf36dcSFrançois Tigeot 	struct radeon_wait_cb *wait =
10677dcf36dcSFrançois Tigeot 		container_of(cb, struct radeon_wait_cb, base);
10687dcf36dcSFrançois Tigeot 
10697dcf36dcSFrançois Tigeot 	wake_up_process(wait->task);
10707dcf36dcSFrançois Tigeot }
10717dcf36dcSFrançois Tigeot 
radeon_fence_default_wait(struct dma_fence * f,bool intr,signed long t)10726559babbSFrançois Tigeot static signed long radeon_fence_default_wait(struct dma_fence *f, bool intr,
10731cfef1a5SFrançois Tigeot 					     signed long t)
10741cfef1a5SFrançois Tigeot {
10751cfef1a5SFrançois Tigeot 	struct radeon_fence *fence = to_radeon_fence(f);
10761cfef1a5SFrançois Tigeot 	struct radeon_device *rdev = fence->rdev;
10777dcf36dcSFrançois Tigeot 	struct radeon_wait_cb cb;
10781cfef1a5SFrançois Tigeot 
10797dcf36dcSFrançois Tigeot 	cb.task = current;
10807dcf36dcSFrançois Tigeot 
10816559babbSFrançois Tigeot 	if (dma_fence_add_callback(f, &cb.base, radeon_fence_wait_cb))
10827dcf36dcSFrançois Tigeot 		return t;
10837dcf36dcSFrançois Tigeot 
10847dcf36dcSFrançois Tigeot 	while (t > 0) {
10857dcf36dcSFrançois Tigeot 		if (intr)
10867dcf36dcSFrançois Tigeot 			set_current_state(TASK_INTERRUPTIBLE);
10877dcf36dcSFrançois Tigeot 		else
10887dcf36dcSFrançois Tigeot 			set_current_state(TASK_UNINTERRUPTIBLE);
10891cfef1a5SFrançois Tigeot 
10901cfef1a5SFrançois Tigeot 		/*
10917dcf36dcSFrançois Tigeot 		 * radeon_test_signaled must be called after
10927dcf36dcSFrançois Tigeot 		 * set_current_state to prevent a race with wake_up_process
10931cfef1a5SFrançois Tigeot 		 */
10947dcf36dcSFrançois Tigeot 		if (radeon_test_signaled(fence))
10957dcf36dcSFrançois Tigeot 			break;
10961cfef1a5SFrançois Tigeot 
10977dcf36dcSFrançois Tigeot 		if (rdev->needs_reset) {
10987dcf36dcSFrançois Tigeot 			t = -EDEADLK;
10997dcf36dcSFrançois Tigeot 			break;
11007dcf36dcSFrançois Tigeot 		}
11011cfef1a5SFrançois Tigeot 
11027dcf36dcSFrançois Tigeot 		t = schedule_timeout(t);
11037dcf36dcSFrançois Tigeot 
11047dcf36dcSFrançois Tigeot 		if (t > 0 && intr && signal_pending(current))
11057dcf36dcSFrançois Tigeot 			t = -ERESTARTSYS;
11067dcf36dcSFrançois Tigeot 	}
11077dcf36dcSFrançois Tigeot 
11087dcf36dcSFrançois Tigeot 	__set_current_state(TASK_RUNNING);
11096559babbSFrançois Tigeot 	dma_fence_remove_callback(f, &cb.base);
11107dcf36dcSFrançois Tigeot 
11111cfef1a5SFrançois Tigeot 	return t;
11121cfef1a5SFrançois Tigeot }
11131cfef1a5SFrançois Tigeot 
11146559babbSFrançois Tigeot const struct dma_fence_ops radeon_fence_ops = {
11151cfef1a5SFrançois Tigeot 	.get_driver_name = radeon_fence_get_driver_name,
11161cfef1a5SFrançois Tigeot 	.get_timeline_name = radeon_fence_get_timeline_name,
11171cfef1a5SFrançois Tigeot 	.enable_signaling = radeon_fence_enable_signaling,
11181cfef1a5SFrançois Tigeot 	.signaled = radeon_fence_is_signaled,
11191cfef1a5SFrançois Tigeot 	.wait = radeon_fence_default_wait,
11201cfef1a5SFrançois Tigeot 	.release = NULL,
11211cfef1a5SFrançois Tigeot };
1122