1d38ceaf9SAlex Deucher /*
2d38ceaf9SAlex Deucher  * Copyright 2009 Jerome Glisse.
3d38ceaf9SAlex Deucher  * All Rights Reserved.
4d38ceaf9SAlex Deucher  *
5d38ceaf9SAlex Deucher  * Permission is hereby granted, free of charge, to any person obtaining a
6d38ceaf9SAlex Deucher  * copy of this software and associated documentation files (the
7d38ceaf9SAlex Deucher  * "Software"), to deal in the Software without restriction, including
8d38ceaf9SAlex Deucher  * without limitation the rights to use, copy, modify, merge, publish,
9d38ceaf9SAlex Deucher  * distribute, sub license, and/or sell copies of the Software, and to
10d38ceaf9SAlex Deucher  * permit persons to whom the Software is furnished to do so, subject to
11d38ceaf9SAlex Deucher  * the following conditions:
12d38ceaf9SAlex Deucher  *
13d38ceaf9SAlex Deucher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14d38ceaf9SAlex Deucher  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15d38ceaf9SAlex Deucher  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16d38ceaf9SAlex Deucher  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17d38ceaf9SAlex Deucher  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18d38ceaf9SAlex Deucher  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19d38ceaf9SAlex Deucher  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20d38ceaf9SAlex Deucher  *
21d38ceaf9SAlex Deucher  * The above copyright notice and this permission notice (including the
22d38ceaf9SAlex Deucher  * next paragraph) shall be included in all copies or substantial portions
23d38ceaf9SAlex Deucher  * of the Software.
24d38ceaf9SAlex Deucher  *
25d38ceaf9SAlex Deucher  */
26d38ceaf9SAlex Deucher /*
27d38ceaf9SAlex Deucher  * Authors:
28d38ceaf9SAlex Deucher  *    Jerome Glisse <glisse@freedesktop.org>
29d38ceaf9SAlex Deucher  *    Dave Airlie
30d38ceaf9SAlex Deucher  */
31d38ceaf9SAlex Deucher #include <linux/seq_file.h>
32d38ceaf9SAlex Deucher #include <linux/atomic.h>
33d38ceaf9SAlex Deucher #include <linux/wait.h>
34d38ceaf9SAlex Deucher #include <linux/kref.h>
35d38ceaf9SAlex Deucher #include <linux/slab.h>
36d38ceaf9SAlex Deucher #include <linux/firmware.h>
37d38ceaf9SAlex Deucher #include <drm/drmP.h>
38d38ceaf9SAlex Deucher #include "amdgpu.h"
39d38ceaf9SAlex Deucher #include "amdgpu_trace.h"
40d38ceaf9SAlex Deucher 
41d38ceaf9SAlex Deucher /*
42d38ceaf9SAlex Deucher  * Fences
43d38ceaf9SAlex Deucher  * Fences mark an event in the GPUs pipeline and are used
44d38ceaf9SAlex Deucher  * for GPU/CPU synchronization.  When the fence is written,
45d38ceaf9SAlex Deucher  * it is expected that all buffers associated with that fence
46d38ceaf9SAlex Deucher  * are no longer in use by the associated ring on the GPU and
47d38ceaf9SAlex Deucher  * that the the relevant GPU caches have been flushed.
48d38ceaf9SAlex Deucher  */
49d38ceaf9SAlex Deucher 
50b49c84a5SChunming Zhou static struct kmem_cache *amdgpu_fence_slab;
51b49c84a5SChunming Zhou static atomic_t amdgpu_fence_slab_ref = ATOMIC_INIT(0);
52b49c84a5SChunming Zhou 
53d38ceaf9SAlex Deucher /**
54d38ceaf9SAlex Deucher  * amdgpu_fence_write - write a fence value
55d38ceaf9SAlex Deucher  *
56d38ceaf9SAlex Deucher  * @ring: ring the fence is associated with
57d38ceaf9SAlex Deucher  * @seq: sequence number to write
58d38ceaf9SAlex Deucher  *
59d38ceaf9SAlex Deucher  * Writes a fence value to memory (all asics).
60d38ceaf9SAlex Deucher  */
61d38ceaf9SAlex Deucher static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
62d38ceaf9SAlex Deucher {
63d38ceaf9SAlex Deucher 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
64d38ceaf9SAlex Deucher 
65d38ceaf9SAlex Deucher 	if (drv->cpu_addr)
66d38ceaf9SAlex Deucher 		*drv->cpu_addr = cpu_to_le32(seq);
67d38ceaf9SAlex Deucher }
68d38ceaf9SAlex Deucher 
69d38ceaf9SAlex Deucher /**
70d38ceaf9SAlex Deucher  * amdgpu_fence_read - read a fence value
71d38ceaf9SAlex Deucher  *
72d38ceaf9SAlex Deucher  * @ring: ring the fence is associated with
73d38ceaf9SAlex Deucher  *
74d38ceaf9SAlex Deucher  * Reads a fence value from memory (all asics).
75d38ceaf9SAlex Deucher  * Returns the value of the fence read from memory.
76d38ceaf9SAlex Deucher  */
77d38ceaf9SAlex Deucher static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
78d38ceaf9SAlex Deucher {
79d38ceaf9SAlex Deucher 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
80d38ceaf9SAlex Deucher 	u32 seq = 0;
81d38ceaf9SAlex Deucher 
82d38ceaf9SAlex Deucher 	if (drv->cpu_addr)
83d38ceaf9SAlex Deucher 		seq = le32_to_cpu(*drv->cpu_addr);
84d38ceaf9SAlex Deucher 	else
85d38ceaf9SAlex Deucher 		seq = lower_32_bits(atomic64_read(&drv->last_seq));
86d38ceaf9SAlex Deucher 
87d38ceaf9SAlex Deucher 	return seq;
88d38ceaf9SAlex Deucher }
89d38ceaf9SAlex Deucher 
90d38ceaf9SAlex Deucher /**
91d38ceaf9SAlex Deucher  * amdgpu_fence_emit - emit a fence on the requested ring
92d38ceaf9SAlex Deucher  *
93d38ceaf9SAlex Deucher  * @ring: ring the fence is associated with
94d38ceaf9SAlex Deucher  * @owner: creator of the fence
95d38ceaf9SAlex Deucher  * @fence: amdgpu fence object
96d38ceaf9SAlex Deucher  *
97d38ceaf9SAlex Deucher  * Emits a fence command on the requested ring (all asics).
98d38ceaf9SAlex Deucher  * Returns 0 on success, -ENOMEM on failure.
99d38ceaf9SAlex Deucher  */
100d38ceaf9SAlex Deucher int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner,
101d38ceaf9SAlex Deucher 		      struct amdgpu_fence **fence)
102d38ceaf9SAlex Deucher {
103d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = ring->adev;
104d38ceaf9SAlex Deucher 
105d38ceaf9SAlex Deucher 	/* we are protected by the ring emission mutex */
106b49c84a5SChunming Zhou 	*fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
107d38ceaf9SAlex Deucher 	if ((*fence) == NULL) {
108d38ceaf9SAlex Deucher 		return -ENOMEM;
109d38ceaf9SAlex Deucher 	}
1105907a0d8SChristian König 	(*fence)->seq = ++ring->fence_drv.sync_seq;
111d38ceaf9SAlex Deucher 	(*fence)->ring = ring;
112d38ceaf9SAlex Deucher 	(*fence)->owner = owner;
113d38ceaf9SAlex Deucher 	fence_init(&(*fence)->base, &amdgpu_fence_ops,
1147f06c236Smonk.liu 		&ring->fence_drv.fence_queue.lock,
1157f06c236Smonk.liu 		adev->fence_context + ring->idx,
116d38ceaf9SAlex Deucher 		(*fence)->seq);
117890ee23fSChunming Zhou 	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
118890ee23fSChunming Zhou 			       (*fence)->seq,
119890ee23fSChunming Zhou 			       AMDGPU_FENCE_FLAG_INT);
120d38ceaf9SAlex Deucher 	return 0;
121d38ceaf9SAlex Deucher }
122d38ceaf9SAlex Deucher 
123d38ceaf9SAlex Deucher /**
124c2776afeSChristian König  * amdgpu_fence_schedule_fallback - schedule fallback check
125c2776afeSChristian König  *
126c2776afeSChristian König  * @ring: pointer to struct amdgpu_ring
127c2776afeSChristian König  *
128c2776afeSChristian König  * Start a timer as fallback to our interrupts.
129c2776afeSChristian König  */
130c2776afeSChristian König static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
131c2776afeSChristian König {
132c2776afeSChristian König 	mod_timer(&ring->fence_drv.fallback_timer,
133c2776afeSChristian König 		  jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
134c2776afeSChristian König }
135c2776afeSChristian König 
136c2776afeSChristian König /**
137d38ceaf9SAlex Deucher  * amdgpu_fence_activity - check for fence activity
138d38ceaf9SAlex Deucher  *
139d38ceaf9SAlex Deucher  * @ring: pointer to struct amdgpu_ring
140d38ceaf9SAlex Deucher  *
141d38ceaf9SAlex Deucher  * Checks the current fence value and calculates the last
142d38ceaf9SAlex Deucher  * signalled fence value. Returns true if activity occured
143d38ceaf9SAlex Deucher  * on the ring, and the fence_queue should be waken up.
144d38ceaf9SAlex Deucher  */
145d38ceaf9SAlex Deucher static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
146d38ceaf9SAlex Deucher {
147d38ceaf9SAlex Deucher 	uint64_t seq, last_seq, last_emitted;
148d38ceaf9SAlex Deucher 	unsigned count_loop = 0;
149d38ceaf9SAlex Deucher 	bool wake = false;
150d38ceaf9SAlex Deucher 
151d38ceaf9SAlex Deucher 	/* Note there is a scenario here for an infinite loop but it's
152d38ceaf9SAlex Deucher 	 * very unlikely to happen. For it to happen, the current polling
153d38ceaf9SAlex Deucher 	 * process need to be interrupted by another process and another
154d38ceaf9SAlex Deucher 	 * process needs to update the last_seq btw the atomic read and
155d38ceaf9SAlex Deucher 	 * xchg of the current process.
156d38ceaf9SAlex Deucher 	 *
157d38ceaf9SAlex Deucher 	 * More over for this to go in infinite loop there need to be
15886c2b790SJammy Zhou 	 * continuously new fence signaled ie amdgpu_fence_read needs
159d38ceaf9SAlex Deucher 	 * to return a different value each time for both the currently
160d38ceaf9SAlex Deucher 	 * polling process and the other process that xchg the last_seq
161d38ceaf9SAlex Deucher 	 * btw atomic read and xchg of the current process. And the
162d38ceaf9SAlex Deucher 	 * value the other process set as last seq must be higher than
163d38ceaf9SAlex Deucher 	 * the seq value we just read. Which means that current process
16486c2b790SJammy Zhou 	 * need to be interrupted after amdgpu_fence_read and before
165d38ceaf9SAlex Deucher 	 * atomic xchg.
166d38ceaf9SAlex Deucher 	 *
167d38ceaf9SAlex Deucher 	 * To be even more safe we count the number of time we loop and
168d38ceaf9SAlex Deucher 	 * we bail after 10 loop just accepting the fact that we might
169d38ceaf9SAlex Deucher 	 * have temporarly set the last_seq not to the true real last
170d38ceaf9SAlex Deucher 	 * seq but to an older one.
171d38ceaf9SAlex Deucher 	 */
172d38ceaf9SAlex Deucher 	last_seq = atomic64_read(&ring->fence_drv.last_seq);
173d38ceaf9SAlex Deucher 	do {
1745907a0d8SChristian König 		last_emitted = ring->fence_drv.sync_seq;
175d38ceaf9SAlex Deucher 		seq = amdgpu_fence_read(ring);
176d38ceaf9SAlex Deucher 		seq |= last_seq & 0xffffffff00000000LL;
177d38ceaf9SAlex Deucher 		if (seq < last_seq) {
178d38ceaf9SAlex Deucher 			seq &= 0xffffffff;
179d38ceaf9SAlex Deucher 			seq |= last_emitted & 0xffffffff00000000LL;
180d38ceaf9SAlex Deucher 		}
181d38ceaf9SAlex Deucher 
182d38ceaf9SAlex Deucher 		if (seq <= last_seq || seq > last_emitted) {
183d38ceaf9SAlex Deucher 			break;
184d38ceaf9SAlex Deucher 		}
185d38ceaf9SAlex Deucher 		/* If we loop over we don't want to return without
186d38ceaf9SAlex Deucher 		 * checking if a fence is signaled as it means that the
187d38ceaf9SAlex Deucher 		 * seq we just read is different from the previous on.
188d38ceaf9SAlex Deucher 		 */
189d38ceaf9SAlex Deucher 		wake = true;
190d38ceaf9SAlex Deucher 		last_seq = seq;
191d38ceaf9SAlex Deucher 		if ((count_loop++) > 10) {
192d38ceaf9SAlex Deucher 			/* We looped over too many time leave with the
193d38ceaf9SAlex Deucher 			 * fact that we might have set an older fence
194d38ceaf9SAlex Deucher 			 * seq then the current real last seq as signaled
195d38ceaf9SAlex Deucher 			 * by the hw.
196d38ceaf9SAlex Deucher 			 */
197d38ceaf9SAlex Deucher 			break;
198d38ceaf9SAlex Deucher 		}
199d38ceaf9SAlex Deucher 	} while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
200d38ceaf9SAlex Deucher 
201d38ceaf9SAlex Deucher 	if (seq < last_emitted)
202c2776afeSChristian König 		amdgpu_fence_schedule_fallback(ring);
203d38ceaf9SAlex Deucher 
204d38ceaf9SAlex Deucher 	return wake;
205d38ceaf9SAlex Deucher }
206d38ceaf9SAlex Deucher 
207d38ceaf9SAlex Deucher /**
208d38ceaf9SAlex Deucher  * amdgpu_fence_process - process a fence
209d38ceaf9SAlex Deucher  *
210d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
211d38ceaf9SAlex Deucher  * @ring: ring index the fence is associated with
212d38ceaf9SAlex Deucher  *
213d38ceaf9SAlex Deucher  * Checks the current fence value and wakes the fence queue
214d38ceaf9SAlex Deucher  * if the sequence number has increased (all asics).
215d38ceaf9SAlex Deucher  */
216d38ceaf9SAlex Deucher void amdgpu_fence_process(struct amdgpu_ring *ring)
217d38ceaf9SAlex Deucher {
21868ed3de4SChristian König 	if (amdgpu_fence_activity(ring))
2197f06c236Smonk.liu 		wake_up_all(&ring->fence_drv.fence_queue);
220e0d8f3c3SChunming Zhou }
221d38ceaf9SAlex Deucher 
222d38ceaf9SAlex Deucher /**
223c2776afeSChristian König  * amdgpu_fence_fallback - fallback for hardware interrupts
224c2776afeSChristian König  *
225c2776afeSChristian König  * @work: delayed work item
226c2776afeSChristian König  *
227c2776afeSChristian König  * Checks for fence activity.
228c2776afeSChristian König  */
229c2776afeSChristian König static void amdgpu_fence_fallback(unsigned long arg)
230c2776afeSChristian König {
231c2776afeSChristian König 	struct amdgpu_ring *ring = (void *)arg;
232c2776afeSChristian König 
233c2776afeSChristian König 	amdgpu_fence_process(ring);
234c2776afeSChristian König }
235c2776afeSChristian König 
236c2776afeSChristian König /**
237d38ceaf9SAlex Deucher  * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
238d38ceaf9SAlex Deucher  *
239d38ceaf9SAlex Deucher  * @ring: ring the fence is associated with
240d38ceaf9SAlex Deucher  * @seq: sequence number
241d38ceaf9SAlex Deucher  *
242d38ceaf9SAlex Deucher  * Check if the last signaled fence sequnce number is >= the requested
243d38ceaf9SAlex Deucher  * sequence number (all asics).
244d38ceaf9SAlex Deucher  * Returns true if the fence has signaled (current fence value
245d38ceaf9SAlex Deucher  * is >= requested value) or false if it has not (current fence
246d38ceaf9SAlex Deucher  * value is < the requested value.  Helper function for
247d38ceaf9SAlex Deucher  * amdgpu_fence_signaled().
248d38ceaf9SAlex Deucher  */
249d38ceaf9SAlex Deucher static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
250d38ceaf9SAlex Deucher {
251d38ceaf9SAlex Deucher 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
252d38ceaf9SAlex Deucher 		return true;
253d38ceaf9SAlex Deucher 
254d38ceaf9SAlex Deucher 	/* poll new last sequence at least once */
255d38ceaf9SAlex Deucher 	amdgpu_fence_process(ring);
256d38ceaf9SAlex Deucher 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
257d38ceaf9SAlex Deucher 		return true;
258d38ceaf9SAlex Deucher 
259d38ceaf9SAlex Deucher 	return false;
260d38ceaf9SAlex Deucher }
261d38ceaf9SAlex Deucher 
2627f06c236Smonk.liu /*
2637f06c236Smonk.liu  * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal
2647f06c236Smonk.liu  * @ring: ring to wait on for the seq number
2657f06c236Smonk.liu  * @seq: seq number wait for
266d38ceaf9SAlex Deucher  *
2677f06c236Smonk.liu  * return value:
26800d2a2b2SChristian König  * 0: seq signaled, and gpu not hang
26900d2a2b2SChristian König  * -EDEADL: GPU hang detected
2707f06c236Smonk.liu  * -EINVAL: some paramter is not valid
271d38ceaf9SAlex Deucher  */
27200d2a2b2SChristian König static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
273d38ceaf9SAlex Deucher {
2747f06c236Smonk.liu 	bool signaled = false;
275d38ceaf9SAlex Deucher 
2767f06c236Smonk.liu 	BUG_ON(!ring);
2775907a0d8SChristian König 	if (seq > ring->fence_drv.sync_seq)
2787f06c236Smonk.liu 		return -EINVAL;
279d38ceaf9SAlex Deucher 
2807f06c236Smonk.liu 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
281d38ceaf9SAlex Deucher 		return 0;
28200d2a2b2SChristian König 
283c2776afeSChristian König 	amdgpu_fence_schedule_fallback(ring);
28400d2a2b2SChristian König 	wait_event(ring->fence_drv.fence_queue, (
285b7e4dad3SChristian König 		   (signaled = amdgpu_fence_seq_signaled(ring, seq))));
28600d2a2b2SChristian König 
28700d2a2b2SChristian König 	if (signaled)
28800d2a2b2SChristian König 		return 0;
28900d2a2b2SChristian König 	else
29000d2a2b2SChristian König 		return -EDEADLK;
291d38ceaf9SAlex Deucher }
2927f06c236Smonk.liu 
293d38ceaf9SAlex Deucher /**
294d38ceaf9SAlex Deucher  * amdgpu_fence_wait_next - wait for the next fence to signal
295d38ceaf9SAlex Deucher  *
296d38ceaf9SAlex Deucher  * @adev: amdgpu device pointer
297d38ceaf9SAlex Deucher  * @ring: ring index the fence is associated with
298d38ceaf9SAlex Deucher  *
299d38ceaf9SAlex Deucher  * Wait for the next fence on the requested ring to signal (all asics).
300d38ceaf9SAlex Deucher  * Returns 0 if the next fence has passed, error for all other cases.
301d38ceaf9SAlex Deucher  * Caller must hold ring lock.
302d38ceaf9SAlex Deucher  */
303d38ceaf9SAlex Deucher int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
304d38ceaf9SAlex Deucher {
3057f06c236Smonk.liu 	uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
30600d2a2b2SChristian König 
3075907a0d8SChristian König 	if (seq >= ring->fence_drv.sync_seq)
308d38ceaf9SAlex Deucher 		return -ENOENT;
3097f06c236Smonk.liu 
31000d2a2b2SChristian König 	return amdgpu_fence_ring_wait_seq(ring, seq);
311d38ceaf9SAlex Deucher }
312d38ceaf9SAlex Deucher 
313d38ceaf9SAlex Deucher /**
314d38ceaf9SAlex Deucher  * amdgpu_fence_wait_empty - wait for all fences to signal
315d38ceaf9SAlex Deucher  *
316d38ceaf9SAlex Deucher  * @adev: amdgpu device pointer
317d38ceaf9SAlex Deucher  * @ring: ring index the fence is associated with
318d38ceaf9SAlex Deucher  *
319d38ceaf9SAlex Deucher  * Wait for all fences on the requested ring to signal (all asics).
320d38ceaf9SAlex Deucher  * Returns 0 if the fences have passed, error for all other cases.
321d38ceaf9SAlex Deucher  * Caller must hold ring lock.
322d38ceaf9SAlex Deucher  */
323d38ceaf9SAlex Deucher int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
324d38ceaf9SAlex Deucher {
3255907a0d8SChristian König 	uint64_t seq = ring->fence_drv.sync_seq;
32600d2a2b2SChristian König 
3277f06c236Smonk.liu 	if (!seq)
328d38ceaf9SAlex Deucher 		return 0;
329d38ceaf9SAlex Deucher 
33000d2a2b2SChristian König 	return amdgpu_fence_ring_wait_seq(ring, seq);
331d38ceaf9SAlex Deucher }
332d38ceaf9SAlex Deucher 
333d38ceaf9SAlex Deucher /**
334d38ceaf9SAlex Deucher  * amdgpu_fence_count_emitted - get the count of emitted fences
335d38ceaf9SAlex Deucher  *
336d38ceaf9SAlex Deucher  * @ring: ring the fence is associated with
337d38ceaf9SAlex Deucher  *
338d38ceaf9SAlex Deucher  * Get the number of fences emitted on the requested ring (all asics).
339d38ceaf9SAlex Deucher  * Returns the number of emitted fences on the ring.  Used by the
340d38ceaf9SAlex Deucher  * dynpm code to ring track activity.
341d38ceaf9SAlex Deucher  */
342d38ceaf9SAlex Deucher unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
343d38ceaf9SAlex Deucher {
344d38ceaf9SAlex Deucher 	uint64_t emitted;
345d38ceaf9SAlex Deucher 
346d38ceaf9SAlex Deucher 	/* We are not protected by ring lock when reading the last sequence
347d38ceaf9SAlex Deucher 	 * but it's ok to report slightly wrong fence count here.
348d38ceaf9SAlex Deucher 	 */
349d38ceaf9SAlex Deucher 	amdgpu_fence_process(ring);
3505907a0d8SChristian König 	emitted = ring->fence_drv.sync_seq
351d38ceaf9SAlex Deucher 		- atomic64_read(&ring->fence_drv.last_seq);
352d38ceaf9SAlex Deucher 	/* to avoid 32bits warp around */
353d38ceaf9SAlex Deucher 	if (emitted > 0x10000000)
354d38ceaf9SAlex Deucher 		emitted = 0x10000000;
355d38ceaf9SAlex Deucher 
356d38ceaf9SAlex Deucher 	return (unsigned)emitted;
357d38ceaf9SAlex Deucher }
358d38ceaf9SAlex Deucher 
359d38ceaf9SAlex Deucher /**
360d38ceaf9SAlex Deucher  * amdgpu_fence_driver_start_ring - make the fence driver
361d38ceaf9SAlex Deucher  * ready for use on the requested ring.
362d38ceaf9SAlex Deucher  *
363d38ceaf9SAlex Deucher  * @ring: ring to start the fence driver on
364d38ceaf9SAlex Deucher  * @irq_src: interrupt source to use for this ring
365d38ceaf9SAlex Deucher  * @irq_type: interrupt type to use for this ring
366d38ceaf9SAlex Deucher  *
367d38ceaf9SAlex Deucher  * Make the fence driver ready for processing (all asics).
368d38ceaf9SAlex Deucher  * Not all asics have all rings, so each asic will only
369d38ceaf9SAlex Deucher  * start the fence driver on the rings it has.
370d38ceaf9SAlex Deucher  * Returns 0 for success, errors for failure.
371d38ceaf9SAlex Deucher  */
372d38ceaf9SAlex Deucher int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
373d38ceaf9SAlex Deucher 				   struct amdgpu_irq_src *irq_src,
374d38ceaf9SAlex Deucher 				   unsigned irq_type)
375d38ceaf9SAlex Deucher {
376d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = ring->adev;
377d38ceaf9SAlex Deucher 	uint64_t index;
378d38ceaf9SAlex Deucher 
379d38ceaf9SAlex Deucher 	if (ring != &adev->uvd.ring) {
380d38ceaf9SAlex Deucher 		ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
381d38ceaf9SAlex Deucher 		ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
382d38ceaf9SAlex Deucher 	} else {
383d38ceaf9SAlex Deucher 		/* put fence directly behind firmware */
384d38ceaf9SAlex Deucher 		index = ALIGN(adev->uvd.fw->size, 8);
385d38ceaf9SAlex Deucher 		ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
386d38ceaf9SAlex Deucher 		ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
387d38ceaf9SAlex Deucher 	}
388d38ceaf9SAlex Deucher 	amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
389c6a4079bSChunming Zhou 	amdgpu_irq_get(adev, irq_src, irq_type);
390c6a4079bSChunming Zhou 
391d38ceaf9SAlex Deucher 	ring->fence_drv.irq_src = irq_src;
392d38ceaf9SAlex Deucher 	ring->fence_drv.irq_type = irq_type;
393c6a4079bSChunming Zhou 	ring->fence_drv.initialized = true;
394c6a4079bSChunming Zhou 
395d38ceaf9SAlex Deucher 	dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
396d38ceaf9SAlex Deucher 		 "cpu addr 0x%p\n", ring->idx,
397d38ceaf9SAlex Deucher 		 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
398d38ceaf9SAlex Deucher 	return 0;
399d38ceaf9SAlex Deucher }
400d38ceaf9SAlex Deucher 
401d38ceaf9SAlex Deucher /**
402d38ceaf9SAlex Deucher  * amdgpu_fence_driver_init_ring - init the fence driver
403d38ceaf9SAlex Deucher  * for the requested ring.
404d38ceaf9SAlex Deucher  *
405d38ceaf9SAlex Deucher  * @ring: ring to init the fence driver on
406d38ceaf9SAlex Deucher  *
407d38ceaf9SAlex Deucher  * Init the fence driver for the requested ring (all asics).
408d38ceaf9SAlex Deucher  * Helper function for amdgpu_fence_driver_init().
409d38ceaf9SAlex Deucher  */
4104f839a24SChristian König int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
411d38ceaf9SAlex Deucher {
412cadf97b1SChunming Zhou 	long timeout;
4135907a0d8SChristian König 	int r;
414d38ceaf9SAlex Deucher 
415d38ceaf9SAlex Deucher 	ring->fence_drv.cpu_addr = NULL;
416d38ceaf9SAlex Deucher 	ring->fence_drv.gpu_addr = 0;
4175907a0d8SChristian König 	ring->fence_drv.sync_seq = 0;
418d38ceaf9SAlex Deucher 	atomic64_set(&ring->fence_drv.last_seq, 0);
419d38ceaf9SAlex Deucher 	ring->fence_drv.initialized = false;
420d38ceaf9SAlex Deucher 
421c2776afeSChristian König 	setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
422c2776afeSChristian König 		    (unsigned long)ring);
423b80d8475SAlex Deucher 
4245ec92a76SChristian König 	init_waitqueue_head(&ring->fence_drv.fence_queue);
4255ec92a76SChristian König 
426cadf97b1SChunming Zhou 	timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
4272440ff2cSJunwei Zhang 	if (timeout == 0) {
4282440ff2cSJunwei Zhang 		/*
4292440ff2cSJunwei Zhang 		 * FIXME:
4302440ff2cSJunwei Zhang 		 * Delayed workqueue cannot use it directly,
4312440ff2cSJunwei Zhang 		 * so the scheduler will not use delayed workqueue if
4322440ff2cSJunwei Zhang 		 * MAX_SCHEDULE_TIMEOUT is set.
4332440ff2cSJunwei Zhang 		 * Currently keep it simple and silly.
4342440ff2cSJunwei Zhang 		 */
4352440ff2cSJunwei Zhang 		timeout = MAX_SCHEDULE_TIMEOUT;
4362440ff2cSJunwei Zhang 	}
4374f839a24SChristian König 	r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
4382440ff2cSJunwei Zhang 			   amdgpu_sched_hw_submission,
4392440ff2cSJunwei Zhang 			   timeout, ring->name);
4404f839a24SChristian König 	if (r) {
4414f839a24SChristian König 		DRM_ERROR("Failed to create scheduler on ring %s.\n",
4424f839a24SChristian König 			  ring->name);
4434f839a24SChristian König 		return r;
444b80d8475SAlex Deucher 	}
445d38ceaf9SAlex Deucher 
4464f839a24SChristian König 	return 0;
4474f839a24SChristian König }
4484f839a24SChristian König 
449d38ceaf9SAlex Deucher /**
450d38ceaf9SAlex Deucher  * amdgpu_fence_driver_init - init the fence driver
451d38ceaf9SAlex Deucher  * for all possible rings.
452d38ceaf9SAlex Deucher  *
453d38ceaf9SAlex Deucher  * @adev: amdgpu device pointer
454d38ceaf9SAlex Deucher  *
455d38ceaf9SAlex Deucher  * Init the fence driver for all possible rings (all asics).
456d38ceaf9SAlex Deucher  * Not all asics have all rings, so each asic will only
457d38ceaf9SAlex Deucher  * start the fence driver on the rings it has using
458d38ceaf9SAlex Deucher  * amdgpu_fence_driver_start_ring().
459d38ceaf9SAlex Deucher  * Returns 0 for success.
460d38ceaf9SAlex Deucher  */
461d38ceaf9SAlex Deucher int amdgpu_fence_driver_init(struct amdgpu_device *adev)
462d38ceaf9SAlex Deucher {
463b49c84a5SChunming Zhou 	if (atomic_inc_return(&amdgpu_fence_slab_ref) == 1) {
464b49c84a5SChunming Zhou 		amdgpu_fence_slab = kmem_cache_create(
465b49c84a5SChunming Zhou 			"amdgpu_fence", sizeof(struct amdgpu_fence), 0,
466b49c84a5SChunming Zhou 			SLAB_HWCACHE_ALIGN, NULL);
467b49c84a5SChunming Zhou 		if (!amdgpu_fence_slab)
468b49c84a5SChunming Zhou 			return -ENOMEM;
469b49c84a5SChunming Zhou 	}
470d38ceaf9SAlex Deucher 	if (amdgpu_debugfs_fence_init(adev))
471d38ceaf9SAlex Deucher 		dev_err(adev->dev, "fence debugfs file creation failed\n");
472d38ceaf9SAlex Deucher 
473d38ceaf9SAlex Deucher 	return 0;
474d38ceaf9SAlex Deucher }
475d38ceaf9SAlex Deucher 
476d38ceaf9SAlex Deucher /**
477d38ceaf9SAlex Deucher  * amdgpu_fence_driver_fini - tear down the fence driver
478d38ceaf9SAlex Deucher  * for all possible rings.
479d38ceaf9SAlex Deucher  *
480d38ceaf9SAlex Deucher  * @adev: amdgpu device pointer
481d38ceaf9SAlex Deucher  *
482d38ceaf9SAlex Deucher  * Tear down the fence driver for all possible rings (all asics).
483d38ceaf9SAlex Deucher  */
484d38ceaf9SAlex Deucher void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
485d38ceaf9SAlex Deucher {
486d38ceaf9SAlex Deucher 	int i, r;
487d38ceaf9SAlex Deucher 
488b49c84a5SChunming Zhou 	if (atomic_dec_and_test(&amdgpu_fence_slab_ref))
489b49c84a5SChunming Zhou 		kmem_cache_destroy(amdgpu_fence_slab);
490d38ceaf9SAlex Deucher 	mutex_lock(&adev->ring_lock);
491d38ceaf9SAlex Deucher 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
492d38ceaf9SAlex Deucher 		struct amdgpu_ring *ring = adev->rings[i];
493c2776afeSChristian König 
494d38ceaf9SAlex Deucher 		if (!ring || !ring->fence_drv.initialized)
495d38ceaf9SAlex Deucher 			continue;
496d38ceaf9SAlex Deucher 		r = amdgpu_fence_wait_empty(ring);
497d38ceaf9SAlex Deucher 		if (r) {
498d38ceaf9SAlex Deucher 			/* no need to trigger GPU reset as we are unloading */
499d38ceaf9SAlex Deucher 			amdgpu_fence_driver_force_completion(adev);
500d38ceaf9SAlex Deucher 		}
5017f06c236Smonk.liu 		wake_up_all(&ring->fence_drv.fence_queue);
502c6a4079bSChunming Zhou 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
503c6a4079bSChunming Zhou 			       ring->fence_drv.irq_type);
5044f839a24SChristian König 		amd_sched_fini(&ring->sched);
505c2776afeSChristian König 		del_timer_sync(&ring->fence_drv.fallback_timer);
506d38ceaf9SAlex Deucher 		ring->fence_drv.initialized = false;
507d38ceaf9SAlex Deucher 	}
508d38ceaf9SAlex Deucher 	mutex_unlock(&adev->ring_lock);
509d38ceaf9SAlex Deucher }
510d38ceaf9SAlex Deucher 
511d38ceaf9SAlex Deucher /**
5125ceb54c6SAlex Deucher  * amdgpu_fence_driver_suspend - suspend the fence driver
5135ceb54c6SAlex Deucher  * for all possible rings.
5145ceb54c6SAlex Deucher  *
5155ceb54c6SAlex Deucher  * @adev: amdgpu device pointer
5165ceb54c6SAlex Deucher  *
5175ceb54c6SAlex Deucher  * Suspend the fence driver for all possible rings (all asics).
5185ceb54c6SAlex Deucher  */
5195ceb54c6SAlex Deucher void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
5205ceb54c6SAlex Deucher {
5215ceb54c6SAlex Deucher 	int i, r;
5225ceb54c6SAlex Deucher 
5235ceb54c6SAlex Deucher 	mutex_lock(&adev->ring_lock);
5245ceb54c6SAlex Deucher 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
5255ceb54c6SAlex Deucher 		struct amdgpu_ring *ring = adev->rings[i];
5265ceb54c6SAlex Deucher 		if (!ring || !ring->fence_drv.initialized)
5275ceb54c6SAlex Deucher 			continue;
5285ceb54c6SAlex Deucher 
5295ceb54c6SAlex Deucher 		/* wait for gpu to finish processing current batch */
5305ceb54c6SAlex Deucher 		r = amdgpu_fence_wait_empty(ring);
5315ceb54c6SAlex Deucher 		if (r) {
5325ceb54c6SAlex Deucher 			/* delay GPU reset to resume */
5335ceb54c6SAlex Deucher 			amdgpu_fence_driver_force_completion(adev);
5345ceb54c6SAlex Deucher 		}
5355ceb54c6SAlex Deucher 
5365ceb54c6SAlex Deucher 		/* disable the interrupt */
5375ceb54c6SAlex Deucher 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
5385ceb54c6SAlex Deucher 			       ring->fence_drv.irq_type);
5395ceb54c6SAlex Deucher 	}
5405ceb54c6SAlex Deucher 	mutex_unlock(&adev->ring_lock);
5415ceb54c6SAlex Deucher }
5425ceb54c6SAlex Deucher 
5435ceb54c6SAlex Deucher /**
5445ceb54c6SAlex Deucher  * amdgpu_fence_driver_resume - resume the fence driver
5455ceb54c6SAlex Deucher  * for all possible rings.
5465ceb54c6SAlex Deucher  *
5475ceb54c6SAlex Deucher  * @adev: amdgpu device pointer
5485ceb54c6SAlex Deucher  *
5495ceb54c6SAlex Deucher  * Resume the fence driver for all possible rings (all asics).
5505ceb54c6SAlex Deucher  * Not all asics have all rings, so each asic will only
5515ceb54c6SAlex Deucher  * start the fence driver on the rings it has using
5525ceb54c6SAlex Deucher  * amdgpu_fence_driver_start_ring().
5535ceb54c6SAlex Deucher  * Returns 0 for success.
5545ceb54c6SAlex Deucher  */
5555ceb54c6SAlex Deucher void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
5565ceb54c6SAlex Deucher {
5575ceb54c6SAlex Deucher 	int i;
5585ceb54c6SAlex Deucher 
5595ceb54c6SAlex Deucher 	mutex_lock(&adev->ring_lock);
5605ceb54c6SAlex Deucher 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
5615ceb54c6SAlex Deucher 		struct amdgpu_ring *ring = adev->rings[i];
5625ceb54c6SAlex Deucher 		if (!ring || !ring->fence_drv.initialized)
5635ceb54c6SAlex Deucher 			continue;
5645ceb54c6SAlex Deucher 
5655ceb54c6SAlex Deucher 		/* enable the interrupt */
5665ceb54c6SAlex Deucher 		amdgpu_irq_get(adev, ring->fence_drv.irq_src,
5675ceb54c6SAlex Deucher 			       ring->fence_drv.irq_type);
5685ceb54c6SAlex Deucher 	}
5695ceb54c6SAlex Deucher 	mutex_unlock(&adev->ring_lock);
5705ceb54c6SAlex Deucher }
5715ceb54c6SAlex Deucher 
5725ceb54c6SAlex Deucher /**
573d38ceaf9SAlex Deucher  * amdgpu_fence_driver_force_completion - force all fence waiter to complete
574d38ceaf9SAlex Deucher  *
575d38ceaf9SAlex Deucher  * @adev: amdgpu device pointer
576d38ceaf9SAlex Deucher  *
577d38ceaf9SAlex Deucher  * In case of GPU reset failure make sure no process keep waiting on fence
578d38ceaf9SAlex Deucher  * that will never complete.
579d38ceaf9SAlex Deucher  */
580d38ceaf9SAlex Deucher void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
581d38ceaf9SAlex Deucher {
582d38ceaf9SAlex Deucher 	int i;
583d38ceaf9SAlex Deucher 
584d38ceaf9SAlex Deucher 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
585d38ceaf9SAlex Deucher 		struct amdgpu_ring *ring = adev->rings[i];
586d38ceaf9SAlex Deucher 		if (!ring || !ring->fence_drv.initialized)
587d38ceaf9SAlex Deucher 			continue;
588d38ceaf9SAlex Deucher 
5895907a0d8SChristian König 		amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
590d38ceaf9SAlex Deucher 	}
591d38ceaf9SAlex Deucher }
592d38ceaf9SAlex Deucher 
593a95e2642SChristian König /*
594a95e2642SChristian König  * Common fence implementation
595a95e2642SChristian König  */
596a95e2642SChristian König 
597a95e2642SChristian König static const char *amdgpu_fence_get_driver_name(struct fence *fence)
598a95e2642SChristian König {
599a95e2642SChristian König 	return "amdgpu";
600a95e2642SChristian König }
601a95e2642SChristian König 
602a95e2642SChristian König static const char *amdgpu_fence_get_timeline_name(struct fence *f)
603a95e2642SChristian König {
604a95e2642SChristian König 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
605a95e2642SChristian König 	return (const char *)fence->ring->name;
606a95e2642SChristian König }
607a95e2642SChristian König 
608a95e2642SChristian König /**
609a95e2642SChristian König  * amdgpu_fence_is_signaled - test if fence is signaled
610a95e2642SChristian König  *
611a95e2642SChristian König  * @f: fence to test
612a95e2642SChristian König  *
613a95e2642SChristian König  * Test the fence sequence number if it is already signaled. If it isn't
614a95e2642SChristian König  * signaled start fence processing. Returns True if the fence is signaled.
615a95e2642SChristian König  */
616a95e2642SChristian König static bool amdgpu_fence_is_signaled(struct fence *f)
617a95e2642SChristian König {
618a95e2642SChristian König 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
619a95e2642SChristian König 	struct amdgpu_ring *ring = fence->ring;
620a95e2642SChristian König 
621a95e2642SChristian König 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
622a95e2642SChristian König 		return true;
623a95e2642SChristian König 
624a95e2642SChristian König 	amdgpu_fence_process(ring);
625a95e2642SChristian König 
626a95e2642SChristian König 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
627a95e2642SChristian König 		return true;
628a95e2642SChristian König 
629a95e2642SChristian König 	return false;
630a95e2642SChristian König }
631a95e2642SChristian König 
632a95e2642SChristian König /**
633a95e2642SChristian König  * amdgpu_fence_check_signaled - callback from fence_queue
634a95e2642SChristian König  *
635a95e2642SChristian König  * this function is called with fence_queue lock held, which is also used
636a95e2642SChristian König  * for the fence locking itself, so unlocked variants are used for
637a95e2642SChristian König  * fence_signal, and remove_wait_queue.
638a95e2642SChristian König  */
639a95e2642SChristian König static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
640a95e2642SChristian König {
641a95e2642SChristian König 	struct amdgpu_fence *fence;
642a95e2642SChristian König 	struct amdgpu_device *adev;
643a95e2642SChristian König 	u64 seq;
644a95e2642SChristian König 	int ret;
645a95e2642SChristian König 
646a95e2642SChristian König 	fence = container_of(wait, struct amdgpu_fence, fence_wake);
647a95e2642SChristian König 	adev = fence->ring->adev;
648a95e2642SChristian König 
649a95e2642SChristian König 	/*
650a95e2642SChristian König 	 * We cannot use amdgpu_fence_process here because we're already
651a95e2642SChristian König 	 * in the waitqueue, in a call from wake_up_all.
652a95e2642SChristian König 	 */
653a95e2642SChristian König 	seq = atomic64_read(&fence->ring->fence_drv.last_seq);
654a95e2642SChristian König 	if (seq >= fence->seq) {
655a95e2642SChristian König 		ret = fence_signal_locked(&fence->base);
656a95e2642SChristian König 		if (!ret)
657a95e2642SChristian König 			FENCE_TRACE(&fence->base, "signaled from irq context\n");
658a95e2642SChristian König 		else
659a95e2642SChristian König 			FENCE_TRACE(&fence->base, "was already signaled\n");
660a95e2642SChristian König 
661a95e2642SChristian König 		__remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
662a95e2642SChristian König 		fence_put(&fence->base);
663a95e2642SChristian König 	} else
664a95e2642SChristian König 		FENCE_TRACE(&fence->base, "pending\n");
665a95e2642SChristian König 	return 0;
666a95e2642SChristian König }
667a95e2642SChristian König 
668a95e2642SChristian König /**
669a95e2642SChristian König  * amdgpu_fence_enable_signaling - enable signalling on fence
670a95e2642SChristian König  * @fence: fence
671a95e2642SChristian König  *
672a95e2642SChristian König  * This function is called with fence_queue lock held, and adds a callback
673a95e2642SChristian König  * to fence_queue that checks if this fence is signaled, and if so it
674a95e2642SChristian König  * signals the fence and removes itself.
675a95e2642SChristian König  */
676a95e2642SChristian König static bool amdgpu_fence_enable_signaling(struct fence *f)
677a95e2642SChristian König {
678a95e2642SChristian König 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
679a95e2642SChristian König 	struct amdgpu_ring *ring = fence->ring;
680a95e2642SChristian König 
681a95e2642SChristian König 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
682a95e2642SChristian König 		return false;
683a95e2642SChristian König 
684a95e2642SChristian König 	fence->fence_wake.flags = 0;
685a95e2642SChristian König 	fence->fence_wake.private = NULL;
686a95e2642SChristian König 	fence->fence_wake.func = amdgpu_fence_check_signaled;
687a95e2642SChristian König 	__add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
688a95e2642SChristian König 	fence_get(f);
689c2776afeSChristian König 	if (!timer_pending(&ring->fence_drv.fallback_timer))
690c2776afeSChristian König 		amdgpu_fence_schedule_fallback(ring);
691a95e2642SChristian König 	FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
692a95e2642SChristian König 	return true;
693a95e2642SChristian König }
694a95e2642SChristian König 
695b49c84a5SChunming Zhou static void amdgpu_fence_release(struct fence *f)
696b49c84a5SChunming Zhou {
697b49c84a5SChunming Zhou 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
698b49c84a5SChunming Zhou 	kmem_cache_free(amdgpu_fence_slab, fence);
699b49c84a5SChunming Zhou }
700b49c84a5SChunming Zhou 
701a95e2642SChristian König const struct fence_ops amdgpu_fence_ops = {
702a95e2642SChristian König 	.get_driver_name = amdgpu_fence_get_driver_name,
703a95e2642SChristian König 	.get_timeline_name = amdgpu_fence_get_timeline_name,
704a95e2642SChristian König 	.enable_signaling = amdgpu_fence_enable_signaling,
705a95e2642SChristian König 	.signaled = amdgpu_fence_is_signaled,
706a95e2642SChristian König 	.wait = fence_default_wait,
707b49c84a5SChunming Zhou 	.release = amdgpu_fence_release,
708a95e2642SChristian König };
709d38ceaf9SAlex Deucher 
710d38ceaf9SAlex Deucher /*
711d38ceaf9SAlex Deucher  * Fence debugfs
712d38ceaf9SAlex Deucher  */
713d38ceaf9SAlex Deucher #if defined(CONFIG_DEBUG_FS)
714d38ceaf9SAlex Deucher static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
715d38ceaf9SAlex Deucher {
716d38ceaf9SAlex Deucher 	struct drm_info_node *node = (struct drm_info_node *)m->private;
717d38ceaf9SAlex Deucher 	struct drm_device *dev = node->minor->dev;
718d38ceaf9SAlex Deucher 	struct amdgpu_device *adev = dev->dev_private;
7195907a0d8SChristian König 	int i;
720d38ceaf9SAlex Deucher 
721d38ceaf9SAlex Deucher 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
722d38ceaf9SAlex Deucher 		struct amdgpu_ring *ring = adev->rings[i];
723d38ceaf9SAlex Deucher 		if (!ring || !ring->fence_drv.initialized)
724d38ceaf9SAlex Deucher 			continue;
725d38ceaf9SAlex Deucher 
726d38ceaf9SAlex Deucher 		amdgpu_fence_process(ring);
727d38ceaf9SAlex Deucher 
728344c19f9SChristian König 		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
729d38ceaf9SAlex Deucher 		seq_printf(m, "Last signaled fence 0x%016llx\n",
730d38ceaf9SAlex Deucher 			   (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
731d38ceaf9SAlex Deucher 		seq_printf(m, "Last emitted        0x%016llx\n",
7325907a0d8SChristian König 			   ring->fence_drv.sync_seq);
733d38ceaf9SAlex Deucher 	}
734d38ceaf9SAlex Deucher 	return 0;
735d38ceaf9SAlex Deucher }
736d38ceaf9SAlex Deucher 
737d38ceaf9SAlex Deucher static struct drm_info_list amdgpu_debugfs_fence_list[] = {
738d38ceaf9SAlex Deucher 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
739d38ceaf9SAlex Deucher };
740d38ceaf9SAlex Deucher #endif
741d38ceaf9SAlex Deucher 
742d38ceaf9SAlex Deucher int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
743d38ceaf9SAlex Deucher {
744d38ceaf9SAlex Deucher #if defined(CONFIG_DEBUG_FS)
745d38ceaf9SAlex Deucher 	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
746d38ceaf9SAlex Deucher #else
747d38ceaf9SAlex Deucher 	return 0;
748d38ceaf9SAlex Deucher #endif
749d38ceaf9SAlex Deucher }
750d38ceaf9SAlex Deucher 
751