2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 * Copyright 2018 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * on the rights to use, copy, modify, merge, publish, distribute, sub
10 * license, and/or sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 * USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include "util/u_format.h"
31 static void si_dma_copy_buffer(struct si_context
*ctx
,
32 struct pipe_resource
*dst
,
33 struct pipe_resource
*src
,
38 struct radeon_cmdbuf
*cs
= ctx
->dma_cs
;
39 unsigned i
, ncopy
, count
, max_size
, sub_cmd
, shift
;
40 struct si_resource
*sdst
= si_resource(dst
);
41 struct si_resource
*ssrc
= si_resource(src
);
43 /* Mark the buffer range of destination as valid (initialized),
44 * so that transfer_map knows it should wait for the GPU when mapping
46 util_range_add(dst
, &sdst
->valid_buffer_range
, dst_offset
,
49 dst_offset
+= sdst
->gpu_address
;
50 src_offset
+= ssrc
->gpu_address
;
52 /* see whether we should use the dword-aligned or byte-aligned copy */
53 if (!(dst_offset
% 4) && !(src_offset
% 4) && !(size
% 4)) {
54 sub_cmd
= SI_DMA_COPY_DWORD_ALIGNED
;
56 max_size
= SI_DMA_COPY_MAX_DWORD_ALIGNED_SIZE
;
58 sub_cmd
= SI_DMA_COPY_BYTE_ALIGNED
;
60 max_size
= SI_DMA_COPY_MAX_BYTE_ALIGNED_SIZE
;
63 ncopy
= DIV_ROUND_UP(size
, max_size
);
64 si_need_dma_space(ctx
, ncopy
* 5, sdst
, ssrc
);
66 for (i
= 0; i
< ncopy
; i
++) {
67 count
= MIN2(size
, max_size
);
68 radeon_emit(cs
, SI_DMA_PACKET(SI_DMA_PACKET_COPY
, sub_cmd
,
70 radeon_emit(cs
, dst_offset
);
71 radeon_emit(cs
, src_offset
);
72 radeon_emit(cs
, (dst_offset
>> 32UL) & 0xff);
73 radeon_emit(cs
, (src_offset
>> 32UL) & 0xff);
80 static void si_dma_copy_tile(struct si_context
*ctx
,
81 struct pipe_resource
*dst
,
86 struct pipe_resource
*src
,
95 struct radeon_cmdbuf
*cs
= ctx
->dma_cs
;
96 struct si_texture
*ssrc
= (struct si_texture
*)src
;
97 struct si_texture
*sdst
= (struct si_texture
*)dst
;
98 unsigned dst_mode
= sdst
->surface
.u
.legacy
.level
[dst_level
].mode
;
99 bool detile
= dst_mode
== RADEON_SURF_MODE_LINEAR_ALIGNED
;
100 struct si_texture
*linear
= detile
? sdst
: ssrc
;
101 struct si_texture
*tiled
= detile
? ssrc
: sdst
;
102 unsigned linear_lvl
= detile
? dst_level
: src_level
;
103 unsigned tiled_lvl
= detile
? src_level
: dst_level
;
104 struct radeon_info
*info
= &ctx
->screen
->info
;
105 unsigned index
= tiled
->surface
.u
.legacy
.tiling_index
[tiled_lvl
];
106 unsigned tile_mode
= info
->si_tile_mode_array
[index
];
107 unsigned array_mode
, lbpp
, pitch_tile_max
, slice_tile_max
, size
;
108 unsigned ncopy
, height
, cheight
, i
;
109 unsigned linear_x
, linear_y
, linear_z
, tiled_x
, tiled_y
, tiled_z
;
110 unsigned sub_cmd
, bank_h
, bank_w
, mt_aspect
, nbanks
, tile_split
, mt
;
112 unsigned pipe_config
;
114 assert(dst_mode
!= ssrc
->surface
.u
.legacy
.level
[src_level
].mode
);
116 sub_cmd
= SI_DMA_COPY_TILED
;
117 lbpp
= util_logbase2(bpp
);
118 pitch_tile_max
= ((pitch
/ bpp
) / 8) - 1;
120 linear_x
= detile
? dst_x
: src_x
;
121 linear_y
= detile
? dst_y
: src_y
;
122 linear_z
= detile
? dst_z
: src_z
;
123 tiled_x
= detile
? src_x
: dst_x
;
124 tiled_y
= detile
? src_y
: dst_y
;
125 tiled_z
= detile
? src_z
: dst_z
;
127 assert(!util_format_is_depth_and_stencil(tiled
->buffer
.b
.b
.format
));
129 array_mode
= G_009910_ARRAY_MODE(tile_mode
);
130 slice_tile_max
= (tiled
->surface
.u
.legacy
.level
[tiled_lvl
].nblk_x
*
131 tiled
->surface
.u
.legacy
.level
[tiled_lvl
].nblk_y
) / (8*8) - 1;
132 /* linear height must be the same as the slice tile max height, it's ok even
133 * if the linear destination/source have smaller heigh as the size of the
134 * dma packet will be using the copy_height which is always smaller or equal
135 * to the linear height
137 height
= tiled
->surface
.u
.legacy
.level
[tiled_lvl
].nblk_y
;
138 base
= tiled
->surface
.u
.legacy
.level
[tiled_lvl
].offset
;
139 addr
= linear
->surface
.u
.legacy
.level
[linear_lvl
].offset
;
140 addr
+= (uint64_t)linear
->surface
.u
.legacy
.level
[linear_lvl
].slice_size_dw
* 4 * linear_z
;
141 addr
+= linear_y
* pitch
+ linear_x
* bpp
;
142 bank_h
= G_009910_BANK_HEIGHT(tile_mode
);
143 bank_w
= G_009910_BANK_WIDTH(tile_mode
);
144 mt_aspect
= G_009910_MACRO_TILE_ASPECT(tile_mode
);
145 /* Non-depth modes don't have TILE_SPLIT set. */
146 tile_split
= util_logbase2(tiled
->surface
.u
.legacy
.tile_split
>> 6);
147 nbanks
= G_009910_NUM_BANKS(tile_mode
);
148 base
+= tiled
->buffer
.gpu_address
;
149 addr
+= linear
->buffer
.gpu_address
;
151 pipe_config
= G_009910_PIPE_CONFIG(tile_mode
);
152 mt
= G_009910_MICRO_TILE_MODE(tile_mode
);
153 size
= copy_height
* pitch
;
154 ncopy
= DIV_ROUND_UP(size
, SI_DMA_COPY_MAX_DWORD_ALIGNED_SIZE
);
155 si_need_dma_space(ctx
, ncopy
* 9, &sdst
->buffer
, &ssrc
->buffer
);
157 for (i
= 0; i
< ncopy
; i
++) {
158 cheight
= copy_height
;
159 if (cheight
* pitch
> SI_DMA_COPY_MAX_DWORD_ALIGNED_SIZE
) {
160 cheight
= SI_DMA_COPY_MAX_DWORD_ALIGNED_SIZE
/ pitch
;
162 size
= cheight
* pitch
;
163 radeon_emit(cs
, SI_DMA_PACKET(SI_DMA_PACKET_COPY
, sub_cmd
, size
/ 4));
164 radeon_emit(cs
, base
>> 8);
165 radeon_emit(cs
, (detile
<< 31) | (array_mode
<< 27) |
166 (lbpp
<< 24) | (bank_h
<< 21) |
167 (bank_w
<< 18) | (mt_aspect
<< 16));
168 radeon_emit(cs
, (pitch_tile_max
<< 0) | ((height
- 1) << 16));
169 radeon_emit(cs
, (slice_tile_max
<< 0) | (pipe_config
<< 26));
170 radeon_emit(cs
, (tiled_x
<< 0) | (tiled_z
<< 18));
171 radeon_emit(cs
, (tiled_y
<< 0) | (tile_split
<< 21) | (nbanks
<< 25) | (mt
<< 27));
172 radeon_emit(cs
, addr
& 0xfffffffc);
173 radeon_emit(cs
, (addr
>> 32UL) & 0xff);
174 copy_height
-= cheight
;
175 addr
+= cheight
* pitch
;
180 static void si_dma_copy(struct pipe_context
*ctx
,
181 struct pipe_resource
*dst
,
183 unsigned dstx
, unsigned dsty
, unsigned dstz
,
184 struct pipe_resource
*src
,
186 const struct pipe_box
*src_box
)
188 struct si_context
*sctx
= (struct si_context
*)ctx
;
189 struct si_texture
*ssrc
= (struct si_texture
*)src
;
190 struct si_texture
*sdst
= (struct si_texture
*)dst
;
191 unsigned dst_pitch
, src_pitch
, bpp
, dst_mode
, src_mode
;
192 unsigned src_w
, dst_w
;
193 unsigned src_x
, src_y
;
194 unsigned dst_x
= dstx
, dst_y
= dsty
, dst_z
= dstz
;
196 if (sctx
->dma_cs
== NULL
||
197 src
->flags
& PIPE_RESOURCE_FLAG_SPARSE
||
198 dst
->flags
& PIPE_RESOURCE_FLAG_SPARSE
) {
202 if (dst
->target
== PIPE_BUFFER
&& src
->target
== PIPE_BUFFER
) {
203 si_dma_copy_buffer(sctx
, dst
, src
, dst_x
, src_box
->x
, src_box
->width
);
207 /* XXX: Using the asynchronous DMA engine for multi-dimensional
208 * operations seems to cause random GPU lockups for various people.
209 * While the root cause for this might need to be fixed in the kernel,
210 * let's disable it for now.
212 * Before re-enabling this, please make sure you can hit all newly
213 * enabled paths in your testing, preferably with both piglit and real
214 * world apps, and get in touch with people on the bug reports below
215 * for stability testing.
217 * https://bugs.freedesktop.org/show_bug.cgi?id=85647
218 * https://bugs.freedesktop.org/show_bug.cgi?id=83500
222 if (src_box
->depth
> 1 ||
223 !si_prepare_for_dma_blit(sctx
, sdst
, dst_level
, dstx
, dsty
,
224 dstz
, ssrc
, src_level
, src_box
))
227 src_x
= util_format_get_nblocksx(src
->format
, src_box
->x
);
228 dst_x
= util_format_get_nblocksx(src
->format
, dst_x
);
229 src_y
= util_format_get_nblocksy(src
->format
, src_box
->y
);
230 dst_y
= util_format_get_nblocksy(src
->format
, dst_y
);
232 bpp
= sdst
->surface
.bpe
;
233 dst_pitch
= sdst
->surface
.u
.legacy
.level
[dst_level
].nblk_x
* sdst
->surface
.bpe
;
234 src_pitch
= ssrc
->surface
.u
.legacy
.level
[src_level
].nblk_x
* ssrc
->surface
.bpe
;
235 src_w
= u_minify(ssrc
->buffer
.b
.b
.width0
, src_level
);
236 dst_w
= u_minify(sdst
->buffer
.b
.b
.width0
, dst_level
);
238 dst_mode
= sdst
->surface
.u
.legacy
.level
[dst_level
].mode
;
239 src_mode
= ssrc
->surface
.u
.legacy
.level
[src_level
].mode
;
241 if (src_pitch
!= dst_pitch
|| src_box
->x
|| dst_x
|| src_w
!= dst_w
||
242 src_box
->width
!= src_w
||
243 src_box
->height
!= u_minify(ssrc
->buffer
.b
.b
.height0
, src_level
) ||
244 src_box
->height
!= u_minify(sdst
->buffer
.b
.b
.height0
, dst_level
) ||
245 ssrc
->surface
.u
.legacy
.level
[src_level
].nblk_y
!=
246 sdst
->surface
.u
.legacy
.level
[dst_level
].nblk_y
) {
247 /* FIXME si can do partial blit */
250 /* the x test here are currently useless (because we don't support partial blit)
251 * but keep them around so we don't forget about those
253 if ((src_pitch
% 8) || (src_box
->x
% 8) || (dst_x
% 8) ||
254 (src_box
->y
% 8) || (dst_y
% 8) || (src_box
->height
% 8)) {
258 if (src_mode
== dst_mode
) {
259 uint64_t dst_offset
, src_offset
;
260 /* simple dma blit would do NOTE code here assume :
263 * dst_pitch == src_pitch
265 src_offset
= ssrc
->surface
.u
.legacy
.level
[src_level
].offset
;
266 src_offset
+= (uint64_t)ssrc
->surface
.u
.legacy
.level
[src_level
].slice_size_dw
* 4 * src_box
->z
;
267 src_offset
+= src_y
* src_pitch
+ src_x
* bpp
;
268 dst_offset
= sdst
->surface
.u
.legacy
.level
[dst_level
].offset
;
269 dst_offset
+= (uint64_t)sdst
->surface
.u
.legacy
.level
[dst_level
].slice_size_dw
* 4 * dst_z
;
270 dst_offset
+= dst_y
* dst_pitch
+ dst_x
* bpp
;
271 si_dma_copy_buffer(sctx
, dst
, src
, dst_offset
, src_offset
,
272 (uint64_t)ssrc
->surface
.u
.legacy
.level
[src_level
].slice_size_dw
* 4);
274 si_dma_copy_tile(sctx
, dst
, dst_level
, dst_x
, dst_y
, dst_z
,
275 src
, src_level
, src_x
, src_y
, src_box
->z
,
276 src_box
->height
/ ssrc
->surface
.blk_h
,
282 si_resource_copy_region(ctx
, dst
, dst_level
, dstx
, dsty
, dstz
,
283 src
, src_level
, src_box
);
286 void si_init_dma_functions(struct si_context
*sctx
)
288 sctx
->dma_copy
= si_dma_copy
;