2 #include "util/u_format.h"
4 #include "nvc0/nvc0_context.h"
7 struct pipe_transfer base;
8 struct nv50_m2mf_rect rect[2];
15 nvc0_m2mf_transfer_rect(struct nvc0_context *nvc0,
16 const struct nv50_m2mf_rect *dst,
17 const struct nv50_m2mf_rect *src,
18 uint32_t nblocksx, uint32_t nblocksy)
20 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
21 struct nouveau_bufctx *bctx = nvc0->bufctx;
22 const int cpp = dst->cpp;
23 uint32_t src_ofst = src->base;
24 uint32_t dst_ofst = dst->base;
25 uint32_t height = nblocksy;
28 uint32_t exec = (1 << 20);
30 assert(dst->cpp == src->cpp);
32 nouveau_bufctx_refn(bctx, 0, src->bo, src->domain | NOUVEAU_BO_RD);
33 nouveau_bufctx_refn(bctx, 0, dst->bo, dst->domain | NOUVEAU_BO_WR);
34 nouveau_pushbuf_bufctx(push, bctx);
35 nouveau_pushbuf_validate(push);
37 if (nouveau_bo_memtype(src->bo)) {
38 BEGIN_NVC0(push, NVC0_M2MF(TILING_MODE_IN), 5);
39 PUSH_DATA (push, src->tile_mode);
40 PUSH_DATA (push, src->width * cpp);
41 PUSH_DATA (push, src->height);
42 PUSH_DATA (push, src->depth);
43 PUSH_DATA (push, src->z);
45 src_ofst += src->y * src->pitch + src->x * cpp;
47 BEGIN_NVC0(push, NVC0_M2MF(PITCH_IN), 1);
48 PUSH_DATA (push, src->width * cpp);
50 exec |= NVC0_M2MF_EXEC_LINEAR_IN;
53 if (nouveau_bo_memtype(dst->bo)) {
54 BEGIN_NVC0(push, NVC0_M2MF(TILING_MODE_OUT), 5);
55 PUSH_DATA (push, dst->tile_mode);
56 PUSH_DATA (push, dst->width * cpp);
57 PUSH_DATA (push, dst->height);
58 PUSH_DATA (push, dst->depth);
59 PUSH_DATA (push, dst->z);
61 dst_ofst += dst->y * dst->pitch + dst->x * cpp;
63 BEGIN_NVC0(push, NVC0_M2MF(PITCH_OUT), 1);
64 PUSH_DATA (push, dst->width * cpp);
66 exec |= NVC0_M2MF_EXEC_LINEAR_OUT;
70 int line_count = height > 2047 ? 2047 : height;
72 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_IN_HIGH), 2);
73 PUSH_DATAh(push, src->bo->offset + src_ofst);
74 PUSH_DATA (push, src->bo->offset + src_ofst);
76 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_OUT_HIGH), 2);
77 PUSH_DATAh(push, dst->bo->offset + dst_ofst);
78 PUSH_DATA (push, dst->bo->offset + dst_ofst);
80 if (!(exec & NVC0_M2MF_EXEC_LINEAR_IN)) {
81 BEGIN_NVC0(push, NVC0_M2MF(TILING_POSITION_IN_X), 2);
82 PUSH_DATA (push, src->x * cpp);
85 src_ofst += line_count * src->pitch;
87 if (!(exec & NVC0_M2MF_EXEC_LINEAR_OUT)) {
88 BEGIN_NVC0(push, NVC0_M2MF(TILING_POSITION_OUT_X), 2);
89 PUSH_DATA (push, dst->x * cpp);
92 dst_ofst += line_count * dst->pitch;
95 BEGIN_NVC0(push, NVC0_M2MF(LINE_LENGTH_IN), 2);
96 PUSH_DATA (push, nblocksx * cpp);
97 PUSH_DATA (push, line_count);
98 BEGIN_NVC0(push, NVC0_M2MF(EXEC), 1);
99 PUSH_DATA (push, exec);
101 height -= line_count;
106 nouveau_bufctx_reset(bctx, 0);
110 nve4_m2mf_transfer_rect(struct nvc0_context *nvc0,
111 const struct nv50_m2mf_rect *dst,
112 const struct nv50_m2mf_rect *src,
113 uint32_t nblocksx, uint32_t nblocksy)
115 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
116 struct nouveau_bufctx *bctx = nvc0->bufctx;
118 uint32_t src_base = src->base;
119 uint32_t dst_base = dst->base;
120 const int cpp = dst->cpp;
122 assert(dst->cpp == src->cpp);
124 nouveau_bufctx_refn(bctx, 0, dst->bo, dst->domain | NOUVEAU_BO_WR);
125 nouveau_bufctx_refn(bctx, 0, src->bo, src->domain | NOUVEAU_BO_RD);
126 nouveau_pushbuf_bufctx(push, bctx);
127 nouveau_pushbuf_validate(push);
129 exec = 0x200 /* 2D_ENABLE */ | 0x6 /* UNK */;
131 if (!nouveau_bo_memtype(dst->bo)) {
133 dst_base += dst->y * dst->pitch + dst->x * cpp;
134 exec |= 0x100; /* DST_MODE_2D_LINEAR */
136 if (!nouveau_bo_memtype(src->bo)) {
138 src_base += src->y * src->pitch + src->x * cpp;
139 exec |= 0x080; /* SRC_MODE_2D_LINEAR */
142 BEGIN_NVC0(push, SUBC_COPY(0x070c), 6);
143 PUSH_DATA (push, 0x1000 | dst->tile_mode);
144 PUSH_DATA (push, dst->pitch);
145 PUSH_DATA (push, dst->height);
146 PUSH_DATA (push, dst->depth);
147 PUSH_DATA (push, dst->z);
148 PUSH_DATA (push, (dst->y << 16) | (dst->x * cpp));
150 BEGIN_NVC0(push, SUBC_COPY(0x0728), 6);
151 PUSH_DATA (push, 0x1000 | src->tile_mode);
152 PUSH_DATA (push, src->pitch);
153 PUSH_DATA (push, src->height);
154 PUSH_DATA (push, src->depth);
155 PUSH_DATA (push, src->z);
156 PUSH_DATA (push, (src->y << 16) | (src->x * cpp));
158 BEGIN_NVC0(push, SUBC_COPY(0x0400), 8);
159 PUSH_DATAh(push, src->bo->offset + src_base);
160 PUSH_DATA (push, src->bo->offset + src_base);
161 PUSH_DATAh(push, dst->bo->offset + dst_base);
162 PUSH_DATA (push, dst->bo->offset + dst_base);
163 PUSH_DATA (push, src->pitch);
164 PUSH_DATA (push, dst->pitch);
165 PUSH_DATA (push, nblocksx * cpp);
166 PUSH_DATA (push, nblocksy);
168 BEGIN_NVC0(push, SUBC_COPY(0x0300), 1);
169 PUSH_DATA (push, exec);
171 nouveau_bufctx_reset(bctx, 0);
175 nvc0_m2mf_push_linear(struct nouveau_context *nv,
176 struct nouveau_bo *dst, unsigned offset, unsigned domain,
177 unsigned size, const void *data)
179 struct nvc0_context *nvc0 = nvc0_context(&nv->pipe);
180 struct nouveau_pushbuf *push = nv->pushbuf;
181 uint32_t *src = (uint32_t *)data;
182 unsigned count = (size + 3) / 4;
184 nouveau_bufctx_refn(nvc0->bufctx, 0, dst, domain | NOUVEAU_BO_WR);
185 nouveau_pushbuf_bufctx(push, nvc0->bufctx);
186 nouveau_pushbuf_validate(push);
189 unsigned nr = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN);
191 if (!PUSH_SPACE(push, nr + 9))
194 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_OUT_HIGH), 2);
195 PUSH_DATAh(push, dst->offset + offset);
196 PUSH_DATA (push, dst->offset + offset);
197 BEGIN_NVC0(push, NVC0_M2MF(LINE_LENGTH_IN), 2);
198 PUSH_DATA (push, MIN2(size, nr * 4));
200 BEGIN_NVC0(push, NVC0_M2MF(EXEC), 1);
201 PUSH_DATA (push, 0x100111);
203 /* must not be interrupted (trap on QUERY fence, 0x50 works however) */
204 BEGIN_NIC0(push, NVC0_M2MF(DATA), nr);
205 PUSH_DATAp(push, src, nr);
213 nouveau_bufctx_reset(nvc0->bufctx, 0);
217 nve4_p2mf_push_linear(struct nouveau_context *nv,
218 struct nouveau_bo *dst, unsigned offset, unsigned domain,
219 unsigned size, const void *data)
221 struct nvc0_context *nvc0 = nvc0_context(&nv->pipe);
222 struct nouveau_pushbuf *push = nv->pushbuf;
223 uint32_t *src = (uint32_t *)data;
224 unsigned count = (size + 3) / 4;
226 nouveau_bufctx_refn(nvc0->bufctx, 0, dst, domain | NOUVEAU_BO_WR);
227 nouveau_pushbuf_bufctx(push, nvc0->bufctx);
228 nouveau_pushbuf_validate(push);
231 unsigned nr = MIN2(count, (NV04_PFIFO_MAX_PACKET_LEN - 1));
233 if (!PUSH_SPACE(push, nr + 10))
236 BEGIN_NVC0(push, NVE4_P2MF(UPLOAD_DST_ADDRESS_HIGH), 2);
237 PUSH_DATAh(push, dst->offset + offset);
238 PUSH_DATA (push, dst->offset + offset);
239 BEGIN_NVC0(push, NVE4_P2MF(UPLOAD_LINE_LENGTH_IN), 2);
240 PUSH_DATA (push, MIN2(size, nr * 4));
242 /* must not be interrupted (trap on QUERY fence, 0x50 works however) */
243 BEGIN_1IC0(push, NVE4_P2MF(UPLOAD_EXEC), nr + 1);
244 PUSH_DATA (push, 0x1001);
245 PUSH_DATAp(push, src, nr);
253 nouveau_bufctx_reset(nvc0->bufctx, 0);
257 nvc0_m2mf_copy_linear(struct nouveau_context *nv,
258 struct nouveau_bo *dst, unsigned dstoff, unsigned dstdom,
259 struct nouveau_bo *src, unsigned srcoff, unsigned srcdom,
262 struct nouveau_pushbuf *push = nv->pushbuf;
263 struct nouveau_bufctx *bctx = nvc0_context(&nv->pipe)->bufctx;
265 nouveau_bufctx_refn(bctx, 0, src, srcdom | NOUVEAU_BO_RD);
266 nouveau_bufctx_refn(bctx, 0, dst, dstdom | NOUVEAU_BO_WR);
267 nouveau_pushbuf_bufctx(push, bctx);
268 nouveau_pushbuf_validate(push);
271 unsigned bytes = MIN2(size, 1 << 17);
273 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_OUT_HIGH), 2);
274 PUSH_DATAh(push, dst->offset + dstoff);
275 PUSH_DATA (push, dst->offset + dstoff);
276 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_IN_HIGH), 2);
277 PUSH_DATAh(push, src->offset + srcoff);
278 PUSH_DATA (push, src->offset + srcoff);
279 BEGIN_NVC0(push, NVC0_M2MF(LINE_LENGTH_IN), 2);
280 PUSH_DATA (push, bytes);
282 BEGIN_NVC0(push, NVC0_M2MF(EXEC), 1);
283 PUSH_DATA (push, NVC0_M2MF_EXEC_QUERY_SHORT |
284 NVC0_M2MF_EXEC_LINEAR_IN | NVC0_M2MF_EXEC_LINEAR_OUT);
291 nouveau_bufctx_reset(bctx, 0);
295 nve4_m2mf_copy_linear(struct nouveau_context *nv,
296 struct nouveau_bo *dst, unsigned dstoff, unsigned dstdom,
297 struct nouveau_bo *src, unsigned srcoff, unsigned srcdom,
300 struct nouveau_pushbuf *push = nv->pushbuf;
301 struct nouveau_bufctx *bctx = nvc0_context(&nv->pipe)->bufctx;
303 nouveau_bufctx_refn(bctx, 0, src, srcdom | NOUVEAU_BO_RD);
304 nouveau_bufctx_refn(bctx, 0, dst, dstdom | NOUVEAU_BO_WR);
305 nouveau_pushbuf_bufctx(push, bctx);
306 nouveau_pushbuf_validate(push);
308 BEGIN_NVC0(push, SUBC_COPY(0x0400), 4);
309 PUSH_DATAh(push, src->offset + srcoff);
310 PUSH_DATA (push, src->offset + srcoff);
311 PUSH_DATAh(push, dst->offset + dstoff);
312 PUSH_DATA (push, dst->offset + dstoff);
313 BEGIN_NVC0(push, SUBC_COPY(0x0418), 1);
314 PUSH_DATA (push, size);
315 BEGIN_NVC0(push, SUBC_COPY(0x0300), 1);
316 PUSH_DATA (push, 0x186);
318 nouveau_bufctx_reset(bctx, 0);
323 nvc0_mt_transfer_can_map_directly(struct nv50_miptree *mt)
325 if (mt->base.domain == NOUVEAU_BO_VRAM)
327 if (mt->base.base.usage != PIPE_USAGE_STAGING)
329 return !nouveau_bo_memtype(mt->base.bo);
333 nvc0_mt_sync(struct nvc0_context *nvc0, struct nv50_miptree *mt, unsigned usage)
336 uint32_t access = (usage & PIPE_TRANSFER_WRITE) ?
337 NOUVEAU_BO_WR : NOUVEAU_BO_RD;
338 return !nouveau_bo_wait(mt->base.bo, access, nvc0->base.client);
340 if (usage & PIPE_TRANSFER_WRITE)
341 return !mt->base.fence || nouveau_fence_wait(mt->base.fence, &nvc0->base.debug);
342 return !mt->base.fence_wr || nouveau_fence_wait(mt->base.fence_wr, &nvc0->base.debug);
346 nvc0_miptree_transfer_map_unlocked(
347 struct pipe_context *pctx,
348 struct pipe_resource *res,
351 const struct pipe_box *box,
352 struct pipe_transfer **ptransfer)
354 struct nvc0_context *nvc0 = nvc0_context(pctx);
355 struct nvc0_screen *screen = nvc0->screen;
356 struct nouveau_device *dev = screen->base.device;
357 struct nv50_miptree *mt = nv50_miptree(res);
358 struct nvc0_transfer *tx;
363 if (nvc0_mt_transfer_can_map_directly(mt)) {
364 ret = !nvc0_mt_sync(nvc0, mt, usage);
366 ret = nouveau_bo_map(mt->base.bo, 0, NULL);
368 (usage & PIPE_TRANSFER_MAP_DIRECTLY))
371 usage |= PIPE_TRANSFER_MAP_DIRECTLY;
373 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
376 tx = CALLOC_STRUCT(nvc0_transfer);
380 pipe_resource_reference(&tx->base.resource, res);
382 tx->base.level = level;
383 tx->base.usage = usage;
386 if (util_format_is_plain(res->format)) {
387 tx->nblocksx = box->width << mt->ms_x;
388 tx->nblocksy = box->height << mt->ms_y;
390 tx->nblocksx = util_format_get_nblocksx(res->format, box->width);
391 tx->nblocksy = util_format_get_nblocksy(res->format, box->height);
393 tx->nlayers = box->depth;
395 if (usage & PIPE_TRANSFER_MAP_DIRECTLY) {
396 tx->base.stride = mt->level[level].pitch;
397 tx->base.layer_stride = mt->layer_stride;
398 uint32_t offset = box->y * tx->base.stride +
399 util_format_get_stride(res->format, box->x);
401 offset += mt->layer_stride * box->z;
403 offset += nvc0_mt_zslice_offset(mt, level, box->z);
404 *ptransfer = &tx->base;
405 return mt->base.bo->map + mt->base.offset + offset;
408 tx->base.stride = tx->nblocksx * util_format_get_blocksize(res->format);
409 tx->base.layer_stride = tx->nblocksy * tx->base.stride;
411 nv50_m2mf_rect_setup(&tx->rect[0], res, level, box->x, box->y, box->z);
413 size = tx->base.layer_stride;
415 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0,
416 size * tx->nlayers, NULL, &tx->rect[1].bo);
418 pipe_resource_reference(&tx->base.resource, NULL);
423 tx->rect[1].cpp = tx->rect[0].cpp;
424 tx->rect[1].width = tx->nblocksx;
425 tx->rect[1].height = tx->nblocksy;
426 tx->rect[1].depth = 1;
427 tx->rect[1].pitch = tx->base.stride;
428 tx->rect[1].domain = NOUVEAU_BO_GART;
430 if (usage & PIPE_TRANSFER_READ) {
431 unsigned base = tx->rect[0].base;
432 unsigned z = tx->rect[0].z;
434 for (i = 0; i < tx->nlayers; ++i) {
435 nvc0->m2mf_copy_rect(nvc0, &tx->rect[1], &tx->rect[0],
436 tx->nblocksx, tx->nblocksy);
440 tx->rect[0].base += mt->layer_stride;
441 tx->rect[1].base += size;
444 tx->rect[0].base = base;
445 tx->rect[1].base = 0;
448 if (tx->rect[1].bo->map) {
449 *ptransfer = &tx->base;
450 return tx->rect[1].bo->map;
453 if (usage & PIPE_TRANSFER_READ)
454 flags = NOUVEAU_BO_RD;
455 if (usage & PIPE_TRANSFER_WRITE)
456 flags |= NOUVEAU_BO_WR;
458 ret = nouveau_bo_map(tx->rect[1].bo, flags, nvc0->screen->base.client);
460 pipe_resource_reference(&tx->base.resource, NULL);
461 nouveau_bo_ref(NULL, &tx->rect[1].bo);
466 *ptransfer = &tx->base;
467 return tx->rect[1].bo->map;
471 nvc0_miptree_transfer_map(
472 struct pipe_context *pctx,
473 struct pipe_resource *res,
476 const struct pipe_box *box,
477 struct pipe_transfer **ptransfer)
479 struct nvc0_context *nvc0 = nvc0_context(pctx);
480 struct nvc0_screen *screen = nvc0->screen;
482 pipe_mutex_lock(screen->base.push_mutex);
483 void *ret = nvc0_miptree_transfer_map_unlocked(
484 pctx, res, level, usage, box, ptransfer);
485 pipe_mutex_unlock(screen->base.push_mutex);
491 nvc0_miptree_transfer_unmap_unlocked(struct pipe_context *pctx,
492 struct pipe_transfer *transfer)
494 struct nvc0_context *nvc0 = nvc0_context(pctx);
495 struct nvc0_transfer *tx = (struct nvc0_transfer *)transfer;
496 struct nv50_miptree *mt = nv50_miptree(tx->base.resource);
499 if (tx->base.usage & PIPE_TRANSFER_MAP_DIRECTLY) {
500 pipe_resource_reference(&transfer->resource, NULL);
506 if (tx->base.usage & PIPE_TRANSFER_WRITE) {
507 for (i = 0; i < tx->nlayers; ++i) {
508 nvc0->m2mf_copy_rect(nvc0, &tx->rect[0], &tx->rect[1],
509 tx->nblocksx, tx->nblocksy);
513 tx->rect[0].base += mt->layer_stride;
514 tx->rect[1].base += tx->nblocksy * tx->base.stride;
516 NOUVEAU_DRV_STAT(&nvc0->screen->base, tex_transfers_wr, 1);
518 /* Allow the copies above to finish executing before freeing the source */
519 nouveau_fence_work(nvc0->screen->base.fence.current,
520 nouveau_fence_unref_bo, tx->rect[1].bo);
522 nouveau_bo_ref(NULL, &tx->rect[1].bo);
524 if (tx->base.usage & PIPE_TRANSFER_READ)
525 NOUVEAU_DRV_STAT(&nvc0->screen->base, tex_transfers_rd, 1);
527 pipe_resource_reference(&transfer->resource, NULL);
533 nvc0_miptree_transfer_unmap(struct pipe_context *pctx,
534 struct pipe_transfer *transfer)
536 struct nvc0_context *nvc0 = nvc0_context(pctx);
537 struct nvc0_screen *screen = nvc0->screen;
539 pipe_mutex_lock(screen->base.push_mutex);
540 nvc0_miptree_transfer_unmap_unlocked(pctx, transfer);
541 pipe_mutex_unlock(screen->base.push_mutex);
544 /* This happens rather often with DTD9/st. */
546 nvc0_cb_push(struct nouveau_context *nv,
547 struct nv04_resource *res,
548 unsigned offset, unsigned words, const uint32_t *data)
550 struct nvc0_context *nvc0 = nvc0_context(&nv->pipe);
551 struct nvc0_constbuf *cb = NULL;
554 /* Go through all the constbuf binding points of this buffer and try to
555 * find one which contains the region to be updated.
557 for (s = 0; s < 6 && !cb; s++) {
558 uint16_t bindings = res->cb_bindings[s];
560 int i = ffs(bindings) - 1;
561 uint32_t cb_offset = nvc0->constbuf[s][i].offset;
563 bindings &= ~(1 << i);
564 if (cb_offset <= offset &&
565 cb_offset + nvc0->constbuf[s][i].size >= offset + words * 4) {
566 cb = &nvc0->constbuf[s][i];
573 nvc0_cb_bo_push(nv, res->bo, res->domain,
574 res->offset + cb->offset, cb->size,
575 offset - cb->offset, words, data);
577 nv->push_data(nv, res->bo, res->offset + offset, res->domain,
583 nvc0_cb_bo_push(struct nouveau_context *nv,
584 struct nouveau_bo *bo, unsigned domain,
585 unsigned base, unsigned size,
586 unsigned offset, unsigned words, const uint32_t *data)
588 struct nouveau_pushbuf *push = nv->pushbuf;
590 NOUVEAU_DRV_STAT(nv->screen, constbuf_upload_count, 1);
591 NOUVEAU_DRV_STAT(nv->screen, constbuf_upload_bytes, words * 4);
593 assert(!(offset & 3));
594 size = align(size, 0x100);
596 assert(offset < size);
597 assert(offset + words * 4 <= size);
599 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
600 PUSH_DATA (push, size);
601 PUSH_DATAh(push, bo->offset + base);
602 PUSH_DATA (push, bo->offset + base);
605 unsigned nr = MIN2(words, NV04_PFIFO_MAX_PACKET_LEN - 1);
607 PUSH_SPACE(push, nr + 2);
608 PUSH_REFN (push, bo, NOUVEAU_BO_WR | domain);
609 BEGIN_1IC0(push, NVC0_3D(CB_POS), nr + 1);
610 PUSH_DATA (push, offset);
611 PUSH_DATAp(push, data, nr);
620 nvc0_init_transfer_functions(struct nvc0_context *nvc0)
622 if (nvc0->screen->base.class_3d >= NVE4_3D_CLASS) {
623 nvc0->m2mf_copy_rect = nve4_m2mf_transfer_rect;
624 nvc0->base.copy_data = nve4_m2mf_copy_linear;
625 nvc0->base.push_data = nve4_p2mf_push_linear;
627 nvc0->m2mf_copy_rect = nvc0_m2mf_transfer_rect;
628 nvc0->base.copy_data = nvc0_m2mf_copy_linear;
629 nvc0->base.push_data = nvc0_m2mf_push_linear;
631 nvc0->base.push_cb = nvc0_cb_push;