Home | History | Annotate | Line # | Download | only in radeon
      1  1.5  riastrad /*	$NetBSD: radeon_ring.c,v 1.5 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.3  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.5  riastrad 
     32  1.3  riastrad #include <sys/cdefs.h>
     33  1.5  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_ring.c,v 1.5 2021/12/18 23:45:43 riastradh Exp $");
     34  1.5  riastrad 
     35  1.5  riastrad #include <drm/drm_debugfs.h>
     36  1.5  riastrad #include <drm/drm_device.h>
     37  1.5  riastrad #include <drm/drm_file.h>
     38  1.3  riastrad 
     39  1.1  riastrad #include "radeon.h"
     40  1.1  riastrad 
     41  1.1  riastrad /*
     42  1.1  riastrad  * Rings
     43  1.1  riastrad  * Most engines on the GPU are fed via ring buffers.  Ring
     44  1.1  riastrad  * buffers are areas of GPU accessible memory that the host
     45  1.1  riastrad  * writes commands into and the GPU reads commands out of.
     46  1.1  riastrad  * There is a rptr (read pointer) that determines where the
     47  1.1  riastrad  * GPU is currently reading, and a wptr (write pointer)
     48  1.1  riastrad  * which determines where the host has written.  When the
     49  1.1  riastrad  * pointers are equal, the ring is idle.  When the host
     50  1.1  riastrad  * writes commands to the ring buffer, it increments the
     51  1.1  riastrad  * wptr.  The GPU then starts fetching commands and executes
     52  1.1  riastrad  * them until the pointers are equal again.
     53  1.1  riastrad  */
     54  1.1  riastrad static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
     55  1.1  riastrad 
     56  1.1  riastrad /**
     57  1.1  riastrad  * radeon_ring_supports_scratch_reg - check if the ring supports
     58  1.1  riastrad  * writing to scratch registers
     59  1.1  riastrad  *
     60  1.1  riastrad  * @rdev: radeon_device pointer
     61  1.1  riastrad  * @ring: radeon_ring structure holding ring information
     62  1.1  riastrad  *
     63  1.1  riastrad  * Check if a specific ring supports writing to scratch registers (all asics).
     64  1.1  riastrad  * Returns true if the ring supports writing to scratch regs, false if not.
     65  1.1  riastrad  */
     66  1.1  riastrad bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
     67  1.1  riastrad 				      struct radeon_ring *ring)
     68  1.1  riastrad {
     69  1.1  riastrad 	switch (ring->idx) {
     70  1.1  riastrad 	case RADEON_RING_TYPE_GFX_INDEX:
     71  1.1  riastrad 	case CAYMAN_RING_TYPE_CP1_INDEX:
     72  1.1  riastrad 	case CAYMAN_RING_TYPE_CP2_INDEX:
     73  1.1  riastrad 		return true;
     74  1.1  riastrad 	default:
     75  1.1  riastrad 		return false;
     76  1.1  riastrad 	}
     77  1.1  riastrad }
     78  1.1  riastrad 
     79  1.1  riastrad /**
     80  1.1  riastrad  * radeon_ring_free_size - update the free size
     81  1.1  riastrad  *
     82  1.1  riastrad  * @rdev: radeon_device pointer
     83  1.1  riastrad  * @ring: radeon_ring structure holding ring information
     84  1.1  riastrad  *
     85  1.1  riastrad  * Update the free dw slots in the ring buffer (all asics).
     86  1.1  riastrad  */
     87  1.1  riastrad void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
     88  1.1  riastrad {
     89  1.1  riastrad 	uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
     90  1.1  riastrad 
     91  1.1  riastrad 	/* This works because ring_size is a power of 2 */
     92  1.1  riastrad 	ring->ring_free_dw = rptr + (ring->ring_size / 4);
     93  1.1  riastrad 	ring->ring_free_dw -= ring->wptr;
     94  1.1  riastrad 	ring->ring_free_dw &= ring->ptr_mask;
     95  1.1  riastrad 	if (!ring->ring_free_dw) {
     96  1.1  riastrad 		/* this is an empty ring */
     97  1.1  riastrad 		ring->ring_free_dw = ring->ring_size / 4;
     98  1.1  riastrad 		/*  update lockup info to avoid false positive */
     99  1.1  riastrad 		radeon_ring_lockup_update(rdev, ring);
    100  1.1  riastrad 	}
    101  1.1  riastrad }
    102  1.1  riastrad 
    103  1.1  riastrad /**
    104  1.1  riastrad  * radeon_ring_alloc - allocate space on the ring buffer
    105  1.1  riastrad  *
    106  1.1  riastrad  * @rdev: radeon_device pointer
    107  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    108  1.1  riastrad  * @ndw: number of dwords to allocate in the ring buffer
    109  1.1  riastrad  *
    110  1.1  riastrad  * Allocate @ndw dwords in the ring buffer (all asics).
    111  1.1  riastrad  * Returns 0 on success, error on failure.
    112  1.1  riastrad  */
    113  1.1  riastrad int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
    114  1.1  riastrad {
    115  1.1  riastrad 	int r;
    116  1.1  riastrad 
    117  1.1  riastrad 	/* make sure we aren't trying to allocate more space than there is on the ring */
    118  1.1  riastrad 	if (ndw > (ring->ring_size / 4))
    119  1.1  riastrad 		return -ENOMEM;
    120  1.1  riastrad 	/* Align requested size with padding so unlock_commit can
    121  1.1  riastrad 	 * pad safely */
    122  1.1  riastrad 	radeon_ring_free_size(rdev, ring);
    123  1.1  riastrad 	ndw = (ndw + ring->align_mask) & ~ring->align_mask;
    124  1.1  riastrad 	while (ndw > (ring->ring_free_dw - 1)) {
    125  1.1  riastrad 		radeon_ring_free_size(rdev, ring);
    126  1.1  riastrad 		if (ndw < ring->ring_free_dw) {
    127  1.1  riastrad 			break;
    128  1.1  riastrad 		}
    129  1.1  riastrad 		r = radeon_fence_wait_next(rdev, ring->idx);
    130  1.1  riastrad 		if (r)
    131  1.1  riastrad 			return r;
    132  1.1  riastrad 	}
    133  1.1  riastrad 	ring->count_dw = ndw;
    134  1.1  riastrad 	ring->wptr_old = ring->wptr;
    135  1.1  riastrad 	return 0;
    136  1.1  riastrad }
    137  1.1  riastrad 
    138  1.1  riastrad /**
    139  1.1  riastrad  * radeon_ring_lock - lock the ring and allocate space on it
    140  1.1  riastrad  *
    141  1.1  riastrad  * @rdev: radeon_device pointer
    142  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    143  1.1  riastrad  * @ndw: number of dwords to allocate in the ring buffer
    144  1.1  riastrad  *
    145  1.1  riastrad  * Lock the ring and allocate @ndw dwords in the ring buffer
    146  1.1  riastrad  * (all asics).
    147  1.1  riastrad  * Returns 0 on success, error on failure.
    148  1.1  riastrad  */
    149  1.1  riastrad int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
    150  1.1  riastrad {
    151  1.1  riastrad 	int r;
    152  1.1  riastrad 
    153  1.1  riastrad 	mutex_lock(&rdev->ring_lock);
    154  1.1  riastrad 	r = radeon_ring_alloc(rdev, ring, ndw);
    155  1.1  riastrad 	if (r) {
    156  1.1  riastrad 		mutex_unlock(&rdev->ring_lock);
    157  1.1  riastrad 		return r;
    158  1.1  riastrad 	}
    159  1.1  riastrad 	return 0;
    160  1.1  riastrad }
    161  1.1  riastrad 
    162  1.1  riastrad /**
    163  1.1  riastrad  * radeon_ring_commit - tell the GPU to execute the new
    164  1.1  riastrad  * commands on the ring buffer
    165  1.1  riastrad  *
    166  1.1  riastrad  * @rdev: radeon_device pointer
    167  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    168  1.3  riastrad  * @hdp_flush: Whether or not to perform an HDP cache flush
    169  1.1  riastrad  *
    170  1.1  riastrad  * Update the wptr (write pointer) to tell the GPU to
    171  1.1  riastrad  * execute new commands on the ring buffer (all asics).
    172  1.1  riastrad  */
    173  1.3  riastrad void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring,
    174  1.3  riastrad 			bool hdp_flush)
    175  1.1  riastrad {
    176  1.3  riastrad 	/* If we are emitting the HDP flush via the ring buffer, we need to
    177  1.3  riastrad 	 * do it before padding.
    178  1.3  riastrad 	 */
    179  1.3  riastrad 	if (hdp_flush && rdev->asic->ring[ring->idx]->hdp_flush)
    180  1.3  riastrad 		rdev->asic->ring[ring->idx]->hdp_flush(rdev, ring);
    181  1.1  riastrad 	/* We pad to match fetch size */
    182  1.1  riastrad 	while (ring->wptr & ring->align_mask) {
    183  1.1  riastrad 		radeon_ring_write(ring, ring->nop);
    184  1.1  riastrad 	}
    185  1.1  riastrad 	mb();
    186  1.3  riastrad 	/* If we are emitting the HDP flush via MMIO, we need to do it after
    187  1.3  riastrad 	 * all CPU writes to VRAM finished.
    188  1.3  riastrad 	 */
    189  1.3  riastrad 	if (hdp_flush && rdev->asic->mmio_hdp_flush)
    190  1.3  riastrad 		rdev->asic->mmio_hdp_flush(rdev);
    191  1.1  riastrad 	radeon_ring_set_wptr(rdev, ring);
    192  1.1  riastrad }
    193  1.1  riastrad 
    194  1.1  riastrad /**
    195  1.1  riastrad  * radeon_ring_unlock_commit - tell the GPU to execute the new
    196  1.1  riastrad  * commands on the ring buffer and unlock it
    197  1.1  riastrad  *
    198  1.1  riastrad  * @rdev: radeon_device pointer
    199  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    200  1.3  riastrad  * @hdp_flush: Whether or not to perform an HDP cache flush
    201  1.1  riastrad  *
    202  1.1  riastrad  * Call radeon_ring_commit() then unlock the ring (all asics).
    203  1.1  riastrad  */
    204  1.3  riastrad void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring,
    205  1.3  riastrad 			       bool hdp_flush)
    206  1.1  riastrad {
    207  1.3  riastrad 	radeon_ring_commit(rdev, ring, hdp_flush);
    208  1.1  riastrad 	mutex_unlock(&rdev->ring_lock);
    209  1.1  riastrad }
    210  1.1  riastrad 
    211  1.1  riastrad /**
    212  1.1  riastrad  * radeon_ring_undo - reset the wptr
    213  1.1  riastrad  *
    214  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    215  1.1  riastrad  *
    216  1.1  riastrad  * Reset the driver's copy of the wptr (all asics).
    217  1.1  riastrad  */
    218  1.1  riastrad void radeon_ring_undo(struct radeon_ring *ring)
    219  1.1  riastrad {
    220  1.1  riastrad 	ring->wptr = ring->wptr_old;
    221  1.1  riastrad }
    222  1.1  riastrad 
    223  1.1  riastrad /**
    224  1.1  riastrad  * radeon_ring_unlock_undo - reset the wptr and unlock the ring
    225  1.1  riastrad  *
    226  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    227  1.1  riastrad  *
    228  1.1  riastrad  * Call radeon_ring_undo() then unlock the ring (all asics).
    229  1.1  riastrad  */
    230  1.1  riastrad void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
    231  1.1  riastrad {
    232  1.1  riastrad 	radeon_ring_undo(ring);
    233  1.1  riastrad 	mutex_unlock(&rdev->ring_lock);
    234  1.1  riastrad }
    235  1.1  riastrad 
    236  1.1  riastrad /**
    237  1.1  riastrad  * radeon_ring_lockup_update - update lockup variables
    238  1.1  riastrad  *
    239  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    240  1.1  riastrad  *
    241  1.1  riastrad  * Update the last rptr value and timestamp (all asics).
    242  1.1  riastrad  */
    243  1.1  riastrad void radeon_ring_lockup_update(struct radeon_device *rdev,
    244  1.1  riastrad 			       struct radeon_ring *ring)
    245  1.1  riastrad {
    246  1.1  riastrad 	atomic_set(&ring->last_rptr, radeon_ring_get_rptr(rdev, ring));
    247  1.1  riastrad 	atomic64_set(&ring->last_activity, jiffies_64);
    248  1.1  riastrad }
    249  1.1  riastrad 
    250  1.1  riastrad /**
    251  1.1  riastrad  * radeon_ring_test_lockup() - check if ring is lockedup by recording information
    252  1.1  riastrad  * @rdev:       radeon device structure
    253  1.1  riastrad  * @ring:       radeon_ring structure holding ring information
    254  1.1  riastrad  *
    255  1.1  riastrad  */
    256  1.1  riastrad bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
    257  1.1  riastrad {
    258  1.1  riastrad 	uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
    259  1.1  riastrad 	uint64_t last = atomic64_read(&ring->last_activity);
    260  1.1  riastrad 	uint64_t elapsed;
    261  1.1  riastrad 
    262  1.1  riastrad 	if (rptr != atomic_read(&ring->last_rptr)) {
    263  1.1  riastrad 		/* ring is still working, no lockup */
    264  1.1  riastrad 		radeon_ring_lockup_update(rdev, ring);
    265  1.1  riastrad 		return false;
    266  1.1  riastrad 	}
    267  1.1  riastrad 
    268  1.1  riastrad 	elapsed = jiffies_to_msecs(jiffies_64 - last);
    269  1.1  riastrad 	if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
    270  1.2  riastrad 		dev_err(rdev->dev, "ring %d stalled for more than %"PRIu64"msec\n",
    271  1.1  riastrad 			ring->idx, elapsed);
    272  1.1  riastrad 		return true;
    273  1.1  riastrad 	}
    274  1.1  riastrad 	/* give a chance to the GPU ... */
    275  1.1  riastrad 	return false;
    276  1.1  riastrad }
    277  1.1  riastrad 
    278  1.1  riastrad /**
    279  1.1  riastrad  * radeon_ring_backup - Back up the content of a ring
    280  1.1  riastrad  *
    281  1.1  riastrad  * @rdev: radeon_device pointer
    282  1.1  riastrad  * @ring: the ring we want to back up
    283  1.1  riastrad  *
    284  1.1  riastrad  * Saves all unprocessed commits from a ring, returns the number of dwords saved.
    285  1.1  riastrad  */
    286  1.1  riastrad unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
    287  1.1  riastrad 			    uint32_t **data)
    288  1.1  riastrad {
    289  1.1  riastrad 	unsigned size, ptr, i;
    290  1.1  riastrad 
    291  1.1  riastrad 	/* just in case lock the ring */
    292  1.1  riastrad 	mutex_lock(&rdev->ring_lock);
    293  1.1  riastrad 	*data = NULL;
    294  1.1  riastrad 
    295  1.1  riastrad 	if (ring->ring_obj == NULL) {
    296  1.1  riastrad 		mutex_unlock(&rdev->ring_lock);
    297  1.1  riastrad 		return 0;
    298  1.1  riastrad 	}
    299  1.1  riastrad 
    300  1.1  riastrad 	/* it doesn't make sense to save anything if all fences are signaled */
    301  1.1  riastrad 	if (!radeon_fence_count_emitted(rdev, ring->idx)) {
    302  1.1  riastrad 		mutex_unlock(&rdev->ring_lock);
    303  1.1  riastrad 		return 0;
    304  1.1  riastrad 	}
    305  1.1  riastrad 
    306  1.1  riastrad 	/* calculate the number of dw on the ring */
    307  1.1  riastrad 	if (ring->rptr_save_reg)
    308  1.1  riastrad 		ptr = RREG32(ring->rptr_save_reg);
    309  1.1  riastrad 	else if (rdev->wb.enabled)
    310  1.1  riastrad 		ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
    311  1.1  riastrad 	else {
    312  1.1  riastrad 		/* no way to read back the next rptr */
    313  1.1  riastrad 		mutex_unlock(&rdev->ring_lock);
    314  1.1  riastrad 		return 0;
    315  1.1  riastrad 	}
    316  1.1  riastrad 
    317  1.1  riastrad 	size = ring->wptr + (ring->ring_size / 4);
    318  1.1  riastrad 	size -= ptr;
    319  1.1  riastrad 	size &= ring->ptr_mask;
    320  1.1  riastrad 	if (size == 0) {
    321  1.1  riastrad 		mutex_unlock(&rdev->ring_lock);
    322  1.1  riastrad 		return 0;
    323  1.1  riastrad 	}
    324  1.1  riastrad 
    325  1.1  riastrad 	/* and then save the content of the ring */
    326  1.5  riastrad 	*data = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
    327  1.1  riastrad 	if (!*data) {
    328  1.1  riastrad 		mutex_unlock(&rdev->ring_lock);
    329  1.1  riastrad 		return 0;
    330  1.1  riastrad 	}
    331  1.1  riastrad 	for (i = 0; i < size; ++i) {
    332  1.1  riastrad 		(*data)[i] = ring->ring[ptr++];
    333  1.1  riastrad 		ptr &= ring->ptr_mask;
    334  1.1  riastrad 	}
    335  1.1  riastrad 
    336  1.1  riastrad 	mutex_unlock(&rdev->ring_lock);
    337  1.1  riastrad 	return size;
    338  1.1  riastrad }
    339  1.1  riastrad 
    340  1.1  riastrad /**
    341  1.1  riastrad  * radeon_ring_restore - append saved commands to the ring again
    342  1.1  riastrad  *
    343  1.1  riastrad  * @rdev: radeon_device pointer
    344  1.1  riastrad  * @ring: ring to append commands to
    345  1.1  riastrad  * @size: number of dwords we want to write
    346  1.1  riastrad  * @data: saved commands
    347  1.1  riastrad  *
    348  1.1  riastrad  * Allocates space on the ring and restore the previously saved commands.
    349  1.1  riastrad  */
    350  1.1  riastrad int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
    351  1.1  riastrad 			unsigned size, uint32_t *data)
    352  1.1  riastrad {
    353  1.1  riastrad 	int i, r;
    354  1.1  riastrad 
    355  1.1  riastrad 	if (!size || !data)
    356  1.1  riastrad 		return 0;
    357  1.1  riastrad 
    358  1.1  riastrad 	/* restore the saved ring content */
    359  1.1  riastrad 	r = radeon_ring_lock(rdev, ring, size);
    360  1.1  riastrad 	if (r)
    361  1.1  riastrad 		return r;
    362  1.1  riastrad 
    363  1.1  riastrad 	for (i = 0; i < size; ++i) {
    364  1.1  riastrad 		radeon_ring_write(ring, data[i]);
    365  1.1  riastrad 	}
    366  1.1  riastrad 
    367  1.3  riastrad 	radeon_ring_unlock_commit(rdev, ring, false);
    368  1.5  riastrad 	kvfree(data);
    369  1.1  riastrad 	return 0;
    370  1.1  riastrad }
    371  1.1  riastrad 
    372  1.1  riastrad /**
    373  1.1  riastrad  * radeon_ring_init - init driver ring struct.
    374  1.1  riastrad  *
    375  1.1  riastrad  * @rdev: radeon_device pointer
    376  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    377  1.1  riastrad  * @ring_size: size of the ring
    378  1.1  riastrad  * @rptr_offs: offset of the rptr writeback location in the WB buffer
    379  1.1  riastrad  * @nop: nop packet for this ring
    380  1.1  riastrad  *
    381  1.1  riastrad  * Initialize the driver information for the selected ring (all asics).
    382  1.1  riastrad  * Returns 0 on success, error on failure.
    383  1.1  riastrad  */
    384  1.1  riastrad int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
    385  1.1  riastrad 		     unsigned rptr_offs, u32 nop)
    386  1.1  riastrad {
    387  1.1  riastrad 	int r;
    388  1.1  riastrad 
    389  1.1  riastrad 	ring->ring_size = ring_size;
    390  1.1  riastrad 	ring->rptr_offs = rptr_offs;
    391  1.1  riastrad 	ring->nop = nop;
    392  1.1  riastrad 	/* Allocate ring buffer */
    393  1.1  riastrad 	if (ring->ring_obj == NULL) {
    394  1.1  riastrad 		r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
    395  1.3  riastrad 				     RADEON_GEM_DOMAIN_GTT, 0, NULL,
    396  1.1  riastrad 				     NULL, &ring->ring_obj);
    397  1.1  riastrad 		if (r) {
    398  1.1  riastrad 			dev_err(rdev->dev, "(%d) ring create failed\n", r);
    399  1.1  riastrad 			return r;
    400  1.1  riastrad 		}
    401  1.1  riastrad 		r = radeon_bo_reserve(ring->ring_obj, false);
    402  1.1  riastrad 		if (unlikely(r != 0))
    403  1.1  riastrad 			return r;
    404  1.1  riastrad 		r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
    405  1.1  riastrad 					&ring->gpu_addr);
    406  1.1  riastrad 		if (r) {
    407  1.1  riastrad 			radeon_bo_unreserve(ring->ring_obj);
    408  1.1  riastrad 			dev_err(rdev->dev, "(%d) ring pin failed\n", r);
    409  1.1  riastrad 			return r;
    410  1.1  riastrad 		}
    411  1.1  riastrad 		r = radeon_bo_kmap(ring->ring_obj,
    412  1.2  riastrad 				       (void **)__UNVOLATILE(&ring->ring));
    413  1.1  riastrad 		radeon_bo_unreserve(ring->ring_obj);
    414  1.1  riastrad 		if (r) {
    415  1.1  riastrad 			dev_err(rdev->dev, "(%d) ring map failed\n", r);
    416  1.1  riastrad 			return r;
    417  1.1  riastrad 		}
    418  1.1  riastrad 	}
    419  1.1  riastrad 	ring->ptr_mask = (ring->ring_size / 4) - 1;
    420  1.1  riastrad 	ring->ring_free_dw = ring->ring_size / 4;
    421  1.1  riastrad 	if (rdev->wb.enabled) {
    422  1.1  riastrad 		u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
    423  1.1  riastrad 		ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
    424  1.1  riastrad 		ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
    425  1.1  riastrad 	}
    426  1.1  riastrad 	if (radeon_debugfs_ring_init(rdev, ring)) {
    427  1.1  riastrad 		DRM_ERROR("Failed to register debugfs file for rings !\n");
    428  1.1  riastrad 	}
    429  1.1  riastrad 	radeon_ring_lockup_update(rdev, ring);
    430  1.1  riastrad 	return 0;
    431  1.1  riastrad }
    432  1.1  riastrad 
    433  1.1  riastrad /**
    434  1.1  riastrad  * radeon_ring_fini - tear down the driver ring struct.
    435  1.1  riastrad  *
    436  1.1  riastrad  * @rdev: radeon_device pointer
    437  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    438  1.1  riastrad  *
    439  1.1  riastrad  * Tear down the driver information for the selected ring (all asics).
    440  1.1  riastrad  */
    441  1.1  riastrad void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
    442  1.1  riastrad {
    443  1.1  riastrad 	int r;
    444  1.1  riastrad 	struct radeon_bo *ring_obj;
    445  1.1  riastrad 
    446  1.1  riastrad 	mutex_lock(&rdev->ring_lock);
    447  1.1  riastrad 	ring_obj = ring->ring_obj;
    448  1.1  riastrad 	ring->ready = false;
    449  1.1  riastrad 	ring->ring = NULL;
    450  1.1  riastrad 	ring->ring_obj = NULL;
    451  1.1  riastrad 	mutex_unlock(&rdev->ring_lock);
    452  1.1  riastrad 
    453  1.1  riastrad 	if (ring_obj) {
    454  1.1  riastrad 		r = radeon_bo_reserve(ring_obj, false);
    455  1.1  riastrad 		if (likely(r == 0)) {
    456  1.1  riastrad 			radeon_bo_kunmap(ring_obj);
    457  1.1  riastrad 			radeon_bo_unpin(ring_obj);
    458  1.1  riastrad 			radeon_bo_unreserve(ring_obj);
    459  1.1  riastrad 		}
    460  1.1  riastrad 		radeon_bo_unref(&ring_obj);
    461  1.1  riastrad 	}
    462  1.1  riastrad }
    463  1.1  riastrad 
    464  1.1  riastrad /*
    465  1.1  riastrad  * Debugfs info
    466  1.1  riastrad  */
    467  1.1  riastrad #if defined(CONFIG_DEBUG_FS)
    468  1.1  riastrad 
    469  1.1  riastrad static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
    470  1.1  riastrad {
    471  1.1  riastrad 	struct drm_info_node *node = (struct drm_info_node *) m->private;
    472  1.1  riastrad 	struct drm_device *dev = node->minor->dev;
    473  1.1  riastrad 	struct radeon_device *rdev = dev->dev_private;
    474  1.1  riastrad 	int ridx = *(int*)node->info_ent->data;
    475  1.1  riastrad 	struct radeon_ring *ring = &rdev->ring[ridx];
    476  1.1  riastrad 
    477  1.1  riastrad 	uint32_t rptr, wptr, rptr_next;
    478  1.1  riastrad 	unsigned count, i, j;
    479  1.1  riastrad 
    480  1.1  riastrad 	radeon_ring_free_size(rdev, ring);
    481  1.1  riastrad 	count = (ring->ring_size / 4) - ring->ring_free_dw;
    482  1.1  riastrad 
    483  1.1  riastrad 	wptr = radeon_ring_get_wptr(rdev, ring);
    484  1.1  riastrad 	seq_printf(m, "wptr: 0x%08x [%5d]\n",
    485  1.1  riastrad 		   wptr, wptr);
    486  1.1  riastrad 
    487  1.1  riastrad 	rptr = radeon_ring_get_rptr(rdev, ring);
    488  1.1  riastrad 	seq_printf(m, "rptr: 0x%08x [%5d]\n",
    489  1.1  riastrad 		   rptr, rptr);
    490  1.1  riastrad 
    491  1.1  riastrad 	if (ring->rptr_save_reg) {
    492  1.1  riastrad 		rptr_next = RREG32(ring->rptr_save_reg);
    493  1.1  riastrad 		seq_printf(m, "rptr next(0x%04x): 0x%08x [%5d]\n",
    494  1.1  riastrad 			   ring->rptr_save_reg, rptr_next, rptr_next);
    495  1.1  riastrad 	} else
    496  1.1  riastrad 		rptr_next = ~0;
    497  1.1  riastrad 
    498  1.1  riastrad 	seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
    499  1.1  riastrad 		   ring->wptr, ring->wptr);
    500  1.1  riastrad 	seq_printf(m, "last semaphore signal addr : 0x%016llx\n",
    501  1.1  riastrad 		   ring->last_semaphore_signal_addr);
    502  1.1  riastrad 	seq_printf(m, "last semaphore wait addr   : 0x%016llx\n",
    503  1.1  riastrad 		   ring->last_semaphore_wait_addr);
    504  1.1  riastrad 	seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
    505  1.1  riastrad 	seq_printf(m, "%u dwords in ring\n", count);
    506  1.1  riastrad 
    507  1.3  riastrad 	if (!ring->ring)
    508  1.1  riastrad 		return 0;
    509  1.1  riastrad 
    510  1.1  riastrad 	/* print 8 dw before current rptr as often it's the last executed
    511  1.1  riastrad 	 * packet that is the root issue
    512  1.1  riastrad 	 */
    513  1.1  riastrad 	i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
    514  1.1  riastrad 	for (j = 0; j <= (count + 32); j++) {
    515  1.1  riastrad 		seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
    516  1.1  riastrad 		if (rptr == i)
    517  1.1  riastrad 			seq_puts(m, " *");
    518  1.1  riastrad 		if (rptr_next == i)
    519  1.1  riastrad 			seq_puts(m, " #");
    520  1.1  riastrad 		seq_puts(m, "\n");
    521  1.1  riastrad 		i = (i + 1) & ring->ptr_mask;
    522  1.1  riastrad 	}
    523  1.1  riastrad 	return 0;
    524  1.1  riastrad }
    525  1.1  riastrad 
    526  1.1  riastrad static int radeon_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
    527  1.1  riastrad static int cayman_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
    528  1.1  riastrad static int cayman_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
    529  1.1  riastrad static int radeon_dma1_index = R600_RING_TYPE_DMA_INDEX;
    530  1.1  riastrad static int radeon_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX;
    531  1.1  riastrad static int r600_uvd_index = R600_RING_TYPE_UVD_INDEX;
    532  1.1  riastrad static int si_vce1_index = TN_RING_TYPE_VCE1_INDEX;
    533  1.1  riastrad static int si_vce2_index = TN_RING_TYPE_VCE2_INDEX;
    534  1.1  riastrad 
    535  1.1  riastrad static struct drm_info_list radeon_debugfs_ring_info_list[] = {
    536  1.1  riastrad 	{"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_gfx_index},
    537  1.1  riastrad 	{"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_cp1_index},
    538  1.1  riastrad 	{"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_cp2_index},
    539  1.1  riastrad 	{"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_dma1_index},
    540  1.1  riastrad 	{"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_dma2_index},
    541  1.1  riastrad 	{"radeon_ring_uvd", radeon_debugfs_ring_info, 0, &r600_uvd_index},
    542  1.1  riastrad 	{"radeon_ring_vce1", radeon_debugfs_ring_info, 0, &si_vce1_index},
    543  1.1  riastrad 	{"radeon_ring_vce2", radeon_debugfs_ring_info, 0, &si_vce2_index},
    544  1.1  riastrad };
    545  1.1  riastrad 
    546  1.1  riastrad #endif
    547  1.1  riastrad 
    548  1.1  riastrad static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
    549  1.1  riastrad {
    550  1.1  riastrad #if defined(CONFIG_DEBUG_FS)
    551  1.1  riastrad 	unsigned i;
    552  1.1  riastrad 	for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
    553  1.1  riastrad 		struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
    554  1.1  riastrad 		int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
    555  1.1  riastrad 		unsigned r;
    556  1.1  riastrad 
    557  1.1  riastrad 		if (&rdev->ring[ridx] != ring)
    558  1.1  riastrad 			continue;
    559  1.1  riastrad 
    560  1.1  riastrad 		r = radeon_debugfs_add_files(rdev, info, 1);
    561  1.1  riastrad 		if (r)
    562  1.1  riastrad 			return r;
    563  1.1  riastrad 	}
    564  1.1  riastrad #endif
    565  1.1  riastrad 	return 0;
    566  1.1  riastrad }
    567