18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2010 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Authors: Alex Deucher
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "radeon.h"
268c2ecf20Sopenharmony_ci#include "radeon_asic.h"
278c2ecf20Sopenharmony_ci#include "evergreend.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciu32 evergreen_gpu_check_soft_reset(struct radeon_device *rdev);
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/**
328c2ecf20Sopenharmony_ci * evergreen_dma_fence_ring_emit - emit a fence on the DMA ring
338c2ecf20Sopenharmony_ci *
348c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
358c2ecf20Sopenharmony_ci * @fence: radeon fence object
368c2ecf20Sopenharmony_ci *
378c2ecf20Sopenharmony_ci * Add a DMA fence packet to the ring to write
388c2ecf20Sopenharmony_ci * the fence seq number and DMA trap packet to generate
398c2ecf20Sopenharmony_ci * an interrupt if needed (evergreen-SI).
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_civoid evergreen_dma_fence_ring_emit(struct radeon_device *rdev,
428c2ecf20Sopenharmony_ci				   struct radeon_fence *fence)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[fence->ring];
458c2ecf20Sopenharmony_ci	u64 addr = rdev->fence_drv[fence->ring].gpu_addr;
468c2ecf20Sopenharmony_ci	/* write the fence */
478c2ecf20Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_FENCE, 0, 0));
488c2ecf20Sopenharmony_ci	radeon_ring_write(ring, addr & 0xfffffffc);
498c2ecf20Sopenharmony_ci	radeon_ring_write(ring, (upper_32_bits(addr) & 0xff));
508c2ecf20Sopenharmony_ci	radeon_ring_write(ring, fence->seq);
518c2ecf20Sopenharmony_ci	/* generate an interrupt */
528c2ecf20Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_TRAP, 0, 0));
538c2ecf20Sopenharmony_ci	/* flush HDP */
548c2ecf20Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0));
558c2ecf20Sopenharmony_ci	radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2));
568c2ecf20Sopenharmony_ci	radeon_ring_write(ring, 1);
578c2ecf20Sopenharmony_ci}
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/**
608c2ecf20Sopenharmony_ci * evergreen_dma_ring_ib_execute - schedule an IB on the DMA engine
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
638c2ecf20Sopenharmony_ci * @ib: IB object to schedule
648c2ecf20Sopenharmony_ci *
658c2ecf20Sopenharmony_ci * Schedule an IB in the DMA ring (evergreen).
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_civoid evergreen_dma_ring_ib_execute(struct radeon_device *rdev,
688c2ecf20Sopenharmony_ci				   struct radeon_ib *ib)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ib->ring];
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if (rdev->wb.enabled) {
738c2ecf20Sopenharmony_ci		u32 next_rptr = ring->wptr + 4;
748c2ecf20Sopenharmony_ci		while ((next_rptr & 7) != 5)
758c2ecf20Sopenharmony_ci			next_rptr++;
768c2ecf20Sopenharmony_ci		next_rptr += 3;
778c2ecf20Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_WRITE, 0, 1));
788c2ecf20Sopenharmony_ci		radeon_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
798c2ecf20Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xff);
808c2ecf20Sopenharmony_ci		radeon_ring_write(ring, next_rptr);
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/* The indirect buffer packet must end on an 8 DW boundary in the DMA ring.
848c2ecf20Sopenharmony_ci	 * Pad as necessary with NOPs.
858c2ecf20Sopenharmony_ci	 */
868c2ecf20Sopenharmony_ci	while ((ring->wptr & 7) != 5)
878c2ecf20Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_NOP, 0, 0));
888c2ecf20Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_INDIRECT_BUFFER, 0, 0));
898c2ecf20Sopenharmony_ci	radeon_ring_write(ring, (ib->gpu_addr & 0xFFFFFFE0));
908c2ecf20Sopenharmony_ci	radeon_ring_write(ring, (ib->length_dw << 12) | (upper_32_bits(ib->gpu_addr) & 0xFF));
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/**
958c2ecf20Sopenharmony_ci * evergreen_copy_dma - copy pages using the DMA engine
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
988c2ecf20Sopenharmony_ci * @src_offset: src GPU address
998c2ecf20Sopenharmony_ci * @dst_offset: dst GPU address
1008c2ecf20Sopenharmony_ci * @num_gpu_pages: number of GPU pages to xfer
1018c2ecf20Sopenharmony_ci * @fence: radeon fence object
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * Copy GPU paging using the DMA engine (evergreen-cayman).
1048c2ecf20Sopenharmony_ci * Used by the radeon ttm implementation to move pages if
1058c2ecf20Sopenharmony_ci * registered as the asic copy callback.
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_cistruct radeon_fence *evergreen_copy_dma(struct radeon_device *rdev,
1088c2ecf20Sopenharmony_ci					uint64_t src_offset,
1098c2ecf20Sopenharmony_ci					uint64_t dst_offset,
1108c2ecf20Sopenharmony_ci					unsigned num_gpu_pages,
1118c2ecf20Sopenharmony_ci					struct dma_resv *resv)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct radeon_fence *fence;
1148c2ecf20Sopenharmony_ci	struct radeon_sync sync;
1158c2ecf20Sopenharmony_ci	int ring_index = rdev->asic->copy.dma_ring_index;
1168c2ecf20Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ring_index];
1178c2ecf20Sopenharmony_ci	u32 size_in_dw, cur_size_in_dw;
1188c2ecf20Sopenharmony_ci	int i, num_loops;
1198c2ecf20Sopenharmony_ci	int r = 0;
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	radeon_sync_create(&sync);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	size_in_dw = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT) / 4;
1248c2ecf20Sopenharmony_ci	num_loops = DIV_ROUND_UP(size_in_dw, 0xfffff);
1258c2ecf20Sopenharmony_ci	r = radeon_ring_lock(rdev, ring, num_loops * 5 + 11);
1268c2ecf20Sopenharmony_ci	if (r) {
1278c2ecf20Sopenharmony_ci		DRM_ERROR("radeon: moving bo (%d).\n", r);
1288c2ecf20Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
1298c2ecf20Sopenharmony_ci		return ERR_PTR(r);
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	radeon_sync_resv(rdev, &sync, resv, false);
1338c2ecf20Sopenharmony_ci	radeon_sync_rings(rdev, &sync, ring->idx);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	for (i = 0; i < num_loops; i++) {
1368c2ecf20Sopenharmony_ci		cur_size_in_dw = size_in_dw;
1378c2ecf20Sopenharmony_ci		if (cur_size_in_dw > 0xFFFFF)
1388c2ecf20Sopenharmony_ci			cur_size_in_dw = 0xFFFFF;
1398c2ecf20Sopenharmony_ci		size_in_dw -= cur_size_in_dw;
1408c2ecf20Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 0, cur_size_in_dw));
1418c2ecf20Sopenharmony_ci		radeon_ring_write(ring, dst_offset & 0xfffffffc);
1428c2ecf20Sopenharmony_ci		radeon_ring_write(ring, src_offset & 0xfffffffc);
1438c2ecf20Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(dst_offset) & 0xff);
1448c2ecf20Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(src_offset) & 0xff);
1458c2ecf20Sopenharmony_ci		src_offset += cur_size_in_dw * 4;
1468c2ecf20Sopenharmony_ci		dst_offset += cur_size_in_dw * 4;
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	r = radeon_fence_emit(rdev, &fence, ring->idx);
1508c2ecf20Sopenharmony_ci	if (r) {
1518c2ecf20Sopenharmony_ci		radeon_ring_unlock_undo(rdev, ring);
1528c2ecf20Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
1538c2ecf20Sopenharmony_ci		return ERR_PTR(r);
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	radeon_ring_unlock_commit(rdev, ring, false);
1578c2ecf20Sopenharmony_ci	radeon_sync_free(rdev, &sync, fence);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return fence;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/**
1638c2ecf20Sopenharmony_ci * evergreen_dma_is_lockup - Check if the DMA engine is locked up
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
1668c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
1678c2ecf20Sopenharmony_ci *
1688c2ecf20Sopenharmony_ci * Check if the async DMA engine is locked up.
1698c2ecf20Sopenharmony_ci * Returns true if the engine appears to be locked up, false if not.
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_cibool evergreen_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	u32 reset_mask = evergreen_gpu_check_soft_reset(rdev);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	if (!(reset_mask & RADEON_RESET_DMA)) {
1768c2ecf20Sopenharmony_ci		radeon_ring_lockup_update(rdev, ring);
1778c2ecf20Sopenharmony_ci		return false;
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci	return radeon_ring_test_lockup(rdev, ring);
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci
183