Home | History | Annotate | Line # | Download | only in amdgpu
amdgpu_fence.c revision 1.1.1.2
      1 /*	$NetBSD: amdgpu_fence.c,v 1.1.1.2 2021/12/18 20:11:06 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2009 Jerome Glisse.
      5  * All Rights Reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the
      9  * "Software"), to deal in the Software without restriction, including
     10  * without limitation the rights to use, copy, modify, merge, publish,
     11  * distribute, sub license, and/or sell copies of the Software, and to
     12  * permit persons to whom the Software is furnished to do so, subject to
     13  * the following conditions:
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     18  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  *
     23  * The above copyright notice and this permission notice (including the
     24  * next paragraph) shall be included in all copies or substantial portions
     25  * of the Software.
     26  *
     27  */
     28 /*
     29  * Authors:
     30  *    Jerome Glisse <glisse (at) freedesktop.org>
     31  *    Dave Airlie
     32  */
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: amdgpu_fence.c,v 1.1.1.2 2021/12/18 20:11:06 riastradh Exp $");
     35 
     36 #include <linux/seq_file.h>
     37 #include <linux/atomic.h>
     38 #include <linux/wait.h>
     39 #include <linux/kref.h>
     40 #include <linux/slab.h>
     41 #include <linux/firmware.h>
     42 #include <linux/pm_runtime.h>
     43 
     44 #include <drm/drm_debugfs.h>
     45 
     46 #include "amdgpu.h"
     47 #include "amdgpu_trace.h"
     48 
     49 /*
     50  * Fences
     51  * Fences mark an event in the GPUs pipeline and are used
     52  * for GPU/CPU synchronization.  When the fence is written,
     53  * it is expected that all buffers associated with that fence
     54  * are no longer in use by the associated ring on the GPU and
     55  * that the the relevant GPU caches have been flushed.
     56  */
     57 
     58 struct amdgpu_fence {
     59 	struct dma_fence base;
     60 
     61 	/* RB, DMA, etc. */
     62 	struct amdgpu_ring		*ring;
     63 };
     64 
     65 static struct kmem_cache *amdgpu_fence_slab;
     66 
     67 int amdgpu_fence_slab_init(void)
     68 {
     69 	amdgpu_fence_slab = kmem_cache_create(
     70 		"amdgpu_fence", sizeof(struct amdgpu_fence), 0,
     71 		SLAB_HWCACHE_ALIGN, NULL);
     72 	if (!amdgpu_fence_slab)
     73 		return -ENOMEM;
     74 	return 0;
     75 }
     76 
     77 void amdgpu_fence_slab_fini(void)
     78 {
     79 	rcu_barrier();
     80 	kmem_cache_destroy(amdgpu_fence_slab);
     81 }
     82 /*
     83  * Cast helper
     84  */
     85 static const struct dma_fence_ops amdgpu_fence_ops;
     86 static inline struct amdgpu_fence *to_amdgpu_fence(struct dma_fence *f)
     87 {
     88 	struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
     89 
     90 	if (__f->base.ops == &amdgpu_fence_ops)
     91 		return __f;
     92 
     93 	return NULL;
     94 }
     95 
     96 /**
     97  * amdgpu_fence_write - write a fence value
     98  *
     99  * @ring: ring the fence is associated with
    100  * @seq: sequence number to write
    101  *
    102  * Writes a fence value to memory (all asics).
    103  */
    104 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
    105 {
    106 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
    107 
    108 	if (drv->cpu_addr)
    109 		*drv->cpu_addr = cpu_to_le32(seq);
    110 }
    111 
    112 /**
    113  * amdgpu_fence_read - read a fence value
    114  *
    115  * @ring: ring the fence is associated with
    116  *
    117  * Reads a fence value from memory (all asics).
    118  * Returns the value of the fence read from memory.
    119  */
    120 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
    121 {
    122 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
    123 	u32 seq = 0;
    124 
    125 	if (drv->cpu_addr)
    126 		seq = le32_to_cpu(*drv->cpu_addr);
    127 	else
    128 		seq = atomic_read(&drv->last_seq);
    129 
    130 	return seq;
    131 }
    132 
    133 /**
    134  * amdgpu_fence_emit - emit a fence on the requested ring
    135  *
    136  * @ring: ring the fence is associated with
    137  * @f: resulting fence object
    138  *
    139  * Emits a fence command on the requested ring (all asics).
    140  * Returns 0 on success, -ENOMEM on failure.
    141  */
    142 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **f,
    143 		      unsigned flags)
    144 {
    145 	struct amdgpu_device *adev = ring->adev;
    146 	struct amdgpu_fence *fence;
    147 	struct dma_fence __rcu **ptr;
    148 	uint32_t seq;
    149 	int r;
    150 
    151 	fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
    152 	if (fence == NULL)
    153 		return -ENOMEM;
    154 
    155 	seq = ++ring->fence_drv.sync_seq;
    156 	fence->ring = ring;
    157 	dma_fence_init(&fence->base, &amdgpu_fence_ops,
    158 		       &ring->fence_drv.lock,
    159 		       adev->fence_context + ring->idx,
    160 		       seq);
    161 	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
    162 			       seq, flags | AMDGPU_FENCE_FLAG_INT);
    163 	pm_runtime_get_noresume(adev->ddev->dev);
    164 	ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
    165 	if (unlikely(rcu_dereference_protected(*ptr, 1))) {
    166 		struct dma_fence *old;
    167 
    168 		rcu_read_lock();
    169 		old = dma_fence_get_rcu_safe(ptr);
    170 		rcu_read_unlock();
    171 
    172 		if (old) {
    173 			r = dma_fence_wait(old, false);
    174 			dma_fence_put(old);
    175 			if (r)
    176 				return r;
    177 		}
    178 	}
    179 
    180 	/* This function can't be called concurrently anyway, otherwise
    181 	 * emitting the fence would mess up the hardware ring buffer.
    182 	 */
    183 	rcu_assign_pointer(*ptr, dma_fence_get(&fence->base));
    184 
    185 	*f = &fence->base;
    186 
    187 	return 0;
    188 }
    189 
    190 /**
    191  * amdgpu_fence_emit_polling - emit a fence on the requeste ring
    192  *
    193  * @ring: ring the fence is associated with
    194  * @s: resulting sequence number
    195  *
    196  * Emits a fence command on the requested ring (all asics).
    197  * Used For polling fence.
    198  * Returns 0 on success, -ENOMEM on failure.
    199  */
    200 int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s)
    201 {
    202 	uint32_t seq;
    203 
    204 	if (!s)
    205 		return -EINVAL;
    206 
    207 	seq = ++ring->fence_drv.sync_seq;
    208 	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
    209 			       seq, 0);
    210 
    211 	*s = seq;
    212 
    213 	return 0;
    214 }
    215 
    216 /**
    217  * amdgpu_fence_schedule_fallback - schedule fallback check
    218  *
    219  * @ring: pointer to struct amdgpu_ring
    220  *
    221  * Start a timer as fallback to our interrupts.
    222  */
    223 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
    224 {
    225 	mod_timer(&ring->fence_drv.fallback_timer,
    226 		  jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
    227 }
    228 
    229 /**
    230  * amdgpu_fence_process - check for fence activity
    231  *
    232  * @ring: pointer to struct amdgpu_ring
    233  *
    234  * Checks the current fence value and calculates the last
    235  * signalled fence value. Wakes the fence queue if the
    236  * sequence number has increased.
    237  *
    238  * Returns true if fence was processed
    239  */
    240 bool amdgpu_fence_process(struct amdgpu_ring *ring)
    241 {
    242 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
    243 	struct amdgpu_device *adev = ring->adev;
    244 	uint32_t seq, last_seq;
    245 	int r;
    246 
    247 	do {
    248 		last_seq = atomic_read(&ring->fence_drv.last_seq);
    249 		seq = amdgpu_fence_read(ring);
    250 
    251 	} while (atomic_cmpxchg(&drv->last_seq, last_seq, seq) != last_seq);
    252 
    253 	if (del_timer(&ring->fence_drv.fallback_timer) &&
    254 	    seq != ring->fence_drv.sync_seq)
    255 		amdgpu_fence_schedule_fallback(ring);
    256 
    257 	if (unlikely(seq == last_seq))
    258 		return false;
    259 
    260 	last_seq &= drv->num_fences_mask;
    261 	seq &= drv->num_fences_mask;
    262 
    263 	do {
    264 		struct dma_fence *fence, **ptr;
    265 
    266 		++last_seq;
    267 		last_seq &= drv->num_fences_mask;
    268 		ptr = &drv->fences[last_seq];
    269 
    270 		/* There is always exactly one thread signaling this fence slot */
    271 		fence = rcu_dereference_protected(*ptr, 1);
    272 		RCU_INIT_POINTER(*ptr, NULL);
    273 
    274 		if (!fence)
    275 			continue;
    276 
    277 		r = dma_fence_signal(fence);
    278 		if (!r)
    279 			DMA_FENCE_TRACE(fence, "signaled from irq context\n");
    280 		else
    281 			BUG();
    282 
    283 		dma_fence_put(fence);
    284 		pm_runtime_mark_last_busy(adev->ddev->dev);
    285 		pm_runtime_put_autosuspend(adev->ddev->dev);
    286 	} while (last_seq != seq);
    287 
    288 	return true;
    289 }
    290 
    291 /**
    292  * amdgpu_fence_fallback - fallback for hardware interrupts
    293  *
    294  * @work: delayed work item
    295  *
    296  * Checks for fence activity.
    297  */
    298 static void amdgpu_fence_fallback(struct timer_list *t)
    299 {
    300 	struct amdgpu_ring *ring = from_timer(ring, t,
    301 					      fence_drv.fallback_timer);
    302 
    303 	if (amdgpu_fence_process(ring))
    304 		DRM_WARN("Fence fallback timer expired on ring %s\n", ring->name);
    305 }
    306 
    307 /**
    308  * amdgpu_fence_wait_empty - wait for all fences to signal
    309  *
    310  * @adev: amdgpu device pointer
    311  * @ring: ring index the fence is associated with
    312  *
    313  * Wait for all fences on the requested ring to signal (all asics).
    314  * Returns 0 if the fences have passed, error for all other cases.
    315  */
    316 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
    317 {
    318 	uint64_t seq = READ_ONCE(ring->fence_drv.sync_seq);
    319 	struct dma_fence *fence, **ptr;
    320 	int r;
    321 
    322 	if (!seq)
    323 		return 0;
    324 
    325 	ptr = &ring->fence_drv.fences[seq & ring->fence_drv.num_fences_mask];
    326 	rcu_read_lock();
    327 	fence = rcu_dereference(*ptr);
    328 	if (!fence || !dma_fence_get_rcu(fence)) {
    329 		rcu_read_unlock();
    330 		return 0;
    331 	}
    332 	rcu_read_unlock();
    333 
    334 	r = dma_fence_wait(fence, false);
    335 	dma_fence_put(fence);
    336 	return r;
    337 }
    338 
    339 /**
    340  * amdgpu_fence_wait_polling - busy wait for givn sequence number
    341  *
    342  * @ring: ring index the fence is associated with
    343  * @wait_seq: sequence number to wait
    344  * @timeout: the timeout for waiting in usecs
    345  *
    346  * Wait for all fences on the requested ring to signal (all asics).
    347  * Returns left time if no timeout, 0 or minus if timeout.
    348  */
    349 signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
    350 				      uint32_t wait_seq,
    351 				      signed long timeout)
    352 {
    353 	uint32_t seq;
    354 
    355 	do {
    356 		seq = amdgpu_fence_read(ring);
    357 		udelay(5);
    358 		timeout -= 5;
    359 	} while ((int32_t)(wait_seq - seq) > 0 && timeout > 0);
    360 
    361 	return timeout > 0 ? timeout : 0;
    362 }
    363 /**
    364  * amdgpu_fence_count_emitted - get the count of emitted fences
    365  *
    366  * @ring: ring the fence is associated with
    367  *
    368  * Get the number of fences emitted on the requested ring (all asics).
    369  * Returns the number of emitted fences on the ring.  Used by the
    370  * dynpm code to ring track activity.
    371  */
    372 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
    373 {
    374 	uint64_t emitted;
    375 
    376 	/* We are not protected by ring lock when reading the last sequence
    377 	 * but it's ok to report slightly wrong fence count here.
    378 	 */
    379 	amdgpu_fence_process(ring);
    380 	emitted = 0x100000000ull;
    381 	emitted -= atomic_read(&ring->fence_drv.last_seq);
    382 	emitted += READ_ONCE(ring->fence_drv.sync_seq);
    383 	return lower_32_bits(emitted);
    384 }
    385 
    386 /**
    387  * amdgpu_fence_driver_start_ring - make the fence driver
    388  * ready for use on the requested ring.
    389  *
    390  * @ring: ring to start the fence driver on
    391  * @irq_src: interrupt source to use for this ring
    392  * @irq_type: interrupt type to use for this ring
    393  *
    394  * Make the fence driver ready for processing (all asics).
    395  * Not all asics have all rings, so each asic will only
    396  * start the fence driver on the rings it has.
    397  * Returns 0 for success, errors for failure.
    398  */
    399 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
    400 				   struct amdgpu_irq_src *irq_src,
    401 				   unsigned irq_type)
    402 {
    403 	struct amdgpu_device *adev = ring->adev;
    404 	uint64_t index;
    405 
    406 	if (ring->funcs->type != AMDGPU_RING_TYPE_UVD) {
    407 		ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
    408 		ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
    409 	} else {
    410 		/* put fence directly behind firmware */
    411 		index = ALIGN(adev->uvd.fw->size, 8);
    412 		ring->fence_drv.cpu_addr = adev->uvd.inst[ring->me].cpu_addr + index;
    413 		ring->fence_drv.gpu_addr = adev->uvd.inst[ring->me].gpu_addr + index;
    414 	}
    415 	amdgpu_fence_write(ring, atomic_read(&ring->fence_drv.last_seq));
    416 	amdgpu_irq_get(adev, irq_src, irq_type);
    417 
    418 	ring->fence_drv.irq_src = irq_src;
    419 	ring->fence_drv.irq_type = irq_type;
    420 	ring->fence_drv.initialized = true;
    421 
    422 	DRM_DEV_DEBUG(adev->dev, "fence driver on ring %s use gpu addr "
    423 		      "0x%016llx, cpu addr 0x%p\n", ring->name,
    424 		      ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
    425 	return 0;
    426 }
    427 
    428 /**
    429  * amdgpu_fence_driver_init_ring - init the fence driver
    430  * for the requested ring.
    431  *
    432  * @ring: ring to init the fence driver on
    433  * @num_hw_submission: number of entries on the hardware queue
    434  *
    435  * Init the fence driver for the requested ring (all asics).
    436  * Helper function for amdgpu_fence_driver_init().
    437  */
    438 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
    439 				  unsigned num_hw_submission)
    440 {
    441 	struct amdgpu_device *adev = ring->adev;
    442 	long timeout;
    443 	int r;
    444 
    445 	if (!adev)
    446 		return -EINVAL;
    447 
    448 	/* Check that num_hw_submission is a power of two */
    449 	if ((num_hw_submission & (num_hw_submission - 1)) != 0)
    450 		return -EINVAL;
    451 
    452 	ring->fence_drv.cpu_addr = NULL;
    453 	ring->fence_drv.gpu_addr = 0;
    454 	ring->fence_drv.sync_seq = 0;
    455 	atomic_set(&ring->fence_drv.last_seq, 0);
    456 	ring->fence_drv.initialized = false;
    457 
    458 	timer_setup(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback, 0);
    459 
    460 	ring->fence_drv.num_fences_mask = num_hw_submission * 2 - 1;
    461 	spin_lock_init(&ring->fence_drv.lock);
    462 	ring->fence_drv.fences = kcalloc(num_hw_submission * 2, sizeof(void *),
    463 					 GFP_KERNEL);
    464 	if (!ring->fence_drv.fences)
    465 		return -ENOMEM;
    466 
    467 	/* No need to setup the GPU scheduler for KIQ ring */
    468 	if (ring->funcs->type != AMDGPU_RING_TYPE_KIQ) {
    469 		switch (ring->funcs->type) {
    470 		case AMDGPU_RING_TYPE_GFX:
    471 			timeout = adev->gfx_timeout;
    472 			break;
    473 		case AMDGPU_RING_TYPE_COMPUTE:
    474 			timeout = adev->compute_timeout;
    475 			break;
    476 		case AMDGPU_RING_TYPE_SDMA:
    477 			timeout = adev->sdma_timeout;
    478 			break;
    479 		default:
    480 			timeout = adev->video_timeout;
    481 			break;
    482 		}
    483 
    484 		r = drm_sched_init(&ring->sched, &amdgpu_sched_ops,
    485 				   num_hw_submission, amdgpu_job_hang_limit,
    486 				   timeout, ring->name);
    487 		if (r) {
    488 			DRM_ERROR("Failed to create scheduler on ring %s.\n",
    489 				  ring->name);
    490 			return r;
    491 		}
    492 	}
    493 
    494 	return 0;
    495 }
    496 
    497 /**
    498  * amdgpu_fence_driver_init - init the fence driver
    499  * for all possible rings.
    500  *
    501  * @adev: amdgpu device pointer
    502  *
    503  * Init the fence driver for all possible rings (all asics).
    504  * Not all asics have all rings, so each asic will only
    505  * start the fence driver on the rings it has using
    506  * amdgpu_fence_driver_start_ring().
    507  * Returns 0 for success.
    508  */
    509 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
    510 {
    511 	if (amdgpu_debugfs_fence_init(adev))
    512 		dev_err(adev->dev, "fence debugfs file creation failed\n");
    513 
    514 	return 0;
    515 }
    516 
    517 /**
    518  * amdgpu_fence_driver_fini - tear down the fence driver
    519  * for all possible rings.
    520  *
    521  * @adev: amdgpu device pointer
    522  *
    523  * Tear down the fence driver for all possible rings (all asics).
    524  */
    525 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
    526 {
    527 	unsigned i, j;
    528 	int r;
    529 
    530 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
    531 		struct amdgpu_ring *ring = adev->rings[i];
    532 
    533 		if (!ring || !ring->fence_drv.initialized)
    534 			continue;
    535 		r = amdgpu_fence_wait_empty(ring);
    536 		if (r) {
    537 			/* no need to trigger GPU reset as we are unloading */
    538 			amdgpu_fence_driver_force_completion(ring);
    539 		}
    540 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
    541 			       ring->fence_drv.irq_type);
    542 		drm_sched_fini(&ring->sched);
    543 		del_timer_sync(&ring->fence_drv.fallback_timer);
    544 		for (j = 0; j <= ring->fence_drv.num_fences_mask; ++j)
    545 			dma_fence_put(ring->fence_drv.fences[j]);
    546 		kfree(ring->fence_drv.fences);
    547 		ring->fence_drv.fences = NULL;
    548 		ring->fence_drv.initialized = false;
    549 	}
    550 }
    551 
    552 /**
    553  * amdgpu_fence_driver_suspend - suspend the fence driver
    554  * for all possible rings.
    555  *
    556  * @adev: amdgpu device pointer
    557  *
    558  * Suspend the fence driver for all possible rings (all asics).
    559  */
    560 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
    561 {
    562 	int i, r;
    563 
    564 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
    565 		struct amdgpu_ring *ring = adev->rings[i];
    566 		if (!ring || !ring->fence_drv.initialized)
    567 			continue;
    568 
    569 		/* wait for gpu to finish processing current batch */
    570 		r = amdgpu_fence_wait_empty(ring);
    571 		if (r) {
    572 			/* delay GPU reset to resume */
    573 			amdgpu_fence_driver_force_completion(ring);
    574 		}
    575 
    576 		/* disable the interrupt */
    577 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
    578 			       ring->fence_drv.irq_type);
    579 	}
    580 }
    581 
    582 /**
    583  * amdgpu_fence_driver_resume - resume the fence driver
    584  * for all possible rings.
    585  *
    586  * @adev: amdgpu device pointer
    587  *
    588  * Resume the fence driver for all possible rings (all asics).
    589  * Not all asics have all rings, so each asic will only
    590  * start the fence driver on the rings it has using
    591  * amdgpu_fence_driver_start_ring().
    592  * Returns 0 for success.
    593  */
    594 void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
    595 {
    596 	int i;
    597 
    598 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
    599 		struct amdgpu_ring *ring = adev->rings[i];
    600 		if (!ring || !ring->fence_drv.initialized)
    601 			continue;
    602 
    603 		/* enable the interrupt */
    604 		amdgpu_irq_get(adev, ring->fence_drv.irq_src,
    605 			       ring->fence_drv.irq_type);
    606 	}
    607 }
    608 
    609 /**
    610  * amdgpu_fence_driver_force_completion - force signal latest fence of ring
    611  *
    612  * @ring: fence of the ring to signal
    613  *
    614  */
    615 void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring)
    616 {
    617 	amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
    618 	amdgpu_fence_process(ring);
    619 }
    620 
    621 /*
    622  * Common fence implementation
    623  */
    624 
    625 static const char *amdgpu_fence_get_driver_name(struct dma_fence *fence)
    626 {
    627 	return "amdgpu";
    628 }
    629 
    630 static const char *amdgpu_fence_get_timeline_name(struct dma_fence *f)
    631 {
    632 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
    633 	return (const char *)fence->ring->name;
    634 }
    635 
    636 /**
    637  * amdgpu_fence_enable_signaling - enable signalling on fence
    638  * @fence: fence
    639  *
    640  * This function is called with fence_queue lock held, and adds a callback
    641  * to fence_queue that checks if this fence is signaled, and if so it
    642  * signals the fence and removes itself.
    643  */
    644 static bool amdgpu_fence_enable_signaling(struct dma_fence *f)
    645 {
    646 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
    647 	struct amdgpu_ring *ring = fence->ring;
    648 
    649 	if (!timer_pending(&ring->fence_drv.fallback_timer))
    650 		amdgpu_fence_schedule_fallback(ring);
    651 
    652 	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
    653 
    654 	return true;
    655 }
    656 
    657 /**
    658  * amdgpu_fence_free - free up the fence memory
    659  *
    660  * @rcu: RCU callback head
    661  *
    662  * Free up the fence memory after the RCU grace period.
    663  */
    664 static void amdgpu_fence_free(struct rcu_head *rcu)
    665 {
    666 	struct dma_fence *f = container_of(rcu, struct dma_fence, rcu);
    667 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
    668 	kmem_cache_free(amdgpu_fence_slab, fence);
    669 }
    670 
    671 /**
    672  * amdgpu_fence_release - callback that fence can be freed
    673  *
    674  * @fence: fence
    675  *
    676  * This function is called when the reference count becomes zero.
    677  * It just RCU schedules freeing up the fence.
    678  */
    679 static void amdgpu_fence_release(struct dma_fence *f)
    680 {
    681 	call_rcu(&f->rcu, amdgpu_fence_free);
    682 }
    683 
    684 static const struct dma_fence_ops amdgpu_fence_ops = {
    685 	.get_driver_name = amdgpu_fence_get_driver_name,
    686 	.get_timeline_name = amdgpu_fence_get_timeline_name,
    687 	.enable_signaling = amdgpu_fence_enable_signaling,
    688 	.release = amdgpu_fence_release,
    689 };
    690 
    691 /*
    692  * Fence debugfs
    693  */
    694 #if defined(CONFIG_DEBUG_FS)
    695 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
    696 {
    697 	struct drm_info_node *node = (struct drm_info_node *)m->private;
    698 	struct drm_device *dev = node->minor->dev;
    699 	struct amdgpu_device *adev = dev->dev_private;
    700 	int i;
    701 
    702 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
    703 		struct amdgpu_ring *ring = adev->rings[i];
    704 		if (!ring || !ring->fence_drv.initialized)
    705 			continue;
    706 
    707 		amdgpu_fence_process(ring);
    708 
    709 		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
    710 		seq_printf(m, "Last signaled fence          0x%08x\n",
    711 			   atomic_read(&ring->fence_drv.last_seq));
    712 		seq_printf(m, "Last emitted                 0x%08x\n",
    713 			   ring->fence_drv.sync_seq);
    714 
    715 		if (ring->funcs->type == AMDGPU_RING_TYPE_GFX ||
    716 		    ring->funcs->type == AMDGPU_RING_TYPE_SDMA) {
    717 			seq_printf(m, "Last signaled trailing fence 0x%08x\n",
    718 				   le32_to_cpu(*ring->trail_fence_cpu_addr));
    719 			seq_printf(m, "Last emitted                 0x%08x\n",
    720 				   ring->trail_seq);
    721 		}
    722 
    723 		if (ring->funcs->type != AMDGPU_RING_TYPE_GFX)
    724 			continue;
    725 
    726 		/* set in CP_VMID_PREEMPT and preemption occurred */
    727 		seq_printf(m, "Last preempted               0x%08x\n",
    728 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 2)));
    729 		/* set in CP_VMID_RESET and reset occurred */
    730 		seq_printf(m, "Last reset                   0x%08x\n",
    731 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 4)));
    732 		/* Both preemption and reset occurred */
    733 		seq_printf(m, "Last both                    0x%08x\n",
    734 			   le32_to_cpu(*(ring->fence_drv.cpu_addr + 6)));
    735 	}
    736 	return 0;
    737 }
    738 
    739 /**
    740  * amdgpu_debugfs_gpu_recover - manually trigger a gpu reset & recover
    741  *
    742  * Manually trigger a gpu reset at the next fence wait.
    743  */
    744 static int amdgpu_debugfs_gpu_recover(struct seq_file *m, void *data)
    745 {
    746 	struct drm_info_node *node = (struct drm_info_node *) m->private;
    747 	struct drm_device *dev = node->minor->dev;
    748 	struct amdgpu_device *adev = dev->dev_private;
    749 	int r;
    750 
    751 	r = pm_runtime_get_sync(dev->dev);
    752 	if (r < 0)
    753 		return 0;
    754 
    755 	seq_printf(m, "gpu recover\n");
    756 	amdgpu_device_gpu_recover(adev, NULL);
    757 
    758 	pm_runtime_mark_last_busy(dev->dev);
    759 	pm_runtime_put_autosuspend(dev->dev);
    760 
    761 	return 0;
    762 }
    763 
    764 static const struct drm_info_list amdgpu_debugfs_fence_list[] = {
    765 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
    766 	{"amdgpu_gpu_recover", &amdgpu_debugfs_gpu_recover, 0, NULL}
    767 };
    768 
    769 static const struct drm_info_list amdgpu_debugfs_fence_list_sriov[] = {
    770 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
    771 };
    772 #endif
    773 
    774 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
    775 {
    776 #if defined(CONFIG_DEBUG_FS)
    777 	if (amdgpu_sriov_vf(adev))
    778 		return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list_sriov, 1);
    779 	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
    780 #else
    781 	return 0;
    782 #endif
    783 }
    784 
    785