Home | History | Annotate | Line # | Download | only in amdgpu
      1  1.7  riastrad /*	$NetBSD: amdgpu_ring.c,v 1.7 2021/12/19 12:31:45 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2008 Advanced Micro Devices, Inc.
      5  1.1  riastrad  * Copyright 2008 Red Hat Inc.
      6  1.1  riastrad  * Copyright 2009 Jerome Glisse.
      7  1.1  riastrad  *
      8  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      9  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     10  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     11  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     13  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     16  1.1  riastrad  * all copies or substantial portions of the Software.
     17  1.1  riastrad  *
     18  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     22  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     23  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     24  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     25  1.1  riastrad  *
     26  1.1  riastrad  * Authors: Dave Airlie
     27  1.1  riastrad  *          Alex Deucher
     28  1.1  riastrad  *          Jerome Glisse
     29  1.1  riastrad  *          Christian Knig
     30  1.1  riastrad  */
     31  1.1  riastrad #include <sys/cdefs.h>
     32  1.7  riastrad __KERNEL_RCSID(0, "$NetBSD: amdgpu_ring.c,v 1.7 2021/12/19 12:31:45 riastradh Exp $");
     33  1.1  riastrad 
     34  1.1  riastrad #include <linux/seq_file.h>
     35  1.1  riastrad #include <linux/slab.h>
     36  1.5  riastrad #include <linux/uaccess.h>
     37  1.5  riastrad #include <linux/debugfs.h>
     38  1.5  riastrad 
     39  1.1  riastrad #include <drm/amdgpu_drm.h>
     40  1.1  riastrad #include "amdgpu.h"
     41  1.1  riastrad #include "atom.h"
     42  1.1  riastrad 
     43  1.6  riastrad #include <linux/nbsd-namespace.h>
     44  1.6  riastrad 
     45  1.1  riastrad /*
     46  1.1  riastrad  * Rings
     47  1.1  riastrad  * Most engines on the GPU are fed via ring buffers.  Ring
     48  1.1  riastrad  * buffers are areas of GPU accessible memory that the host
     49  1.1  riastrad  * writes commands into and the GPU reads commands out of.
     50  1.1  riastrad  * There is a rptr (read pointer) that determines where the
     51  1.1  riastrad  * GPU is currently reading, and a wptr (write pointer)
     52  1.1  riastrad  * which determines where the host has written.  When the
     53  1.1  riastrad  * pointers are equal, the ring is idle.  When the host
     54  1.1  riastrad  * writes commands to the ring buffer, it increments the
     55  1.1  riastrad  * wptr.  The GPU then starts fetching commands and executes
     56  1.1  riastrad  * them until the pointers are equal again.
     57  1.1  riastrad  */
     58  1.5  riastrad static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
     59  1.5  riastrad 				    struct amdgpu_ring *ring);
     60  1.5  riastrad static void amdgpu_debugfs_ring_fini(struct amdgpu_ring *ring);
     61  1.1  riastrad 
     62  1.1  riastrad /**
     63  1.1  riastrad  * amdgpu_ring_alloc - allocate space on the ring buffer
     64  1.1  riastrad  *
     65  1.1  riastrad  * @adev: amdgpu_device pointer
     66  1.1  riastrad  * @ring: amdgpu_ring structure holding ring information
     67  1.1  riastrad  * @ndw: number of dwords to allocate in the ring buffer
     68  1.1  riastrad  *
     69  1.1  riastrad  * Allocate @ndw dwords in the ring buffer (all asics).
     70  1.1  riastrad  * Returns 0 on success, error on failure.
     71  1.1  riastrad  */
     72  1.1  riastrad int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw)
     73  1.1  riastrad {
     74  1.5  riastrad 	/* Align requested size with padding so unlock_commit can
     75  1.5  riastrad 	 * pad safely */
     76  1.5  riastrad 	ndw = (ndw + ring->funcs->align_mask) & ~ring->funcs->align_mask;
     77  1.1  riastrad 
     78  1.5  riastrad 	/* Make sure we aren't trying to allocate more space
     79  1.5  riastrad 	 * than the maximum for one submission
     80  1.5  riastrad 	 */
     81  1.5  riastrad 	if (WARN_ON_ONCE(ndw > ring->max_dw))
     82  1.1  riastrad 		return -ENOMEM;
     83  1.5  riastrad 
     84  1.1  riastrad 	ring->count_dw = ndw;
     85  1.1  riastrad 	ring->wptr_old = ring->wptr;
     86  1.1  riastrad 
     87  1.5  riastrad 	if (ring->funcs->begin_use)
     88  1.5  riastrad 		ring->funcs->begin_use(ring);
     89  1.1  riastrad 
     90  1.1  riastrad 	return 0;
     91  1.1  riastrad }
     92  1.1  riastrad 
     93  1.1  riastrad /** amdgpu_ring_insert_nop - insert NOP packets
     94  1.1  riastrad  *
     95  1.1  riastrad  * @ring: amdgpu_ring structure holding ring information
     96  1.1  riastrad  * @count: the number of NOP packets to insert
     97  1.1  riastrad  *
     98  1.1  riastrad  * This is the generic insert_nop function for rings except SDMA
     99  1.1  riastrad  */
    100  1.1  riastrad void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
    101  1.1  riastrad {
    102  1.1  riastrad 	int i;
    103  1.1  riastrad 
    104  1.1  riastrad 	for (i = 0; i < count; i++)
    105  1.5  riastrad 		amdgpu_ring_write(ring, ring->funcs->nop);
    106  1.5  riastrad }
    107  1.5  riastrad 
    108  1.5  riastrad /** amdgpu_ring_generic_pad_ib - pad IB with NOP packets
    109  1.5  riastrad  *
    110  1.5  riastrad  * @ring: amdgpu_ring structure holding ring information
    111  1.5  riastrad  * @ib: IB to add NOP packets to
    112  1.5  riastrad  *
    113  1.5  riastrad  * This is the generic pad_ib function for rings except SDMA
    114  1.5  riastrad  */
    115  1.5  riastrad void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
    116  1.5  riastrad {
    117  1.5  riastrad 	while (ib->length_dw & ring->funcs->align_mask)
    118  1.5  riastrad 		ib->ptr[ib->length_dw++] = ring->funcs->nop;
    119  1.1  riastrad }
    120  1.1  riastrad 
    121  1.1  riastrad /**
    122  1.1  riastrad  * amdgpu_ring_commit - tell the GPU to execute the new
    123  1.1  riastrad  * commands on the ring buffer
    124  1.1  riastrad  *
    125  1.1  riastrad  * @adev: amdgpu_device pointer
    126  1.1  riastrad  * @ring: amdgpu_ring structure holding ring information
    127  1.1  riastrad  *
    128  1.1  riastrad  * Update the wptr (write pointer) to tell the GPU to
    129  1.1  riastrad  * execute new commands on the ring buffer (all asics).
    130  1.1  riastrad  */
    131  1.1  riastrad void amdgpu_ring_commit(struct amdgpu_ring *ring)
    132  1.1  riastrad {
    133  1.1  riastrad 	uint32_t count;
    134  1.1  riastrad 
    135  1.1  riastrad 	/* We pad to match fetch size */
    136  1.5  riastrad 	count = ring->funcs->align_mask + 1 -
    137  1.5  riastrad 		(ring->wptr & ring->funcs->align_mask);
    138  1.5  riastrad 	count %= ring->funcs->align_mask + 1;
    139  1.1  riastrad 	ring->funcs->insert_nop(ring, count);
    140  1.1  riastrad 
    141  1.1  riastrad 	mb();
    142  1.1  riastrad 	amdgpu_ring_set_wptr(ring);
    143  1.1  riastrad 
    144  1.5  riastrad 	if (ring->funcs->end_use)
    145  1.5  riastrad 		ring->funcs->end_use(ring);
    146  1.1  riastrad }
    147  1.1  riastrad 
    148  1.1  riastrad /**
    149  1.1  riastrad  * amdgpu_ring_undo - reset the wptr
    150  1.1  riastrad  *
    151  1.1  riastrad  * @ring: amdgpu_ring structure holding ring information
    152  1.1  riastrad  *
    153  1.1  riastrad  * Reset the driver's copy of the wptr (all asics).
    154  1.1  riastrad  */
    155  1.1  riastrad void amdgpu_ring_undo(struct amdgpu_ring *ring)
    156  1.1  riastrad {
    157  1.1  riastrad 	ring->wptr = ring->wptr_old;
    158  1.1  riastrad 
    159  1.5  riastrad 	if (ring->funcs->end_use)
    160  1.5  riastrad 		ring->funcs->end_use(ring);
    161  1.1  riastrad }
    162  1.1  riastrad 
    163  1.1  riastrad /**
    164  1.5  riastrad  * amdgpu_ring_priority_put - restore a ring's priority
    165  1.1  riastrad  *
    166  1.5  riastrad  * @ring: amdgpu_ring structure holding the information
    167  1.5  riastrad  * @priority: target priority
    168  1.1  riastrad  *
    169  1.5  riastrad  * Release a request for executing at @priority
    170  1.1  riastrad  */
    171  1.5  riastrad void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
    172  1.5  riastrad 			      enum drm_sched_priority priority)
    173  1.1  riastrad {
    174  1.5  riastrad 	int i;
    175  1.1  riastrad 
    176  1.5  riastrad 	if (!ring->funcs->set_priority)
    177  1.5  riastrad 		return;
    178  1.1  riastrad 
    179  1.5  riastrad 	if (atomic_dec_return(&ring->num_jobs[priority]) > 0)
    180  1.5  riastrad 		return;
    181  1.1  riastrad 
    182  1.5  riastrad 	/* no need to restore if the job is already at the lowest priority */
    183  1.5  riastrad 	if (priority == DRM_SCHED_PRIORITY_NORMAL)
    184  1.5  riastrad 		return;
    185  1.1  riastrad 
    186  1.5  riastrad 	mutex_lock(&ring->priority_mutex);
    187  1.5  riastrad 	/* something higher prio is executing, no need to decay */
    188  1.5  riastrad 	if (ring->priority > priority)
    189  1.5  riastrad 		goto out_unlock;
    190  1.5  riastrad 
    191  1.5  riastrad 	/* decay priority to the next level with a job available */
    192  1.5  riastrad 	for (i = priority; i >= DRM_SCHED_PRIORITY_MIN; i--) {
    193  1.5  riastrad 		if (i == DRM_SCHED_PRIORITY_NORMAL
    194  1.5  riastrad 				|| atomic_read(&ring->num_jobs[i])) {
    195  1.5  riastrad 			ring->priority = i;
    196  1.5  riastrad 			ring->funcs->set_priority(ring, i);
    197  1.5  riastrad 			break;
    198  1.5  riastrad 		}
    199  1.1  riastrad 	}
    200  1.1  riastrad 
    201  1.5  riastrad out_unlock:
    202  1.5  riastrad 	mutex_unlock(&ring->priority_mutex);
    203  1.1  riastrad }
    204  1.1  riastrad 
    205  1.1  riastrad /**
    206  1.5  riastrad  * amdgpu_ring_priority_get - change the ring's priority
    207  1.1  riastrad  *
    208  1.5  riastrad  * @ring: amdgpu_ring structure holding the information
    209  1.5  riastrad  * @priority: target priority
    210  1.1  riastrad  *
    211  1.5  riastrad  * Request a ring's priority to be raised to @priority (refcounted).
    212  1.1  riastrad  */
    213  1.5  riastrad void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
    214  1.5  riastrad 			      enum drm_sched_priority priority)
    215  1.1  riastrad {
    216  1.5  riastrad 	if (!ring->funcs->set_priority)
    217  1.5  riastrad 		return;
    218  1.1  riastrad 
    219  1.5  riastrad 	if (atomic_inc_return(&ring->num_jobs[priority]) <= 0)
    220  1.5  riastrad 		return;
    221  1.1  riastrad 
    222  1.5  riastrad 	mutex_lock(&ring->priority_mutex);
    223  1.5  riastrad 	if (priority <= ring->priority)
    224  1.5  riastrad 		goto out_unlock;
    225  1.1  riastrad 
    226  1.5  riastrad 	ring->priority = priority;
    227  1.5  riastrad 	ring->funcs->set_priority(ring, priority);
    228  1.1  riastrad 
    229  1.5  riastrad out_unlock:
    230  1.5  riastrad 	mutex_unlock(&ring->priority_mutex);
    231  1.1  riastrad }
    232  1.1  riastrad 
    233  1.1  riastrad /**
    234  1.1  riastrad  * amdgpu_ring_init - init driver ring struct.
    235  1.1  riastrad  *
    236  1.1  riastrad  * @adev: amdgpu_device pointer
    237  1.1  riastrad  * @ring: amdgpu_ring structure holding ring information
    238  1.5  riastrad  * @max_ndw: maximum number of dw for ring alloc
    239  1.1  riastrad  * @nop: nop packet for this ring
    240  1.1  riastrad  *
    241  1.1  riastrad  * Initialize the driver information for the selected ring (all asics).
    242  1.1  riastrad  * Returns 0 on success, error on failure.
    243  1.1  riastrad  */
    244  1.1  riastrad int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
    245  1.5  riastrad 		     unsigned max_dw, struct amdgpu_irq_src *irq_src,
    246  1.5  riastrad 		     unsigned irq_type)
    247  1.1  riastrad {
    248  1.5  riastrad 	int r, i;
    249  1.5  riastrad 	int sched_hw_submission = amdgpu_sched_hw_submission;
    250  1.5  riastrad 
    251  1.5  riastrad 	/* Set the hw submission limit higher for KIQ because
    252  1.5  riastrad 	 * it's used for a number of gfx/compute tasks by both
    253  1.5  riastrad 	 * KFD and KGD which may have outstanding fences and
    254  1.5  riastrad 	 * it doesn't really use the gpu scheduler anyway;
    255  1.5  riastrad 	 * KIQ tasks get submitted directly to the ring.
    256  1.5  riastrad 	 */
    257  1.5  riastrad 	if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
    258  1.5  riastrad 		sched_hw_submission = max(sched_hw_submission, 256);
    259  1.5  riastrad 	else if (ring == &adev->sdma.instance[0].page)
    260  1.5  riastrad 		sched_hw_submission = 256;
    261  1.1  riastrad 
    262  1.1  riastrad 	if (ring->adev == NULL) {
    263  1.1  riastrad 		if (adev->num_rings >= AMDGPU_MAX_RINGS)
    264  1.1  riastrad 			return -EINVAL;
    265  1.1  riastrad 
    266  1.1  riastrad 		ring->adev = adev;
    267  1.1  riastrad 		ring->idx = adev->num_rings++;
    268  1.1  riastrad 		adev->rings[ring->idx] = ring;
    269  1.5  riastrad 		r = amdgpu_fence_driver_init_ring(ring, sched_hw_submission);
    270  1.1  riastrad 		if (r)
    271  1.1  riastrad 			return r;
    272  1.1  riastrad 	}
    273  1.1  riastrad 
    274  1.5  riastrad 	r = amdgpu_device_wb_get(adev, &ring->rptr_offs);
    275  1.1  riastrad 	if (r) {
    276  1.1  riastrad 		dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
    277  1.1  riastrad 		return r;
    278  1.1  riastrad 	}
    279  1.1  riastrad 
    280  1.5  riastrad 	r = amdgpu_device_wb_get(adev, &ring->wptr_offs);
    281  1.1  riastrad 	if (r) {
    282  1.1  riastrad 		dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
    283  1.1  riastrad 		return r;
    284  1.1  riastrad 	}
    285  1.1  riastrad 
    286  1.5  riastrad 	r = amdgpu_device_wb_get(adev, &ring->fence_offs);
    287  1.1  riastrad 	if (r) {
    288  1.1  riastrad 		dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
    289  1.1  riastrad 		return r;
    290  1.1  riastrad 	}
    291  1.1  riastrad 
    292  1.5  riastrad 	r = amdgpu_device_wb_get(adev, &ring->trail_fence_offs);
    293  1.5  riastrad 	if (r) {
    294  1.5  riastrad 		dev_err(adev->dev,
    295  1.5  riastrad 			"(%d) ring trail_fence_offs wb alloc failed\n", r);
    296  1.5  riastrad 		return r;
    297  1.5  riastrad 	}
    298  1.5  riastrad 	ring->trail_fence_gpu_addr =
    299  1.5  riastrad 		adev->wb.gpu_addr + (ring->trail_fence_offs * 4);
    300  1.5  riastrad 	ring->trail_fence_cpu_addr = &adev->wb.wb[ring->trail_fence_offs];
    301  1.5  riastrad 
    302  1.5  riastrad 	r = amdgpu_device_wb_get(adev, &ring->cond_exe_offs);
    303  1.1  riastrad 	if (r) {
    304  1.5  riastrad 		dev_err(adev->dev, "(%d) ring cond_exec_polling wb alloc failed\n", r);
    305  1.1  riastrad 		return r;
    306  1.1  riastrad 	}
    307  1.5  riastrad 	ring->cond_exe_gpu_addr = adev->wb.gpu_addr + (ring->cond_exe_offs * 4);
    308  1.5  riastrad 	ring->cond_exe_cpu_addr = &adev->wb.wb[ring->cond_exe_offs];
    309  1.5  riastrad 	/* always set cond_exec_polling to CONTINUE */
    310  1.5  riastrad 	*ring->cond_exe_cpu_addr = 1;
    311  1.5  riastrad 
    312  1.1  riastrad 	r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
    313  1.1  riastrad 	if (r) {
    314  1.1  riastrad 		dev_err(adev->dev, "failed initializing fences (%d).\n", r);
    315  1.1  riastrad 		return r;
    316  1.1  riastrad 	}
    317  1.1  riastrad 
    318  1.5  riastrad 	ring->ring_size = roundup_pow_of_two(max_dw * 4 * sched_hw_submission);
    319  1.1  riastrad 
    320  1.5  riastrad 	ring->buf_mask = (ring->ring_size / 4) - 1;
    321  1.5  riastrad 	ring->ptr_mask = ring->funcs->support_64bit_ptrs ?
    322  1.5  riastrad 		0xffffffffffffffff : ring->buf_mask;
    323  1.1  riastrad 	/* Allocate ring buffer */
    324  1.1  riastrad 	if (ring->ring_obj == NULL) {
    325  1.5  riastrad 		r = amdgpu_bo_create_kernel(adev, ring->ring_size + ring->funcs->extra_dw, PAGE_SIZE,
    326  1.5  riastrad 					    AMDGPU_GEM_DOMAIN_GTT,
    327  1.5  riastrad 					    &ring->ring_obj,
    328  1.5  riastrad 					    &ring->gpu_addr,
    329  1.6  riastrad 					    (void **)__UNVOLATILE(&ring->ring));
    330  1.1  riastrad 		if (r) {
    331  1.1  riastrad 			dev_err(adev->dev, "(%d) ring create failed\n", r);
    332  1.1  riastrad 			return r;
    333  1.1  riastrad 		}
    334  1.5  riastrad 		amdgpu_ring_clear_ring(ring);
    335  1.1  riastrad 	}
    336  1.5  riastrad 
    337  1.5  riastrad 	ring->max_dw = max_dw;
    338  1.5  riastrad 	ring->priority = DRM_SCHED_PRIORITY_NORMAL;
    339  1.5  riastrad 	mutex_init(&ring->priority_mutex);
    340  1.5  riastrad 
    341  1.5  riastrad 	for (i = 0; i < DRM_SCHED_PRIORITY_MAX; ++i)
    342  1.5  riastrad 		atomic_set(&ring->num_jobs[i], 0);
    343  1.1  riastrad 
    344  1.1  riastrad 	if (amdgpu_debugfs_ring_init(adev, ring)) {
    345  1.1  riastrad 		DRM_ERROR("Failed to register debugfs file for rings !\n");
    346  1.1  riastrad 	}
    347  1.5  riastrad 
    348  1.1  riastrad 	return 0;
    349  1.1  riastrad }
    350  1.1  riastrad 
    351  1.1  riastrad /**
    352  1.1  riastrad  * amdgpu_ring_fini - tear down the driver ring struct.
    353  1.1  riastrad  *
    354  1.1  riastrad  * @adev: amdgpu_device pointer
    355  1.1  riastrad  * @ring: amdgpu_ring structure holding ring information
    356  1.1  riastrad  *
    357  1.1  riastrad  * Tear down the driver information for the selected ring (all asics).
    358  1.1  riastrad  */
    359  1.1  riastrad void amdgpu_ring_fini(struct amdgpu_ring *ring)
    360  1.1  riastrad {
    361  1.5  riastrad 	ring->sched.ready = false;
    362  1.1  riastrad 
    363  1.5  riastrad 	/* Not to finish a ring which is not initialized */
    364  1.5  riastrad 	if (!(ring->adev) || !(ring->adev->rings[ring->idx]))
    365  1.1  riastrad 		return;
    366  1.1  riastrad 
    367  1.5  riastrad 	amdgpu_device_wb_free(ring->adev, ring->rptr_offs);
    368  1.5  riastrad 	amdgpu_device_wb_free(ring->adev, ring->wptr_offs);
    369  1.5  riastrad 
    370  1.5  riastrad 	amdgpu_device_wb_free(ring->adev, ring->cond_exe_offs);
    371  1.5  riastrad 	amdgpu_device_wb_free(ring->adev, ring->fence_offs);
    372  1.5  riastrad 
    373  1.5  riastrad 	amdgpu_bo_free_kernel(&ring->ring_obj,
    374  1.5  riastrad 			      &ring->gpu_addr,
    375  1.6  riastrad 			      (void **)__UNVOLATILE(&ring->ring));
    376  1.5  riastrad 
    377  1.5  riastrad 	amdgpu_debugfs_ring_fini(ring);
    378  1.5  riastrad 
    379  1.5  riastrad 	dma_fence_put(ring->vmid_wait);
    380  1.5  riastrad 	ring->vmid_wait = NULL;
    381  1.5  riastrad 	ring->me = 0;
    382  1.5  riastrad 
    383  1.5  riastrad 	ring->adev->rings[ring->idx] = NULL;
    384  1.7  riastrad 
    385  1.7  riastrad 	mutex_destroy(&ring->priority_mutex);
    386  1.5  riastrad }
    387  1.5  riastrad 
    388  1.5  riastrad /**
    389  1.5  riastrad  * amdgpu_ring_emit_reg_write_reg_wait_helper - ring helper
    390  1.5  riastrad  *
    391  1.5  riastrad  * @adev: amdgpu_device pointer
    392  1.5  riastrad  * @reg0: register to write
    393  1.5  riastrad  * @reg1: register to wait on
    394  1.5  riastrad  * @ref: reference value to write/wait on
    395  1.5  riastrad  * @mask: mask to wait on
    396  1.5  riastrad  *
    397  1.5  riastrad  * Helper for rings that don't support write and wait in a
    398  1.5  riastrad  * single oneshot packet.
    399  1.5  riastrad  */
    400  1.5  riastrad void amdgpu_ring_emit_reg_write_reg_wait_helper(struct amdgpu_ring *ring,
    401  1.5  riastrad 						uint32_t reg0, uint32_t reg1,
    402  1.5  riastrad 						uint32_t ref, uint32_t mask)
    403  1.5  riastrad {
    404  1.5  riastrad 	amdgpu_ring_emit_wreg(ring, reg0, ref);
    405  1.5  riastrad 	amdgpu_ring_emit_reg_wait(ring, reg1, mask, mask);
    406  1.1  riastrad }
    407  1.1  riastrad 
    408  1.1  riastrad /**
    409  1.5  riastrad  * amdgpu_ring_soft_recovery - try to soft recover a ring lockup
    410  1.1  riastrad  *
    411  1.5  riastrad  * @ring: ring to try the recovery on
    412  1.5  riastrad  * @vmid: VMID we try to get going again
    413  1.5  riastrad  * @fence: timedout fence
    414  1.1  riastrad  *
    415  1.5  riastrad  * Tries to get a ring proceeding again when it is stuck.
    416  1.1  riastrad  */
    417  1.5  riastrad bool amdgpu_ring_soft_recovery(struct amdgpu_ring *ring, unsigned int vmid,
    418  1.5  riastrad 			       struct dma_fence *fence)
    419  1.1  riastrad {
    420  1.5  riastrad 	ktime_t deadline = ktime_add_us(ktime_get(), 10000);
    421  1.1  riastrad 
    422  1.5  riastrad 	if (amdgpu_sriov_vf(ring->adev) || !ring->funcs->soft_recovery || !fence)
    423  1.5  riastrad 		return false;
    424  1.1  riastrad 
    425  1.5  riastrad 	atomic_inc(&ring->adev->gpu_reset_counter);
    426  1.5  riastrad 	while (!dma_fence_is_signaled(fence) &&
    427  1.5  riastrad 	       ktime_to_ns(ktime_sub(deadline, ktime_get())) > 0)
    428  1.5  riastrad 		ring->funcs->soft_recovery(ring, vmid);
    429  1.1  riastrad 
    430  1.5  riastrad 	return dma_fence_is_signaled(fence);
    431  1.1  riastrad }
    432  1.1  riastrad 
    433  1.1  riastrad /*
    434  1.1  riastrad  * Debugfs info
    435  1.1  riastrad  */
    436  1.1  riastrad #if defined(CONFIG_DEBUG_FS)
    437  1.1  riastrad 
    438  1.5  riastrad /* Layout of file is 12 bytes consisting of
    439  1.5  riastrad  * - rptr
    440  1.5  riastrad  * - wptr
    441  1.5  riastrad  * - driver's copy of wptr
    442  1.5  riastrad  *
    443  1.5  riastrad  * followed by n-words of ring data
    444  1.5  riastrad  */
    445  1.5  riastrad static ssize_t amdgpu_debugfs_ring_read(struct file *f, char __user *buf,
    446  1.5  riastrad 					size_t size, loff_t *pos)
    447  1.5  riastrad {
    448  1.5  riastrad 	struct amdgpu_ring *ring = file_inode(f)->i_private;
    449  1.5  riastrad 	int r, i;
    450  1.5  riastrad 	uint32_t value, result, early[3];
    451  1.5  riastrad 
    452  1.5  riastrad 	if (*pos & 3 || size & 3)
    453  1.5  riastrad 		return -EINVAL;
    454  1.5  riastrad 
    455  1.5  riastrad 	result = 0;
    456  1.5  riastrad 
    457  1.5  riastrad 	if (*pos < 12) {
    458  1.5  riastrad 		early[0] = amdgpu_ring_get_rptr(ring) & ring->buf_mask;
    459  1.5  riastrad 		early[1] = amdgpu_ring_get_wptr(ring) & ring->buf_mask;
    460  1.5  riastrad 		early[2] = ring->wptr & ring->buf_mask;
    461  1.5  riastrad 		for (i = *pos / 4; i < 3 && size; i++) {
    462  1.5  riastrad 			r = put_user(early[i], (uint32_t *)buf);
    463  1.5  riastrad 			if (r)
    464  1.5  riastrad 				return r;
    465  1.5  riastrad 			buf += 4;
    466  1.5  riastrad 			result += 4;
    467  1.5  riastrad 			size -= 4;
    468  1.5  riastrad 			*pos += 4;
    469  1.5  riastrad 		}
    470  1.5  riastrad 	}
    471  1.1  riastrad 
    472  1.5  riastrad 	while (size) {
    473  1.5  riastrad 		if (*pos >= (ring->ring_size + 12))
    474  1.5  riastrad 			return result;
    475  1.1  riastrad 
    476  1.5  riastrad 		value = ring->ring[(*pos - 12)/4];
    477  1.5  riastrad 		r = put_user(value, (uint32_t*)buf);
    478  1.5  riastrad 		if (r)
    479  1.5  riastrad 			return r;
    480  1.5  riastrad 		buf += 4;
    481  1.5  riastrad 		result += 4;
    482  1.5  riastrad 		size -= 4;
    483  1.5  riastrad 		*pos += 4;
    484  1.1  riastrad 	}
    485  1.5  riastrad 
    486  1.5  riastrad 	return result;
    487  1.1  riastrad }
    488  1.1  riastrad 
    489  1.5  riastrad static const struct file_operations amdgpu_debugfs_ring_fops = {
    490  1.5  riastrad 	.owner = THIS_MODULE,
    491  1.5  riastrad 	.read = amdgpu_debugfs_ring_read,
    492  1.5  riastrad 	.llseek = default_llseek
    493  1.1  riastrad };
    494  1.1  riastrad 
    495  1.1  riastrad #endif
    496  1.1  riastrad 
    497  1.5  riastrad static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
    498  1.5  riastrad 				    struct amdgpu_ring *ring)
    499  1.1  riastrad {
    500  1.1  riastrad #if defined(CONFIG_DEBUG_FS)
    501  1.5  riastrad 	struct drm_minor *minor = adev->ddev->primary;
    502  1.5  riastrad 	struct dentry *ent, *root = minor->debugfs_root;
    503  1.5  riastrad 	char name[32];
    504  1.5  riastrad 
    505  1.5  riastrad 	sprintf(name, "amdgpu_ring_%s", ring->name);
    506  1.5  riastrad 
    507  1.5  riastrad 	ent = debugfs_create_file(name,
    508  1.5  riastrad 				  S_IFREG | S_IRUGO, root,
    509  1.5  riastrad 				  ring, &amdgpu_debugfs_ring_fops);
    510  1.5  riastrad 	if (!ent)
    511  1.5  riastrad 		return -ENOMEM;
    512  1.1  riastrad 
    513  1.5  riastrad 	i_size_write(ent->d_inode, ring->ring_size + 12);
    514  1.5  riastrad 	ring->ent = ent;
    515  1.5  riastrad #endif
    516  1.5  riastrad 	return 0;
    517  1.5  riastrad }
    518  1.1  riastrad 
    519  1.5  riastrad static void amdgpu_debugfs_ring_fini(struct amdgpu_ring *ring)
    520  1.5  riastrad {
    521  1.5  riastrad #if defined(CONFIG_DEBUG_FS)
    522  1.5  riastrad 	debugfs_remove(ring->ent);
    523  1.1  riastrad #endif
    524  1.5  riastrad }
    525  1.5  riastrad 
    526  1.5  riastrad /**
    527  1.5  riastrad  * amdgpu_ring_test_helper - tests ring and set sched readiness status
    528  1.5  riastrad  *
    529  1.5  riastrad  * @ring: ring to try the recovery on
    530  1.5  riastrad  *
    531  1.5  riastrad  * Tests ring and set sched readiness status
    532  1.5  riastrad  *
    533  1.5  riastrad  * Returns 0 on success, error on failure.
    534  1.5  riastrad  */
    535  1.5  riastrad int amdgpu_ring_test_helper(struct amdgpu_ring *ring)
    536  1.5  riastrad {
    537  1.5  riastrad 	struct amdgpu_device *adev = ring->adev;
    538  1.5  riastrad 	int r;
    539  1.5  riastrad 
    540  1.5  riastrad 	r = amdgpu_ring_test_ring(ring);
    541  1.5  riastrad 	if (r)
    542  1.5  riastrad 		DRM_DEV_ERROR(adev->dev, "ring %s test failed (%d)\n",
    543  1.5  riastrad 			      ring->name, r);
    544  1.5  riastrad 	else
    545  1.5  riastrad 		DRM_DEV_DEBUG(adev->dev, "ring test on %s succeeded\n",
    546  1.5  riastrad 			      ring->name);
    547  1.5  riastrad 
    548  1.5  riastrad 	ring->sched.ready = !r;
    549  1.5  riastrad 	return r;
    550  1.1  riastrad }
    551