2 #include "util/u_format.h"
4 #include "nvc0/nvc0_context.h"
6 #include "nv50/nv50_defs.xml.h"
9 struct pipe_transfer base;
10 struct nv50_m2mf_rect rect[2];
17 nvc0_m2mf_transfer_rect(struct nvc0_context *nvc0,
18 const struct nv50_m2mf_rect *dst,
19 const struct nv50_m2mf_rect *src,
20 uint32_t nblocksx, uint32_t nblocksy)
22 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
23 struct nouveau_bufctx *bctx = nvc0->bufctx;
24 const int cpp = dst->cpp;
25 uint32_t src_ofst = src->base;
26 uint32_t dst_ofst = dst->base;
27 uint32_t height = nblocksy;
30 uint32_t exec = (1 << 20);
32 assert(dst->cpp == src->cpp);
34 nouveau_bufctx_refn(bctx, 0, src->bo, src->domain | NOUVEAU_BO_RD);
35 nouveau_bufctx_refn(bctx, 0, dst->bo, dst->domain | NOUVEAU_BO_WR);
36 nouveau_pushbuf_bufctx(push, bctx);
37 nouveau_pushbuf_validate(push);
39 if (nouveau_bo_memtype(src->bo)) {
40 BEGIN_NVC0(push, NVC0_M2MF(TILING_MODE_IN), 5);
41 PUSH_DATA (push, src->tile_mode);
42 PUSH_DATA (push, src->width * cpp);
43 PUSH_DATA (push, src->height);
44 PUSH_DATA (push, src->depth);
45 PUSH_DATA (push, src->z);
47 src_ofst += src->y * src->pitch + src->x * cpp;
49 BEGIN_NVC0(push, NVC0_M2MF(PITCH_IN), 1);
50 PUSH_DATA (push, src->width * cpp);
52 exec |= NVC0_M2MF_EXEC_LINEAR_IN;
55 if (nouveau_bo_memtype(dst->bo)) {
56 BEGIN_NVC0(push, NVC0_M2MF(TILING_MODE_OUT), 5);
57 PUSH_DATA (push, dst->tile_mode);
58 PUSH_DATA (push, dst->width * cpp);
59 PUSH_DATA (push, dst->height);
60 PUSH_DATA (push, dst->depth);
61 PUSH_DATA (push, dst->z);
63 dst_ofst += dst->y * dst->pitch + dst->x * cpp;
65 BEGIN_NVC0(push, NVC0_M2MF(PITCH_OUT), 1);
66 PUSH_DATA (push, dst->width * cpp);
68 exec |= NVC0_M2MF_EXEC_LINEAR_OUT;
72 int line_count = height > 2047 ? 2047 : height;
74 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_IN_HIGH), 2);
75 PUSH_DATAh(push, src->bo->offset + src_ofst);
76 PUSH_DATA (push, src->bo->offset + src_ofst);
78 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_OUT_HIGH), 2);
79 PUSH_DATAh(push, dst->bo->offset + dst_ofst);
80 PUSH_DATA (push, dst->bo->offset + dst_ofst);
82 if (!(exec & NVC0_M2MF_EXEC_LINEAR_IN)) {
83 BEGIN_NVC0(push, NVC0_M2MF(TILING_POSITION_IN_X), 2);
84 PUSH_DATA (push, src->x * cpp);
87 src_ofst += line_count * src->pitch;
89 if (!(exec & NVC0_M2MF_EXEC_LINEAR_OUT)) {
90 BEGIN_NVC0(push, NVC0_M2MF(TILING_POSITION_OUT_X), 2);
91 PUSH_DATA (push, dst->x * cpp);
94 dst_ofst += line_count * dst->pitch;
97 BEGIN_NVC0(push, NVC0_M2MF(LINE_LENGTH_IN), 2);
98 PUSH_DATA (push, nblocksx * cpp);
99 PUSH_DATA (push, line_count);
100 BEGIN_NVC0(push, NVC0_M2MF(EXEC), 1);
101 PUSH_DATA (push, exec);
103 height -= line_count;
108 nouveau_bufctx_reset(bctx, 0);
112 nve4_m2mf_transfer_rect(struct nvc0_context *nvc0,
113 const struct nv50_m2mf_rect *dst,
114 const struct nv50_m2mf_rect *src,
115 uint32_t nblocksx, uint32_t nblocksy)
117 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
118 struct nouveau_bufctx *bctx = nvc0->bufctx;
120 uint32_t src_base = src->base;
121 uint32_t dst_base = dst->base;
122 const int cpp = dst->cpp;
124 assert(dst->cpp == src->cpp);
126 nouveau_bufctx_refn(bctx, 0, dst->bo, dst->domain | NOUVEAU_BO_WR);
127 nouveau_bufctx_refn(bctx, 0, src->bo, src->domain | NOUVEAU_BO_RD);
128 nouveau_pushbuf_bufctx(push, bctx);
129 nouveau_pushbuf_validate(push);
131 exec = 0x200 /* 2D_ENABLE */ | 0x6 /* UNK */;
133 if (!nouveau_bo_memtype(dst->bo)) {
135 dst_base += dst->y * dst->pitch + dst->x * cpp;
136 exec |= 0x100; /* DST_MODE_2D_LINEAR */
138 if (!nouveau_bo_memtype(src->bo)) {
140 src_base += src->y * src->pitch + src->x * cpp;
141 exec |= 0x080; /* SRC_MODE_2D_LINEAR */
144 BEGIN_NVC0(push, SUBC_COPY(0x070c), 6);
145 PUSH_DATA (push, 0x1000 | dst->tile_mode);
146 PUSH_DATA (push, dst->pitch);
147 PUSH_DATA (push, dst->height);
148 PUSH_DATA (push, dst->depth);
149 PUSH_DATA (push, dst->z);
150 PUSH_DATA (push, (dst->y << 16) | (dst->x * cpp));
152 BEGIN_NVC0(push, SUBC_COPY(0x0728), 6);
153 PUSH_DATA (push, 0x1000 | src->tile_mode);
154 PUSH_DATA (push, src->pitch);
155 PUSH_DATA (push, src->height);
156 PUSH_DATA (push, src->depth);
157 PUSH_DATA (push, src->z);
158 PUSH_DATA (push, (src->y << 16) | (src->x * cpp));
160 BEGIN_NVC0(push, SUBC_COPY(0x0400), 8);
161 PUSH_DATAh(push, src->bo->offset + src_base);
162 PUSH_DATA (push, src->bo->offset + src_base);
163 PUSH_DATAh(push, dst->bo->offset + dst_base);
164 PUSH_DATA (push, dst->bo->offset + dst_base);
165 PUSH_DATA (push, src->pitch);
166 PUSH_DATA (push, dst->pitch);
167 PUSH_DATA (push, nblocksx * cpp);
168 PUSH_DATA (push, nblocksy);
170 BEGIN_NVC0(push, SUBC_COPY(0x0300), 1);
171 PUSH_DATA (push, exec);
173 nouveau_bufctx_reset(bctx, 0);
177 nvc0_m2mf_push_linear(struct nouveau_context *nv,
178 struct nouveau_bo *dst, unsigned offset, unsigned domain,
179 unsigned size, const void *data)
181 struct nvc0_context *nvc0 = nvc0_context(&nv->pipe);
182 struct nouveau_pushbuf *push = nv->pushbuf;
183 uint32_t *src = (uint32_t *)data;
184 unsigned count = (size + 3) / 4;
186 nouveau_bufctx_refn(nvc0->bufctx, 0, dst, domain | NOUVEAU_BO_WR);
187 nouveau_pushbuf_bufctx(push, nvc0->bufctx);
188 nouveau_pushbuf_validate(push);
193 if (!PUSH_SPACE(push, 16))
195 nr = PUSH_AVAIL(push);
197 nr = MIN2(count, nr - 9);
198 nr = MIN2(nr, NV04_PFIFO_MAX_PACKET_LEN);
200 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_OUT_HIGH), 2);
201 PUSH_DATAh(push, dst->offset + offset);
202 PUSH_DATA (push, dst->offset + offset);
203 BEGIN_NVC0(push, NVC0_M2MF(LINE_LENGTH_IN), 2);
204 PUSH_DATA (push, MIN2(size, nr * 4));
206 BEGIN_NVC0(push, NVC0_M2MF(EXEC), 1);
207 PUSH_DATA (push, 0x100111);
209 /* must not be interrupted (trap on QUERY fence, 0x50 works however) */
210 BEGIN_NIC0(push, NVC0_M2MF(DATA), nr);
211 PUSH_DATAp(push, src, nr);
219 nouveau_bufctx_reset(nvc0->bufctx, 0);
223 nve4_p2mf_push_linear(struct nouveau_context *nv,
224 struct nouveau_bo *dst, unsigned offset, unsigned domain,
225 unsigned size, const void *data)
227 struct nvc0_context *nvc0 = nvc0_context(&nv->pipe);
228 struct nouveau_pushbuf *push = nv->pushbuf;
229 uint32_t *src = (uint32_t *)data;
230 unsigned count = (size + 3) / 4;
232 nouveau_bufctx_refn(nvc0->bufctx, 0, dst, domain | NOUVEAU_BO_WR);
233 nouveau_pushbuf_bufctx(push, nvc0->bufctx);
234 nouveau_pushbuf_validate(push);
239 if (!PUSH_SPACE(push, 16))
241 nr = PUSH_AVAIL(push);
243 nr = MIN2(count, nr - 8);
244 nr = MIN2(nr, (NV04_PFIFO_MAX_PACKET_LEN - 1));
246 BEGIN_NVC0(push, NVE4_P2MF(UPLOAD_DST_ADDRESS_HIGH), 2);
247 PUSH_DATAh(push, dst->offset + offset);
248 PUSH_DATA (push, dst->offset + offset);
249 BEGIN_NVC0(push, NVE4_P2MF(UPLOAD_LINE_LENGTH_IN), 2);
250 PUSH_DATA (push, MIN2(size, nr * 4));
252 /* must not be interrupted (trap on QUERY fence, 0x50 works however) */
253 BEGIN_1IC0(push, NVE4_P2MF(UPLOAD_EXEC), nr + 1);
254 PUSH_DATA (push, 0x1001);
255 PUSH_DATAp(push, src, nr);
263 nouveau_bufctx_reset(nvc0->bufctx, 0);
267 nvc0_m2mf_copy_linear(struct nouveau_context *nv,
268 struct nouveau_bo *dst, unsigned dstoff, unsigned dstdom,
269 struct nouveau_bo *src, unsigned srcoff, unsigned srcdom,
272 struct nouveau_pushbuf *push = nv->pushbuf;
273 struct nouveau_bufctx *bctx = nvc0_context(&nv->pipe)->bufctx;
275 nouveau_bufctx_refn(bctx, 0, src, srcdom | NOUVEAU_BO_RD);
276 nouveau_bufctx_refn(bctx, 0, dst, dstdom | NOUVEAU_BO_WR);
277 nouveau_pushbuf_bufctx(push, bctx);
278 nouveau_pushbuf_validate(push);
281 unsigned bytes = MIN2(size, 1 << 17);
283 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_OUT_HIGH), 2);
284 PUSH_DATAh(push, dst->offset + dstoff);
285 PUSH_DATA (push, dst->offset + dstoff);
286 BEGIN_NVC0(push, NVC0_M2MF(OFFSET_IN_HIGH), 2);
287 PUSH_DATAh(push, src->offset + srcoff);
288 PUSH_DATA (push, src->offset + srcoff);
289 BEGIN_NVC0(push, NVC0_M2MF(LINE_LENGTH_IN), 2);
290 PUSH_DATA (push, bytes);
292 BEGIN_NVC0(push, NVC0_M2MF(EXEC), 1);
293 PUSH_DATA (push, NVC0_M2MF_EXEC_QUERY_SHORT |
294 NVC0_M2MF_EXEC_LINEAR_IN | NVC0_M2MF_EXEC_LINEAR_OUT);
301 nouveau_bufctx_reset(bctx, 0);
305 nve4_m2mf_copy_linear(struct nouveau_context *nv,
306 struct nouveau_bo *dst, unsigned dstoff, unsigned dstdom,
307 struct nouveau_bo *src, unsigned srcoff, unsigned srcdom,
310 struct nouveau_pushbuf *push = nv->pushbuf;
311 struct nouveau_bufctx *bctx = nvc0_context(&nv->pipe)->bufctx;
313 nouveau_bufctx_refn(bctx, 0, src, srcdom | NOUVEAU_BO_RD);
314 nouveau_bufctx_refn(bctx, 0, dst, dstdom | NOUVEAU_BO_WR);
315 nouveau_pushbuf_bufctx(push, bctx);
316 nouveau_pushbuf_validate(push);
318 BEGIN_NVC0(push, SUBC_COPY(0x0400), 4);
319 PUSH_DATAh(push, src->offset + srcoff);
320 PUSH_DATA (push, src->offset + srcoff);
321 PUSH_DATAh(push, dst->offset + dstoff);
322 PUSH_DATA (push, dst->offset + dstoff);
323 BEGIN_NVC0(push, SUBC_COPY(0x0418), 1);
324 PUSH_DATA (push, size);
325 BEGIN_NVC0(push, SUBC_COPY(0x0300), 1);
326 PUSH_DATA (push, 0x186);
328 nouveau_bufctx_reset(bctx, 0);
333 nvc0_mt_transfer_can_map_directly(struct nv50_miptree *mt)
335 if (mt->base.domain == NOUVEAU_BO_VRAM)
337 if (mt->base.base.usage != PIPE_USAGE_STAGING)
339 return !nouveau_bo_memtype(mt->base.bo);
343 nvc0_mt_sync(struct nvc0_context *nvc0, struct nv50_miptree *mt, unsigned usage)
346 uint32_t access = (usage & PIPE_TRANSFER_WRITE) ?
347 NOUVEAU_BO_WR : NOUVEAU_BO_RD;
348 return !nouveau_bo_wait(mt->base.bo, access, nvc0->base.client);
350 if (usage & PIPE_TRANSFER_WRITE)
351 return !mt->base.fence || nouveau_fence_wait(mt->base.fence);
352 return !mt->base.fence_wr || nouveau_fence_wait(mt->base.fence_wr);
356 nvc0_miptree_transfer_map(struct pipe_context *pctx,
357 struct pipe_resource *res,
360 const struct pipe_box *box,
361 struct pipe_transfer **ptransfer)
363 struct nvc0_context *nvc0 = nvc0_context(pctx);
364 struct nouveau_device *dev = nvc0->screen->base.device;
365 struct nv50_miptree *mt = nv50_miptree(res);
366 struct nvc0_transfer *tx;
371 if (nvc0_mt_transfer_can_map_directly(mt)) {
372 ret = !nvc0_mt_sync(nvc0, mt, usage);
374 ret = nouveau_bo_map(mt->base.bo, 0, NULL);
376 (usage & PIPE_TRANSFER_MAP_DIRECTLY))
379 usage |= PIPE_TRANSFER_MAP_DIRECTLY;
381 if (usage & PIPE_TRANSFER_MAP_DIRECTLY)
384 tx = CALLOC_STRUCT(nvc0_transfer);
388 pipe_resource_reference(&tx->base.resource, res);
390 tx->base.level = level;
391 tx->base.usage = usage;
394 if (util_format_is_plain(res->format)) {
395 tx->nblocksx = box->width << mt->ms_x;
396 tx->nblocksy = box->height << mt->ms_y;
398 tx->nblocksx = util_format_get_nblocksx(res->format, box->width);
399 tx->nblocksy = util_format_get_nblocksy(res->format, box->height);
401 tx->nlayers = box->depth;
403 tx->base.stride = tx->nblocksx * util_format_get_blocksize(res->format);
404 tx->base.layer_stride = tx->nblocksy * tx->base.stride;
406 if (usage & PIPE_TRANSFER_MAP_DIRECTLY) {
407 tx->base.stride = align(tx->base.stride, 128);
408 *ptransfer = &tx->base;
409 return mt->base.bo->map + mt->base.offset;
412 nv50_m2mf_rect_setup(&tx->rect[0], res, level, box->x, box->y, box->z);
414 size = tx->base.layer_stride;
416 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0,
417 size * tx->nlayers, NULL, &tx->rect[1].bo);
419 pipe_resource_reference(&tx->base.resource, NULL);
424 tx->rect[1].cpp = tx->rect[0].cpp;
425 tx->rect[1].width = tx->nblocksx;
426 tx->rect[1].height = tx->nblocksy;
427 tx->rect[1].depth = 1;
428 tx->rect[1].pitch = tx->base.stride;
429 tx->rect[1].domain = NOUVEAU_BO_GART;
431 if (usage & PIPE_TRANSFER_READ) {
432 unsigned base = tx->rect[0].base;
433 unsigned z = tx->rect[0].z;
435 for (i = 0; i < tx->nlayers; ++i) {
436 nvc0->m2mf_copy_rect(nvc0, &tx->rect[1], &tx->rect[0],
437 tx->nblocksx, tx->nblocksy);
441 tx->rect[0].base += mt->layer_stride;
442 tx->rect[1].base += size;
445 tx->rect[0].base = base;
446 tx->rect[1].base = 0;
449 if (tx->rect[1].bo->map) {
450 *ptransfer = &tx->base;
451 return tx->rect[1].bo->map;
454 if (usage & PIPE_TRANSFER_READ)
455 flags = NOUVEAU_BO_RD;
456 if (usage & PIPE_TRANSFER_WRITE)
457 flags |= NOUVEAU_BO_WR;
459 ret = nouveau_bo_map(tx->rect[1].bo, flags, nvc0->screen->base.client);
461 pipe_resource_reference(&tx->base.resource, NULL);
462 nouveau_bo_ref(NULL, &tx->rect[1].bo);
467 *ptransfer = &tx->base;
468 return tx->rect[1].bo->map;
472 nvc0_miptree_transfer_unmap(struct pipe_context *pctx,
473 struct pipe_transfer *transfer)
475 struct nvc0_context *nvc0 = nvc0_context(pctx);
476 struct nvc0_transfer *tx = (struct nvc0_transfer *)transfer;
477 struct nv50_miptree *mt = nv50_miptree(tx->base.resource);
480 if (tx->base.usage & PIPE_TRANSFER_MAP_DIRECTLY) {
481 pipe_resource_reference(&transfer->resource, NULL);
487 if (tx->base.usage & PIPE_TRANSFER_WRITE) {
488 for (i = 0; i < tx->nlayers; ++i) {
489 nvc0->m2mf_copy_rect(nvc0, &tx->rect[0], &tx->rect[1],
490 tx->nblocksx, tx->nblocksy);
494 tx->rect[0].base += mt->layer_stride;
495 tx->rect[1].base += tx->nblocksy * tx->base.stride;
497 NOUVEAU_DRV_STAT(&nvc0->screen->base, tex_transfers_wr, 1);
499 if (tx->base.usage & PIPE_TRANSFER_READ)
500 NOUVEAU_DRV_STAT(&nvc0->screen->base, tex_transfers_rd, 1);
502 nouveau_bo_ref(NULL, &tx->rect[1].bo);
503 pipe_resource_reference(&transfer->resource, NULL);
508 /* This happens rather often with DTD9/st. */
510 nvc0_cb_push(struct nouveau_context *nv,
511 struct nv04_resource *res,
512 unsigned offset, unsigned words, const uint32_t *data)
514 struct nvc0_context *nvc0 = nvc0_context(&nv->pipe);
515 struct nvc0_constbuf *cb = NULL;
518 /* Go through all the constbuf binding points of this buffer and try to
519 * find one which contains the region to be updated.
521 for (s = 0; s < 6 && !cb; s++) {
522 uint16_t bindings = res->cb_bindings[s];
524 int i = ffs(bindings) - 1;
525 uint32_t cb_offset = nvc0->constbuf[s][i].offset;
527 bindings &= ~(1 << i);
528 if (cb_offset <= offset &&
529 cb_offset + nvc0->constbuf[s][i].size >= offset + words * 4) {
530 cb = &nvc0->constbuf[s][i];
537 nvc0_cb_bo_push(nv, res->bo, res->domain,
538 res->offset + cb->offset, cb->size,
539 offset - cb->offset, words, data);
541 nv->push_data(nv, res->bo, res->offset + offset, res->domain,
547 nvc0_cb_bo_push(struct nouveau_context *nv,
548 struct nouveau_bo *bo, unsigned domain,
549 unsigned base, unsigned size,
550 unsigned offset, unsigned words, const uint32_t *data)
552 struct nouveau_pushbuf *push = nv->pushbuf;
554 NOUVEAU_DRV_STAT(nv->screen, constbuf_upload_count, 1);
555 NOUVEAU_DRV_STAT(nv->screen, constbuf_upload_bytes, words * 4);
557 assert(!(offset & 3));
558 size = align(size, 0x100);
560 assert(offset < size);
561 assert(offset + words * 4 <= size);
563 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
564 PUSH_DATA (push, size);
565 PUSH_DATAh(push, bo->offset + base);
566 PUSH_DATA (push, bo->offset + base);
569 unsigned nr = PUSH_AVAIL(push);
570 nr = MIN2(nr, words);
571 nr = MIN2(nr, NV04_PFIFO_MAX_PACKET_LEN - 1);
573 PUSH_SPACE(push, nr + 2);
574 PUSH_REFN (push, bo, NOUVEAU_BO_WR | domain);
575 BEGIN_1IC0(push, NVC0_3D(CB_POS), nr + 1);
576 PUSH_DATA (push, offset);
577 PUSH_DATAp(push, data, nr);
586 nvc0_init_transfer_functions(struct nvc0_context *nvc0)
588 if (nvc0->screen->base.class_3d >= NVE4_3D_CLASS) {
589 nvc0->m2mf_copy_rect = nve4_m2mf_transfer_rect;
590 nvc0->base.copy_data = nve4_m2mf_copy_linear;
591 nvc0->base.push_data = nve4_p2mf_push_linear;
593 nvc0->m2mf_copy_rect = nvc0_m2mf_transfer_rect;
594 nvc0->base.copy_data = nvc0_m2mf_copy_linear;
595 nvc0->base.push_data = nvc0_m2mf_push_linear;
597 nvc0->base.push_cb = nvc0_cb_push;