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