xref: /openbsd/sys/dev/pci/drm/amd/amdgpu/amdgpu_fence.c (revision 09467b48)
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 	    0, 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 	amdgpu_irq_get(adev, irq_src, irq_type);
424 
425 	ring->fence_drv.irq_src = irq_src;
426 	ring->fence_drv.irq_type = irq_type;
427 	ring->fence_drv.initialized = true;
428 
429 	DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr "
430 		      "0x%016llx, cpu addr 0x%p\n", ring->name,
431 		      ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
432 	return 0;
433 }
434 
435 /**
436  * amdgpu_fence_driver_init_ring - init the fence driver
437  * for the requested ring.
438  *
439  * @ring: ring to init the fence driver on
440  * @num_hw_submission: number of entries on the hardware queue
441  *
442  * Init the fence driver for the requested ring (all asics).
443  * Helper function for amdgpu_fence_driver_init().
444  */
445 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
446 				  unsigned num_hw_submission)
447 {
448 	struct amdgpu_device *adev = ring->adev;
449 	long timeout;
450 	int r;
451 
452 	if (!adev)
453 		return -EINVAL;
454 
455 	/* Check that num_hw_submission is a power of two */
456 	if ((num_hw_submission & (num_hw_submission - 1)) != 0)
457 		return -EINVAL;
458 
459 	ring->fence_drv.cpu_addr = NULL;
460 	ring->fence_drv.gpu_addr = 0;
461 	ring->fence_drv.sync_seq = 0;
462 	atomic_set(&ring->fence_drv.last_seq, 0);
463 	ring->fence_drv.initialized = false;
464 
465 #ifdef __linux__
466 	timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
467 #else
468 	timeout_set(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
469 	    ring);
470 #endif
471 
472 	ring->fence_drv.num_fences_mask = num_hw_submission * 2 - 1;
473 	mtx_init(&ring->fence_drv.lock, IPL_TTY);
474 	ring->fence_drv.fences = kcalloc(num_hw_submission * 2, sizeof(void *),
475 					 GFP_KERNEL);
476 	if (!ring->fence_drv.fences)
477 		return -ENOMEM;
478 
479 	/* No need to setup the GPU scheduler for KIQ ring */
480 	if (ring->funcs->type != AMDGPU_RING_TYPE_KIQ) {
481 		switch (ring->funcs->type) {
482 		case AMDGPU_RING_TYPE_GFX:
483 			timeout = adev->gfx_timeout;
484 			break;
485 		case AMDGPU_RING_TYPE_COMPUTE:
486 			timeout = adev->compute_timeout;
487 			break;
488 		case AMDGPU_RING_TYPE_SDMA:
489 			timeout = adev->sdma_timeout;
490 			break;
491 		default:
492 			timeout = adev->video_timeout;
493 			break;
494 		}
495 
496 		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
497 				   num_hw_submission, amdgpu_job_hang_limit,
498 				   timeout, ring->name);
499 		if (r) {
500 			DRM_ERROR("Failed to create scheduler on ring %s.\n",
501 				  ring->name);
502 			return r;
503 		}
504 	}
505 
506 	return 0;
507 }
508 
509 /**
510  * amdgpu_fence_driver_init - init the fence driver
511  * for all possible rings.
512  *
513  * @adev: amdgpu device pointer
514  *
515  * Init the fence driver for all possible rings (all asics).
516  * Not all asics have all rings, so each asic will only
517  * start the fence driver on the rings it has using
518  * amdgpu_fence_driver_start_ring().
519  * Returns 0 for success.
520  */
521 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
522 {
523 	return 0;
524 }
525 
526 /**
527  * amdgpu_fence_driver_fini - tear down the fence driver
528  * for all possible rings.
529  *
530  * @adev: amdgpu device pointer
531  *
532  * Tear down the fence driver for all possible rings (all asics).
533  */
534 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
535 {
536 	unsigned i, j;
537 	int r;
538 
539 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
540 		struct amdgpu_ring *ring = adev->rings[i];
541 
542 		if (!ring || !ring->fence_drv.initialized)
543 			continue;
544 		r = amdgpu_fence_wait_empty(ring);
545 		if (r) {
546 			/* no need to trigger GPU reset as we are unloading */
547 			amdgpu_fence_driver_force_completion(ring);
548 		}
549 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
550 			       ring->fence_drv.irq_type);
551 		drm_sched_fini(&ring->sched);
552 		del_timer_sync(&ring->fence_drv.fallback_timer);
553 		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
554 			dma_fence_put(ring->fence_drv.fences[j]);
555 		kfree(ring->fence_drv.fences);
556 		ring->fence_drv.fences = NULL;
557 		ring->fence_drv.initialized = false;
558 	}
559 }
560 
561 /**
562  * amdgpu_fence_driver_suspend - suspend the fence driver
563  * for all possible rings.
564  *
565  * @adev: amdgpu device pointer
566  *
567  * Suspend the fence driver for all possible rings (all asics).
568  */
569 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
570 {
571 	int i, r;
572 
573 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
574 		struct amdgpu_ring *ring = adev->rings[i];
575 		if (!ring || !ring->fence_drv.initialized)
576 			continue;
577 
578 		/* wait for gpu to finish processing current batch */
579 		r = amdgpu_fence_wait_empty(ring);
580 		if (r) {
581 			/* delay GPU reset to resume */
582 			amdgpu_fence_driver_force_completion(ring);
583 		}
584 
585 		/* disable the interrupt */
586 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
587 			       ring->fence_drv.irq_type);
588 	}
589 }
590 
591 /**
592  * amdgpu_fence_driver_resume - resume the fence driver
593  * for all possible rings.
594  *
595  * @adev: amdgpu device pointer
596  *
597  * Resume the fence driver for all possible rings (all asics).
598  * Not all asics have all rings, so each asic will only
599  * start the fence driver on the rings it has using
600  * amdgpu_fence_driver_start_ring().
601  * Returns 0 for success.
602  */
603 void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
604 {
605 	int i;
606 
607 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
608 		struct amdgpu_ring *ring = adev->rings[i];
609 		if (!ring || !ring->fence_drv.initialized)
610 			continue;
611 
612 		/* enable the interrupt */
613 		amdgpu_irq_get(adev, ring->fence_drv.irq_src,
614 			       ring->fence_drv.irq_type);
615 	}
616 }
617 
618 /**
619  * amdgpu_fence_driver_force_completion - force signal latest fence of ring
620  *
621  * @ring: fence of the ring to signal
622  *
623  */
624 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
625 {
626 	amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
627 	amdgpu_fence_process(ring);
628 }
629 
630 /*
631  * Common fence implementation
632  */
633 
634 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
635 {
636 	return "amdgpu";
637 }
638 
639 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
640 {
641 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
642 	return (const char *)fence->ring->name;
643 }
644 
645 /**
646  * amdgpu_fence_enable_signaling - enable signalling on fence
647  * @fence: fence
648  *
649  * This function is called with fence_queue lock held, and adds a callback
650  * to fence_queue that checks if this fence is signaled, and if so it
651  * signals the fence and removes itself.
652  */
653 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
654 {
655 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
656 	struct amdgpu_ring *ring = fence->ring;
657 
658 	if (!timer_pending(&ring->fence_drv.fallback_timer))
659 		amdgpu_fence_schedule_fallback(ring);
660 
661 	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
662 
663 	return true;
664 }
665 
666 /**
667  * amdgpu_fence_free - free up the fence memory
668  *
669  * @rcu: RCU callback head
670  *
671  * Free up the fence memory after the RCU grace period.
672  */
673 static void amdgpu_fence_free(struct rcu_head *rcu)
674 {
675 	struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
676 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
677 #ifdef __linux__
678 	kmem_cache_free(amdgpu_fence_slab, fence);
679 #else
680 	pool_put(&amdgpu_fence_slab, fence);
681 #endif
682 }
683 
684 /**
685  * amdgpu_fence_release - callback that fence can be freed
686  *
687  * @fence: fence
688  *
689  * This function is called when the reference count becomes zero.
690  * It just RCU schedules freeing up the fence.
691  */
692 static void amdgpu_fence_release(struct dma_fence *f)
693 {
694 	call_rcu(&f->rcu, amdgpu_fence_free);
695 }
696 
697 static const struct dma_fence_ops amdgpu_fence_ops = {
698 	.get_driver_name = amdgpu_fence_get_driver_name,
699 	.get_timeline_name = amdgpu_fence_get_timeline_name,
700 	.enable_signaling = amdgpu_fence_enable_signaling,
701 	.release = amdgpu_fence_release,
702 };
703 
704 /*
705  * Fence debugfs
706  */
707 #if defined(CONFIG_DEBUG_FS)
708 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
709 {
710 	struct drm_info_node *node = (struct drm_info_node *)m->private;
711 	struct drm_device *dev = node->minor->dev;
712 	struct amdgpu_device *adev = dev->dev_private;
713 	int i;
714 
715 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
716 		struct amdgpu_ring *ring = adev->rings[i];
717 		if (!ring || !ring->fence_drv.initialized)
718 			continue;
719 
720 		amdgpu_fence_process(ring);
721 
722 		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
723 		seq_printf(m, "Last signaled fence          0x%08x\n",
724 			   atomic_read(&ring->fence_drv.last_seq));
725 		seq_printf(m, "Last emitted                 0x%08x\n",
726 			   ring->fence_drv.sync_seq);
727 
728 		if (ring->funcs->type == AMDGPU_RING_TYPE_GFX ||
729 		    ring->funcs->type == AMDGPU_RING_TYPE_SDMA) {
730 			seq_printf(m, "Last signaled trailing fence 0x%08x\n",
731 				   le32_to_cpu(*ring->trail_fence_cpu_addr));
732 			seq_printf(m, "Last emitted                 0x%08x\n",
733 				   ring->trail_seq);
734 		}
735 
736 		if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
737 			continue;
738 
739 		/* set in CP_VMID_PREEMPT and preemption occurred */
740 		seq_printf(m, "Last preempted               0x%08x\n",
741 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
742 		/* set in CP_VMID_RESET and reset occurred */
743 		seq_printf(m, "Last reset                   0x%08x\n",
744 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
745 		/* Both preemption and reset occurred */
746 		seq_printf(m, "Last both                    0x%08x\n",
747 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
748 	}
749 	return 0;
750 }
751 
752 /**
753  * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
754  *
755  * Manually trigger a gpu reset at the next fence wait.
756  */
757 static int amdgpu_debugfs_gpu_recover(struct seq_file *m, void *data)
758 {
759 	struct drm_info_node *node = (struct drm_info_node *) m->private;
760 	struct drm_device *dev = node->minor->dev;
761 	struct amdgpu_device *adev = dev->dev_private;
762 	int r;
763 
764 	r = pm_runtime_get_sync(dev->dev);
765 	if (r < 0)
766 		return 0;
767 
768 	seq_printf(m, "gpu recover\n");
769 	amdgpu_device_gpu_recover(adev, NULL);
770 
771 	pm_runtime_mark_last_busy(dev->dev);
772 	pm_runtime_put_autosuspend(dev->dev);
773 
774 	return 0;
775 }
776 
777 static const struct drm_info_list amdgpu_debugfs_fence_list[] = {
778 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
779 	{"amdgpu_gpu_recover", &amdgpu_debugfs_gpu_recover, 0, NULL}
780 };
781 
782 static const struct drm_info_list amdgpu_debugfs_fence_list_sriov[] = {
783 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
784 };
785 #endif
786 
787 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
788 {
789 #if defined(CONFIG_DEBUG_FS)
790 	if (amdgpu_sriov_vf(adev))
791 		return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list_sriov, 1);
792 	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
793 #else
794 	return 0;
795 #endif
796 }
797 
798