162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2013 Advanced Micro Devices, Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * Authors: Alex Deucher
2362306a36Sopenharmony_ci */
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include "radeon.h"
2662306a36Sopenharmony_ci#include "radeon_asic.h"
2762306a36Sopenharmony_ci#include "r600.h"
2862306a36Sopenharmony_ci#include "r600d.h"
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/*
3162306a36Sopenharmony_ci * DMA
3262306a36Sopenharmony_ci * Starting with R600, the GPU has an asynchronous
3362306a36Sopenharmony_ci * DMA engine.  The programming model is very similar
3462306a36Sopenharmony_ci * to the 3D engine (ring buffer, IBs, etc.), but the
3562306a36Sopenharmony_ci * DMA controller has it's own packet format that is
3662306a36Sopenharmony_ci * different form the PM4 format used by the 3D engine.
3762306a36Sopenharmony_ci * It supports copying data, writing embedded data,
3862306a36Sopenharmony_ci * solid fills, and a number of other things.  It also
3962306a36Sopenharmony_ci * has support for tiling/detiling of buffers.
4062306a36Sopenharmony_ci */
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/**
4362306a36Sopenharmony_ci * r600_dma_get_rptr - get the current read pointer
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci * @rdev: radeon_device pointer
4662306a36Sopenharmony_ci * @ring: radeon ring pointer
4762306a36Sopenharmony_ci *
4862306a36Sopenharmony_ci * Get the current rptr from the hardware (r6xx+).
4962306a36Sopenharmony_ci */
5062306a36Sopenharmony_ciuint32_t r600_dma_get_rptr(struct radeon_device *rdev,
5162306a36Sopenharmony_ci			   struct radeon_ring *ring)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	u32 rptr;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	if (rdev->wb.enabled)
5662306a36Sopenharmony_ci		rptr = rdev->wb.wb[ring->rptr_offs/4];
5762306a36Sopenharmony_ci	else
5862306a36Sopenharmony_ci		rptr = RREG32(DMA_RB_RPTR);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	return (rptr & 0x3fffc) >> 2;
6162306a36Sopenharmony_ci}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci/**
6462306a36Sopenharmony_ci * r600_dma_get_wptr - get the current write pointer
6562306a36Sopenharmony_ci *
6662306a36Sopenharmony_ci * @rdev: radeon_device pointer
6762306a36Sopenharmony_ci * @ring: radeon ring pointer
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * Get the current wptr from the hardware (r6xx+).
7062306a36Sopenharmony_ci */
7162306a36Sopenharmony_ciuint32_t r600_dma_get_wptr(struct radeon_device *rdev,
7262306a36Sopenharmony_ci			   struct radeon_ring *ring)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	return (RREG32(DMA_RB_WPTR) & 0x3fffc) >> 2;
7562306a36Sopenharmony_ci}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/**
7862306a36Sopenharmony_ci * r600_dma_set_wptr - commit the write pointer
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * @rdev: radeon_device pointer
8162306a36Sopenharmony_ci * @ring: radeon ring pointer
8262306a36Sopenharmony_ci *
8362306a36Sopenharmony_ci * Write the wptr back to the hardware (r6xx+).
8462306a36Sopenharmony_ci */
8562306a36Sopenharmony_civoid r600_dma_set_wptr(struct radeon_device *rdev,
8662306a36Sopenharmony_ci		       struct radeon_ring *ring)
8762306a36Sopenharmony_ci{
8862306a36Sopenharmony_ci	WREG32(DMA_RB_WPTR, (ring->wptr << 2) & 0x3fffc);
8962306a36Sopenharmony_ci}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci/**
9262306a36Sopenharmony_ci * r600_dma_stop - stop the async dma engine
9362306a36Sopenharmony_ci *
9462306a36Sopenharmony_ci * @rdev: radeon_device pointer
9562306a36Sopenharmony_ci *
9662306a36Sopenharmony_ci * Stop the async dma engine (r6xx-evergreen).
9762306a36Sopenharmony_ci */
9862306a36Sopenharmony_civoid r600_dma_stop(struct radeon_device *rdev)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	u32 rb_cntl = RREG32(DMA_RB_CNTL);
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	if (rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX)
10362306a36Sopenharmony_ci		radeon_ttm_set_active_vram_size(rdev, rdev->mc.visible_vram_size);
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	rb_cntl &= ~DMA_RB_ENABLE;
10662306a36Sopenharmony_ci	WREG32(DMA_RB_CNTL, rb_cntl);
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	rdev->ring[R600_RING_TYPE_DMA_INDEX].ready = false;
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/**
11262306a36Sopenharmony_ci * r600_dma_resume - setup and start the async dma engine
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci * @rdev: radeon_device pointer
11562306a36Sopenharmony_ci *
11662306a36Sopenharmony_ci * Set up the DMA ring buffer and enable it. (r6xx-evergreen).
11762306a36Sopenharmony_ci * Returns 0 for success, error for failure.
11862306a36Sopenharmony_ci */
11962306a36Sopenharmony_ciint r600_dma_resume(struct radeon_device *rdev)
12062306a36Sopenharmony_ci{
12162306a36Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[R600_RING_TYPE_DMA_INDEX];
12262306a36Sopenharmony_ci	u32 rb_cntl, dma_cntl, ib_cntl;
12362306a36Sopenharmony_ci	u32 rb_bufsz;
12462306a36Sopenharmony_ci	int r;
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	WREG32(DMA_SEM_INCOMPLETE_TIMER_CNTL, 0);
12762306a36Sopenharmony_ci	WREG32(DMA_SEM_WAIT_FAIL_TIMER_CNTL, 0);
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	/* Set ring buffer size in dwords */
13062306a36Sopenharmony_ci	rb_bufsz = order_base_2(ring->ring_size / 4);
13162306a36Sopenharmony_ci	rb_cntl = rb_bufsz << 1;
13262306a36Sopenharmony_ci#ifdef __BIG_ENDIAN
13362306a36Sopenharmony_ci	rb_cntl |= DMA_RB_SWAP_ENABLE | DMA_RPTR_WRITEBACK_SWAP_ENABLE;
13462306a36Sopenharmony_ci#endif
13562306a36Sopenharmony_ci	WREG32(DMA_RB_CNTL, rb_cntl);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	/* Initialize the ring buffer's read and write pointers */
13862306a36Sopenharmony_ci	WREG32(DMA_RB_RPTR, 0);
13962306a36Sopenharmony_ci	WREG32(DMA_RB_WPTR, 0);
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	/* set the wb address whether it's enabled or not */
14262306a36Sopenharmony_ci	WREG32(DMA_RB_RPTR_ADDR_HI,
14362306a36Sopenharmony_ci	       upper_32_bits(rdev->wb.gpu_addr + R600_WB_DMA_RPTR_OFFSET) & 0xFF);
14462306a36Sopenharmony_ci	WREG32(DMA_RB_RPTR_ADDR_LO,
14562306a36Sopenharmony_ci	       ((rdev->wb.gpu_addr + R600_WB_DMA_RPTR_OFFSET) & 0xFFFFFFFC));
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	if (rdev->wb.enabled)
14862306a36Sopenharmony_ci		rb_cntl |= DMA_RPTR_WRITEBACK_ENABLE;
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	WREG32(DMA_RB_BASE, ring->gpu_addr >> 8);
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	/* enable DMA IBs */
15362306a36Sopenharmony_ci	ib_cntl = DMA_IB_ENABLE;
15462306a36Sopenharmony_ci#ifdef __BIG_ENDIAN
15562306a36Sopenharmony_ci	ib_cntl |= DMA_IB_SWAP_ENABLE;
15662306a36Sopenharmony_ci#endif
15762306a36Sopenharmony_ci	WREG32(DMA_IB_CNTL, ib_cntl);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci	dma_cntl = RREG32(DMA_CNTL);
16062306a36Sopenharmony_ci	dma_cntl &= ~CTXEMPTY_INT_ENABLE;
16162306a36Sopenharmony_ci	WREG32(DMA_CNTL, dma_cntl);
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_ci	if (rdev->family >= CHIP_RV770)
16462306a36Sopenharmony_ci		WREG32(DMA_MODE, 1);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	ring->wptr = 0;
16762306a36Sopenharmony_ci	WREG32(DMA_RB_WPTR, ring->wptr << 2);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	WREG32(DMA_RB_CNTL, rb_cntl | DMA_RB_ENABLE);
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	ring->ready = true;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	r = radeon_ring_test(rdev, R600_RING_TYPE_DMA_INDEX, ring);
17462306a36Sopenharmony_ci	if (r) {
17562306a36Sopenharmony_ci		ring->ready = false;
17662306a36Sopenharmony_ci		return r;
17762306a36Sopenharmony_ci	}
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	if (rdev->asic->copy.copy_ring_index == R600_RING_TYPE_DMA_INDEX)
18062306a36Sopenharmony_ci		radeon_ttm_set_active_vram_size(rdev, rdev->mc.real_vram_size);
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	return 0;
18362306a36Sopenharmony_ci}
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci/**
18662306a36Sopenharmony_ci * r600_dma_fini - tear down the async dma engine
18762306a36Sopenharmony_ci *
18862306a36Sopenharmony_ci * @rdev: radeon_device pointer
18962306a36Sopenharmony_ci *
19062306a36Sopenharmony_ci * Stop the async dma engine and free the ring (r6xx-evergreen).
19162306a36Sopenharmony_ci */
19262306a36Sopenharmony_civoid r600_dma_fini(struct radeon_device *rdev)
19362306a36Sopenharmony_ci{
19462306a36Sopenharmony_ci	r600_dma_stop(rdev);
19562306a36Sopenharmony_ci	radeon_ring_fini(rdev, &rdev->ring[R600_RING_TYPE_DMA_INDEX]);
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci/**
19962306a36Sopenharmony_ci * r600_dma_is_lockup - Check if the DMA engine is locked up
20062306a36Sopenharmony_ci *
20162306a36Sopenharmony_ci * @rdev: radeon_device pointer
20262306a36Sopenharmony_ci * @ring: radeon_ring structure holding ring information
20362306a36Sopenharmony_ci *
20462306a36Sopenharmony_ci * Check if the async DMA engine is locked up.
20562306a36Sopenharmony_ci * Returns true if the engine appears to be locked up, false if not.
20662306a36Sopenharmony_ci */
20762306a36Sopenharmony_cibool r600_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	u32 reset_mask = r600_gpu_check_soft_reset(rdev);
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	if (!(reset_mask & RADEON_RESET_DMA)) {
21262306a36Sopenharmony_ci		radeon_ring_lockup_update(rdev, ring);
21362306a36Sopenharmony_ci		return false;
21462306a36Sopenharmony_ci	}
21562306a36Sopenharmony_ci	return radeon_ring_test_lockup(rdev, ring);
21662306a36Sopenharmony_ci}
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci/**
22062306a36Sopenharmony_ci * r600_dma_ring_test - simple async dma engine test
22162306a36Sopenharmony_ci *
22262306a36Sopenharmony_ci * @rdev: radeon_device pointer
22362306a36Sopenharmony_ci * @ring: radeon_ring structure holding ring information
22462306a36Sopenharmony_ci *
22562306a36Sopenharmony_ci * Test the DMA engine by writing using it to write an
22662306a36Sopenharmony_ci * value to memory. (r6xx-SI).
22762306a36Sopenharmony_ci * Returns 0 for success, error for failure.
22862306a36Sopenharmony_ci */
22962306a36Sopenharmony_ciint r600_dma_ring_test(struct radeon_device *rdev,
23062306a36Sopenharmony_ci		       struct radeon_ring *ring)
23162306a36Sopenharmony_ci{
23262306a36Sopenharmony_ci	unsigned i;
23362306a36Sopenharmony_ci	int r;
23462306a36Sopenharmony_ci	unsigned index;
23562306a36Sopenharmony_ci	u32 tmp;
23662306a36Sopenharmony_ci	u64 gpu_addr;
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	if (ring->idx == R600_RING_TYPE_DMA_INDEX)
23962306a36Sopenharmony_ci		index = R600_WB_DMA_RING_TEST_OFFSET;
24062306a36Sopenharmony_ci	else
24162306a36Sopenharmony_ci		index = CAYMAN_WB_DMA1_RING_TEST_OFFSET;
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	gpu_addr = rdev->wb.gpu_addr + index;
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	tmp = 0xCAFEDEAD;
24662306a36Sopenharmony_ci	rdev->wb.wb[index/4] = cpu_to_le32(tmp);
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	r = radeon_ring_lock(rdev, ring, 4);
24962306a36Sopenharmony_ci	if (r) {
25062306a36Sopenharmony_ci		DRM_ERROR("radeon: dma failed to lock ring %d (%d).\n", ring->idx, r);
25162306a36Sopenharmony_ci		return r;
25262306a36Sopenharmony_ci	}
25362306a36Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1));
25462306a36Sopenharmony_ci	radeon_ring_write(ring, lower_32_bits(gpu_addr));
25562306a36Sopenharmony_ci	radeon_ring_write(ring, upper_32_bits(gpu_addr) & 0xff);
25662306a36Sopenharmony_ci	radeon_ring_write(ring, 0xDEADBEEF);
25762306a36Sopenharmony_ci	radeon_ring_unlock_commit(rdev, ring, false);
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
26062306a36Sopenharmony_ci		tmp = le32_to_cpu(rdev->wb.wb[index/4]);
26162306a36Sopenharmony_ci		if (tmp == 0xDEADBEEF)
26262306a36Sopenharmony_ci			break;
26362306a36Sopenharmony_ci		udelay(1);
26462306a36Sopenharmony_ci	}
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci	if (i < rdev->usec_timeout) {
26762306a36Sopenharmony_ci		DRM_INFO("ring test on %d succeeded in %d usecs\n", ring->idx, i);
26862306a36Sopenharmony_ci	} else {
26962306a36Sopenharmony_ci		DRM_ERROR("radeon: ring %d test failed (0x%08X)\n",
27062306a36Sopenharmony_ci			  ring->idx, tmp);
27162306a36Sopenharmony_ci		r = -EINVAL;
27262306a36Sopenharmony_ci	}
27362306a36Sopenharmony_ci	return r;
27462306a36Sopenharmony_ci}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci/**
27762306a36Sopenharmony_ci * r600_dma_fence_ring_emit - emit a fence on the DMA ring
27862306a36Sopenharmony_ci *
27962306a36Sopenharmony_ci * @rdev: radeon_device pointer
28062306a36Sopenharmony_ci * @fence: radeon fence object
28162306a36Sopenharmony_ci *
28262306a36Sopenharmony_ci * Add a DMA fence packet to the ring to write
28362306a36Sopenharmony_ci * the fence seq number and DMA trap packet to generate
28462306a36Sopenharmony_ci * an interrupt if needed (r6xx-r7xx).
28562306a36Sopenharmony_ci */
28662306a36Sopenharmony_civoid r600_dma_fence_ring_emit(struct radeon_device *rdev,
28762306a36Sopenharmony_ci			      struct radeon_fence *fence)
28862306a36Sopenharmony_ci{
28962306a36Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[fence->ring];
29062306a36Sopenharmony_ci	u64 addr = rdev->fence_drv[fence->ring].gpu_addr;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	/* write the fence */
29362306a36Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_FENCE, 0, 0, 0));
29462306a36Sopenharmony_ci	radeon_ring_write(ring, addr & 0xfffffffc);
29562306a36Sopenharmony_ci	radeon_ring_write(ring, (upper_32_bits(addr) & 0xff));
29662306a36Sopenharmony_ci	radeon_ring_write(ring, lower_32_bits(fence->seq));
29762306a36Sopenharmony_ci	/* generate an interrupt */
29862306a36Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_TRAP, 0, 0, 0));
29962306a36Sopenharmony_ci}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci/**
30262306a36Sopenharmony_ci * r600_dma_semaphore_ring_emit - emit a semaphore on the dma ring
30362306a36Sopenharmony_ci *
30462306a36Sopenharmony_ci * @rdev: radeon_device pointer
30562306a36Sopenharmony_ci * @ring: radeon_ring structure holding ring information
30662306a36Sopenharmony_ci * @semaphore: radeon semaphore object
30762306a36Sopenharmony_ci * @emit_wait: wait or signal semaphore
30862306a36Sopenharmony_ci *
30962306a36Sopenharmony_ci * Add a DMA semaphore packet to the ring wait on or signal
31062306a36Sopenharmony_ci * other rings (r6xx-SI).
31162306a36Sopenharmony_ci */
31262306a36Sopenharmony_cibool r600_dma_semaphore_ring_emit(struct radeon_device *rdev,
31362306a36Sopenharmony_ci				  struct radeon_ring *ring,
31462306a36Sopenharmony_ci				  struct radeon_semaphore *semaphore,
31562306a36Sopenharmony_ci				  bool emit_wait)
31662306a36Sopenharmony_ci{
31762306a36Sopenharmony_ci	u64 addr = semaphore->gpu_addr;
31862306a36Sopenharmony_ci	u32 s = emit_wait ? 0 : 1;
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SEMAPHORE, 0, s, 0));
32162306a36Sopenharmony_ci	radeon_ring_write(ring, addr & 0xfffffffc);
32262306a36Sopenharmony_ci	radeon_ring_write(ring, upper_32_bits(addr) & 0xff);
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	return true;
32562306a36Sopenharmony_ci}
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci/**
32862306a36Sopenharmony_ci * r600_dma_ib_test - test an IB on the DMA engine
32962306a36Sopenharmony_ci *
33062306a36Sopenharmony_ci * @rdev: radeon_device pointer
33162306a36Sopenharmony_ci * @ring: radeon_ring structure holding ring information
33262306a36Sopenharmony_ci *
33362306a36Sopenharmony_ci * Test a simple IB in the DMA ring (r6xx-SI).
33462306a36Sopenharmony_ci * Returns 0 on success, error on failure.
33562306a36Sopenharmony_ci */
33662306a36Sopenharmony_ciint r600_dma_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
33762306a36Sopenharmony_ci{
33862306a36Sopenharmony_ci	struct radeon_ib ib;
33962306a36Sopenharmony_ci	unsigned i;
34062306a36Sopenharmony_ci	unsigned index;
34162306a36Sopenharmony_ci	int r;
34262306a36Sopenharmony_ci	u32 tmp = 0;
34362306a36Sopenharmony_ci	u64 gpu_addr;
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	if (ring->idx == R600_RING_TYPE_DMA_INDEX)
34662306a36Sopenharmony_ci		index = R600_WB_DMA_RING_TEST_OFFSET;
34762306a36Sopenharmony_ci	else
34862306a36Sopenharmony_ci		index = CAYMAN_WB_DMA1_RING_TEST_OFFSET;
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_ci	gpu_addr = rdev->wb.gpu_addr + index;
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	r = radeon_ib_get(rdev, ring->idx, &ib, NULL, 256);
35362306a36Sopenharmony_ci	if (r) {
35462306a36Sopenharmony_ci		DRM_ERROR("radeon: failed to get ib (%d).\n", r);
35562306a36Sopenharmony_ci		return r;
35662306a36Sopenharmony_ci	}
35762306a36Sopenharmony_ci
35862306a36Sopenharmony_ci	ib.ptr[0] = DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1);
35962306a36Sopenharmony_ci	ib.ptr[1] = lower_32_bits(gpu_addr);
36062306a36Sopenharmony_ci	ib.ptr[2] = upper_32_bits(gpu_addr) & 0xff;
36162306a36Sopenharmony_ci	ib.ptr[3] = 0xDEADBEEF;
36262306a36Sopenharmony_ci	ib.length_dw = 4;
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci	r = radeon_ib_schedule(rdev, &ib, NULL, false);
36562306a36Sopenharmony_ci	if (r) {
36662306a36Sopenharmony_ci		radeon_ib_free(rdev, &ib);
36762306a36Sopenharmony_ci		DRM_ERROR("radeon: failed to schedule ib (%d).\n", r);
36862306a36Sopenharmony_ci		return r;
36962306a36Sopenharmony_ci	}
37062306a36Sopenharmony_ci	r = radeon_fence_wait_timeout(ib.fence, false, usecs_to_jiffies(
37162306a36Sopenharmony_ci		RADEON_USEC_IB_TEST_TIMEOUT));
37262306a36Sopenharmony_ci	if (r < 0) {
37362306a36Sopenharmony_ci		DRM_ERROR("radeon: fence wait failed (%d).\n", r);
37462306a36Sopenharmony_ci		return r;
37562306a36Sopenharmony_ci	} else if (r == 0) {
37662306a36Sopenharmony_ci		DRM_ERROR("radeon: fence wait timed out.\n");
37762306a36Sopenharmony_ci		return -ETIMEDOUT;
37862306a36Sopenharmony_ci	}
37962306a36Sopenharmony_ci	r = 0;
38062306a36Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
38162306a36Sopenharmony_ci		tmp = le32_to_cpu(rdev->wb.wb[index/4]);
38262306a36Sopenharmony_ci		if (tmp == 0xDEADBEEF)
38362306a36Sopenharmony_ci			break;
38462306a36Sopenharmony_ci		udelay(1);
38562306a36Sopenharmony_ci	}
38662306a36Sopenharmony_ci	if (i < rdev->usec_timeout) {
38762306a36Sopenharmony_ci		DRM_INFO("ib test on ring %d succeeded in %u usecs\n", ib.fence->ring, i);
38862306a36Sopenharmony_ci	} else {
38962306a36Sopenharmony_ci		DRM_ERROR("radeon: ib test failed (0x%08X)\n", tmp);
39062306a36Sopenharmony_ci		r = -EINVAL;
39162306a36Sopenharmony_ci	}
39262306a36Sopenharmony_ci	radeon_ib_free(rdev, &ib);
39362306a36Sopenharmony_ci	return r;
39462306a36Sopenharmony_ci}
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci/**
39762306a36Sopenharmony_ci * r600_dma_ring_ib_execute - Schedule an IB on the DMA engine
39862306a36Sopenharmony_ci *
39962306a36Sopenharmony_ci * @rdev: radeon_device pointer
40062306a36Sopenharmony_ci * @ib: IB object to schedule
40162306a36Sopenharmony_ci *
40262306a36Sopenharmony_ci * Schedule an IB in the DMA ring (r6xx-r7xx).
40362306a36Sopenharmony_ci */
40462306a36Sopenharmony_civoid r600_dma_ring_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
40562306a36Sopenharmony_ci{
40662306a36Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ib->ring];
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci	if (rdev->wb.enabled) {
40962306a36Sopenharmony_ci		u32 next_rptr = ring->wptr + 4;
41062306a36Sopenharmony_ci		while ((next_rptr & 7) != 5)
41162306a36Sopenharmony_ci			next_rptr++;
41262306a36Sopenharmony_ci		next_rptr += 3;
41362306a36Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 1));
41462306a36Sopenharmony_ci		radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
41562306a36Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xff);
41662306a36Sopenharmony_ci		radeon_ring_write(ring, next_rptr);
41762306a36Sopenharmony_ci	}
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci	/* The indirect buffer packet must end on an 8 DW boundary in the DMA ring.
42062306a36Sopenharmony_ci	 * Pad as necessary with NOPs.
42162306a36Sopenharmony_ci	 */
42262306a36Sopenharmony_ci	while ((ring->wptr & 7) != 5)
42362306a36Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_NOP, 0, 0, 0));
42462306a36Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_INDIRECT_BUFFER, 0, 0, 0));
42562306a36Sopenharmony_ci	radeon_ring_write(ring, (ib->gpu_addr & 0xFFFFFFE0));
42662306a36Sopenharmony_ci	radeon_ring_write(ring, (ib->length_dw << 16) | (upper_32_bits(ib->gpu_addr) & 0xFF));
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci}
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci/**
43162306a36Sopenharmony_ci * r600_copy_dma - copy pages using the DMA engine
43262306a36Sopenharmony_ci *
43362306a36Sopenharmony_ci * @rdev: radeon_device pointer
43462306a36Sopenharmony_ci * @src_offset: src GPU address
43562306a36Sopenharmony_ci * @dst_offset: dst GPU address
43662306a36Sopenharmony_ci * @num_gpu_pages: number of GPU pages to xfer
43762306a36Sopenharmony_ci * @resv: reservation object to sync to
43862306a36Sopenharmony_ci *
43962306a36Sopenharmony_ci * Copy GPU paging using the DMA engine (r6xx).
44062306a36Sopenharmony_ci * Used by the radeon ttm implementation to move pages if
44162306a36Sopenharmony_ci * registered as the asic copy callback.
44262306a36Sopenharmony_ci */
44362306a36Sopenharmony_cistruct radeon_fence *r600_copy_dma(struct radeon_device *rdev,
44462306a36Sopenharmony_ci				   uint64_t src_offset, uint64_t dst_offset,
44562306a36Sopenharmony_ci				   unsigned num_gpu_pages,
44662306a36Sopenharmony_ci				   struct dma_resv *resv)
44762306a36Sopenharmony_ci{
44862306a36Sopenharmony_ci	struct radeon_fence *fence;
44962306a36Sopenharmony_ci	struct radeon_sync sync;
45062306a36Sopenharmony_ci	int ring_index = rdev->asic->copy.dma_ring_index;
45162306a36Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ring_index];
45262306a36Sopenharmony_ci	u32 size_in_dw, cur_size_in_dw;
45362306a36Sopenharmony_ci	int i, num_loops;
45462306a36Sopenharmony_ci	int r = 0;
45562306a36Sopenharmony_ci
45662306a36Sopenharmony_ci	radeon_sync_create(&sync);
45762306a36Sopenharmony_ci
45862306a36Sopenharmony_ci	size_in_dw = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT) / 4;
45962306a36Sopenharmony_ci	num_loops = DIV_ROUND_UP(size_in_dw, 0xFFFE);
46062306a36Sopenharmony_ci	r = radeon_ring_lock(rdev, ring, num_loops * 4 + 8);
46162306a36Sopenharmony_ci	if (r) {
46262306a36Sopenharmony_ci		DRM_ERROR("radeon: moving bo (%d).\n", r);
46362306a36Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
46462306a36Sopenharmony_ci		return ERR_PTR(r);
46562306a36Sopenharmony_ci	}
46662306a36Sopenharmony_ci
46762306a36Sopenharmony_ci	radeon_sync_resv(rdev, &sync, resv, false);
46862306a36Sopenharmony_ci	radeon_sync_rings(rdev, &sync, ring->idx);
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci	for (i = 0; i < num_loops; i++) {
47162306a36Sopenharmony_ci		cur_size_in_dw = size_in_dw;
47262306a36Sopenharmony_ci		if (cur_size_in_dw > 0xFFFE)
47362306a36Sopenharmony_ci			cur_size_in_dw = 0xFFFE;
47462306a36Sopenharmony_ci		size_in_dw -= cur_size_in_dw;
47562306a36Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 0, 0, cur_size_in_dw));
47662306a36Sopenharmony_ci		radeon_ring_write(ring, dst_offset & 0xfffffffc);
47762306a36Sopenharmony_ci		radeon_ring_write(ring, src_offset & 0xfffffffc);
47862306a36Sopenharmony_ci		radeon_ring_write(ring, (((upper_32_bits(dst_offset) & 0xff) << 16) |
47962306a36Sopenharmony_ci					 (upper_32_bits(src_offset) & 0xff)));
48062306a36Sopenharmony_ci		src_offset += cur_size_in_dw * 4;
48162306a36Sopenharmony_ci		dst_offset += cur_size_in_dw * 4;
48262306a36Sopenharmony_ci	}
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci	r = radeon_fence_emit(rdev, &fence, ring->idx);
48562306a36Sopenharmony_ci	if (r) {
48662306a36Sopenharmony_ci		radeon_ring_unlock_undo(rdev, ring);
48762306a36Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
48862306a36Sopenharmony_ci		return ERR_PTR(r);
48962306a36Sopenharmony_ci	}
49062306a36Sopenharmony_ci
49162306a36Sopenharmony_ci	radeon_ring_unlock_commit(rdev, ring, false);
49262306a36Sopenharmony_ci	radeon_sync_free(rdev, &sync, fence);
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci	return fence;
49562306a36Sopenharmony_ci}
496