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