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 "rv770d.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/**
308c2ecf20Sopenharmony_ci * rv770_copy_dma - copy pages using the DMA engine
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * @rdev: radeon_device pointer
338c2ecf20Sopenharmony_ci * @src_offset: src GPU address
348c2ecf20Sopenharmony_ci * @dst_offset: dst GPU address
358c2ecf20Sopenharmony_ci * @num_gpu_pages: number of GPU pages to xfer
368c2ecf20Sopenharmony_ci * @resv: reservation object to sync to
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * Copy GPU paging using the DMA engine (r7xx).
398c2ecf20Sopenharmony_ci * Used by the radeon ttm implementation to move pages if
408c2ecf20Sopenharmony_ci * registered as the asic copy callback.
418c2ecf20Sopenharmony_ci */
428c2ecf20Sopenharmony_cistruct radeon_fence *rv770_copy_dma(struct radeon_device *rdev,
438c2ecf20Sopenharmony_ci				    uint64_t src_offset, uint64_t dst_offset,
448c2ecf20Sopenharmony_ci				    unsigned num_gpu_pages,
458c2ecf20Sopenharmony_ci				    struct dma_resv *resv)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct radeon_fence *fence;
488c2ecf20Sopenharmony_ci	struct radeon_sync sync;
498c2ecf20Sopenharmony_ci	int ring_index = rdev->asic->copy.dma_ring_index;
508c2ecf20Sopenharmony_ci	struct radeon_ring *ring = &rdev->ring[ring_index];
518c2ecf20Sopenharmony_ci	u32 size_in_dw, cur_size_in_dw;
528c2ecf20Sopenharmony_ci	int i, num_loops;
538c2ecf20Sopenharmony_ci	int r = 0;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	radeon_sync_create(&sync);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	size_in_dw = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT) / 4;
588c2ecf20Sopenharmony_ci	num_loops = DIV_ROUND_UP(size_in_dw, 0xFFFF);
598c2ecf20Sopenharmony_ci	r = radeon_ring_lock(rdev, ring, num_loops * 5 + 8);
608c2ecf20Sopenharmony_ci	if (r) {
618c2ecf20Sopenharmony_ci		DRM_ERROR("radeon: moving bo (%d).\n", r);
628c2ecf20Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
638c2ecf20Sopenharmony_ci		return ERR_PTR(r);
648c2ecf20Sopenharmony_ci	}
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	radeon_sync_resv(rdev, &sync, resv, false);
678c2ecf20Sopenharmony_ci	radeon_sync_rings(rdev, &sync, ring->idx);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	for (i = 0; i < num_loops; i++) {
708c2ecf20Sopenharmony_ci		cur_size_in_dw = size_in_dw;
718c2ecf20Sopenharmony_ci		if (cur_size_in_dw > 0xFFFF)
728c2ecf20Sopenharmony_ci			cur_size_in_dw = 0xFFFF;
738c2ecf20Sopenharmony_ci		size_in_dw -= cur_size_in_dw;
748c2ecf20Sopenharmony_ci		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 0, 0, cur_size_in_dw));
758c2ecf20Sopenharmony_ci		radeon_ring_write(ring, dst_offset & 0xfffffffc);
768c2ecf20Sopenharmony_ci		radeon_ring_write(ring, src_offset & 0xfffffffc);
778c2ecf20Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(dst_offset) & 0xff);
788c2ecf20Sopenharmony_ci		radeon_ring_write(ring, upper_32_bits(src_offset) & 0xff);
798c2ecf20Sopenharmony_ci		src_offset += cur_size_in_dw * 4;
808c2ecf20Sopenharmony_ci		dst_offset += cur_size_in_dw * 4;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	r = radeon_fence_emit(rdev, &fence, ring->idx);
848c2ecf20Sopenharmony_ci	if (r) {
858c2ecf20Sopenharmony_ci		radeon_ring_unlock_undo(rdev, ring);
868c2ecf20Sopenharmony_ci		radeon_sync_free(rdev, &sync, NULL);
878c2ecf20Sopenharmony_ci		return ERR_PTR(r);
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	radeon_ring_unlock_commit(rdev, ring, false);
918c2ecf20Sopenharmony_ci	radeon_sync_free(rdev, &sync, fence);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	return fence;
948c2ecf20Sopenharmony_ci}
95