Home | History | Annotate | Line # | Download | only in radeon
      1  1.3  riastrad /*	$NetBSD: radeon_cik_sdma.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2013 Advanced Micro Devices, Inc.
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     14  1.1  riastrad  * all copies or substantial portions of the Software.
     15  1.1  riastrad  *
     16  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     19  1.1  riastrad  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
     20  1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     21  1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     22  1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     23  1.1  riastrad  *
     24  1.1  riastrad  * Authors: Alex Deucher
     25  1.1  riastrad  */
     26  1.1  riastrad #include <sys/cdefs.h>
     27  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_cik_sdma.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $");
     28  1.1  riastrad 
     29  1.1  riastrad #include <linux/firmware.h>
     30  1.3  riastrad 
     31  1.1  riastrad #include "radeon.h"
     32  1.1  riastrad #include "radeon_ucode.h"
     33  1.1  riastrad #include "radeon_asic.h"
     34  1.1  riastrad #include "radeon_trace.h"
     35  1.1  riastrad #include "cikd.h"
     36  1.1  riastrad 
     37  1.1  riastrad /* sdma */
     38  1.1  riastrad #define CIK_SDMA_UCODE_SIZE 1050
     39  1.1  riastrad #define CIK_SDMA_UCODE_VERSION 64
     40  1.1  riastrad 
     41  1.1  riastrad u32 cik_gpu_check_soft_reset(struct radeon_device *rdev);
     42  1.1  riastrad 
     43  1.1  riastrad /*
     44  1.1  riastrad  * sDMA - System DMA
     45  1.1  riastrad  * Starting with CIK, the GPU has new asynchronous
     46  1.1  riastrad  * DMA engines.  These engines are used for compute
     47  1.1  riastrad  * and gfx.  There are two DMA engines (SDMA0, SDMA1)
     48  1.1  riastrad  * and each one supports 1 ring buffer used for gfx
     49  1.1  riastrad  * and 2 queues used for compute.
     50  1.1  riastrad  *
     51  1.1  riastrad  * The programming model is very similar to the CP
     52  1.1  riastrad  * (ring buffer, IBs, etc.), but sDMA has it's own
     53  1.1  riastrad  * packet format that is different from the PM4 format
     54  1.1  riastrad  * used by the CP. sDMA supports copying data, writing
     55  1.1  riastrad  * embedded data, solid fills, and a number of other
     56  1.1  riastrad  * things.  It also has support for tiling/detiling of
     57  1.1  riastrad  * buffers.
     58  1.1  riastrad  */
     59  1.1  riastrad 
     60  1.1  riastrad /**
     61  1.1  riastrad  * cik_sdma_get_rptr - get the current read pointer
     62  1.1  riastrad  *
     63  1.1  riastrad  * @rdev: radeon_device pointer
     64  1.1  riastrad  * @ring: radeon ring pointer
     65  1.1  riastrad  *
     66  1.1  riastrad  * Get the current rptr from the hardware (CIK+).
     67  1.1  riastrad  */
     68  1.1  riastrad uint32_t cik_sdma_get_rptr(struct radeon_device *rdev,
     69  1.1  riastrad 			   struct radeon_ring *ring)
     70  1.1  riastrad {
     71  1.1  riastrad 	u32 rptr, reg;
     72  1.1  riastrad 
     73  1.1  riastrad 	if (rdev->wb.enabled) {
     74  1.1  riastrad 		rptr = rdev->wb.wb[ring->rptr_offs/4];
     75  1.1  riastrad 	} else {
     76  1.1  riastrad 		if (ring->idx == R600_RING_TYPE_DMA_INDEX)
     77  1.1  riastrad 			reg = SDMA0_GFX_RB_RPTR + SDMA0_REGISTER_OFFSET;
     78  1.1  riastrad 		else
     79  1.1  riastrad 			reg = SDMA0_GFX_RB_RPTR + SDMA1_REGISTER_OFFSET;
     80  1.1  riastrad 
     81  1.1  riastrad 		rptr = RREG32(reg);
     82  1.1  riastrad 	}
     83  1.1  riastrad 
     84  1.1  riastrad 	return (rptr & 0x3fffc) >> 2;
     85  1.1  riastrad }
     86  1.1  riastrad 
     87  1.1  riastrad /**
     88  1.1  riastrad  * cik_sdma_get_wptr - get the current write pointer
     89  1.1  riastrad  *
     90  1.1  riastrad  * @rdev: radeon_device pointer
     91  1.1  riastrad  * @ring: radeon ring pointer
     92  1.1  riastrad  *
     93  1.1  riastrad  * Get the current wptr from the hardware (CIK+).
     94  1.1  riastrad  */
     95  1.1  riastrad uint32_t cik_sdma_get_wptr(struct radeon_device *rdev,
     96  1.1  riastrad 			   struct radeon_ring *ring)
     97  1.1  riastrad {
     98  1.1  riastrad 	u32 reg;
     99  1.1  riastrad 
    100  1.1  riastrad 	if (ring->idx == R600_RING_TYPE_DMA_INDEX)
    101  1.1  riastrad 		reg = SDMA0_GFX_RB_WPTR + SDMA0_REGISTER_OFFSET;
    102  1.1  riastrad 	else
    103  1.1  riastrad 		reg = SDMA0_GFX_RB_WPTR + SDMA1_REGISTER_OFFSET;
    104  1.1  riastrad 
    105  1.1  riastrad 	return (RREG32(reg) & 0x3fffc) >> 2;
    106  1.1  riastrad }
    107  1.1  riastrad 
    108  1.1  riastrad /**
    109  1.1  riastrad  * cik_sdma_set_wptr - commit the write pointer
    110  1.1  riastrad  *
    111  1.1  riastrad  * @rdev: radeon_device pointer
    112  1.1  riastrad  * @ring: radeon ring pointer
    113  1.1  riastrad  *
    114  1.1  riastrad  * Write the wptr back to the hardware (CIK+).
    115  1.1  riastrad  */
    116  1.1  riastrad void cik_sdma_set_wptr(struct radeon_device *rdev,
    117  1.1  riastrad 		       struct radeon_ring *ring)
    118  1.1  riastrad {
    119  1.1  riastrad 	u32 reg;
    120  1.1  riastrad 
    121  1.1  riastrad 	if (ring->idx == R600_RING_TYPE_DMA_INDEX)
    122  1.1  riastrad 		reg = SDMA0_GFX_RB_WPTR + SDMA0_REGISTER_OFFSET;
    123  1.1  riastrad 	else
    124  1.1  riastrad 		reg = SDMA0_GFX_RB_WPTR + SDMA1_REGISTER_OFFSET;
    125  1.1  riastrad 
    126  1.1  riastrad 	WREG32(reg, (ring->wptr << 2) & 0x3fffc);
    127  1.1  riastrad 	(void)RREG32(reg);
    128  1.1  riastrad }
    129  1.1  riastrad 
    130  1.1  riastrad /**
    131  1.1  riastrad  * cik_sdma_ring_ib_execute - Schedule an IB on the DMA engine
    132  1.1  riastrad  *
    133  1.1  riastrad  * @rdev: radeon_device pointer
    134  1.1  riastrad  * @ib: IB object to schedule
    135  1.1  riastrad  *
    136  1.1  riastrad  * Schedule an IB in the DMA ring (CIK).
    137  1.1  riastrad  */
    138  1.1  riastrad void cik_sdma_ring_ib_execute(struct radeon_device *rdev,
    139  1.1  riastrad 			      struct radeon_ib *ib)
    140  1.1  riastrad {
    141  1.1  riastrad 	struct radeon_ring *ring = &rdev->ring[ib->ring];
    142  1.1  riastrad 	u32 extra_bits = (ib->vm ? ib->vm->ids[ib->ring].id : 0) & 0xf;
    143  1.1  riastrad 
    144  1.1  riastrad 	if (rdev->wb.enabled) {
    145  1.1  riastrad 		u32 next_rptr = ring->wptr + 5;
    146  1.1  riastrad 		while ((next_rptr & 7) != 4)
    147  1.1  riastrad 			next_rptr++;
    148  1.1  riastrad 		next_rptr += 4;
    149  1.1  riastrad 		radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0));
    150  1.1  riastrad 		radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
    151  1.1  riastrad 		radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr));
    152  1.1  riastrad 		radeon_ring_write(ring, 1); /* number of DWs to follow */
    153  1.1  riastrad 		radeon_ring_write(ring, next_rptr);
    154  1.1  riastrad 	}
    155  1.1  riastrad 
    156  1.1  riastrad 	/* IB packet must end on a 8 DW boundary */
    157  1.1  riastrad 	while ((ring->wptr & 7) != 4)
    158  1.1  riastrad 		radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0));
    159  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_INDIRECT_BUFFER, 0, extra_bits));
    160  1.1  riastrad 	radeon_ring_write(ring, ib->gpu_addr & 0xffffffe0); /* base must be 32 byte aligned */
    161  1.1  riastrad 	radeon_ring_write(ring, upper_32_bits(ib->gpu_addr));
    162  1.1  riastrad 	radeon_ring_write(ring, ib->length_dw);
    163  1.1  riastrad 
    164  1.1  riastrad }
    165  1.1  riastrad 
    166  1.1  riastrad /**
    167  1.1  riastrad  * cik_sdma_hdp_flush_ring_emit - emit an hdp flush on the DMA ring
    168  1.1  riastrad  *
    169  1.1  riastrad  * @rdev: radeon_device pointer
    170  1.1  riastrad  * @ridx: radeon ring index
    171  1.1  riastrad  *
    172  1.1  riastrad  * Emit an hdp flush packet on the requested DMA ring.
    173  1.1  riastrad  */
    174  1.1  riastrad static void cik_sdma_hdp_flush_ring_emit(struct radeon_device *rdev,
    175  1.1  riastrad 					 int ridx)
    176  1.1  riastrad {
    177  1.1  riastrad 	struct radeon_ring *ring = &rdev->ring[ridx];
    178  1.1  riastrad 	u32 extra_bits = (SDMA_POLL_REG_MEM_EXTRA_OP(1) |
    179  1.1  riastrad 			  SDMA_POLL_REG_MEM_EXTRA_FUNC(3)); /* == */
    180  1.1  riastrad 	u32 ref_and_mask;
    181  1.1  riastrad 
    182  1.1  riastrad 	if (ridx == R600_RING_TYPE_DMA_INDEX)
    183  1.1  riastrad 		ref_and_mask = SDMA0;
    184  1.1  riastrad 	else
    185  1.1  riastrad 		ref_and_mask = SDMA1;
    186  1.1  riastrad 
    187  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0, extra_bits));
    188  1.1  riastrad 	radeon_ring_write(ring, GPU_HDP_FLUSH_DONE);
    189  1.1  riastrad 	radeon_ring_write(ring, GPU_HDP_FLUSH_REQ);
    190  1.1  riastrad 	radeon_ring_write(ring, ref_and_mask); /* reference */
    191  1.1  riastrad 	radeon_ring_write(ring, ref_and_mask); /* mask */
    192  1.1  riastrad 	radeon_ring_write(ring, (0xfff << 16) | 10); /* retry count, poll interval */
    193  1.1  riastrad }
    194  1.1  riastrad 
    195  1.1  riastrad /**
    196  1.1  riastrad  * cik_sdma_fence_ring_emit - emit a fence on the DMA ring
    197  1.1  riastrad  *
    198  1.1  riastrad  * @rdev: radeon_device pointer
    199  1.1  riastrad  * @fence: radeon fence object
    200  1.1  riastrad  *
    201  1.1  riastrad  * Add a DMA fence packet to the ring to write
    202  1.1  riastrad  * the fence seq number and DMA trap packet to generate
    203  1.1  riastrad  * an interrupt if needed (CIK).
    204  1.1  riastrad  */
    205  1.1  riastrad void cik_sdma_fence_ring_emit(struct radeon_device *rdev,
    206  1.1  riastrad 			      struct radeon_fence *fence)
    207  1.1  riastrad {
    208  1.1  riastrad 	struct radeon_ring *ring = &rdev->ring[fence->ring];
    209  1.1  riastrad 	u64 addr = rdev->fence_drv[fence->ring].gpu_addr;
    210  1.1  riastrad 
    211  1.1  riastrad 	/* write the fence */
    212  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_FENCE, 0, 0));
    213  1.1  riastrad 	radeon_ring_write(ring, lower_32_bits(addr));
    214  1.1  riastrad 	radeon_ring_write(ring, upper_32_bits(addr));
    215  1.1  riastrad 	radeon_ring_write(ring, fence->seq);
    216  1.1  riastrad 	/* generate an interrupt */
    217  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_TRAP, 0, 0));
    218  1.1  riastrad 	/* flush HDP */
    219  1.1  riastrad 	cik_sdma_hdp_flush_ring_emit(rdev, fence->ring);
    220  1.1  riastrad }
    221  1.1  riastrad 
    222  1.1  riastrad /**
    223  1.1  riastrad  * cik_sdma_semaphore_ring_emit - emit a semaphore on the dma ring
    224  1.1  riastrad  *
    225  1.1  riastrad  * @rdev: radeon_device pointer
    226  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    227  1.1  riastrad  * @semaphore: radeon semaphore object
    228  1.1  riastrad  * @emit_wait: wait or signal semaphore
    229  1.1  riastrad  *
    230  1.1  riastrad  * Add a DMA semaphore packet to the ring wait on or signal
    231  1.1  riastrad  * other rings (CIK).
    232  1.1  riastrad  */
    233  1.1  riastrad bool cik_sdma_semaphore_ring_emit(struct radeon_device *rdev,
    234  1.1  riastrad 				  struct radeon_ring *ring,
    235  1.1  riastrad 				  struct radeon_semaphore *semaphore,
    236  1.1  riastrad 				  bool emit_wait)
    237  1.1  riastrad {
    238  1.1  riastrad 	u64 addr = semaphore->gpu_addr;
    239  1.1  riastrad 	u32 extra_bits = emit_wait ? 0 : SDMA_SEMAPHORE_EXTRA_S;
    240  1.1  riastrad 
    241  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SEMAPHORE, 0, extra_bits));
    242  1.1  riastrad 	radeon_ring_write(ring, addr & 0xfffffff8);
    243  1.1  riastrad 	radeon_ring_write(ring, upper_32_bits(addr));
    244  1.1  riastrad 
    245  1.1  riastrad 	return true;
    246  1.1  riastrad }
    247  1.1  riastrad 
    248  1.1  riastrad /**
    249  1.1  riastrad  * cik_sdma_gfx_stop - stop the gfx async dma engines
    250  1.1  riastrad  *
    251  1.1  riastrad  * @rdev: radeon_device pointer
    252  1.1  riastrad  *
    253  1.1  riastrad  * Stop the gfx async dma ring buffers (CIK).
    254  1.1  riastrad  */
    255  1.1  riastrad static void cik_sdma_gfx_stop(struct radeon_device *rdev)
    256  1.1  riastrad {
    257  1.1  riastrad 	u32 rb_cntl, reg_offset;
    258  1.1  riastrad 	int i;
    259  1.1  riastrad 
    260  1.1  riastrad 	if ((rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX) ||
    261  1.1  riastrad 	    (rdev->asic->copy.copy_ring_index == CAYMAN_RING_TYPE_DMA1_INDEX))
    262  1.1  riastrad 		radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
    263  1.1  riastrad 
    264  1.1  riastrad 	for (i = 0; i < 2; i++) {
    265  1.1  riastrad 		if (i == 0)
    266  1.1  riastrad 			reg_offset = SDMA0_REGISTER_OFFSET;
    267  1.1  riastrad 		else
    268  1.1  riastrad 			reg_offset = SDMA1_REGISTER_OFFSET;
    269  1.1  riastrad 		rb_cntl = RREG32(SDMA0_GFX_RB_CNTL + reg_offset);
    270  1.1  riastrad 		rb_cntl &= ~SDMA_RB_ENABLE;
    271  1.1  riastrad 		WREG32(SDMA0_GFX_RB_CNTL + reg_offset, rb_cntl);
    272  1.1  riastrad 		WREG32(SDMA0_GFX_IB_CNTL + reg_offset, 0);
    273  1.1  riastrad 	}
    274  1.1  riastrad 	rdev->ring[R600_RING_TYPE_DMA_INDEX].ready = false;
    275  1.1  riastrad 	rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready = false;
    276  1.1  riastrad 
    277  1.1  riastrad 	/* FIXME use something else than big hammer but after few days can not
    278  1.1  riastrad 	 * seem to find good combination so reset SDMA blocks as it seems we
    279  1.1  riastrad 	 * do not shut them down properly. This fix hibernation and does not
    280  1.1  riastrad 	 * affect suspend to ram.
    281  1.1  riastrad 	 */
    282  1.1  riastrad 	WREG32(SRBM_SOFT_RESET, SOFT_RESET_SDMA | SOFT_RESET_SDMA1);
    283  1.1  riastrad 	(void)RREG32(SRBM_SOFT_RESET);
    284  1.1  riastrad 	udelay(50);
    285  1.1  riastrad 	WREG32(SRBM_SOFT_RESET, 0);
    286  1.1  riastrad 	(void)RREG32(SRBM_SOFT_RESET);
    287  1.1  riastrad }
    288  1.1  riastrad 
    289  1.1  riastrad /**
    290  1.1  riastrad  * cik_sdma_rlc_stop - stop the compute async dma engines
    291  1.1  riastrad  *
    292  1.1  riastrad  * @rdev: radeon_device pointer
    293  1.1  riastrad  *
    294  1.1  riastrad  * Stop the compute async dma queues (CIK).
    295  1.1  riastrad  */
    296  1.1  riastrad static void cik_sdma_rlc_stop(struct radeon_device *rdev)
    297  1.1  riastrad {
    298  1.1  riastrad 	/* XXX todo */
    299  1.1  riastrad }
    300  1.1  riastrad 
    301  1.1  riastrad /**
    302  1.1  riastrad  * cik_sdma_ctx_switch_enable - enable/disable sdma engine preemption
    303  1.1  riastrad  *
    304  1.1  riastrad  * @rdev: radeon_device pointer
    305  1.1  riastrad  * @enable: enable/disable preemption.
    306  1.1  riastrad  *
    307  1.1  riastrad  * Halt or unhalt the async dma engines (CIK).
    308  1.1  riastrad  */
    309  1.1  riastrad static void cik_sdma_ctx_switch_enable(struct radeon_device *rdev, bool enable)
    310  1.1  riastrad {
    311  1.1  riastrad 	uint32_t reg_offset, value;
    312  1.1  riastrad 	int i;
    313  1.1  riastrad 
    314  1.1  riastrad 	for (i = 0; i < 2; i++) {
    315  1.1  riastrad 		if (i == 0)
    316  1.1  riastrad 			reg_offset = SDMA0_REGISTER_OFFSET;
    317  1.1  riastrad 		else
    318  1.1  riastrad 			reg_offset = SDMA1_REGISTER_OFFSET;
    319  1.1  riastrad 		value = RREG32(SDMA0_CNTL + reg_offset);
    320  1.1  riastrad 		if (enable)
    321  1.1  riastrad 			value |= AUTO_CTXSW_ENABLE;
    322  1.1  riastrad 		else
    323  1.1  riastrad 			value &= ~AUTO_CTXSW_ENABLE;
    324  1.1  riastrad 		WREG32(SDMA0_CNTL + reg_offset, value);
    325  1.1  riastrad 	}
    326  1.1  riastrad }
    327  1.1  riastrad 
    328  1.1  riastrad /**
    329  1.1  riastrad  * cik_sdma_enable - stop the async dma engines
    330  1.1  riastrad  *
    331  1.1  riastrad  * @rdev: radeon_device pointer
    332  1.1  riastrad  * @enable: enable/disable the DMA MEs.
    333  1.1  riastrad  *
    334  1.1  riastrad  * Halt or unhalt the async dma engines (CIK).
    335  1.1  riastrad  */
    336  1.1  riastrad void cik_sdma_enable(struct radeon_device *rdev, bool enable)
    337  1.1  riastrad {
    338  1.1  riastrad 	u32 me_cntl, reg_offset;
    339  1.1  riastrad 	int i;
    340  1.1  riastrad 
    341  1.3  riastrad 	if (!enable) {
    342  1.1  riastrad 		cik_sdma_gfx_stop(rdev);
    343  1.1  riastrad 		cik_sdma_rlc_stop(rdev);
    344  1.1  riastrad 	}
    345  1.1  riastrad 
    346  1.1  riastrad 	for (i = 0; i < 2; i++) {
    347  1.1  riastrad 		if (i == 0)
    348  1.1  riastrad 			reg_offset = SDMA0_REGISTER_OFFSET;
    349  1.1  riastrad 		else
    350  1.1  riastrad 			reg_offset = SDMA1_REGISTER_OFFSET;
    351  1.1  riastrad 		me_cntl = RREG32(SDMA0_ME_CNTL + reg_offset);
    352  1.1  riastrad 		if (enable)
    353  1.1  riastrad 			me_cntl &= ~SDMA_HALT;
    354  1.1  riastrad 		else
    355  1.1  riastrad 			me_cntl |= SDMA_HALT;
    356  1.1  riastrad 		WREG32(SDMA0_ME_CNTL + reg_offset, me_cntl);
    357  1.1  riastrad 	}
    358  1.1  riastrad 
    359  1.1  riastrad 	cik_sdma_ctx_switch_enable(rdev, enable);
    360  1.1  riastrad }
    361  1.1  riastrad 
    362  1.1  riastrad /**
    363  1.1  riastrad  * cik_sdma_gfx_resume - setup and start the async dma engines
    364  1.1  riastrad  *
    365  1.1  riastrad  * @rdev: radeon_device pointer
    366  1.1  riastrad  *
    367  1.1  riastrad  * Set up the gfx DMA ring buffers and enable them (CIK).
    368  1.1  riastrad  * Returns 0 for success, error for failure.
    369  1.1  riastrad  */
    370  1.1  riastrad static int cik_sdma_gfx_resume(struct radeon_device *rdev)
    371  1.1  riastrad {
    372  1.1  riastrad 	struct radeon_ring *ring;
    373  1.1  riastrad 	u32 rb_cntl, ib_cntl;
    374  1.1  riastrad 	u32 rb_bufsz;
    375  1.1  riastrad 	u32 reg_offset, wb_offset;
    376  1.1  riastrad 	int i, r;
    377  1.1  riastrad 
    378  1.1  riastrad 	for (i = 0; i < 2; i++) {
    379  1.1  riastrad 		if (i == 0) {
    380  1.1  riastrad 			ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
    381  1.1  riastrad 			reg_offset = SDMA0_REGISTER_OFFSET;
    382  1.1  riastrad 			wb_offset = R600_WB_DMA_RPTR_OFFSET;
    383  1.1  riastrad 		} else {
    384  1.1  riastrad 			ring = &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX];
    385  1.1  riastrad 			reg_offset = SDMA1_REGISTER_OFFSET;
    386  1.1  riastrad 			wb_offset = CAYMAN_WB_DMA1_RPTR_OFFSET;
    387  1.1  riastrad 		}
    388  1.1  riastrad 
    389  1.1  riastrad 		WREG32(SDMA0_SEM_INCOMPLETE_TIMER_CNTL + reg_offset, 0);
    390  1.1  riastrad 		WREG32(SDMA0_SEM_WAIT_FAIL_TIMER_CNTL + reg_offset, 0);
    391  1.1  riastrad 
    392  1.1  riastrad 		/* Set ring buffer size in dwords */
    393  1.1  riastrad 		rb_bufsz = order_base_2(ring->ring_size / 4);
    394  1.1  riastrad 		rb_cntl = rb_bufsz << 1;
    395  1.1  riastrad #ifdef __BIG_ENDIAN
    396  1.1  riastrad 		rb_cntl |= SDMA_RB_SWAP_ENABLE | SDMA_RPTR_WRITEBACK_SWAP_ENABLE;
    397  1.1  riastrad #endif
    398  1.1  riastrad 		WREG32(SDMA0_GFX_RB_CNTL + reg_offset, rb_cntl);
    399  1.1  riastrad 
    400  1.1  riastrad 		/* Initialize the ring buffer's read and write pointers */
    401  1.1  riastrad 		WREG32(SDMA0_GFX_RB_RPTR + reg_offset, 0);
    402  1.1  riastrad 		WREG32(SDMA0_GFX_RB_WPTR + reg_offset, 0);
    403  1.1  riastrad 
    404  1.1  riastrad 		/* set the wb address whether it's enabled or not */
    405  1.1  riastrad 		WREG32(SDMA0_GFX_RB_RPTR_ADDR_HI + reg_offset,
    406  1.1  riastrad 		       upper_32_bits(rdev->wb.gpu_addr + wb_offset) & 0xFFFFFFFF);
    407  1.1  riastrad 		WREG32(SDMA0_GFX_RB_RPTR_ADDR_LO + reg_offset,
    408  1.1  riastrad 		       ((rdev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC));
    409  1.1  riastrad 
    410  1.1  riastrad 		if (rdev->wb.enabled)
    411  1.1  riastrad 			rb_cntl |= SDMA_RPTR_WRITEBACK_ENABLE;
    412  1.1  riastrad 
    413  1.1  riastrad 		WREG32(SDMA0_GFX_RB_BASE + reg_offset, ring->gpu_addr >> 8);
    414  1.1  riastrad 		WREG32(SDMA0_GFX_RB_BASE_HI + reg_offset, ring->gpu_addr >> 40);
    415  1.1  riastrad 
    416  1.1  riastrad 		ring->wptr = 0;
    417  1.1  riastrad 		WREG32(SDMA0_GFX_RB_WPTR + reg_offset, ring->wptr << 2);
    418  1.1  riastrad 
    419  1.1  riastrad 		/* enable DMA RB */
    420  1.1  riastrad 		WREG32(SDMA0_GFX_RB_CNTL + reg_offset, rb_cntl | SDMA_RB_ENABLE);
    421  1.1  riastrad 
    422  1.1  riastrad 		ib_cntl = SDMA_IB_ENABLE;
    423  1.1  riastrad #ifdef __BIG_ENDIAN
    424  1.1  riastrad 		ib_cntl |= SDMA_IB_SWAP_ENABLE;
    425  1.1  riastrad #endif
    426  1.1  riastrad 		/* enable DMA IBs */
    427  1.1  riastrad 		WREG32(SDMA0_GFX_IB_CNTL + reg_offset, ib_cntl);
    428  1.1  riastrad 
    429  1.1  riastrad 		ring->ready = true;
    430  1.1  riastrad 
    431  1.1  riastrad 		r = radeon_ring_test(rdev, ring->idx, ring);
    432  1.1  riastrad 		if (r) {
    433  1.1  riastrad 			ring->ready = false;
    434  1.1  riastrad 			return r;
    435  1.1  riastrad 		}
    436  1.1  riastrad 	}
    437  1.1  riastrad 
    438  1.1  riastrad 	if ((rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX) ||
    439  1.1  riastrad 	    (rdev->asic->copy.copy_ring_index == CAYMAN_RING_TYPE_DMA1_INDEX))
    440  1.1  riastrad 		radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
    441  1.1  riastrad 
    442  1.1  riastrad 	return 0;
    443  1.1  riastrad }
    444  1.1  riastrad 
    445  1.1  riastrad /**
    446  1.1  riastrad  * cik_sdma_rlc_resume - setup and start the async dma engines
    447  1.1  riastrad  *
    448  1.1  riastrad  * @rdev: radeon_device pointer
    449  1.1  riastrad  *
    450  1.1  riastrad  * Set up the compute DMA queues and enable them (CIK).
    451  1.1  riastrad  * Returns 0 for success, error for failure.
    452  1.1  riastrad  */
    453  1.1  riastrad static int cik_sdma_rlc_resume(struct radeon_device *rdev)
    454  1.1  riastrad {
    455  1.1  riastrad 	/* XXX todo */
    456  1.1  riastrad 	return 0;
    457  1.1  riastrad }
    458  1.1  riastrad 
    459  1.1  riastrad /**
    460  1.1  riastrad  * cik_sdma_load_microcode - load the sDMA ME ucode
    461  1.1  riastrad  *
    462  1.1  riastrad  * @rdev: radeon_device pointer
    463  1.1  riastrad  *
    464  1.1  riastrad  * Loads the sDMA0/1 ucode.
    465  1.1  riastrad  * Returns 0 for success, -EINVAL if the ucode is not available.
    466  1.1  riastrad  */
    467  1.1  riastrad static int cik_sdma_load_microcode(struct radeon_device *rdev)
    468  1.1  riastrad {
    469  1.1  riastrad 	int i;
    470  1.1  riastrad 
    471  1.1  riastrad 	if (!rdev->sdma_fw)
    472  1.1  riastrad 		return -EINVAL;
    473  1.1  riastrad 
    474  1.1  riastrad 	/* halt the MEs */
    475  1.1  riastrad 	cik_sdma_enable(rdev, false);
    476  1.1  riastrad 
    477  1.1  riastrad 	if (rdev->new_fw) {
    478  1.1  riastrad 		const struct sdma_firmware_header_v1_0 *hdr =
    479  1.1  riastrad 			(const struct sdma_firmware_header_v1_0 *)rdev->sdma_fw->data;
    480  1.1  riastrad 		const __le32 *fw_data;
    481  1.1  riastrad 		u32 fw_size;
    482  1.1  riastrad 
    483  1.1  riastrad 		radeon_ucode_print_sdma_hdr(&hdr->header);
    484  1.1  riastrad 
    485  1.1  riastrad 		/* sdma0 */
    486  1.1  riastrad 		fw_data = (const __le32 *)
    487  1.1  riastrad 			(rdev->sdma_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
    488  1.1  riastrad 		fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
    489  1.1  riastrad 		WREG32(SDMA0_UCODE_ADDR + SDMA0_REGISTER_OFFSET, 0);
    490  1.1  riastrad 		for (i = 0; i < fw_size; i++)
    491  1.1  riastrad 			WREG32(SDMA0_UCODE_DATA + SDMA0_REGISTER_OFFSET, le32_to_cpup(fw_data++));
    492  1.1  riastrad 		WREG32(SDMA0_UCODE_DATA + SDMA0_REGISTER_OFFSET, CIK_SDMA_UCODE_VERSION);
    493  1.1  riastrad 
    494  1.1  riastrad 		/* sdma1 */
    495  1.1  riastrad 		fw_data = (const __le32 *)
    496  1.1  riastrad 			(rdev->sdma_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
    497  1.1  riastrad 		fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
    498  1.1  riastrad 		WREG32(SDMA0_UCODE_ADDR + SDMA1_REGISTER_OFFSET, 0);
    499  1.1  riastrad 		for (i = 0; i < fw_size; i++)
    500  1.1  riastrad 			WREG32(SDMA0_UCODE_DATA + SDMA1_REGISTER_OFFSET, le32_to_cpup(fw_data++));
    501  1.1  riastrad 		WREG32(SDMA0_UCODE_DATA + SDMA1_REGISTER_OFFSET, CIK_SDMA_UCODE_VERSION);
    502  1.1  riastrad 	} else {
    503  1.1  riastrad 		const __be32 *fw_data;
    504  1.1  riastrad 
    505  1.1  riastrad 		/* sdma0 */
    506  1.1  riastrad 		fw_data = (const __be32 *)rdev->sdma_fw->data;
    507  1.1  riastrad 		WREG32(SDMA0_UCODE_ADDR + SDMA0_REGISTER_OFFSET, 0);
    508  1.1  riastrad 		for (i = 0; i < CIK_SDMA_UCODE_SIZE; i++)
    509  1.1  riastrad 			WREG32(SDMA0_UCODE_DATA + SDMA0_REGISTER_OFFSET, be32_to_cpup(fw_data++));
    510  1.1  riastrad 		WREG32(SDMA0_UCODE_DATA + SDMA0_REGISTER_OFFSET, CIK_SDMA_UCODE_VERSION);
    511  1.1  riastrad 
    512  1.1  riastrad 		/* sdma1 */
    513  1.1  riastrad 		fw_data = (const __be32 *)rdev->sdma_fw->data;
    514  1.1  riastrad 		WREG32(SDMA0_UCODE_ADDR + SDMA1_REGISTER_OFFSET, 0);
    515  1.1  riastrad 		for (i = 0; i < CIK_SDMA_UCODE_SIZE; i++)
    516  1.1  riastrad 			WREG32(SDMA0_UCODE_DATA + SDMA1_REGISTER_OFFSET, be32_to_cpup(fw_data++));
    517  1.1  riastrad 		WREG32(SDMA0_UCODE_DATA + SDMA1_REGISTER_OFFSET, CIK_SDMA_UCODE_VERSION);
    518  1.1  riastrad 	}
    519  1.1  riastrad 
    520  1.1  riastrad 	WREG32(SDMA0_UCODE_ADDR + SDMA0_REGISTER_OFFSET, 0);
    521  1.1  riastrad 	WREG32(SDMA0_UCODE_ADDR + SDMA1_REGISTER_OFFSET, 0);
    522  1.1  riastrad 	return 0;
    523  1.1  riastrad }
    524  1.1  riastrad 
    525  1.1  riastrad /**
    526  1.1  riastrad  * cik_sdma_resume - setup and start the async dma engines
    527  1.1  riastrad  *
    528  1.1  riastrad  * @rdev: radeon_device pointer
    529  1.1  riastrad  *
    530  1.1  riastrad  * Set up the DMA engines and enable them (CIK).
    531  1.1  riastrad  * Returns 0 for success, error for failure.
    532  1.1  riastrad  */
    533  1.1  riastrad int cik_sdma_resume(struct radeon_device *rdev)
    534  1.1  riastrad {
    535  1.1  riastrad 	int r;
    536  1.1  riastrad 
    537  1.1  riastrad 	r = cik_sdma_load_microcode(rdev);
    538  1.1  riastrad 	if (r)
    539  1.1  riastrad 		return r;
    540  1.1  riastrad 
    541  1.1  riastrad 	/* unhalt the MEs */
    542  1.1  riastrad 	cik_sdma_enable(rdev, true);
    543  1.1  riastrad 
    544  1.1  riastrad 	/* start the gfx rings and rlc compute queues */
    545  1.1  riastrad 	r = cik_sdma_gfx_resume(rdev);
    546  1.1  riastrad 	if (r)
    547  1.1  riastrad 		return r;
    548  1.1  riastrad 	r = cik_sdma_rlc_resume(rdev);
    549  1.1  riastrad 	if (r)
    550  1.1  riastrad 		return r;
    551  1.1  riastrad 
    552  1.1  riastrad 	return 0;
    553  1.1  riastrad }
    554  1.1  riastrad 
    555  1.1  riastrad /**
    556  1.1  riastrad  * cik_sdma_fini - tear down the async dma engines
    557  1.1  riastrad  *
    558  1.1  riastrad  * @rdev: radeon_device pointer
    559  1.1  riastrad  *
    560  1.1  riastrad  * Stop the async dma engines and free the rings (CIK).
    561  1.1  riastrad  */
    562  1.1  riastrad void cik_sdma_fini(struct radeon_device *rdev)
    563  1.1  riastrad {
    564  1.1  riastrad 	/* halt the MEs */
    565  1.1  riastrad 	cik_sdma_enable(rdev, false);
    566  1.1  riastrad 	radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_DMA_INDEX]);
    567  1.1  riastrad 	radeon_ring_fini(rdev, &rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX]);
    568  1.1  riastrad 	/* XXX - compute dma queue tear down */
    569  1.1  riastrad }
    570  1.1  riastrad 
    571  1.1  riastrad /**
    572  1.1  riastrad  * cik_copy_dma - copy pages using the DMA engine
    573  1.1  riastrad  *
    574  1.1  riastrad  * @rdev: radeon_device pointer
    575  1.1  riastrad  * @src_offset: src GPU address
    576  1.1  riastrad  * @dst_offset: dst GPU address
    577  1.1  riastrad  * @num_gpu_pages: number of GPU pages to xfer
    578  1.1  riastrad  * @resv: reservation object to sync to
    579  1.1  riastrad  *
    580  1.1  riastrad  * Copy GPU paging using the DMA engine (CIK).
    581  1.1  riastrad  * Used by the radeon ttm implementation to move pages if
    582  1.1  riastrad  * registered as the asic copy callback.
    583  1.1  riastrad  */
    584  1.1  riastrad struct radeon_fence *cik_copy_dma(struct radeon_device *rdev,
    585  1.1  riastrad 				  uint64_t src_offset, uint64_t dst_offset,
    586  1.1  riastrad 				  unsigned num_gpu_pages,
    587  1.3  riastrad 				  struct dma_resv *resv)
    588  1.1  riastrad {
    589  1.1  riastrad 	struct radeon_fence *fence;
    590  1.1  riastrad 	struct radeon_sync sync;
    591  1.1  riastrad 	int ring_index = rdev->asic->copy.dma_ring_index;
    592  1.1  riastrad 	struct radeon_ring *ring = &rdev->ring[ring_index];
    593  1.1  riastrad 	u32 size_in_bytes, cur_size_in_bytes;
    594  1.1  riastrad 	int i, num_loops;
    595  1.1  riastrad 	int r = 0;
    596  1.1  riastrad 
    597  1.1  riastrad 	radeon_sync_create(&sync);
    598  1.1  riastrad 
    599  1.1  riastrad 	size_in_bytes = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT);
    600  1.1  riastrad 	num_loops = DIV_ROUND_UP(size_in_bytes, 0x1fffff);
    601  1.1  riastrad 	r = radeon_ring_lock(rdev, ring, num_loops * 7 + 14);
    602  1.1  riastrad 	if (r) {
    603  1.1  riastrad 		DRM_ERROR("radeon: moving bo (%d).\n", r);
    604  1.1  riastrad 		radeon_sync_free(rdev, &sync, NULL);
    605  1.1  riastrad 		return ERR_PTR(r);
    606  1.1  riastrad 	}
    607  1.1  riastrad 
    608  1.1  riastrad 	radeon_sync_resv(rdev, &sync, resv, false);
    609  1.1  riastrad 	radeon_sync_rings(rdev, &sync, ring->idx);
    610  1.1  riastrad 
    611  1.1  riastrad 	for (i = 0; i < num_loops; i++) {
    612  1.1  riastrad 		cur_size_in_bytes = size_in_bytes;
    613  1.1  riastrad 		if (cur_size_in_bytes > 0x1fffff)
    614  1.1  riastrad 			cur_size_in_bytes = 0x1fffff;
    615  1.1  riastrad 		size_in_bytes -= cur_size_in_bytes;
    616  1.1  riastrad 		radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_COPY, SDMA_COPY_SUB_OPCODE_LINEAR, 0));
    617  1.1  riastrad 		radeon_ring_write(ring, cur_size_in_bytes);
    618  1.1  riastrad 		radeon_ring_write(ring, 0); /* src/dst endian swap */
    619  1.1  riastrad 		radeon_ring_write(ring, lower_32_bits(src_offset));
    620  1.1  riastrad 		radeon_ring_write(ring, upper_32_bits(src_offset));
    621  1.1  riastrad 		radeon_ring_write(ring, lower_32_bits(dst_offset));
    622  1.1  riastrad 		radeon_ring_write(ring, upper_32_bits(dst_offset));
    623  1.1  riastrad 		src_offset += cur_size_in_bytes;
    624  1.1  riastrad 		dst_offset += cur_size_in_bytes;
    625  1.1  riastrad 	}
    626  1.1  riastrad 
    627  1.1  riastrad 	r = radeon_fence_emit(rdev, &fence, ring->idx);
    628  1.1  riastrad 	if (r) {
    629  1.1  riastrad 		radeon_ring_unlock_undo(rdev, ring);
    630  1.1  riastrad 		radeon_sync_free(rdev, &sync, NULL);
    631  1.1  riastrad 		return ERR_PTR(r);
    632  1.1  riastrad 	}
    633  1.1  riastrad 
    634  1.1  riastrad 	radeon_ring_unlock_commit(rdev, ring, false);
    635  1.1  riastrad 	radeon_sync_free(rdev, &sync, fence);
    636  1.1  riastrad 
    637  1.1  riastrad 	return fence;
    638  1.1  riastrad }
    639  1.1  riastrad 
    640  1.1  riastrad /**
    641  1.1  riastrad  * cik_sdma_ring_test - simple async dma engine test
    642  1.1  riastrad  *
    643  1.1  riastrad  * @rdev: radeon_device pointer
    644  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    645  1.1  riastrad  *
    646  1.1  riastrad  * Test the DMA engine by writing using it to write an
    647  1.1  riastrad  * value to memory. (CIK).
    648  1.1  riastrad  * Returns 0 for success, error for failure.
    649  1.1  riastrad  */
    650  1.1  riastrad int cik_sdma_ring_test(struct radeon_device *rdev,
    651  1.1  riastrad 		       struct radeon_ring *ring)
    652  1.1  riastrad {
    653  1.1  riastrad 	unsigned i;
    654  1.1  riastrad 	int r;
    655  1.1  riastrad 	unsigned index;
    656  1.1  riastrad 	u32 tmp;
    657  1.1  riastrad 	u64 gpu_addr;
    658  1.1  riastrad 
    659  1.1  riastrad 	if (ring->idx == R600_RING_TYPE_DMA_INDEX)
    660  1.1  riastrad 		index = R600_WB_DMA_RING_TEST_OFFSET;
    661  1.1  riastrad 	else
    662  1.1  riastrad 		index = CAYMAN_WB_DMA1_RING_TEST_OFFSET;
    663  1.1  riastrad 
    664  1.1  riastrad 	gpu_addr = rdev->wb.gpu_addr + index;
    665  1.1  riastrad 
    666  1.1  riastrad 	tmp = 0xCAFEDEAD;
    667  1.1  riastrad 	rdev->wb.wb[index/4] = cpu_to_le32(tmp);
    668  1.1  riastrad 
    669  1.1  riastrad 	r = radeon_ring_lock(rdev, ring, 5);
    670  1.1  riastrad 	if (r) {
    671  1.1  riastrad 		DRM_ERROR("radeon: dma failed to lock ring %d (%d).\n", ring->idx, r);
    672  1.1  riastrad 		return r;
    673  1.1  riastrad 	}
    674  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0));
    675  1.1  riastrad 	radeon_ring_write(ring, lower_32_bits(gpu_addr));
    676  1.1  riastrad 	radeon_ring_write(ring, upper_32_bits(gpu_addr));
    677  1.1  riastrad 	radeon_ring_write(ring, 1); /* number of DWs to follow */
    678  1.1  riastrad 	radeon_ring_write(ring, 0xDEADBEEF);
    679  1.1  riastrad 	radeon_ring_unlock_commit(rdev, ring, false);
    680  1.1  riastrad 
    681  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
    682  1.1  riastrad 		tmp = le32_to_cpu(rdev->wb.wb[index/4]);
    683  1.1  riastrad 		if (tmp == 0xDEADBEEF)
    684  1.1  riastrad 			break;
    685  1.3  riastrad 		udelay(1);
    686  1.1  riastrad 	}
    687  1.1  riastrad 
    688  1.1  riastrad 	if (i < rdev->usec_timeout) {
    689  1.1  riastrad 		DRM_INFO("ring test on %d succeeded in %d usecs\n", ring->idx, i);
    690  1.1  riastrad 	} else {
    691  1.1  riastrad 		DRM_ERROR("radeon: ring %d test failed (0x%08X)\n",
    692  1.1  riastrad 			  ring->idx, tmp);
    693  1.1  riastrad 		r = -EINVAL;
    694  1.1  riastrad 	}
    695  1.1  riastrad 	return r;
    696  1.1  riastrad }
    697  1.1  riastrad 
    698  1.1  riastrad /**
    699  1.1  riastrad  * cik_sdma_ib_test - test an IB on the DMA engine
    700  1.1  riastrad  *
    701  1.1  riastrad  * @rdev: radeon_device pointer
    702  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    703  1.1  riastrad  *
    704  1.1  riastrad  * Test a simple IB in the DMA ring (CIK).
    705  1.1  riastrad  * Returns 0 on success, error on failure.
    706  1.1  riastrad  */
    707  1.1  riastrad int cik_sdma_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
    708  1.1  riastrad {
    709  1.1  riastrad 	struct radeon_ib ib;
    710  1.1  riastrad 	unsigned i;
    711  1.1  riastrad 	unsigned index;
    712  1.1  riastrad 	int r;
    713  1.1  riastrad 	u32 tmp = 0;
    714  1.1  riastrad 	u64 gpu_addr;
    715  1.1  riastrad 
    716  1.1  riastrad 	if (ring->idx == R600_RING_TYPE_DMA_INDEX)
    717  1.1  riastrad 		index = R600_WB_DMA_RING_TEST_OFFSET;
    718  1.1  riastrad 	else
    719  1.1  riastrad 		index = CAYMAN_WB_DMA1_RING_TEST_OFFSET;
    720  1.1  riastrad 
    721  1.1  riastrad 	gpu_addr = rdev->wb.gpu_addr + index;
    722  1.1  riastrad 
    723  1.1  riastrad 	tmp = 0xCAFEDEAD;
    724  1.1  riastrad 	rdev->wb.wb[index/4] = cpu_to_le32(tmp);
    725  1.1  riastrad 
    726  1.1  riastrad 	r = radeon_ib_get(rdev, ring->idx, &ib, NULL, 256);
    727  1.1  riastrad 	if (r) {
    728  1.1  riastrad 		DRM_ERROR("radeon: failed to get ib (%d).\n", r);
    729  1.1  riastrad 		return r;
    730  1.1  riastrad 	}
    731  1.1  riastrad 
    732  1.1  riastrad 	ib.ptr[0] = SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
    733  1.1  riastrad 	ib.ptr[1] = lower_32_bits(gpu_addr);
    734  1.1  riastrad 	ib.ptr[2] = upper_32_bits(gpu_addr);
    735  1.1  riastrad 	ib.ptr[3] = 1;
    736  1.1  riastrad 	ib.ptr[4] = 0xDEADBEEF;
    737  1.1  riastrad 	ib.length_dw = 5;
    738  1.1  riastrad 
    739  1.1  riastrad 	r = radeon_ib_schedule(rdev, &ib, NULL, false);
    740  1.1  riastrad 	if (r) {
    741  1.1  riastrad 		radeon_ib_free(rdev, &ib);
    742  1.1  riastrad 		DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
    743  1.1  riastrad 		return r;
    744  1.1  riastrad 	}
    745  1.3  riastrad 	r = radeon_fence_wait_timeout(ib.fence, false, usecs_to_jiffies(
    746  1.3  riastrad 		RADEON_USEC_IB_TEST_TIMEOUT));
    747  1.3  riastrad 	if (r < 0) {
    748  1.1  riastrad 		DRM_ERROR("radeon: fence wait failed (%d).\n", r);
    749  1.1  riastrad 		return r;
    750  1.3  riastrad 	} else if (r == 0) {
    751  1.3  riastrad 		DRM_ERROR("radeon: fence wait timed out.\n");
    752  1.3  riastrad 		return -ETIMEDOUT;
    753  1.1  riastrad 	}
    754  1.3  riastrad 	r = 0;
    755  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
    756  1.1  riastrad 		tmp = le32_to_cpu(rdev->wb.wb[index/4]);
    757  1.1  riastrad 		if (tmp == 0xDEADBEEF)
    758  1.1  riastrad 			break;
    759  1.3  riastrad 		udelay(1);
    760  1.1  riastrad 	}
    761  1.1  riastrad 	if (i < rdev->usec_timeout) {
    762  1.1  riastrad 		DRM_INFO("ib test on ring %d succeeded in %u usecs\n", ib.fence->ring, i);
    763  1.1  riastrad 	} else {
    764  1.1  riastrad 		DRM_ERROR("radeon: ib test failed (0x%08X)\n", tmp);
    765  1.1  riastrad 		r = -EINVAL;
    766  1.1  riastrad 	}
    767  1.1  riastrad 	radeon_ib_free(rdev, &ib);
    768  1.1  riastrad 	return r;
    769  1.1  riastrad }
    770  1.1  riastrad 
    771  1.1  riastrad /**
    772  1.1  riastrad  * cik_sdma_is_lockup - Check if the DMA engine is locked up
    773  1.1  riastrad  *
    774  1.1  riastrad  * @rdev: radeon_device pointer
    775  1.1  riastrad  * @ring: radeon_ring structure holding ring information
    776  1.1  riastrad  *
    777  1.1  riastrad  * Check if the async DMA engine is locked up (CIK).
    778  1.1  riastrad  * Returns true if the engine appears to be locked up, false if not.
    779  1.1  riastrad  */
    780  1.1  riastrad bool cik_sdma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
    781  1.1  riastrad {
    782  1.1  riastrad 	u32 reset_mask = cik_gpu_check_soft_reset(rdev);
    783  1.1  riastrad 	u32 mask;
    784  1.1  riastrad 
    785  1.1  riastrad 	if (ring->idx == R600_RING_TYPE_DMA_INDEX)
    786  1.1  riastrad 		mask = RADEON_RESET_DMA;
    787  1.1  riastrad 	else
    788  1.1  riastrad 		mask = RADEON_RESET_DMA1;
    789  1.1  riastrad 
    790  1.1  riastrad 	if (!(reset_mask & mask)) {
    791  1.1  riastrad 		radeon_ring_lockup_update(rdev, ring);
    792  1.1  riastrad 		return false;
    793  1.1  riastrad 	}
    794  1.1  riastrad 	return radeon_ring_test_lockup(rdev, ring);
    795  1.1  riastrad }
    796  1.1  riastrad 
    797  1.1  riastrad /**
    798  1.1  riastrad  * cik_sdma_vm_copy_pages - update PTEs by copying them from the GART
    799  1.1  riastrad  *
    800  1.1  riastrad  * @rdev: radeon_device pointer
    801  1.1  riastrad  * @ib: indirect buffer to fill with commands
    802  1.1  riastrad  * @pe: addr of the page entry
    803  1.1  riastrad  * @src: src addr to copy from
    804  1.1  riastrad  * @count: number of page entries to update
    805  1.1  riastrad  *
    806  1.1  riastrad  * Update PTEs by copying them from the GART using sDMA (CIK).
    807  1.1  riastrad  */
    808  1.1  riastrad void cik_sdma_vm_copy_pages(struct radeon_device *rdev,
    809  1.1  riastrad 			    struct radeon_ib *ib,
    810  1.1  riastrad 			    uint64_t pe, uint64_t src,
    811  1.1  riastrad 			    unsigned count)
    812  1.1  riastrad {
    813  1.1  riastrad 	while (count) {
    814  1.1  riastrad 		unsigned bytes = count * 8;
    815  1.1  riastrad 		if (bytes > 0x1FFFF8)
    816  1.1  riastrad 			bytes = 0x1FFFF8;
    817  1.1  riastrad 
    818  1.1  riastrad 		ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY,
    819  1.1  riastrad 			SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
    820  1.1  riastrad 		ib->ptr[ib->length_dw++] = bytes;
    821  1.1  riastrad 		ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
    822  1.1  riastrad 		ib->ptr[ib->length_dw++] = lower_32_bits(src);
    823  1.1  riastrad 		ib->ptr[ib->length_dw++] = upper_32_bits(src);
    824  1.1  riastrad 		ib->ptr[ib->length_dw++] = lower_32_bits(pe);
    825  1.1  riastrad 		ib->ptr[ib->length_dw++] = upper_32_bits(pe);
    826  1.1  riastrad 
    827  1.1  riastrad 		pe += bytes;
    828  1.1  riastrad 		src += bytes;
    829  1.1  riastrad 		count -= bytes / 8;
    830  1.1  riastrad 	}
    831  1.1  riastrad }
    832  1.1  riastrad 
    833  1.1  riastrad /**
    834  1.1  riastrad  * cik_sdma_vm_write_pages - update PTEs by writing them manually
    835  1.1  riastrad  *
    836  1.1  riastrad  * @rdev: radeon_device pointer
    837  1.1  riastrad  * @ib: indirect buffer to fill with commands
    838  1.1  riastrad  * @pe: addr of the page entry
    839  1.1  riastrad  * @addr: dst addr to write into pe
    840  1.1  riastrad  * @count: number of page entries to update
    841  1.1  riastrad  * @incr: increase next addr by incr bytes
    842  1.1  riastrad  * @flags: access flags
    843  1.1  riastrad  *
    844  1.1  riastrad  * Update PTEs by writing them manually using sDMA (CIK).
    845  1.1  riastrad  */
    846  1.1  riastrad void cik_sdma_vm_write_pages(struct radeon_device *rdev,
    847  1.1  riastrad 			     struct radeon_ib *ib,
    848  1.1  riastrad 			     uint64_t pe,
    849  1.1  riastrad 			     uint64_t addr, unsigned count,
    850  1.1  riastrad 			     uint32_t incr, uint32_t flags)
    851  1.1  riastrad {
    852  1.1  riastrad 	uint64_t value;
    853  1.1  riastrad 	unsigned ndw;
    854  1.1  riastrad 
    855  1.1  riastrad 	while (count) {
    856  1.1  riastrad 		ndw = count * 2;
    857  1.1  riastrad 		if (ndw > 0xFFFFE)
    858  1.1  riastrad 			ndw = 0xFFFFE;
    859  1.1  riastrad 
    860  1.1  riastrad 		/* for non-physically contiguous pages (system) */
    861  1.1  riastrad 		ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_WRITE,
    862  1.1  riastrad 			SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
    863  1.1  riastrad 		ib->ptr[ib->length_dw++] = pe;
    864  1.1  riastrad 		ib->ptr[ib->length_dw++] = upper_32_bits(pe);
    865  1.1  riastrad 		ib->ptr[ib->length_dw++] = ndw;
    866  1.1  riastrad 		for (; ndw > 0; ndw -= 2, --count, pe += 8) {
    867  1.1  riastrad 			if (flags & R600_PTE_SYSTEM) {
    868  1.1  riastrad 				value = radeon_vm_map_gart(rdev, addr);
    869  1.1  riastrad 			} else if (flags & R600_PTE_VALID) {
    870  1.1  riastrad 				value = addr;
    871  1.1  riastrad 			} else {
    872  1.1  riastrad 				value = 0;
    873  1.1  riastrad 			}
    874  1.1  riastrad 			addr += incr;
    875  1.1  riastrad 			value |= flags;
    876  1.1  riastrad 			ib->ptr[ib->length_dw++] = value;
    877  1.1  riastrad 			ib->ptr[ib->length_dw++] = upper_32_bits(value);
    878  1.1  riastrad 		}
    879  1.1  riastrad 	}
    880  1.1  riastrad }
    881  1.1  riastrad 
    882  1.1  riastrad /**
    883  1.1  riastrad  * cik_sdma_vm_set_pages - update the page tables using sDMA
    884  1.1  riastrad  *
    885  1.1  riastrad  * @rdev: radeon_device pointer
    886  1.1  riastrad  * @ib: indirect buffer to fill with commands
    887  1.1  riastrad  * @pe: addr of the page entry
    888  1.1  riastrad  * @addr: dst addr to write into pe
    889  1.1  riastrad  * @count: number of page entries to update
    890  1.1  riastrad  * @incr: increase next addr by incr bytes
    891  1.1  riastrad  * @flags: access flags
    892  1.1  riastrad  *
    893  1.1  riastrad  * Update the page tables using sDMA (CIK).
    894  1.1  riastrad  */
    895  1.1  riastrad void cik_sdma_vm_set_pages(struct radeon_device *rdev,
    896  1.1  riastrad 			   struct radeon_ib *ib,
    897  1.1  riastrad 			   uint64_t pe,
    898  1.1  riastrad 			   uint64_t addr, unsigned count,
    899  1.1  riastrad 			   uint32_t incr, uint32_t flags)
    900  1.1  riastrad {
    901  1.1  riastrad 	uint64_t value;
    902  1.1  riastrad 	unsigned ndw;
    903  1.1  riastrad 
    904  1.1  riastrad 	while (count) {
    905  1.1  riastrad 		ndw = count;
    906  1.1  riastrad 		if (ndw > 0x7FFFF)
    907  1.1  riastrad 			ndw = 0x7FFFF;
    908  1.1  riastrad 
    909  1.1  riastrad 		if (flags & R600_PTE_VALID)
    910  1.1  riastrad 			value = addr;
    911  1.1  riastrad 		else
    912  1.1  riastrad 			value = 0;
    913  1.1  riastrad 
    914  1.1  riastrad 		/* for physically contiguous pages (vram) */
    915  1.1  riastrad 		ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_GENERATE_PTE_PDE, 0, 0);
    916  1.1  riastrad 		ib->ptr[ib->length_dw++] = pe; /* dst addr */
    917  1.1  riastrad 		ib->ptr[ib->length_dw++] = upper_32_bits(pe);
    918  1.1  riastrad 		ib->ptr[ib->length_dw++] = flags; /* mask */
    919  1.1  riastrad 		ib->ptr[ib->length_dw++] = 0;
    920  1.1  riastrad 		ib->ptr[ib->length_dw++] = value; /* value */
    921  1.1  riastrad 		ib->ptr[ib->length_dw++] = upper_32_bits(value);
    922  1.1  riastrad 		ib->ptr[ib->length_dw++] = incr; /* increment size */
    923  1.1  riastrad 		ib->ptr[ib->length_dw++] = 0;
    924  1.1  riastrad 		ib->ptr[ib->length_dw++] = ndw; /* number of entries */
    925  1.1  riastrad 
    926  1.1  riastrad 		pe += ndw * 8;
    927  1.1  riastrad 		addr += ndw * incr;
    928  1.1  riastrad 		count -= ndw;
    929  1.1  riastrad 	}
    930  1.1  riastrad }
    931  1.1  riastrad 
    932  1.1  riastrad /**
    933  1.1  riastrad  * cik_sdma_vm_pad_ib - pad the IB to the required number of dw
    934  1.1  riastrad  *
    935  1.1  riastrad  * @ib: indirect buffer to fill with padding
    936  1.1  riastrad  *
    937  1.1  riastrad  */
    938  1.1  riastrad void cik_sdma_vm_pad_ib(struct radeon_ib *ib)
    939  1.1  riastrad {
    940  1.1  riastrad 	while (ib->length_dw & 0x7)
    941  1.1  riastrad 		ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0);
    942  1.1  riastrad }
    943  1.1  riastrad 
    944  1.1  riastrad /**
    945  1.1  riastrad  * cik_dma_vm_flush - cik vm flush using sDMA
    946  1.1  riastrad  *
    947  1.1  riastrad  * @rdev: radeon_device pointer
    948  1.1  riastrad  *
    949  1.1  riastrad  * Update the page table base and flush the VM TLB
    950  1.1  riastrad  * using sDMA (CIK).
    951  1.1  riastrad  */
    952  1.1  riastrad void cik_dma_vm_flush(struct radeon_device *rdev, struct radeon_ring *ring,
    953  1.1  riastrad 		      unsigned vm_id, uint64_t pd_addr)
    954  1.1  riastrad {
    955  1.1  riastrad 	u32 extra_bits = (SDMA_POLL_REG_MEM_EXTRA_OP(0) |
    956  1.1  riastrad 			  SDMA_POLL_REG_MEM_EXTRA_FUNC(0)); /* always */
    957  1.1  riastrad 
    958  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
    959  1.1  riastrad 	if (vm_id < 8) {
    960  1.1  riastrad 		radeon_ring_write(ring, (VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm_id << 2)) >> 2);
    961  1.1  riastrad 	} else {
    962  1.1  riastrad 		radeon_ring_write(ring, (VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((vm_id - 8) << 2)) >> 2);
    963  1.1  riastrad 	}
    964  1.1  riastrad 	radeon_ring_write(ring, pd_addr >> 12);
    965  1.1  riastrad 
    966  1.1  riastrad 	/* update SH_MEM_* regs */
    967  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
    968  1.1  riastrad 	radeon_ring_write(ring, SRBM_GFX_CNTL >> 2);
    969  1.1  riastrad 	radeon_ring_write(ring, VMID(vm_id));
    970  1.1  riastrad 
    971  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
    972  1.1  riastrad 	radeon_ring_write(ring, SH_MEM_BASES >> 2);
    973  1.1  riastrad 	radeon_ring_write(ring, 0);
    974  1.1  riastrad 
    975  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
    976  1.1  riastrad 	radeon_ring_write(ring, SH_MEM_CONFIG >> 2);
    977  1.1  riastrad 	radeon_ring_write(ring, 0);
    978  1.1  riastrad 
    979  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
    980  1.1  riastrad 	radeon_ring_write(ring, SH_MEM_APE1_BASE >> 2);
    981  1.1  riastrad 	radeon_ring_write(ring, 1);
    982  1.1  riastrad 
    983  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
    984  1.1  riastrad 	radeon_ring_write(ring, SH_MEM_APE1_LIMIT >> 2);
    985  1.1  riastrad 	radeon_ring_write(ring, 0);
    986  1.1  riastrad 
    987  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
    988  1.1  riastrad 	radeon_ring_write(ring, SRBM_GFX_CNTL >> 2);
    989  1.1  riastrad 	radeon_ring_write(ring, VMID(0));
    990  1.1  riastrad 
    991  1.1  riastrad 	/* flush HDP */
    992  1.1  riastrad 	cik_sdma_hdp_flush_ring_emit(rdev, ring->idx);
    993  1.1  riastrad 
    994  1.1  riastrad 	/* flush TLB */
    995  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
    996  1.1  riastrad 	radeon_ring_write(ring, VM_INVALIDATE_REQUEST >> 2);
    997  1.1  riastrad 	radeon_ring_write(ring, 1 << vm_id);
    998  1.1  riastrad 
    999  1.1  riastrad 	radeon_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0, extra_bits));
   1000  1.1  riastrad 	radeon_ring_write(ring, VM_INVALIDATE_REQUEST >> 2);
   1001  1.1  riastrad 	radeon_ring_write(ring, 0);
   1002  1.1  riastrad 	radeon_ring_write(ring, 0); /* reference */
   1003  1.1  riastrad 	radeon_ring_write(ring, 0); /* mask */
   1004  1.1  riastrad 	radeon_ring_write(ring, (0xfff << 16) | 10); /* retry count, poll interval */
   1005  1.1  riastrad }
   1006  1.1  riastrad 
   1007