xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_fence.c (revision 8dfe214903ce3625c937d5fad2469e8a0d1d4d71)
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_drv.h>
40 #include "amdgpu.h"
41 #include "amdgpu_trace.h"
42 #include "amdgpu_reset.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 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 const struct dma_fence_ops amdgpu_job_fence_ops;
91 static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)
92 {
93 	struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
94 
95 	if (__f->base.ops == &amdgpu_fence_ops ||
96 	    __f->base.ops == &amdgpu_job_fence_ops)
97 		return __f;
98 
99 	return NULL;
100 }
101 
102 /**
103  * amdgpu_fence_write - write a fence value
104  *
105  * @ring: ring the fence is associated with
106  * @seq: sequence number to write
107  *
108  * Writes a fence value to memory (all asics).
109  */
110 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
111 {
112 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
113 
114 	if (drv->cpu_addr)
115 		*drv->cpu_addr = cpu_to_le32(seq);
116 }
117 
118 /**
119  * amdgpu_fence_read - read a fence value
120  *
121  * @ring: ring the fence is associated with
122  *
123  * Reads a fence value from memory (all asics).
124  * Returns the value of the fence read from memory.
125  */
126 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
127 {
128 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
129 	u32 seq = 0;
130 
131 	if (drv->cpu_addr)
132 		seq = le32_to_cpu(*drv->cpu_addr);
133 	else
134 		seq = atomic_read(&drv->last_seq);
135 
136 	return seq;
137 }
138 
139 /**
140  * amdgpu_fence_emit - emit a fence on the requested ring
141  *
142  * @ring: ring the fence is associated with
143  * @f: resulting fence object
144  * @job: job the fence is embedded in
145  * @flags: flags to pass into the subordinate .emit_fence() call
146  *
147  * Emits a fence command on the requested ring (all asics).
148  * Returns 0 on success, -ENOMEM on failure.
149  */
150 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f, struct amdgpu_job *job,
151 		      unsigned flags)
152 {
153 	struct amdgpu_device *adev = ring->adev;
154 	struct dma_fence *fence;
155 	struct amdgpu_fence *am_fence;
156 	struct dma_fence __rcu **ptr;
157 	uint32_t seq;
158 	int r;
159 
160 	if (job == NULL) {
161 		/* create a sperate hw fence */
162 #ifdef __linux__
163 		am_fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_ATOMIC);
164 #else
165 		am_fence = pool_get(&amdgpu_fence_slab, PR_NOWAIT);
166 #endif
167 		if (am_fence == NULL)
168 			return -ENOMEM;
169 		fence = &am_fence->base;
170 		am_fence->ring = ring;
171 	} else {
172 		/* take use of job-embedded fence */
173 		fence = &job->hw_fence;
174 	}
175 
176 	seq = ++ring->fence_drv.sync_seq;
177 	if (job && job->job_run_counter) {
178 		/* reinit seq for resubmitted jobs */
179 		fence->seqno = seq;
180 		/* TO be inline with external fence creation and other drivers */
181 		dma_fence_get(fence);
182 	} else {
183 		if (job) {
184 			dma_fence_init(fence, &amdgpu_job_fence_ops,
185 				       &ring->fence_drv.lock,
186 				       adev->fence_context + ring->idx, seq);
187 			/* Against remove in amdgpu_job_{free, free_cb} */
188 			dma_fence_get(fence);
189 		}
190 		else
191 			dma_fence_init(fence, &amdgpu_fence_ops,
192 				       &ring->fence_drv.lock,
193 				       adev->fence_context + ring->idx, seq);
194 	}
195 
196 	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
197 			       seq, flags | AMDGPU_FENCE_FLAG_INT);
198 	pm_runtime_get_noresume(adev_to_drm(adev)->dev);
199 	ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
200 	if (unlikely(rcu_dereference_protected(*ptr, 1))) {
201 		struct dma_fence *old;
202 
203 		rcu_read_lock();
204 		old = dma_fence_get_rcu_safe(ptr);
205 		rcu_read_unlock();
206 
207 		if (old) {
208 			r = dma_fence_wait(old, false);
209 			dma_fence_put(old);
210 			if (r)
211 				return r;
212 		}
213 	}
214 
215 	/* This function can't be called concurrently anyway, otherwise
216 	 * emitting the fence would mess up the hardware ring buffer.
217 	 */
218 	rcu_assign_pointer(*ptr, dma_fence_get(fence));
219 
220 	*f = fence;
221 
222 	return 0;
223 }
224 
225 /**
226  * amdgpu_fence_emit_polling - emit a fence on the requeste ring
227  *
228  * @ring: ring the fence is associated with
229  * @s: resulting sequence number
230  * @timeout: the timeout for waiting in usecs
231  *
232  * Emits a fence command on the requested ring (all asics).
233  * Used For polling fence.
234  * Returns 0 on success, -ENOMEM on failure.
235  */
236 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s,
237 			      uint32_t timeout)
238 {
239 	uint32_t seq;
240 	signed long r;
241 
242 	if (!s)
243 		return -EINVAL;
244 
245 	seq = ++ring->fence_drv.sync_seq;
246 	r = amdgpu_fence_wait_polling(ring,
247 				      seq - ring->fence_drv.num_fences_mask,
248 				      timeout);
249 	if (r < 1)
250 		return -ETIMEDOUT;
251 
252 	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
253 			       seq, 0);
254 
255 	*s = seq;
256 
257 	return 0;
258 }
259 
260 /**
261  * amdgpu_fence_schedule_fallback - schedule fallback check
262  *
263  * @ring: pointer to struct amdgpu_ring
264  *
265  * Start a timer as fallback to our interrupts.
266  */
267 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
268 {
269 	mod_timer(&ring->fence_drv.fallback_timer,
270 		  jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
271 }
272 
273 /**
274  * amdgpu_fence_process - check for fence activity
275  *
276  * @ring: pointer to struct amdgpu_ring
277  *
278  * Checks the current fence value and calculates the last
279  * signalled fence value. Wakes the fence queue if the
280  * sequence number has increased.
281  *
282  * Returns true if fence was processed
283  */
284 bool amdgpu_fence_process(struct amdgpu_ring *ring)
285 {
286 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
287 	struct amdgpu_device *adev = ring->adev;
288 	uint32_t seq, last_seq;
289 
290 	do {
291 		last_seq = atomic_read(&ring->fence_drv.last_seq);
292 		seq = amdgpu_fence_read(ring);
293 
294 	} while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq);
295 
296 	if (del_timer(&ring->fence_drv.fallback_timer) &&
297 	    seq != ring->fence_drv.sync_seq)
298 		amdgpu_fence_schedule_fallback(ring);
299 
300 	if (unlikely(seq == last_seq))
301 		return false;
302 
303 	last_seq &= drv->num_fences_mask;
304 	seq &= drv->num_fences_mask;
305 
306 	do {
307 		struct dma_fence *fence, **ptr;
308 
309 		++last_seq;
310 		last_seq &= drv->num_fences_mask;
311 		ptr = &drv->fences[last_seq];
312 
313 		/* There is always exactly one thread signaling this fence slot */
314 		fence = rcu_dereference_protected(*ptr, 1);
315 		RCU_INIT_POINTER(*ptr, NULL);
316 
317 		if (!fence)
318 			continue;
319 
320 		dma_fence_signal(fence);
321 		dma_fence_put(fence);
322 		pm_runtime_mark_last_busy(adev_to_drm(adev)->dev);
323 		pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
324 	} while (last_seq != seq);
325 
326 	return true;
327 }
328 
329 /**
330  * amdgpu_fence_fallback - fallback for hardware interrupts
331  *
332  * @t: timer context used to obtain the pointer to ring structure
333  *
334  * Checks for fence activity.
335  */
336 static void amdgpu_fence_fallback(void *arg)
337 {
338 	struct amdgpu_ring *ring = arg;
339 
340 	if (amdgpu_fence_process(ring))
341 		DRM_WARN("Fence fallback timer expired on ring %s\n", ring->name);
342 }
343 
344 /**
345  * amdgpu_fence_wait_empty - wait for all fences to signal
346  *
347  * @ring: ring index the fence is associated with
348  *
349  * Wait for all fences on the requested ring to signal (all asics).
350  * Returns 0 if the fences have passed, error for all other cases.
351  */
352 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
353 {
354 	uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq);
355 	struct dma_fence *fence, **ptr;
356 	int r;
357 
358 	if (!seq)
359 		return 0;
360 
361 	ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
362 	rcu_read_lock();
363 	fence = rcu_dereference(*ptr);
364 	if (!fence || !dma_fence_get_rcu(fence)) {
365 		rcu_read_unlock();
366 		return 0;
367 	}
368 	rcu_read_unlock();
369 
370 	r = dma_fence_wait(fence, false);
371 	dma_fence_put(fence);
372 	return r;
373 }
374 
375 /**
376  * amdgpu_fence_wait_polling - busy wait for givn sequence number
377  *
378  * @ring: ring index the fence is associated with
379  * @wait_seq: sequence number to wait
380  * @timeout: the timeout for waiting in usecs
381  *
382  * Wait for all fences on the requested ring to signal (all asics).
383  * Returns left time if no timeout, 0 or minus if timeout.
384  */
385 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
386 				      uint32_t wait_seq,
387 				      signed long timeout)
388 {
389 	uint32_t seq;
390 
391 	do {
392 		seq = amdgpu_fence_read(ring);
393 		udelay(5);
394 		timeout -= 5;
395 	} while ((int32_t)(wait_seq - seq) > 0 && timeout > 0);
396 
397 	return timeout > 0 ? timeout : 0;
398 }
399 /**
400  * amdgpu_fence_count_emitted - get the count of emitted fences
401  *
402  * @ring: ring the fence is associated with
403  *
404  * Get the number of fences emitted on the requested ring (all asics).
405  * Returns the number of emitted fences on the ring.  Used by the
406  * dynpm code to ring track activity.
407  */
408 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
409 {
410 	uint64_t emitted;
411 
412 	/* We are not protected by ring lock when reading the last sequence
413 	 * but it's ok to report slightly wrong fence count here.
414 	 */
415 	emitted = 0x100000000ull;
416 	emitted -= atomic_read(&ring->fence_drv.last_seq);
417 	emitted += READ_ONCE(ring->fence_drv.sync_seq);
418 	return lower_32_bits(emitted);
419 }
420 
421 /**
422  * amdgpu_fence_driver_start_ring - make the fence driver
423  * ready for use on the requested ring.
424  *
425  * @ring: ring to start the fence driver on
426  * @irq_src: interrupt source to use for this ring
427  * @irq_type: interrupt type to use for this ring
428  *
429  * Make the fence driver ready for processing (all asics).
430  * Not all asics have all rings, so each asic will only
431  * start the fence driver on the rings it has.
432  * Returns 0 for success, errors for failure.
433  */
434 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
435 				   struct amdgpu_irq_src *irq_src,
436 				   unsigned irq_type)
437 {
438 	struct amdgpu_device *adev = ring->adev;
439 	uint64_t index;
440 
441 	if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) {
442 		ring->fence_drv.cpu_addr = ring->fence_cpu_addr;
443 		ring->fence_drv.gpu_addr = ring->fence_gpu_addr;
444 	} else {
445 		/* put fence directly behind firmware */
446 		index = roundup2(adev->uvd.fw->size, 8);
447 		ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index;
448 		ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index;
449 	}
450 	amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq));
451 
452 	ring->fence_drv.irq_src = irq_src;
453 	ring->fence_drv.irq_type = irq_type;
454 	ring->fence_drv.initialized = true;
455 
456 	DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr 0x%016llx\n",
457 		      ring->name, ring->fence_drv.gpu_addr);
458 	return 0;
459 }
460 
461 /**
462  * amdgpu_fence_driver_init_ring - init the fence driver
463  * for the requested ring.
464  *
465  * @ring: ring to init the fence driver on
466  *
467  * Init the fence driver for the requested ring (all asics).
468  * Helper function for amdgpu_fence_driver_init().
469  */
470 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
471 {
472 	struct amdgpu_device *adev = ring->adev;
473 
474 	if (!adev)
475 		return -EINVAL;
476 
477 	if (!is_power_of_2(ring->num_hw_submission))
478 		return -EINVAL;
479 
480 	ring->fence_drv.cpu_addr = NULL;
481 	ring->fence_drv.gpu_addr = 0;
482 	ring->fence_drv.sync_seq = 0;
483 	atomic_set(&ring->fence_drv.last_seq, 0);
484 	ring->fence_drv.initialized = false;
485 
486 #ifdef __linux__
487 	timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
488 #else
489 	timeout_set(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
490 	    ring);
491 #endif
492 
493 	ring->fence_drv.num_fences_mask = ring->num_hw_submission * 2 - 1;
494 	mtx_init(&ring->fence_drv.lock, IPL_TTY);
495 	ring->fence_drv.fences = kcalloc(ring->num_hw_submission * 2, sizeof(void *),
496 					 GFP_KERNEL);
497 
498 	if (!ring->fence_drv.fences)
499 		return -ENOMEM;
500 
501 	return 0;
502 }
503 
504 /**
505  * amdgpu_fence_driver_sw_init - init the fence driver
506  * for all possible rings.
507  *
508  * @adev: amdgpu device pointer
509  *
510  * Init the fence driver for all possible rings (all asics).
511  * Not all asics have all rings, so each asic will only
512  * start the fence driver on the rings it has using
513  * amdgpu_fence_driver_start_ring().
514  * Returns 0 for success.
515  */
516 int amdgpu_fence_driver_sw_init(struct amdgpu_device *adev)
517 {
518 	return 0;
519 }
520 
521 /**
522  * amdgpu_fence_need_ring_interrupt_restore - helper function to check whether
523  * fence driver interrupts need to be restored.
524  *
525  * @ring: ring that to be checked
526  *
527  * Interrupts for rings that belong to GFX IP don't need to be restored
528  * when the target power state is s0ix.
529  *
530  * Return true if need to restore interrupts, false otherwise.
531  */
532 static bool amdgpu_fence_need_ring_interrupt_restore(struct amdgpu_ring *ring)
533 {
534 	struct amdgpu_device *adev = ring->adev;
535 	bool is_gfx_power_domain = false;
536 
537 	switch (ring->funcs->type) {
538 	case AMDGPU_RING_TYPE_SDMA:
539 	/* SDMA 5.x+ is part of GFX power domain so it's covered by GFXOFF */
540 		if (adev->ip_versions[SDMA0_HWIP][0] >= IP_VERSION(5, 0, 0))
541 			is_gfx_power_domain = true;
542 		break;
543 	case AMDGPU_RING_TYPE_GFX:
544 	case AMDGPU_RING_TYPE_COMPUTE:
545 	case AMDGPU_RING_TYPE_KIQ:
546 	case AMDGPU_RING_TYPE_MES:
547 		is_gfx_power_domain = true;
548 		break;
549 	default:
550 		break;
551 	}
552 
553 	return !(adev->in_s0ix && is_gfx_power_domain);
554 }
555 
556 /**
557  * amdgpu_fence_driver_hw_fini - tear down the fence driver
558  * for all possible rings.
559  *
560  * @adev: amdgpu device pointer
561  *
562  * Tear down the fence driver for all possible rings (all asics).
563  */
564 void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev)
565 {
566 	int i, r;
567 
568 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
569 		struct amdgpu_ring *ring = adev->rings[i];
570 
571 		if (!ring || !ring->fence_drv.initialized)
572 			continue;
573 
574 		/* You can't wait for HW to signal if it's gone */
575 		if (!drm_dev_is_unplugged(adev_to_drm(adev)))
576 			r = amdgpu_fence_wait_empty(ring);
577 		else
578 			r = -ENODEV;
579 		/* no need to trigger GPU reset as we are unloading */
580 		if (r)
581 			amdgpu_fence_driver_force_completion(ring);
582 
583 		if (!drm_dev_is_unplugged(adev_to_drm(adev)) &&
584 		    ring->fence_drv.irq_src &&
585 		    amdgpu_fence_need_ring_interrupt_restore(ring))
586 			amdgpu_irq_put(adev, ring->fence_drv.irq_src,
587 				       ring->fence_drv.irq_type);
588 
589 		del_timer_sync(&ring->fence_drv.fallback_timer);
590 	}
591 }
592 
593 /* Will either stop and flush handlers for amdgpu interrupt or reanble it */
594 void amdgpu_fence_driver_isr_toggle(struct amdgpu_device *adev, bool stop)
595 {
596 	STUB();
597 #ifdef notyet
598 	int i;
599 
600 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
601 		struct amdgpu_ring *ring = adev->rings[i];
602 
603 		if (!ring || !ring->fence_drv.initialized || !ring->fence_drv.irq_src)
604 			continue;
605 
606 		if (stop)
607 			disable_irq(adev->irq.irq);
608 		else
609 			enable_irq(adev->irq.irq);
610 	}
611 #endif
612 }
613 
614 void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev)
615 {
616 	unsigned int i, j;
617 
618 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
619 		struct amdgpu_ring *ring = adev->rings[i];
620 
621 		if (!ring || !ring->fence_drv.initialized)
622 			continue;
623 
624 		/*
625 		 * Notice we check for sched.ops since there's some
626 		 * override on the meaning of sched.ready by amdgpu.
627 		 * The natural check would be sched.ready, which is
628 		 * set as drm_sched_init() finishes...
629 		 */
630 		if (ring->sched.ops)
631 			drm_sched_fini(&ring->sched);
632 
633 		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
634 			dma_fence_put(ring->fence_drv.fences[j]);
635 		kfree(ring->fence_drv.fences);
636 		ring->fence_drv.fences = NULL;
637 		ring->fence_drv.initialized = false;
638 	}
639 }
640 
641 /**
642  * amdgpu_fence_driver_hw_init - enable the fence driver
643  * for all possible rings.
644  *
645  * @adev: amdgpu device pointer
646  *
647  * Enable the fence driver for all possible rings (all asics).
648  * Not all asics have all rings, so each asic will only
649  * start the fence driver on the rings it has using
650  * amdgpu_fence_driver_start_ring().
651  * Returns 0 for success.
652  */
653 void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev)
654 {
655 	int i;
656 
657 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
658 		struct amdgpu_ring *ring = adev->rings[i];
659 		if (!ring || !ring->fence_drv.initialized)
660 			continue;
661 
662 		/* enable the interrupt */
663 		if (ring->fence_drv.irq_src &&
664 		    amdgpu_fence_need_ring_interrupt_restore(ring))
665 			amdgpu_irq_get(adev, ring->fence_drv.irq_src,
666 				       ring->fence_drv.irq_type);
667 	}
668 }
669 
670 /**
671  * amdgpu_fence_driver_clear_job_fences - clear job embedded fences of ring
672  *
673  * @ring: fence of the ring to be cleared
674  *
675  */
676 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring)
677 {
678 	int i;
679 	struct dma_fence *old, **ptr;
680 
681 	for (i = 0; i <= ring->fence_drv.num_fences_mask; i++) {
682 		ptr = &ring->fence_drv.fences[i];
683 		old = rcu_dereference_protected(*ptr, 1);
684 		if (old && old->ops == &amdgpu_job_fence_ops) {
685 			struct amdgpu_job *job;
686 
687 			/* For non-scheduler bad job, i.e. failed ib test, we need to signal
688 			 * it right here or we won't be able to track them in fence_drv
689 			 * and they will remain unsignaled during sa_bo free.
690 			 */
691 			job = container_of(old, struct amdgpu_job, hw_fence);
692 			if (!job->base.s_fence && !dma_fence_is_signaled(old))
693 				dma_fence_signal(old);
694 			RCU_INIT_POINTER(*ptr, NULL);
695 			dma_fence_put(old);
696 		}
697 	}
698 }
699 
700 /**
701  * amdgpu_fence_driver_force_completion - force signal latest fence of ring
702  *
703  * @ring: fence of the ring to signal
704  *
705  */
706 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
707 {
708 	amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
709 	amdgpu_fence_process(ring);
710 }
711 
712 /*
713  * Common fence implementation
714  */
715 
716 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
717 {
718 	return "amdgpu";
719 }
720 
721 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
722 {
723 	return (const char *)to_amdgpu_fence(f)->ring->name;
724 }
725 
726 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
727 {
728 	struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
729 
730 	return (const char *)to_amdgpu_ring(job->base.sched)->name;
731 }
732 
733 /**
734  * amdgpu_fence_enable_signaling - enable signalling on fence
735  * @f: fence
736  *
737  * This function is called with fence_queue lock held, and adds a callback
738  * to fence_queue that checks if this fence is signaled, and if so it
739  * signals the fence and removes itself.
740  */
741 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
742 {
743 	if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
744 		amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
745 
746 	return true;
747 }
748 
749 /**
750  * amdgpu_job_fence_enable_signaling - enable signalling on job fence
751  * @f: fence
752  *
753  * This is the simliar function with amdgpu_fence_enable_signaling above, it
754  * only handles the job embedded fence.
755  */
756 static bool amdgpu_job_fence_enable_signaling(struct dma_fence *f)
757 {
758 	struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
759 
760 	if (!timer_pending(&to_amdgpu_ring(job->base.sched)->fence_drv.fallback_timer))
761 		amdgpu_fence_schedule_fallback(to_amdgpu_ring(job->base.sched));
762 
763 	return true;
764 }
765 
766 /**
767  * amdgpu_fence_free - free up the fence memory
768  *
769  * @rcu: RCU callback head
770  *
771  * Free up the fence memory after the RCU grace period.
772  */
773 static void amdgpu_fence_free(struct rcu_head *rcu)
774 {
775 	struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
776 
777 	/* free fence_slab if it's separated fence*/
778 #ifdef __linux__
779 	kmem_cache_free(amdgpu_fence_slab, to_amdgpu_fence(f));
780 #else
781 	pool_put(&amdgpu_fence_slab, to_amdgpu_fence(f));
782 #endif
783 }
784 
785 /**
786  * amdgpu_job_fence_free - free up the job with embedded fence
787  *
788  * @rcu: RCU callback head
789  *
790  * Free up the job with embedded fence after the RCU grace period.
791  */
792 static void amdgpu_job_fence_free(struct rcu_head *rcu)
793 {
794 	struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
795 
796 	/* free job if fence has a parent job */
797 	kfree(container_of(f, struct amdgpu_job, hw_fence));
798 }
799 
800 /**
801  * amdgpu_fence_release - callback that fence can be freed
802  *
803  * @f: fence
804  *
805  * This function is called when the reference count becomes zero.
806  * It just RCU schedules freeing up the fence.
807  */
808 static void amdgpu_fence_release(struct dma_fence *f)
809 {
810 	call_rcu(&f->rcu, amdgpu_fence_free);
811 }
812 
813 /**
814  * amdgpu_job_fence_release - callback that job embedded fence can be freed
815  *
816  * @f: fence
817  *
818  * This is the simliar function with amdgpu_fence_release above, it
819  * only handles the job embedded fence.
820  */
821 static void amdgpu_job_fence_release(struct dma_fence *f)
822 {
823 	call_rcu(&f->rcu, amdgpu_job_fence_free);
824 }
825 
826 static const struct dma_fence_ops amdgpu_fence_ops = {
827 	.get_driver_name = amdgpu_fence_get_driver_name,
828 	.get_timeline_name = amdgpu_fence_get_timeline_name,
829 	.enable_signaling = amdgpu_fence_enable_signaling,
830 	.release = amdgpu_fence_release,
831 };
832 
833 static const struct dma_fence_ops amdgpu_job_fence_ops = {
834 	.get_driver_name = amdgpu_fence_get_driver_name,
835 	.get_timeline_name = amdgpu_job_fence_get_timeline_name,
836 	.enable_signaling = amdgpu_job_fence_enable_signaling,
837 	.release = amdgpu_job_fence_release,
838 };
839 
840 /*
841  * Fence debugfs
842  */
843 #if defined(CONFIG_DEBUG_FS)
844 static int amdgpu_debugfs_fence_info_show(struct seq_file *m, void *unused)
845 {
846 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
847 	int i;
848 
849 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
850 		struct amdgpu_ring *ring = adev->rings[i];
851 		if (!ring || !ring->fence_drv.initialized)
852 			continue;
853 
854 		amdgpu_fence_process(ring);
855 
856 		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
857 		seq_printf(m, "Last signaled fence          0x%08x\n",
858 			   atomic_read(&ring->fence_drv.last_seq));
859 		seq_printf(m, "Last emitted                 0x%08x\n",
860 			   ring->fence_drv.sync_seq);
861 
862 		if (ring->funcs->type == AMDGPU_RING_TYPE_GFX ||
863 		    ring->funcs->type == AMDGPU_RING_TYPE_SDMA) {
864 			seq_printf(m, "Last signaled trailing fence 0x%08x\n",
865 				   le32_to_cpu(*ring->trail_fence_cpu_addr));
866 			seq_printf(m, "Last emitted                 0x%08x\n",
867 				   ring->trail_seq);
868 		}
869 
870 		if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
871 			continue;
872 
873 		/* set in CP_VMID_PREEMPT and preemption occurred */
874 		seq_printf(m, "Last preempted               0x%08x\n",
875 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
876 		/* set in CP_VMID_RESET and reset occurred */
877 		seq_printf(m, "Last reset                   0x%08x\n",
878 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
879 		/* Both preemption and reset occurred */
880 		seq_printf(m, "Last both                    0x%08x\n",
881 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
882 	}
883 	return 0;
884 }
885 
886 /*
887  * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
888  *
889  * Manually trigger a gpu reset at the next fence wait.
890  */
891 static int gpu_recover_get(void *data, u64 *val)
892 {
893 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
894 	struct drm_device *dev = adev_to_drm(adev);
895 	int r;
896 
897 	r = pm_runtime_get_sync(dev->dev);
898 	if (r < 0) {
899 		pm_runtime_put_autosuspend(dev->dev);
900 		return 0;
901 	}
902 
903 	if (amdgpu_reset_domain_schedule(adev->reset_domain, &adev->reset_work))
904 		flush_work(&adev->reset_work);
905 
906 	*val = atomic_read(&adev->reset_domain->reset_res);
907 
908 	pm_runtime_mark_last_busy(dev->dev);
909 	pm_runtime_put_autosuspend(dev->dev);
910 
911 	return 0;
912 }
913 
914 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_fence_info);
915 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_gpu_recover_fops, gpu_recover_get, NULL,
916 			 "%lld\n");
917 
918 static void amdgpu_debugfs_reset_work(struct work_struct *work)
919 {
920 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
921 						  reset_work);
922 
923 	struct amdgpu_reset_context reset_context;
924 	memset(&reset_context, 0, sizeof(reset_context));
925 
926 	reset_context.method = AMD_RESET_METHOD_NONE;
927 	reset_context.reset_req_dev = adev;
928 	set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
929 
930 	amdgpu_device_gpu_recover(adev, NULL, &reset_context);
931 }
932 
933 #endif
934 
935 void amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
936 {
937 #if defined(CONFIG_DEBUG_FS)
938 	struct drm_minor *minor = adev_to_drm(adev)->primary;
939 	struct dentry *root = minor->debugfs_root;
940 
941 	debugfs_create_file("amdgpu_fence_info", 0444, root, adev,
942 			    &amdgpu_debugfs_fence_info_fops);
943 
944 	if (!amdgpu_sriov_vf(adev)) {
945 
946 		INIT_WORK(&adev->reset_work, amdgpu_debugfs_reset_work);
947 		debugfs_create_file("amdgpu_gpu_recover", 0444, root, adev,
948 				    &amdgpu_debugfs_gpu_recover_fops);
949 	}
950 #endif
951 }
952 
953