2 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
3 * Copyright © 2015 Advanced Micro Devices, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
29 * Marek Olšák <maraeo@gmail.com>
32 #include "amdgpu_cs.h"
34 #include "os/os_time.h"
35 #include "state_tracker/drm_driver.h"
36 #include <amdgpu_drm.h>
41 static struct pb_buffer
*
42 amdgpu_bo_create(struct radeon_winsys
*rws
,
45 enum radeon_bo_domain domain
,
46 enum radeon_bo_flag flags
);
48 static bool amdgpu_bo_wait(struct pb_buffer
*_buf
, uint64_t timeout
,
49 enum radeon_bo_usage usage
)
51 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
52 struct amdgpu_winsys
*ws
= bo
->ws
;
56 if (p_atomic_read(&bo
->num_active_ioctls
))
60 abs_timeout
= os_time_get_absolute_timeout(timeout
);
62 /* Wait if any ioctl is being submitted with this buffer. */
63 if (!os_wait_until_zero_abs_timeout(&bo
->num_active_ioctls
, abs_timeout
))
68 /* We can't use user fences for shared buffers, because user fences
69 * are local to this process only. If we want to wait for all buffer
70 * uses in all processes, we have to use amdgpu_bo_wait_for_idle.
72 bool buffer_busy
= true;
75 r
= amdgpu_bo_wait_for_idle(bo
->bo
, timeout
, &buffer_busy
);
77 fprintf(stderr
, "%s: amdgpu_bo_wait_for_idle failed %i\n", __func__
,
86 pipe_mutex_lock(ws
->bo_fence_lock
);
88 for (idle_fences
= 0; idle_fences
< bo
->num_fences
; ++idle_fences
) {
89 if (!amdgpu_fence_wait(bo
->fences
[idle_fences
], 0, false))
93 /* Release the idle fences to avoid checking them again later. */
94 for (unsigned i
= 0; i
< idle_fences
; ++i
)
95 amdgpu_fence_reference(&bo
->fences
[i
], NULL
);
97 memmove(&bo
->fences
[0], &bo
->fences
[idle_fences
],
98 (bo
->num_fences
- idle_fences
) * sizeof(*bo
->fences
));
99 bo
->num_fences
-= idle_fences
;
101 buffer_idle
= !bo
->num_fences
;
102 pipe_mutex_unlock(ws
->bo_fence_lock
);
106 bool buffer_idle
= true;
108 pipe_mutex_lock(ws
->bo_fence_lock
);
109 while (bo
->num_fences
&& buffer_idle
) {
110 struct pipe_fence_handle
*fence
= NULL
;
111 bool fence_idle
= false;
113 amdgpu_fence_reference(&fence
, bo
->fences
[0]);
115 /* Wait for the fence. */
116 pipe_mutex_unlock(ws
->bo_fence_lock
);
117 if (amdgpu_fence_wait(fence
, abs_timeout
, true))
121 pipe_mutex_lock(ws
->bo_fence_lock
);
123 /* Release an idle fence to avoid checking it again later, keeping in
124 * mind that the fence array may have been modified by other threads.
126 if (fence_idle
&& bo
->num_fences
&& bo
->fences
[0] == fence
) {
127 amdgpu_fence_reference(&bo
->fences
[0], NULL
);
128 memmove(&bo
->fences
[0], &bo
->fences
[1],
129 (bo
->num_fences
- 1) * sizeof(*bo
->fences
));
133 amdgpu_fence_reference(&fence
, NULL
);
135 pipe_mutex_unlock(ws
->bo_fence_lock
);
141 static enum radeon_bo_domain
amdgpu_bo_get_initial_domain(
142 struct pb_buffer
*buf
)
144 return ((struct amdgpu_winsys_bo
*)buf
)->initial_domain
;
147 static void amdgpu_bo_remove_fences(struct amdgpu_winsys_bo
*bo
)
149 for (unsigned i
= 0; i
< bo
->num_fences
; ++i
)
150 amdgpu_fence_reference(&bo
->fences
[i
], NULL
);
157 void amdgpu_bo_destroy(struct pb_buffer
*_buf
)
159 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
161 assert(bo
->bo
&& "must not be called for slab entries");
163 pipe_mutex_lock(bo
->ws
->global_bo_list_lock
);
164 LIST_DEL(&bo
->u
.real
.global_list_item
);
165 bo
->ws
->num_buffers
--;
166 pipe_mutex_unlock(bo
->ws
->global_bo_list_lock
);
168 amdgpu_bo_va_op(bo
->bo
, 0, bo
->base
.size
, bo
->va
, 0, AMDGPU_VA_OP_UNMAP
);
169 amdgpu_va_range_free(bo
->u
.real
.va_handle
);
170 amdgpu_bo_free(bo
->bo
);
172 amdgpu_bo_remove_fences(bo
);
174 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
175 bo
->ws
->allocated_vram
-= align64(bo
->base
.size
, bo
->ws
->info
.gart_page_size
);
176 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
177 bo
->ws
->allocated_gtt
-= align64(bo
->base
.size
, bo
->ws
->info
.gart_page_size
);
179 if (bo
->u
.real
.map_count
>= 1) {
180 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
181 bo
->ws
->mapped_vram
-= bo
->base
.size
;
182 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
183 bo
->ws
->mapped_gtt
-= bo
->base
.size
;
189 static void amdgpu_bo_destroy_or_cache(struct pb_buffer
*_buf
)
191 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
193 assert(bo
->bo
); /* slab buffers have a separate vtbl */
195 if (bo
->u
.real
.use_reusable_pool
)
196 pb_cache_add_buffer(&bo
->u
.real
.cache_entry
);
198 amdgpu_bo_destroy(_buf
);
201 static void *amdgpu_bo_map(struct pb_buffer
*buf
,
202 struct radeon_winsys_cs
*rcs
,
203 enum pipe_transfer_usage usage
)
205 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)buf
;
206 struct amdgpu_winsys_bo
*real
;
207 struct amdgpu_cs
*cs
= (struct amdgpu_cs
*)rcs
;
212 /* If it's not unsynchronized bo_map, flush CS if needed and then wait. */
213 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
214 /* DONTBLOCK doesn't make sense with UNSYNCHRONIZED. */
215 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
216 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
219 * Since we are mapping for read, we don't need to wait
220 * if the GPU is using the buffer for read too
221 * (neither one is changing it).
223 * Only check whether the buffer is being used for write. */
224 if (cs
&& amdgpu_bo_is_referenced_by_cs_with_usage(cs
, bo
,
225 RADEON_USAGE_WRITE
)) {
226 cs
->flush_cs(cs
->flush_data
, RADEON_FLUSH_ASYNC
, NULL
);
230 if (!amdgpu_bo_wait((struct pb_buffer
*)bo
, 0,
231 RADEON_USAGE_WRITE
)) {
235 if (cs
&& amdgpu_bo_is_referenced_by_cs(cs
, bo
)) {
236 cs
->flush_cs(cs
->flush_data
, RADEON_FLUSH_ASYNC
, NULL
);
240 if (!amdgpu_bo_wait((struct pb_buffer
*)bo
, 0,
241 RADEON_USAGE_READWRITE
)) {
246 uint64_t time
= os_time_get_nano();
248 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
251 * Since we are mapping for read, we don't need to wait
252 * if the GPU is using the buffer for read too
253 * (neither one is changing it).
255 * Only check whether the buffer is being used for write. */
256 if (cs
&& amdgpu_bo_is_referenced_by_cs_with_usage(cs
, bo
,
257 RADEON_USAGE_WRITE
)) {
258 cs
->flush_cs(cs
->flush_data
, 0, NULL
);
260 /* Try to avoid busy-waiting in amdgpu_bo_wait. */
261 if (p_atomic_read(&bo
->num_active_ioctls
))
262 amdgpu_cs_sync_flush(rcs
);
264 amdgpu_bo_wait((struct pb_buffer
*)bo
, PIPE_TIMEOUT_INFINITE
,
267 /* Mapping for write. */
269 if (amdgpu_bo_is_referenced_by_cs(cs
, bo
)) {
270 cs
->flush_cs(cs
->flush_data
, 0, NULL
);
272 /* Try to avoid busy-waiting in amdgpu_bo_wait. */
273 if (p_atomic_read(&bo
->num_active_ioctls
))
274 amdgpu_cs_sync_flush(rcs
);
278 amdgpu_bo_wait((struct pb_buffer
*)bo
, PIPE_TIMEOUT_INFINITE
,
279 RADEON_USAGE_READWRITE
);
282 bo
->ws
->buffer_wait_time
+= os_time_get_nano() - time
;
286 /* If the buffer is created from user memory, return the user pointer. */
293 real
= bo
->u
.slab
.real
;
294 offset
= bo
->va
- real
->va
;
297 r
= amdgpu_bo_cpu_map(real
->bo
, &cpu
);
299 /* Clear the cache and try again. */
300 pb_cache_release_all_buffers(&real
->ws
->bo_cache
);
301 r
= amdgpu_bo_cpu_map(real
->bo
, &cpu
);
306 if (p_atomic_inc_return(&real
->u
.real
.map_count
) == 1) {
307 if (real
->initial_domain
& RADEON_DOMAIN_VRAM
)
308 real
->ws
->mapped_vram
+= real
->base
.size
;
309 else if (real
->initial_domain
& RADEON_DOMAIN_GTT
)
310 real
->ws
->mapped_gtt
+= real
->base
.size
;
312 return (uint8_t*)cpu
+ offset
;
315 static void amdgpu_bo_unmap(struct pb_buffer
*buf
)
317 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)buf
;
318 struct amdgpu_winsys_bo
*real
;
323 real
= bo
->bo
? bo
: bo
->u
.slab
.real
;
325 if (p_atomic_dec_zero(&real
->u
.real
.map_count
)) {
326 if (real
->initial_domain
& RADEON_DOMAIN_VRAM
)
327 real
->ws
->mapped_vram
-= real
->base
.size
;
328 else if (real
->initial_domain
& RADEON_DOMAIN_GTT
)
329 real
->ws
->mapped_gtt
-= real
->base
.size
;
332 amdgpu_bo_cpu_unmap(real
->bo
);
335 static const struct pb_vtbl amdgpu_winsys_bo_vtbl
= {
336 amdgpu_bo_destroy_or_cache
337 /* other functions are never called */
340 static void amdgpu_add_buffer_to_global_list(struct amdgpu_winsys_bo
*bo
)
342 struct amdgpu_winsys
*ws
= bo
->ws
;
346 pipe_mutex_lock(ws
->global_bo_list_lock
);
347 LIST_ADDTAIL(&bo
->u
.real
.global_list_item
, &ws
->global_bo_list
);
349 pipe_mutex_unlock(ws
->global_bo_list_lock
);
352 static struct amdgpu_winsys_bo
*amdgpu_create_bo(struct amdgpu_winsys
*ws
,
356 enum radeon_bo_domain initial_domain
,
358 unsigned pb_cache_bucket
)
360 struct amdgpu_bo_alloc_request request
= {0};
361 amdgpu_bo_handle buf_handle
;
363 struct amdgpu_winsys_bo
*bo
;
364 amdgpu_va_handle va_handle
;
365 unsigned va_gap_size
;
368 assert(initial_domain
& RADEON_DOMAIN_VRAM_GTT
);
369 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
374 pb_cache_init_entry(&ws
->bo_cache
, &bo
->u
.real
.cache_entry
, &bo
->base
,
376 request
.alloc_size
= size
;
377 request
.phys_alignment
= alignment
;
379 if (initial_domain
& RADEON_DOMAIN_VRAM
)
380 request
.preferred_heap
|= AMDGPU_GEM_DOMAIN_VRAM
;
381 if (initial_domain
& RADEON_DOMAIN_GTT
)
382 request
.preferred_heap
|= AMDGPU_GEM_DOMAIN_GTT
;
384 if (flags
& RADEON_FLAG_CPU_ACCESS
)
385 request
.flags
|= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
;
386 if (flags
& RADEON_FLAG_NO_CPU_ACCESS
)
387 request
.flags
|= AMDGPU_GEM_CREATE_NO_CPU_ACCESS
;
388 if (flags
& RADEON_FLAG_GTT_WC
)
389 request
.flags
|= AMDGPU_GEM_CREATE_CPU_GTT_USWC
;
391 r
= amdgpu_bo_alloc(ws
->dev
, &request
, &buf_handle
);
393 fprintf(stderr
, "amdgpu: Failed to allocate a buffer:\n");
394 fprintf(stderr
, "amdgpu: size : %"PRIu64
" bytes\n", size
);
395 fprintf(stderr
, "amdgpu: alignment : %u bytes\n", alignment
);
396 fprintf(stderr
, "amdgpu: domains : %u\n", initial_domain
);
400 va_gap_size
= ws
->check_vm
? MAX2(4 * alignment
, 64 * 1024) : 0;
401 r
= amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
402 size
+ va_gap_size
, alignment
, 0, &va
, &va_handle
, 0);
406 r
= amdgpu_bo_va_op(buf_handle
, 0, size
, va
, 0, AMDGPU_VA_OP_MAP
);
410 pipe_reference_init(&bo
->base
.reference
, 1);
411 bo
->base
.alignment
= alignment
;
412 bo
->base
.usage
= usage
;
413 bo
->base
.size
= size
;
414 bo
->base
.vtbl
= &amdgpu_winsys_bo_vtbl
;
418 bo
->u
.real
.va_handle
= va_handle
;
419 bo
->initial_domain
= initial_domain
;
420 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
422 if (initial_domain
& RADEON_DOMAIN_VRAM
)
423 ws
->allocated_vram
+= align64(size
, ws
->info
.gart_page_size
);
424 else if (initial_domain
& RADEON_DOMAIN_GTT
)
425 ws
->allocated_gtt
+= align64(size
, ws
->info
.gart_page_size
);
427 amdgpu_add_buffer_to_global_list(bo
);
432 amdgpu_va_range_free(va_handle
);
435 amdgpu_bo_free(buf_handle
);
442 bool amdgpu_bo_can_reclaim(struct pb_buffer
*_buf
)
444 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
446 if (amdgpu_bo_is_referenced_by_any_cs(bo
)) {
450 return amdgpu_bo_wait(_buf
, 0, RADEON_USAGE_READWRITE
);
453 bool amdgpu_bo_can_reclaim_slab(void *priv
, struct pb_slab_entry
*entry
)
455 struct amdgpu_winsys_bo
*bo
= NULL
; /* fix container_of */
456 bo
= container_of(entry
, bo
, u
.slab
.entry
);
458 return amdgpu_bo_can_reclaim(&bo
->base
);
461 static void amdgpu_bo_slab_destroy(struct pb_buffer
*_buf
)
463 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
467 pb_slab_free(&bo
->ws
->bo_slabs
, &bo
->u
.slab
.entry
);
470 static const struct pb_vtbl amdgpu_winsys_bo_slab_vtbl
= {
471 amdgpu_bo_slab_destroy
472 /* other functions are never called */
475 struct pb_slab
*amdgpu_bo_slab_alloc(void *priv
, unsigned heap
,
477 unsigned group_index
)
479 struct amdgpu_winsys
*ws
= priv
;
480 struct amdgpu_slab
*slab
= CALLOC_STRUCT(amdgpu_slab
);
481 enum radeon_bo_domain domains
;
482 enum radeon_bo_flag flags
= 0;
489 flags
|= RADEON_FLAG_GTT_WC
;
491 flags
|= RADEON_FLAG_CPU_ACCESS
;
495 domains
= RADEON_DOMAIN_VRAM
;
499 domains
= RADEON_DOMAIN_VRAM_GTT
;
502 domains
= RADEON_DOMAIN_GTT
;
506 slab
->buffer
= amdgpu_winsys_bo(amdgpu_bo_create(&ws
->base
,
507 64 * 1024, 64 * 1024,
512 assert(slab
->buffer
->bo
);
514 slab
->base
.num_entries
= slab
->buffer
->base
.size
/ entry_size
;
515 slab
->base
.num_free
= slab
->base
.num_entries
;
516 slab
->entries
= CALLOC(slab
->base
.num_entries
, sizeof(*slab
->entries
));
520 LIST_INITHEAD(&slab
->base
.free
);
522 base_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, slab
->base
.num_entries
);
524 for (unsigned i
= 0; i
< slab
->base
.num_entries
; ++i
) {
525 struct amdgpu_winsys_bo
*bo
= &slab
->entries
[i
];
527 bo
->base
.alignment
= entry_size
;
528 bo
->base
.usage
= slab
->buffer
->base
.usage
;
529 bo
->base
.size
= entry_size
;
530 bo
->base
.vtbl
= &amdgpu_winsys_bo_slab_vtbl
;
532 bo
->va
= slab
->buffer
->va
+ i
* entry_size
;
533 bo
->initial_domain
= domains
;
534 bo
->unique_id
= base_id
+ i
;
535 bo
->u
.slab
.entry
.slab
= &slab
->base
;
536 bo
->u
.slab
.entry
.group_index
= group_index
;
537 bo
->u
.slab
.real
= slab
->buffer
;
539 LIST_ADDTAIL(&bo
->u
.slab
.entry
.head
, &slab
->base
.free
);
545 amdgpu_winsys_bo_reference(&slab
->buffer
, NULL
);
551 void amdgpu_bo_slab_free(void *priv
, struct pb_slab
*pslab
)
553 struct amdgpu_slab
*slab
= amdgpu_slab(pslab
);
555 for (unsigned i
= 0; i
< slab
->base
.num_entries
; ++i
)
556 amdgpu_bo_remove_fences(&slab
->entries
[i
]);
559 amdgpu_winsys_bo_reference(&slab
->buffer
, NULL
);
563 static unsigned eg_tile_split(unsigned tile_split
)
565 switch (tile_split
) {
566 case 0: tile_split
= 64; break;
567 case 1: tile_split
= 128; break;
568 case 2: tile_split
= 256; break;
569 case 3: tile_split
= 512; break;
571 case 4: tile_split
= 1024; break;
572 case 5: tile_split
= 2048; break;
573 case 6: tile_split
= 4096; break;
578 static unsigned eg_tile_split_rev(unsigned eg_tile_split
)
580 switch (eg_tile_split
) {
592 static void amdgpu_buffer_get_metadata(struct pb_buffer
*_buf
,
593 struct radeon_bo_metadata
*md
)
595 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
596 struct amdgpu_bo_info info
= {0};
597 uint32_t tiling_flags
;
600 assert(bo
->bo
&& "must not be called for slab entries");
602 r
= amdgpu_bo_query_info(bo
->bo
, &info
);
606 tiling_flags
= info
.metadata
.tiling_info
;
608 md
->microtile
= RADEON_LAYOUT_LINEAR
;
609 md
->macrotile
= RADEON_LAYOUT_LINEAR
;
611 if (AMDGPU_TILING_GET(tiling_flags
, ARRAY_MODE
) == 4) /* 2D_TILED_THIN1 */
612 md
->macrotile
= RADEON_LAYOUT_TILED
;
613 else if (AMDGPU_TILING_GET(tiling_flags
, ARRAY_MODE
) == 2) /* 1D_TILED_THIN1 */
614 md
->microtile
= RADEON_LAYOUT_TILED
;
616 md
->pipe_config
= AMDGPU_TILING_GET(tiling_flags
, PIPE_CONFIG
);
617 md
->bankw
= 1 << AMDGPU_TILING_GET(tiling_flags
, BANK_WIDTH
);
618 md
->bankh
= 1 << AMDGPU_TILING_GET(tiling_flags
, BANK_HEIGHT
);
619 md
->tile_split
= eg_tile_split(AMDGPU_TILING_GET(tiling_flags
, TILE_SPLIT
));
620 md
->mtilea
= 1 << AMDGPU_TILING_GET(tiling_flags
, MACRO_TILE_ASPECT
);
621 md
->num_banks
= 2 << AMDGPU_TILING_GET(tiling_flags
, NUM_BANKS
);
622 md
->scanout
= AMDGPU_TILING_GET(tiling_flags
, MICRO_TILE_MODE
) == 0; /* DISPLAY */
624 md
->size_metadata
= info
.metadata
.size_metadata
;
625 memcpy(md
->metadata
, info
.metadata
.umd_metadata
, sizeof(md
->metadata
));
628 static void amdgpu_buffer_set_metadata(struct pb_buffer
*_buf
,
629 struct radeon_bo_metadata
*md
)
631 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
632 struct amdgpu_bo_metadata metadata
= {0};
633 uint32_t tiling_flags
= 0;
635 assert(bo
->bo
&& "must not be called for slab entries");
637 if (md
->macrotile
== RADEON_LAYOUT_TILED
)
638 tiling_flags
|= AMDGPU_TILING_SET(ARRAY_MODE
, 4); /* 2D_TILED_THIN1 */
639 else if (md
->microtile
== RADEON_LAYOUT_TILED
)
640 tiling_flags
|= AMDGPU_TILING_SET(ARRAY_MODE
, 2); /* 1D_TILED_THIN1 */
642 tiling_flags
|= AMDGPU_TILING_SET(ARRAY_MODE
, 1); /* LINEAR_ALIGNED */
644 tiling_flags
|= AMDGPU_TILING_SET(PIPE_CONFIG
, md
->pipe_config
);
645 tiling_flags
|= AMDGPU_TILING_SET(BANK_WIDTH
, util_logbase2(md
->bankw
));
646 tiling_flags
|= AMDGPU_TILING_SET(BANK_HEIGHT
, util_logbase2(md
->bankh
));
648 tiling_flags
|= AMDGPU_TILING_SET(TILE_SPLIT
, eg_tile_split_rev(md
->tile_split
));
649 tiling_flags
|= AMDGPU_TILING_SET(MACRO_TILE_ASPECT
, util_logbase2(md
->mtilea
));
650 tiling_flags
|= AMDGPU_TILING_SET(NUM_BANKS
, util_logbase2(md
->num_banks
)-1);
653 tiling_flags
|= AMDGPU_TILING_SET(MICRO_TILE_MODE
, 0); /* DISPLAY_MICRO_TILING */
655 tiling_flags
|= AMDGPU_TILING_SET(MICRO_TILE_MODE
, 1); /* THIN_MICRO_TILING */
657 metadata
.tiling_info
= tiling_flags
;
658 metadata
.size_metadata
= md
->size_metadata
;
659 memcpy(metadata
.umd_metadata
, md
->metadata
, sizeof(md
->metadata
));
661 amdgpu_bo_set_metadata(bo
->bo
, &metadata
);
664 static struct pb_buffer
*
665 amdgpu_bo_create(struct radeon_winsys
*rws
,
668 enum radeon_bo_domain domain
,
669 enum radeon_bo_flag flags
)
671 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
672 struct amdgpu_winsys_bo
*bo
;
673 unsigned usage
= 0, pb_cache_bucket
;
675 /* Sub-allocate small buffers from slabs. */
676 if (!(flags
& RADEON_FLAG_HANDLE
) &&
677 size
<= (1 << AMDGPU_SLAB_MAX_SIZE_LOG2
) &&
678 alignment
<= MAX2(1 << AMDGPU_SLAB_MIN_SIZE_LOG2
, util_next_power_of_two(size
))) {
679 struct pb_slab_entry
*entry
;
682 if (flags
& RADEON_FLAG_GTT_WC
)
684 if (flags
& RADEON_FLAG_CPU_ACCESS
)
686 if (flags
& ~(RADEON_FLAG_GTT_WC
| RADEON_FLAG_CPU_ACCESS
))
690 case RADEON_DOMAIN_VRAM
:
693 case RADEON_DOMAIN_VRAM_GTT
:
696 case RADEON_DOMAIN_GTT
:
703 entry
= pb_slab_alloc(&ws
->bo_slabs
, size
, heap
);
705 /* Clear the cache and try again. */
706 pb_cache_release_all_buffers(&ws
->bo_cache
);
708 entry
= pb_slab_alloc(&ws
->bo_slabs
, size
, heap
);
714 bo
= container_of(entry
, bo
, u
.slab
.entry
);
716 pipe_reference_init(&bo
->base
.reference
, 1);
722 /* This flag is irrelevant for the cache. */
723 flags
&= ~RADEON_FLAG_HANDLE
;
725 /* Align size to page size. This is the minimum alignment for normal
726 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
727 * like constant/uniform buffers, can benefit from better and more reuse.
729 size
= align64(size
, ws
->info
.gart_page_size
);
730 alignment
= align(alignment
, ws
->info
.gart_page_size
);
732 /* Only set one usage bit each for domains and flags, or the cache manager
733 * might consider different sets of domains / flags compatible
735 if (domain
== RADEON_DOMAIN_VRAM_GTT
)
739 assert(flags
< sizeof(usage
) * 8 - 3);
740 usage
|= 1 << (flags
+ 3);
742 /* Determine the pb_cache bucket for minimizing pb_cache misses. */
744 if (domain
& RADEON_DOMAIN_VRAM
) /* VRAM or VRAM+GTT */
745 pb_cache_bucket
+= 1;
746 if (flags
== RADEON_FLAG_GTT_WC
) /* WC */
747 pb_cache_bucket
+= 2;
748 assert(pb_cache_bucket
< ARRAY_SIZE(ws
->bo_cache
.buckets
));
750 /* Get a buffer from the cache. */
751 bo
= (struct amdgpu_winsys_bo
*)
752 pb_cache_reclaim_buffer(&ws
->bo_cache
, size
, alignment
, usage
,
757 /* Create a new one. */
758 bo
= amdgpu_create_bo(ws
, size
, alignment
, usage
, domain
, flags
,
761 /* Clear the cache and try again. */
762 pb_slabs_reclaim(&ws
->bo_slabs
);
763 pb_cache_release_all_buffers(&ws
->bo_cache
);
764 bo
= amdgpu_create_bo(ws
, size
, alignment
, usage
, domain
, flags
,
770 bo
->u
.real
.use_reusable_pool
= true;
774 static struct pb_buffer
*amdgpu_bo_from_handle(struct radeon_winsys
*rws
,
775 struct winsys_handle
*whandle
,
779 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
780 struct amdgpu_winsys_bo
*bo
;
781 enum amdgpu_bo_handle_type type
;
782 struct amdgpu_bo_import_result result
= {0};
784 amdgpu_va_handle va_handle
;
785 struct amdgpu_bo_info info
= {0};
786 enum radeon_bo_domain initial
= 0;
789 /* Initialize the structure. */
790 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
795 switch (whandle
->type
) {
796 case DRM_API_HANDLE_TYPE_SHARED
:
797 type
= amdgpu_bo_handle_type_gem_flink_name
;
799 case DRM_API_HANDLE_TYPE_FD
:
800 type
= amdgpu_bo_handle_type_dma_buf_fd
;
806 r
= amdgpu_bo_import(ws
->dev
, type
, whandle
->handle
, &result
);
810 /* Get initial domains. */
811 r
= amdgpu_bo_query_info(result
.buf_handle
, &info
);
815 r
= amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
816 result
.alloc_size
, 1 << 20, 0, &va
, &va_handle
, 0);
820 r
= amdgpu_bo_va_op(result
.buf_handle
, 0, result
.alloc_size
, va
, 0, AMDGPU_VA_OP_MAP
);
824 if (info
.preferred_heap
& AMDGPU_GEM_DOMAIN_VRAM
)
825 initial
|= RADEON_DOMAIN_VRAM
;
826 if (info
.preferred_heap
& AMDGPU_GEM_DOMAIN_GTT
)
827 initial
|= RADEON_DOMAIN_GTT
;
830 pipe_reference_init(&bo
->base
.reference
, 1);
831 bo
->base
.alignment
= info
.phys_alignment
;
832 bo
->bo
= result
.buf_handle
;
833 bo
->base
.size
= result
.alloc_size
;
834 bo
->base
.vtbl
= &amdgpu_winsys_bo_vtbl
;
837 bo
->u
.real
.va_handle
= va_handle
;
838 bo
->initial_domain
= initial
;
839 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
840 bo
->is_shared
= true;
843 *stride
= whandle
->stride
;
845 *offset
= whandle
->offset
;
847 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
848 ws
->allocated_vram
+= align64(bo
->base
.size
, ws
->info
.gart_page_size
);
849 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
850 ws
->allocated_gtt
+= align64(bo
->base
.size
, ws
->info
.gart_page_size
);
852 amdgpu_add_buffer_to_global_list(bo
);
857 amdgpu_va_range_free(va_handle
);
860 amdgpu_bo_free(result
.buf_handle
);
867 static bool amdgpu_bo_get_handle(struct pb_buffer
*buffer
,
868 unsigned stride
, unsigned offset
,
870 struct winsys_handle
*whandle
)
872 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(buffer
);
873 enum amdgpu_bo_handle_type type
;
877 offset
+= bo
->va
- bo
->u
.slab
.real
->va
;
878 bo
= bo
->u
.slab
.real
;
881 bo
->u
.real
.use_reusable_pool
= false;
883 switch (whandle
->type
) {
884 case DRM_API_HANDLE_TYPE_SHARED
:
885 type
= amdgpu_bo_handle_type_gem_flink_name
;
887 case DRM_API_HANDLE_TYPE_FD
:
888 type
= amdgpu_bo_handle_type_dma_buf_fd
;
890 case DRM_API_HANDLE_TYPE_KMS
:
891 type
= amdgpu_bo_handle_type_kms
;
897 r
= amdgpu_bo_export(bo
->bo
, type
, &whandle
->handle
);
901 whandle
->stride
= stride
;
902 whandle
->offset
= offset
;
903 whandle
->offset
+= slice_size
* whandle
->layer
;
904 bo
->is_shared
= true;
908 static struct pb_buffer
*amdgpu_bo_from_ptr(struct radeon_winsys
*rws
,
909 void *pointer
, uint64_t size
)
911 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
912 amdgpu_bo_handle buf_handle
;
913 struct amdgpu_winsys_bo
*bo
;
915 amdgpu_va_handle va_handle
;
917 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
921 if (amdgpu_create_bo_from_user_mem(ws
->dev
, pointer
, size
, &buf_handle
))
924 if (amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
925 size
, 1 << 12, 0, &va
, &va_handle
, 0))
928 if (amdgpu_bo_va_op(buf_handle
, 0, size
, va
, 0, AMDGPU_VA_OP_MAP
))
932 pipe_reference_init(&bo
->base
.reference
, 1);
934 bo
->base
.alignment
= 0;
935 bo
->base
.size
= size
;
936 bo
->base
.vtbl
= &amdgpu_winsys_bo_vtbl
;
938 bo
->user_ptr
= pointer
;
940 bo
->u
.real
.va_handle
= va_handle
;
941 bo
->initial_domain
= RADEON_DOMAIN_GTT
;
942 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
944 ws
->allocated_gtt
+= align64(bo
->base
.size
, ws
->info
.gart_page_size
);
946 amdgpu_add_buffer_to_global_list(bo
);
948 return (struct pb_buffer
*)bo
;
951 amdgpu_va_range_free(va_handle
);
954 amdgpu_bo_free(buf_handle
);
961 static bool amdgpu_bo_is_user_ptr(struct pb_buffer
*buf
)
963 return ((struct amdgpu_winsys_bo
*)buf
)->user_ptr
!= NULL
;
966 static uint64_t amdgpu_bo_get_va(struct pb_buffer
*buf
)
968 return ((struct amdgpu_winsys_bo
*)buf
)->va
;
971 void amdgpu_bo_init_functions(struct amdgpu_winsys
*ws
)
973 ws
->base
.buffer_set_metadata
= amdgpu_buffer_set_metadata
;
974 ws
->base
.buffer_get_metadata
= amdgpu_buffer_get_metadata
;
975 ws
->base
.buffer_map
= amdgpu_bo_map
;
976 ws
->base
.buffer_unmap
= amdgpu_bo_unmap
;
977 ws
->base
.buffer_wait
= amdgpu_bo_wait
;
978 ws
->base
.buffer_create
= amdgpu_bo_create
;
979 ws
->base
.buffer_from_handle
= amdgpu_bo_from_handle
;
980 ws
->base
.buffer_from_ptr
= amdgpu_bo_from_ptr
;
981 ws
->base
.buffer_is_user_ptr
= amdgpu_bo_is_user_ptr
;
982 ws
->base
.buffer_get_handle
= amdgpu_bo_get_handle
;
983 ws
->base
.buffer_get_virtual_address
= amdgpu_bo_get_va
;
984 ws
->base
.buffer_get_initial_domain
= amdgpu_bo_get_initial_domain
;