xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_fence.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*
2  * Copyright 2009 Jerome Glisse.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * The above copyright notice and this permission notice (including the
22  * next paragraph) shall be included in all copies or substantial portions
23  * of the Software.
24  *
25  */
26 /*
27  * Authors:
28  *    Jerome Glisse <glisse@freedesktop.org>
29  *    Dave Airlie
30  */
31 #include <linux/seq_file.h>
32 #include <linux/atomic.h>
33 #include <linux/wait.h>
34 #include <linux/kref.h>
35 #include <linux/slab.h>
36 #include <linux/firmware.h>
37 #include <drm/drmP.h>
38 #include "amdgpu.h"
39 #include "amdgpu_trace.h"
40 
41 /*
42  * Fences
43  * Fences mark an event in the GPUs pipeline and are used
44  * for GPU/CPU synchronization.  When the fence is written,
45  * it is expected that all buffers associated with that fence
46  * are no longer in use by the associated ring on the GPU and
47  * that the the relevant GPU caches have been flushed.
48  */
49 
50 struct amdgpu_fence {
51 	struct dma_fence base;
52 
53 	/* RB, DMA, etc. */
54 	struct amdgpu_ring		*ring;
55 };
56 
57 static struct pool amdgpu_fence_slab;
58 
59 int amdgpu_fence_slab_init(void)
60 {
61 #ifdef __linux__
62 	amdgpu_fence_slab = kmem_cache_create(
63 		"amdgpu_fence", sizeof(struct amdgpu_fence), 0,
64 		SLAB_HWCACHE_ALIGN, NULL);
65 	if (!amdgpu_fence_slab)
66 		return -ENOMEM;
67 #else
68 	pool_init(&amdgpu_fence_slab, sizeof(struct amdgpu_fence),
69 	    0, IPL_TTY, 0, "amdgpu_fence", NULL);
70 #endif
71 	return 0;
72 }
73 
74 void amdgpu_fence_slab_fini(void)
75 {
76 	rcu_barrier();
77 #ifdef __linux__
78 	kmem_cache_destroy(amdgpu_fence_slab);
79 #else
80 	pool_destroy(&amdgpu_fence_slab);
81 #endif
82 }
83 /*
84  * Cast helper
85  */
86 static const struct dma_fence_ops amdgpu_fence_ops;
87 static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)
88 {
89 	struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
90 
91 	if (__f->base.ops == &amdgpu_fence_ops)
92 		return __f;
93 
94 	return NULL;
95 }
96 
97 /**
98  * amdgpu_fence_write - write a fence value
99  *
100  * @ring: ring the fence is associated with
101  * @seq: sequence number to write
102  *
103  * Writes a fence value to memory (all asics).
104  */
105 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
106 {
107 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
108 
109 	if (drv->cpu_addr)
110 		*drv->cpu_addr = cpu_to_le32(seq);
111 }
112 
113 /**
114  * amdgpu_fence_read - read a fence value
115  *
116  * @ring: ring the fence is associated with
117  *
118  * Reads a fence value from memory (all asics).
119  * Returns the value of the fence read from memory.
120  */
121 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
122 {
123 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
124 	u32 seq = 0;
125 
126 	if (drv->cpu_addr)
127 		seq = le32_to_cpu(*drv->cpu_addr);
128 	else
129 		seq = atomic_read(&drv->last_seq);
130 
131 	return seq;
132 }
133 
134 /**
135  * amdgpu_fence_emit - emit a fence on the requested ring
136  *
137  * @ring: ring the fence is associated with
138  * @f: resulting fence object
139  *
140  * Emits a fence command on the requested ring (all asics).
141  * Returns 0 on success, -ENOMEM on failure.
142  */
143 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
144 		      unsigned flags)
145 {
146 	struct amdgpu_device *adev = ring->adev;
147 	struct amdgpu_fence *fence;
148 	struct dma_fence __rcu **ptr;
149 	uint32_t seq;
150 	int r;
151 
152 #ifdef __linux__
153 	fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
154 #else
155 	fence = pool_get(&amdgpu_fence_slab, PR_WAITOK);
156 #endif
157 	if (fence == NULL)
158 		return -ENOMEM;
159 
160 	seq = ++ring->fence_drv.sync_seq;
161 	fence->ring = ring;
162 	dma_fence_init(&fence->base, &amdgpu_fence_ops,
163 		       &ring->fence_drv.lock,
164 		       adev->fence_context + ring->idx,
165 		       seq);
166 	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
167 			       seq, flags | AMDGPU_FENCE_FLAG_INT);
168 
169 	ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
170 	if (unlikely(rcu_dereference_protected(*ptr, 1))) {
171 		struct dma_fence *old;
172 
173 		rcu_read_lock();
174 		old = dma_fence_get_rcu_safe(ptr);
175 		rcu_read_unlock();
176 
177 		if (old) {
178 			r = dma_fence_wait(old, false);
179 			dma_fence_put(old);
180 			if (r)
181 				return r;
182 		}
183 	}
184 
185 	/* This function can't be called concurrently anyway, otherwise
186 	 * emitting the fence would mess up the hardware ring buffer.
187 	 */
188 	rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
189 
190 	*f = &fence->base;
191 
192 	return 0;
193 }
194 
195 /**
196  * amdgpu_fence_emit_polling - emit a fence on the requeste ring
197  *
198  * @ring: ring the fence is associated with
199  * @s: resulting sequence number
200  *
201  * Emits a fence command on the requested ring (all asics).
202  * Used For polling fence.
203  * Returns 0 on success, -ENOMEM on failure.
204  */
205 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s)
206 {
207 	uint32_t seq;
208 
209 	if (!s)
210 		return -EINVAL;
211 
212 	seq = ++ring->fence_drv.sync_seq;
213 	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
214 			       seq, 0);
215 
216 	*s = seq;
217 
218 	return 0;
219 }
220 
221 /**
222  * amdgpu_fence_schedule_fallback - schedule fallback check
223  *
224  * @ring: pointer to struct amdgpu_ring
225  *
226  * Start a timer as fallback to our interrupts.
227  */
228 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
229 {
230 	mod_timer(&ring->fence_drv.fallback_timer,
231 		  jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
232 }
233 
234 /**
235  * amdgpu_fence_process - check for fence activity
236  *
237  * @ring: pointer to struct amdgpu_ring
238  *
239  * Checks the current fence value and calculates the last
240  * signalled fence value. Wakes the fence queue if the
241  * sequence number has increased.
242  */
243 void amdgpu_fence_process(struct amdgpu_ring *ring)
244 {
245 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
246 	uint32_t seq, last_seq;
247 	int r;
248 
249 	do {
250 		last_seq = atomic_read(&ring->fence_drv.last_seq);
251 		seq = amdgpu_fence_read(ring);
252 
253 	} while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq);
254 
255 	if (seq != ring->fence_drv.sync_seq)
256 		amdgpu_fence_schedule_fallback(ring);
257 
258 	if (unlikely(seq == last_seq))
259 		return;
260 
261 	last_seq &= drv->num_fences_mask;
262 	seq &= drv->num_fences_mask;
263 
264 	do {
265 		struct dma_fence *fence, **ptr;
266 
267 		++last_seq;
268 		last_seq &= drv->num_fences_mask;
269 		ptr = &drv->fences[last_seq];
270 
271 		/* There is always exactly one thread signaling this fence slot */
272 		fence = rcu_dereference_protected(*ptr, 1);
273 		RCU_INIT_POINTER(*ptr, NULL);
274 
275 		if (!fence)
276 			continue;
277 
278 		r = dma_fence_signal(fence);
279 		if (!r)
280 			DMA_FENCE_TRACE(fence, "signaled from irq context\n");
281 		else
282 			BUG();
283 
284 		dma_fence_put(fence);
285 	} while (last_seq != seq);
286 }
287 
288 /**
289  * amdgpu_fence_fallback - fallback for hardware interrupts
290  *
291  * @work: delayed work item
292  *
293  * Checks for fence activity.
294  */
295 static void amdgpu_fence_fallback(void *arg)
296 {
297 	struct amdgpu_ring *ring = arg;
298 
299 	amdgpu_fence_process(ring);
300 }
301 
302 /**
303  * amdgpu_fence_wait_empty - wait for all fences to signal
304  *
305  * @adev: amdgpu device pointer
306  * @ring: ring index the fence is associated with
307  *
308  * Wait for all fences on the requested ring to signal (all asics).
309  * Returns 0 if the fences have passed, error for all other cases.
310  */
311 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
312 {
313 	uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq);
314 	struct dma_fence *fence, **ptr;
315 	int r;
316 
317 	if (!seq)
318 		return 0;
319 
320 	ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
321 	rcu_read_lock();
322 	fence = rcu_dereference(*ptr);
323 	if (!fence || !dma_fence_get_rcu(fence)) {
324 		rcu_read_unlock();
325 		return 0;
326 	}
327 	rcu_read_unlock();
328 
329 	r = dma_fence_wait(fence, false);
330 	dma_fence_put(fence);
331 	return r;
332 }
333 
334 /**
335  * amdgpu_fence_wait_polling - busy wait for givn sequence number
336  *
337  * @ring: ring index the fence is associated with
338  * @wait_seq: sequence number to wait
339  * @timeout: the timeout for waiting in usecs
340  *
341  * Wait for all fences on the requested ring to signal (all asics).
342  * Returns left time if no timeout, 0 or minus if timeout.
343  */
344 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
345 				      uint32_t wait_seq,
346 				      signed long timeout)
347 {
348 	uint32_t seq;
349 
350 	do {
351 		seq = amdgpu_fence_read(ring);
352 		udelay(5);
353 		timeout -= 5;
354 	} while ((int32_t)(wait_seq - seq) > 0 && timeout > 0);
355 
356 	return timeout > 0 ? timeout : 0;
357 }
358 /**
359  * amdgpu_fence_count_emitted - get the count of emitted fences
360  *
361  * @ring: ring the fence is associated with
362  *
363  * Get the number of fences emitted on the requested ring (all asics).
364  * Returns the number of emitted fences on the ring.  Used by the
365  * dynpm code to ring track activity.
366  */
367 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
368 {
369 	uint64_t emitted;
370 
371 	/* We are not protected by ring lock when reading the last sequence
372 	 * but it's ok to report slightly wrong fence count here.
373 	 */
374 	amdgpu_fence_process(ring);
375 	emitted = 0x100000000ull;
376 	emitted -= atomic_read(&ring->fence_drv.last_seq);
377 	emitted += READ_ONCE(ring->fence_drv.sync_seq);
378 	return lower_32_bits(emitted);
379 }
380 
381 /**
382  * amdgpu_fence_driver_start_ring - make the fence driver
383  * ready for use on the requested ring.
384  *
385  * @ring: ring to start the fence driver on
386  * @irq_src: interrupt source to use for this ring
387  * @irq_type: interrupt type to use for this ring
388  *
389  * Make the fence driver ready for processing (all asics).
390  * Not all asics have all rings, so each asic will only
391  * start the fence driver on the rings it has.
392  * Returns 0 for success, errors for failure.
393  */
394 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
395 				   struct amdgpu_irq_src *irq_src,
396 				   unsigned irq_type)
397 {
398 	struct amdgpu_device *adev = ring->adev;
399 	uint64_t index;
400 
401 	if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) {
402 		ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
403 		ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
404 	} else {
405 		/* put fence directly behind firmware */
406 		index = roundup2(adev->uvd.fw->size, 8);
407 		ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index;
408 		ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index;
409 	}
410 	amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq));
411 	amdgpu_irq_get(adev, irq_src, irq_type);
412 
413 	ring->fence_drv.irq_src = irq_src;
414 	ring->fence_drv.irq_type = irq_type;
415 	ring->fence_drv.initialized = true;
416 
417 	dev_dbg(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
418 		"cpu addr 0x%p\n", ring->idx,
419 		ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
420 	return 0;
421 }
422 
423 /**
424  * amdgpu_fence_driver_init_ring - init the fence driver
425  * for the requested ring.
426  *
427  * @ring: ring to init the fence driver on
428  * @num_hw_submission: number of entries on the hardware queue
429  *
430  * Init the fence driver for the requested ring (all asics).
431  * Helper function for amdgpu_fence_driver_init().
432  */
433 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
434 				  unsigned num_hw_submission)
435 {
436 	long timeout;
437 	int r;
438 
439 	/* Check that num_hw_submission is a power of two */
440 	if ((num_hw_submission & (num_hw_submission - 1)) != 0)
441 		return -EINVAL;
442 
443 	ring->fence_drv.cpu_addr = NULL;
444 	ring->fence_drv.gpu_addr = 0;
445 	ring->fence_drv.sync_seq = 0;
446 	atomic_set(&ring->fence_drv.last_seq, 0);
447 	ring->fence_drv.initialized = false;
448 
449 #ifdef __linux__
450 	timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
451 #else
452 	timeout_set(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
453 	    ring);
454 #endif
455 
456 	ring->fence_drv.num_fences_mask = num_hw_submission * 2 - 1;
457 	mtx_init(&ring->fence_drv.lock, IPL_TTY);
458 	ring->fence_drv.fences = kcalloc(num_hw_submission * 2, sizeof(void *),
459 					 GFP_KERNEL);
460 	if (!ring->fence_drv.fences)
461 		return -ENOMEM;
462 
463 	/* No need to setup the GPU scheduler for KIQ ring */
464 	if (ring->funcs->type != AMDGPU_RING_TYPE_KIQ) {
465 		/* for non-sriov case, no timeout enforce on compute ring */
466 		if ((ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
467 				&& !amdgpu_sriov_vf(ring->adev))
468 			timeout = MAX_SCHEDULE_TIMEOUT;
469 		else
470 			timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
471 
472 		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
473 				   num_hw_submission, amdgpu_job_hang_limit,
474 				   timeout, ring->name);
475 		if (r) {
476 			DRM_ERROR("Failed to create scheduler on ring %s.\n",
477 				  ring->name);
478 			return r;
479 		}
480 	}
481 
482 	return 0;
483 }
484 
485 /**
486  * amdgpu_fence_driver_init - init the fence driver
487  * for all possible rings.
488  *
489  * @adev: amdgpu device pointer
490  *
491  * Init the fence driver for all possible rings (all asics).
492  * Not all asics have all rings, so each asic will only
493  * start the fence driver on the rings it has using
494  * amdgpu_fence_driver_start_ring().
495  * Returns 0 for success.
496  */
497 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
498 {
499 	if (amdgpu_debugfs_fence_init(adev))
500 		dev_err(adev->dev, "fence debugfs file creation failed\n");
501 
502 	return 0;
503 }
504 
505 /**
506  * amdgpu_fence_driver_fini - tear down the fence driver
507  * for all possible rings.
508  *
509  * @adev: amdgpu device pointer
510  *
511  * Tear down the fence driver for all possible rings (all asics).
512  */
513 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
514 {
515 	unsigned i, j;
516 	int r;
517 
518 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
519 		struct amdgpu_ring *ring = adev->rings[i];
520 
521 		if (!ring || !ring->fence_drv.initialized)
522 			continue;
523 		r = amdgpu_fence_wait_empty(ring);
524 		if (r) {
525 			/* no need to trigger GPU reset as we are unloading */
526 			amdgpu_fence_driver_force_completion(ring);
527 		}
528 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
529 			       ring->fence_drv.irq_type);
530 		drm_sched_fini(&ring->sched);
531 		del_timer_sync(&ring->fence_drv.fallback_timer);
532 		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
533 			dma_fence_put(ring->fence_drv.fences[j]);
534 		kfree(ring->fence_drv.fences);
535 		ring->fence_drv.fences = NULL;
536 		ring->fence_drv.initialized = false;
537 	}
538 }
539 
540 /**
541  * amdgpu_fence_driver_suspend - suspend the fence driver
542  * for all possible rings.
543  *
544  * @adev: amdgpu device pointer
545  *
546  * Suspend the fence driver for all possible rings (all asics).
547  */
548 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
549 {
550 	int i, r;
551 
552 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
553 		struct amdgpu_ring *ring = adev->rings[i];
554 		if (!ring || !ring->fence_drv.initialized)
555 			continue;
556 
557 		/* wait for gpu to finish processing current batch */
558 		r = amdgpu_fence_wait_empty(ring);
559 		if (r) {
560 			/* delay GPU reset to resume */
561 			amdgpu_fence_driver_force_completion(ring);
562 		}
563 
564 		/* disable the interrupt */
565 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
566 			       ring->fence_drv.irq_type);
567 	}
568 }
569 
570 /**
571  * amdgpu_fence_driver_resume - resume the fence driver
572  * for all possible rings.
573  *
574  * @adev: amdgpu device pointer
575  *
576  * Resume the fence driver for all possible rings (all asics).
577  * Not all asics have all rings, so each asic will only
578  * start the fence driver on the rings it has using
579  * amdgpu_fence_driver_start_ring().
580  * Returns 0 for success.
581  */
582 void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
583 {
584 	int i;
585 
586 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
587 		struct amdgpu_ring *ring = adev->rings[i];
588 		if (!ring || !ring->fence_drv.initialized)
589 			continue;
590 
591 		/* enable the interrupt */
592 		amdgpu_irq_get(adev, ring->fence_drv.irq_src,
593 			       ring->fence_drv.irq_type);
594 	}
595 }
596 
597 /**
598  * amdgpu_fence_driver_force_completion - force signal latest fence of ring
599  *
600  * @ring: fence of the ring to signal
601  *
602  */
603 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
604 {
605 	amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
606 	amdgpu_fence_process(ring);
607 }
608 
609 /*
610  * Common fence implementation
611  */
612 
613 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
614 {
615 	return "amdgpu";
616 }
617 
618 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
619 {
620 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
621 	return (const char *)fence->ring->name;
622 }
623 
624 /**
625  * amdgpu_fence_enable_signaling - enable signalling on fence
626  * @fence: fence
627  *
628  * This function is called with fence_queue lock held, and adds a callback
629  * to fence_queue that checks if this fence is signaled, and if so it
630  * signals the fence and removes itself.
631  */
632 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
633 {
634 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
635 	struct amdgpu_ring *ring = fence->ring;
636 
637 	if (!timer_pending(&ring->fence_drv.fallback_timer))
638 		amdgpu_fence_schedule_fallback(ring);
639 
640 	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
641 
642 	return true;
643 }
644 
645 /**
646  * amdgpu_fence_free - free up the fence memory
647  *
648  * @rcu: RCU callback head
649  *
650  * Free up the fence memory after the RCU grace period.
651  */
652 static void amdgpu_fence_free(struct rcu_head *rcu)
653 {
654 	struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
655 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
656 #ifdef __linux__
657 	kmem_cache_free(amdgpu_fence_slab, fence);
658 #else
659 	pool_put(&amdgpu_fence_slab, fence);
660 #endif
661 }
662 
663 /**
664  * amdgpu_fence_release - callback that fence can be freed
665  *
666  * @fence: fence
667  *
668  * This function is called when the reference count becomes zero.
669  * It just RCU schedules freeing up the fence.
670  */
671 static void amdgpu_fence_release(struct dma_fence *f)
672 {
673 	call_rcu(&f->rcu, amdgpu_fence_free);
674 }
675 
676 static const struct dma_fence_ops amdgpu_fence_ops = {
677 	.get_driver_name = amdgpu_fence_get_driver_name,
678 	.get_timeline_name = amdgpu_fence_get_timeline_name,
679 	.enable_signaling = amdgpu_fence_enable_signaling,
680 	.release = amdgpu_fence_release,
681 };
682 
683 /*
684  * Fence debugfs
685  */
686 #if defined(CONFIG_DEBUG_FS)
687 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
688 {
689 	struct drm_info_node *node = (struct drm_info_node *)m->private;
690 	struct drm_device *dev = node->minor->dev;
691 	struct amdgpu_device *adev = dev->dev_private;
692 	int i;
693 
694 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
695 		struct amdgpu_ring *ring = adev->rings[i];
696 		if (!ring || !ring->fence_drv.initialized)
697 			continue;
698 
699 		amdgpu_fence_process(ring);
700 
701 		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
702 		seq_printf(m, "Last signaled fence 0x%08x\n",
703 			   atomic_read(&ring->fence_drv.last_seq));
704 		seq_printf(m, "Last emitted        0x%08x\n",
705 			   ring->fence_drv.sync_seq);
706 
707 		if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
708 			continue;
709 
710 		/* set in CP_VMID_PREEMPT and preemption occurred */
711 		seq_printf(m, "Last preempted      0x%08x\n",
712 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
713 		/* set in CP_VMID_RESET and reset occurred */
714 		seq_printf(m, "Last reset          0x%08x\n",
715 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
716 		/* Both preemption and reset occurred */
717 		seq_printf(m, "Last both           0x%08x\n",
718 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
719 	}
720 	return 0;
721 }
722 
723 /**
724  * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
725  *
726  * Manually trigger a gpu reset at the next fence wait.
727  */
728 static int amdgpu_debugfs_gpu_recover(struct seq_file *m, void *data)
729 {
730 	struct drm_info_node *node = (struct drm_info_node *) m->private;
731 	struct drm_device *dev = node->minor->dev;
732 	struct amdgpu_device *adev = dev->dev_private;
733 
734 	seq_printf(m, "gpu recover\n");
735 	amdgpu_device_gpu_recover(adev, NULL, true);
736 
737 	return 0;
738 }
739 
740 static const struct drm_info_list amdgpu_debugfs_fence_list[] = {
741 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
742 	{"amdgpu_gpu_recover", &amdgpu_debugfs_gpu_recover, 0, NULL}
743 };
744 
745 static const struct drm_info_list amdgpu_debugfs_fence_list_sriov[] = {
746 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
747 };
748 #endif
749 
750 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
751 {
752 #if defined(CONFIG_DEBUG_FS)
753 	if (amdgpu_sriov_vf(adev))
754 		return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list_sriov, 1);
755 	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
756 #else
757 	return 0;
758 #endif
759 }
760 
761