radeonsi: set CB_COLORn_INFO.ROUND_MODE
[mesa.git] / src / gallium / drivers / radeonsi / si_dma.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 */
26
27 #include "sid.h"
28 #include "si_pipe.h"
29 #include "radeon/r600_cs.h"
30
31 #include "util/u_format.h"
32
33 static void si_dma_copy_buffer(struct si_context *ctx,
34 struct pipe_resource *dst,
35 struct pipe_resource *src,
36 uint64_t dst_offset,
37 uint64_t src_offset,
38 uint64_t size)
39 {
40 struct radeon_winsys_cs *cs = ctx->b.dma.cs;
41 unsigned i, ncopy, csize, max_csize, sub_cmd, shift;
42 struct r600_resource *rdst = (struct r600_resource*)dst;
43 struct r600_resource *rsrc = (struct r600_resource*)src;
44
45 /* Mark the buffer range of destination as valid (initialized),
46 * so that transfer_map knows it should wait for the GPU when mapping
47 * that range. */
48 util_range_add(&rdst->valid_buffer_range, dst_offset,
49 dst_offset + size);
50
51 dst_offset += rdst->gpu_address;
52 src_offset += rsrc->gpu_address;
53
54 /* see if we use dword or byte copy */
55 if (!(dst_offset % 4) && !(src_offset % 4) && !(size % 4)) {
56 size >>= 2;
57 sub_cmd = SI_DMA_COPY_DWORD_ALIGNED;
58 shift = 2;
59 max_csize = SI_DMA_COPY_MAX_SIZE_DW;
60 } else {
61 sub_cmd = SI_DMA_COPY_BYTE_ALIGNED;
62 shift = 0;
63 max_csize = SI_DMA_COPY_MAX_SIZE;
64 }
65 ncopy = (size / max_csize) + !!(size % max_csize);
66
67 r600_need_dma_space(&ctx->b, ncopy * 5, rdst, rsrc);
68
69 for (i = 0; i < ncopy; i++) {
70 csize = size < max_csize ? size : max_csize;
71 radeon_emit(cs, SI_DMA_PACKET(SI_DMA_PACKET_COPY, sub_cmd, csize));
72 radeon_emit(cs, dst_offset);
73 radeon_emit(cs, src_offset);
74 radeon_emit(cs, (dst_offset >> 32UL) & 0xff);
75 radeon_emit(cs, (src_offset >> 32UL) & 0xff);
76 dst_offset += csize << shift;
77 src_offset += csize << shift;
78 size -= csize;
79 }
80 r600_dma_emit_wait_idle(&ctx->b);
81 }
82
83 static void si_dma_copy_tile(struct si_context *ctx,
84 struct pipe_resource *dst,
85 unsigned dst_level,
86 unsigned dst_x,
87 unsigned dst_y,
88 unsigned dst_z,
89 struct pipe_resource *src,
90 unsigned src_level,
91 unsigned src_x,
92 unsigned src_y,
93 unsigned src_z,
94 unsigned copy_height,
95 unsigned pitch,
96 unsigned bpp)
97 {
98 struct radeon_winsys_cs *cs = ctx->b.dma.cs;
99 struct r600_texture *rsrc = (struct r600_texture*)src;
100 struct r600_texture *rdst = (struct r600_texture*)dst;
101 unsigned dst_mode = rdst->surface.level[dst_level].mode;
102 unsigned src_mode = rsrc->surface.level[src_level].mode;
103 bool detile = dst_mode == RADEON_SURF_MODE_LINEAR_ALIGNED;
104 struct r600_texture *rlinear = detile ? rdst : rsrc;
105 struct r600_texture *rtiled = detile ? rsrc : rdst;
106 unsigned linear_lvl = detile ? dst_level : src_level;
107 unsigned tiled_lvl = detile ? src_level : dst_level;
108 struct radeon_info *info = &ctx->screen->b.info;
109 unsigned index = rtiled->surface.tiling_index[tiled_lvl];
110 unsigned tile_mode = info->si_tile_mode_array[index];
111 unsigned array_mode, lbpp, pitch_tile_max, slice_tile_max, size;
112 unsigned ncopy, height, cheight, i;
113 unsigned linear_x, linear_y, linear_z, tiled_x, tiled_y, tiled_z;
114 unsigned sub_cmd, bank_h, bank_w, mt_aspect, nbanks, tile_split, mt;
115 uint64_t base, addr;
116 unsigned pipe_config;
117
118 assert(dst_mode != src_mode);
119
120 sub_cmd = SI_DMA_COPY_TILED;
121 lbpp = util_logbase2(bpp);
122 pitch_tile_max = ((pitch / bpp) / 8) - 1;
123
124 linear_x = detile ? dst_x : src_x;
125 linear_y = detile ? dst_y : src_y;
126 linear_z = detile ? dst_z : src_z;
127 tiled_x = detile ? src_x : dst_x;
128 tiled_y = detile ? src_y : dst_y;
129 tiled_z = detile ? src_z : dst_z;
130
131 assert(!util_format_is_depth_and_stencil(rtiled->resource.b.b.format));
132
133 array_mode = G_009910_ARRAY_MODE(tile_mode);
134 slice_tile_max = (rtiled->surface.level[tiled_lvl].nblk_x *
135 rtiled->surface.level[tiled_lvl].nblk_y) / (8*8) - 1;
136 /* linear height must be the same as the slice tile max height, it's ok even
137 * if the linear destination/source have smaller heigh as the size of the
138 * dma packet will be using the copy_height which is always smaller or equal
139 * to the linear height
140 */
141 height = rtiled->surface.level[tiled_lvl].nblk_y;
142 base = rtiled->surface.level[tiled_lvl].offset;
143 addr = rlinear->surface.level[linear_lvl].offset;
144 addr += rlinear->surface.level[linear_lvl].slice_size * linear_z;
145 addr += linear_y * pitch + linear_x * bpp;
146 bank_h = G_009910_BANK_HEIGHT(tile_mode);
147 bank_w = G_009910_BANK_WIDTH(tile_mode);
148 mt_aspect = G_009910_MACRO_TILE_ASPECT(tile_mode);
149 /* Non-depth modes don't have TILE_SPLIT set. */
150 tile_split = util_logbase2(rtiled->surface.tile_split >> 6);
151 nbanks = G_009910_NUM_BANKS(tile_mode);
152 base += rtiled->resource.gpu_address;
153 addr += rlinear->resource.gpu_address;
154
155 pipe_config = G_009910_PIPE_CONFIG(tile_mode);
156 mt = G_009910_MICRO_TILE_MODE(tile_mode);
157 size = (copy_height * pitch) / 4;
158 ncopy = (size / SI_DMA_COPY_MAX_SIZE_DW) + !!(size % SI_DMA_COPY_MAX_SIZE_DW);
159 r600_need_dma_space(&ctx->b, ncopy * 9, &rdst->resource, &rsrc->resource);
160
161 for (i = 0; i < ncopy; i++) {
162 cheight = copy_height;
163 if (((cheight * pitch) / 4) > SI_DMA_COPY_MAX_SIZE_DW) {
164 cheight = (SI_DMA_COPY_MAX_SIZE_DW * 4) / pitch;
165 }
166 size = (cheight * pitch) / 4;
167 radeon_emit(cs, SI_DMA_PACKET(SI_DMA_PACKET_COPY, sub_cmd, size));
168 radeon_emit(cs, base >> 8);
169 radeon_emit(cs, (detile << 31) | (array_mode << 27) |
170 (lbpp << 24) | (bank_h << 21) |
171 (bank_w << 18) | (mt_aspect << 16));
172 radeon_emit(cs, (pitch_tile_max << 0) | ((height - 1) << 16));
173 radeon_emit(cs, (slice_tile_max << 0) | (pipe_config << 26));
174 radeon_emit(cs, (tiled_x << 0) | (tiled_z << 18));
175 radeon_emit(cs, (tiled_y << 0) | (tile_split << 21) | (nbanks << 25) | (mt << 27));
176 radeon_emit(cs, addr & 0xfffffffc);
177 radeon_emit(cs, (addr >> 32UL) & 0xff);
178 copy_height -= cheight;
179 addr += cheight * pitch;
180 tiled_y += cheight;
181 }
182 r600_dma_emit_wait_idle(&ctx->b);
183 }
184
185 static void si_dma_copy(struct pipe_context *ctx,
186 struct pipe_resource *dst,
187 unsigned dst_level,
188 unsigned dstx, unsigned dsty, unsigned dstz,
189 struct pipe_resource *src,
190 unsigned src_level,
191 const struct pipe_box *src_box)
192 {
193 struct si_context *sctx = (struct si_context *)ctx;
194 struct r600_texture *rsrc = (struct r600_texture*)src;
195 struct r600_texture *rdst = (struct r600_texture*)dst;
196 unsigned dst_pitch, src_pitch, bpp, dst_mode, src_mode;
197 unsigned src_w, dst_w;
198 unsigned src_x, src_y;
199 unsigned dst_x = dstx, dst_y = dsty, dst_z = dstz;
200
201 if (sctx->b.dma.cs == NULL) {
202 goto fallback;
203 }
204
205 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
206 si_dma_copy_buffer(sctx, dst, src, dst_x, src_box->x, src_box->width);
207 return;
208 }
209
210 /* XXX: Using the asynchronous DMA engine for multi-dimensional
211 * operations seems to cause random GPU lockups for various people.
212 * While the root cause for this might need to be fixed in the kernel,
213 * let's disable it for now.
214 *
215 * Before re-enabling this, please make sure you can hit all newly
216 * enabled paths in your testing, preferably with both piglit and real
217 * world apps, and get in touch with people on the bug reports below
218 * for stability testing.
219 *
220 * https://bugs.freedesktop.org/show_bug.cgi?id=85647
221 * https://bugs.freedesktop.org/show_bug.cgi?id=83500
222 */
223 goto fallback;
224
225 if (src_box->depth > 1 ||
226 !r600_prepare_for_dma_blit(&sctx->b, rdst, dst_level, dstx, dsty,
227 dstz, rsrc, src_level, src_box))
228 goto fallback;
229
230 src_x = util_format_get_nblocksx(src->format, src_box->x);
231 dst_x = util_format_get_nblocksx(src->format, dst_x);
232 src_y = util_format_get_nblocksy(src->format, src_box->y);
233 dst_y = util_format_get_nblocksy(src->format, dst_y);
234
235 bpp = rdst->surface.bpe;
236 dst_pitch = rdst->surface.level[dst_level].pitch_bytes;
237 src_pitch = rsrc->surface.level[src_level].pitch_bytes;
238 src_w = rsrc->surface.level[src_level].npix_x;
239 dst_w = rdst->surface.level[dst_level].npix_x;
240
241 dst_mode = rdst->surface.level[dst_level].mode;
242 src_mode = rsrc->surface.level[src_level].mode;
243
244 if (src_pitch != dst_pitch || src_box->x || dst_x || src_w != dst_w ||
245 src_box->width != src_w ||
246 src_box->height != rsrc->surface.level[src_level].npix_y ||
247 src_box->height != rdst->surface.level[dst_level].npix_y ||
248 rsrc->surface.level[src_level].nblk_y !=
249 rdst->surface.level[dst_level].nblk_y) {
250 /* FIXME si can do partial blit */
251 goto fallback;
252 }
253 /* the x test here are currently useless (because we don't support partial blit)
254 * but keep them around so we don't forget about those
255 */
256 if ((src_pitch % 8) || (src_box->x % 8) || (dst_x % 8) ||
257 (src_box->y % 8) || (dst_y % 8) || (src_box->height % 8)) {
258 goto fallback;
259 }
260
261 if (src_mode == dst_mode) {
262 uint64_t dst_offset, src_offset;
263 /* simple dma blit would do NOTE code here assume :
264 * src_box.x/y == 0
265 * dst_x/y == 0
266 * dst_pitch == src_pitch
267 */
268 src_offset= rsrc->surface.level[src_level].offset;
269 src_offset += rsrc->surface.level[src_level].slice_size * src_box->z;
270 src_offset += src_y * src_pitch + src_x * bpp;
271 dst_offset = rdst->surface.level[dst_level].offset;
272 dst_offset += rdst->surface.level[dst_level].slice_size * dst_z;
273 dst_offset += dst_y * dst_pitch + dst_x * bpp;
274 si_dma_copy_buffer(sctx, dst, src, dst_offset, src_offset,
275 rsrc->surface.level[src_level].slice_size);
276 } else {
277 si_dma_copy_tile(sctx, dst, dst_level, dst_x, dst_y, dst_z,
278 src, src_level, src_x, src_y, src_box->z,
279 src_box->height / rsrc->surface.blk_h,
280 dst_pitch, bpp);
281 }
282 return;
283
284 fallback:
285 si_resource_copy_region(ctx, dst, dst_level, dstx, dsty, dstz,
286 src, src_level, src_box);
287 }
288
289 void si_init_dma_functions(struct si_context *sctx)
290 {
291 sctx->b.dma_copy = si_dma_copy;
292 }