Home | History | Annotate | Line # | Download | only in radeon
radeon_fence.c revision 1.2
      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 (at) 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 <drm/drmP.h>
     38 #include "radeon_reg.h"
     39 #include "radeon.h"
     40 #include "radeon_trace.h"
     41 
     42 /*
     43  * Fences
     44  * Fences mark an event in the GPUs pipeline and are used
     45  * for GPU/CPU synchronization.  When the fence is written,
     46  * it is expected that all buffers associated with that fence
     47  * are no longer in use by the associated ring on the GPU and
     48  * that the the relevant GPU caches have been flushed.  Whether
     49  * we use a scratch register or memory location depends on the asic
     50  * and whether writeback is enabled.
     51  */
     52 
     53 /**
     54  * radeon_fence_write - write a fence value
     55  *
     56  * @rdev: radeon_device pointer
     57  * @seq: sequence number to write
     58  * @ring: ring index the fence is associated with
     59  *
     60  * Writes a fence value to memory or a scratch register (all asics).
     61  */
     62 static void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
     63 {
     64 	struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
     65 	if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
     66 		if (drv->cpu_addr) {
     67 			*drv->cpu_addr = cpu_to_le32(seq);
     68 		}
     69 	} else {
     70 		WREG32(drv->scratch_reg, seq);
     71 	}
     72 }
     73 
     74 /**
     75  * radeon_fence_read - read a fence value
     76  *
     77  * @rdev: radeon_device pointer
     78  * @ring: ring index the fence is associated with
     79  *
     80  * Reads a fence value from memory or a scratch register (all asics).
     81  * Returns the value of the fence read from memory or register.
     82  */
     83 static u32 radeon_fence_read(struct radeon_device *rdev, int ring)
     84 {
     85 	struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
     86 	u32 seq = 0;
     87 
     88 	if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
     89 		if (drv->cpu_addr) {
     90 			seq = le32_to_cpu(*drv->cpu_addr);
     91 		} else {
     92 			seq = lower_32_bits(atomic64_read(&drv->last_seq));
     93 		}
     94 	} else {
     95 		seq = RREG32(drv->scratch_reg);
     96 	}
     97 	return seq;
     98 }
     99 
    100 /**
    101  * radeon_fence_emit - emit a fence on the requested ring
    102  *
    103  * @rdev: radeon_device pointer
    104  * @fence: radeon fence object
    105  * @ring: ring index the fence is associated with
    106  *
    107  * Emits a fence command on the requested ring (all asics).
    108  * Returns 0 on success, -ENOMEM on failure.
    109  */
    110 int radeon_fence_emit(struct radeon_device *rdev,
    111 		      struct radeon_fence **fence,
    112 		      int ring)
    113 {
    114 	/* we are protected by the ring emission mutex */
    115 	*fence = kmalloc(sizeof(struct radeon_fence), GFP_KERNEL);
    116 	if ((*fence) == NULL) {
    117 		return -ENOMEM;
    118 	}
    119 	kref_init(&((*fence)->kref));
    120 	(*fence)->rdev = rdev;
    121 	(*fence)->seq = ++rdev->fence_drv[ring].sync_seq[ring];
    122 	(*fence)->ring = ring;
    123 	radeon_fence_ring_emit(rdev, ring, *fence);
    124 	trace_radeon_fence_emit(rdev->ddev, ring, (*fence)->seq);
    125 	return 0;
    126 }
    127 
    128 /**
    129  * radeon_fence_process - process a fence
    130  *
    131  * @rdev: radeon_device pointer
    132  * @ring: ring index the fence is associated with
    133  *
    134  * Checks the current fence value and wakes the fence queue
    135  * if the sequence number has increased (all asics).
    136  */
    137 void radeon_fence_process(struct radeon_device *rdev, int ring)
    138 {
    139 	uint64_t seq, last_seq, last_emitted;
    140 	unsigned count_loop = 0;
    141 	bool wake = false;
    142 
    143 	/* Note there is a scenario here for an infinite loop but it's
    144 	 * very unlikely to happen. For it to happen, the current polling
    145 	 * process need to be interrupted by another process and another
    146 	 * process needs to update the last_seq btw the atomic read and
    147 	 * xchg of the current process.
    148 	 *
    149 	 * More over for this to go in infinite loop there need to be
    150 	 * continuously new fence signaled ie radeon_fence_read needs
    151 	 * to return a different value each time for both the currently
    152 	 * polling process and the other process that xchg the last_seq
    153 	 * btw atomic read and xchg of the current process. And the
    154 	 * value the other process set as last seq must be higher than
    155 	 * the seq value we just read. Which means that current process
    156 	 * need to be interrupted after radeon_fence_read and before
    157 	 * atomic xchg.
    158 	 *
    159 	 * To be even more safe we count the number of time we loop and
    160 	 * we bail after 10 loop just accepting the fact that we might
    161 	 * have temporarly set the last_seq not to the true real last
    162 	 * seq but to an older one.
    163 	 */
    164 	last_seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
    165 	do {
    166 		last_emitted = rdev->fence_drv[ring].sync_seq[ring];
    167 		seq = radeon_fence_read(rdev, ring);
    168 		seq |= last_seq & 0xffffffff00000000LL;
    169 		if (seq < last_seq) {
    170 			seq &= 0xffffffff;
    171 			seq |= last_emitted & 0xffffffff00000000LL;
    172 		}
    173 
    174 		if (seq <= last_seq || seq > last_emitted) {
    175 			break;
    176 		}
    177 		/* If we loop over we don't want to return without
    178 		 * checking if a fence is signaled as it means that the
    179 		 * seq we just read is different from the previous on.
    180 		 */
    181 		wake = true;
    182 		last_seq = seq;
    183 		if ((count_loop++) > 10) {
    184 			/* We looped over too many time leave with the
    185 			 * fact that we might have set an older fence
    186 			 * seq then the current real last seq as signaled
    187 			 * by the hw.
    188 			 */
    189 			break;
    190 		}
    191 	} while (atomic64_xchg(&rdev->fence_drv[ring].last_seq, seq) > seq);
    192 
    193 	if (wake)
    194 #ifdef __NetBSD__
    195 		DRM_SPIN_WAKEUP_ALL(&rdev->fence_queue, &rdev->fence_lock);
    196 #else
    197 		wake_up_all(&rdev->fence_queue);
    198 #endif
    199 }
    200 
    201 /**
    202  * radeon_fence_destroy - destroy a fence
    203  *
    204  * @kref: fence kref
    205  *
    206  * Frees the fence object (all asics).
    207  */
    208 static void radeon_fence_destroy(struct kref *kref)
    209 {
    210 	struct radeon_fence *fence;
    211 
    212 	fence = container_of(kref, struct radeon_fence, kref);
    213 	kfree(fence);
    214 }
    215 
    216 /**
    217  * radeon_fence_seq_signaled - check if a fence sequence number has signaled
    218  *
    219  * @rdev: radeon device pointer
    220  * @seq: sequence number
    221  * @ring: ring index the fence is associated with
    222  *
    223  * Check if the last signaled fence sequnce number is >= the requested
    224  * sequence number (all asics).
    225  * Returns true if the fence has signaled (current fence value
    226  * is >= requested value) or false if it has not (current fence
    227  * value is < the requested value.  Helper function for
    228  * radeon_fence_signaled().
    229  */
    230 static bool radeon_fence_seq_signaled(struct radeon_device *rdev,
    231 				      u64 seq, unsigned ring)
    232 {
    233 	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
    234 		return true;
    235 	}
    236 	/* poll new last sequence at least once */
    237 	radeon_fence_process(rdev, ring);
    238 	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
    239 		return true;
    240 	}
    241 	return false;
    242 }
    243 
    244 /**
    245  * radeon_fence_signaled - check if a fence has signaled
    246  *
    247  * @fence: radeon fence object
    248  *
    249  * Check if the requested fence has signaled (all asics).
    250  * Returns true if the fence has signaled or false if it has not.
    251  */
    252 bool radeon_fence_signaled(struct radeon_fence *fence)
    253 {
    254 	if (!fence) {
    255 		return true;
    256 	}
    257 	if (fence->seq == RADEON_FENCE_SIGNALED_SEQ) {
    258 		return true;
    259 	}
    260 	if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
    261 		fence->seq = RADEON_FENCE_SIGNALED_SEQ;
    262 		return true;
    263 	}
    264 	return false;
    265 }
    266 
    267 /**
    268  * radeon_fence_any_seq_signaled - check if any sequence number is signaled
    269  *
    270  * @rdev: radeon device pointer
    271  * @seq: sequence numbers
    272  *
    273  * Check if the last signaled fence sequnce number is >= the requested
    274  * sequence number (all asics).
    275  * Returns true if any has signaled (current value is >= requested value)
    276  * or false if it has not. Helper function for radeon_fence_wait_seq.
    277  */
    278 static bool radeon_fence_any_seq_signaled(struct radeon_device *rdev, u64 *seq)
    279 {
    280 	unsigned i;
    281 
    282 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    283 		if (seq[i] && radeon_fence_seq_signaled(rdev, seq[i], i))
    284 			return true;
    285 	}
    286 	return false;
    287 }
    288 
    289 /**
    290  * radeon_fence_wait_seq - wait for a specific sequence numbers
    291  *
    292  * @rdev: radeon device pointer
    293  * @target_seq: sequence number(s) we want to wait for
    294  * @intr: use interruptable sleep
    295  *
    296  * Wait for the requested sequence number(s) to be written by any ring
    297  * (all asics).  Sequnce number array is indexed by ring id.
    298  * @intr selects whether to use interruptable (true) or non-interruptable
    299  * (false) sleep when waiting for the sequence number.  Helper function
    300  * for radeon_fence_wait_*().
    301  * Returns 0 if the sequence number has passed, error for all other cases.
    302  * -EDEADLK is returned when a GPU lockup has been detected.
    303  */
    304 static int radeon_fence_wait_seq(struct radeon_device *rdev, u64 *target_seq,
    305 				 bool intr)
    306 {
    307 	uint64_t last_seq[RADEON_NUM_RINGS];
    308 	bool signaled;
    309 	int i, r;
    310 
    311 	while (!radeon_fence_any_seq_signaled(rdev, target_seq)) {
    312 
    313 		/* Save current sequence values, used to check for GPU lockups */
    314 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    315 			if (!target_seq[i])
    316 				continue;
    317 
    318 			last_seq[i] = atomic64_read(&rdev->fence_drv[i].last_seq);
    319 			trace_radeon_fence_wait_begin(rdev->ddev, i, target_seq[i]);
    320 			radeon_irq_kms_sw_irq_get(rdev, i);
    321 		}
    322 
    323 #ifdef __NetBSD__
    324 		spin_lock(&rdev->fence_lock);
    325 		if (intr)
    326 			DRM_SPIN_TIMED_WAIT_UNTIL(r, &rdev->fence_queue,
    327 			    &rdev->fence_lock, RADEON_FENCE_JIFFIES_TIMEOUT,
    328 			    ((signaled = radeon_fence_any_seq_signaled(rdev,
    329 				    target_seq))
    330 				|| rdev->needs_reset));
    331 		else
    332 			DRM_SPIN_TIMED_WAIT_NOINTR_UNTIL(r, &rdev->fence_queue,
    333 			    &rdev->fence_lock, RADEON_FENCE_JIFFIES_TIMEOUT,
    334 			    ((signaled = radeon_fence_any_seq_signaled(rdev,
    335 				    target_seq))
    336 				|| rdev->needs_reset));
    337 		spin_unlock(&rdev->fence_lock);
    338 #else
    339 		if (intr) {
    340 			r = wait_event_interruptible_timeout(rdev->fence_queue, (
    341 				(signaled = radeon_fence_any_seq_signaled(rdev, target_seq))
    342 				 || rdev->needs_reset), RADEON_FENCE_JIFFIES_TIMEOUT);
    343 		} else {
    344 			r = wait_event_timeout(rdev->fence_queue, (
    345 				(signaled = radeon_fence_any_seq_signaled(rdev, target_seq))
    346 				 || rdev->needs_reset), RADEON_FENCE_JIFFIES_TIMEOUT);
    347 		}
    348 #endif
    349 
    350 		for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    351 			if (!target_seq[i])
    352 				continue;
    353 
    354 			radeon_irq_kms_sw_irq_put(rdev, i);
    355 			trace_radeon_fence_wait_end(rdev->ddev, i, target_seq[i]);
    356 		}
    357 
    358 		if (unlikely(r < 0))
    359 			return r;
    360 
    361 		if (unlikely(!signaled)) {
    362 			if (rdev->needs_reset)
    363 				return -EDEADLK;
    364 
    365 			/* we were interrupted for some reason and fence
    366 			 * isn't signaled yet, resume waiting */
    367 			if (r)
    368 				continue;
    369 
    370 			for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    371 				if (!target_seq[i])
    372 					continue;
    373 
    374 				if (last_seq[i] != atomic64_read(&rdev->fence_drv[i].last_seq))
    375 					break;
    376 			}
    377 
    378 			if (i != RADEON_NUM_RINGS)
    379 				continue;
    380 
    381 			for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    382 				if (!target_seq[i])
    383 					continue;
    384 
    385 				if (radeon_ring_is_lockup(rdev, i, &rdev->ring[i]))
    386 					break;
    387 			}
    388 
    389 			if (i < RADEON_NUM_RINGS) {
    390 				/* good news we believe it's a lockup */
    391 				dev_warn(rdev->dev, "GPU lockup (waiting for "
    392 					 "0x%016"PRIx64" last fence id 0x%016"PRIx64" on"
    393 					 " ring %d)\n",
    394 					 target_seq[i], last_seq[i], i);
    395 
    396 				/* remember that we need an reset */
    397 #ifdef __NetBSD__
    398 				spin_lock(&rdev->fence_lock);
    399 				rdev->needs_reset = true;
    400 				DRM_SPIN_WAKEUP_ALL(&rdev->fence_queue,
    401 				    &rdev->fence_lock);
    402 				spin_unlock(&rdev->fence_lock);
    403 #else
    404 				rdev->needs_reset = true;
    405 				wake_up_all(&rdev->fence_queue);
    406 #endif
    407 				return -EDEADLK;
    408 			}
    409 		}
    410 	}
    411 	return 0;
    412 }
    413 
    414 /**
    415  * radeon_fence_wait - wait for a fence to signal
    416  *
    417  * @fence: radeon fence object
    418  * @intr: use interruptable sleep
    419  *
    420  * Wait for the requested fence to signal (all asics).
    421  * @intr selects whether to use interruptable (true) or non-interruptable
    422  * (false) sleep when waiting for the fence.
    423  * Returns 0 if the fence has passed, error for all other cases.
    424  */
    425 int radeon_fence_wait(struct radeon_fence *fence, bool intr)
    426 {
    427 	uint64_t seq[RADEON_NUM_RINGS] = {};
    428 	int r;
    429 
    430 	if (fence == NULL) {
    431 		WARN(1, "Querying an invalid fence : %p !\n", fence);
    432 		return -EINVAL;
    433 	}
    434 
    435 	seq[fence->ring] = fence->seq;
    436 	if (seq[fence->ring] == RADEON_FENCE_SIGNALED_SEQ)
    437 		return 0;
    438 
    439 	r = radeon_fence_wait_seq(fence->rdev, seq, intr);
    440 	if (r)
    441 		return r;
    442 
    443 	fence->seq = RADEON_FENCE_SIGNALED_SEQ;
    444 	return 0;
    445 }
    446 
    447 /**
    448  * radeon_fence_wait_any - wait for a fence to signal on any ring
    449  *
    450  * @rdev: radeon device pointer
    451  * @fences: radeon fence object(s)
    452  * @intr: use interruptable sleep
    453  *
    454  * Wait for any requested fence to signal (all asics).  Fence
    455  * array is indexed by ring id.  @intr selects whether to use
    456  * interruptable (true) or non-interruptable (false) sleep when
    457  * waiting for the fences. Used by the suballocator.
    458  * Returns 0 if any fence has passed, error for all other cases.
    459  */
    460 int radeon_fence_wait_any(struct radeon_device *rdev,
    461 			  struct radeon_fence **fences,
    462 			  bool intr)
    463 {
    464 	uint64_t seq[RADEON_NUM_RINGS];
    465 	unsigned i, num_rings = 0;
    466 	int r;
    467 
    468 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    469 		seq[i] = 0;
    470 
    471 		if (!fences[i]) {
    472 			continue;
    473 		}
    474 
    475 		seq[i] = fences[i]->seq;
    476 		++num_rings;
    477 
    478 		/* test if something was allready signaled */
    479 		if (seq[i] == RADEON_FENCE_SIGNALED_SEQ)
    480 			return 0;
    481 	}
    482 
    483 	/* nothing to wait for ? */
    484 	if (num_rings == 0)
    485 		return -ENOENT;
    486 
    487 	r = radeon_fence_wait_seq(rdev, seq, intr);
    488 	if (r) {
    489 		return r;
    490 	}
    491 	return 0;
    492 }
    493 
    494 /**
    495  * radeon_fence_wait_next - wait for the next fence to signal
    496  *
    497  * @rdev: radeon device pointer
    498  * @ring: ring index the fence is associated with
    499  *
    500  * Wait for the next fence on the requested ring to signal (all asics).
    501  * Returns 0 if the next fence has passed, error for all other cases.
    502  * Caller must hold ring lock.
    503  */
    504 int radeon_fence_wait_next(struct radeon_device *rdev, int ring)
    505 {
    506 	uint64_t seq[RADEON_NUM_RINGS] = {};
    507 
    508 	seq[ring] = atomic64_read(&rdev->fence_drv[ring].last_seq) + 1ULL;
    509 	if (seq[ring] >= rdev->fence_drv[ring].sync_seq[ring]) {
    510 		/* nothing to wait for, last_seq is
    511 		   already the last emited fence */
    512 		return -ENOENT;
    513 	}
    514 	return radeon_fence_wait_seq(rdev, seq, false);
    515 }
    516 
    517 /**
    518  * radeon_fence_wait_empty - wait for all fences to signal
    519  *
    520  * @rdev: radeon device pointer
    521  * @ring: ring index the fence is associated with
    522  *
    523  * Wait for all fences on the requested ring to signal (all asics).
    524  * Returns 0 if the fences have passed, error for all other cases.
    525  * Caller must hold ring lock.
    526  */
    527 int radeon_fence_wait_empty(struct radeon_device *rdev, int ring)
    528 {
    529 	uint64_t seq[RADEON_NUM_RINGS] = {};
    530 	int r;
    531 
    532 	seq[ring] = rdev->fence_drv[ring].sync_seq[ring];
    533 	if (!seq[ring])
    534 		return 0;
    535 
    536 	r = radeon_fence_wait_seq(rdev, seq, false);
    537 	if (r) {
    538 		if (r == -EDEADLK)
    539 			return -EDEADLK;
    540 
    541 		dev_err(rdev->dev, "error waiting for ring[%d] to become idle (%d)\n",
    542 			ring, r);
    543 	}
    544 	return 0;
    545 }
    546 
    547 /**
    548  * radeon_fence_ref - take a ref on a fence
    549  *
    550  * @fence: radeon fence object
    551  *
    552  * Take a reference on a fence (all asics).
    553  * Returns the fence.
    554  */
    555 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
    556 {
    557 	kref_get(&fence->kref);
    558 	return fence;
    559 }
    560 
    561 /**
    562  * radeon_fence_unref - remove a ref on a fence
    563  *
    564  * @fence: radeon fence object
    565  *
    566  * Remove a reference on a fence (all asics).
    567  */
    568 void radeon_fence_unref(struct radeon_fence **fence)
    569 {
    570 	struct radeon_fence *tmp = *fence;
    571 
    572 	*fence = NULL;
    573 	if (tmp) {
    574 		kref_put(&tmp->kref, radeon_fence_destroy);
    575 	}
    576 }
    577 
    578 /**
    579  * radeon_fence_count_emitted - get the count of emitted fences
    580  *
    581  * @rdev: radeon device pointer
    582  * @ring: ring index the fence is associated with
    583  *
    584  * Get the number of fences emitted on the requested ring (all asics).
    585  * Returns the number of emitted fences on the ring.  Used by the
    586  * dynpm code to ring track activity.
    587  */
    588 unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
    589 {
    590 	uint64_t emitted;
    591 
    592 	/* We are not protected by ring lock when reading the last sequence
    593 	 * but it's ok to report slightly wrong fence count here.
    594 	 */
    595 	radeon_fence_process(rdev, ring);
    596 	emitted = rdev->fence_drv[ring].sync_seq[ring]
    597 		- atomic64_read(&rdev->fence_drv[ring].last_seq);
    598 	/* to avoid 32bits warp around */
    599 	if (emitted > 0x10000000) {
    600 		emitted = 0x10000000;
    601 	}
    602 	return (unsigned)emitted;
    603 }
    604 
    605 /**
    606  * radeon_fence_need_sync - do we need a semaphore
    607  *
    608  * @fence: radeon fence object
    609  * @dst_ring: which ring to check against
    610  *
    611  * Check if the fence needs to be synced against another ring
    612  * (all asics).  If so, we need to emit a semaphore.
    613  * Returns true if we need to sync with another ring, false if
    614  * not.
    615  */
    616 bool radeon_fence_need_sync(struct radeon_fence *fence, int dst_ring)
    617 {
    618 	struct radeon_fence_driver *fdrv;
    619 
    620 	if (!fence) {
    621 		return false;
    622 	}
    623 
    624 	if (fence->ring == dst_ring) {
    625 		return false;
    626 	}
    627 
    628 	/* we are protected by the ring mutex */
    629 	fdrv = &fence->rdev->fence_drv[dst_ring];
    630 	if (fence->seq <= fdrv->sync_seq[fence->ring]) {
    631 		return false;
    632 	}
    633 
    634 	return true;
    635 }
    636 
    637 /**
    638  * radeon_fence_note_sync - record the sync point
    639  *
    640  * @fence: radeon fence object
    641  * @dst_ring: which ring to check against
    642  *
    643  * Note the sequence number at which point the fence will
    644  * be synced with the requested ring (all asics).
    645  */
    646 void radeon_fence_note_sync(struct radeon_fence *fence, int dst_ring)
    647 {
    648 	struct radeon_fence_driver *dst, *src;
    649 	unsigned i;
    650 
    651 	if (!fence) {
    652 		return;
    653 	}
    654 
    655 	if (fence->ring == dst_ring) {
    656 		return;
    657 	}
    658 
    659 	/* we are protected by the ring mutex */
    660 	src = &fence->rdev->fence_drv[fence->ring];
    661 	dst = &fence->rdev->fence_drv[dst_ring];
    662 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    663 		if (i == dst_ring) {
    664 			continue;
    665 		}
    666 		dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
    667 	}
    668 }
    669 
    670 /**
    671  * radeon_fence_driver_start_ring - make the fence driver
    672  * ready for use on the requested ring.
    673  *
    674  * @rdev: radeon device pointer
    675  * @ring: ring index to start the fence driver on
    676  *
    677  * Make the fence driver ready for processing (all asics).
    678  * Not all asics have all rings, so each asic will only
    679  * start the fence driver on the rings it has.
    680  * Returns 0 for success, errors for failure.
    681  */
    682 int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
    683 {
    684 	uint64_t index;
    685 	int r;
    686 
    687 	radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
    688 	if (rdev->wb.use_event || !radeon_ring_supports_scratch_reg(rdev, &rdev->ring[ring])) {
    689 		rdev->fence_drv[ring].scratch_reg = 0;
    690 		if (ring != R600_RING_TYPE_UVD_INDEX) {
    691 			index = R600_WB_EVENT_OFFSET + ring * 4;
    692 			rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
    693 			rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr +
    694 							 index;
    695 
    696 		} else {
    697 			/* put fence directly behind firmware */
    698 #ifdef __NetBSD__		/* XXX ALIGN means something else.  */
    699 			index = round_up(rdev->uvd_fw->size, 8);
    700 #else
    701 			index = ALIGN(rdev->uvd_fw->size, 8);
    702 #endif
    703 			rdev->fence_drv[ring].cpu_addr = (uint32_t *)((uint8_t *)rdev->uvd.cpu_addr + index);
    704 			rdev->fence_drv[ring].gpu_addr = rdev->uvd.gpu_addr + index;
    705 		}
    706 
    707 	} else {
    708 		r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
    709 		if (r) {
    710 			dev_err(rdev->dev, "fence failed to get scratch register\n");
    711 			return r;
    712 		}
    713 		index = RADEON_WB_SCRATCH_OFFSET +
    714 			rdev->fence_drv[ring].scratch_reg -
    715 			rdev->scratch.reg_base;
    716 		rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
    717 		rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
    718 	}
    719 	radeon_fence_write(rdev, atomic64_read(&rdev->fence_drv[ring].last_seq), ring);
    720 	rdev->fence_drv[ring].initialized = true;
    721 	dev_info(rdev->dev, "fence driver on ring %d use gpu addr 0x%016"PRIx64" and cpu addr 0x%p\n",
    722 		 ring, rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
    723 	return 0;
    724 }
    725 
    726 /**
    727  * radeon_fence_driver_init_ring - init the fence driver
    728  * for the requested ring.
    729  *
    730  * @rdev: radeon device pointer
    731  * @ring: ring index to start the fence driver on
    732  *
    733  * Init the fence driver for the requested ring (all asics).
    734  * Helper function for radeon_fence_driver_init().
    735  */
    736 static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
    737 {
    738 	int i;
    739 
    740 	rdev->fence_drv[ring].scratch_reg = -1;
    741 	rdev->fence_drv[ring].cpu_addr = NULL;
    742 	rdev->fence_drv[ring].gpu_addr = 0;
    743 	for (i = 0; i < RADEON_NUM_RINGS; ++i)
    744 		rdev->fence_drv[ring].sync_seq[i] = 0;
    745 	atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
    746 	rdev->fence_drv[ring].initialized = false;
    747 }
    748 
    749 /**
    750  * radeon_fence_driver_init - init the fence driver
    751  * for all possible rings.
    752  *
    753  * @rdev: radeon device pointer
    754  *
    755  * Init the fence driver for all possible rings (all asics).
    756  * Not all asics have all rings, so each asic will only
    757  * start the fence driver on the rings it has using
    758  * radeon_fence_driver_start_ring().
    759  * Returns 0 for success.
    760  */
    761 int radeon_fence_driver_init(struct radeon_device *rdev)
    762 {
    763 	int ring;
    764 
    765 #ifdef __NetBSD__
    766 	spin_lock_init(&rdev->fence_lock);
    767 	DRM_INIT_WAITQUEUE(&rdev->fence_queue, "radfence");
    768 #else
    769 	init_waitqueue_head(&rdev->fence_queue);
    770 #endif
    771 	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
    772 		radeon_fence_driver_init_ring(rdev, ring);
    773 	}
    774 	if (radeon_debugfs_fence_init(rdev)) {
    775 		dev_err(rdev->dev, "fence debugfs file creation failed\n");
    776 	}
    777 	return 0;
    778 }
    779 
    780 /**
    781  * radeon_fence_driver_fini - tear down the fence driver
    782  * for all possible rings.
    783  *
    784  * @rdev: radeon device pointer
    785  *
    786  * Tear down the fence driver for all possible rings (all asics).
    787  */
    788 void radeon_fence_driver_fini(struct radeon_device *rdev)
    789 {
    790 	int ring, r;
    791 
    792 	mutex_lock(&rdev->ring_lock);
    793 	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
    794 		if (!rdev->fence_drv[ring].initialized)
    795 			continue;
    796 		r = radeon_fence_wait_empty(rdev, ring);
    797 		if (r) {
    798 			/* no need to trigger GPU reset as we are unloading */
    799 			radeon_fence_driver_force_completion(rdev);
    800 		}
    801 #ifdef __NetBSD__
    802 		spin_lock(&rdev->fence_lock);
    803 		DRM_SPIN_WAKEUP_ALL(&rdev->fence_queue, &rdev->fence_lock);
    804 		spin_unlock(&rdev->fence_lock);
    805 #else
    806 		wake_up_all(&rdev->fence_queue);
    807 #endif
    808 		radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
    809 		rdev->fence_drv[ring].initialized = false;
    810 	}
    811 	mutex_unlock(&rdev->ring_lock);
    812 
    813 #ifdef __NetBSD__
    814 	DRM_DESTROY_WAITQUEUE(&rdev->fence_queue);
    815 	spin_lock_destroy(&rdev->fence_lock);
    816 #endif
    817 }
    818 
    819 /**
    820  * radeon_fence_driver_force_completion - force all fence waiter to complete
    821  *
    822  * @rdev: radeon device pointer
    823  *
    824  * In case of GPU reset failure make sure no process keep waiting on fence
    825  * that will never complete.
    826  */
    827 void radeon_fence_driver_force_completion(struct radeon_device *rdev)
    828 {
    829 	int ring;
    830 
    831 	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
    832 		if (!rdev->fence_drv[ring].initialized)
    833 			continue;
    834 		radeon_fence_write(rdev, rdev->fence_drv[ring].sync_seq[ring], ring);
    835 	}
    836 }
    837 
    838 
    839 /*
    840  * Fence debugfs
    841  */
    842 #if defined(CONFIG_DEBUG_FS)
    843 static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
    844 {
    845 	struct drm_info_node *node = (struct drm_info_node *)m->private;
    846 	struct drm_device *dev = node->minor->dev;
    847 	struct radeon_device *rdev = dev->dev_private;
    848 	int i, j;
    849 
    850 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    851 		if (!rdev->fence_drv[i].initialized)
    852 			continue;
    853 
    854 		radeon_fence_process(rdev, i);
    855 
    856 		seq_printf(m, "--- ring %d ---\n", i);
    857 		seq_printf(m, "Last signaled fence 0x%016llx\n",
    858 			   (unsigned long long)atomic64_read(&rdev->fence_drv[i].last_seq));
    859 		seq_printf(m, "Last emitted        0x%016"PRIx64"\n",
    860 			   rdev->fence_drv[i].sync_seq[i]);
    861 
    862 		for (j = 0; j < RADEON_NUM_RINGS; ++j) {
    863 			if (i != j && rdev->fence_drv[j].initialized)
    864 				seq_printf(m, "Last sync to ring %d 0x%016"PRIx64"\n",
    865 					   j, rdev->fence_drv[i].sync_seq[j]);
    866 		}
    867 	}
    868 	return 0;
    869 }
    870 
    871 static struct drm_info_list radeon_debugfs_fence_list[] = {
    872 	{"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
    873 };
    874 #endif
    875 
    876 int radeon_debugfs_fence_init(struct radeon_device *rdev)
    877 {
    878 #if defined(CONFIG_DEBUG_FS)
    879 	return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 1);
    880 #else
    881 	return 0;
    882 #endif
    883 }
    884