xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_fence.c (revision 521ba2f2ab0e0e89d1776559874b3ecc227442fc)
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_driver_hw_fini - tear down the fence driver
523  * for all possible rings.
524  *
525  * @adev: amdgpu device pointer
526  *
527  * Tear down the fence driver for all possible rings (all asics).
528  */
529 void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev)
530 {
531 	int i, r;
532 
533 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
534 		struct amdgpu_ring *ring = adev->rings[i];
535 
536 		if (!ring || !ring->fence_drv.initialized)
537 			continue;
538 
539 		/* You can't wait for HW to signal if it's gone */
540 		if (!drm_dev_is_unplugged(adev_to_drm(adev)))
541 			r = amdgpu_fence_wait_empty(ring);
542 		else
543 			r = -ENODEV;
544 		/* no need to trigger GPU reset as we are unloading */
545 		if (r)
546 			amdgpu_fence_driver_force_completion(ring);
547 
548 		if (!drm_dev_is_unplugged(adev_to_drm(adev)) &&
549 		    ring->fence_drv.irq_src)
550 			amdgpu_irq_put(adev, ring->fence_drv.irq_src,
551 				       ring->fence_drv.irq_type);
552 
553 		del_timer_sync(&ring->fence_drv.fallback_timer);
554 	}
555 }
556 
557 /* Will either stop and flush handlers for amdgpu interrupt or reanble it */
558 void amdgpu_fence_driver_isr_toggle(struct amdgpu_device *adev, bool stop)
559 {
560 	STUB();
561 #ifdef notyet
562 	int i;
563 
564 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
565 		struct amdgpu_ring *ring = adev->rings[i];
566 
567 		if (!ring || !ring->fence_drv.initialized || !ring->fence_drv.irq_src)
568 			continue;
569 
570 		if (stop)
571 			disable_irq(adev->irq.irq);
572 		else
573 			enable_irq(adev->irq.irq);
574 	}
575 #endif
576 }
577 
578 void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev)
579 {
580 	unsigned int i, j;
581 
582 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
583 		struct amdgpu_ring *ring = adev->rings[i];
584 
585 		if (!ring || !ring->fence_drv.initialized)
586 			continue;
587 
588 		/*
589 		 * Notice we check for sched.ops since there's some
590 		 * override on the meaning of sched.ready by amdgpu.
591 		 * The natural check would be sched.ready, which is
592 		 * set as drm_sched_init() finishes...
593 		 */
594 		if (ring->sched.ops)
595 			drm_sched_fini(&ring->sched);
596 
597 		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
598 			dma_fence_put(ring->fence_drv.fences[j]);
599 		kfree(ring->fence_drv.fences);
600 		ring->fence_drv.fences = NULL;
601 		ring->fence_drv.initialized = false;
602 	}
603 }
604 
605 /**
606  * amdgpu_fence_driver_hw_init - enable the fence driver
607  * for all possible rings.
608  *
609  * @adev: amdgpu device pointer
610  *
611  * Enable the fence driver for all possible rings (all asics).
612  * Not all asics have all rings, so each asic will only
613  * start the fence driver on the rings it has using
614  * amdgpu_fence_driver_start_ring().
615  * Returns 0 for success.
616  */
617 void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev)
618 {
619 	int i;
620 
621 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
622 		struct amdgpu_ring *ring = adev->rings[i];
623 		if (!ring || !ring->fence_drv.initialized)
624 			continue;
625 
626 		/* enable the interrupt */
627 		if (ring->fence_drv.irq_src)
628 			amdgpu_irq_get(adev, ring->fence_drv.irq_src,
629 				       ring->fence_drv.irq_type);
630 	}
631 }
632 
633 /**
634  * amdgpu_fence_driver_clear_job_fences - clear job embedded fences of ring
635  *
636  * @ring: fence of the ring to be cleared
637  *
638  */
639 void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring)
640 {
641 	int i;
642 	struct dma_fence *old, **ptr;
643 
644 	for (i = 0; i <= ring->fence_drv.num_fences_mask; i++) {
645 		ptr = &ring->fence_drv.fences[i];
646 		old = rcu_dereference_protected(*ptr, 1);
647 		if (old && old->ops == &amdgpu_job_fence_ops) {
648 			struct amdgpu_job *job;
649 
650 			/* For non-scheduler bad job, i.e. failed ib test, we need to signal
651 			 * it right here or we won't be able to track them in fence_drv
652 			 * and they will remain unsignaled during sa_bo free.
653 			 */
654 			job = container_of(old, struct amdgpu_job, hw_fence);
655 			if (!job->base.s_fence && !dma_fence_is_signaled(old))
656 				dma_fence_signal(old);
657 			RCU_INIT_POINTER(*ptr, NULL);
658 			dma_fence_put(old);
659 		}
660 	}
661 }
662 
663 /**
664  * amdgpu_fence_driver_force_completion - force signal latest fence of ring
665  *
666  * @ring: fence of the ring to signal
667  *
668  */
669 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
670 {
671 	amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
672 	amdgpu_fence_process(ring);
673 }
674 
675 /*
676  * Common fence implementation
677  */
678 
679 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
680 {
681 	return "amdgpu";
682 }
683 
684 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
685 {
686 	return (const char *)to_amdgpu_fence(f)->ring->name;
687 }
688 
689 static const char *amdgpu_job_fence_get_timeline_name(struct dma_fence *f)
690 {
691 	struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
692 
693 	return (const char *)to_amdgpu_ring(job->base.sched)->name;
694 }
695 
696 /**
697  * amdgpu_fence_enable_signaling - enable signalling on fence
698  * @f: fence
699  *
700  * This function is called with fence_queue lock held, and adds a callback
701  * to fence_queue that checks if this fence is signaled, and if so it
702  * signals the fence and removes itself.
703  */
704 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
705 {
706 	if (!timer_pending(&to_amdgpu_fence(f)->ring->fence_drv.fallback_timer))
707 		amdgpu_fence_schedule_fallback(to_amdgpu_fence(f)->ring);
708 
709 	return true;
710 }
711 
712 /**
713  * amdgpu_job_fence_enable_signaling - enable signalling on job fence
714  * @f: fence
715  *
716  * This is the simliar function with amdgpu_fence_enable_signaling above, it
717  * only handles the job embedded fence.
718  */
719 static bool amdgpu_job_fence_enable_signaling(struct dma_fence *f)
720 {
721 	struct amdgpu_job *job = container_of(f, struct amdgpu_job, hw_fence);
722 
723 	if (!timer_pending(&to_amdgpu_ring(job->base.sched)->fence_drv.fallback_timer))
724 		amdgpu_fence_schedule_fallback(to_amdgpu_ring(job->base.sched));
725 
726 	return true;
727 }
728 
729 /**
730  * amdgpu_fence_free - free up the fence memory
731  *
732  * @rcu: RCU callback head
733  *
734  * Free up the fence memory after the RCU grace period.
735  */
736 static void amdgpu_fence_free(struct rcu_head *rcu)
737 {
738 	struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
739 
740 	/* free fence_slab if it's separated fence*/
741 #ifdef __linux__
742 	kmem_cache_free(amdgpu_fence_slab, to_amdgpu_fence(f));
743 #else
744 	pool_put(&amdgpu_fence_slab, to_amdgpu_fence(f));
745 #endif
746 }
747 
748 /**
749  * amdgpu_job_fence_free - free up the job with embedded fence
750  *
751  * @rcu: RCU callback head
752  *
753  * Free up the job with embedded fence after the RCU grace period.
754  */
755 static void amdgpu_job_fence_free(struct rcu_head *rcu)
756 {
757 	struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
758 
759 	/* free job if fence has a parent job */
760 	kfree(container_of(f, struct amdgpu_job, hw_fence));
761 }
762 
763 /**
764  * amdgpu_fence_release - callback that fence can be freed
765  *
766  * @f: fence
767  *
768  * This function is called when the reference count becomes zero.
769  * It just RCU schedules freeing up the fence.
770  */
771 static void amdgpu_fence_release(struct dma_fence *f)
772 {
773 	call_rcu(&f->rcu, amdgpu_fence_free);
774 }
775 
776 /**
777  * amdgpu_job_fence_release - callback that job embedded fence can be freed
778  *
779  * @f: fence
780  *
781  * This is the simliar function with amdgpu_fence_release above, it
782  * only handles the job embedded fence.
783  */
784 static void amdgpu_job_fence_release(struct dma_fence *f)
785 {
786 	call_rcu(&f->rcu, amdgpu_job_fence_free);
787 }
788 
789 static const struct dma_fence_ops amdgpu_fence_ops = {
790 	.get_driver_name = amdgpu_fence_get_driver_name,
791 	.get_timeline_name = amdgpu_fence_get_timeline_name,
792 	.enable_signaling = amdgpu_fence_enable_signaling,
793 	.release = amdgpu_fence_release,
794 };
795 
796 static const struct dma_fence_ops amdgpu_job_fence_ops = {
797 	.get_driver_name = amdgpu_fence_get_driver_name,
798 	.get_timeline_name = amdgpu_job_fence_get_timeline_name,
799 	.enable_signaling = amdgpu_job_fence_enable_signaling,
800 	.release = amdgpu_job_fence_release,
801 };
802 
803 /*
804  * Fence debugfs
805  */
806 #if defined(CONFIG_DEBUG_FS)
807 static int amdgpu_debugfs_fence_info_show(struct seq_file *m, void *unused)
808 {
809 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
810 	int i;
811 
812 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
813 		struct amdgpu_ring *ring = adev->rings[i];
814 		if (!ring || !ring->fence_drv.initialized)
815 			continue;
816 
817 		amdgpu_fence_process(ring);
818 
819 		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
820 		seq_printf(m, "Last signaled fence          0x%08x\n",
821 			   atomic_read(&ring->fence_drv.last_seq));
822 		seq_printf(m, "Last emitted                 0x%08x\n",
823 			   ring->fence_drv.sync_seq);
824 
825 		if (ring->funcs->type == AMDGPU_RING_TYPE_GFX ||
826 		    ring->funcs->type == AMDGPU_RING_TYPE_SDMA) {
827 			seq_printf(m, "Last signaled trailing fence 0x%08x\n",
828 				   le32_to_cpu(*ring->trail_fence_cpu_addr));
829 			seq_printf(m, "Last emitted                 0x%08x\n",
830 				   ring->trail_seq);
831 		}
832 
833 		if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
834 			continue;
835 
836 		/* set in CP_VMID_PREEMPT and preemption occurred */
837 		seq_printf(m, "Last preempted               0x%08x\n",
838 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
839 		/* set in CP_VMID_RESET and reset occurred */
840 		seq_printf(m, "Last reset                   0x%08x\n",
841 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
842 		/* Both preemption and reset occurred */
843 		seq_printf(m, "Last both                    0x%08x\n",
844 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
845 	}
846 	return 0;
847 }
848 
849 /*
850  * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
851  *
852  * Manually trigger a gpu reset at the next fence wait.
853  */
854 static int gpu_recover_get(void *data, u64 *val)
855 {
856 	struct amdgpu_device *adev = (struct amdgpu_device *)data;
857 	struct drm_device *dev = adev_to_drm(adev);
858 	int r;
859 
860 	r = pm_runtime_get_sync(dev->dev);
861 	if (r < 0) {
862 		pm_runtime_put_autosuspend(dev->dev);
863 		return 0;
864 	}
865 
866 	if (amdgpu_reset_domain_schedule(adev->reset_domain, &adev->reset_work))
867 		flush_work(&adev->reset_work);
868 
869 	*val = atomic_read(&adev->reset_domain->reset_res);
870 
871 	pm_runtime_mark_last_busy(dev->dev);
872 	pm_runtime_put_autosuspend(dev->dev);
873 
874 	return 0;
875 }
876 
877 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_fence_info);
878 DEFINE_DEBUGFS_ATTRIBUTE(amdgpu_debugfs_gpu_recover_fops, gpu_recover_get, NULL,
879 			 "%lld\n");
880 
881 static void amdgpu_debugfs_reset_work(struct work_struct *work)
882 {
883 	struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
884 						  reset_work);
885 
886 	struct amdgpu_reset_context reset_context;
887 	memset(&reset_context, 0, sizeof(reset_context));
888 
889 	reset_context.method = AMD_RESET_METHOD_NONE;
890 	reset_context.reset_req_dev = adev;
891 	set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
892 
893 	amdgpu_device_gpu_recover(adev, NULL, &reset_context);
894 }
895 
896 #endif
897 
898 void amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
899 {
900 #if defined(CONFIG_DEBUG_FS)
901 	struct drm_minor *minor = adev_to_drm(adev)->primary;
902 	struct dentry *root = minor->debugfs_root;
903 
904 	debugfs_create_file("amdgpu_fence_info", 0444, root, adev,
905 			    &amdgpu_debugfs_fence_info_fops);
906 
907 	if (!amdgpu_sriov_vf(adev)) {
908 
909 		INIT_WORK(&adev->reset_work, amdgpu_debugfs_reset_work);
910 		debugfs_create_file("amdgpu_gpu_recover", 0444, root, adev,
911 				    &amdgpu_debugfs_gpu_recover_fops);
912 	}
913 #endif
914 }
915 
916