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