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