1 /*
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "si_pipe.h"
8 #include "util/u_memory.h"
9 #include "util/u_transfer.h"
10 #include "util/u_upload_mgr.h"
11
12 #include <inttypes.h>
13 #include <stdio.h>
14
si_cs_is_buffer_referenced(struct si_context * sctx,struct pb_buffer_lean * buf,unsigned usage)15 bool si_cs_is_buffer_referenced(struct si_context *sctx, struct pb_buffer_lean *buf,
16 unsigned usage)
17 {
18 return sctx->ws->cs_is_buffer_referenced(&sctx->gfx_cs, buf, usage);
19 }
20
si_buffer_map(struct si_context * sctx,struct si_resource * resource,unsigned usage)21 void *si_buffer_map(struct si_context *sctx, struct si_resource *resource,
22 unsigned usage)
23 {
24 return sctx->ws->buffer_map(sctx->ws, resource->buf, &sctx->gfx_cs, usage);
25 }
26
si_init_resource_fields(struct si_screen * sscreen,struct si_resource * res,uint64_t size,unsigned alignment)27 void si_init_resource_fields(struct si_screen *sscreen, struct si_resource *res, uint64_t size,
28 unsigned alignment)
29 {
30 struct si_texture *tex = (struct si_texture *)res;
31
32 res->bo_size = size;
33 res->bo_alignment_log2 = util_logbase2(alignment);
34 res->flags = 0;
35 res->texture_handle_allocated = false;
36 res->image_handle_allocated = false;
37
38 switch (res->b.b.usage) {
39 case PIPE_USAGE_STREAM:
40 res->flags |= RADEON_FLAG_GTT_WC;
41 res->domains = RADEON_DOMAIN_GTT;
42 break;
43 case PIPE_USAGE_STAGING:
44 /* Transfers are likely to occur more often with these
45 * resources. */
46 res->domains = RADEON_DOMAIN_GTT;
47 break;
48 case PIPE_USAGE_DYNAMIC:
49 case PIPE_USAGE_DEFAULT:
50 case PIPE_USAGE_IMMUTABLE:
51 default:
52 /* Not listing GTT here improves performance in some
53 * apps. */
54 res->domains = RADEON_DOMAIN_VRAM;
55 res->flags |= RADEON_FLAG_GTT_WC;
56 break;
57 }
58
59 if (res->b.b.target == PIPE_BUFFER && res->b.b.flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) {
60 /* Use GTT for all persistent mappings with older
61 * kernels, because they didn't always flush the HDP
62 * cache before CS execution.
63 *
64 * Write-combined CPU mappings are fine, the kernel
65 * ensures all CPU writes finish before the GPU
66 * executes a command stream.
67 *
68 * radeon doesn't have good BO move throttling, so put all
69 * persistent buffers into GTT to prevent VRAM CPU page faults.
70 */
71 if (!sscreen->info.is_amdgpu)
72 res->domains = RADEON_DOMAIN_GTT;
73 }
74
75 /* Tiled textures are unmappable. Always put them in VRAM. */
76 if ((res->b.b.target != PIPE_BUFFER && !tex->surface.is_linear) ||
77 res->b.b.flags & PIPE_RESOURCE_FLAG_UNMAPPABLE) {
78 res->domains = RADEON_DOMAIN_VRAM;
79 res->flags |= RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_GTT_WC;
80 }
81
82 /* Displayable and shareable surfaces are not suballocated. */
83 if (res->b.b.bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT))
84 res->flags |= RADEON_FLAG_NO_SUBALLOC; /* shareable */
85 else
86 res->flags |= RADEON_FLAG_NO_INTERPROCESS_SHARING;
87
88 /* PIPE_BIND_CUSTOM is used by si_vid_create_buffer which wants
89 * non-suballocated buffers.
90 */
91 if (res->b.b.bind & PIPE_BIND_CUSTOM)
92 res->flags |= RADEON_FLAG_NO_SUBALLOC;
93
94 if (res->b.b.bind & PIPE_BIND_PROTECTED ||
95 /* Force scanout/depth/stencil buffer allocation to be encrypted */
96 (sscreen->debug_flags & DBG(TMZ) &&
97 res->b.b.bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL)))
98 res->flags |= RADEON_FLAG_ENCRYPTED;
99
100 if (res->b.b.flags & PIPE_RESOURCE_FLAG_ENCRYPTED)
101 res->flags |= RADEON_FLAG_ENCRYPTED;
102
103 if (sscreen->debug_flags & DBG(NO_WC))
104 res->flags &= ~RADEON_FLAG_GTT_WC;
105
106 if (res->b.b.flags & SI_RESOURCE_FLAG_32BIT)
107 res->flags |= RADEON_FLAG_32BIT;
108
109 if (res->b.b.flags & SI_RESOURCE_FLAG_DRIVER_INTERNAL)
110 res->flags |= RADEON_FLAG_DRIVER_INTERNAL;
111
112 if (res->b.b.flags & PIPE_RESOURCE_FLAG_SPARSE)
113 res->flags |= RADEON_FLAG_SPARSE;
114
115 /* For bypassing GL2 for performance reasons (not being slowed down by GL2, and not slowing down
116 * parallel GL2 traffic) such as asynchronous DRI prime blits, or when coherency with non-GL2
117 * clients is required, such as with GFX12. GFX8 and older don't support RADEON_FLAG_GL2_BYPASS.
118 */
119 if (sscreen->info.gfx_level >= GFX9 &&
120 res->b.b.flags & SI_RESOURCE_FLAG_GL2_BYPASS)
121 res->flags |= RADEON_FLAG_GL2_BYPASS;
122
123 if (res->b.b.flags & SI_RESOURCE_FLAG_DISCARDABLE &&
124 sscreen->info.drm_major == 3 && sscreen->info.drm_minor >= 47) {
125 /* Assume VRAM, so that we can use BIG_PAGE. */
126 assert(res->domains == RADEON_DOMAIN_VRAM);
127 res->flags |= RADEON_FLAG_DISCARDABLE;
128 }
129
130 if (res->domains & RADEON_DOMAIN_VRAM) {
131 /* We don't want to evict buffers from VRAM by mapping them for CPU access,
132 * because they might never be moved back again. If a buffer is large enough,
133 * upload data by copying from a temporary GTT buffer.
134 */
135 if (sscreen->info.has_dedicated_vram && !sscreen->info.all_vram_visible &&
136 !res->b.cpu_storage && /* TODO: The CPU storage breaks this. */
137 size >= sscreen->options.max_vram_map_size)
138 res->b.b.flags |= PIPE_RESOURCE_FLAG_DONT_MAP_DIRECTLY;
139 }
140 }
141
si_alloc_resource(struct si_screen * sscreen,struct si_resource * res)142 bool si_alloc_resource(struct si_screen *sscreen, struct si_resource *res)
143 {
144 struct pb_buffer_lean *old_buf, *new_buf;
145
146 /* Allocate a new resource. */
147 new_buf = sscreen->ws->buffer_create(sscreen->ws, res->bo_size, 1 << res->bo_alignment_log2,
148 res->domains, res->flags);
149 if (!new_buf) {
150 return false;
151 }
152
153 /* Replace the pointer such that if res->buf wasn't NULL, it won't be
154 * NULL. This should prevent crashes with multiple contexts using
155 * the same buffer where one of the contexts invalidates it while
156 * the others are using it. */
157 old_buf = res->buf;
158 res->buf = new_buf; /* should be atomic */
159 res->gpu_address = sscreen->ws->buffer_get_virtual_address(res->buf);
160
161 if (res->flags & RADEON_FLAG_32BIT) {
162 uint64_t start = res->gpu_address;
163 uint64_t last = start + res->bo_size - 1;
164 (void)start;
165 (void)last;
166
167 assert((start >> 32) == sscreen->info.address32_hi);
168 assert((last >> 32) == sscreen->info.address32_hi);
169 }
170
171 radeon_bo_reference(sscreen->ws, &old_buf, NULL);
172
173 util_range_set_empty(&res->valid_buffer_range);
174 res->L2_cache_dirty = false;
175
176 if (res->b.b.target != PIPE_BUFFER && !(res->b.b.flags & SI_RESOURCE_AUX_PLANE)) {
177 /* The buffer is shared with other planes. */
178 struct si_resource *plane = (struct si_resource *)res->b.b.next;
179 for (; plane; plane = (struct si_resource *)plane->b.b.next) {
180 radeon_bo_reference(sscreen->ws, &plane->buf, res->buf);
181 plane->gpu_address = res->gpu_address;
182 }
183 }
184
185 /* Print debug information. */
186 if (sscreen->debug_flags & DBG(VM) && res->b.b.target == PIPE_BUFFER) {
187 fprintf(stderr, "VM start=0x%" PRIX64 " end=0x%" PRIX64 " | Buffer %" PRIu64 " bytes | Flags: ",
188 res->gpu_address, res->gpu_address + res->buf->size, res->buf->size);
189 si_res_print_flags(res->flags);
190 fprintf(stderr, "\n");
191 }
192
193 if (res->b.b.flags & SI_RESOURCE_FLAG_CLEAR) {
194 struct si_context *ctx = si_get_aux_context(&sscreen->aux_context.general);
195 uint32_t value = 0;
196
197 si_clear_buffer(ctx, &res->b.b, 0, res->bo_size, &value, 4, SI_AUTO_SELECT_CLEAR_METHOD,
198 false);
199 si_barrier_after_simple_buffer_op(ctx, 0, &res->b.b, NULL);
200 si_put_aux_context_flush(&sscreen->aux_context.general);
201 }
202
203 return true;
204 }
205
si_resource_destroy(struct pipe_screen * screen,struct pipe_resource * buf)206 static void si_resource_destroy(struct pipe_screen *screen, struct pipe_resource *buf)
207 {
208 if (buf->target == PIPE_BUFFER) {
209 struct si_screen *sscreen = (struct si_screen *)screen;
210 struct si_resource *buffer = si_resource(buf);
211
212 threaded_resource_deinit(buf);
213 util_range_destroy(&buffer->valid_buffer_range);
214 radeon_bo_reference(((struct si_screen*)screen)->ws, &buffer->buf, NULL);
215 util_idalloc_mt_free(&sscreen->buffer_ids, buffer->b.buffer_id_unique);
216 FREE_CL(buffer);
217 } else if (buf->flags & SI_RESOURCE_AUX_PLANE) {
218 struct si_auxiliary_texture *tex = (struct si_auxiliary_texture *)buf;
219
220 radeon_bo_reference(((struct si_screen*)screen)->ws, &tex->buffer, NULL);
221 FREE_CL(tex);
222 } else {
223 struct si_texture *tex = (struct si_texture *)buf;
224 struct si_resource *resource = &tex->buffer;
225
226 si_texture_reference(&tex->flushed_depth_texture, NULL);
227
228 if (tex->cmask_buffer != &tex->buffer) {
229 si_resource_reference(&tex->cmask_buffer, NULL);
230 }
231 radeon_bo_reference(((struct si_screen*)screen)->ws, &resource->buf, NULL);
232 FREE_CL(tex);
233 }
234 }
235
236 /* Reallocate the buffer a update all resource bindings where the buffer is
237 * bound.
238 *
239 * This is used to avoid CPU-GPU synchronizations, because it makes the buffer
240 * idle by discarding its contents.
241 */
si_invalidate_buffer(struct si_context * sctx,struct si_resource * buf)242 static bool si_invalidate_buffer(struct si_context *sctx, struct si_resource *buf)
243 {
244 /* Shared buffers can't be reallocated. */
245 if (buf->b.is_shared)
246 return false;
247
248 /* Sparse buffers can't be reallocated. */
249 if (buf->flags & RADEON_FLAG_SPARSE)
250 return false;
251
252 /* In AMD_pinned_memory, the user pointer association only gets
253 * broken when the buffer is explicitly re-allocated.
254 */
255 if (buf->b.is_user_ptr)
256 return false;
257
258 /* Check if mapping this buffer would cause waiting for the GPU. */
259 if (si_cs_is_buffer_referenced(sctx, buf->buf, RADEON_USAGE_READWRITE) ||
260 !sctx->ws->buffer_wait(sctx->ws, buf->buf, 0, RADEON_USAGE_READWRITE)) {
261 /* Reallocate the buffer in the same pipe_resource. */
262 si_alloc_resource(sctx->screen, buf);
263 si_rebind_buffer(sctx, &buf->b.b);
264 } else {
265 util_range_set_empty(&buf->valid_buffer_range);
266 }
267
268 return true;
269 }
270
271 /* Replace the storage of dst with src. */
si_replace_buffer_storage(struct pipe_context * ctx,struct pipe_resource * dst,struct pipe_resource * src,unsigned num_rebinds,uint32_t rebind_mask,uint32_t delete_buffer_id)272 void si_replace_buffer_storage(struct pipe_context *ctx, struct pipe_resource *dst,
273 struct pipe_resource *src, unsigned num_rebinds, uint32_t rebind_mask,
274 uint32_t delete_buffer_id)
275 {
276 struct si_context *sctx = (struct si_context *)ctx;
277 struct si_resource *sdst = si_resource(dst);
278 struct si_resource *ssrc = si_resource(src);
279
280 radeon_bo_reference(sctx->screen->ws, &sdst->buf, ssrc->buf);
281 sdst->gpu_address = ssrc->gpu_address;
282 sdst->b.b.bind = ssrc->b.b.bind;
283 sdst->flags = ssrc->flags;
284
285 assert(sdst->bo_size == ssrc->bo_size);
286 assert(sdst->bo_alignment_log2 == ssrc->bo_alignment_log2);
287 assert(sdst->domains == ssrc->domains);
288
289 si_rebind_buffer(sctx, dst);
290
291 util_idalloc_mt_free(&sctx->screen->buffer_ids, delete_buffer_id);
292 }
293
si_invalidate_resource(struct pipe_context * ctx,struct pipe_resource * resource)294 static void si_invalidate_resource(struct pipe_context *ctx, struct pipe_resource *resource)
295 {
296 struct si_context *sctx = (struct si_context *)ctx;
297 struct si_resource *buf = si_resource(resource);
298
299 /* We currently only do anything here for buffers */
300 if (resource->target == PIPE_BUFFER)
301 (void)si_invalidate_buffer(sctx, buf);
302 }
303
si_buffer_get_transfer(struct pipe_context * ctx,struct pipe_resource * resource,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer,void * data,struct si_resource * staging,unsigned offset)304 static void *si_buffer_get_transfer(struct pipe_context *ctx, struct pipe_resource *resource,
305 unsigned usage, const struct pipe_box *box,
306 struct pipe_transfer **ptransfer, void *data,
307 struct si_resource *staging, unsigned offset)
308 {
309 struct si_context *sctx = (struct si_context *)ctx;
310 struct si_transfer *transfer;
311
312 if (usage & PIPE_MAP_THREAD_SAFE)
313 transfer = calloc(1, sizeof(*transfer));
314 else if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
315 transfer = slab_zalloc(&sctx->pool_transfers_unsync);
316 else
317 transfer = slab_zalloc(&sctx->pool_transfers);
318
319 pipe_resource_reference(&transfer->b.b.resource, resource);
320 transfer->b.b.usage = usage;
321 transfer->b.b.box = *box;
322 transfer->b.b.offset = offset;
323 transfer->staging = staging;
324 *ptransfer = &transfer->b.b;
325 return data;
326 }
327
si_buffer_transfer_map(struct pipe_context * ctx,struct pipe_resource * resource,unsigned level,unsigned usage,const struct pipe_box * box,struct pipe_transfer ** ptransfer)328 static void *si_buffer_transfer_map(struct pipe_context *ctx, struct pipe_resource *resource,
329 unsigned level, unsigned usage, const struct pipe_box *box,
330 struct pipe_transfer **ptransfer)
331 {
332 struct si_context *sctx = (struct si_context *)ctx;
333 struct si_resource *buf = si_resource(resource);
334 uint8_t *data;
335
336 assert(resource->target == PIPE_BUFFER);
337 assert(box->x + box->width <= resource->width0);
338
339 /* From GL_AMD_pinned_memory issues:
340 *
341 * 4) Is glMapBuffer on a shared buffer guaranteed to return the
342 * same system address which was specified at creation time?
343 *
344 * RESOLVED: NO. The GL implementation might return a different
345 * virtual mapping of that memory, although the same physical
346 * page will be used.
347 *
348 * So don't ever use staging buffers.
349 */
350 if (buf->b.is_user_ptr)
351 usage |= PIPE_MAP_PERSISTENT;
352 if (usage & PIPE_MAP_ONCE)
353 usage |= RADEON_MAP_TEMPORARY;
354
355 /* See if the buffer range being mapped has never been initialized,
356 * in which case it can be mapped unsynchronized. */
357 if (!(usage & (PIPE_MAP_UNSYNCHRONIZED | TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED)) &&
358 usage & PIPE_MAP_WRITE && !buf->b.is_shared &&
359 !util_ranges_intersect(&buf->valid_buffer_range, box->x, box->x + box->width)) {
360 usage |= PIPE_MAP_UNSYNCHRONIZED;
361 }
362
363 /* If discarding the entire range, discard the whole resource instead. */
364 if (usage & PIPE_MAP_DISCARD_RANGE && box->x == 0 && box->width == resource->width0) {
365 usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
366 }
367
368 /* If a buffer in VRAM is too large and the range is discarded, don't
369 * map it directly. This makes sure that the buffer stays in VRAM.
370 */
371 bool force_discard_range = false;
372 if (usage & (PIPE_MAP_DISCARD_WHOLE_RESOURCE | PIPE_MAP_DISCARD_RANGE) &&
373 !(usage & PIPE_MAP_PERSISTENT) &&
374 buf->b.b.flags & PIPE_RESOURCE_FLAG_DONT_MAP_DIRECTLY) {
375 usage &= ~(PIPE_MAP_DISCARD_WHOLE_RESOURCE | PIPE_MAP_UNSYNCHRONIZED);
376 usage |= PIPE_MAP_DISCARD_RANGE;
377 force_discard_range = true;
378 }
379
380 if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE &&
381 !(usage & (PIPE_MAP_UNSYNCHRONIZED | TC_TRANSFER_MAP_NO_INVALIDATE))) {
382 assert(usage & PIPE_MAP_WRITE);
383
384 if (si_invalidate_buffer(sctx, buf)) {
385 /* At this point, the buffer is always idle. */
386 usage |= PIPE_MAP_UNSYNCHRONIZED;
387 } else {
388 /* Fall back to a temporary buffer. */
389 usage |= PIPE_MAP_DISCARD_RANGE;
390 }
391 }
392
393 if (usage & PIPE_MAP_DISCARD_RANGE &&
394 ((!(usage & (PIPE_MAP_UNSYNCHRONIZED | PIPE_MAP_PERSISTENT))) ||
395 (buf->flags & RADEON_FLAG_SPARSE))) {
396 assert(usage & PIPE_MAP_WRITE);
397
398 /* Check if mapping this buffer would cause waiting for the GPU.
399 */
400 if (buf->flags & (RADEON_FLAG_SPARSE | RADEON_FLAG_NO_CPU_ACCESS) ||
401 force_discard_range ||
402 si_cs_is_buffer_referenced(sctx, buf->buf, RADEON_USAGE_READWRITE) ||
403 !sctx->ws->buffer_wait(sctx->ws, buf->buf, 0, RADEON_USAGE_READWRITE)) {
404 /* Do a wait-free write-only transfer using a temporary buffer. */
405 struct u_upload_mgr *uploader;
406 struct si_resource *staging = NULL;
407 unsigned offset;
408
409 /* If we are not called from the driver thread, we have
410 * to use the uploader from u_threaded_context, which is
411 * local to the calling thread.
412 */
413 if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
414 uploader = sctx->tc->base.stream_uploader;
415 else
416 uploader = sctx->b.stream_uploader;
417
418 u_upload_alloc(uploader, 0, box->width + (box->x % SI_MAP_BUFFER_ALIGNMENT),
419 sctx->screen->info.tcc_cache_line_size, &offset,
420 (struct pipe_resource **)&staging, (void **)&data);
421
422 if (staging) {
423 data += box->x % SI_MAP_BUFFER_ALIGNMENT;
424 return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, staging,
425 offset);
426 } else if (buf->flags & RADEON_FLAG_SPARSE) {
427 return NULL;
428 }
429 } else {
430 /* At this point, the buffer is always idle (we checked it above). */
431 usage |= PIPE_MAP_UNSYNCHRONIZED;
432 }
433 }
434 /* Use a staging buffer in cached GTT for reads. */
435 else if (((usage & PIPE_MAP_READ) && !(usage & PIPE_MAP_PERSISTENT) &&
436 (buf->domains & RADEON_DOMAIN_VRAM || buf->flags & RADEON_FLAG_GTT_WC)) ||
437 (buf->flags & (RADEON_FLAG_SPARSE | RADEON_FLAG_NO_CPU_ACCESS))) {
438 struct si_resource *staging;
439
440 assert(!(usage & (TC_TRANSFER_MAP_THREADED_UNSYNC | PIPE_MAP_THREAD_SAFE)));
441 staging = si_aligned_buffer_create(ctx->screen,
442 SI_RESOURCE_FLAG_GL2_BYPASS | SI_RESOURCE_FLAG_DRIVER_INTERNAL,
443 PIPE_USAGE_STAGING,
444 box->width + (box->x % SI_MAP_BUFFER_ALIGNMENT), 256);
445 if (staging) {
446 /* Copy the VRAM buffer to the staging buffer. */
447 si_barrier_before_simple_buffer_op(sctx, 0, &staging->b.b, resource);
448 si_copy_buffer(sctx, &staging->b.b, resource, box->x % SI_MAP_BUFFER_ALIGNMENT,
449 box->x, box->width);
450 si_barrier_after_simple_buffer_op(sctx, 0, &staging->b.b, resource);
451
452 data = si_buffer_map(sctx, staging, usage & ~PIPE_MAP_UNSYNCHRONIZED);
453 if (!data) {
454 si_resource_reference(&staging, NULL);
455 return NULL;
456 }
457 data += box->x % SI_MAP_BUFFER_ALIGNMENT;
458
459 return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, staging, 0);
460 } else if (buf->flags & RADEON_FLAG_SPARSE) {
461 return NULL;
462 }
463 }
464
465 data = si_buffer_map(sctx, buf, usage);
466 if (!data) {
467 return NULL;
468 }
469 data += box->x;
470
471 return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, NULL, 0);
472 }
473
si_buffer_do_flush_region(struct pipe_context * ctx,struct pipe_transfer * transfer,const struct pipe_box * box)474 static void si_buffer_do_flush_region(struct pipe_context *ctx, struct pipe_transfer *transfer,
475 const struct pipe_box *box)
476 {
477 struct si_context *sctx = (struct si_context *)ctx;
478 struct si_transfer *stransfer = (struct si_transfer *)transfer;
479 struct si_resource *buf = si_resource(transfer->resource);
480
481 if (stransfer->staging) {
482 unsigned src_offset =
483 stransfer->b.b.offset + transfer->box.x % SI_MAP_BUFFER_ALIGNMENT + (box->x - transfer->box.x);
484
485 /* Copy the staging buffer into the original one. */
486 si_barrier_before_simple_buffer_op(sctx, 0, transfer->resource, &stransfer->staging->b.b);
487 si_copy_buffer(sctx, transfer->resource, &stransfer->staging->b.b, box->x, src_offset,
488 box->width);
489 si_barrier_after_simple_buffer_op(sctx, 0, transfer->resource, &stransfer->staging->b.b);
490 }
491
492 util_range_add(&buf->b.b, &buf->valid_buffer_range, box->x, box->x + box->width);
493 }
494
si_buffer_flush_region(struct pipe_context * ctx,struct pipe_transfer * transfer,const struct pipe_box * rel_box)495 static void si_buffer_flush_region(struct pipe_context *ctx, struct pipe_transfer *transfer,
496 const struct pipe_box *rel_box)
497 {
498 unsigned required_usage = PIPE_MAP_WRITE | PIPE_MAP_FLUSH_EXPLICIT;
499
500 if ((transfer->usage & required_usage) == required_usage) {
501 struct pipe_box box;
502
503 u_box_1d(transfer->box.x + rel_box->x, rel_box->width, &box);
504 si_buffer_do_flush_region(ctx, transfer, &box);
505 }
506 }
507
si_buffer_transfer_unmap(struct pipe_context * ctx,struct pipe_transfer * transfer)508 static void si_buffer_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *transfer)
509 {
510 struct si_context *sctx = (struct si_context *)ctx;
511 struct si_transfer *stransfer = (struct si_transfer *)transfer;
512
513 if (transfer->usage & PIPE_MAP_WRITE && !(transfer->usage & PIPE_MAP_FLUSH_EXPLICIT))
514 si_buffer_do_flush_region(ctx, transfer, &transfer->box);
515
516 if (transfer->usage & (PIPE_MAP_ONCE | RADEON_MAP_TEMPORARY) &&
517 !stransfer->staging)
518 sctx->ws->buffer_unmap(sctx->ws, si_resource(stransfer->b.b.resource)->buf);
519
520 si_resource_reference(&stransfer->staging, NULL);
521 assert(stransfer->b.staging == NULL); /* for threaded context only */
522 pipe_resource_reference(&transfer->resource, NULL);
523
524 if (transfer->usage & PIPE_MAP_THREAD_SAFE) {
525 free(transfer);
526 } else {
527 /* Don't use pool_transfers_unsync. We are always in the driver
528 * thread. Freeing an object into a different pool is allowed.
529 */
530 slab_free(&sctx->pool_transfers, transfer);
531 }
532 }
533
si_buffer_subdata(struct pipe_context * ctx,struct pipe_resource * buffer,unsigned usage,unsigned offset,unsigned size,const void * data)534 static void si_buffer_subdata(struct pipe_context *ctx, struct pipe_resource *buffer,
535 unsigned usage, unsigned offset, unsigned size, const void *data)
536 {
537 struct pipe_transfer *transfer = NULL;
538 struct pipe_box box;
539 uint8_t *map = NULL;
540
541 usage |= PIPE_MAP_WRITE;
542
543 if (!(usage & PIPE_MAP_DIRECTLY))
544 usage |= PIPE_MAP_DISCARD_RANGE;
545
546 u_box_1d(offset, size, &box);
547 map = si_buffer_transfer_map(ctx, buffer, 0, usage, &box, &transfer);
548 if (!map)
549 return;
550
551 memcpy(map, data, size);
552 si_buffer_transfer_unmap(ctx, transfer);
553 }
554
si_alloc_buffer_struct(struct pipe_screen * screen,const struct pipe_resource * templ,bool allow_cpu_storage)555 static struct si_resource *si_alloc_buffer_struct(struct pipe_screen *screen,
556 const struct pipe_resource *templ,
557 bool allow_cpu_storage)
558 {
559 struct si_resource *buf = MALLOC_STRUCT_CL(si_resource);
560
561 buf->b.b = *templ;
562 buf->b.b.next = NULL;
563 pipe_reference_init(&buf->b.b.reference, 1);
564 buf->b.b.screen = screen;
565
566 threaded_resource_init(&buf->b.b, allow_cpu_storage);
567
568 buf->buf = NULL;
569 buf->bind_history = 0;
570 buf->L2_cache_dirty = false;
571 util_range_init(&buf->valid_buffer_range);
572 return buf;
573 }
574
si_buffer_create(struct pipe_screen * screen,const struct pipe_resource * templ,unsigned alignment)575 static struct pipe_resource *si_buffer_create(struct pipe_screen *screen,
576 const struct pipe_resource *templ, unsigned alignment)
577 {
578 struct si_screen *sscreen = (struct si_screen *)screen;
579 struct si_resource *buf =
580 si_alloc_buffer_struct(screen, templ,
581 templ->width0 <= sscreen->options.tc_max_cpu_storage_size);
582
583 if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE)
584 buf->b.b.flags |= PIPE_RESOURCE_FLAG_UNMAPPABLE;
585
586 si_init_resource_fields(sscreen, buf, templ->width0, alignment);
587
588 buf->b.buffer_id_unique = util_idalloc_mt_alloc(&sscreen->buffer_ids);
589
590 if (!si_alloc_resource(sscreen, buf)) {
591 si_resource_destroy(screen, &buf->b.b);
592 return NULL;
593 }
594
595 return &buf->b.b;
596 }
597
pipe_aligned_buffer_create(struct pipe_screen * screen,unsigned flags,unsigned usage,unsigned size,unsigned alignment)598 struct pipe_resource *pipe_aligned_buffer_create(struct pipe_screen *screen, unsigned flags,
599 unsigned usage, unsigned size, unsigned alignment)
600 {
601 struct pipe_resource buffer;
602
603 memset(&buffer, 0, sizeof buffer);
604 buffer.target = PIPE_BUFFER;
605 buffer.format = PIPE_FORMAT_R8_UNORM;
606 buffer.bind = 0;
607 buffer.usage = usage;
608 buffer.flags = flags;
609 buffer.width0 = size;
610 buffer.height0 = 1;
611 buffer.depth0 = 1;
612 buffer.array_size = 1;
613 return si_buffer_create(screen, &buffer, alignment);
614 }
615
si_aligned_buffer_create(struct pipe_screen * screen,unsigned flags,unsigned usage,unsigned size,unsigned alignment)616 struct si_resource *si_aligned_buffer_create(struct pipe_screen *screen, unsigned flags,
617 unsigned usage, unsigned size, unsigned alignment)
618 {
619 return si_resource(pipe_aligned_buffer_create(screen, flags, usage, size, alignment));
620 }
621
si_buffer_from_user_memory(struct pipe_screen * screen,const struct pipe_resource * templ,void * user_memory)622 static struct pipe_resource *si_buffer_from_user_memory(struct pipe_screen *screen,
623 const struct pipe_resource *templ,
624 void *user_memory)
625 {
626 if (templ->target != PIPE_BUFFER)
627 return NULL;
628
629 struct si_screen *sscreen = (struct si_screen *)screen;
630 struct radeon_winsys *ws = sscreen->ws;
631 struct si_resource *buf = si_alloc_buffer_struct(screen, templ, false);
632
633 buf->domains = RADEON_DOMAIN_GTT;
634 buf->flags = 0;
635 buf->b.is_user_ptr = true;
636 util_range_add(&buf->b.b, &buf->valid_buffer_range, 0, templ->width0);
637 util_range_add(&buf->b.b, &buf->b.valid_buffer_range, 0, templ->width0);
638
639 buf->b.buffer_id_unique = util_idalloc_mt_alloc(&sscreen->buffer_ids);
640
641 /* Convert a user pointer to a buffer. */
642 buf->buf = ws->buffer_from_ptr(ws, user_memory, templ->width0, 0);
643 if (!buf->buf) {
644 si_resource_destroy(screen, &buf->b.b);
645 return NULL;
646 }
647
648 buf->gpu_address = ws->buffer_get_virtual_address(buf->buf);
649 buf->bo_size = templ->width0;
650 return &buf->b.b;
651 }
652
si_buffer_from_winsys_buffer(struct pipe_screen * screen,const struct pipe_resource * templ,struct pb_buffer_lean * imported_buf,uint64_t offset)653 struct pipe_resource *si_buffer_from_winsys_buffer(struct pipe_screen *screen,
654 const struct pipe_resource *templ,
655 struct pb_buffer_lean *imported_buf,
656 uint64_t offset)
657 {
658 if (offset + templ->width0 > imported_buf->size)
659 return NULL;
660
661 struct si_screen *sscreen = (struct si_screen *)screen;
662 struct si_resource *res = si_alloc_buffer_struct(screen, templ, false);
663
664 if (!res)
665 return NULL;
666
667 enum radeon_bo_domain domains = sscreen->ws->buffer_get_initial_domain(imported_buf);
668
669 /* Get or guess the BO flags. */
670 unsigned flags = RADEON_FLAG_NO_SUBALLOC;
671
672 if (sscreen->ws->buffer_get_flags)
673 res->flags |= sscreen->ws->buffer_get_flags(imported_buf);
674 else
675 flags |= RADEON_FLAG_GTT_WC; /* unknown flags, guess them */
676
677 /* Deduce the usage. */
678 switch (domains) {
679 case RADEON_DOMAIN_VRAM:
680 case RADEON_DOMAIN_VRAM_GTT:
681 res->b.b.usage = PIPE_USAGE_DEFAULT;
682 break;
683
684 default:
685 /* Other values are interpreted as GTT. */
686 domains = RADEON_DOMAIN_GTT;
687
688 if (flags & RADEON_FLAG_GTT_WC)
689 res->b.b.usage = PIPE_USAGE_STREAM;
690 else
691 res->b.b.usage = PIPE_USAGE_STAGING;
692 }
693
694 si_init_resource_fields(sscreen, res, imported_buf->size,
695 1 << imported_buf->alignment_log2);
696
697 res->b.is_shared = true;
698 res->b.buffer_id_unique = util_idalloc_mt_alloc(&sscreen->buffer_ids);
699 res->buf = imported_buf;
700 res->gpu_address = sscreen->ws->buffer_get_virtual_address(res->buf) + offset;
701 res->domains = domains;
702 res->flags = flags;
703
704 if (res->flags & RADEON_FLAG_NO_CPU_ACCESS)
705 res->b.b.flags |= PIPE_RESOURCE_FLAG_UNMAPPABLE;
706
707 util_range_add(&res->b.b, &res->valid_buffer_range, 0, templ->width0);
708 util_range_add(&res->b.b, &res->b.valid_buffer_range, 0, templ->width0);
709
710 return &res->b.b;
711 }
712
si_resource_create(struct pipe_screen * screen,const struct pipe_resource * templ)713 static struct pipe_resource *si_resource_create(struct pipe_screen *screen,
714 const struct pipe_resource *templ)
715 {
716 if (templ->target == PIPE_BUFFER) {
717 return si_buffer_create(screen, templ, 256);
718 } else {
719 return si_texture_create(screen, templ);
720 }
721 }
722
si_buffer_commit(struct si_context * ctx,struct si_resource * res,struct pipe_box * box,bool commit)723 static bool si_buffer_commit(struct si_context *ctx, struct si_resource *res,
724 struct pipe_box *box, bool commit)
725 {
726 return ctx->ws->buffer_commit(ctx->ws, res->buf, box->x, box->width, commit);
727 }
728
si_resource_commit(struct pipe_context * pctx,struct pipe_resource * resource,unsigned level,struct pipe_box * box,bool commit)729 static bool si_resource_commit(struct pipe_context *pctx, struct pipe_resource *resource,
730 unsigned level, struct pipe_box *box, bool commit)
731 {
732 struct si_context *ctx = (struct si_context *)pctx;
733 struct si_resource *res = si_resource(resource);
734
735 /*
736 * Since buffer commitment changes cannot be pipelined, we need to
737 * (a) flush any pending commands that refer to the buffer we're about
738 * to change, and
739 * (b) wait for threaded submit to finish, including those that were
740 * triggered by some other, earlier operation.
741 */
742 if (radeon_emitted(&ctx->gfx_cs, ctx->initial_gfx_cs_size) &&
743 ctx->ws->cs_is_buffer_referenced(&ctx->gfx_cs, res->buf, RADEON_USAGE_READWRITE)) {
744 si_flush_gfx_cs(ctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
745 }
746 ctx->ws->cs_sync_flush(&ctx->gfx_cs);
747
748 if (resource->target == PIPE_BUFFER)
749 return si_buffer_commit(ctx, res, box, commit);
750 else
751 return si_texture_commit(ctx, res, level, box, commit);
752 }
753
si_init_screen_buffer_functions(struct si_screen * sscreen)754 void si_init_screen_buffer_functions(struct si_screen *sscreen)
755 {
756 sscreen->b.resource_create = si_resource_create;
757 sscreen->b.resource_destroy = si_resource_destroy;
758 sscreen->b.resource_from_user_memory = si_buffer_from_user_memory;
759 }
760
si_init_buffer_functions(struct si_context * sctx)761 void si_init_buffer_functions(struct si_context *sctx)
762 {
763 sctx->b.invalidate_resource = si_invalidate_resource;
764 sctx->b.buffer_map = si_buffer_transfer_map;
765 sctx->b.transfer_flush_region = si_buffer_flush_region;
766 sctx->b.buffer_unmap = si_buffer_transfer_unmap;
767 sctx->b.texture_subdata = u_default_texture_subdata;
768 sctx->b.buffer_subdata = si_buffer_subdata;
769 sctx->b.resource_commit = si_resource_commit;
770 }
771