Home | History | Annotate | Line # | Download | only in radeon
radeon_fence.c revision 1.1.1.3
      1 /*	$NetBSD: radeon_fence.c,v 1.1.1.3 2021/12/18 20:15:48 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 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: radeon_fence.c,v 1.1.1.3 2021/12/18 20:15:48 riastradh Exp $");
     36 
     37 #include <linux/atomic.h>
     38 #include <linux/firmware.h>
     39 #include <linux/kref.h>
     40 #include <linux/sched/signal.h>
     41 #include <linux/seq_file.h>
     42 #include <linux/slab.h>
     43 #include <linux/wait.h>
     44 
     45 #include <drm/drm_debugfs.h>
     46 #include <drm/drm_device.h>
     47 #include <drm/drm_file.h>
     48 
     49 #include "radeon.h"
     50 #include "radeon_reg.h"
     51 #include "radeon_trace.h"
     52 
     53 /*
     54  * Fences
     55  * Fences mark an event in the GPUs pipeline and are used
     56  * for GPU/CPU synchronization.  When the fence is written,
     57  * it is expected that all buffers associated with that fence
     58  * are no longer in use by the associated ring on the GPU and
     59  * that the the relevant GPU caches have been flushed.  Whether
     60  * we use a scratch register or memory location depends on the asic
     61  * and whether writeback is enabled.
     62  */
     63 
     64 /**
     65  * radeon_fence_write - write a fence value
     66  *
     67  * @rdev: radeon_device pointer
     68  * @seq: sequence number to write
     69  * @ring: ring index the fence is associated with
     70  *
     71  * Writes a fence value to memory or a scratch register (all asics).
     72  */
     73 static void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
     74 {
     75 	struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
     76 	if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
     77 		if (drv->cpu_addr) {
     78 			*drv->cpu_addr = cpu_to_le32(seq);
     79 		}
     80 	} else {
     81 		WREG32(drv->scratch_reg, seq);
     82 	}
     83 }
     84 
     85 /**
     86  * radeon_fence_read - read a fence value
     87  *
     88  * @rdev: radeon_device pointer
     89  * @ring: ring index the fence is associated with
     90  *
     91  * Reads a fence value from memory or a scratch register (all asics).
     92  * Returns the value of the fence read from memory or register.
     93  */
     94 static u32 radeon_fence_read(struct radeon_device *rdev, int ring)
     95 {
     96 	struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
     97 	u32 seq = 0;
     98 
     99 	if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
    100 		if (drv->cpu_addr) {
    101 			seq = le32_to_cpu(*drv->cpu_addr);
    102 		} else {
    103 			seq = lower_32_bits(atomic64_read(&drv->last_seq));
    104 		}
    105 	} else {
    106 		seq = RREG32(drv->scratch_reg);
    107 	}
    108 	return seq;
    109 }
    110 
    111 /**
    112  * radeon_fence_schedule_check - schedule lockup check
    113  *
    114  * @rdev: radeon_device pointer
    115  * @ring: ring index we should work with
    116  *
    117  * Queues a delayed work item to check for lockups.
    118  */
    119 static void radeon_fence_schedule_check(struct radeon_device *rdev, int ring)
    120 {
    121 	/*
    122 	 * Do not reset the timer here with mod_delayed_work,
    123 	 * this can livelock in an interaction with TTM delayed destroy.
    124 	 */
    125 	queue_delayed_work(system_power_efficient_wq,
    126 			   &rdev->fence_drv[ring].lockup_work,
    127 			   RADEON_FENCE_JIFFIES_TIMEOUT);
    128 }
    129 
    130 /**
    131  * radeon_fence_emit - emit a fence on the requested ring
    132  *
    133  * @rdev: radeon_device pointer
    134  * @fence: radeon fence object
    135  * @ring: ring index the fence is associated with
    136  *
    137  * Emits a fence command on the requested ring (all asics).
    138  * Returns 0 on success, -ENOMEM on failure.
    139  */
    140 int radeon_fence_emit(struct radeon_device *rdev,
    141 		      struct radeon_fence **fence,
    142 		      int ring)
    143 {
    144 	u64 seq;
    145 
    146 	/* we are protected by the ring emission mutex */
    147 	*fence = kmalloc(sizeof(struct radeon_fence), GFP_KERNEL);
    148 	if ((*fence) == NULL) {
    149 		return -ENOMEM;
    150 	}
    151 	(*fence)->rdev = rdev;
    152 	(*fence)->seq = seq = ++rdev->fence_drv[ring].sync_seq[ring];
    153 	(*fence)->ring = ring;
    154 	(*fence)->is_vm_update = false;
    155 	dma_fence_init(&(*fence)->base, &radeon_fence_ops,
    156 		       &rdev->fence_queue.lock,
    157 		       rdev->fence_context + ring,
    158 		       seq);
    159 	radeon_fence_ring_emit(rdev, ring, *fence);
    160 	trace_radeon_fence_emit(rdev->ddev, ring, (*fence)->seq);
    161 	radeon_fence_schedule_check(rdev, ring);
    162 	return 0;
    163 }
    164 
    165 /**
    166  * radeon_fence_check_signaled - callback from fence_queue
    167  *
    168  * this function is called with fence_queue lock held, which is also used
    169  * for the fence locking itself, so unlocked variants are used for
    170  * fence_signal, and remove_wait_queue.
    171  */
    172 static int radeon_fence_check_signaled(wait_queue_entry_t *wait, unsigned mode, int flags, void *key)
    173 {
    174 	struct radeon_fence *fence;
    175 	u64 seq;
    176 
    177 	fence = container_of(wait, struct radeon_fence, fence_wake);
    178 
    179 	/*
    180 	 * We cannot use radeon_fence_process here because we're already
    181 	 * in the waitqueue, in a call from wake_up_all.
    182 	 */
    183 	seq = atomic64_read(&fence->rdev->fence_drv[fence->ring].last_seq);
    184 	if (seq >= fence->seq) {
    185 		int ret = dma_fence_signal_locked(&fence->base);
    186 
    187 		if (!ret)
    188 			DMA_FENCE_TRACE(&fence->base, "signaled from irq context\n");
    189 		else
    190 			DMA_FENCE_TRACE(&fence->base, "was already signaled\n");
    191 
    192 		radeon_irq_kms_sw_irq_put(fence->rdev, fence->ring);
    193 		__remove_wait_queue(&fence->rdev->fence_queue, &fence->fence_wake);
    194 		dma_fence_put(&fence->base);
    195 	} else
    196 		DMA_FENCE_TRACE(&fence->base, "pending\n");
    197 	return 0;
    198 }
    199 
    200 /**
    201  * radeon_fence_activity - check for fence activity
    202  *
    203  * @rdev: radeon_device pointer
    204  * @ring: ring index the fence is associated with
    205  *
    206  * Checks the current fence value and calculates the last
    207  * signalled fence value. Returns true if activity occured
    208  * on the ring, and the fence_queue should be waken up.
    209  */
    210 static bool radeon_fence_activity(struct radeon_device *rdev, int ring)
    211 {
    212 	uint64_t seq, last_seq, last_emitted;
    213 	unsigned count_loop = 0;
    214 	bool wake = false;
    215 
    216 	/* Note there is a scenario here for an infinite loop but it's
    217 	 * very unlikely to happen. For it to happen, the current polling
    218 	 * process need to be interrupted by another process and another
    219 	 * process needs to update the last_seq btw the atomic read and
    220 	 * xchg of the current process.
    221 	 *
    222 	 * More over for this to go in infinite loop there need to be
    223 	 * continuously new fence signaled ie radeon_fence_read needs
    224 	 * to return a different value each time for both the currently
    225 	 * polling process and the other process that xchg the last_seq
    226 	 * btw atomic read and xchg of the current process. And the
    227 	 * value the other process set as last seq must be higher than
    228 	 * the seq value we just read. Which means that current process
    229 	 * need to be interrupted after radeon_fence_read and before
    230 	 * atomic xchg.
    231 	 *
    232 	 * To be even more safe we count the number of time we loop and
    233 	 * we bail after 10 loop just accepting the fact that we might
    234 	 * have temporarly set the last_seq not to the true real last
    235 	 * seq but to an older one.
    236 	 */
    237 	last_seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
    238 	do {
    239 		last_emitted = rdev->fence_drv[ring].sync_seq[ring];
    240 		seq = radeon_fence_read(rdev, ring);
    241 		seq |= last_seq & 0xffffffff00000000LL;
    242 		if (seq < last_seq) {
    243 			seq &= 0xffffffff;
    244 			seq |= last_emitted & 0xffffffff00000000LL;
    245 		}
    246 
    247 		if (seq <= last_seq || seq > last_emitted) {
    248 			break;
    249 		}
    250 		/* If we loop over we don't want to return without
    251 		 * checking if a fence is signaled as it means that the
    252 		 * seq we just read is different from the previous on.
    253 		 */
    254 		wake = true;
    255 		last_seq = seq;
    256 		if ((count_loop++) > 10) {
    257 			/* We looped over too many time leave with the
    258 			 * fact that we might have set an older fence
    259 			 * seq then the current real last seq as signaled
    260 			 * by the hw.
    261 			 */
    262 			break;
    263 		}
    264 	} while (atomic64_xchg(&rdev->fence_drv[ring].last_seq, seq) > seq);
    265 
    266 	if (seq < last_emitted)
    267 		radeon_fence_schedule_check(rdev, ring);
    268 
    269 	return wake;
    270 }
    271 
    272 /**
    273  * radeon_fence_check_lockup - check for hardware lockup
    274  *
    275  * @work: delayed work item
    276  *
    277  * Checks for fence activity and if there is none probe
    278  * the hardware if a lockup occured.
    279  */
    280 static void radeon_fence_check_lockup(struct work_struct *work)
    281 {
    282 	struct radeon_fence_driver *fence_drv;
    283 	struct radeon_device *rdev;
    284 	int ring;
    285 
    286 	fence_drv = container_of(work, struct radeon_fence_driver,
    287 				 lockup_work.work);
    288 	rdev = fence_drv->rdev;
    289 	ring = fence_drv - &rdev->fence_drv[0];
    290 
    291 	if (!down_read_trylock(&rdev->exclusive_lock)) {
    292 		/* just reschedule the check if a reset is going on */
    293 		radeon_fence_schedule_check(rdev, ring);
    294 		return;
    295 	}
    296 
    297 	if (fence_drv->delayed_irq && rdev->ddev->irq_enabled) {
    298 		unsigned long irqflags;
    299 
    300 		fence_drv->delayed_irq = false;
    301 		spin_lock_irqsave(&rdev->irq.lock, irqflags);
    302 		radeon_irq_set(rdev);
    303 		spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
    304 	}
    305 
    306 	if (radeon_fence_activity(rdev, ring))
    307 		wake_up_all(&rdev->fence_queue);
    308 
    309 	else if (radeon_ring_is_lockup(rdev, ring, &rdev->ring[ring])) {
    310 
    311 		/* good news we believe it's a lockup */
    312 		dev_warn(rdev->dev, "GPU lockup (current fence id "
    313 			 "0x%016llx last fence id 0x%016llx on ring %d)\n",
    314 			 (uint64_t)atomic64_read(&fence_drv->last_seq),
    315 			 fence_drv->sync_seq[ring], ring);
    316 
    317 		/* remember that we need an reset */
    318 		rdev->needs_reset = true;
    319 		wake_up_all(&rdev->fence_queue);
    320 	}
    321 	up_read(&rdev->exclusive_lock);
    322 }
    323 
    324 /**
    325  * radeon_fence_process - process a fence
    326  *
    327  * @rdev: radeon_device pointer
    328  * @ring: ring index the fence is associated with
    329  *
    330  * Checks the current fence value and wakes the fence queue
    331  * if the sequence number has increased (all asics).
    332  */
    333 void radeon_fence_process(struct radeon_device *rdev, int ring)
    334 {
    335 	if (radeon_fence_activity(rdev, ring))
    336 		wake_up_all(&rdev->fence_queue);
    337 }
    338 
    339 /**
    340  * radeon_fence_seq_signaled - check if a fence sequence number has signaled
    341  *
    342  * @rdev: radeon device pointer
    343  * @seq: sequence number
    344  * @ring: ring index the fence is associated with
    345  *
    346  * Check if the last signaled fence sequnce number is >= the requested
    347  * sequence number (all asics).
    348  * Returns true if the fence has signaled (current fence value
    349  * is >= requested value) or false if it has not (current fence
    350  * value is < the requested value.  Helper function for
    351  * radeon_fence_signaled().
    352  */
    353 static bool radeon_fence_seq_signaled(struct radeon_device *rdev,
    354 				      u64 seq, unsigned ring)
    355 {
    356 	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
    357 		return true;
    358 	}
    359 	/* poll new last sequence at least once */
    360 	radeon_fence_process(rdev, ring);
    361 	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
    362 		return true;
    363 	}
    364 	return false;
    365 }
    366 
    367 static bool radeon_fence_is_signaled(struct dma_fence *f)
    368 {
    369 	struct radeon_fence *fence = to_radeon_fence(f);
    370 	struct radeon_device *rdev = fence->rdev;
    371 	unsigned ring = fence->ring;
    372 	u64 seq = fence->seq;
    373 
    374 	if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
    375 		return true;
    376 	}
    377 
    378 	if (down_read_trylock(&rdev->exclusive_lock)) {
    379 		radeon_fence_process(rdev, ring);
    380 		up_read(&rdev->exclusive_lock);
    381 
    382 		if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
    383 			return true;
    384 		}
    385 	}
    386 	return false;
    387 }
    388 
    389 /**
    390  * radeon_fence_enable_signaling - enable signalling on fence
    391  * @fence: fence
    392  *
    393  * This function is called with fence_queue lock held, and adds a callback
    394  * to fence_queue that checks if this fence is signaled, and if so it
    395  * signals the fence and removes itself.
    396  */
    397 static bool radeon_fence_enable_signaling(struct dma_fence *f)
    398 {
    399 	struct radeon_fence *fence = to_radeon_fence(f);
    400 	struct radeon_device *rdev = fence->rdev;
    401 
    402 	if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq)
    403 		return false;
    404 
    405 	if (down_read_trylock(&rdev->exclusive_lock)) {
    406 		radeon_irq_kms_sw_irq_get(rdev, fence->ring);
    407 
    408 		if (radeon_fence_activity(rdev, fence->ring))
    409 			wake_up_all_locked(&rdev->fence_queue);
    410 
    411 		/* did fence get signaled after we enabled the sw irq? */
    412 		if (atomic64_read(&rdev->fence_drv[fence->ring].last_seq) >= fence->seq) {
    413 			radeon_irq_kms_sw_irq_put(rdev, fence->ring);
    414 			up_read(&rdev->exclusive_lock);
    415 			return false;
    416 		}
    417 
    418 		up_read(&rdev->exclusive_lock);
    419 	} else {
    420 		/* we're probably in a lockup, lets not fiddle too much */
    421 		if (radeon_irq_kms_sw_irq_get_delayed(rdev, fence->ring))
    422 			rdev->fence_drv[fence->ring].delayed_irq = true;
    423 		radeon_fence_schedule_check(rdev, fence->ring);
    424 	}
    425 
    426 	fence->fence_wake.flags = 0;
    427 	fence->fence_wake.private = NULL;
    428 	fence->fence_wake.func = radeon_fence_check_signaled;
    429 	__add_wait_queue(&rdev->fence_queue, &fence->fence_wake);
    430 	dma_fence_get(f);
    431 
    432 	DMA_FENCE_TRACE(&fence->base, "armed on ring %i!\n", fence->ring);
    433 	return true;
    434 }
    435 
    436 /**
    437  * radeon_fence_signaled - check if a fence has signaled
    438  *
    439  * @fence: radeon fence object
    440  *
    441  * Check if the requested fence has signaled (all asics).
    442  * Returns true if the fence has signaled or false if it has not.
    443  */
    444 bool radeon_fence_signaled(struct radeon_fence *fence)
    445 {
    446 	if (!fence)
    447 		return true;
    448 
    449 	if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
    450 		int ret;
    451 
    452 		ret = dma_fence_signal(&fence->base);
    453 		if (!ret)
    454 			DMA_FENCE_TRACE(&fence->base, "signaled from radeon_fence_signaled\n");
    455 		return true;
    456 	}
    457 	return false;
    458 }
    459 
    460 /**
    461  * radeon_fence_any_seq_signaled - check if any sequence number is signaled
    462  *
    463  * @rdev: radeon device pointer
    464  * @seq: sequence numbers
    465  *
    466  * Check if the last signaled fence sequnce number is >= the requested
    467  * sequence number (all asics).
    468  * Returns true if any has signaled (current value is >= requested value)
    469  * or false if it has not. Helper function for radeon_fence_wait_seq.
    470  */
    471 static bool radeon_fence_any_seq_signaled(struct radeon_device *rdev, u64 *seq)
    472 {
    473 	unsigned i;
    474 
    475 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    476 		if (seq[i] && radeon_fence_seq_signaled(rdev, seq[i], i))
    477 			return true;
    478 	}
    479 	return false;
    480 }
    481 
    482 /**
    483  * radeon_fence_wait_seq_timeout - wait for a specific sequence numbers
    484  *
    485  * @rdev: radeon device pointer
    486  * @target_seq: sequence number(s) we want to wait for
    487  * @intr: use interruptable sleep
    488  * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
    489  *
    490  * Wait for the requested sequence number(s) to be written by any ring
    491  * (all asics).  Sequnce number array is indexed by ring id.
    492  * @intr selects whether to use interruptable (true) or non-interruptable
    493  * (false) sleep when waiting for the sequence number.  Helper function
    494  * for radeon_fence_wait_*().
    495  * Returns remaining time if the sequence number has passed, 0 when
    496  * the wait timeout, or an error for all other cases.
    497  * -EDEADLK is returned when a GPU lockup has been detected.
    498  */
    499 static long radeon_fence_wait_seq_timeout(struct radeon_device *rdev,
    500 					  u64 *target_seq, bool intr,
    501 					  long timeout)
    502 {
    503 	long r;
    504 	int i;
    505 
    506 	if (radeon_fence_any_seq_signaled(rdev, target_seq))
    507 		return timeout;
    508 
    509 	/* enable IRQs and tracing */
    510 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    511 		if (!target_seq[i])
    512 			continue;
    513 
    514 		trace_radeon_fence_wait_begin(rdev->ddev, i, target_seq[i]);
    515 		radeon_irq_kms_sw_irq_get(rdev, i);
    516 	}
    517 
    518 	if (intr) {
    519 		r = wait_event_interruptible_timeout(rdev->fence_queue, (
    520 			radeon_fence_any_seq_signaled(rdev, target_seq)
    521 			 || rdev->needs_reset), timeout);
    522 	} else {
    523 		r = wait_event_timeout(rdev->fence_queue, (
    524 			radeon_fence_any_seq_signaled(rdev, target_seq)
    525 			 || rdev->needs_reset), timeout);
    526 	}
    527 
    528 	if (rdev->needs_reset)
    529 		r = -EDEADLK;
    530 
    531 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    532 		if (!target_seq[i])
    533 			continue;
    534 
    535 		radeon_irq_kms_sw_irq_put(rdev, i);
    536 		trace_radeon_fence_wait_end(rdev->ddev, i, target_seq[i]);
    537 	}
    538 
    539 	return r;
    540 }
    541 
    542 /**
    543  * radeon_fence_wait_timeout - wait for a fence to signal with timeout
    544  *
    545  * @fence: radeon fence object
    546  * @intr: use interruptible sleep
    547  *
    548  * Wait for the requested fence to signal (all asics).
    549  * @intr selects whether to use interruptable (true) or non-interruptable
    550  * (false) sleep when waiting for the fence.
    551  * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
    552  * Returns remaining time if the sequence number has passed, 0 when
    553  * the wait timeout, or an error for all other cases.
    554  */
    555 long radeon_fence_wait_timeout(struct radeon_fence *fence, bool intr, long timeout)
    556 {
    557 	uint64_t seq[RADEON_NUM_RINGS] = {};
    558 	long r;
    559 	int r_sig;
    560 
    561 	/*
    562 	 * This function should not be called on !radeon fences.
    563 	 * If this is the case, it would mean this function can
    564 	 * also be called on radeon fences belonging to another card.
    565 	 * exclusive_lock is not held in that case.
    566 	 */
    567 	if (WARN_ON_ONCE(!to_radeon_fence(&fence->base)))
    568 		return dma_fence_wait(&fence->base, intr);
    569 
    570 	seq[fence->ring] = fence->seq;
    571 	r = radeon_fence_wait_seq_timeout(fence->rdev, seq, intr, timeout);
    572 	if (r <= 0) {
    573 		return r;
    574 	}
    575 
    576 	r_sig = dma_fence_signal(&fence->base);
    577 	if (!r_sig)
    578 		DMA_FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
    579 	return r;
    580 }
    581 
    582 /**
    583  * radeon_fence_wait - wait for a fence to signal
    584  *
    585  * @fence: radeon fence object
    586  * @intr: use interruptible sleep
    587  *
    588  * Wait for the requested fence to signal (all asics).
    589  * @intr selects whether to use interruptable (true) or non-interruptable
    590  * (false) sleep when waiting for the fence.
    591  * Returns 0 if the fence has passed, error for all other cases.
    592  */
    593 int radeon_fence_wait(struct radeon_fence *fence, bool intr)
    594 {
    595 	long r = radeon_fence_wait_timeout(fence, intr, MAX_SCHEDULE_TIMEOUT);
    596 	if (r > 0) {
    597 		return 0;
    598 	} else {
    599 		return r;
    600 	}
    601 }
    602 
    603 /**
    604  * radeon_fence_wait_any - wait for a fence to signal on any ring
    605  *
    606  * @rdev: radeon device pointer
    607  * @fences: radeon fence object(s)
    608  * @intr: use interruptable sleep
    609  *
    610  * Wait for any requested fence to signal (all asics).  Fence
    611  * array is indexed by ring id.  @intr selects whether to use
    612  * interruptable (true) or non-interruptable (false) sleep when
    613  * waiting for the fences. Used by the suballocator.
    614  * Returns 0 if any fence has passed, error for all other cases.
    615  */
    616 int radeon_fence_wait_any(struct radeon_device *rdev,
    617 			  struct radeon_fence **fences,
    618 			  bool intr)
    619 {
    620 	uint64_t seq[RADEON_NUM_RINGS];
    621 	unsigned i, num_rings = 0;
    622 	long r;
    623 
    624 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    625 		seq[i] = 0;
    626 
    627 		if (!fences[i]) {
    628 			continue;
    629 		}
    630 
    631 		seq[i] = fences[i]->seq;
    632 		++num_rings;
    633 	}
    634 
    635 	/* nothing to wait for ? */
    636 	if (num_rings == 0)
    637 		return -ENOENT;
    638 
    639 	r = radeon_fence_wait_seq_timeout(rdev, seq, intr, MAX_SCHEDULE_TIMEOUT);
    640 	if (r < 0) {
    641 		return r;
    642 	}
    643 	return 0;
    644 }
    645 
    646 /**
    647  * radeon_fence_wait_next - wait for the next fence to signal
    648  *
    649  * @rdev: radeon device pointer
    650  * @ring: ring index the fence is associated with
    651  *
    652  * Wait for the next fence on the requested ring to signal (all asics).
    653  * Returns 0 if the next fence has passed, error for all other cases.
    654  * Caller must hold ring lock.
    655  */
    656 int radeon_fence_wait_next(struct radeon_device *rdev, int ring)
    657 {
    658 	uint64_t seq[RADEON_NUM_RINGS] = {};
    659 	long r;
    660 
    661 	seq[ring] = atomic64_read(&rdev->fence_drv[ring].last_seq) + 1ULL;
    662 	if (seq[ring] >= rdev->fence_drv[ring].sync_seq[ring]) {
    663 		/* nothing to wait for, last_seq is
    664 		   already the last emited fence */
    665 		return -ENOENT;
    666 	}
    667 	r = radeon_fence_wait_seq_timeout(rdev, seq, false, MAX_SCHEDULE_TIMEOUT);
    668 	if (r < 0)
    669 		return r;
    670 	return 0;
    671 }
    672 
    673 /**
    674  * radeon_fence_wait_empty - wait for all fences to signal
    675  *
    676  * @rdev: radeon device pointer
    677  * @ring: ring index the fence is associated with
    678  *
    679  * Wait for all fences on the requested ring to signal (all asics).
    680  * Returns 0 if the fences have passed, error for all other cases.
    681  * Caller must hold ring lock.
    682  */
    683 int radeon_fence_wait_empty(struct radeon_device *rdev, int ring)
    684 {
    685 	uint64_t seq[RADEON_NUM_RINGS] = {};
    686 	long r;
    687 
    688 	seq[ring] = rdev->fence_drv[ring].sync_seq[ring];
    689 	if (!seq[ring])
    690 		return 0;
    691 
    692 	r = radeon_fence_wait_seq_timeout(rdev, seq, false, MAX_SCHEDULE_TIMEOUT);
    693 	if (r < 0) {
    694 		if (r == -EDEADLK)
    695 			return -EDEADLK;
    696 
    697 		dev_err(rdev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
    698 			ring, r);
    699 	}
    700 	return 0;
    701 }
    702 
    703 /**
    704  * radeon_fence_ref - take a ref on a fence
    705  *
    706  * @fence: radeon fence object
    707  *
    708  * Take a reference on a fence (all asics).
    709  * Returns the fence.
    710  */
    711 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
    712 {
    713 	dma_fence_get(&fence->base);
    714 	return fence;
    715 }
    716 
    717 /**
    718  * radeon_fence_unref - remove a ref on a fence
    719  *
    720  * @fence: radeon fence object
    721  *
    722  * Remove a reference on a fence (all asics).
    723  */
    724 void radeon_fence_unref(struct radeon_fence **fence)
    725 {
    726 	struct radeon_fence *tmp = *fence;
    727 
    728 	*fence = NULL;
    729 	if (tmp) {
    730 		dma_fence_put(&tmp->base);
    731 	}
    732 }
    733 
    734 /**
    735  * radeon_fence_count_emitted - get the count of emitted fences
    736  *
    737  * @rdev: radeon device pointer
    738  * @ring: ring index the fence is associated with
    739  *
    740  * Get the number of fences emitted on the requested ring (all asics).
    741  * Returns the number of emitted fences on the ring.  Used by the
    742  * dynpm code to ring track activity.
    743  */
    744 unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
    745 {
    746 	uint64_t emitted;
    747 
    748 	/* We are not protected by ring lock when reading the last sequence
    749 	 * but it's ok to report slightly wrong fence count here.
    750 	 */
    751 	radeon_fence_process(rdev, ring);
    752 	emitted = rdev->fence_drv[ring].sync_seq[ring]
    753 		- atomic64_read(&rdev->fence_drv[ring].last_seq);
    754 	/* to avoid 32bits warp around */
    755 	if (emitted > 0x10000000) {
    756 		emitted = 0x10000000;
    757 	}
    758 	return (unsigned)emitted;
    759 }
    760 
    761 /**
    762  * radeon_fence_need_sync - do we need a semaphore
    763  *
    764  * @fence: radeon fence object
    765  * @dst_ring: which ring to check against
    766  *
    767  * Check if the fence needs to be synced against another ring
    768  * (all asics).  If so, we need to emit a semaphore.
    769  * Returns true if we need to sync with another ring, false if
    770  * not.
    771  */
    772 bool radeon_fence_need_sync(struct radeon_fence *fence, int dst_ring)
    773 {
    774 	struct radeon_fence_driver *fdrv;
    775 
    776 	if (!fence) {
    777 		return false;
    778 	}
    779 
    780 	if (fence->ring == dst_ring) {
    781 		return false;
    782 	}
    783 
    784 	/* we are protected by the ring mutex */
    785 	fdrv = &fence->rdev->fence_drv[dst_ring];
    786 	if (fence->seq <= fdrv->sync_seq[fence->ring]) {
    787 		return false;
    788 	}
    789 
    790 	return true;
    791 }
    792 
    793 /**
    794  * radeon_fence_note_sync - record the sync point
    795  *
    796  * @fence: radeon fence object
    797  * @dst_ring: which ring to check against
    798  *
    799  * Note the sequence number at which point the fence will
    800  * be synced with the requested ring (all asics).
    801  */
    802 void radeon_fence_note_sync(struct radeon_fence *fence, int dst_ring)
    803 {
    804 	struct radeon_fence_driver *dst, *src;
    805 	unsigned i;
    806 
    807 	if (!fence) {
    808 		return;
    809 	}
    810 
    811 	if (fence->ring == dst_ring) {
    812 		return;
    813 	}
    814 
    815 	/* we are protected by the ring mutex */
    816 	src = &fence->rdev->fence_drv[fence->ring];
    817 	dst = &fence->rdev->fence_drv[dst_ring];
    818 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    819 		if (i == dst_ring) {
    820 			continue;
    821 		}
    822 		dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
    823 	}
    824 }
    825 
    826 /**
    827  * radeon_fence_driver_start_ring - make the fence driver
    828  * ready for use on the requested ring.
    829  *
    830  * @rdev: radeon device pointer
    831  * @ring: ring index to start the fence driver on
    832  *
    833  * Make the fence driver ready for processing (all asics).
    834  * Not all asics have all rings, so each asic will only
    835  * start the fence driver on the rings it has.
    836  * Returns 0 for success, errors for failure.
    837  */
    838 int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
    839 {
    840 	uint64_t index;
    841 	int r;
    842 
    843 	radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
    844 	if (rdev->wb.use_event || !radeon_ring_supports_scratch_reg(rdev, &rdev->ring[ring])) {
    845 		rdev->fence_drv[ring].scratch_reg = 0;
    846 		if (ring != R600_RING_TYPE_UVD_INDEX) {
    847 			index = R600_WB_EVENT_OFFSET + ring * 4;
    848 			rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
    849 			rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr +
    850 							 index;
    851 
    852 		} else {
    853 			/* put fence directly behind firmware */
    854 			index = ALIGN(rdev->uvd_fw->size, 8);
    855 			rdev->fence_drv[ring].cpu_addr = rdev->uvd.cpu_addr + index;
    856 			rdev->fence_drv[ring].gpu_addr = rdev->uvd.gpu_addr + index;
    857 		}
    858 
    859 	} else {
    860 		r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
    861 		if (r) {
    862 			dev_err(rdev->dev, "fence failed to get scratch register\n");
    863 			return r;
    864 		}
    865 		index = RADEON_WB_SCRATCH_OFFSET +
    866 			rdev->fence_drv[ring].scratch_reg -
    867 			rdev->scratch.reg_base;
    868 		rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
    869 		rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
    870 	}
    871 	radeon_fence_write(rdev, atomic64_read(&rdev->fence_drv[ring].last_seq), ring);
    872 	rdev->fence_drv[ring].initialized = true;
    873 	dev_info(rdev->dev, "fence driver on ring %d use gpu addr 0x%016llx and cpu addr 0x%p\n",
    874 		 ring, rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
    875 	return 0;
    876 }
    877 
    878 /**
    879  * radeon_fence_driver_init_ring - init the fence driver
    880  * for the requested ring.
    881  *
    882  * @rdev: radeon device pointer
    883  * @ring: ring index to start the fence driver on
    884  *
    885  * Init the fence driver for the requested ring (all asics).
    886  * Helper function for radeon_fence_driver_init().
    887  */
    888 static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
    889 {
    890 	int i;
    891 
    892 	rdev->fence_drv[ring].scratch_reg = -1;
    893 	rdev->fence_drv[ring].cpu_addr = NULL;
    894 	rdev->fence_drv[ring].gpu_addr = 0;
    895 	for (i = 0; i < RADEON_NUM_RINGS; ++i)
    896 		rdev->fence_drv[ring].sync_seq[i] = 0;
    897 	atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
    898 	rdev->fence_drv[ring].initialized = false;
    899 	INIT_DELAYED_WORK(&rdev->fence_drv[ring].lockup_work,
    900 			  radeon_fence_check_lockup);
    901 	rdev->fence_drv[ring].rdev = rdev;
    902 }
    903 
    904 /**
    905  * radeon_fence_driver_init - init the fence driver
    906  * for all possible rings.
    907  *
    908  * @rdev: radeon device pointer
    909  *
    910  * Init the fence driver for all possible rings (all asics).
    911  * Not all asics have all rings, so each asic will only
    912  * start the fence driver on the rings it has using
    913  * radeon_fence_driver_start_ring().
    914  * Returns 0 for success.
    915  */
    916 int radeon_fence_driver_init(struct radeon_device *rdev)
    917 {
    918 	int ring;
    919 
    920 	init_waitqueue_head(&rdev->fence_queue);
    921 	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
    922 		radeon_fence_driver_init_ring(rdev, ring);
    923 	}
    924 	if (radeon_debugfs_fence_init(rdev)) {
    925 		dev_err(rdev->dev, "fence debugfs file creation failed\n");
    926 	}
    927 	return 0;
    928 }
    929 
    930 /**
    931  * radeon_fence_driver_fini - tear down the fence driver
    932  * for all possible rings.
    933  *
    934  * @rdev: radeon device pointer
    935  *
    936  * Tear down the fence driver for all possible rings (all asics).
    937  */
    938 void radeon_fence_driver_fini(struct radeon_device *rdev)
    939 {
    940 	int ring, r;
    941 
    942 	mutex_lock(&rdev->ring_lock);
    943 	for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
    944 		if (!rdev->fence_drv[ring].initialized)
    945 			continue;
    946 		r = radeon_fence_wait_empty(rdev, ring);
    947 		if (r) {
    948 			/* no need to trigger GPU reset as we are unloading */
    949 			radeon_fence_driver_force_completion(rdev, ring);
    950 		}
    951 		cancel_delayed_work_sync(&rdev->fence_drv[ring].lockup_work);
    952 		wake_up_all(&rdev->fence_queue);
    953 		radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
    954 		rdev->fence_drv[ring].initialized = false;
    955 	}
    956 	mutex_unlock(&rdev->ring_lock);
    957 }
    958 
    959 /**
    960  * radeon_fence_driver_force_completion - force all fence waiter to complete
    961  *
    962  * @rdev: radeon device pointer
    963  * @ring: the ring to complete
    964  *
    965  * In case of GPU reset failure make sure no process keep waiting on fence
    966  * that will never complete.
    967  */
    968 void radeon_fence_driver_force_completion(struct radeon_device *rdev, int ring)
    969 {
    970 	if (rdev->fence_drv[ring].initialized) {
    971 		radeon_fence_write(rdev, rdev->fence_drv[ring].sync_seq[ring], ring);
    972 		cancel_delayed_work_sync(&rdev->fence_drv[ring].lockup_work);
    973 	}
    974 }
    975 
    976 
    977 /*
    978  * Fence debugfs
    979  */
    980 #if defined(CONFIG_DEBUG_FS)
    981 static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
    982 {
    983 	struct drm_info_node *node = (struct drm_info_node *)m->private;
    984 	struct drm_device *dev = node->minor->dev;
    985 	struct radeon_device *rdev = dev->dev_private;
    986 	int i, j;
    987 
    988 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
    989 		if (!rdev->fence_drv[i].initialized)
    990 			continue;
    991 
    992 		radeon_fence_process(rdev, i);
    993 
    994 		seq_printf(m, "--- ring %d ---\n", i);
    995 		seq_printf(m, "Last signaled fence 0x%016llx\n",
    996 			   (unsigned long long)atomic64_read(&rdev->fence_drv[i].last_seq));
    997 		seq_printf(m, "Last emitted        0x%016llx\n",
    998 			   rdev->fence_drv[i].sync_seq[i]);
    999 
   1000 		for (j = 0; j < RADEON_NUM_RINGS; ++j) {
   1001 			if (i != j && rdev->fence_drv[j].initialized)
   1002 				seq_printf(m, "Last sync to ring %d 0x%016llx\n",
   1003 					   j, rdev->fence_drv[i].sync_seq[j]);
   1004 		}
   1005 	}
   1006 	return 0;
   1007 }
   1008 
   1009 /**
   1010  * radeon_debugfs_gpu_reset - manually trigger a gpu reset
   1011  *
   1012  * Manually trigger a gpu reset at the next fence wait.
   1013  */
   1014 static int radeon_debugfs_gpu_reset(struct seq_file *m, void *data)
   1015 {
   1016 	struct drm_info_node *node = (struct drm_info_node *) m->private;
   1017 	struct drm_device *dev = node->minor->dev;
   1018 	struct radeon_device *rdev = dev->dev_private;
   1019 
   1020 	down_read(&rdev->exclusive_lock);
   1021 	seq_printf(m, "%d\n", rdev->needs_reset);
   1022 	rdev->needs_reset = true;
   1023 	wake_up_all(&rdev->fence_queue);
   1024 	up_read(&rdev->exclusive_lock);
   1025 
   1026 	return 0;
   1027 }
   1028 
   1029 static struct drm_info_list radeon_debugfs_fence_list[] = {
   1030 	{"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
   1031 	{"radeon_gpu_reset", &radeon_debugfs_gpu_reset, 0, NULL}
   1032 };
   1033 #endif
   1034 
   1035 int radeon_debugfs_fence_init(struct radeon_device *rdev)
   1036 {
   1037 #if defined(CONFIG_DEBUG_FS)
   1038 	return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 2);
   1039 #else
   1040 	return 0;
   1041 #endif
   1042 }
   1043 
   1044 static const char *radeon_fence_get_driver_name(struct dma_fence *fence)
   1045 {
   1046 	return "radeon";
   1047 }
   1048 
   1049 static const char *radeon_fence_get_timeline_name(struct dma_fence *f)
   1050 {
   1051 	struct radeon_fence *fence = to_radeon_fence(f);
   1052 	switch (fence->ring) {
   1053 	case RADEON_RING_TYPE_GFX_INDEX: return "radeon.gfx";
   1054 	case CAYMAN_RING_TYPE_CP1_INDEX: return "radeon.cp1";
   1055 	case CAYMAN_RING_TYPE_CP2_INDEX: return "radeon.cp2";
   1056 	case R600_RING_TYPE_DMA_INDEX: return "radeon.dma";
   1057 	case CAYMAN_RING_TYPE_DMA1_INDEX: return "radeon.dma1";
   1058 	case R600_RING_TYPE_UVD_INDEX: return "radeon.uvd";
   1059 	case TN_RING_TYPE_VCE1_INDEX: return "radeon.vce1";
   1060 	case TN_RING_TYPE_VCE2_INDEX: return "radeon.vce2";
   1061 	default: WARN_ON_ONCE(1); return "radeon.unk";
   1062 	}
   1063 }
   1064 
   1065 static inline bool radeon_test_signaled(struct radeon_fence *fence)
   1066 {
   1067 	return test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
   1068 }
   1069 
   1070 struct radeon_wait_cb {
   1071 	struct dma_fence_cb base;
   1072 	struct task_struct *task;
   1073 };
   1074 
   1075 static void
   1076 radeon_fence_wait_cb(struct dma_fence *fence, struct dma_fence_cb *cb)
   1077 {
   1078 	struct radeon_wait_cb *wait =
   1079 		container_of(cb, struct radeon_wait_cb, base);
   1080 
   1081 	wake_up_process(wait->task);
   1082 }
   1083 
   1084 static signed long radeon_fence_default_wait(struct dma_fence *f, bool intr,
   1085 					     signed long t)
   1086 {
   1087 	struct radeon_fence *fence = to_radeon_fence(f);
   1088 	struct radeon_device *rdev = fence->rdev;
   1089 	struct radeon_wait_cb cb;
   1090 
   1091 	cb.task = current;
   1092 
   1093 	if (dma_fence_add_callback(f, &cb.base, radeon_fence_wait_cb))
   1094 		return t;
   1095 
   1096 	while (t > 0) {
   1097 		if (intr)
   1098 			set_current_state(TASK_INTERRUPTIBLE);
   1099 		else
   1100 			set_current_state(TASK_UNINTERRUPTIBLE);
   1101 
   1102 		/*
   1103 		 * radeon_test_signaled must be called after
   1104 		 * set_current_state to prevent a race with wake_up_process
   1105 		 */
   1106 		if (radeon_test_signaled(fence))
   1107 			break;
   1108 
   1109 		if (rdev->needs_reset) {
   1110 			t = -EDEADLK;
   1111 			break;
   1112 		}
   1113 
   1114 		t = schedule_timeout(t);
   1115 
   1116 		if (t > 0 && intr && signal_pending(current))
   1117 			t = -ERESTARTSYS;
   1118 	}
   1119 
   1120 	__set_current_state(TASK_RUNNING);
   1121 	dma_fence_remove_callback(f, &cb.base);
   1122 
   1123 	return t;
   1124 }
   1125 
   1126 const struct dma_fence_ops radeon_fence_ops = {
   1127 	.get_driver_name = radeon_fence_get_driver_name,
   1128 	.get_timeline_name = radeon_fence_get_timeline_name,
   1129 	.enable_signaling = radeon_fence_enable_signaling,
   1130 	.signaled = radeon_fence_is_signaled,
   1131 	.wait = radeon_fence_default_wait,
   1132 	.release = NULL,
   1133 };
   1134