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 "rv770d.h"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/**
3062306a36Sopenharmony_ci * rv770_copy_dma - copy pages using the DMA engine
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * @rdev: radeon_device pointer
3362306a36Sopenharmony_ci * @src_offset: src GPU address
3462306a36Sopenharmony_ci * @dst_offset: dst GPU address
3562306a36Sopenharmony_ci * @num_gpu_pages: number of GPU pages to xfer
3662306a36Sopenharmony_ci * @resv: reservation object to sync to
3762306a36Sopenharmony_ci *
3862306a36Sopenharmony_ci * Copy GPU paging using the DMA engine (r7xx).
3962306a36Sopenharmony_ci * Used by the radeon ttm implementation to move pages if
4062306a36Sopenharmony_ci * registered as the asic copy callback.
4162306a36Sopenharmony_ci */
4262306a36Sopenharmony_cistruct radeon_fence *rv770_copy_dma(struct radeon_device *rdev,
4362306a36Sopenharmony_ci				    uint64_t src_offset, uint64_t dst_offset,
4462306a36Sopenharmony_ci				    unsigned num_gpu_pages,
4562306a36Sopenharmony_ci				    struct dma_resv *resv)
4662306a36Sopenharmony_ci{
4762306a36Sopenharmony_ci	struct radeon_fence *fence;
4862306a36Sopenharmony_ci	struct radeon_sync sync;
4962306a36Sopenharmony_ci	int ring_index = rdev->asic->copy.dma_ring_index;
5062306a36Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ring_index];
5162306a36Sopenharmony_ci	u32 size_in_dw, cur_size_in_dw;
5262306a36Sopenharmony_ci	int i, num_loops;
5362306a36Sopenharmony_ci	int r = 0;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	radeon_sync_create(&sync);
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	size_in_dw = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT) / 4;
5862306a36Sopenharmony_ci	num_loops = DIV_ROUND_UP(size_in_dw, 0xFFFF);
5962306a36Sopenharmony_ci	r = radeon_ring_lock(rdev, ring, num_loops * 5 + 8);
6062306a36Sopenharmony_ci	if (r) {
6162306a36Sopenharmony_ci		DRM_ERROR("radeon: moving bo (%d).\n", r);
6262306a36Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
6362306a36Sopenharmony_ci		return ERR_PTR(r);
6462306a36Sopenharmony_ci	}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	radeon_sync_resv(rdev, &sync, resv, false);
6762306a36Sopenharmony_ci	radeon_sync_rings(rdev, &sync, ring->idx);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	for (i = 0; i < num_loops; i++) {
7062306a36Sopenharmony_ci		cur_size_in_dw = size_in_dw;
7162306a36Sopenharmony_ci		if (cur_size_in_dw > 0xFFFF)
7262306a36Sopenharmony_ci			cur_size_in_dw = 0xFFFF;
7362306a36Sopenharmony_ci		size_in_dw -= cur_size_in_dw;
7462306a36Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 0, 0, cur_size_in_dw));
7562306a36Sopenharmony_ci		radeon_ring_write(ring, dst_offset & 0xfffffffc);
7662306a36Sopenharmony_ci		radeon_ring_write(ring, src_offset & 0xfffffffc);
7762306a36Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(dst_offset) & 0xff);
7862306a36Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(src_offset) & 0xff);
7962306a36Sopenharmony_ci		src_offset += cur_size_in_dw * 4;
8062306a36Sopenharmony_ci		dst_offset += cur_size_in_dw * 4;
8162306a36Sopenharmony_ci	}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	r = radeon_fence_emit(rdev, &fence, ring->idx);
8462306a36Sopenharmony_ci	if (r) {
8562306a36Sopenharmony_ci		radeon_ring_unlock_undo(rdev, ring);
8662306a36Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
8762306a36Sopenharmony_ci		return ERR_PTR(r);
8862306a36Sopenharmony_ci	}
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	radeon_ring_unlock_commit(rdev, ring, false);
9162306a36Sopenharmony_ci	radeon_sync_free(rdev, &sync, fence);
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci	return fence;
9462306a36Sopenharmony_ci}
95