2db538c70af935ead1c483a28d1390594f9f44bd
[mesa.git] / src / gallium / drivers / nouveau / nouveau_buffer.c
1
2 #include "util/u_inlines.h"
3 #include "util/u_memory.h"
4 #include "util/u_math.h"
5 #include "util/u_surface.h"
6
7 #include "nouveau_screen.h"
8 #include "nouveau_context.h"
9 #include "nouveau_winsys.h"
10 #include "nouveau_fence.h"
11 #include "nouveau_buffer.h"
12 #include "nouveau_mm.h"
13
14 #define NOUVEAU_TRANSFER_PUSHBUF_THRESHOLD 192
15
16 struct nouveau_transfer {
17 struct pipe_transfer base;
18
19 uint8_t *map;
20 struct nouveau_bo *bo;
21 struct nouveau_mm_allocation *mm;
22 uint32_t offset;
23 };
24
25 static inline struct nouveau_transfer *
26 nouveau_transfer(struct pipe_transfer *transfer)
27 {
28 return (struct nouveau_transfer *)transfer;
29 }
30
31 static inline bool
32 nouveau_buffer_malloc(struct nv04_resource *buf)
33 {
34 if (!buf->data)
35 buf->data = align_malloc(buf->base.width0, NOUVEAU_MIN_BUFFER_MAP_ALIGN);
36 return !!buf->data;
37 }
38
39 static inline bool
40 nouveau_buffer_allocate(struct nouveau_screen *screen,
41 struct nv04_resource *buf, unsigned domain)
42 {
43 uint32_t size = align(buf->base.width0, 0x100);
44
45 if (domain == NOUVEAU_BO_VRAM) {
46 buf->mm = nouveau_mm_allocate(screen->mm_VRAM, size,
47 &buf->bo, &buf->offset);
48 if (!buf->bo)
49 return nouveau_buffer_allocate(screen, buf, NOUVEAU_BO_GART);
50 NOUVEAU_DRV_STAT(screen, buf_obj_current_bytes_vid, buf->base.width0);
51 } else
52 if (domain == NOUVEAU_BO_GART) {
53 buf->mm = nouveau_mm_allocate(screen->mm_GART, size,
54 &buf->bo, &buf->offset);
55 if (!buf->bo)
56 return false;
57 NOUVEAU_DRV_STAT(screen, buf_obj_current_bytes_sys, buf->base.width0);
58 } else {
59 assert(domain == 0);
60 if (!nouveau_buffer_malloc(buf))
61 return false;
62 }
63 buf->domain = domain;
64 if (buf->bo)
65 buf->address = buf->bo->offset + buf->offset;
66
67 util_range_set_empty(&buf->valid_buffer_range);
68
69 return true;
70 }
71
72 static inline void
73 release_allocation(struct nouveau_mm_allocation **mm,
74 struct nouveau_fence *fence)
75 {
76 nouveau_fence_work(fence, nouveau_mm_free_work, *mm);
77 (*mm) = NULL;
78 }
79
80 inline void
81 nouveau_buffer_release_gpu_storage(struct nv04_resource *buf)
82 {
83 if (buf->fence && buf->fence->state < NOUVEAU_FENCE_STATE_FLUSHED) {
84 nouveau_fence_work(buf->fence, nouveau_fence_unref_bo, buf->bo);
85 buf->bo = NULL;
86 } else {
87 nouveau_bo_ref(NULL, &buf->bo);
88 }
89
90 if (buf->mm)
91 release_allocation(&buf->mm, buf->fence);
92
93 if (buf->domain == NOUVEAU_BO_VRAM)
94 NOUVEAU_DRV_STAT_RES(buf, buf_obj_current_bytes_vid, -(uint64_t)buf->base.width0);
95 if (buf->domain == NOUVEAU_BO_GART)
96 NOUVEAU_DRV_STAT_RES(buf, buf_obj_current_bytes_sys, -(uint64_t)buf->base.width0);
97
98 buf->domain = 0;
99 }
100
101 static inline bool
102 nouveau_buffer_reallocate(struct nouveau_screen *screen,
103 struct nv04_resource *buf, unsigned domain)
104 {
105 nouveau_buffer_release_gpu_storage(buf);
106
107 nouveau_fence_ref(NULL, &buf->fence);
108 nouveau_fence_ref(NULL, &buf->fence_wr);
109
110 buf->status &= NOUVEAU_BUFFER_STATUS_REALLOC_MASK;
111
112 return nouveau_buffer_allocate(screen, buf, domain);
113 }
114
115 static void
116 nouveau_buffer_destroy(struct pipe_screen *pscreen,
117 struct pipe_resource *presource)
118 {
119 struct nv04_resource *res = nv04_resource(presource);
120
121 nouveau_buffer_release_gpu_storage(res);
122
123 if (res->data && !(res->status & NOUVEAU_BUFFER_STATUS_USER_MEMORY))
124 align_free(res->data);
125
126 nouveau_fence_ref(NULL, &res->fence);
127 nouveau_fence_ref(NULL, &res->fence_wr);
128
129 util_range_destroy(&res->valid_buffer_range);
130
131 FREE(res);
132
133 NOUVEAU_DRV_STAT(nouveau_screen(pscreen), buf_obj_current_count, -1);
134 }
135
136 /* Set up a staging area for the transfer. This is either done in "regular"
137 * system memory if the driver supports push_data (nv50+) and the data is
138 * small enough (and permit_pb == true), or in GART memory.
139 */
140 static uint8_t *
141 nouveau_transfer_staging(struct nouveau_context *nv,
142 struct nouveau_transfer *tx, bool permit_pb)
143 {
144 const unsigned adj = tx->base.box.x & NOUVEAU_MIN_BUFFER_MAP_ALIGN_MASK;
145 const unsigned size = align(tx->base.box.width, 4) + adj;
146
147 if (!nv->push_data)
148 permit_pb = false;
149
150 if ((size <= NOUVEAU_TRANSFER_PUSHBUF_THRESHOLD) && permit_pb) {
151 tx->map = align_malloc(size, NOUVEAU_MIN_BUFFER_MAP_ALIGN);
152 if (tx->map)
153 tx->map += adj;
154 } else {
155 tx->mm =
156 nouveau_mm_allocate(nv->screen->mm_GART, size, &tx->bo, &tx->offset);
157 if (tx->bo) {
158 tx->offset += adj;
159 if (!nouveau_bo_map(tx->bo, 0, NULL))
160 tx->map = (uint8_t *)tx->bo->map + tx->offset;
161 }
162 }
163 return tx->map;
164 }
165
166 /* Copies data from the resource into the the transfer's temporary GART
167 * buffer. Also updates buf->data if present.
168 *
169 * Maybe just migrate to GART right away if we actually need to do this. */
170 static bool
171 nouveau_transfer_read(struct nouveau_context *nv, struct nouveau_transfer *tx)
172 {
173 struct nv04_resource *buf = nv04_resource(tx->base.resource);
174 const unsigned base = tx->base.box.x;
175 const unsigned size = tx->base.box.width;
176
177 NOUVEAU_DRV_STAT(nv->screen, buf_read_bytes_staging_vid, size);
178
179 nv->copy_data(nv, tx->bo, tx->offset, NOUVEAU_BO_GART,
180 buf->bo, buf->offset + base, buf->domain, size);
181
182 if (nouveau_bo_wait(tx->bo, NOUVEAU_BO_RD, nv->client))
183 return false;
184
185 if (buf->data)
186 memcpy(buf->data + base, tx->map, size);
187
188 return true;
189 }
190
191 static void
192 nouveau_transfer_write(struct nouveau_context *nv, struct nouveau_transfer *tx,
193 unsigned offset, unsigned size)
194 {
195 struct nv04_resource *buf = nv04_resource(tx->base.resource);
196 uint8_t *data = tx->map + offset;
197 const unsigned base = tx->base.box.x + offset;
198 const bool can_cb = !((base | size) & 3);
199
200 if (buf->data)
201 memcpy(data, buf->data + base, size);
202 else
203 buf->status |= NOUVEAU_BUFFER_STATUS_DIRTY;
204
205 if (buf->domain == NOUVEAU_BO_VRAM)
206 NOUVEAU_DRV_STAT(nv->screen, buf_write_bytes_staging_vid, size);
207 if (buf->domain == NOUVEAU_BO_GART)
208 NOUVEAU_DRV_STAT(nv->screen, buf_write_bytes_staging_sys, size);
209
210 if (tx->bo)
211 nv->copy_data(nv, buf->bo, buf->offset + base, buf->domain,
212 tx->bo, tx->offset + offset, NOUVEAU_BO_GART, size);
213 else
214 if (nv->push_cb && can_cb)
215 nv->push_cb(nv, buf,
216 base, size / 4, (const uint32_t *)data);
217 else
218 nv->push_data(nv, buf->bo, buf->offset + base, buf->domain, size, data);
219
220 nouveau_fence_ref(nv->screen->fence.current, &buf->fence);
221 nouveau_fence_ref(nv->screen->fence.current, &buf->fence_wr);
222 }
223
224 /* Does a CPU wait for the buffer's backing data to become reliably accessible
225 * for write/read by waiting on the buffer's relevant fences.
226 */
227 static inline bool
228 nouveau_buffer_sync(struct nouveau_context *nv,
229 struct nv04_resource *buf, unsigned rw)
230 {
231 if (rw == PIPE_TRANSFER_READ) {
232 if (!buf->fence_wr)
233 return true;
234 NOUVEAU_DRV_STAT_RES(buf, buf_non_kernel_fence_sync_count,
235 !nouveau_fence_signalled(buf->fence_wr));
236 if (!nouveau_fence_wait(buf->fence_wr, &nv->debug))
237 return false;
238 } else {
239 if (!buf->fence)
240 return true;
241 NOUVEAU_DRV_STAT_RES(buf, buf_non_kernel_fence_sync_count,
242 !nouveau_fence_signalled(buf->fence));
243 if (!nouveau_fence_wait(buf->fence, &nv->debug))
244 return false;
245
246 nouveau_fence_ref(NULL, &buf->fence);
247 }
248 nouveau_fence_ref(NULL, &buf->fence_wr);
249
250 return true;
251 }
252
253 static inline bool
254 nouveau_buffer_busy(struct nv04_resource *buf, unsigned rw)
255 {
256 if (rw == PIPE_TRANSFER_READ)
257 return (buf->fence_wr && !nouveau_fence_signalled(buf->fence_wr));
258 else
259 return (buf->fence && !nouveau_fence_signalled(buf->fence));
260 }
261
262 static inline void
263 nouveau_buffer_transfer_init(struct nouveau_transfer *tx,
264 struct pipe_resource *resource,
265 const struct pipe_box *box,
266 unsigned usage)
267 {
268 tx->base.resource = resource;
269 tx->base.level = 0;
270 tx->base.usage = usage;
271 tx->base.box.x = box->x;
272 tx->base.box.y = 0;
273 tx->base.box.z = 0;
274 tx->base.box.width = box->width;
275 tx->base.box.height = 1;
276 tx->base.box.depth = 1;
277 tx->base.stride = 0;
278 tx->base.layer_stride = 0;
279
280 tx->bo = NULL;
281 tx->map = NULL;
282 }
283
284 static inline void
285 nouveau_buffer_transfer_del(struct nouveau_context *nv,
286 struct nouveau_transfer *tx)
287 {
288 if (tx->map) {
289 if (likely(tx->bo)) {
290 nouveau_fence_work(nv->screen->fence.current,
291 nouveau_fence_unref_bo, tx->bo);
292 if (tx->mm)
293 release_allocation(&tx->mm, nv->screen->fence.current);
294 } else {
295 align_free(tx->map -
296 (tx->base.box.x & NOUVEAU_MIN_BUFFER_MAP_ALIGN_MASK));
297 }
298 }
299 }
300
301 /* Creates a cache in system memory of the buffer data. */
302 static bool
303 nouveau_buffer_cache(struct nouveau_context *nv, struct nv04_resource *buf)
304 {
305 struct nouveau_transfer tx;
306 bool ret;
307 tx.base.resource = &buf->base;
308 tx.base.box.x = 0;
309 tx.base.box.width = buf->base.width0;
310 tx.bo = NULL;
311 tx.map = NULL;
312
313 if (!buf->data)
314 if (!nouveau_buffer_malloc(buf))
315 return false;
316 if (!(buf->status & NOUVEAU_BUFFER_STATUS_DIRTY))
317 return true;
318 nv->stats.buf_cache_count++;
319
320 if (!nouveau_transfer_staging(nv, &tx, false))
321 return false;
322
323 ret = nouveau_transfer_read(nv, &tx);
324 if (ret) {
325 buf->status &= ~NOUVEAU_BUFFER_STATUS_DIRTY;
326 memcpy(buf->data, tx.map, buf->base.width0);
327 }
328 nouveau_buffer_transfer_del(nv, &tx);
329 return ret;
330 }
331
332
333 #define NOUVEAU_TRANSFER_DISCARD \
334 (PIPE_TRANSFER_DISCARD_RANGE | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)
335
336 /* Checks whether it is possible to completely discard the memory backing this
337 * resource. This can be useful if we would otherwise have to wait for a read
338 * operation to complete on this data.
339 */
340 static inline bool
341 nouveau_buffer_should_discard(struct nv04_resource *buf, unsigned usage)
342 {
343 if (!(usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE))
344 return false;
345 if (unlikely(buf->base.bind & PIPE_BIND_SHARED))
346 return false;
347 if (unlikely(usage & PIPE_TRANSFER_PERSISTENT))
348 return false;
349 return buf->mm && nouveau_buffer_busy(buf, PIPE_TRANSFER_WRITE);
350 }
351
352 /* Returns a pointer to a memory area representing a window into the
353 * resource's data.
354 *
355 * This may or may not be the _actual_ memory area of the resource. However
356 * when calling nouveau_buffer_transfer_unmap, if it wasn't the actual memory
357 * area, the contents of the returned map are copied over to the resource.
358 *
359 * The usage indicates what the caller plans to do with the map:
360 *
361 * WRITE means that the user plans to write to it
362 *
363 * READ means that the user plans on reading from it
364 *
365 * DISCARD_WHOLE_RESOURCE means that the whole resource is going to be
366 * potentially overwritten, and even if it isn't, the bits that aren't don't
367 * need to be maintained.
368 *
369 * DISCARD_RANGE means that all the data in the specified range is going to
370 * be overwritten.
371 *
372 * The strategy for determining what kind of memory area to return is complex,
373 * see comments inside of the function.
374 */
375 static void *
376 nouveau_buffer_transfer_map(struct pipe_context *pipe,
377 struct pipe_resource *resource,
378 unsigned level, unsigned usage,
379 const struct pipe_box *box,
380 struct pipe_transfer **ptransfer)
381 {
382 struct nouveau_context *nv = nouveau_context(pipe);
383 struct nv04_resource *buf = nv04_resource(resource);
384 struct nouveau_transfer *tx = MALLOC_STRUCT(nouveau_transfer);
385 uint8_t *map;
386 int ret;
387
388 if (!tx)
389 return NULL;
390 nouveau_buffer_transfer_init(tx, resource, box, usage);
391 *ptransfer = &tx->base;
392
393 if (usage & PIPE_TRANSFER_READ)
394 NOUVEAU_DRV_STAT(nv->screen, buf_transfers_rd, 1);
395 if (usage & PIPE_TRANSFER_WRITE)
396 NOUVEAU_DRV_STAT(nv->screen, buf_transfers_wr, 1);
397
398 /* If we are trying to write to an uninitialized range, the user shouldn't
399 * care what was there before. So we can treat the write as if the target
400 * range were being discarded. Furthermore, since we know that even if this
401 * buffer is busy due to GPU activity, because the contents were
402 * uninitialized, the GPU can't care what was there, and so we can treat
403 * the write as being unsynchronized.
404 */
405 if ((usage & PIPE_TRANSFER_WRITE) &&
406 !util_ranges_intersect(&buf->valid_buffer_range, box->x, box->x + box->width))
407 usage |= PIPE_TRANSFER_DISCARD_RANGE | PIPE_TRANSFER_UNSYNCHRONIZED;
408
409 if (usage & PIPE_TRANSFER_PERSISTENT)
410 usage |= PIPE_TRANSFER_UNSYNCHRONIZED;
411
412 if (buf->domain == NOUVEAU_BO_VRAM) {
413 if (usage & NOUVEAU_TRANSFER_DISCARD) {
414 /* Set up a staging area for the user to write to. It will be copied
415 * back into VRAM on unmap. */
416 if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)
417 buf->status &= NOUVEAU_BUFFER_STATUS_REALLOC_MASK;
418 nouveau_transfer_staging(nv, tx, true);
419 } else {
420 if (buf->status & NOUVEAU_BUFFER_STATUS_GPU_WRITING) {
421 /* The GPU is currently writing to this buffer. Copy its current
422 * contents to a staging area in the GART. This is necessary since
423 * not the whole area being mapped is being discarded.
424 */
425 if (buf->data) {
426 align_free(buf->data);
427 buf->data = NULL;
428 }
429 nouveau_transfer_staging(nv, tx, false);
430 nouveau_transfer_read(nv, tx);
431 } else {
432 /* The buffer is currently idle. Create a staging area for writes,
433 * and make sure that the cached data is up-to-date. */
434 if (usage & PIPE_TRANSFER_WRITE)
435 nouveau_transfer_staging(nv, tx, true);
436 if (!buf->data)
437 nouveau_buffer_cache(nv, buf);
438 }
439 }
440 return buf->data ? (buf->data + box->x) : tx->map;
441 } else
442 if (unlikely(buf->domain == 0)) {
443 return buf->data + box->x;
444 }
445
446 /* At this point, buf->domain == GART */
447
448 if (nouveau_buffer_should_discard(buf, usage)) {
449 int ref = buf->base.reference.count - 1;
450 nouveau_buffer_reallocate(nv->screen, buf, buf->domain);
451 if (ref > 0) /* any references inside context possible ? */
452 nv->invalidate_resource_storage(nv, &buf->base, ref);
453 }
454
455 /* Note that nouveau_bo_map ends up doing a nouveau_bo_wait with the
456 * relevant flags. If buf->mm is set, that means this resource is part of a
457 * larger slab bo that holds multiple resources. So in that case, don't
458 * wait on the whole slab and instead use the logic below to return a
459 * reasonable buffer for that case.
460 */
461 ret = nouveau_bo_map(buf->bo,
462 buf->mm ? 0 : nouveau_screen_transfer_flags(usage),
463 nv->client);
464 if (ret) {
465 FREE(tx);
466 return NULL;
467 }
468 map = (uint8_t *)buf->bo->map + buf->offset + box->x;
469
470 /* using kernel fences only if !buf->mm */
471 if ((usage & PIPE_TRANSFER_UNSYNCHRONIZED) || !buf->mm)
472 return map;
473
474 /* If the GPU is currently reading/writing this buffer, we shouldn't
475 * interfere with its progress. So instead we either wait for the GPU to
476 * complete its operation, or set up a staging area to perform our work in.
477 */
478 if (nouveau_buffer_busy(buf, usage & PIPE_TRANSFER_READ_WRITE)) {
479 if (unlikely(usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)) {
480 /* Discarding was not possible, must sync because
481 * subsequent transfers might use UNSYNCHRONIZED. */
482 nouveau_buffer_sync(nv, buf, usage & PIPE_TRANSFER_READ_WRITE);
483 } else
484 if (usage & PIPE_TRANSFER_DISCARD_RANGE) {
485 /* The whole range is being discarded, so it doesn't matter what was
486 * there before. No need to copy anything over. */
487 nouveau_transfer_staging(nv, tx, true);
488 map = tx->map;
489 } else
490 if (nouveau_buffer_busy(buf, PIPE_TRANSFER_READ)) {
491 if (usage & PIPE_TRANSFER_DONTBLOCK)
492 map = NULL;
493 else
494 nouveau_buffer_sync(nv, buf, usage & PIPE_TRANSFER_READ_WRITE);
495 } else {
496 /* It is expected that the returned buffer be a representation of the
497 * data in question, so we must copy it over from the buffer. */
498 nouveau_transfer_staging(nv, tx, true);
499 if (tx->map)
500 memcpy(tx->map, map, box->width);
501 map = tx->map;
502 }
503 }
504 if (!map)
505 FREE(tx);
506 return map;
507 }
508
509
510
511 static void
512 nouveau_buffer_transfer_flush_region(struct pipe_context *pipe,
513 struct pipe_transfer *transfer,
514 const struct pipe_box *box)
515 {
516 struct nouveau_transfer *tx = nouveau_transfer(transfer);
517 struct nv04_resource *buf = nv04_resource(transfer->resource);
518
519 if (tx->map)
520 nouveau_transfer_write(nouveau_context(pipe), tx, box->x, box->width);
521
522 util_range_add(&buf->valid_buffer_range,
523 tx->base.box.x + box->x,
524 tx->base.box.x + box->x + box->width);
525 }
526
527 /* Unmap stage of the transfer. If it was a WRITE transfer and the map that
528 * was returned was not the real resource's data, this needs to transfer the
529 * data back to the resource.
530 *
531 * Also marks vbo dirty based on the buffer's binding
532 */
533 static void
534 nouveau_buffer_transfer_unmap(struct pipe_context *pipe,
535 struct pipe_transfer *transfer)
536 {
537 struct nouveau_context *nv = nouveau_context(pipe);
538 struct nouveau_transfer *tx = nouveau_transfer(transfer);
539 struct nv04_resource *buf = nv04_resource(transfer->resource);
540
541 if (tx->base.usage & PIPE_TRANSFER_WRITE) {
542 if (!(tx->base.usage & PIPE_TRANSFER_FLUSH_EXPLICIT)) {
543 if (tx->map)
544 nouveau_transfer_write(nv, tx, 0, tx->base.box.width);
545
546 util_range_add(&buf->valid_buffer_range,
547 tx->base.box.x, tx->base.box.x + tx->base.box.width);
548 }
549
550 if (likely(buf->domain)) {
551 const uint8_t bind = buf->base.bind;
552 /* make sure we invalidate dedicated caches */
553 if (bind & (PIPE_BIND_VERTEX_BUFFER | PIPE_BIND_INDEX_BUFFER))
554 nv->vbo_dirty = true;
555 }
556 }
557
558 if (!tx->bo && (tx->base.usage & PIPE_TRANSFER_WRITE))
559 NOUVEAU_DRV_STAT(nv->screen, buf_write_bytes_direct, tx->base.box.width);
560
561 nouveau_buffer_transfer_del(nv, tx);
562 FREE(tx);
563 }
564
565
566 void
567 nouveau_copy_buffer(struct nouveau_context *nv,
568 struct nv04_resource *dst, unsigned dstx,
569 struct nv04_resource *src, unsigned srcx, unsigned size)
570 {
571 assert(dst->base.target == PIPE_BUFFER && src->base.target == PIPE_BUFFER);
572
573 if (likely(dst->domain) && likely(src->domain)) {
574 nv->copy_data(nv,
575 dst->bo, dst->offset + dstx, dst->domain,
576 src->bo, src->offset + srcx, src->domain, size);
577
578 dst->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
579 nouveau_fence_ref(nv->screen->fence.current, &dst->fence);
580 nouveau_fence_ref(nv->screen->fence.current, &dst->fence_wr);
581
582 src->status |= NOUVEAU_BUFFER_STATUS_GPU_READING;
583 nouveau_fence_ref(nv->screen->fence.current, &src->fence);
584 } else {
585 struct pipe_box src_box;
586 src_box.x = srcx;
587 src_box.y = 0;
588 src_box.z = 0;
589 src_box.width = size;
590 src_box.height = 1;
591 src_box.depth = 1;
592 util_resource_copy_region(&nv->pipe,
593 &dst->base, 0, dstx, 0, 0,
594 &src->base, 0, &src_box);
595 }
596
597 util_range_add(&dst->valid_buffer_range, dstx, dstx + size);
598 }
599
600
601 void *
602 nouveau_resource_map_offset(struct nouveau_context *nv,
603 struct nv04_resource *res, uint32_t offset,
604 uint32_t flags)
605 {
606 if (unlikely(res->status & NOUVEAU_BUFFER_STATUS_USER_MEMORY))
607 return res->data + offset;
608
609 if (res->domain == NOUVEAU_BO_VRAM) {
610 if (!res->data || (res->status & NOUVEAU_BUFFER_STATUS_GPU_WRITING))
611 nouveau_buffer_cache(nv, res);
612 }
613 if (res->domain != NOUVEAU_BO_GART)
614 return res->data + offset;
615
616 if (res->mm) {
617 unsigned rw;
618 rw = (flags & NOUVEAU_BO_WR) ? PIPE_TRANSFER_WRITE : PIPE_TRANSFER_READ;
619 nouveau_buffer_sync(nv, res, rw);
620 if (nouveau_bo_map(res->bo, 0, NULL))
621 return NULL;
622 } else {
623 if (nouveau_bo_map(res->bo, flags, nv->client))
624 return NULL;
625 }
626 return (uint8_t *)res->bo->map + res->offset + offset;
627 }
628
629
630 const struct u_resource_vtbl nouveau_buffer_vtbl =
631 {
632 u_default_resource_get_handle, /* get_handle */
633 nouveau_buffer_destroy, /* resource_destroy */
634 nouveau_buffer_transfer_map, /* transfer_map */
635 nouveau_buffer_transfer_flush_region, /* transfer_flush_region */
636 nouveau_buffer_transfer_unmap, /* transfer_unmap */
637 u_default_transfer_inline_write /* transfer_inline_write */
638 };
639
640 struct pipe_resource *
641 nouveau_buffer_create(struct pipe_screen *pscreen,
642 const struct pipe_resource *templ)
643 {
644 struct nouveau_screen *screen = nouveau_screen(pscreen);
645 struct nv04_resource *buffer;
646 bool ret;
647
648 buffer = CALLOC_STRUCT(nv04_resource);
649 if (!buffer)
650 return NULL;
651
652 buffer->base = *templ;
653 buffer->vtbl = &nouveau_buffer_vtbl;
654 pipe_reference_init(&buffer->base.reference, 1);
655 buffer->base.screen = pscreen;
656
657 if (buffer->base.flags & (PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
658 PIPE_RESOURCE_FLAG_MAP_COHERENT)) {
659 buffer->domain = NOUVEAU_BO_GART;
660 } else if (buffer->base.bind == 0 || (buffer->base.bind &
661 (screen->vidmem_bindings & screen->sysmem_bindings))) {
662 switch (buffer->base.usage) {
663 case PIPE_USAGE_DEFAULT:
664 case PIPE_USAGE_IMMUTABLE:
665 buffer->domain = NV_VRAM_DOMAIN(screen);
666 break;
667 case PIPE_USAGE_DYNAMIC:
668 /* For most apps, we'd have to do staging transfers to avoid sync
669 * with this usage, and GART -> GART copies would be suboptimal.
670 */
671 buffer->domain = NV_VRAM_DOMAIN(screen);
672 break;
673 case PIPE_USAGE_STAGING:
674 case PIPE_USAGE_STREAM:
675 buffer->domain = NOUVEAU_BO_GART;
676 break;
677 default:
678 assert(0);
679 break;
680 }
681 } else {
682 if (buffer->base.bind & screen->vidmem_bindings)
683 buffer->domain = NV_VRAM_DOMAIN(screen);
684 else
685 if (buffer->base.bind & screen->sysmem_bindings)
686 buffer->domain = NOUVEAU_BO_GART;
687 }
688
689 ret = nouveau_buffer_allocate(screen, buffer, buffer->domain);
690
691 if (ret == false)
692 goto fail;
693
694 if (buffer->domain == NOUVEAU_BO_VRAM && screen->hint_buf_keep_sysmem_copy)
695 nouveau_buffer_cache(NULL, buffer);
696
697 NOUVEAU_DRV_STAT(screen, buf_obj_current_count, 1);
698
699 util_range_init(&buffer->valid_buffer_range);
700
701 return &buffer->base;
702
703 fail:
704 FREE(buffer);
705 return NULL;
706 }
707
708
709 struct pipe_resource *
710 nouveau_user_buffer_create(struct pipe_screen *pscreen, void *ptr,
711 unsigned bytes, unsigned bind)
712 {
713 struct nv04_resource *buffer;
714
715 buffer = CALLOC_STRUCT(nv04_resource);
716 if (!buffer)
717 return NULL;
718
719 pipe_reference_init(&buffer->base.reference, 1);
720 buffer->vtbl = &nouveau_buffer_vtbl;
721 buffer->base.screen = pscreen;
722 buffer->base.format = PIPE_FORMAT_R8_UNORM;
723 buffer->base.usage = PIPE_USAGE_IMMUTABLE;
724 buffer->base.bind = bind;
725 buffer->base.width0 = bytes;
726 buffer->base.height0 = 1;
727 buffer->base.depth0 = 1;
728
729 buffer->data = ptr;
730 buffer->status = NOUVEAU_BUFFER_STATUS_USER_MEMORY;
731
732 util_range_init(&buffer->valid_buffer_range);
733 util_range_add(&buffer->valid_buffer_range, 0, bytes);
734
735 return &buffer->base;
736 }
737
738 static inline bool
739 nouveau_buffer_data_fetch(struct nouveau_context *nv, struct nv04_resource *buf,
740 struct nouveau_bo *bo, unsigned offset, unsigned size)
741 {
742 if (!nouveau_buffer_malloc(buf))
743 return false;
744 if (nouveau_bo_map(bo, NOUVEAU_BO_RD, nv->client))
745 return false;
746 memcpy(buf->data, (uint8_t *)bo->map + offset, size);
747 return true;
748 }
749
750 /* Migrate a linear buffer (vertex, index, constants) USER -> GART -> VRAM. */
751 bool
752 nouveau_buffer_migrate(struct nouveau_context *nv,
753 struct nv04_resource *buf, const unsigned new_domain)
754 {
755 struct nouveau_screen *screen = nv->screen;
756 struct nouveau_bo *bo;
757 const unsigned old_domain = buf->domain;
758 unsigned size = buf->base.width0;
759 unsigned offset;
760 int ret;
761
762 assert(new_domain != old_domain);
763
764 if (new_domain == NOUVEAU_BO_GART && old_domain == 0) {
765 if (!nouveau_buffer_allocate(screen, buf, new_domain))
766 return false;
767 ret = nouveau_bo_map(buf->bo, 0, nv->client);
768 if (ret)
769 return ret;
770 memcpy((uint8_t *)buf->bo->map + buf->offset, buf->data, size);
771 align_free(buf->data);
772 } else
773 if (old_domain != 0 && new_domain != 0) {
774 struct nouveau_mm_allocation *mm = buf->mm;
775
776 if (new_domain == NOUVEAU_BO_VRAM) {
777 /* keep a system memory copy of our data in case we hit a fallback */
778 if (!nouveau_buffer_data_fetch(nv, buf, buf->bo, buf->offset, size))
779 return false;
780 if (nouveau_mesa_debug)
781 debug_printf("migrating %u KiB to VRAM\n", size / 1024);
782 }
783
784 offset = buf->offset;
785 bo = buf->bo;
786 buf->bo = NULL;
787 buf->mm = NULL;
788 nouveau_buffer_allocate(screen, buf, new_domain);
789
790 nv->copy_data(nv, buf->bo, buf->offset, new_domain,
791 bo, offset, old_domain, buf->base.width0);
792
793 nouveau_fence_work(screen->fence.current, nouveau_fence_unref_bo, bo);
794 if (mm)
795 release_allocation(&mm, screen->fence.current);
796 } else
797 if (new_domain == NOUVEAU_BO_VRAM && old_domain == 0) {
798 struct nouveau_transfer tx;
799 if (!nouveau_buffer_allocate(screen, buf, NOUVEAU_BO_VRAM))
800 return false;
801 tx.base.resource = &buf->base;
802 tx.base.box.x = 0;
803 tx.base.box.width = buf->base.width0;
804 tx.bo = NULL;
805 tx.map = NULL;
806 if (!nouveau_transfer_staging(nv, &tx, false))
807 return false;
808 nouveau_transfer_write(nv, &tx, 0, tx.base.box.width);
809 nouveau_buffer_transfer_del(nv, &tx);
810 } else
811 return false;
812
813 assert(buf->domain == new_domain);
814 return true;
815 }
816
817 /* Migrate data from glVertexAttribPointer(non-VBO) user buffers to GART.
818 * We'd like to only allocate @size bytes here, but then we'd have to rebase
819 * the vertex indices ...
820 */
821 bool
822 nouveau_user_buffer_upload(struct nouveau_context *nv,
823 struct nv04_resource *buf,
824 unsigned base, unsigned size)
825 {
826 struct nouveau_screen *screen = nouveau_screen(buf->base.screen);
827 int ret;
828
829 assert(buf->status & NOUVEAU_BUFFER_STATUS_USER_MEMORY);
830
831 buf->base.width0 = base + size;
832 if (!nouveau_buffer_reallocate(screen, buf, NOUVEAU_BO_GART))
833 return false;
834
835 ret = nouveau_bo_map(buf->bo, 0, nv->client);
836 if (ret)
837 return false;
838 memcpy((uint8_t *)buf->bo->map + buf->offset + base, buf->data + base, size);
839
840 return true;
841 }
842
843 /* Invalidate underlying buffer storage, reset fences, reallocate to non-busy
844 * buffer.
845 */
846 void
847 nouveau_buffer_invalidate(struct pipe_context *pipe,
848 struct pipe_resource *resource)
849 {
850 struct nouveau_context *nv = nouveau_context(pipe);
851 struct nv04_resource *buf = nv04_resource(resource);
852 int ref = buf->base.reference.count - 1;
853
854 /* Shared buffers shouldn't get reallocated */
855 if (unlikely(buf->base.bind & PIPE_BIND_SHARED))
856 return;
857
858 /* We can't touch persistent/coherent buffers */
859 if (buf->base.flags & (PIPE_RESOURCE_FLAG_MAP_PERSISTENT |
860 PIPE_RESOURCE_FLAG_MAP_COHERENT))
861 return;
862
863 /* If the buffer is sub-allocated and not currently being written, just
864 * wipe the valid buffer range. Otherwise we have to create fresh
865 * storage. (We don't keep track of fences for non-sub-allocated BO's.)
866 */
867 if (buf->mm && !nouveau_buffer_busy(buf, PIPE_TRANSFER_WRITE)) {
868 util_range_set_empty(&buf->valid_buffer_range);
869 } else {
870 nouveau_buffer_reallocate(nv->screen, buf, buf->domain);
871 if (ref > 0) /* any references inside context possible ? */
872 nv->invalidate_resource_storage(nv, &buf->base, ref);
873 }
874 }
875
876
877 /* Scratch data allocation. */
878
879 static inline int
880 nouveau_scratch_bo_alloc(struct nouveau_context *nv, struct nouveau_bo **pbo,
881 unsigned size)
882 {
883 return nouveau_bo_new(nv->screen->device, NOUVEAU_BO_GART | NOUVEAU_BO_MAP,
884 4096, size, NULL, pbo);
885 }
886
887 static void
888 nouveau_scratch_unref_bos(void *d)
889 {
890 struct runout *b = d;
891 int i;
892
893 for (i = 0; i < b->nr; ++i)
894 nouveau_bo_ref(NULL, &b->bo[i]);
895
896 FREE(b);
897 }
898
899 void
900 nouveau_scratch_runout_release(struct nouveau_context *nv)
901 {
902 if (!nv->scratch.runout)
903 return;
904
905 if (!nouveau_fence_work(nv->screen->fence.current, nouveau_scratch_unref_bos,
906 nv->scratch.runout))
907 return;
908
909 nv->scratch.end = 0;
910 nv->scratch.runout = NULL;
911 }
912
913 /* Allocate an extra bo if we can't fit everything we need simultaneously.
914 * (Could happen for very large user arrays.)
915 */
916 static inline bool
917 nouveau_scratch_runout(struct nouveau_context *nv, unsigned size)
918 {
919 int ret;
920 unsigned n;
921
922 if (nv->scratch.runout)
923 n = nv->scratch.runout->nr;
924 else
925 n = 0;
926 nv->scratch.runout = REALLOC(nv->scratch.runout, n == 0 ? 0 :
927 (sizeof(*nv->scratch.runout) + (n + 0) * sizeof(void *)),
928 sizeof(*nv->scratch.runout) + (n + 1) * sizeof(void *));
929 nv->scratch.runout->nr = n + 1;
930 nv->scratch.runout->bo[n] = NULL;
931
932 ret = nouveau_scratch_bo_alloc(nv, &nv->scratch.runout->bo[n], size);
933 if (!ret) {
934 ret = nouveau_bo_map(nv->scratch.runout->bo[n], 0, NULL);
935 if (ret)
936 nouveau_bo_ref(NULL, &nv->scratch.runout->bo[--nv->scratch.runout->nr]);
937 }
938 if (!ret) {
939 nv->scratch.current = nv->scratch.runout->bo[n];
940 nv->scratch.offset = 0;
941 nv->scratch.end = size;
942 nv->scratch.map = nv->scratch.current->map;
943 }
944 return !ret;
945 }
946
947 /* Continue to next scratch buffer, if available (no wrapping, large enough).
948 * Allocate it if it has not yet been created.
949 */
950 static inline bool
951 nouveau_scratch_next(struct nouveau_context *nv, unsigned size)
952 {
953 struct nouveau_bo *bo;
954 int ret;
955 const unsigned i = (nv->scratch.id + 1) % NOUVEAU_MAX_SCRATCH_BUFS;
956
957 if ((size > nv->scratch.bo_size) || (i == nv->scratch.wrap))
958 return false;
959 nv->scratch.id = i;
960
961 bo = nv->scratch.bo[i];
962 if (!bo) {
963 ret = nouveau_scratch_bo_alloc(nv, &bo, nv->scratch.bo_size);
964 if (ret)
965 return false;
966 nv->scratch.bo[i] = bo;
967 }
968 nv->scratch.current = bo;
969 nv->scratch.offset = 0;
970 nv->scratch.end = nv->scratch.bo_size;
971
972 ret = nouveau_bo_map(bo, NOUVEAU_BO_WR, nv->client);
973 if (!ret)
974 nv->scratch.map = bo->map;
975 return !ret;
976 }
977
978 static bool
979 nouveau_scratch_more(struct nouveau_context *nv, unsigned min_size)
980 {
981 bool ret;
982
983 ret = nouveau_scratch_next(nv, min_size);
984 if (!ret)
985 ret = nouveau_scratch_runout(nv, min_size);
986 return ret;
987 }
988
989
990 /* Copy data to a scratch buffer and return address & bo the data resides in. */
991 uint64_t
992 nouveau_scratch_data(struct nouveau_context *nv,
993 const void *data, unsigned base, unsigned size,
994 struct nouveau_bo **bo)
995 {
996 unsigned bgn = MAX2(base, nv->scratch.offset);
997 unsigned end = bgn + size;
998
999 if (end >= nv->scratch.end) {
1000 end = base + size;
1001 if (!nouveau_scratch_more(nv, end))
1002 return 0;
1003 bgn = base;
1004 }
1005 nv->scratch.offset = align(end, 4);
1006
1007 memcpy(nv->scratch.map + bgn, (const uint8_t *)data + base, size);
1008
1009 *bo = nv->scratch.current;
1010 return (*bo)->offset + (bgn - base);
1011 }
1012
1013 void *
1014 nouveau_scratch_get(struct nouveau_context *nv,
1015 unsigned size, uint64_t *gpu_addr, struct nouveau_bo **pbo)
1016 {
1017 unsigned bgn = nv->scratch.offset;
1018 unsigned end = nv->scratch.offset + size;
1019
1020 if (end >= nv->scratch.end) {
1021 end = size;
1022 if (!nouveau_scratch_more(nv, end))
1023 return NULL;
1024 bgn = 0;
1025 }
1026 nv->scratch.offset = align(end, 4);
1027
1028 *pbo = nv->scratch.current;
1029 *gpu_addr = nv->scratch.current->offset + bgn;
1030 return nv->scratch.map + bgn;
1031 }