18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2013 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 "radeon_trace.h"
288c2ecf20Sopenharmony_ci#include "sid.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciu32 si_gpu_check_soft_reset(struct radeon_device *rdev);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/**
338c2ecf20Sopenharmony_ci * si_dma_is_lockup - Check if the DMA engine is locked up
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
368c2ecf20Sopenharmony_ci * @ring: radeon_ring structure holding ring information
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * Check if the async DMA engine is locked up.
398c2ecf20Sopenharmony_ci * Returns true if the engine appears to be locked up, false if not.
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_cibool si_dma_is_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	u32 reset_mask = si_gpu_check_soft_reset(rdev);
448c2ecf20Sopenharmony_ci	u32 mask;
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (ring->idx == R600_RING_TYPE_DMA_INDEX)
478c2ecf20Sopenharmony_ci		mask = RADEON_RESET_DMA;
488c2ecf20Sopenharmony_ci	else
498c2ecf20Sopenharmony_ci		mask = RADEON_RESET_DMA1;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	if (!(reset_mask & mask)) {
528c2ecf20Sopenharmony_ci		radeon_ring_lockup_update(rdev, ring);
538c2ecf20Sopenharmony_ci		return false;
548c2ecf20Sopenharmony_ci	}
558c2ecf20Sopenharmony_ci	return radeon_ring_test_lockup(rdev, ring);
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/**
598c2ecf20Sopenharmony_ci * si_dma_vm_copy_pages - update PTEs by copying them from the GART
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
628c2ecf20Sopenharmony_ci * @ib: indirect buffer to fill with commands
638c2ecf20Sopenharmony_ci * @pe: addr of the page entry
648c2ecf20Sopenharmony_ci * @src: src addr where to copy from
658c2ecf20Sopenharmony_ci * @count: number of page entries to update
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * Update PTEs by copying them from the GART using the DMA (SI).
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_civoid si_dma_vm_copy_pages(struct radeon_device *rdev,
708c2ecf20Sopenharmony_ci			  struct radeon_ib *ib,
718c2ecf20Sopenharmony_ci			  uint64_t pe, uint64_t src,
728c2ecf20Sopenharmony_ci			  unsigned count)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	while (count) {
758c2ecf20Sopenharmony_ci		unsigned bytes = count * 8;
768c2ecf20Sopenharmony_ci		if (bytes > 0xFFFF8)
778c2ecf20Sopenharmony_ci			bytes = 0xFFFF8;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_COPY,
808c2ecf20Sopenharmony_ci						      1, 0, 0, bytes);
818c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = lower_32_bits(pe);
828c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = lower_32_bits(src);
838c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
848c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = upper_32_bits(src) & 0xff;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci		pe += bytes;
878c2ecf20Sopenharmony_ci		src += bytes;
888c2ecf20Sopenharmony_ci		count -= bytes / 8;
898c2ecf20Sopenharmony_ci	}
908c2ecf20Sopenharmony_ci}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/**
938c2ecf20Sopenharmony_ci * si_dma_vm_write_pages - update PTEs by writing them manually
948c2ecf20Sopenharmony_ci *
958c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
968c2ecf20Sopenharmony_ci * @ib: indirect buffer to fill with commands
978c2ecf20Sopenharmony_ci * @pe: addr of the page entry
988c2ecf20Sopenharmony_ci * @addr: dst addr to write into pe
998c2ecf20Sopenharmony_ci * @count: number of page entries to update
1008c2ecf20Sopenharmony_ci * @incr: increase next addr by incr bytes
1018c2ecf20Sopenharmony_ci * @flags: access flags
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * Update PTEs by writing them manually using the DMA (SI).
1048c2ecf20Sopenharmony_ci */
1058c2ecf20Sopenharmony_civoid si_dma_vm_write_pages(struct radeon_device *rdev,
1068c2ecf20Sopenharmony_ci			   struct radeon_ib *ib,
1078c2ecf20Sopenharmony_ci			   uint64_t pe,
1088c2ecf20Sopenharmony_ci			   uint64_t addr, unsigned count,
1098c2ecf20Sopenharmony_ci			   uint32_t incr, uint32_t flags)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	uint64_t value;
1128c2ecf20Sopenharmony_ci	unsigned ndw;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	while (count) {
1158c2ecf20Sopenharmony_ci		ndw = count * 2;
1168c2ecf20Sopenharmony_ci		if (ndw > 0xFFFFE)
1178c2ecf20Sopenharmony_ci			ndw = 0xFFFFE;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci		/* for non-physically contiguous pages (system) */
1208c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = DMA_PACKET(DMA_PACKET_WRITE, 0, 0, 0, ndw);
1218c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = pe;
1228c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
1238c2ecf20Sopenharmony_ci		for (; ndw > 0; ndw -= 2, --count, pe += 8) {
1248c2ecf20Sopenharmony_ci			if (flags & R600_PTE_SYSTEM) {
1258c2ecf20Sopenharmony_ci				value = radeon_vm_map_gart(rdev, addr);
1268c2ecf20Sopenharmony_ci			} else if (flags & R600_PTE_VALID) {
1278c2ecf20Sopenharmony_ci				value = addr;
1288c2ecf20Sopenharmony_ci			} else {
1298c2ecf20Sopenharmony_ci				value = 0;
1308c2ecf20Sopenharmony_ci			}
1318c2ecf20Sopenharmony_ci			addr += incr;
1328c2ecf20Sopenharmony_ci			value |= flags;
1338c2ecf20Sopenharmony_ci			ib->ptr[ib->length_dw++] = value;
1348c2ecf20Sopenharmony_ci			ib->ptr[ib->length_dw++] = upper_32_bits(value);
1358c2ecf20Sopenharmony_ci		}
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/**
1408c2ecf20Sopenharmony_ci * si_dma_vm_set_pages - update the page tables using the DMA
1418c2ecf20Sopenharmony_ci *
1428c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
1438c2ecf20Sopenharmony_ci * @ib: indirect buffer to fill with commands
1448c2ecf20Sopenharmony_ci * @pe: addr of the page entry
1458c2ecf20Sopenharmony_ci * @addr: dst addr to write into pe
1468c2ecf20Sopenharmony_ci * @count: number of page entries to update
1478c2ecf20Sopenharmony_ci * @incr: increase next addr by incr bytes
1488c2ecf20Sopenharmony_ci * @flags: access flags
1498c2ecf20Sopenharmony_ci *
1508c2ecf20Sopenharmony_ci * Update the page tables using the DMA (SI).
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_civoid si_dma_vm_set_pages(struct radeon_device *rdev,
1538c2ecf20Sopenharmony_ci			 struct radeon_ib *ib,
1548c2ecf20Sopenharmony_ci			 uint64_t pe,
1558c2ecf20Sopenharmony_ci			 uint64_t addr, unsigned count,
1568c2ecf20Sopenharmony_ci			 uint32_t incr, uint32_t flags)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	uint64_t value;
1598c2ecf20Sopenharmony_ci	unsigned ndw;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	while (count) {
1628c2ecf20Sopenharmony_ci		ndw = count * 2;
1638c2ecf20Sopenharmony_ci		if (ndw > 0xFFFFE)
1648c2ecf20Sopenharmony_ci			ndw = 0xFFFFE;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci		if (flags & R600_PTE_VALID)
1678c2ecf20Sopenharmony_ci			value = addr;
1688c2ecf20Sopenharmony_ci		else
1698c2ecf20Sopenharmony_ci			value = 0;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci		/* for physically contiguous pages (vram) */
1728c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = DMA_PTE_PDE_PACKET(ndw);
1738c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = pe; /* dst addr */
1748c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = upper_32_bits(pe) & 0xff;
1758c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = flags; /* mask */
1768c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = 0;
1778c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = value; /* value */
1788c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = upper_32_bits(value);
1798c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = incr; /* increment size */
1808c2ecf20Sopenharmony_ci		ib->ptr[ib->length_dw++] = 0;
1818c2ecf20Sopenharmony_ci		pe += ndw * 4;
1828c2ecf20Sopenharmony_ci		addr += (ndw / 2) * incr;
1838c2ecf20Sopenharmony_ci		count -= ndw / 2;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_civoid si_dma_vm_flush(struct radeon_device *rdev, struct radeon_ring *ring,
1888c2ecf20Sopenharmony_ci		     unsigned vm_id, uint64_t pd_addr)
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci{
1918c2ecf20Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0, 0));
1928c2ecf20Sopenharmony_ci	if (vm_id < 8) {
1938c2ecf20Sopenharmony_ci		radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT0_PAGE_TABLE_BASE_ADDR + (vm_id << 2)) >> 2));
1948c2ecf20Sopenharmony_ci	} else {
1958c2ecf20Sopenharmony_ci		radeon_ring_write(ring, (0xf << 16) | ((VM_CONTEXT8_PAGE_TABLE_BASE_ADDR + ((vm_id - 8) << 2)) >> 2));
1968c2ecf20Sopenharmony_ci	}
1978c2ecf20Sopenharmony_ci	radeon_ring_write(ring, pd_addr >> 12);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* flush hdp cache */
2008c2ecf20Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0, 0));
2018c2ecf20Sopenharmony_ci	radeon_ring_write(ring, (0xf << 16) | (HDP_MEM_COHERENCY_FLUSH_CNTL >> 2));
2028c2ecf20Sopenharmony_ci	radeon_ring_write(ring, 1);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	/* bits 0-7 are the VM contexts0-7 */
2058c2ecf20Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_SRBM_WRITE, 0, 0, 0, 0));
2068c2ecf20Sopenharmony_ci	radeon_ring_write(ring, (0xf << 16) | (VM_INVALIDATE_REQUEST >> 2));
2078c2ecf20Sopenharmony_ci	radeon_ring_write(ring, 1 << vm_id);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	/* wait for invalidate to complete */
2108c2ecf20Sopenharmony_ci	radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_POLL_REG_MEM, 0, 0, 0, 0));
2118c2ecf20Sopenharmony_ci	radeon_ring_write(ring, VM_INVALIDATE_REQUEST);
2128c2ecf20Sopenharmony_ci	radeon_ring_write(ring, 0xff << 16); /* retry */
2138c2ecf20Sopenharmony_ci	radeon_ring_write(ring, 1 << vm_id); /* mask */
2148c2ecf20Sopenharmony_ci	radeon_ring_write(ring, 0); /* value */
2158c2ecf20Sopenharmony_ci	radeon_ring_write(ring, (0 << 28) | 0x20); /* func(always) | poll interval */
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci/**
2198c2ecf20Sopenharmony_ci * si_copy_dma - copy pages using the DMA engine
2208c2ecf20Sopenharmony_ci *
2218c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
2228c2ecf20Sopenharmony_ci * @src_offset: src GPU address
2238c2ecf20Sopenharmony_ci * @dst_offset: dst GPU address
2248c2ecf20Sopenharmony_ci * @num_gpu_pages: number of GPU pages to xfer
2258c2ecf20Sopenharmony_ci * @resv: reservation object to sync to
2268c2ecf20Sopenharmony_ci *
2278c2ecf20Sopenharmony_ci * Copy GPU paging using the DMA engine (SI).
2288c2ecf20Sopenharmony_ci * Used by the radeon ttm implementation to move pages if
2298c2ecf20Sopenharmony_ci * registered as the asic copy callback.
2308c2ecf20Sopenharmony_ci */
2318c2ecf20Sopenharmony_cistruct radeon_fence *si_copy_dma(struct radeon_device *rdev,
2328c2ecf20Sopenharmony_ci				 uint64_t src_offset, uint64_t dst_offset,
2338c2ecf20Sopenharmony_ci				 unsigned num_gpu_pages,
2348c2ecf20Sopenharmony_ci				 struct dma_resv *resv)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	struct radeon_fence *fence;
2378c2ecf20Sopenharmony_ci	struct radeon_sync sync;
2388c2ecf20Sopenharmony_ci	int ring_index = rdev->asic->copy.dma_ring_index;
2398c2ecf20Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ring_index];
2408c2ecf20Sopenharmony_ci	u32 size_in_bytes, cur_size_in_bytes;
2418c2ecf20Sopenharmony_ci	int i, num_loops;
2428c2ecf20Sopenharmony_ci	int r = 0;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	radeon_sync_create(&sync);
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	size_in_bytes = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT);
2478c2ecf20Sopenharmony_ci	num_loops = DIV_ROUND_UP(size_in_bytes, 0xfffff);
2488c2ecf20Sopenharmony_ci	r = radeon_ring_lock(rdev, ring, num_loops * 5 + 11);
2498c2ecf20Sopenharmony_ci	if (r) {
2508c2ecf20Sopenharmony_ci		DRM_ERROR("radeon: moving bo (%d).\n", r);
2518c2ecf20Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
2528c2ecf20Sopenharmony_ci		return ERR_PTR(r);
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	radeon_sync_resv(rdev, &sync, resv, false);
2568c2ecf20Sopenharmony_ci	radeon_sync_rings(rdev, &sync, ring->idx);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	for (i = 0; i < num_loops; i++) {
2598c2ecf20Sopenharmony_ci		cur_size_in_bytes = size_in_bytes;
2608c2ecf20Sopenharmony_ci		if (cur_size_in_bytes > 0xFFFFF)
2618c2ecf20Sopenharmony_ci			cur_size_in_bytes = 0xFFFFF;
2628c2ecf20Sopenharmony_ci		size_in_bytes -= cur_size_in_bytes;
2638c2ecf20Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 1, 0, 0, cur_size_in_bytes));
2648c2ecf20Sopenharmony_ci		radeon_ring_write(ring, lower_32_bits(dst_offset));
2658c2ecf20Sopenharmony_ci		radeon_ring_write(ring, lower_32_bits(src_offset));
2668c2ecf20Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(dst_offset) & 0xff);
2678c2ecf20Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(src_offset) & 0xff);
2688c2ecf20Sopenharmony_ci		src_offset += cur_size_in_bytes;
2698c2ecf20Sopenharmony_ci		dst_offset += cur_size_in_bytes;
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	r = radeon_fence_emit(rdev, &fence, ring->idx);
2738c2ecf20Sopenharmony_ci	if (r) {
2748c2ecf20Sopenharmony_ci		radeon_ring_unlock_undo(rdev, ring);
2758c2ecf20Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
2768c2ecf20Sopenharmony_ci		return ERR_PTR(r);
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	radeon_ring_unlock_commit(rdev, ring, false);
2808c2ecf20Sopenharmony_ci	radeon_sync_free(rdev, &sync, fence);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	return fence;
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
285