• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Advanced Micro Devices, Inc.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22  * USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include "radeonsi/si_pipe.h"
26 #include "util/u_memory.h"
27 #include "util/u_transfer.h"
28 #include "util/u_upload_mgr.h"
29 
30 #include <inttypes.h>
31 #include <stdio.h>
32 
si_rings_is_buffer_referenced(struct si_context * sctx,struct pb_buffer * buf,enum radeon_bo_usage usage)33 bool si_rings_is_buffer_referenced(struct si_context *sctx, struct pb_buffer *buf,
34                                    enum radeon_bo_usage usage)
35 {
36    if (sctx->ws->cs_is_buffer_referenced(sctx->gfx_cs, buf, usage)) {
37       return true;
38    }
39    if (radeon_emitted(sctx->sdma_cs, 0) &&
40        sctx->ws->cs_is_buffer_referenced(sctx->sdma_cs, buf, usage)) {
41       return true;
42    }
43    return false;
44 }
45 
si_buffer_map_sync_with_rings(struct si_context * sctx,struct si_resource * resource,unsigned usage)46 void *si_buffer_map_sync_with_rings(struct si_context *sctx, struct si_resource *resource,
47                                     unsigned usage)
48 {
49    enum radeon_bo_usage rusage = RADEON_USAGE_READWRITE;
50    bool busy = false;
51 
52    assert(!(resource->flags & RADEON_FLAG_SPARSE));
53 
54    if (usage & PIPE_MAP_UNSYNCHRONIZED) {
55       return sctx->ws->buffer_map(resource->buf, NULL, usage);
56    }
57 
58    if (!(usage & PIPE_MAP_WRITE)) {
59       /* have to wait for the last write */
60       rusage = RADEON_USAGE_WRITE;
61    }
62 
63    if (radeon_emitted(sctx->gfx_cs, sctx->initial_gfx_cs_size) &&
64        sctx->ws->cs_is_buffer_referenced(sctx->gfx_cs, resource->buf, rusage)) {
65       if (usage & PIPE_MAP_DONTBLOCK) {
66          si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
67          return NULL;
68       } else {
69          si_flush_gfx_cs(sctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
70          busy = true;
71       }
72    }
73    if (radeon_emitted(sctx->sdma_cs, 0) &&
74        sctx->ws->cs_is_buffer_referenced(sctx->sdma_cs, resource->buf, rusage)) {
75       if (usage & PIPE_MAP_DONTBLOCK) {
76          si_flush_dma_cs(sctx, PIPE_FLUSH_ASYNC, NULL);
77          return NULL;
78       } else {
79          si_flush_dma_cs(sctx, 0, NULL);
80          busy = true;
81       }
82    }
83 
84    if (busy || !sctx->ws->buffer_wait(resource->buf, 0, rusage)) {
85       if (usage & PIPE_MAP_DONTBLOCK) {
86          return NULL;
87       } else {
88          /* We will be wait for the GPU. Wait for any offloaded
89           * CS flush to complete to avoid busy-waiting in the winsys. */
90          sctx->ws->cs_sync_flush(sctx->gfx_cs);
91          if (sctx->sdma_cs)
92             sctx->ws->cs_sync_flush(sctx->sdma_cs);
93       }
94    }
95 
96    /* Setting the CS to NULL will prevent doing checks we have done already. */
97    return sctx->ws->buffer_map(resource->buf, NULL, usage);
98 }
99 
si_init_resource_fields(struct si_screen * sscreen,struct si_resource * res,uint64_t size,unsigned alignment)100 void si_init_resource_fields(struct si_screen *sscreen, struct si_resource *res, uint64_t size,
101                              unsigned alignment)
102 {
103    struct si_texture *tex = (struct si_texture *)res;
104 
105    res->bo_size = size;
106    res->bo_alignment = alignment;
107    res->flags = 0;
108    res->texture_handle_allocated = false;
109    res->image_handle_allocated = false;
110 
111    switch (res->b.b.usage) {
112    case PIPE_USAGE_STREAM:
113       res->flags = RADEON_FLAG_GTT_WC;
114       /* fall through */
115    case PIPE_USAGE_STAGING:
116       /* Transfers are likely to occur more often with these
117        * resources. */
118       res->domains = RADEON_DOMAIN_GTT;
119       break;
120    case PIPE_USAGE_DYNAMIC:
121       /* Older kernels didn't always flush the HDP cache before
122        * CS execution
123        */
124       if (!sscreen->info.kernel_flushes_hdp_before_ib) {
125          res->domains = RADEON_DOMAIN_GTT;
126          res->flags |= RADEON_FLAG_GTT_WC;
127          break;
128       }
129       /* fall through */
130    case PIPE_USAGE_DEFAULT:
131    case PIPE_USAGE_IMMUTABLE:
132    default:
133       /* Not listing GTT here improves performance in some
134        * apps. */
135       res->domains = RADEON_DOMAIN_VRAM;
136       res->flags |= RADEON_FLAG_GTT_WC;
137       break;
138    }
139 
140    if (res->b.b.target == PIPE_BUFFER && res->b.b.flags & PIPE_RESOURCE_FLAG_MAP_PERSISTENT) {
141       /* Use GTT for all persistent mappings with older
142        * kernels, because they didn't always flush the HDP
143        * cache before CS execution.
144        *
145        * Write-combined CPU mappings are fine, the kernel
146        * ensures all CPU writes finish before the GPU
147        * executes a command stream.
148        *
149        * radeon doesn't have good BO move throttling, so put all
150        * persistent buffers into GTT to prevent VRAM CPU page faults.
151        */
152       if (!sscreen->info.kernel_flushes_hdp_before_ib || !sscreen->info.is_amdgpu)
153          res->domains = RADEON_DOMAIN_GTT;
154    }
155 
156    /* Tiled textures are unmappable. Always put them in VRAM. */
157    if ((res->b.b.target != PIPE_BUFFER && !tex->surface.is_linear) ||
158        res->b.b.flags & SI_RESOURCE_FLAG_UNMAPPABLE) {
159       res->domains = RADEON_DOMAIN_VRAM;
160       res->flags |= RADEON_FLAG_NO_CPU_ACCESS | RADEON_FLAG_GTT_WC;
161    }
162 
163    /* Displayable and shareable surfaces are not suballocated. */
164    if (res->b.b.bind & (PIPE_BIND_SHARED | PIPE_BIND_SCANOUT))
165       res->flags |= RADEON_FLAG_NO_SUBALLOC; /* shareable */
166    else
167       res->flags |= RADEON_FLAG_NO_INTERPROCESS_SHARING;
168 
169    if (res->b.b.bind & PIPE_BIND_PROTECTED ||
170        /* Force scanout/depth/stencil buffer allocation to be encrypted */
171        (sscreen->debug_flags & DBG(TMZ) &&
172         res->b.b.bind & (PIPE_BIND_SCANOUT | PIPE_BIND_DEPTH_STENCIL)))
173       res->flags |= RADEON_FLAG_ENCRYPTED;
174 
175    if (res->b.b.flags & PIPE_RESOURCE_FLAG_ENCRYPTED)
176       res->flags |= RADEON_FLAG_ENCRYPTED;
177 
178    if (sscreen->debug_flags & DBG(NO_WC))
179       res->flags &= ~RADEON_FLAG_GTT_WC;
180 
181    if (res->b.b.flags & SI_RESOURCE_FLAG_READ_ONLY)
182       res->flags |= RADEON_FLAG_READ_ONLY;
183 
184    if (res->b.b.flags & SI_RESOURCE_FLAG_32BIT)
185       res->flags |= RADEON_FLAG_32BIT;
186 
187    if (res->b.b.flags & SI_RESOURCE_FLAG_DRIVER_INTERNAL)
188       res->flags |= RADEON_FLAG_DRIVER_INTERNAL;
189 
190    /* For higher throughput and lower latency over PCIe assuming sequential access.
191     * Only CP DMA, SDMA, and optimized compute benefit from this.
192     * GFX8 and older don't support RADEON_FLAG_UNCACHED.
193     */
194    if (sscreen->info.chip_class >= GFX9 &&
195        res->b.b.flags & SI_RESOURCE_FLAG_UNCACHED)
196       res->flags |= RADEON_FLAG_UNCACHED;
197 
198    /* Set expected VRAM and GART usage for the buffer. */
199    res->vram_usage = 0;
200    res->gart_usage = 0;
201    res->max_forced_staging_uploads = 0;
202    res->b.max_forced_staging_uploads = 0;
203 
204    if (res->domains & RADEON_DOMAIN_VRAM) {
205       res->vram_usage = size;
206 
207       res->max_forced_staging_uploads = res->b.max_forced_staging_uploads =
208          sscreen->info.has_dedicated_vram && size >= sscreen->info.vram_vis_size / 4 ? 1 : 0;
209    } else if (res->domains & RADEON_DOMAIN_GTT) {
210       res->gart_usage = size;
211    }
212 }
213 
si_alloc_resource(struct si_screen * sscreen,struct si_resource * res)214 bool si_alloc_resource(struct si_screen *sscreen, struct si_resource *res)
215 {
216    struct pb_buffer *old_buf, *new_buf;
217 
218    /* Allocate a new resource. */
219    new_buf = sscreen->ws->buffer_create(sscreen->ws, res->bo_size, res->bo_alignment, res->domains,
220                                         res->flags);
221    if (!new_buf) {
222       return false;
223    }
224 
225    /* Replace the pointer such that if res->buf wasn't NULL, it won't be
226     * NULL. This should prevent crashes with multiple contexts using
227     * the same buffer where one of the contexts invalidates it while
228     * the others are using it. */
229    old_buf = res->buf;
230    res->buf = new_buf; /* should be atomic */
231    res->gpu_address = sscreen->ws->buffer_get_virtual_address(res->buf);
232 
233    if (res->flags & RADEON_FLAG_32BIT) {
234       uint64_t start = res->gpu_address;
235       uint64_t last = start + res->bo_size - 1;
236       (void)start;
237       (void)last;
238 
239       assert((start >> 32) == sscreen->info.address32_hi);
240       assert((last >> 32) == sscreen->info.address32_hi);
241    }
242 
243    pb_reference(&old_buf, NULL);
244 
245    util_range_set_empty(&res->valid_buffer_range);
246    res->TC_L2_dirty = false;
247 
248    /* Print debug information. */
249    if (sscreen->debug_flags & DBG(VM) && res->b.b.target == PIPE_BUFFER) {
250       fprintf(stderr, "VM start=0x%" PRIX64 "  end=0x%" PRIX64 " | Buffer %" PRIu64 " bytes\n",
251               res->gpu_address, res->gpu_address + res->buf->size, res->buf->size);
252    }
253 
254    if (res->b.b.flags & SI_RESOURCE_FLAG_CLEAR)
255       si_screen_clear_buffer(sscreen, &res->b.b, 0, res->bo_size, 0);
256 
257    return true;
258 }
259 
si_buffer_destroy(struct pipe_screen * screen,struct pipe_resource * buf)260 static void si_buffer_destroy(struct pipe_screen *screen, struct pipe_resource *buf)
261 {
262    struct si_resource *buffer = si_resource(buf);
263 
264    threaded_resource_deinit(buf);
265    util_range_destroy(&buffer->valid_buffer_range);
266    pb_reference(&buffer->buf, NULL);
267    FREE(buffer);
268 }
269 
270 /* Reallocate the buffer a update all resource bindings where the buffer is
271  * bound.
272  *
273  * This is used to avoid CPU-GPU synchronizations, because it makes the buffer
274  * idle by discarding its contents.
275  */
si_invalidate_buffer(struct si_context * sctx,struct si_resource * buf)276 static bool si_invalidate_buffer(struct si_context *sctx, struct si_resource *buf)
277 {
278    /* Shared buffers can't be reallocated. */
279    if (buf->b.is_shared)
280       return false;
281 
282    /* Sparse buffers can't be reallocated. */
283    if (buf->flags & RADEON_FLAG_SPARSE)
284       return false;
285 
286    /* In AMD_pinned_memory, the user pointer association only gets
287     * broken when the buffer is explicitly re-allocated.
288     */
289    if (buf->b.is_user_ptr)
290       return false;
291 
292    /* Check if mapping this buffer would cause waiting for the GPU. */
293    if (si_rings_is_buffer_referenced(sctx, buf->buf, RADEON_USAGE_READWRITE) ||
294        !sctx->ws->buffer_wait(buf->buf, 0, RADEON_USAGE_READWRITE)) {
295       /* Reallocate the buffer in the same pipe_resource. */
296       si_alloc_resource(sctx->screen, buf);
297       si_rebind_buffer(sctx, &buf->b.b);
298    } else {
299       util_range_set_empty(&buf->valid_buffer_range);
300    }
301 
302    return true;
303 }
304 
305 /* Replace the storage of dst with src. */
si_replace_buffer_storage(struct pipe_context * ctx,struct pipe_resource * dst,struct pipe_resource * src)306 void si_replace_buffer_storage(struct pipe_context *ctx, struct pipe_resource *dst,
307                                struct pipe_resource *src)
308 {
309    struct si_context *sctx = (struct si_context *)ctx;
310    struct si_resource *sdst = si_resource(dst);
311    struct si_resource *ssrc = si_resource(src);
312 
313    pb_reference(&sdst->buf, ssrc->buf);
314    sdst->gpu_address = ssrc->gpu_address;
315    sdst->b.b.bind = ssrc->b.b.bind;
316    sdst->b.max_forced_staging_uploads = ssrc->b.max_forced_staging_uploads;
317    sdst->max_forced_staging_uploads = ssrc->max_forced_staging_uploads;
318    sdst->flags = ssrc->flags;
319 
320    assert(sdst->vram_usage == ssrc->vram_usage);
321    assert(sdst->gart_usage == ssrc->gart_usage);
322    assert(sdst->bo_size == ssrc->bo_size);
323    assert(sdst->bo_alignment == ssrc->bo_alignment);
324    assert(sdst->domains == ssrc->domains);
325 
326    si_rebind_buffer(sctx, dst);
327 }
328 
si_invalidate_resource(struct pipe_context * ctx,struct pipe_resource * resource)329 static void si_invalidate_resource(struct pipe_context *ctx, struct pipe_resource *resource)
330 {
331    struct si_context *sctx = (struct si_context *)ctx;
332    struct si_resource *buf = si_resource(resource);
333 
334    /* We currently only do anyting here for buffers */
335    if (resource->target == PIPE_BUFFER)
336       (void)si_invalidate_buffer(sctx, buf);
337 }
338 
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)339 static void *si_buffer_get_transfer(struct pipe_context *ctx, struct pipe_resource *resource,
340                                     unsigned usage, const struct pipe_box *box,
341                                     struct pipe_transfer **ptransfer, void *data,
342                                     struct si_resource *staging, unsigned offset)
343 {
344    struct si_context *sctx = (struct si_context *)ctx;
345    struct si_transfer *transfer;
346 
347    if (usage & PIPE_MAP_THREAD_SAFE)
348       transfer = malloc(sizeof(*transfer));
349    else if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
350       transfer = slab_alloc(&sctx->pool_transfers_unsync);
351    else
352       transfer = slab_alloc(&sctx->pool_transfers);
353 
354    transfer->b.b.resource = NULL;
355    pipe_resource_reference(&transfer->b.b.resource, resource);
356    transfer->b.b.level = 0;
357    transfer->b.b.usage = usage;
358    transfer->b.b.box = *box;
359    transfer->b.b.stride = 0;
360    transfer->b.b.layer_stride = 0;
361    transfer->b.staging = NULL;
362    transfer->offset = offset;
363    transfer->staging = staging;
364    *ptransfer = &transfer->b.b;
365    return data;
366 }
367 
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)368 static void *si_buffer_transfer_map(struct pipe_context *ctx, struct pipe_resource *resource,
369                                     unsigned level, unsigned usage, const struct pipe_box *box,
370                                     struct pipe_transfer **ptransfer)
371 {
372    struct si_context *sctx = (struct si_context *)ctx;
373    struct si_resource *buf = si_resource(resource);
374    uint8_t *data;
375 
376    assert(box->x + box->width <= resource->width0);
377 
378    /* From GL_AMD_pinned_memory issues:
379     *
380     *     4) Is glMapBuffer on a shared buffer guaranteed to return the
381     *        same system address which was specified at creation time?
382     *
383     *        RESOLVED: NO. The GL implementation might return a different
384     *        virtual mapping of that memory, although the same physical
385     *        page will be used.
386     *
387     * So don't ever use staging buffers.
388     */
389    if (buf->b.is_user_ptr)
390       usage |= PIPE_MAP_PERSISTENT;
391 
392    /* See if the buffer range being mapped has never been initialized,
393     * in which case it can be mapped unsynchronized. */
394    if (!(usage & (PIPE_MAP_UNSYNCHRONIZED | TC_TRANSFER_MAP_NO_INFER_UNSYNCHRONIZED)) &&
395        usage & PIPE_MAP_WRITE && !buf->b.is_shared &&
396        !util_ranges_intersect(&buf->valid_buffer_range, box->x, box->x + box->width)) {
397       usage |= PIPE_MAP_UNSYNCHRONIZED;
398    }
399 
400    /* If discarding the entire range, discard the whole resource instead. */
401    if (usage & PIPE_MAP_DISCARD_RANGE && box->x == 0 && box->width == resource->width0) {
402       usage |= PIPE_MAP_DISCARD_WHOLE_RESOURCE;
403    }
404 
405    /* If a buffer in VRAM is too large and the range is discarded, don't
406     * map it directly. This makes sure that the buffer stays in VRAM.
407     */
408    bool force_discard_range = false;
409    if (usage & (PIPE_MAP_DISCARD_WHOLE_RESOURCE | PIPE_MAP_DISCARD_RANGE) &&
410        !(usage & PIPE_MAP_PERSISTENT) &&
411        /* Try not to decrement the counter if it's not positive. Still racy,
412         * but it makes it harder to wrap the counter from INT_MIN to INT_MAX. */
413        buf->max_forced_staging_uploads > 0 &&
414        p_atomic_dec_return(&buf->max_forced_staging_uploads) >= 0) {
415       usage &= ~(PIPE_MAP_DISCARD_WHOLE_RESOURCE | PIPE_MAP_UNSYNCHRONIZED);
416       usage |= PIPE_MAP_DISCARD_RANGE;
417       force_discard_range = true;
418    }
419 
420    if (usage & PIPE_MAP_DISCARD_WHOLE_RESOURCE &&
421        !(usage & (PIPE_MAP_UNSYNCHRONIZED | TC_TRANSFER_MAP_NO_INVALIDATE))) {
422       assert(usage & PIPE_MAP_WRITE);
423 
424       if (si_invalidate_buffer(sctx, buf)) {
425          /* At this point, the buffer is always idle. */
426          usage |= PIPE_MAP_UNSYNCHRONIZED;
427       } else {
428          /* Fall back to a temporary buffer. */
429          usage |= PIPE_MAP_DISCARD_RANGE;
430       }
431    }
432 
433    if (usage & PIPE_MAP_FLUSH_EXPLICIT &&
434        buf->b.b.flags & SI_RESOURCE_FLAG_UPLOAD_FLUSH_EXPLICIT_VIA_SDMA) {
435       usage &= ~(PIPE_MAP_UNSYNCHRONIZED | PIPE_MAP_PERSISTENT);
436       usage |= PIPE_MAP_DISCARD_RANGE;
437       force_discard_range = true;
438    }
439 
440    if (usage & PIPE_MAP_DISCARD_RANGE &&
441        ((!(usage & (PIPE_MAP_UNSYNCHRONIZED | PIPE_MAP_PERSISTENT))) ||
442         (buf->flags & RADEON_FLAG_SPARSE))) {
443       assert(usage & PIPE_MAP_WRITE);
444 
445       /* Check if mapping this buffer would cause waiting for the GPU.
446        */
447       if (buf->flags & RADEON_FLAG_SPARSE || force_discard_range ||
448           si_rings_is_buffer_referenced(sctx, buf->buf, RADEON_USAGE_READWRITE) ||
449           !sctx->ws->buffer_wait(buf->buf, 0, RADEON_USAGE_READWRITE)) {
450          /* Do a wait-free write-only transfer using a temporary buffer. */
451          struct u_upload_mgr *uploader;
452          struct si_resource *staging = NULL;
453          unsigned offset;
454 
455          /* If we are not called from the driver thread, we have
456           * to use the uploader from u_threaded_context, which is
457           * local to the calling thread.
458           */
459          if (usage & TC_TRANSFER_MAP_THREADED_UNSYNC)
460             uploader = sctx->tc->base.stream_uploader;
461          else
462             uploader = sctx->b.stream_uploader;
463 
464          u_upload_alloc(uploader, 0, box->width + (box->x % SI_MAP_BUFFER_ALIGNMENT),
465                         sctx->screen->info.tcc_cache_line_size, &offset,
466                         (struct pipe_resource **)&staging, (void **)&data);
467 
468          if (staging) {
469             data += box->x % SI_MAP_BUFFER_ALIGNMENT;
470             return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, staging,
471                                           offset);
472          } else if (buf->flags & RADEON_FLAG_SPARSE) {
473             return NULL;
474          }
475       } else {
476          /* At this point, the buffer is always idle (we checked it above). */
477          usage |= PIPE_MAP_UNSYNCHRONIZED;
478       }
479    }
480    /* Use a staging buffer in cached GTT for reads. */
481    else if (((usage & PIPE_MAP_READ) && !(usage & PIPE_MAP_PERSISTENT) &&
482              (buf->domains & RADEON_DOMAIN_VRAM || buf->flags & RADEON_FLAG_GTT_WC)) ||
483             (buf->flags & RADEON_FLAG_SPARSE)) {
484       struct si_resource *staging;
485 
486       assert(!(usage & (TC_TRANSFER_MAP_THREADED_UNSYNC | PIPE_MAP_THREAD_SAFE)));
487       staging = si_aligned_buffer_create(ctx->screen,
488                                          SI_RESOURCE_FLAG_UNCACHED | SI_RESOURCE_FLAG_DRIVER_INTERNAL,
489                                          PIPE_USAGE_STAGING,
490                                          box->width + (box->x % SI_MAP_BUFFER_ALIGNMENT), 256);
491       if (staging) {
492          /* Copy the VRAM buffer to the staging buffer. */
493          si_sdma_copy_buffer(sctx, &staging->b.b, resource, box->x % SI_MAP_BUFFER_ALIGNMENT,
494                              box->x, box->width);
495 
496          data = si_buffer_map_sync_with_rings(sctx, staging, usage & ~PIPE_MAP_UNSYNCHRONIZED);
497          if (!data) {
498             si_resource_reference(&staging, NULL);
499             return NULL;
500          }
501          data += box->x % SI_MAP_BUFFER_ALIGNMENT;
502 
503          return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, staging, 0);
504       } else if (buf->flags & RADEON_FLAG_SPARSE) {
505          return NULL;
506       }
507    }
508 
509    data = si_buffer_map_sync_with_rings(sctx, buf, usage);
510    if (!data) {
511       return NULL;
512    }
513    data += box->x;
514 
515    return si_buffer_get_transfer(ctx, resource, usage, box, ptransfer, data, NULL, 0);
516 }
517 
si_buffer_do_flush_region(struct pipe_context * ctx,struct pipe_transfer * transfer,const struct pipe_box * box)518 static void si_buffer_do_flush_region(struct pipe_context *ctx, struct pipe_transfer *transfer,
519                                       const struct pipe_box *box)
520 {
521    struct si_context *sctx = (struct si_context *)ctx;
522    struct si_transfer *stransfer = (struct si_transfer *)transfer;
523    struct si_resource *buf = si_resource(transfer->resource);
524 
525    if (stransfer->staging) {
526       unsigned src_offset =
527          stransfer->offset + transfer->box.x % SI_MAP_BUFFER_ALIGNMENT + (box->x - transfer->box.x);
528 
529       if (buf->b.b.flags & SI_RESOURCE_FLAG_UPLOAD_FLUSH_EXPLICIT_VIA_SDMA) {
530          /* This should be true for all uploaders. */
531          assert(transfer->box.x == 0);
532 
533          /* Find a previous upload and extend its range. The last
534           * upload is likely to be at the end of the list.
535           */
536          for (int i = sctx->num_sdma_uploads - 1; i >= 0; i--) {
537             struct si_sdma_upload *up = &sctx->sdma_uploads[i];
538 
539             if (up->dst != buf)
540                continue;
541 
542             assert(up->src == stransfer->staging);
543             assert(box->x > up->dst_offset);
544             up->size = box->x + box->width - up->dst_offset;
545             return;
546          }
547 
548          /* Enlarge the array if it's full. */
549          if (sctx->num_sdma_uploads == sctx->max_sdma_uploads) {
550             unsigned size;
551 
552             sctx->max_sdma_uploads += 4;
553             size = sctx->max_sdma_uploads * sizeof(sctx->sdma_uploads[0]);
554             sctx->sdma_uploads = realloc(sctx->sdma_uploads, size);
555          }
556 
557          /* Add a new upload. */
558          struct si_sdma_upload *up = &sctx->sdma_uploads[sctx->num_sdma_uploads++];
559          up->dst = up->src = NULL;
560          si_resource_reference(&up->dst, buf);
561          si_resource_reference(&up->src, stransfer->staging);
562          up->dst_offset = box->x;
563          up->src_offset = src_offset;
564          up->size = box->width;
565          return;
566       }
567 
568       /* Copy the staging buffer into the original one. */
569       si_copy_buffer(sctx, transfer->resource, &stransfer->staging->b.b, box->x, src_offset,
570                      box->width);
571    }
572 
573    util_range_add(&buf->b.b, &buf->valid_buffer_range, box->x, box->x + box->width);
574 }
575 
si_buffer_flush_region(struct pipe_context * ctx,struct pipe_transfer * transfer,const struct pipe_box * rel_box)576 static void si_buffer_flush_region(struct pipe_context *ctx, struct pipe_transfer *transfer,
577                                    const struct pipe_box *rel_box)
578 {
579    unsigned required_usage = PIPE_MAP_WRITE | PIPE_MAP_FLUSH_EXPLICIT;
580 
581    if ((transfer->usage & required_usage) == required_usage) {
582       struct pipe_box box;
583 
584       u_box_1d(transfer->box.x + rel_box->x, rel_box->width, &box);
585       si_buffer_do_flush_region(ctx, transfer, &box);
586    }
587 }
588 
si_buffer_transfer_unmap(struct pipe_context * ctx,struct pipe_transfer * transfer)589 static void si_buffer_transfer_unmap(struct pipe_context *ctx, struct pipe_transfer *transfer)
590 {
591    struct si_context *sctx = (struct si_context *)ctx;
592    struct si_transfer *stransfer = (struct si_transfer *)transfer;
593 
594    if (transfer->usage & PIPE_MAP_WRITE && !(transfer->usage & PIPE_MAP_FLUSH_EXPLICIT))
595       si_buffer_do_flush_region(ctx, transfer, &transfer->box);
596 
597    si_resource_reference(&stransfer->staging, NULL);
598    assert(stransfer->b.staging == NULL); /* for threaded context only */
599    pipe_resource_reference(&transfer->resource, NULL);
600 
601    if (transfer->usage & PIPE_MAP_THREAD_SAFE) {
602       free(transfer);
603    } else {
604       /* Don't use pool_transfers_unsync. We are always in the driver
605        * thread. Freeing an object into a different pool is allowed.
606        */
607       slab_free(&sctx->pool_transfers, transfer);
608    }
609 }
610 
si_buffer_subdata(struct pipe_context * ctx,struct pipe_resource * buffer,unsigned usage,unsigned offset,unsigned size,const void * data)611 static void si_buffer_subdata(struct pipe_context *ctx, struct pipe_resource *buffer,
612                               unsigned usage, unsigned offset, unsigned size, const void *data)
613 {
614    struct pipe_transfer *transfer = NULL;
615    struct pipe_box box;
616    uint8_t *map = NULL;
617 
618    usage |= PIPE_MAP_WRITE;
619 
620    if (!(usage & PIPE_MAP_DIRECTLY))
621       usage |= PIPE_MAP_DISCARD_RANGE;
622 
623    u_box_1d(offset, size, &box);
624    map = si_buffer_transfer_map(ctx, buffer, 0, usage, &box, &transfer);
625    if (!map)
626       return;
627 
628    memcpy(map, data, size);
629    si_buffer_transfer_unmap(ctx, transfer);
630 }
631 
632 static const struct u_resource_vtbl si_buffer_vtbl = {
633    NULL,                     /* get_handle */
634    si_buffer_destroy,        /* resource_destroy */
635    si_buffer_transfer_map,   /* transfer_map */
636    si_buffer_flush_region,   /* transfer_flush_region */
637    si_buffer_transfer_unmap, /* transfer_unmap */
638 };
639 
si_alloc_buffer_struct(struct pipe_screen * screen,const struct pipe_resource * templ)640 static struct si_resource *si_alloc_buffer_struct(struct pipe_screen *screen,
641                                                   const struct pipe_resource *templ)
642 {
643    struct si_resource *buf;
644 
645    buf = MALLOC_STRUCT(si_resource);
646 
647    buf->b.b = *templ;
648    buf->b.b.next = NULL;
649    pipe_reference_init(&buf->b.b.reference, 1);
650    buf->b.b.screen = screen;
651 
652    buf->b.vtbl = &si_buffer_vtbl;
653    threaded_resource_init(&buf->b.b);
654 
655    buf->buf = NULL;
656    buf->bind_history = 0;
657    buf->TC_L2_dirty = false;
658    util_range_init(&buf->valid_buffer_range);
659    return buf;
660 }
661 
si_buffer_create(struct pipe_screen * screen,const struct pipe_resource * templ,unsigned alignment)662 static struct pipe_resource *si_buffer_create(struct pipe_screen *screen,
663                                               const struct pipe_resource *templ, unsigned alignment)
664 {
665    struct si_screen *sscreen = (struct si_screen *)screen;
666    struct si_resource *buf = si_alloc_buffer_struct(screen, templ);
667 
668    if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE)
669       buf->b.b.flags |= SI_RESOURCE_FLAG_UNMAPPABLE;
670 
671    si_init_resource_fields(sscreen, buf, templ->width0, alignment);
672 
673    if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE)
674       buf->flags |= RADEON_FLAG_SPARSE;
675 
676    if (!si_alloc_resource(sscreen, buf)) {
677       FREE(buf);
678       return NULL;
679    }
680    return &buf->b.b;
681 }
682 
pipe_aligned_buffer_create(struct pipe_screen * screen,unsigned flags,unsigned usage,unsigned size,unsigned alignment)683 struct pipe_resource *pipe_aligned_buffer_create(struct pipe_screen *screen, unsigned flags,
684                                                  unsigned usage, unsigned size, unsigned alignment)
685 {
686    struct pipe_resource buffer;
687 
688    memset(&buffer, 0, sizeof buffer);
689    buffer.target = PIPE_BUFFER;
690    buffer.format = PIPE_FORMAT_R8_UNORM;
691    buffer.bind = 0;
692    buffer.usage = usage;
693    buffer.flags = flags;
694    buffer.width0 = size;
695    buffer.height0 = 1;
696    buffer.depth0 = 1;
697    buffer.array_size = 1;
698    return si_buffer_create(screen, &buffer, alignment);
699 }
700 
si_aligned_buffer_create(struct pipe_screen * screen,unsigned flags,unsigned usage,unsigned size,unsigned alignment)701 struct si_resource *si_aligned_buffer_create(struct pipe_screen *screen, unsigned flags,
702                                              unsigned usage, unsigned size, unsigned alignment)
703 {
704    return si_resource(pipe_aligned_buffer_create(screen, flags, usage, size, alignment));
705 }
706 
si_buffer_from_user_memory(struct pipe_screen * screen,const struct pipe_resource * templ,void * user_memory)707 static struct pipe_resource *si_buffer_from_user_memory(struct pipe_screen *screen,
708                                                         const struct pipe_resource *templ,
709                                                         void *user_memory)
710 {
711    struct si_screen *sscreen = (struct si_screen *)screen;
712    struct radeon_winsys *ws = sscreen->ws;
713    struct si_resource *buf = si_alloc_buffer_struct(screen, templ);
714 
715    buf->domains = RADEON_DOMAIN_GTT;
716    buf->flags = 0;
717    buf->b.is_user_ptr = true;
718    util_range_add(&buf->b.b, &buf->valid_buffer_range, 0, templ->width0);
719    util_range_add(&buf->b.b, &buf->b.valid_buffer_range, 0, templ->width0);
720 
721    /* Convert a user pointer to a buffer. */
722    buf->buf = ws->buffer_from_ptr(ws, user_memory, templ->width0);
723    if (!buf->buf) {
724       FREE(buf);
725       return NULL;
726    }
727 
728    buf->gpu_address = ws->buffer_get_virtual_address(buf->buf);
729    buf->vram_usage = 0;
730    buf->gart_usage = templ->width0;
731 
732    return &buf->b.b;
733 }
734 
si_buffer_from_winsys_buffer(struct pipe_screen * screen,const struct pipe_resource * templ,struct pb_buffer * imported_buf,bool dedicated)735 struct pipe_resource *si_buffer_from_winsys_buffer(struct pipe_screen *screen,
736                                                    const struct pipe_resource *templ,
737                                                    struct pb_buffer *imported_buf,
738                                                    bool dedicated)
739 {
740    struct si_screen *sscreen = (struct si_screen *)screen;
741    struct si_resource *res = si_alloc_buffer_struct(screen, templ);
742 
743    if (!res)
744       return 0;
745 
746    res->buf = imported_buf;
747    res->gpu_address = sscreen->ws->buffer_get_virtual_address(res->buf);
748    res->bo_size = imported_buf->size;
749    res->bo_alignment = imported_buf->alignment;
750    res->domains = sscreen->ws->buffer_get_initial_domain(res->buf);
751 
752    if (res->domains & RADEON_DOMAIN_VRAM)
753       res->vram_usage = res->bo_size;
754    else if (res->domains & RADEON_DOMAIN_GTT)
755       res->gart_usage = res->bo_size;
756 
757    if (sscreen->ws->buffer_get_flags)
758       res->flags = sscreen->ws->buffer_get_flags(res->buf);
759 
760    if (templ->flags & PIPE_RESOURCE_FLAG_SPARSE) {
761       res->b.b.flags |= SI_RESOURCE_FLAG_UNMAPPABLE;
762       res->flags |= RADEON_FLAG_SPARSE;
763    }
764 
765    return &res->b.b;
766 }
767 
si_resource_create(struct pipe_screen * screen,const struct pipe_resource * templ)768 static struct pipe_resource *si_resource_create(struct pipe_screen *screen,
769                                                 const struct pipe_resource *templ)
770 {
771    if (templ->target == PIPE_BUFFER) {
772       return si_buffer_create(screen, templ, 256);
773    } else {
774       return si_texture_create(screen, templ);
775    }
776 }
777 
si_resource_commit(struct pipe_context * pctx,struct pipe_resource * resource,unsigned level,struct pipe_box * box,bool commit)778 static bool si_resource_commit(struct pipe_context *pctx, struct pipe_resource *resource,
779                                unsigned level, struct pipe_box *box, bool commit)
780 {
781    struct si_context *ctx = (struct si_context *)pctx;
782    struct si_resource *res = si_resource(resource);
783 
784    /*
785     * Since buffer commitment changes cannot be pipelined, we need to
786     * (a) flush any pending commands that refer to the buffer we're about
787     *     to change, and
788     * (b) wait for threaded submit to finish, including those that were
789     *     triggered by some other, earlier operation.
790     */
791    if (radeon_emitted(ctx->gfx_cs, ctx->initial_gfx_cs_size) &&
792        ctx->ws->cs_is_buffer_referenced(ctx->gfx_cs, res->buf, RADEON_USAGE_READWRITE)) {
793       si_flush_gfx_cs(ctx, RADEON_FLUSH_ASYNC_START_NEXT_GFX_IB_NOW, NULL);
794    }
795    if (radeon_emitted(ctx->sdma_cs, 0) &&
796        ctx->ws->cs_is_buffer_referenced(ctx->sdma_cs, res->buf, RADEON_USAGE_READWRITE)) {
797       si_flush_dma_cs(ctx, PIPE_FLUSH_ASYNC, NULL);
798    }
799 
800    if (ctx->sdma_cs)
801       ctx->ws->cs_sync_flush(ctx->sdma_cs);
802    ctx->ws->cs_sync_flush(ctx->gfx_cs);
803 
804    assert(resource->target == PIPE_BUFFER);
805 
806    return ctx->ws->buffer_commit(res->buf, box->x, box->width, commit);
807 }
808 
si_init_screen_buffer_functions(struct si_screen * sscreen)809 void si_init_screen_buffer_functions(struct si_screen *sscreen)
810 {
811    sscreen->b.resource_create = si_resource_create;
812    sscreen->b.resource_destroy = u_resource_destroy_vtbl;
813    sscreen->b.resource_from_user_memory = si_buffer_from_user_memory;
814 }
815 
si_init_buffer_functions(struct si_context * sctx)816 void si_init_buffer_functions(struct si_context *sctx)
817 {
818    sctx->b.invalidate_resource = si_invalidate_resource;
819    sctx->b.transfer_map = u_transfer_map_vtbl;
820    sctx->b.transfer_flush_region = u_transfer_flush_region_vtbl;
821    sctx->b.transfer_unmap = u_transfer_unmap_vtbl;
822    sctx->b.texture_subdata = u_default_texture_subdata;
823    sctx->b.buffer_subdata = si_buffer_subdata;
824    sctx->b.resource_commit = si_resource_commit;
825 }
826