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