2 * Copyright 2013 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "util/u_memory.h"
29 #include "util/u_upload_mgr.h"
33 boolean
r600_rings_is_buffer_referenced(struct r600_common_context
*ctx
,
34 struct pb_buffer
*buf
,
35 enum radeon_bo_usage usage
)
37 if (ctx
->ws
->cs_is_buffer_referenced(ctx
->gfx
.cs
, buf
, usage
)) {
40 if (ctx
->dma
.cs
&& ctx
->dma
.cs
->cdw
&&
41 ctx
->ws
->cs_is_buffer_referenced(ctx
->dma
.cs
, buf
, usage
)) {
47 void *r600_buffer_map_sync_with_rings(struct r600_common_context
*ctx
,
48 struct r600_resource
*resource
,
51 enum radeon_bo_usage rusage
= RADEON_USAGE_READWRITE
;
54 if (usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) {
55 return ctx
->ws
->buffer_map(resource
->buf
, NULL
, usage
);
58 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
59 /* have to wait for the last write */
60 rusage
= RADEON_USAGE_WRITE
;
63 if (ctx
->gfx
.cs
->cdw
!= ctx
->initial_gfx_cs_size
&&
64 ctx
->ws
->cs_is_buffer_referenced(ctx
->gfx
.cs
,
65 resource
->buf
, rusage
)) {
66 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
67 ctx
->gfx
.flush(ctx
, RADEON_FLUSH_ASYNC
, NULL
);
70 ctx
->gfx
.flush(ctx
, 0, NULL
);
76 ctx
->ws
->cs_is_buffer_referenced(ctx
->dma
.cs
,
77 resource
->buf
, rusage
)) {
78 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
79 ctx
->dma
.flush(ctx
, RADEON_FLUSH_ASYNC
, NULL
);
82 ctx
->dma
.flush(ctx
, 0, NULL
);
87 if (busy
|| !ctx
->ws
->buffer_wait(resource
->buf
, 0, rusage
)) {
88 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
91 /* We will be wait for the GPU. Wait for any offloaded
92 * CS flush to complete to avoid busy-waiting in the winsys. */
93 ctx
->ws
->cs_sync_flush(ctx
->gfx
.cs
);
95 ctx
->ws
->cs_sync_flush(ctx
->dma
.cs
);
99 /* Setting the CS to NULL will prevent doing checks we have done already. */
100 return ctx
->ws
->buffer_map(resource
->buf
, NULL
, usage
);
103 bool r600_init_resource(struct r600_common_screen
*rscreen
,
104 struct r600_resource
*res
,
105 uint64_t size
, unsigned alignment
)
107 struct r600_texture
*rtex
= (struct r600_texture
*)res
;
108 struct pb_buffer
*old_buf
, *new_buf
;
109 enum radeon_bo_flag flags
= 0;
111 switch (res
->b
.b
.usage
) {
112 case PIPE_USAGE_STREAM
:
113 flags
= RADEON_FLAG_GTT_WC
;
115 case PIPE_USAGE_STAGING
:
116 /* Transfers are likely to occur more often with these resources. */
117 res
->domains
= RADEON_DOMAIN_GTT
;
119 case PIPE_USAGE_DYNAMIC
:
120 /* Older kernels didn't always flush the HDP cache before
123 if (rscreen
->info
.drm_major
== 2 &&
124 rscreen
->info
.drm_minor
< 40) {
125 res
->domains
= RADEON_DOMAIN_GTT
;
126 flags
|= RADEON_FLAG_GTT_WC
;
129 flags
|= RADEON_FLAG_CPU_ACCESS
;
131 case PIPE_USAGE_DEFAULT
:
132 case PIPE_USAGE_IMMUTABLE
:
134 /* Not listing GTT here improves performance in some apps. */
135 res
->domains
= RADEON_DOMAIN_VRAM
;
136 flags
|= RADEON_FLAG_GTT_WC
;
140 if (res
->b
.b
.target
== PIPE_BUFFER
&&
141 res
->b
.b
.flags
& (PIPE_RESOURCE_FLAG_MAP_PERSISTENT
|
142 PIPE_RESOURCE_FLAG_MAP_COHERENT
)) {
143 /* Use GTT for all persistent mappings with older kernels,
144 * because they didn't always flush the HDP cache before CS
147 * Write-combined CPU mappings are fine, the kernel ensures all CPU
148 * writes finish before the GPU executes a command stream.
150 if (rscreen
->info
.drm_major
== 2 &&
151 rscreen
->info
.drm_minor
< 40)
152 res
->domains
= RADEON_DOMAIN_GTT
;
153 else if (res
->domains
& RADEON_DOMAIN_VRAM
)
154 flags
|= RADEON_FLAG_CPU_ACCESS
;
157 /* Tiled textures are unmappable. Always put them in VRAM. */
158 if (res
->b
.b
.target
!= PIPE_BUFFER
&&
159 rtex
->surface
.level
[0].mode
>= RADEON_SURF_MODE_1D
) {
160 res
->domains
= RADEON_DOMAIN_VRAM
;
161 flags
&= ~RADEON_FLAG_CPU_ACCESS
;
162 flags
|= RADEON_FLAG_NO_CPU_ACCESS
|
166 /* If VRAM is just stolen system memory, allow both VRAM and GTT,
167 * whichever has free space. If a buffer is evicted from VRAM to GTT,
168 * it will stay there.
170 if (!rscreen
->info
.has_dedicated_vram
&&
171 res
->domains
== RADEON_DOMAIN_VRAM
)
172 res
->domains
= RADEON_DOMAIN_VRAM_GTT
;
174 if (rscreen
->debug_flags
& DBG_NO_WC
)
175 flags
&= ~RADEON_FLAG_GTT_WC
;
177 /* Allocate a new resource. */
178 new_buf
= rscreen
->ws
->buffer_create(rscreen
->ws
, size
, alignment
,
179 res
->domains
, flags
);
184 /* Replace the pointer such that if res->buf wasn't NULL, it won't be
185 * NULL. This should prevent crashes with multiple contexts using
186 * the same buffer where one of the contexts invalidates it while
187 * the others are using it. */
189 res
->buf
= new_buf
; /* should be atomic */
191 if (rscreen
->info
.has_virtual_memory
)
192 res
->gpu_address
= rscreen
->ws
->buffer_get_virtual_address(res
->buf
);
194 res
->gpu_address
= 0;
196 pb_reference(&old_buf
, NULL
);
198 util_range_set_empty(&res
->valid_buffer_range
);
199 res
->TC_L2_dirty
= false;
201 if (rscreen
->debug_flags
& DBG_VM
&& res
->b
.b
.target
== PIPE_BUFFER
) {
202 fprintf(stderr
, "VM start=0x%"PRIX64
" end=0x%"PRIX64
" | Buffer %"PRIu64
" bytes\n",
203 res
->gpu_address
, res
->gpu_address
+ res
->buf
->size
,
209 static void r600_buffer_destroy(struct pipe_screen
*screen
,
210 struct pipe_resource
*buf
)
212 struct r600_resource
*rbuffer
= r600_resource(buf
);
214 util_range_destroy(&rbuffer
->valid_buffer_range
);
215 pb_reference(&rbuffer
->buf
, NULL
);
220 r600_invalidate_buffer(struct r600_common_context
*rctx
,
221 struct r600_resource
*rbuffer
)
223 /* Shared buffers can't be reallocated. */
224 if (rbuffer
->is_shared
)
227 /* In AMD_pinned_memory, the user pointer association only gets
228 * broken when the buffer is explicitly re-allocated.
230 if (rctx
->ws
->buffer_is_user_ptr(rbuffer
->buf
))
233 /* Check if mapping this buffer would cause waiting for the GPU. */
234 if (r600_rings_is_buffer_referenced(rctx
, rbuffer
->buf
, RADEON_USAGE_READWRITE
) ||
235 !rctx
->ws
->buffer_wait(rbuffer
->buf
, 0, RADEON_USAGE_READWRITE
)) {
236 rctx
->invalidate_buffer(&rctx
->b
, &rbuffer
->b
.b
);
238 util_range_set_empty(&rbuffer
->valid_buffer_range
);
244 void r600_invalidate_resource(struct pipe_context
*ctx
,
245 struct pipe_resource
*resource
)
247 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
248 struct r600_resource
*rbuffer
= r600_resource(resource
);
250 /* We currently only do anyting here for buffers */
251 if (resource
->target
== PIPE_BUFFER
)
252 (void)r600_invalidate_buffer(rctx
, rbuffer
);
255 static void *r600_buffer_get_transfer(struct pipe_context
*ctx
,
256 struct pipe_resource
*resource
,
259 const struct pipe_box
*box
,
260 struct pipe_transfer
**ptransfer
,
261 void *data
, struct r600_resource
*staging
,
264 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
265 struct r600_transfer
*transfer
= util_slab_alloc(&rctx
->pool_transfers
);
267 transfer
->transfer
.resource
= resource
;
268 transfer
->transfer
.level
= level
;
269 transfer
->transfer
.usage
= usage
;
270 transfer
->transfer
.box
= *box
;
271 transfer
->transfer
.stride
= 0;
272 transfer
->transfer
.layer_stride
= 0;
273 transfer
->offset
= offset
;
274 transfer
->staging
= staging
;
275 *ptransfer
= &transfer
->transfer
;
279 static bool r600_can_dma_copy_buffer(struct r600_common_context
*rctx
,
280 unsigned dstx
, unsigned srcx
, unsigned size
)
282 bool dword_aligned
= !(dstx
% 4) && !(srcx
% 4) && !(size
% 4);
284 return rctx
->screen
->has_cp_dma
||
285 (dword_aligned
&& (rctx
->dma
.cs
||
286 rctx
->screen
->has_streamout
));
290 static void *r600_buffer_transfer_map(struct pipe_context
*ctx
,
291 struct pipe_resource
*resource
,
294 const struct pipe_box
*box
,
295 struct pipe_transfer
**ptransfer
)
297 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
298 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)ctx
->screen
;
299 struct r600_resource
*rbuffer
= r600_resource(resource
);
302 assert(box
->x
+ box
->width
<= resource
->width0
);
304 /* See if the buffer range being mapped has never been initialized,
305 * in which case it can be mapped unsynchronized. */
306 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
) &&
307 usage
& PIPE_TRANSFER_WRITE
&&
308 !rbuffer
->is_shared
&&
309 !util_ranges_intersect(&rbuffer
->valid_buffer_range
, box
->x
, box
->x
+ box
->width
)) {
310 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
313 /* If discarding the entire range, discard the whole resource instead. */
314 if (usage
& PIPE_TRANSFER_DISCARD_RANGE
&&
315 box
->x
== 0 && box
->width
== resource
->width0
) {
316 usage
|= PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
;
319 if (usage
& PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE
&&
320 !(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
321 assert(usage
& PIPE_TRANSFER_WRITE
);
323 if (r600_invalidate_buffer(rctx
, rbuffer
)) {
324 /* At this point, the buffer is always idle. */
325 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
327 /* Fall back to a temporary buffer. */
328 usage
|= PIPE_TRANSFER_DISCARD_RANGE
;
332 if ((usage
& PIPE_TRANSFER_DISCARD_RANGE
) &&
333 !(usage
& (PIPE_TRANSFER_UNSYNCHRONIZED
|
334 PIPE_TRANSFER_PERSISTENT
)) &&
335 !(rscreen
->debug_flags
& DBG_NO_DISCARD_RANGE
) &&
336 r600_can_dma_copy_buffer(rctx
, box
->x
, 0, box
->width
)) {
337 assert(usage
& PIPE_TRANSFER_WRITE
);
339 /* Check if mapping this buffer would cause waiting for the GPU. */
340 if (r600_rings_is_buffer_referenced(rctx
, rbuffer
->buf
, RADEON_USAGE_READWRITE
) ||
341 !rctx
->ws
->buffer_wait(rbuffer
->buf
, 0, RADEON_USAGE_READWRITE
)) {
342 /* Do a wait-free write-only transfer using a temporary buffer. */
344 struct r600_resource
*staging
= NULL
;
346 u_upload_alloc(rctx
->uploader
, 0, box
->width
+ (box
->x
% R600_MAP_BUFFER_ALIGNMENT
),
347 256, &offset
, (struct pipe_resource
**)&staging
, (void**)&data
);
350 data
+= box
->x
% R600_MAP_BUFFER_ALIGNMENT
;
351 return r600_buffer_get_transfer(ctx
, resource
, level
, usage
, box
,
352 ptransfer
, data
, staging
, offset
);
355 /* At this point, the buffer is always idle (we checked it above). */
356 usage
|= PIPE_TRANSFER_UNSYNCHRONIZED
;
359 /* Using a staging buffer in GTT for larger reads is much faster. */
360 else if ((usage
& PIPE_TRANSFER_READ
) &&
361 !(usage
& (PIPE_TRANSFER_WRITE
|
362 PIPE_TRANSFER_PERSISTENT
)) &&
363 rbuffer
->domains
== RADEON_DOMAIN_VRAM
&&
364 r600_can_dma_copy_buffer(rctx
, 0, box
->x
, box
->width
)) {
365 struct r600_resource
*staging
;
367 staging
= (struct r600_resource
*) pipe_buffer_create(
368 ctx
->screen
, PIPE_BIND_TRANSFER_READ
, PIPE_USAGE_STAGING
,
369 box
->width
+ (box
->x
% R600_MAP_BUFFER_ALIGNMENT
));
371 /* Copy the VRAM buffer to the staging buffer. */
372 rctx
->dma_copy(ctx
, &staging
->b
.b
, 0,
373 box
->x
% R600_MAP_BUFFER_ALIGNMENT
,
374 0, 0, resource
, level
, box
);
376 data
= r600_buffer_map_sync_with_rings(rctx
, staging
, PIPE_TRANSFER_READ
);
378 pipe_resource_reference((struct pipe_resource
**)&staging
, NULL
);
381 data
+= box
->x
% R600_MAP_BUFFER_ALIGNMENT
;
383 return r600_buffer_get_transfer(ctx
, resource
, level
, usage
, box
,
384 ptransfer
, data
, staging
, 0);
388 data
= r600_buffer_map_sync_with_rings(rctx
, rbuffer
, usage
);
394 return r600_buffer_get_transfer(ctx
, resource
, level
, usage
, box
,
395 ptransfer
, data
, NULL
, 0);
398 static void r600_buffer_do_flush_region(struct pipe_context
*ctx
,
399 struct pipe_transfer
*transfer
,
400 const struct pipe_box
*box
)
402 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
403 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
404 struct r600_resource
*rbuffer
= r600_resource(transfer
->resource
);
406 if (rtransfer
->staging
) {
407 struct pipe_resource
*dst
, *src
;
409 struct pipe_box dma_box
;
411 dst
= transfer
->resource
;
412 src
= &rtransfer
->staging
->b
.b
;
413 soffset
= rtransfer
->offset
+ box
->x
% R600_MAP_BUFFER_ALIGNMENT
;
415 u_box_1d(soffset
, box
->width
, &dma_box
);
417 /* Copy the staging buffer into the original one. */
418 rctx
->dma_copy(ctx
, dst
, 0, box
->x
, 0, 0, src
, 0, &dma_box
);
421 util_range_add(&rbuffer
->valid_buffer_range
, box
->x
,
422 box
->x
+ box
->width
);
425 static void r600_buffer_flush_region(struct pipe_context
*ctx
,
426 struct pipe_transfer
*transfer
,
427 const struct pipe_box
*rel_box
)
429 if (transfer
->usage
& (PIPE_TRANSFER_WRITE
|
430 PIPE_TRANSFER_FLUSH_EXPLICIT
)) {
433 u_box_1d(transfer
->box
.x
+ rel_box
->x
, rel_box
->width
, &box
);
434 r600_buffer_do_flush_region(ctx
, transfer
, &box
);
438 static void r600_buffer_transfer_unmap(struct pipe_context
*ctx
,
439 struct pipe_transfer
*transfer
)
441 struct r600_common_context
*rctx
= (struct r600_common_context
*)ctx
;
442 struct r600_transfer
*rtransfer
= (struct r600_transfer
*)transfer
;
444 if (transfer
->usage
& PIPE_TRANSFER_WRITE
&&
445 !(transfer
->usage
& PIPE_TRANSFER_FLUSH_EXPLICIT
))
446 r600_buffer_do_flush_region(ctx
, transfer
, &transfer
->box
);
448 if (rtransfer
->staging
)
449 pipe_resource_reference((struct pipe_resource
**)&rtransfer
->staging
, NULL
);
451 util_slab_free(&rctx
->pool_transfers
, transfer
);
454 static const struct u_resource_vtbl r600_buffer_vtbl
=
456 NULL
, /* get_handle */
457 r600_buffer_destroy
, /* resource_destroy */
458 r600_buffer_transfer_map
, /* transfer_map */
459 r600_buffer_flush_region
, /* transfer_flush_region */
460 r600_buffer_transfer_unmap
, /* transfer_unmap */
461 NULL
/* transfer_inline_write */
464 static struct r600_resource
*
465 r600_alloc_buffer_struct(struct pipe_screen
*screen
,
466 const struct pipe_resource
*templ
)
468 struct r600_resource
*rbuffer
;
470 rbuffer
= MALLOC_STRUCT(r600_resource
);
472 rbuffer
->b
.b
= *templ
;
473 pipe_reference_init(&rbuffer
->b
.b
.reference
, 1);
474 rbuffer
->b
.b
.screen
= screen
;
475 rbuffer
->b
.vtbl
= &r600_buffer_vtbl
;
477 rbuffer
->TC_L2_dirty
= false;
478 rbuffer
->is_shared
= false;
479 util_range_init(&rbuffer
->valid_buffer_range
);
483 struct pipe_resource
*r600_buffer_create(struct pipe_screen
*screen
,
484 const struct pipe_resource
*templ
,
487 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
488 struct r600_resource
*rbuffer
= r600_alloc_buffer_struct(screen
, templ
);
490 if (!r600_init_resource(rscreen
, rbuffer
, templ
->width0
, alignment
)) {
494 return &rbuffer
->b
.b
;
497 struct pipe_resource
*r600_aligned_buffer_create(struct pipe_screen
*screen
,
503 struct pipe_resource buffer
;
505 memset(&buffer
, 0, sizeof buffer
);
506 buffer
.target
= PIPE_BUFFER
;
507 buffer
.format
= PIPE_FORMAT_R8_UNORM
;
509 buffer
.usage
= usage
;
511 buffer
.width0
= size
;
514 buffer
.array_size
= 1;
515 return r600_buffer_create(screen
, &buffer
, alignment
);
518 struct pipe_resource
*
519 r600_buffer_from_user_memory(struct pipe_screen
*screen
,
520 const struct pipe_resource
*templ
,
523 struct r600_common_screen
*rscreen
= (struct r600_common_screen
*)screen
;
524 struct radeon_winsys
*ws
= rscreen
->ws
;
525 struct r600_resource
*rbuffer
= r600_alloc_buffer_struct(screen
, templ
);
527 rbuffer
->domains
= RADEON_DOMAIN_GTT
;
528 util_range_add(&rbuffer
->valid_buffer_range
, 0, templ
->width0
);
530 /* Convert a user pointer to a buffer. */
531 rbuffer
->buf
= ws
->buffer_from_ptr(ws
, user_memory
, templ
->width0
);
537 if (rscreen
->info
.has_virtual_memory
)
538 rbuffer
->gpu_address
=
539 ws
->buffer_get_virtual_address(rbuffer
->buf
);
541 rbuffer
->gpu_address
= 0;
543 return &rbuffer
->b
.b
;