Home | History | Annotate | Line # | Download | only in amdgpu
amdgpu_fence.c revision 1.1
      1 /*	$NetBSD: amdgpu_fence.c,v 1.1 2018/08/27 01:34:44 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 2018/08/27 01:34:44 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 <drm/drmP.h>
     43 #include "amdgpu.h"
     44 #include "amdgpu_trace.h"
     45 
     46 /*
     47  * Fences
     48  * Fences mark an event in the GPUs pipeline and are used
     49  * for GPU/CPU synchronization.  When the fence is written,
     50  * it is expected that all buffers associated with that fence
     51  * are no longer in use by the associated ring on the GPU and
     52  * that the the relevant GPU caches have been flushed.
     53  */
     54 
     55 static struct kmem_cache *amdgpu_fence_slab;
     56 static atomic_t amdgpu_fence_slab_ref = ATOMIC_INIT(0);
     57 
     58 /**
     59  * amdgpu_fence_write - write a fence value
     60  *
     61  * @ring: ring the fence is associated with
     62  * @seq: sequence number to write
     63  *
     64  * Writes a fence value to memory (all asics).
     65  */
     66 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
     67 {
     68 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
     69 
     70 	if (drv->cpu_addr)
     71 		*drv->cpu_addr = cpu_to_le32(seq);
     72 }
     73 
     74 /**
     75  * amdgpu_fence_read - read a fence value
     76  *
     77  * @ring: ring the fence is associated with
     78  *
     79  * Reads a fence value from memory (all asics).
     80  * Returns the value of the fence read from memory.
     81  */
     82 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
     83 {
     84 	struct amdgpu_fence_driver *drv = &ring->fence_drv;
     85 	u32 seq = 0;
     86 
     87 	if (drv->cpu_addr)
     88 		seq = le32_to_cpu(*drv->cpu_addr);
     89 	else
     90 		seq = lower_32_bits(atomic64_read(&drv->last_seq));
     91 
     92 	return seq;
     93 }
     94 
     95 /**
     96  * amdgpu_fence_emit - emit a fence on the requested ring
     97  *
     98  * @ring: ring the fence is associated with
     99  * @owner: creator of the fence
    100  * @fence: amdgpu fence object
    101  *
    102  * Emits a fence command on the requested ring (all asics).
    103  * Returns 0 on success, -ENOMEM on failure.
    104  */
    105 int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner,
    106 		      struct amdgpu_fence **fence)
    107 {
    108 	struct amdgpu_device *adev = ring->adev;
    109 
    110 	/* we are protected by the ring emission mutex */
    111 	*fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
    112 	if ((*fence) == NULL) {
    113 		return -ENOMEM;
    114 	}
    115 	(*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx];
    116 	(*fence)->ring = ring;
    117 	(*fence)->owner = owner;
    118 	fence_init(&(*fence)->base, &amdgpu_fence_ops,
    119 		&ring->fence_drv.fence_queue.lock,
    120 		adev->fence_context + ring->idx,
    121 		(*fence)->seq);
    122 	amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
    123 			       (*fence)->seq,
    124 			       AMDGPU_FENCE_FLAG_INT);
    125 	return 0;
    126 }
    127 
    128 /**
    129  * amdgpu_fence_schedule_fallback - schedule fallback check
    130  *
    131  * @ring: pointer to struct amdgpu_ring
    132  *
    133  * Start a timer as fallback to our interrupts.
    134  */
    135 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
    136 {
    137 	mod_timer(&ring->fence_drv.fallback_timer,
    138 		  jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
    139 }
    140 
    141 /**
    142  * amdgpu_fence_activity - check for fence activity
    143  *
    144  * @ring: pointer to struct amdgpu_ring
    145  *
    146  * Checks the current fence value and calculates the last
    147  * signalled fence value. Returns true if activity occured
    148  * on the ring, and the fence_queue should be waken up.
    149  */
    150 static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
    151 {
    152 	uint64_t seq, last_seq, last_emitted;
    153 	unsigned count_loop = 0;
    154 	bool wake = false;
    155 
    156 	/* Note there is a scenario here for an infinite loop but it's
    157 	 * very unlikely to happen. For it to happen, the current polling
    158 	 * process need to be interrupted by another process and another
    159 	 * process needs to update the last_seq btw the atomic read and
    160 	 * xchg of the current process.
    161 	 *
    162 	 * More over for this to go in infinite loop there need to be
    163 	 * continuously new fence signaled ie amdgpu_fence_read needs
    164 	 * to return a different value each time for both the currently
    165 	 * polling process and the other process that xchg the last_seq
    166 	 * btw atomic read and xchg of the current process. And the
    167 	 * value the other process set as last seq must be higher than
    168 	 * the seq value we just read. Which means that current process
    169 	 * need to be interrupted after amdgpu_fence_read and before
    170 	 * atomic xchg.
    171 	 *
    172 	 * To be even more safe we count the number of time we loop and
    173 	 * we bail after 10 loop just accepting the fact that we might
    174 	 * have temporarly set the last_seq not to the true real last
    175 	 * seq but to an older one.
    176 	 */
    177 	last_seq = atomic64_read(&ring->fence_drv.last_seq);
    178 	do {
    179 		last_emitted = ring->fence_drv.sync_seq[ring->idx];
    180 		seq = amdgpu_fence_read(ring);
    181 		seq |= last_seq & 0xffffffff00000000LL;
    182 		if (seq < last_seq) {
    183 			seq &= 0xffffffff;
    184 			seq |= last_emitted & 0xffffffff00000000LL;
    185 		}
    186 
    187 		if (seq <= last_seq || seq > last_emitted) {
    188 			break;
    189 		}
    190 		/* If we loop over we don't want to return without
    191 		 * checking if a fence is signaled as it means that the
    192 		 * seq we just read is different from the previous on.
    193 		 */
    194 		wake = true;
    195 		last_seq = seq;
    196 		if ((count_loop++) > 10) {
    197 			/* We looped over too many time leave with the
    198 			 * fact that we might have set an older fence
    199 			 * seq then the current real last seq as signaled
    200 			 * by the hw.
    201 			 */
    202 			break;
    203 		}
    204 	} while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
    205 
    206 	if (seq < last_emitted)
    207 		amdgpu_fence_schedule_fallback(ring);
    208 
    209 	return wake;
    210 }
    211 
    212 /**
    213  * amdgpu_fence_process - process a fence
    214  *
    215  * @adev: amdgpu_device pointer
    216  * @ring: ring index the fence is associated with
    217  *
    218  * Checks the current fence value and wakes the fence queue
    219  * if the sequence number has increased (all asics).
    220  */
    221 void amdgpu_fence_process(struct amdgpu_ring *ring)
    222 {
    223 	if (amdgpu_fence_activity(ring))
    224 		wake_up_all(&ring->fence_drv.fence_queue);
    225 }
    226 
    227 /**
    228  * amdgpu_fence_fallback - fallback for hardware interrupts
    229  *
    230  * @work: delayed work item
    231  *
    232  * Checks for fence activity.
    233  */
    234 static void amdgpu_fence_fallback(unsigned long arg)
    235 {
    236 	struct amdgpu_ring *ring = (void *)arg;
    237 
    238 	amdgpu_fence_process(ring);
    239 }
    240 
    241 /**
    242  * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
    243  *
    244  * @ring: ring the fence is associated with
    245  * @seq: sequence number
    246  *
    247  * Check if the last signaled fence sequnce number is >= the requested
    248  * sequence number (all asics).
    249  * Returns true if the fence has signaled (current fence value
    250  * is >= requested value) or false if it has not (current fence
    251  * value is < the requested value.  Helper function for
    252  * amdgpu_fence_signaled().
    253  */
    254 static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
    255 {
    256 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
    257 		return true;
    258 
    259 	/* poll new last sequence at least once */
    260 	amdgpu_fence_process(ring);
    261 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
    262 		return true;
    263 
    264 	return false;
    265 }
    266 
    267 /*
    268  * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal
    269  * @ring: ring to wait on for the seq number
    270  * @seq: seq number wait for
    271  *
    272  * return value:
    273  * 0: seq signaled, and gpu not hang
    274  * -EDEADL: GPU hang detected
    275  * -EINVAL: some paramter is not valid
    276  */
    277 static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
    278 {
    279 	bool signaled = false;
    280 
    281 	BUG_ON(!ring);
    282 	if (seq > ring->fence_drv.sync_seq[ring->idx])
    283 		return -EINVAL;
    284 
    285 	if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
    286 		return 0;
    287 
    288 	amdgpu_fence_schedule_fallback(ring);
    289 	wait_event(ring->fence_drv.fence_queue, (
    290 		   (signaled = amdgpu_fence_seq_signaled(ring, seq))));
    291 
    292 	if (signaled)
    293 		return 0;
    294 	else
    295 		return -EDEADLK;
    296 }
    297 
    298 /**
    299  * amdgpu_fence_wait_next - wait for the next fence to signal
    300  *
    301  * @adev: amdgpu device pointer
    302  * @ring: ring index the fence is associated with
    303  *
    304  * Wait for the next fence on the requested ring to signal (all asics).
    305  * Returns 0 if the next fence has passed, error for all other cases.
    306  * Caller must hold ring lock.
    307  */
    308 int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
    309 {
    310 	uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
    311 
    312 	if (seq >= ring->fence_drv.sync_seq[ring->idx])
    313 		return -ENOENT;
    314 
    315 	return amdgpu_fence_ring_wait_seq(ring, seq);
    316 }
    317 
    318 /**
    319  * amdgpu_fence_wait_empty - wait for all fences to signal
    320  *
    321  * @adev: amdgpu device pointer
    322  * @ring: ring index the fence is associated with
    323  *
    324  * Wait for all fences on the requested ring to signal (all asics).
    325  * Returns 0 if the fences have passed, error for all other cases.
    326  * Caller must hold ring lock.
    327  */
    328 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
    329 {
    330 	uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
    331 
    332 	if (!seq)
    333 		return 0;
    334 
    335 	return amdgpu_fence_ring_wait_seq(ring, seq);
    336 }
    337 
    338 /**
    339  * amdgpu_fence_count_emitted - get the count of emitted fences
    340  *
    341  * @ring: ring the fence is associated with
    342  *
    343  * Get the number of fences emitted on the requested ring (all asics).
    344  * Returns the number of emitted fences on the ring.  Used by the
    345  * dynpm code to ring track activity.
    346  */
    347 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
    348 {
    349 	uint64_t emitted;
    350 
    351 	/* We are not protected by ring lock when reading the last sequence
    352 	 * but it's ok to report slightly wrong fence count here.
    353 	 */
    354 	amdgpu_fence_process(ring);
    355 	emitted = ring->fence_drv.sync_seq[ring->idx]
    356 		- atomic64_read(&ring->fence_drv.last_seq);
    357 	/* to avoid 32bits warp around */
    358 	if (emitted > 0x10000000)
    359 		emitted = 0x10000000;
    360 
    361 	return (unsigned)emitted;
    362 }
    363 
    364 /**
    365  * amdgpu_fence_need_sync - do we need a semaphore
    366  *
    367  * @fence: amdgpu fence object
    368  * @dst_ring: which ring to check against
    369  *
    370  * Check if the fence needs to be synced against another ring
    371  * (all asics).  If so, we need to emit a semaphore.
    372  * Returns true if we need to sync with another ring, false if
    373  * not.
    374  */
    375 bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
    376 			    struct amdgpu_ring *dst_ring)
    377 {
    378 	struct amdgpu_fence_driver *fdrv;
    379 
    380 	if (!fence)
    381 		return false;
    382 
    383 	if (fence->ring == dst_ring)
    384 		return false;
    385 
    386 	/* we are protected by the ring mutex */
    387 	fdrv = &dst_ring->fence_drv;
    388 	if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
    389 		return false;
    390 
    391 	return true;
    392 }
    393 
    394 /**
    395  * amdgpu_fence_note_sync - record the sync point
    396  *
    397  * @fence: amdgpu fence object
    398  * @dst_ring: which ring to check against
    399  *
    400  * Note the sequence number at which point the fence will
    401  * be synced with the requested ring (all asics).
    402  */
    403 void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
    404 			    struct amdgpu_ring *dst_ring)
    405 {
    406 	struct amdgpu_fence_driver *dst, *src;
    407 	unsigned i;
    408 
    409 	if (!fence)
    410 		return;
    411 
    412 	if (fence->ring == dst_ring)
    413 		return;
    414 
    415 	/* we are protected by the ring mutex */
    416 	src = &fence->ring->fence_drv;
    417 	dst = &dst_ring->fence_drv;
    418 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
    419 		if (i == dst_ring->idx)
    420 			continue;
    421 
    422 		dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
    423 	}
    424 }
    425 
    426 /**
    427  * amdgpu_fence_driver_start_ring - make the fence driver
    428  * ready for use on the requested ring.
    429  *
    430  * @ring: ring to start the fence driver on
    431  * @irq_src: interrupt source to use for this ring
    432  * @irq_type: interrupt type to use for this ring
    433  *
    434  * Make the fence driver ready for processing (all asics).
    435  * Not all asics have all rings, so each asic will only
    436  * start the fence driver on the rings it has.
    437  * Returns 0 for success, errors for failure.
    438  */
    439 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
    440 				   struct amdgpu_irq_src *irq_src,
    441 				   unsigned irq_type)
    442 {
    443 	struct amdgpu_device *adev = ring->adev;
    444 	uint64_t index;
    445 
    446 	if (ring != &adev->uvd.ring) {
    447 		ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
    448 		ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
    449 	} else {
    450 		/* put fence directly behind firmware */
    451 		index = ALIGN(adev->uvd.fw->size, 8);
    452 		ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
    453 		ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
    454 	}
    455 	amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
    456 	amdgpu_irq_get(adev, irq_src, irq_type);
    457 
    458 	ring->fence_drv.irq_src = irq_src;
    459 	ring->fence_drv.irq_type = irq_type;
    460 	ring->fence_drv.initialized = true;
    461 
    462 	dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
    463 		 "cpu addr 0x%p\n", ring->idx,
    464 		 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
    465 	return 0;
    466 }
    467 
    468 /**
    469  * amdgpu_fence_driver_init_ring - init the fence driver
    470  * for the requested ring.
    471  *
    472  * @ring: ring to init the fence driver on
    473  *
    474  * Init the fence driver for the requested ring (all asics).
    475  * Helper function for amdgpu_fence_driver_init().
    476  */
    477 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
    478 {
    479 	int i, r;
    480 
    481 	ring->fence_drv.cpu_addr = NULL;
    482 	ring->fence_drv.gpu_addr = 0;
    483 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
    484 		ring->fence_drv.sync_seq[i] = 0;
    485 
    486 	atomic64_set(&ring->fence_drv.last_seq, 0);
    487 	ring->fence_drv.initialized = false;
    488 
    489 	setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
    490 		    (unsigned long)ring);
    491 
    492 	init_waitqueue_head(&ring->fence_drv.fence_queue);
    493 
    494 	if (amdgpu_enable_scheduler) {
    495 		long timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
    496 		if (timeout == 0) {
    497 			/*
    498 			 * FIXME:
    499 			 * Delayed workqueue cannot use it directly,
    500 			 * so the scheduler will not use delayed workqueue if
    501 			 * MAX_SCHEDULE_TIMEOUT is set.
    502 			 * Currently keep it simple and silly.
    503 			 */
    504 			timeout = MAX_SCHEDULE_TIMEOUT;
    505 		}
    506 		r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
    507 				   amdgpu_sched_hw_submission,
    508 				   timeout, ring->name);
    509 		if (r) {
    510 			DRM_ERROR("Failed to create scheduler on ring %s.\n",
    511 				  ring->name);
    512 			return r;
    513 		}
    514 	}
    515 
    516 	return 0;
    517 }
    518 
    519 /**
    520  * amdgpu_fence_driver_init - init the fence driver
    521  * for all possible rings.
    522  *
    523  * @adev: amdgpu device pointer
    524  *
    525  * Init the fence driver for all possible rings (all asics).
    526  * Not all asics have all rings, so each asic will only
    527  * start the fence driver on the rings it has using
    528  * amdgpu_fence_driver_start_ring().
    529  * Returns 0 for success.
    530  */
    531 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
    532 {
    533 	if (atomic_inc_return(&amdgpu_fence_slab_ref) == 1) {
    534 		amdgpu_fence_slab = kmem_cache_create(
    535 			"amdgpu_fence", sizeof(struct amdgpu_fence), 0,
    536 			SLAB_HWCACHE_ALIGN, NULL);
    537 		if (!amdgpu_fence_slab)
    538 			return -ENOMEM;
    539 	}
    540 	if (amdgpu_debugfs_fence_init(adev))
    541 		dev_err(adev->dev, "fence debugfs file creation failed\n");
    542 
    543 	return 0;
    544 }
    545 
    546 /**
    547  * amdgpu_fence_driver_fini - tear down the fence driver
    548  * for all possible rings.
    549  *
    550  * @adev: amdgpu device pointer
    551  *
    552  * Tear down the fence driver for all possible rings (all asics).
    553  */
    554 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
    555 {
    556 	int i, r;
    557 
    558 	if (atomic_dec_and_test(&amdgpu_fence_slab_ref))
    559 		kmem_cache_destroy(amdgpu_fence_slab);
    560 	mutex_lock(&adev->ring_lock);
    561 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
    562 		struct amdgpu_ring *ring = adev->rings[i];
    563 
    564 		if (!ring || !ring->fence_drv.initialized)
    565 			continue;
    566 		r = amdgpu_fence_wait_empty(ring);
    567 		if (r) {
    568 			/* no need to trigger GPU reset as we are unloading */
    569 			amdgpu_fence_driver_force_completion(adev);
    570 		}
    571 		wake_up_all(&ring->fence_drv.fence_queue);
    572 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
    573 			       ring->fence_drv.irq_type);
    574 		amd_sched_fini(&ring->sched);
    575 		del_timer_sync(&ring->fence_drv.fallback_timer);
    576 		ring->fence_drv.initialized = false;
    577 	}
    578 	mutex_unlock(&adev->ring_lock);
    579 }
    580 
    581 /**
    582  * amdgpu_fence_driver_suspend - suspend the fence driver
    583  * for all possible rings.
    584  *
    585  * @adev: amdgpu device pointer
    586  *
    587  * Suspend the fence driver for all possible rings (all asics).
    588  */
    589 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
    590 {
    591 	int i, r;
    592 
    593 	mutex_lock(&adev->ring_lock);
    594 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
    595 		struct amdgpu_ring *ring = adev->rings[i];
    596 		if (!ring || !ring->fence_drv.initialized)
    597 			continue;
    598 
    599 		/* wait for gpu to finish processing current batch */
    600 		r = amdgpu_fence_wait_empty(ring);
    601 		if (r) {
    602 			/* delay GPU reset to resume */
    603 			amdgpu_fence_driver_force_completion(adev);
    604 		}
    605 
    606 		/* disable the interrupt */
    607 		amdgpu_irq_put(adev, ring->fence_drv.irq_src,
    608 			       ring->fence_drv.irq_type);
    609 	}
    610 	mutex_unlock(&adev->ring_lock);
    611 }
    612 
    613 /**
    614  * amdgpu_fence_driver_resume - resume the fence driver
    615  * for all possible rings.
    616  *
    617  * @adev: amdgpu device pointer
    618  *
    619  * Resume the fence driver for all possible rings (all asics).
    620  * Not all asics have all rings, so each asic will only
    621  * start the fence driver on the rings it has using
    622  * amdgpu_fence_driver_start_ring().
    623  * Returns 0 for success.
    624  */
    625 void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
    626 {
    627 	int i;
    628 
    629 	mutex_lock(&adev->ring_lock);
    630 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
    631 		struct amdgpu_ring *ring = adev->rings[i];
    632 		if (!ring || !ring->fence_drv.initialized)
    633 			continue;
    634 
    635 		/* enable the interrupt */
    636 		amdgpu_irq_get(adev, ring->fence_drv.irq_src,
    637 			       ring->fence_drv.irq_type);
    638 	}
    639 	mutex_unlock(&adev->ring_lock);
    640 }
    641 
    642 /**
    643  * amdgpu_fence_driver_force_completion - force all fence waiter to complete
    644  *
    645  * @adev: amdgpu device pointer
    646  *
    647  * In case of GPU reset failure make sure no process keep waiting on fence
    648  * that will never complete.
    649  */
    650 void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
    651 {
    652 	int i;
    653 
    654 	for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
    655 		struct amdgpu_ring *ring = adev->rings[i];
    656 		if (!ring || !ring->fence_drv.initialized)
    657 			continue;
    658 
    659 		amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
    660 	}
    661 }
    662 
    663 /*
    664  * Common fence implementation
    665  */
    666 
    667 static const char *amdgpu_fence_get_driver_name(struct fence *fence)
    668 {
    669 	return "amdgpu";
    670 }
    671 
    672 static const char *amdgpu_fence_get_timeline_name(struct fence *f)
    673 {
    674 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
    675 	return (const char *)fence->ring->name;
    676 }
    677 
    678 /**
    679  * amdgpu_fence_is_signaled - test if fence is signaled
    680  *
    681  * @f: fence to test
    682  *
    683  * Test the fence sequence number if it is already signaled. If it isn't
    684  * signaled start fence processing. Returns True if the fence is signaled.
    685  */
    686 static bool amdgpu_fence_is_signaled(struct fence *f)
    687 {
    688 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
    689 	struct amdgpu_ring *ring = fence->ring;
    690 
    691 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
    692 		return true;
    693 
    694 	amdgpu_fence_process(ring);
    695 
    696 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
    697 		return true;
    698 
    699 	return false;
    700 }
    701 
    702 /**
    703  * amdgpu_fence_check_signaled - callback from fence_queue
    704  *
    705  * this function is called with fence_queue lock held, which is also used
    706  * for the fence locking itself, so unlocked variants are used for
    707  * fence_signal, and remove_wait_queue.
    708  */
    709 static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
    710 {
    711 	struct amdgpu_fence *fence;
    712 	struct amdgpu_device *adev;
    713 	u64 seq;
    714 	int ret;
    715 
    716 	fence = container_of(wait, struct amdgpu_fence, fence_wake);
    717 	adev = fence->ring->adev;
    718 
    719 	/*
    720 	 * We cannot use amdgpu_fence_process here because we're already
    721 	 * in the waitqueue, in a call from wake_up_all.
    722 	 */
    723 	seq = atomic64_read(&fence->ring->fence_drv.last_seq);
    724 	if (seq >= fence->seq) {
    725 		ret = fence_signal_locked(&fence->base);
    726 		if (!ret)
    727 			FENCE_TRACE(&fence->base, "signaled from irq context\n");
    728 		else
    729 			FENCE_TRACE(&fence->base, "was already signaled\n");
    730 
    731 		__remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
    732 		fence_put(&fence->base);
    733 	} else
    734 		FENCE_TRACE(&fence->base, "pending\n");
    735 	return 0;
    736 }
    737 
    738 /**
    739  * amdgpu_fence_enable_signaling - enable signalling on fence
    740  * @fence: fence
    741  *
    742  * This function is called with fence_queue lock held, and adds a callback
    743  * to fence_queue that checks if this fence is signaled, and if so it
    744  * signals the fence and removes itself.
    745  */
    746 static bool amdgpu_fence_enable_signaling(struct fence *f)
    747 {
    748 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
    749 	struct amdgpu_ring *ring = fence->ring;
    750 
    751 	if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
    752 		return false;
    753 
    754 	fence->fence_wake.flags = 0;
    755 	fence->fence_wake.private = NULL;
    756 	fence->fence_wake.func = amdgpu_fence_check_signaled;
    757 	__add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
    758 	fence_get(f);
    759 	if (!timer_pending(&ring->fence_drv.fallback_timer))
    760 		amdgpu_fence_schedule_fallback(ring);
    761 	FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
    762 	return true;
    763 }
    764 
    765 static void amdgpu_fence_release(struct fence *f)
    766 {
    767 	struct amdgpu_fence *fence = to_amdgpu_fence(f);
    768 	kmem_cache_free(amdgpu_fence_slab, fence);
    769 }
    770 
    771 const struct fence_ops amdgpu_fence_ops = {
    772 	.get_driver_name = amdgpu_fence_get_driver_name,
    773 	.get_timeline_name = amdgpu_fence_get_timeline_name,
    774 	.enable_signaling = amdgpu_fence_enable_signaling,
    775 	.signaled = amdgpu_fence_is_signaled,
    776 	.wait = fence_default_wait,
    777 	.release = amdgpu_fence_release,
    778 };
    779 
    780 /*
    781  * Fence debugfs
    782  */
    783 #if defined(CONFIG_DEBUG_FS)
    784 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
    785 {
    786 	struct drm_info_node *node = (struct drm_info_node *)m->private;
    787 	struct drm_device *dev = node->minor->dev;
    788 	struct amdgpu_device *adev = dev->dev_private;
    789 	int i, j;
    790 
    791 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
    792 		struct amdgpu_ring *ring = adev->rings[i];
    793 		if (!ring || !ring->fence_drv.initialized)
    794 			continue;
    795 
    796 		amdgpu_fence_process(ring);
    797 
    798 		seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
    799 		seq_printf(m, "Last signaled fence 0x%016llx\n",
    800 			   (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
    801 		seq_printf(m, "Last emitted        0x%016llx\n",
    802 			   ring->fence_drv.sync_seq[i]);
    803 
    804 		for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
    805 			struct amdgpu_ring *other = adev->rings[j];
    806 			if (i != j && other && other->fence_drv.initialized &&
    807 			    ring->fence_drv.sync_seq[j])
    808 				seq_printf(m, "Last sync to ring %d 0x%016llx\n",
    809 					   j, ring->fence_drv.sync_seq[j]);
    810 		}
    811 	}
    812 	return 0;
    813 }
    814 
    815 static struct drm_info_list amdgpu_debugfs_fence_list[] = {
    816 	{"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
    817 };
    818 #endif
    819 
    820 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
    821 {
    822 #if defined(CONFIG_DEBUG_FS)
    823 	return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
    824 #else
    825 	return 0;
    826 #endif
    827 }
    828 
    829