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