1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *      Jerome Glisse
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci#include "r600_pipe.h"
27bf215546Sopenharmony_ci#include "evergreend.h"
28bf215546Sopenharmony_ci#include "util/u_memory.h"
29bf215546Sopenharmony_ci#include "util/u_math.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_civoid evergreen_dma_copy_buffer(struct r600_context *rctx,
32bf215546Sopenharmony_ci			       struct pipe_resource *dst,
33bf215546Sopenharmony_ci			       struct pipe_resource *src,
34bf215546Sopenharmony_ci			       uint64_t dst_offset,
35bf215546Sopenharmony_ci			       uint64_t src_offset,
36bf215546Sopenharmony_ci			       uint64_t size)
37bf215546Sopenharmony_ci{
38bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &rctx->b.dma.cs;
39bf215546Sopenharmony_ci	unsigned i, ncopy, csize, sub_cmd, shift;
40bf215546Sopenharmony_ci	struct r600_resource *rdst = (struct r600_resource*)dst;
41bf215546Sopenharmony_ci	struct r600_resource *rsrc = (struct r600_resource*)src;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci	/* Mark the buffer range of destination as valid (initialized),
44bf215546Sopenharmony_ci	 * so that transfer_map knows it should wait for the GPU when mapping
45bf215546Sopenharmony_ci	 * that range. */
46bf215546Sopenharmony_ci	util_range_add(&rdst->b.b, &rdst->valid_buffer_range, dst_offset,
47bf215546Sopenharmony_ci		       dst_offset + size);
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci	dst_offset += rdst->gpu_address;
50bf215546Sopenharmony_ci	src_offset += rsrc->gpu_address;
51bf215546Sopenharmony_ci
52bf215546Sopenharmony_ci	/* see if we use dword or byte copy */
53bf215546Sopenharmony_ci	if (!(dst_offset % 4) && !(src_offset % 4) && !(size % 4)) {
54bf215546Sopenharmony_ci		size >>= 2;
55bf215546Sopenharmony_ci		sub_cmd = EG_DMA_COPY_DWORD_ALIGNED;
56bf215546Sopenharmony_ci		shift = 2;
57bf215546Sopenharmony_ci	} else {
58bf215546Sopenharmony_ci		sub_cmd = EG_DMA_COPY_BYTE_ALIGNED;
59bf215546Sopenharmony_ci		shift = 0;
60bf215546Sopenharmony_ci	}
61bf215546Sopenharmony_ci	ncopy = (size / EG_DMA_COPY_MAX_SIZE) + !!(size % EG_DMA_COPY_MAX_SIZE);
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_ci	r600_need_dma_space(&rctx->b, ncopy * 5, rdst, rsrc);
64bf215546Sopenharmony_ci	for (i = 0; i < ncopy; i++) {
65bf215546Sopenharmony_ci		csize = size < EG_DMA_COPY_MAX_SIZE ? size : EG_DMA_COPY_MAX_SIZE;
66bf215546Sopenharmony_ci		/* emit reloc before writing cs so that cs is always in consistent state */
67bf215546Sopenharmony_ci		radeon_add_to_buffer_list(&rctx->b, &rctx->b.dma, rsrc, RADEON_USAGE_READ);
68bf215546Sopenharmony_ci		radeon_add_to_buffer_list(&rctx->b, &rctx->b.dma, rdst, RADEON_USAGE_WRITE);
69bf215546Sopenharmony_ci		radeon_emit(cs, DMA_PACKET(DMA_PACKET_COPY, sub_cmd, csize));
70bf215546Sopenharmony_ci		radeon_emit(cs, dst_offset & 0xffffffff);
71bf215546Sopenharmony_ci		radeon_emit(cs, src_offset & 0xffffffff);
72bf215546Sopenharmony_ci		radeon_emit(cs, (dst_offset >> 32UL) & 0xff);
73bf215546Sopenharmony_ci		radeon_emit(cs, (src_offset >> 32UL) & 0xff);
74bf215546Sopenharmony_ci		dst_offset += csize << shift;
75bf215546Sopenharmony_ci		src_offset += csize << shift;
76bf215546Sopenharmony_ci		size -= csize;
77bf215546Sopenharmony_ci	}
78bf215546Sopenharmony_ci}
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci/* The max number of bytes to copy per packet. */
81bf215546Sopenharmony_ci#define CP_DMA_MAX_BYTE_COUNT ((1 << 21) - 8)
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_civoid evergreen_cp_dma_clear_buffer(struct r600_context *rctx,
84bf215546Sopenharmony_ci				   struct pipe_resource *dst, uint64_t offset,
85bf215546Sopenharmony_ci				   unsigned size, uint32_t clear_value,
86bf215546Sopenharmony_ci				   enum r600_coherency coher)
87bf215546Sopenharmony_ci{
88bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &rctx->b.gfx.cs;
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_ci	assert(size);
91bf215546Sopenharmony_ci	assert(rctx->screen->b.has_cp_dma);
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci	/* Mark the buffer range of destination as valid (initialized),
94bf215546Sopenharmony_ci	 * so that transfer_map knows it should wait for the GPU when mapping
95bf215546Sopenharmony_ci	 * that range. */
96bf215546Sopenharmony_ci	util_range_add(dst, &r600_resource(dst)->valid_buffer_range, offset,
97bf215546Sopenharmony_ci		       offset + size);
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci	offset += r600_resource(dst)->gpu_address;
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci	/* Flush the cache where the resource is bound. */
102bf215546Sopenharmony_ci	rctx->b.flags |= r600_get_flush_flags(coher) |
103bf215546Sopenharmony_ci			 R600_CONTEXT_WAIT_3D_IDLE;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci	while (size) {
106bf215546Sopenharmony_ci		unsigned sync = 0;
107bf215546Sopenharmony_ci		unsigned byte_count = MIN2(size, CP_DMA_MAX_BYTE_COUNT);
108bf215546Sopenharmony_ci		unsigned reloc;
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_ci		r600_need_cs_space(rctx,
111bf215546Sopenharmony_ci				   10 + (rctx->b.flags ? R600_MAX_FLUSH_CS_DWORDS : 0) +
112bf215546Sopenharmony_ci				   R600_MAX_PFP_SYNC_ME_DWORDS, FALSE, 0);
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci		/* Flush the caches for the first copy only. */
115bf215546Sopenharmony_ci		if (rctx->b.flags) {
116bf215546Sopenharmony_ci			r600_flush_emit(rctx);
117bf215546Sopenharmony_ci		}
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci		/* Do the synchronization after the last copy, so that all data is written to memory. */
120bf215546Sopenharmony_ci		if (size == byte_count) {
121bf215546Sopenharmony_ci			sync = PKT3_CP_DMA_CP_SYNC;
122bf215546Sopenharmony_ci		}
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci		/* This must be done after r600_need_cs_space. */
125bf215546Sopenharmony_ci		reloc = radeon_add_to_buffer_list(&rctx->b, &rctx->b.gfx,
126bf215546Sopenharmony_ci					      (struct r600_resource*)dst, RADEON_USAGE_WRITE |
127bf215546Sopenharmony_ci					      RADEON_PRIO_CP_DMA);
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci		radeon_emit(cs, PKT3(PKT3_CP_DMA, 4, 0));
130bf215546Sopenharmony_ci		radeon_emit(cs, clear_value);	/* DATA [31:0] */
131bf215546Sopenharmony_ci		radeon_emit(cs, sync | PKT3_CP_DMA_SRC_SEL(2));	/* CP_SYNC [31] | SRC_SEL[30:29] */
132bf215546Sopenharmony_ci		radeon_emit(cs, offset);	/* DST_ADDR_LO [31:0] */
133bf215546Sopenharmony_ci		radeon_emit(cs, (offset >> 32) & 0xff);		/* DST_ADDR_HI [7:0] */
134bf215546Sopenharmony_ci		radeon_emit(cs, byte_count);	/* COMMAND [29:22] | BYTE_COUNT [20:0] */
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci		radeon_emit(cs, PKT3(PKT3_NOP, 0, 0));
137bf215546Sopenharmony_ci		radeon_emit(cs, reloc);
138bf215546Sopenharmony_ci
139bf215546Sopenharmony_ci		size -= byte_count;
140bf215546Sopenharmony_ci		offset += byte_count;
141bf215546Sopenharmony_ci	}
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci	/* CP DMA is executed in ME, but index buffers are read by PFP.
144bf215546Sopenharmony_ci	 * This ensures that ME (CP DMA) is idle before PFP starts fetching
145bf215546Sopenharmony_ci	 * indices. If we wanted to execute CP DMA in PFP, this packet
146bf215546Sopenharmony_ci	 * should precede it.
147bf215546Sopenharmony_ci	 */
148bf215546Sopenharmony_ci	if (coher == R600_COHERENCY_SHADER)
149bf215546Sopenharmony_ci		r600_emit_pfp_sync_me(rctx);
150bf215546Sopenharmony_ci}
151