gallium: add flag PIPE_TRANSFER_MAP_PERMANENTLY
[mesa.git] / src / gallium / drivers / nvc0 / nvc0_transfer.c
1
2 #include "util/u_format.h"
3
4 #include "nvc0_context.h"
5
6 #include "nv50/nv50_defs.xml.h"
7
8 struct nvc0_transfer {
9 struct pipe_transfer base;
10 struct nv50_m2mf_rect rect[2];
11 uint32_t nblocksx;
12 uint16_t nblocksy;
13 uint16_t nlayers;
14 };
15
16 void
17 nvc0_m2mf_transfer_rect(struct pipe_screen *pscreen,
18 const struct nv50_m2mf_rect *dst,
19 const struct nv50_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 nouveau_context *nv,
107 struct nouveau_bo *dst, unsigned offset, unsigned domain,
108 unsigned size, const void *data)
109 {
110 struct nouveau_channel *chan = nv->screen->channel;
111 uint32_t *src = (uint32_t *)data;
112 unsigned count = (size + 3) / 4;
113
114 while (count) {
115 unsigned nr;
116
117 MARK_RING (chan, 16, 2);
118
119 nr = AVAIL_RING(chan) - 9;
120 nr = MIN2(count, nr);
121 nr = MIN2(nr, NV04_PFIFO_MAX_PACKET_LEN);
122
123 BEGIN_RING(chan, RING_MF(OFFSET_OUT_HIGH), 2);
124 OUT_RELOCh(chan, dst, offset, domain | NOUVEAU_BO_WR);
125 OUT_RELOCl(chan, dst, offset, domain | NOUVEAU_BO_WR);
126 BEGIN_RING(chan, RING_MF(LINE_LENGTH_IN), 2);
127 OUT_RING (chan, nr * 4);
128 OUT_RING (chan, 1);
129 BEGIN_RING(chan, RING_MF(EXEC), 1);
130 OUT_RING (chan, 0x100111);
131
132 /* must not be interrupted (trap on QUERY fence, 0x50 works however) */
133 BEGIN_RING_NI(chan, RING_MF(DATA), nr);
134 OUT_RINGp (chan, src, nr);
135
136 count -= nr;
137 src += nr;
138 offset += nr * 4;
139 }
140 }
141
142 void
143 nvc0_m2mf_copy_linear(struct nouveau_context *nv,
144 struct nouveau_bo *dst, unsigned dstoff, unsigned dstdom,
145 struct nouveau_bo *src, unsigned srcoff, unsigned srcdom,
146 unsigned size)
147 {
148 struct nouveau_channel *chan = nv->screen->channel;
149
150 while (size) {
151 unsigned bytes = MIN2(size, 1 << 17);
152
153 MARK_RING (chan, 11, 4);
154
155 BEGIN_RING(chan, RING_MF(OFFSET_OUT_HIGH), 2);
156 OUT_RELOCh(chan, dst, dstoff, dstdom | NOUVEAU_BO_WR);
157 OUT_RELOCl(chan, dst, dstoff, dstdom | NOUVEAU_BO_WR);
158 BEGIN_RING(chan, RING_MF(OFFSET_IN_HIGH), 2);
159 OUT_RELOCh(chan, src, srcoff, srcdom | NOUVEAU_BO_RD);
160 OUT_RELOCl(chan, src, srcoff, srcdom | NOUVEAU_BO_RD);
161 BEGIN_RING(chan, RING_MF(LINE_LENGTH_IN), 2);
162 OUT_RING (chan, bytes);
163 OUT_RING (chan, 1);
164 BEGIN_RING(chan, RING_MF(EXEC), 1);
165 OUT_RING (chan, (1 << NVC0_M2MF_EXEC_INC__SHIFT) |
166 NVC0_M2MF_EXEC_LINEAR_IN | NVC0_M2MF_EXEC_LINEAR_OUT);
167
168 srcoff += bytes;
169 dstoff += bytes;
170 size -= bytes;
171 }
172 }
173
174 #if 0
175 static void
176 nvc0_m2mf_push_rect(struct pipe_screen *pscreen,
177 const struct nv50_m2mf_rect *dst,
178 const void *data,
179 unsigned nblocksx, unsigned nblocksy)
180 {
181 struct nouveau_channel *chan;
182 const uint8_t *src = (const uint8_t *)data;
183 const int cpp = dst->cpp;
184 const int line_len = nblocksx * cpp;
185 int dy = dst->y;
186
187 assert(nouveau_bo_tile_layout(dst->bo));
188
189 BEGIN_RING(chan, RING_MF(TILING_MODE_OUT), 5);
190 OUT_RING (chan, dst->tile_mode);
191 OUT_RING (chan, dst->width * cpp);
192 OUT_RING (chan, dst->height);
193 OUT_RING (chan, dst->depth);
194 OUT_RING (chan, dst->z);
195
196 while (nblocksy) {
197 int line_count, words;
198 int size = MIN2(AVAIL_RING(chan), NV04_PFIFO_MAX_PACKET_LEN);
199
200 if (size < (12 + words)) {
201 FIRE_RING(chan);
202 continue;
203 }
204 line_count = (size * 4) / line_len;
205 words = (line_count * line_len + 3) / 4;
206
207 BEGIN_RING(chan, RING_MF(OFFSET_OUT_HIGH), 2);
208 OUT_RELOCh(chan, dst->bo, dst->base, dst->domain | NOUVEAU_BO_WR);
209 OUT_RELOCl(chan, dst->bo, dst->base, dst->domain | NOUVEAU_BO_WR);
210
211 BEGIN_RING(chan, RING_MF(TILING_POSITION_OUT_X), 2);
212 OUT_RING (chan, dst->x * cpp);
213 OUT_RING (chan, dy);
214 BEGIN_RING(chan, RING_MF(LINE_LENGTH_IN), 2);
215 OUT_RING (chan, line_len);
216 OUT_RING (chan, line_count);
217 BEGIN_RING(chan, RING_MF(EXEC), 1);
218 OUT_RING (chan, (1 << NVC0_M2MF_EXEC_INC__SHIFT) |
219 NVC0_M2MF_EXEC_PUSH | NVC0_M2MF_EXEC_LINEAR_IN);
220
221 BEGIN_RING_NI(chan, RING_MF(DATA), words);
222 OUT_RINGp (chan, src, words);
223
224 dy += line_count;
225 src += line_len * line_count;
226 nblocksy -= line_count;
227 }
228 }
229 #endif
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 nv50_miptree *mt = nv50_miptree(res);
242 struct nvc0_transfer *tx;
243 uint32_t size;
244 int ret;
245
246 if (usage & (PIPE_TRANSFER_MAP_DIRECTLY | PIPE_TRANSFER_MAP_PERMANENTLY))
247 return NULL;
248
249 tx = CALLOC_STRUCT(nvc0_transfer);
250 if (!tx)
251 return NULL;
252
253 pipe_resource_reference(&tx->base.resource, res);
254
255 tx->base.level = level;
256 tx->base.usage = usage;
257 tx->base.box = *box;
258
259 if (util_format_is_plain(res->format)) {
260 tx->nblocksx = box->width << mt->ms_x;
261 tx->nblocksy = box->height << mt->ms_y;
262 } else {
263 tx->nblocksx = util_format_get_nblocksx(res->format, box->width);
264 tx->nblocksy = util_format_get_nblocksy(res->format, box->height);
265 }
266 tx->nlayers = box->depth;
267
268 tx->base.stride = tx->nblocksx * util_format_get_blocksize(res->format);
269 tx->base.layer_stride = tx->nblocksy * tx->base.stride;
270
271 nv50_m2mf_rect_setup(&tx->rect[0], res, level, box->x, box->y, box->z);
272
273 size = tx->base.layer_stride;
274
275 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0,
276 size * tx->nlayers, &tx->rect[1].bo);
277 if (ret) {
278 FREE(tx);
279 return NULL;
280 }
281
282 tx->rect[1].cpp = tx->rect[0].cpp;
283 tx->rect[1].width = tx->nblocksx;
284 tx->rect[1].height = tx->nblocksy;
285 tx->rect[1].depth = 1;
286 tx->rect[1].pitch = tx->base.stride;
287 tx->rect[1].domain = NOUVEAU_BO_GART;
288
289 if (usage & PIPE_TRANSFER_READ) {
290 unsigned base = tx->rect[0].base;
291 unsigned z = tx->rect[0].z;
292 unsigned i;
293 for (i = 0; i < tx->nlayers; ++i) {
294 nvc0_m2mf_transfer_rect(pscreen, &tx->rect[1], &tx->rect[0],
295 tx->nblocksx, tx->nblocksy);
296 if (mt->layout_3d)
297 tx->rect[0].z++;
298 else
299 tx->rect[0].base += mt->layer_stride;
300 tx->rect[1].base += size;
301 }
302 tx->rect[0].z = z;
303 tx->rect[0].base = base;
304 tx->rect[1].base = 0;
305 }
306
307 return &tx->base;
308 }
309
310 void
311 nvc0_miptree_transfer_del(struct pipe_context *pctx,
312 struct pipe_transfer *transfer)
313 {
314 struct pipe_screen *pscreen = pctx->screen;
315 struct nvc0_transfer *tx = (struct nvc0_transfer *)transfer;
316 struct nv50_miptree *mt = nv50_miptree(tx->base.resource);
317 unsigned i;
318
319 if (tx->base.usage & PIPE_TRANSFER_WRITE) {
320 for (i = 0; i < tx->nlayers; ++i) {
321 nvc0_m2mf_transfer_rect(pscreen, &tx->rect[0], &tx->rect[1],
322 tx->nblocksx, tx->nblocksy);
323 if (mt->layout_3d)
324 tx->rect[0].z++;
325 else
326 tx->rect[0].base += mt->layer_stride;
327 tx->rect[1].base += tx->nblocksy * tx->base.stride;
328 }
329 }
330
331 nouveau_bo_ref(NULL, &tx->rect[1].bo);
332 pipe_resource_reference(&transfer->resource, NULL);
333
334 FREE(tx);
335 }
336
337 void *
338 nvc0_miptree_transfer_map(struct pipe_context *pctx,
339 struct pipe_transfer *transfer)
340 {
341 struct nvc0_transfer *tx = (struct nvc0_transfer *)transfer;
342 int ret;
343 unsigned flags = 0;
344
345 if (tx->rect[1].bo->map)
346 return tx->rect[1].bo->map;
347
348 if (transfer->usage & PIPE_TRANSFER_READ)
349 flags = NOUVEAU_BO_RD;
350 if (transfer->usage & PIPE_TRANSFER_WRITE)
351 flags |= NOUVEAU_BO_WR;
352
353 ret = nouveau_bo_map(tx->rect[1].bo, flags);
354 if (ret)
355 return NULL;
356 return tx->rect[1].bo->map;
357 }
358
359 void
360 nvc0_miptree_transfer_unmap(struct pipe_context *pctx,
361 struct pipe_transfer *transfer)
362 {
363 struct nvc0_transfer *tx = (struct nvc0_transfer *)transfer;
364
365 nouveau_bo_unmap(tx->rect[1].bo);
366 }
367
368 void
369 nvc0_cb_push(struct nouveau_context *nv,
370 struct nouveau_bo *bo, unsigned domain,
371 unsigned base, unsigned size,
372 unsigned offset, unsigned words, const uint32_t *data)
373 {
374 struct nouveau_channel *chan = nv->screen->channel;
375
376 assert(!(offset & 3));
377 size = align(size, 0x100);
378
379 MARK_RING (chan, 16, 2);
380 BEGIN_RING(chan, RING_3D(CB_SIZE), 3);
381 OUT_RING (chan, size);
382 OUT_RELOCh(chan, bo, base, domain | NOUVEAU_BO_WR);
383 OUT_RELOCl(chan, bo, base, domain | NOUVEAU_BO_WR);
384
385 while (words) {
386 unsigned nr = AVAIL_RING(chan);
387 nr = MIN2(nr, words);
388 nr = MIN2(nr, NV04_PFIFO_MAX_PACKET_LEN - 1);
389
390 BEGIN_RING_1I(chan, RING_3D(CB_POS), nr + 1);
391 OUT_RING (chan, offset);
392 OUT_RINGp (chan, data, nr);
393
394 words -= nr;
395 data += nr;
396 offset += nr * 4;
397
398 if (words) {
399 MARK_RING(chan, 6, 1);
400 nouveau_bo_validate(chan, bo, domain | NOUVEAU_BO_WR);
401 }
402 }
403 }