162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2010 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 "evergreen.h"
2862306a36Sopenharmony_ci#include "evergreend.h"
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/**
3162306a36Sopenharmony_ci * evergreen_dma_fence_ring_emit - emit a fence on the DMA ring
3262306a36Sopenharmony_ci *
3362306a36Sopenharmony_ci * @rdev: radeon_device pointer
3462306a36Sopenharmony_ci * @fence: radeon fence object
3562306a36Sopenharmony_ci *
3662306a36Sopenharmony_ci * Add a DMA fence packet to the ring to write
3762306a36Sopenharmony_ci * the fence seq number and DMA trap packet to generate
3862306a36Sopenharmony_ci * an interrupt if needed (evergreen-SI).
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_civoid evergreen_dma_fence_ring_emit(struct radeon_device *rdev,
4162306a36Sopenharmony_ci				   struct radeon_fence *fence)
4262306a36Sopenharmony_ci{
4362306a36Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[fence->ring];
4462306a36Sopenharmony_ci	u64 addr = rdev->fence_drv[fence->ring].gpu_addr;
4562306a36Sopenharmony_ci	/* write the fence */
4662306a36Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_FENCE, 0, 0));
4762306a36Sopenharmony_ci	radeon_ring_write(ring, addr & 0xfffffffc);
4862306a36Sopenharmony_ci	radeon_ring_write(ring, (upper_32_bits(addr) & 0xff));
4962306a36Sopenharmony_ci	radeon_ring_write(ring, fence->seq);
5062306a36Sopenharmony_ci	/* generate an interrupt */
5162306a36Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_TRAP, 0, 0));
5262306a36Sopenharmony_ci	/* flush HDP */
5362306a36Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0));
5462306a36Sopenharmony_ci	radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2));
5562306a36Sopenharmony_ci	radeon_ring_write(ring, 1);
5662306a36Sopenharmony_ci}
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/**
5962306a36Sopenharmony_ci * evergreen_dma_ring_ib_execute - schedule an IB on the DMA engine
6062306a36Sopenharmony_ci *
6162306a36Sopenharmony_ci * @rdev: radeon_device pointer
6262306a36Sopenharmony_ci * @ib: IB object to schedule
6362306a36Sopenharmony_ci *
6462306a36Sopenharmony_ci * Schedule an IB in the DMA ring (evergreen).
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_civoid evergreen_dma_ring_ib_execute(struct radeon_device *rdev,
6762306a36Sopenharmony_ci				   struct radeon_ib *ib)
6862306a36Sopenharmony_ci{
6962306a36Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ib->ring];
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci	if (rdev->wb.enabled) {
7262306a36Sopenharmony_ci		u32 next_rptr = ring->wptr + 4;
7362306a36Sopenharmony_ci		while ((next_rptr & 7) != 5)
7462306a36Sopenharmony_ci			next_rptr++;
7562306a36Sopenharmony_ci		next_rptr += 3;
7662306a36Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 1));
7762306a36Sopenharmony_ci		radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
7862306a36Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xff);
7962306a36Sopenharmony_ci		radeon_ring_write(ring, next_rptr);
8062306a36Sopenharmony_ci	}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	/* The indirect buffer packet must end on an 8 DW boundary in the DMA ring.
8362306a36Sopenharmony_ci	 * Pad as necessary with NOPs.
8462306a36Sopenharmony_ci	 */
8562306a36Sopenharmony_ci	while ((ring->wptr & 7) != 5)
8662306a36Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_NOP, 0, 0));
8762306a36Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_INDIRECT_BUFFER, 0, 0));
8862306a36Sopenharmony_ci	radeon_ring_write(ring, (ib->gpu_addr & 0xFFFFFFE0));
8962306a36Sopenharmony_ci	radeon_ring_write(ring, (ib->length_dw << 12) | (upper_32_bits(ib->gpu_addr) & 0xFF));
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci}
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci/**
9462306a36Sopenharmony_ci * evergreen_copy_dma - copy pages using the DMA engine
9562306a36Sopenharmony_ci *
9662306a36Sopenharmony_ci * @rdev: radeon_device pointer
9762306a36Sopenharmony_ci * @src_offset: src GPU address
9862306a36Sopenharmony_ci * @dst_offset: dst GPU address
9962306a36Sopenharmony_ci * @num_gpu_pages: number of GPU pages to xfer
10062306a36Sopenharmony_ci * @resv: reservation object with embedded fence
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * Copy GPU paging using the DMA engine (evergreen-cayman).
10362306a36Sopenharmony_ci * Used by the radeon ttm implementation to move pages if
10462306a36Sopenharmony_ci * registered as the asic copy callback.
10562306a36Sopenharmony_ci */
10662306a36Sopenharmony_cistruct radeon_fence *evergreen_copy_dma(struct radeon_device *rdev,
10762306a36Sopenharmony_ci					uint64_t src_offset,
10862306a36Sopenharmony_ci					uint64_t dst_offset,
10962306a36Sopenharmony_ci					unsigned num_gpu_pages,
11062306a36Sopenharmony_ci					struct dma_resv *resv)
11162306a36Sopenharmony_ci{
11262306a36Sopenharmony_ci	struct radeon_fence *fence;
11362306a36Sopenharmony_ci	struct radeon_sync sync;
11462306a36Sopenharmony_ci	int ring_index = rdev->asic->copy.dma_ring_index;
11562306a36Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ring_index];
11662306a36Sopenharmony_ci	u32 size_in_dw, cur_size_in_dw;
11762306a36Sopenharmony_ci	int i, num_loops;
11862306a36Sopenharmony_ci	int r = 0;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	radeon_sync_create(&sync);
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci	size_in_dw = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT) / 4;
12362306a36Sopenharmony_ci	num_loops = DIV_ROUND_UP(size_in_dw, 0xfffff);
12462306a36Sopenharmony_ci	r = radeon_ring_lock(rdev, ring, num_loops * 5 + 11);
12562306a36Sopenharmony_ci	if (r) {
12662306a36Sopenharmony_ci		DRM_ERROR("radeon: moving bo (%d).\n", r);
12762306a36Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
12862306a36Sopenharmony_ci		return ERR_PTR(r);
12962306a36Sopenharmony_ci	}
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	radeon_sync_resv(rdev, &sync, resv, false);
13262306a36Sopenharmony_ci	radeon_sync_rings(rdev, &sync, ring->idx);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	for (i = 0; i < num_loops; i++) {
13562306a36Sopenharmony_ci		cur_size_in_dw = size_in_dw;
13662306a36Sopenharmony_ci		if (cur_size_in_dw > 0xFFFFF)
13762306a36Sopenharmony_ci			cur_size_in_dw = 0xFFFFF;
13862306a36Sopenharmony_ci		size_in_dw -= cur_size_in_dw;
13962306a36Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 0, cur_size_in_dw));
14062306a36Sopenharmony_ci		radeon_ring_write(ring, dst_offset & 0xfffffffc);
14162306a36Sopenharmony_ci		radeon_ring_write(ring, src_offset & 0xfffffffc);
14262306a36Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(dst_offset) & 0xff);
14362306a36Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(src_offset) & 0xff);
14462306a36Sopenharmony_ci		src_offset += cur_size_in_dw * 4;
14562306a36Sopenharmony_ci		dst_offset += cur_size_in_dw * 4;
14662306a36Sopenharmony_ci	}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	r = radeon_fence_emit(rdev, &fence, ring->idx);
14962306a36Sopenharmony_ci	if (r) {
15062306a36Sopenharmony_ci		radeon_ring_unlock_undo(rdev, ring);
15162306a36Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
15262306a36Sopenharmony_ci		return ERR_PTR(r);
15362306a36Sopenharmony_ci	}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	radeon_ring_unlock_commit(rdev, ring, false);
15662306a36Sopenharmony_ci	radeon_sync_free(rdev, &sync, fence);
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	return fence;
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci/**
16262306a36Sopenharmony_ci * evergreen_dma_is_lockup - Check if the DMA engine is locked up
16362306a36Sopenharmony_ci *
16462306a36Sopenharmony_ci * @rdev: radeon_device pointer
16562306a36Sopenharmony_ci * @ring: radeon_ring structure holding ring information
16662306a36Sopenharmony_ci *
16762306a36Sopenharmony_ci * Check if the async DMA engine is locked up.
16862306a36Sopenharmony_ci * Returns true if the engine appears to be locked up, false if not.
16962306a36Sopenharmony_ci */
17062306a36Sopenharmony_cibool evergreen_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
17162306a36Sopenharmony_ci{
17262306a36Sopenharmony_ci	u32 reset_mask = evergreen_gpu_check_soft_reset(rdev);
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	if (!(reset_mask & RADEON_RESET_DMA)) {
17562306a36Sopenharmony_ci		radeon_ring_lockup_update(rdev, ring);
17662306a36Sopenharmony_ci		return false;
17762306a36Sopenharmony_ci	}
17862306a36Sopenharmony_ci	return radeon_ring_test_lockup(rdev, ring);
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci
182