286b382f58e85f2530e69b5ef3ec1f18c0b56b33
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_transfer.c
1
2 #include "util/u_format.h"
3
4 #include "nvc0_context.h"
5 #include "nvc0_transfer.h"
6
7 #include "nv50_defs.xml.h"
8
9 struct nvc0_transfer {
10 struct pipe_transfer base;
11 struct nvc0_m2mf_rect rect[2];
12 uint32_t nblocksx;
13 uint32_t nblocksy;
14 };
15
16 static void
17 nvc0_m2mf_transfer_rect(struct pipe_screen *pscreen,
18 const struct nvc0_m2mf_rect *dst,
19 const struct nvc0_m2mf_rect *src,
20 uint32_t nblocksx, uint32_t nblocksy)
21 {
22 struct nouveau_channel *chan = nouveau_screen(pscreen)->channel;
23 const int cpp = dst->cpp;
24 uint32_t src_ofst = src->base;
25 uint32_t dst_ofst = dst->base;
26 uint32_t height = nblocksy;
27 uint32_t sy = src->y;
28 uint32_t dy = dst->y;
29 uint32_t exec = (1 << 20);
30
31 assert(dst->cpp == src->cpp);
32
33 if (nouveau_bo_tile_layout(src->bo)) {
34 BEGIN_RING(chan, RING_MF(TILING_MODE_IN), 5);
35 OUT_RING (chan, src->tile_mode);
36 OUT_RING (chan, src->width * cpp);
37 OUT_RING (chan, src->height);
38 OUT_RING (chan, src->depth);
39 OUT_RING (chan, src->z);
40 } else {
41 src_ofst += src->y * src->pitch + src->x * cpp;
42
43 BEGIN_RING(chan, RING_MF(PITCH_IN), 1);
44 OUT_RING (chan, src->width * cpp);
45
46 exec |= NVC0_M2MF_EXEC_LINEAR_IN;
47 }
48
49 if (nouveau_bo_tile_layout(dst->bo)) {
50 BEGIN_RING(chan, RING_MF(TILING_MODE_OUT), 5);
51 OUT_RING (chan, dst->tile_mode);
52 OUT_RING (chan, dst->width * cpp);
53 OUT_RING (chan, dst->height);
54 OUT_RING (chan, dst->depth);
55 OUT_RING (chan, dst->z);
56 } else {
57 dst_ofst += dst->y * dst->pitch + dst->x * cpp;
58
59 BEGIN_RING(chan, RING_MF(PITCH_OUT), 1);
60 OUT_RING (chan, dst->width * cpp);
61
62 exec |= NVC0_M2MF_EXEC_LINEAR_OUT;
63 }
64
65 while (height) {
66 int line_count = height > 2047 ? 2047 : height;
67
68 MARK_RING (chan, 17, 4);
69
70 BEGIN_RING(chan, RING_MF(OFFSET_IN_HIGH), 2);
71 OUT_RELOCh(chan, src->bo, src_ofst, src->domain | NOUVEAU_BO_RD);
72 OUT_RELOCl(chan, src->bo, src_ofst, src->domain | NOUVEAU_BO_RD);
73
74 BEGIN_RING(chan, RING_MF(OFFSET_OUT_HIGH), 2);
75 OUT_RELOCh(chan, dst->bo, dst_ofst, dst->domain | NOUVEAU_BO_WR);
76 OUT_RELOCl(chan, dst->bo, dst_ofst, dst->domain | NOUVEAU_BO_WR);
77
78 if (!(exec & NVC0_M2MF_EXEC_LINEAR_IN)) {
79 BEGIN_RING(chan, RING_MF(TILING_POSITION_IN_X), 2);
80 OUT_RING (chan, src->x * cpp);
81 OUT_RING (chan, sy);
82 } else {
83 src_ofst += line_count * src->pitch;
84 }
85 if (!(exec & NVC0_M2MF_EXEC_LINEAR_OUT)) {
86 BEGIN_RING(chan, RING_MF(TILING_POSITION_OUT_X), 2);
87 OUT_RING (chan, dst->x * cpp);
88 OUT_RING (chan, dy);
89 } else {
90 dst_ofst += line_count * dst->pitch;
91 }
92
93 BEGIN_RING(chan, RING_MF(LINE_LENGTH_IN), 2);
94 OUT_RING (chan, nblocksx * cpp);
95 OUT_RING (chan, line_count);
96 BEGIN_RING(chan, RING_MF(EXEC), 1);
97 OUT_RING (chan, exec);
98
99 height -= line_count;
100 sy += line_count;
101 dy += line_count;
102 }
103 }
104
105 void
106 nvc0_m2mf_push_linear(struct nvc0_context *nvc0,
107 struct nouveau_bo *dst, unsigned domain, int offset,
108 unsigned size, void *data)
109 {
110 struct nouveau_channel *chan = nvc0->screen->base.channel;
111 uint32_t *src = (uint32_t *)data;
112 unsigned count = (size + 3) / 4;
113
114 MARK_RING (chan, 8, 2);
115
116 BEGIN_RING(chan, RING_MF(OFFSET_OUT_HIGH), 2);
117 OUT_RELOCh(chan, dst, offset, domain | NOUVEAU_BO_WR);
118 OUT_RELOCl(chan, dst, offset, domain | NOUVEAU_BO_WR);
119 BEGIN_RING(chan, RING_MF(LINE_LENGTH_IN), 2);
120 OUT_RING (chan, size);
121 OUT_RING (chan, 1);
122 BEGIN_RING(chan, RING_MF(EXEC), 1);
123 OUT_RING (chan, 0x100111);
124
125 while (count) {
126 unsigned nr = AVAIL_RING(chan);
127
128 if (nr < 9) {
129 FIRE_RING(chan);
130 nouveau_bo_validate(chan, dst, NOUVEAU_BO_WR);
131 continue;
132 }
133 nr = MIN2(count, nr - 1);
134 nr = MIN2(nr, NV04_PFIFO_MAX_PACKET_LEN);
135
136 BEGIN_RING_NI(chan, RING_MF(DATA), nr);
137 OUT_RINGp (chan, src, nr);
138
139 src += nr;
140 count -= nr;
141 }
142 }
143
144 void
145 nvc0_m2mf_copy_linear(struct nvc0_context *nvc0,
146 struct nouveau_bo *dst, unsigned dstoff, unsigned dstdom,
147 struct nouveau_bo *src, unsigned srcoff, unsigned srcdom,
148 unsigned size)
149 {
150 struct nouveau_channel *chan = nvc0->screen->base.channel;
151
152 while (size) {
153 unsigned bytes = MIN2(size, 1 << 17);
154
155 MARK_RING (chan, 11, 4);
156
157 BEGIN_RING(chan, RING_MF(OFFSET_OUT_HIGH), 2);
158 OUT_RELOCh(chan, dst, dstoff, dstdom | NOUVEAU_BO_WR);
159 OUT_RELOCl(chan, dst, dstoff, dstdom | NOUVEAU_BO_WR);
160 BEGIN_RING(chan, RING_MF(OFFSET_IN_HIGH), 2);
161 OUT_RELOCh(chan, src, srcoff, srcdom | NOUVEAU_BO_RD);
162 OUT_RELOCl(chan, src, srcoff, srcdom | NOUVEAU_BO_RD);
163 BEGIN_RING(chan, RING_MF(LINE_LENGTH_IN), 2);
164 OUT_RING (chan, bytes);
165 OUT_RING (chan, 1);
166 BEGIN_RING(chan, RING_MF(EXEC), 1);
167 OUT_RING (chan, (1 << NVC0_M2MF_EXEC_INC__SHIFT) |
168 NVC0_M2MF_EXEC_LINEAR_IN | NVC0_M2MF_EXEC_LINEAR_OUT);
169
170 srcoff += bytes;
171 dstoff += bytes;
172 size -= bytes;
173 }
174 }
175
176 static void
177 nvc0_m2mf_push_rect(struct pipe_screen *pscreen,
178 const struct nvc0_m2mf_rect *dst,
179 const void *data,
180 unsigned nblocksx, unsigned nblocksy)
181 {
182 struct nouveau_channel *chan;
183 const uint8_t *src = (const uint8_t *)data;
184 const int cpp = dst->cpp;
185 const int line_len = nblocksx * cpp;
186 int dy = dst->y;
187
188 assert(nouveau_bo_tile_layout(dst->bo));
189
190 BEGIN_RING(chan, RING_MF(TILING_MODE_OUT), 5);
191 OUT_RING (chan, dst->tile_mode);
192 OUT_RING (chan, dst->width * cpp);
193 OUT_RING (chan, dst->height);
194 OUT_RING (chan, dst->depth);
195 OUT_RING (chan, dst->z);
196
197 while (nblocksy) {
198 int line_count, words;
199 int size = MIN2(AVAIL_RING(chan), NV04_PFIFO_MAX_PACKET_LEN);
200
201 if (size < (12 + words)) {
202 FIRE_RING(chan);
203 continue;
204 }
205 line_count = (size * 4) / line_len;
206 words = (line_count * line_len + 3) / 4;
207
208 BEGIN_RING(chan, RING_MF(OFFSET_OUT_HIGH), 2);
209 OUT_RELOCh(chan, dst->bo, dst->base, dst->domain | NOUVEAU_BO_WR);
210 OUT_RELOCl(chan, dst->bo, dst->base, dst->domain | NOUVEAU_BO_WR);
211
212 BEGIN_RING(chan, RING_MF(TILING_POSITION_OUT_X), 2);
213 OUT_RING (chan, dst->x * cpp);
214 OUT_RING (chan, dy);
215 BEGIN_RING(chan, RING_MF(LINE_LENGTH_IN), 2);
216 OUT_RING (chan, line_len);
217 OUT_RING (chan, line_count);
218 BEGIN_RING(chan, RING_MF(EXEC), 1);
219 OUT_RING (chan, (1 << NVC0_M2MF_EXEC_INC__SHIFT) |
220 NVC0_M2MF_EXEC_PUSH | NVC0_M2MF_EXEC_LINEAR_IN);
221
222 BEGIN_RING_NI(chan, RING_MF(DATA), words);
223 OUT_RINGp (chan, src, words);
224
225 dy += line_count;
226 src += line_len * line_count;
227 nblocksy -= line_count;
228 }
229 }
230
231 struct pipe_transfer *
232 nvc0_miptree_transfer_new(struct pipe_context *pctx,
233 struct pipe_resource *res,
234 unsigned level,
235 unsigned usage,
236 const struct pipe_box *box)
237 {
238 struct nvc0_context *nvc0 = nvc0_context(pctx);
239 struct pipe_screen *pscreen = pctx->screen;
240 struct nouveau_device *dev = nvc0->screen->base.device;
241 struct nvc0_miptree *mt = nvc0_miptree(res);
242 struct nvc0_miptree_level *lvl = &mt->level[level];
243 struct nvc0_transfer *tx;
244 uint32_t size;
245 uint32_t w, h, d, z, layer;
246 int ret;
247
248 if (mt->layout_3d) {
249 z = box->z;
250 d = u_minify(res->depth0, level);
251 layer = 0;
252 } else {
253 z = 0;
254 d = 1;
255 layer = box->z;
256 }
257
258 tx = CALLOC_STRUCT(nvc0_transfer);
259 if (!tx)
260 return NULL;
261
262 pipe_resource_reference(&tx->base.resource, res);
263
264 tx->base.level = level;
265 tx->base.usage = usage;
266 tx->base.box = *box;
267
268 tx->nblocksx = util_format_get_nblocksx(res->format, box->width);
269 tx->nblocksy = util_format_get_nblocksy(res->format, box->height);
270
271 tx->base.stride = tx->nblocksx * util_format_get_blocksize(res->format);
272 tx->base.layer_stride = tx->nblocksy * tx->base.stride;
273
274 w = u_minify(res->width0, level);
275 h = u_minify(res->height0, level);
276
277 tx->rect[0].cpp = tx->rect[1].cpp = util_format_get_blocksize(res->format);
278
279 tx->rect[0].bo = mt->base.bo;
280 tx->rect[0].base = lvl->offset + layer * mt->layer_stride;
281 tx->rect[0].tile_mode = lvl->tile_mode;
282 tx->rect[0].x = util_format_get_nblocksx(res->format, box->x);
283 tx->rect[0].y = util_format_get_nblocksy(res->format, box->y);
284 tx->rect[0].z = z;
285 tx->rect[0].width = util_format_get_nblocksx(res->format, w);
286 tx->rect[0].height = util_format_get_nblocksy(res->format, h);
287 tx->rect[0].depth = d;
288 tx->rect[0].pitch = lvl->pitch;
289 tx->rect[0].domain = NOUVEAU_BO_VRAM;
290
291 size = tx->base.layer_stride;
292
293 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0,
294 size * tx->base.box.depth, &tx->rect[1].bo);
295 if (ret) {
296 FREE(tx);
297 return NULL;
298 }
299
300 tx->rect[1].width = tx->nblocksx;
301 tx->rect[1].height = tx->nblocksy;
302 tx->rect[1].depth = 1;
303 tx->rect[1].pitch = tx->base.stride;
304 tx->rect[1].domain = NOUVEAU_BO_GART;
305
306 if (usage & PIPE_TRANSFER_READ) {
307 unsigned i;
308 for (i = 0; i < box->depth; ++i) {
309 nvc0_m2mf_transfer_rect(pscreen, &tx->rect[1], &tx->rect[0],
310 tx->nblocksx, tx->nblocksy);
311 if (mt->layout_3d)
312 tx->rect[0].z++;
313 else
314 tx->rect[0].base += mt->layer_stride;
315 tx->rect[1].base += size;
316 }
317 }
318 tx->rect[0].z = z;
319 tx->rect[1].base = 0;
320
321 return &tx->base;
322 }
323
324 void
325 nvc0_miptree_transfer_del(struct pipe_context *pctx,
326 struct pipe_transfer *transfer)
327 {
328 struct pipe_screen *pscreen = pctx->screen;
329 struct nvc0_transfer *tx = (struct nvc0_transfer *)transfer;
330 struct nvc0_miptree *mt = nvc0_miptree(tx->base.resource);
331 unsigned i;
332
333 if (tx->base.usage & PIPE_TRANSFER_WRITE) {
334 for (i = 0; i < tx->base.box.depth; ++i) {
335 nvc0_m2mf_transfer_rect(pscreen, &tx->rect[0], &tx->rect[1],
336 tx->nblocksx, tx->nblocksy);
337 if (mt->layout_3d)
338 tx->rect[0].z++;
339 else
340 tx->rect[0].base += mt->layer_stride;
341 tx->rect[1].base += tx->nblocksy * tx->base.stride;
342 }
343 }
344
345 nouveau_bo_ref(NULL, &tx->rect[1].bo);
346 pipe_resource_reference(&transfer->resource, NULL);
347
348 FREE(tx);
349 }
350
351 void *
352 nvc0_miptree_transfer_map(struct pipe_context *pctx,
353 struct pipe_transfer *transfer)
354 {
355 struct nvc0_transfer *tx = (struct nvc0_transfer *)transfer;
356 int ret;
357 unsigned flags = 0;
358
359 if (tx->rect[1].bo->map)
360 return tx->rect[1].bo->map;
361
362 if (transfer->usage & PIPE_TRANSFER_READ)
363 flags = NOUVEAU_BO_RD;
364 if (transfer->usage & PIPE_TRANSFER_WRITE)
365 flags |= NOUVEAU_BO_WR;
366
367 ret = nouveau_bo_map(tx->rect[1].bo, flags);
368 if (ret)
369 return NULL;
370 return tx->rect[1].bo->map;
371 }
372
373 void
374 nvc0_miptree_transfer_unmap(struct pipe_context *pctx,
375 struct pipe_transfer *transfer)
376 {
377 struct nvc0_transfer *tx = (struct nvc0_transfer *)transfer;
378
379 nouveau_bo_unmap(tx->rect[1].bo);
380 }
381