xref: /dflybsd-src/sys/dev/drm/radeon/rv770_dma.c (revision 7dcf36dc33228b5b368783d7b6f7ada00ee671d6)
14cd92098Szrj /*
24cd92098Szrj  * Copyright 2013 Advanced Micro Devices, Inc.
34cd92098Szrj  *
44cd92098Szrj  * Permission is hereby granted, free of charge, to any person obtaining a
54cd92098Szrj  * copy of this software and associated documentation files (the "Software"),
64cd92098Szrj  * to deal in the Software without restriction, including without limitation
74cd92098Szrj  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84cd92098Szrj  * and/or sell copies of the Software, and to permit persons to whom the
94cd92098Szrj  * Software is furnished to do so, subject to the following conditions:
104cd92098Szrj  *
114cd92098Szrj  * The above copyright notice and this permission notice shall be included in
124cd92098Szrj  * all copies or substantial portions of the Software.
134cd92098Szrj  *
144cd92098Szrj  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154cd92098Szrj  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164cd92098Szrj  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174cd92098Szrj  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184cd92098Szrj  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194cd92098Szrj  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204cd92098Szrj  * OTHER DEALINGS IN THE SOFTWARE.
214cd92098Szrj  *
224cd92098Szrj  * Authors: Alex Deucher
234cd92098Szrj  */
244cd92098Szrj #include <drm/drmP.h>
254cd92098Szrj #include "radeon.h"
264cd92098Szrj #include "radeon_asic.h"
274cd92098Szrj #include "rv770d.h"
284cd92098Szrj 
294cd92098Szrj /**
304cd92098Szrj  * rv770_copy_dma - copy pages using the DMA engine
314cd92098Szrj  *
324cd92098Szrj  * @rdev: radeon_device pointer
334cd92098Szrj  * @src_offset: src GPU address
344cd92098Szrj  * @dst_offset: dst GPU address
354cd92098Szrj  * @num_gpu_pages: number of GPU pages to xfer
361cfef1a5SFrançois Tigeot  * @resv: reservation object to sync to
374cd92098Szrj  *
384cd92098Szrj  * Copy GPU paging using the DMA engine (r7xx).
394cd92098Szrj  * Used by the radeon ttm implementation to move pages if
404cd92098Szrj  * registered as the asic copy callback.
414cd92098Szrj  */
rv770_copy_dma(struct radeon_device * rdev,uint64_t src_offset,uint64_t dst_offset,unsigned num_gpu_pages,struct reservation_object * resv)421cfef1a5SFrançois Tigeot struct radeon_fence *rv770_copy_dma(struct radeon_device *rdev,
434cd92098Szrj 				    uint64_t src_offset, uint64_t dst_offset,
444cd92098Szrj 				    unsigned num_gpu_pages,
451cfef1a5SFrançois Tigeot 				    struct reservation_object *resv)
464cd92098Szrj {
471cfef1a5SFrançois Tigeot 	struct radeon_fence *fence;
48*7dcf36dcSFrançois Tigeot 	struct radeon_sync sync;
494cd92098Szrj 	int ring_index = rdev->asic->copy.dma_ring_index;
504cd92098Szrj 	struct radeon_ring *ring = &rdev->ring[ring_index];
514cd92098Szrj 	u32 size_in_dw, cur_size_in_dw;
524cd92098Szrj 	int i, num_loops;
534cd92098Szrj 	int r = 0;
544cd92098Szrj 
55*7dcf36dcSFrançois Tigeot 	radeon_sync_create(&sync);
564cd92098Szrj 
574cd92098Szrj 	size_in_dw = (num_gpu_pages << RADEON_GPU_PAGE_SHIFT) / 4;
584cd92098Szrj 	num_loops = DIV_ROUND_UP(size_in_dw, 0xFFFF);
594cd92098Szrj 	r = radeon_ring_lock(rdev, ring, num_loops * 5 + 8);
604cd92098Szrj 	if (r) {
614cd92098Szrj 		DRM_ERROR("radeon: moving bo (%d).\n", r);
62*7dcf36dcSFrançois Tigeot 		radeon_sync_free(rdev, &sync, NULL);
631cfef1a5SFrançois Tigeot 		return ERR_PTR(r);
644cd92098Szrj 	}
654cd92098Szrj 
66*7dcf36dcSFrançois Tigeot 	radeon_sync_resv(rdev, &sync, resv, false);
67*7dcf36dcSFrançois Tigeot 	radeon_sync_rings(rdev, &sync, ring->idx);
684cd92098Szrj 
694cd92098Szrj 	for (i = 0; i < num_loops; i++) {
704cd92098Szrj 		cur_size_in_dw = size_in_dw;
714cd92098Szrj 		if (cur_size_in_dw > 0xFFFF)
724cd92098Szrj 			cur_size_in_dw = 0xFFFF;
734cd92098Szrj 		size_in_dw -= cur_size_in_dw;
744cd92098Szrj 		radeon_ring_write(ring, DMA_PACKET(DMA_PACKET_COPY, 0, 0, cur_size_in_dw));
754cd92098Szrj 		radeon_ring_write(ring, dst_offset & 0xfffffffc);
764cd92098Szrj 		radeon_ring_write(ring, src_offset & 0xfffffffc);
774cd92098Szrj 		radeon_ring_write(ring, upper_32_bits(dst_offset) & 0xff);
784cd92098Szrj 		radeon_ring_write(ring, upper_32_bits(src_offset) & 0xff);
794cd92098Szrj 		src_offset += cur_size_in_dw * 4;
804cd92098Szrj 		dst_offset += cur_size_in_dw * 4;
814cd92098Szrj 	}
824cd92098Szrj 
831cfef1a5SFrançois Tigeot 	r = radeon_fence_emit(rdev, &fence, ring->idx);
844cd92098Szrj 	if (r) {
854cd92098Szrj 		radeon_ring_unlock_undo(rdev, ring);
86*7dcf36dcSFrançois Tigeot 		radeon_sync_free(rdev, &sync, NULL);
871cfef1a5SFrançois Tigeot 		return ERR_PTR(r);
884cd92098Szrj 	}
894cd92098Szrj 
90c6f73aabSFrançois Tigeot 	radeon_ring_unlock_commit(rdev, ring, false);
91*7dcf36dcSFrançois Tigeot 	radeon_sync_free(rdev, &sync, fence);
924cd92098Szrj 
931cfef1a5SFrançois Tigeot 	return fence;
944cd92098Szrj }
95