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
28 #include "amdgpu_cs.h"
30 #include "util/os_time.h"
31 #include "state_tracker/drm_driver.h"
32 #include <amdgpu_drm.h>
37 #ifndef AMDGPU_GEM_CREATE_VM_ALWAYS_VALID
38 #define AMDGPU_GEM_CREATE_VM_ALWAYS_VALID (1 << 6)
41 #ifndef AMDGPU_VA_RANGE_HIGH
42 #define AMDGPU_VA_RANGE_HIGH 0x2
45 /* Set to 1 for verbose output showing committed sparse buffer ranges. */
46 #define DEBUG_SPARSE_COMMITS 0
48 struct amdgpu_sparse_backing_chunk
{
52 static struct pb_buffer
*
53 amdgpu_bo_create(struct radeon_winsys
*rws
,
56 enum radeon_bo_domain domain
,
57 enum radeon_bo_flag flags
);
59 static bool amdgpu_bo_wait(struct pb_buffer
*_buf
, uint64_t timeout
,
60 enum radeon_bo_usage usage
)
62 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
63 struct amdgpu_winsys
*ws
= bo
->ws
;
67 if (p_atomic_read(&bo
->num_active_ioctls
))
71 abs_timeout
= os_time_get_absolute_timeout(timeout
);
73 /* Wait if any ioctl is being submitted with this buffer. */
74 if (!os_wait_until_zero_abs_timeout(&bo
->num_active_ioctls
, abs_timeout
))
79 /* We can't use user fences for shared buffers, because user fences
80 * are local to this process only. If we want to wait for all buffer
81 * uses in all processes, we have to use amdgpu_bo_wait_for_idle.
83 bool buffer_busy
= true;
86 r
= amdgpu_bo_wait_for_idle(bo
->bo
, timeout
, &buffer_busy
);
88 fprintf(stderr
, "%s: amdgpu_bo_wait_for_idle failed %i\n", __func__
,
97 simple_mtx_lock(&ws
->bo_fence_lock
);
99 for (idle_fences
= 0; idle_fences
< bo
->num_fences
; ++idle_fences
) {
100 if (!amdgpu_fence_wait(bo
->fences
[idle_fences
], 0, false))
104 /* Release the idle fences to avoid checking them again later. */
105 for (unsigned i
= 0; i
< idle_fences
; ++i
)
106 amdgpu_fence_reference(&bo
->fences
[i
], NULL
);
108 memmove(&bo
->fences
[0], &bo
->fences
[idle_fences
],
109 (bo
->num_fences
- idle_fences
) * sizeof(*bo
->fences
));
110 bo
->num_fences
-= idle_fences
;
112 buffer_idle
= !bo
->num_fences
;
113 simple_mtx_unlock(&ws
->bo_fence_lock
);
117 bool buffer_idle
= true;
119 simple_mtx_lock(&ws
->bo_fence_lock
);
120 while (bo
->num_fences
&& buffer_idle
) {
121 struct pipe_fence_handle
*fence
= NULL
;
122 bool fence_idle
= false;
124 amdgpu_fence_reference(&fence
, bo
->fences
[0]);
126 /* Wait for the fence. */
127 simple_mtx_unlock(&ws
->bo_fence_lock
);
128 if (amdgpu_fence_wait(fence
, abs_timeout
, true))
132 simple_mtx_lock(&ws
->bo_fence_lock
);
134 /* Release an idle fence to avoid checking it again later, keeping in
135 * mind that the fence array may have been modified by other threads.
137 if (fence_idle
&& bo
->num_fences
&& bo
->fences
[0] == fence
) {
138 amdgpu_fence_reference(&bo
->fences
[0], NULL
);
139 memmove(&bo
->fences
[0], &bo
->fences
[1],
140 (bo
->num_fences
- 1) * sizeof(*bo
->fences
));
144 amdgpu_fence_reference(&fence
, NULL
);
146 simple_mtx_unlock(&ws
->bo_fence_lock
);
152 static enum radeon_bo_domain
amdgpu_bo_get_initial_domain(
153 struct pb_buffer
*buf
)
155 return ((struct amdgpu_winsys_bo
*)buf
)->initial_domain
;
158 static void amdgpu_bo_remove_fences(struct amdgpu_winsys_bo
*bo
)
160 for (unsigned i
= 0; i
< bo
->num_fences
; ++i
)
161 amdgpu_fence_reference(&bo
->fences
[i
], NULL
);
168 void amdgpu_bo_destroy(struct pb_buffer
*_buf
)
170 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
172 assert(bo
->bo
&& "must not be called for slab entries");
174 if (bo
->ws
->debug_all_bos
) {
175 simple_mtx_lock(&bo
->ws
->global_bo_list_lock
);
176 LIST_DEL(&bo
->u
.real
.global_list_item
);
177 bo
->ws
->num_buffers
--;
178 simple_mtx_unlock(&bo
->ws
->global_bo_list_lock
);
181 amdgpu_bo_va_op(bo
->bo
, 0, bo
->base
.size
, bo
->va
, 0, AMDGPU_VA_OP_UNMAP
);
182 amdgpu_va_range_free(bo
->u
.real
.va_handle
);
183 amdgpu_bo_free(bo
->bo
);
185 amdgpu_bo_remove_fences(bo
);
187 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
188 bo
->ws
->allocated_vram
-= align64(bo
->base
.size
, bo
->ws
->info
.gart_page_size
);
189 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
190 bo
->ws
->allocated_gtt
-= align64(bo
->base
.size
, bo
->ws
->info
.gart_page_size
);
192 if (bo
->u
.real
.map_count
>= 1) {
193 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
194 bo
->ws
->mapped_vram
-= bo
->base
.size
;
195 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
196 bo
->ws
->mapped_gtt
-= bo
->base
.size
;
197 bo
->ws
->num_mapped_buffers
--;
203 static void amdgpu_bo_destroy_or_cache(struct pb_buffer
*_buf
)
205 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
207 assert(bo
->bo
); /* slab buffers have a separate vtbl */
209 if (bo
->u
.real
.use_reusable_pool
)
210 pb_cache_add_buffer(&bo
->u
.real
.cache_entry
);
212 amdgpu_bo_destroy(_buf
);
215 static void *amdgpu_bo_map(struct pb_buffer
*buf
,
216 struct radeon_winsys_cs
*rcs
,
217 enum pipe_transfer_usage usage
)
219 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)buf
;
220 struct amdgpu_winsys_bo
*real
;
221 struct amdgpu_cs
*cs
= (struct amdgpu_cs
*)rcs
;
228 /* If it's not unsynchronized bo_map, flush CS if needed and then wait. */
229 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
230 /* DONTBLOCK doesn't make sense with UNSYNCHRONIZED. */
231 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
232 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
235 * Since we are mapping for read, we don't need to wait
236 * if the GPU is using the buffer for read too
237 * (neither one is changing it).
239 * Only check whether the buffer is being used for write. */
240 if (cs
&& amdgpu_bo_is_referenced_by_cs_with_usage(cs
, bo
,
241 RADEON_USAGE_WRITE
)) {
242 cs
->flush_cs(cs
->flush_data
, PIPE_FLUSH_ASYNC
, NULL
);
246 if (!amdgpu_bo_wait((struct pb_buffer
*)bo
, 0,
247 RADEON_USAGE_WRITE
)) {
251 if (cs
&& amdgpu_bo_is_referenced_by_cs(cs
, bo
)) {
252 cs
->flush_cs(cs
->flush_data
, PIPE_FLUSH_ASYNC
, NULL
);
256 if (!amdgpu_bo_wait((struct pb_buffer
*)bo
, 0,
257 RADEON_USAGE_READWRITE
)) {
262 uint64_t time
= os_time_get_nano();
264 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
267 * Since we are mapping for read, we don't need to wait
268 * if the GPU is using the buffer for read too
269 * (neither one is changing it).
271 * Only check whether the buffer is being used for write. */
273 if (amdgpu_bo_is_referenced_by_cs_with_usage(cs
, bo
,
274 RADEON_USAGE_WRITE
)) {
275 cs
->flush_cs(cs
->flush_data
, 0, NULL
);
277 /* Try to avoid busy-waiting in amdgpu_bo_wait. */
278 if (p_atomic_read(&bo
->num_active_ioctls
))
279 amdgpu_cs_sync_flush(rcs
);
283 amdgpu_bo_wait((struct pb_buffer
*)bo
, PIPE_TIMEOUT_INFINITE
,
286 /* Mapping for write. */
288 if (amdgpu_bo_is_referenced_by_cs(cs
, bo
)) {
289 cs
->flush_cs(cs
->flush_data
, 0, NULL
);
291 /* Try to avoid busy-waiting in amdgpu_bo_wait. */
292 if (p_atomic_read(&bo
->num_active_ioctls
))
293 amdgpu_cs_sync_flush(rcs
);
297 amdgpu_bo_wait((struct pb_buffer
*)bo
, PIPE_TIMEOUT_INFINITE
,
298 RADEON_USAGE_READWRITE
);
301 bo
->ws
->buffer_wait_time
+= os_time_get_nano() - time
;
305 /* If the buffer is created from user memory, return the user pointer. */
312 real
= bo
->u
.slab
.real
;
313 offset
= bo
->va
- real
->va
;
316 r
= amdgpu_bo_cpu_map(real
->bo
, &cpu
);
318 /* Clear the cache and try again. */
319 pb_cache_release_all_buffers(&real
->ws
->bo_cache
);
320 r
= amdgpu_bo_cpu_map(real
->bo
, &cpu
);
325 if (p_atomic_inc_return(&real
->u
.real
.map_count
) == 1) {
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
;
330 real
->ws
->num_mapped_buffers
++;
332 return (uint8_t*)cpu
+ offset
;
335 static void amdgpu_bo_unmap(struct pb_buffer
*buf
)
337 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)buf
;
338 struct amdgpu_winsys_bo
*real
;
345 real
= bo
->bo
? bo
: bo
->u
.slab
.real
;
347 if (p_atomic_dec_zero(&real
->u
.real
.map_count
)) {
348 if (real
->initial_domain
& RADEON_DOMAIN_VRAM
)
349 real
->ws
->mapped_vram
-= real
->base
.size
;
350 else if (real
->initial_domain
& RADEON_DOMAIN_GTT
)
351 real
->ws
->mapped_gtt
-= real
->base
.size
;
352 real
->ws
->num_mapped_buffers
--;
355 amdgpu_bo_cpu_unmap(real
->bo
);
358 static const struct pb_vtbl amdgpu_winsys_bo_vtbl
= {
359 amdgpu_bo_destroy_or_cache
360 /* other functions are never called */
363 static void amdgpu_add_buffer_to_global_list(struct amdgpu_winsys_bo
*bo
)
365 struct amdgpu_winsys
*ws
= bo
->ws
;
369 if (ws
->debug_all_bos
) {
370 simple_mtx_lock(&ws
->global_bo_list_lock
);
371 LIST_ADDTAIL(&bo
->u
.real
.global_list_item
, &ws
->global_bo_list
);
373 simple_mtx_unlock(&ws
->global_bo_list_lock
);
377 static struct amdgpu_winsys_bo
*amdgpu_create_bo(struct amdgpu_winsys
*ws
,
380 enum radeon_bo_domain initial_domain
,
384 struct amdgpu_bo_alloc_request request
= {0};
385 amdgpu_bo_handle buf_handle
;
387 struct amdgpu_winsys_bo
*bo
;
388 amdgpu_va_handle va_handle
;
389 unsigned va_gap_size
;
392 /* VRAM or GTT must be specified, but not both at the same time. */
393 assert(util_bitcount(initial_domain
& RADEON_DOMAIN_VRAM_GTT
) == 1);
395 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
401 pb_cache_init_entry(&ws
->bo_cache
, &bo
->u
.real
.cache_entry
, &bo
->base
,
404 request
.alloc_size
= size
;
405 request
.phys_alignment
= alignment
;
407 if (initial_domain
& RADEON_DOMAIN_VRAM
)
408 request
.preferred_heap
|= AMDGPU_GEM_DOMAIN_VRAM
;
409 if (initial_domain
& RADEON_DOMAIN_GTT
)
410 request
.preferred_heap
|= AMDGPU_GEM_DOMAIN_GTT
;
412 /* Since VRAM and GTT have almost the same performance on APUs, we could
413 * just set GTT. However, in order to decrease GTT(RAM) usage, which is
414 * shared with the OS, allow VRAM placements too. The idea is not to use
415 * VRAM usefully, but to use it so that it's not unused and wasted.
417 if (!ws
->info
.has_dedicated_vram
)
418 request
.preferred_heap
|= AMDGPU_GEM_DOMAIN_GTT
;
420 if (flags
& RADEON_FLAG_NO_CPU_ACCESS
)
421 request
.flags
|= AMDGPU_GEM_CREATE_NO_CPU_ACCESS
;
422 if (flags
& RADEON_FLAG_GTT_WC
)
423 request
.flags
|= AMDGPU_GEM_CREATE_CPU_GTT_USWC
;
424 if (flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
&&
425 ws
->info
.has_local_buffers
)
426 request
.flags
|= AMDGPU_GEM_CREATE_VM_ALWAYS_VALID
;
428 r
= amdgpu_bo_alloc(ws
->dev
, &request
, &buf_handle
);
430 fprintf(stderr
, "amdgpu: Failed to allocate a buffer:\n");
431 fprintf(stderr
, "amdgpu: size : %"PRIu64
" bytes\n", size
);
432 fprintf(stderr
, "amdgpu: alignment : %u bytes\n", alignment
);
433 fprintf(stderr
, "amdgpu: domains : %u\n", initial_domain
);
437 va_gap_size
= ws
->check_vm
? MAX2(4 * alignment
, 64 * 1024) : 0;
438 if (size
> ws
->info
.pte_fragment_size
)
439 alignment
= MAX2(alignment
, ws
->info
.pte_fragment_size
);
440 r
= amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
441 size
+ va_gap_size
, alignment
, 0, &va
, &va_handle
,
442 (flags
& RADEON_FLAG_32BIT
? AMDGPU_VA_RANGE_32_BIT
: 0) |
443 AMDGPU_VA_RANGE_HIGH
);
447 unsigned vm_flags
= AMDGPU_VM_PAGE_READABLE
|
448 AMDGPU_VM_PAGE_EXECUTABLE
;
450 if (!(flags
& RADEON_FLAG_READ_ONLY
))
451 vm_flags
|= AMDGPU_VM_PAGE_WRITEABLE
;
453 r
= amdgpu_bo_va_op_raw(ws
->dev
, buf_handle
, 0, size
, va
, vm_flags
,
458 pipe_reference_init(&bo
->base
.reference
, 1);
459 bo
->base
.alignment
= alignment
;
461 bo
->base
.size
= size
;
462 bo
->base
.vtbl
= &amdgpu_winsys_bo_vtbl
;
466 bo
->u
.real
.va_handle
= va_handle
;
467 bo
->initial_domain
= initial_domain
;
468 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
469 bo
->is_local
= !!(request
.flags
& AMDGPU_GEM_CREATE_VM_ALWAYS_VALID
);
471 if (initial_domain
& RADEON_DOMAIN_VRAM
)
472 ws
->allocated_vram
+= align64(size
, ws
->info
.gart_page_size
);
473 else if (initial_domain
& RADEON_DOMAIN_GTT
)
474 ws
->allocated_gtt
+= align64(size
, ws
->info
.gart_page_size
);
476 amdgpu_add_buffer_to_global_list(bo
);
481 amdgpu_va_range_free(va_handle
);
484 amdgpu_bo_free(buf_handle
);
491 bool amdgpu_bo_can_reclaim(struct pb_buffer
*_buf
)
493 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
495 if (amdgpu_bo_is_referenced_by_any_cs(bo
)) {
499 return amdgpu_bo_wait(_buf
, 0, RADEON_USAGE_READWRITE
);
502 bool amdgpu_bo_can_reclaim_slab(void *priv
, struct pb_slab_entry
*entry
)
504 struct amdgpu_winsys_bo
*bo
= NULL
; /* fix container_of */
505 bo
= container_of(entry
, bo
, u
.slab
.entry
);
507 return amdgpu_bo_can_reclaim(&bo
->base
);
510 static void amdgpu_bo_slab_destroy(struct pb_buffer
*_buf
)
512 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
516 pb_slab_free(&bo
->ws
->bo_slabs
, &bo
->u
.slab
.entry
);
519 static const struct pb_vtbl amdgpu_winsys_bo_slab_vtbl
= {
520 amdgpu_bo_slab_destroy
521 /* other functions are never called */
524 struct pb_slab
*amdgpu_bo_slab_alloc(void *priv
, unsigned heap
,
526 unsigned group_index
)
528 struct amdgpu_winsys
*ws
= priv
;
529 struct amdgpu_slab
*slab
= CALLOC_STRUCT(amdgpu_slab
);
530 enum radeon_bo_domain domains
= radeon_domain_from_heap(heap
);
531 enum radeon_bo_flag flags
= radeon_flags_from_heap(heap
);
537 unsigned slab_size
= 1 << AMDGPU_SLAB_BO_SIZE_LOG2
;
538 slab
->buffer
= amdgpu_winsys_bo(amdgpu_bo_create(&ws
->base
,
539 slab_size
, slab_size
,
544 assert(slab
->buffer
->bo
);
546 slab
->base
.num_entries
= slab
->buffer
->base
.size
/ entry_size
;
547 slab
->base
.num_free
= slab
->base
.num_entries
;
548 slab
->entries
= CALLOC(slab
->base
.num_entries
, sizeof(*slab
->entries
));
552 LIST_INITHEAD(&slab
->base
.free
);
554 base_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, slab
->base
.num_entries
);
556 for (unsigned i
= 0; i
< slab
->base
.num_entries
; ++i
) {
557 struct amdgpu_winsys_bo
*bo
= &slab
->entries
[i
];
559 bo
->base
.alignment
= entry_size
;
560 bo
->base
.usage
= slab
->buffer
->base
.usage
;
561 bo
->base
.size
= entry_size
;
562 bo
->base
.vtbl
= &amdgpu_winsys_bo_slab_vtbl
;
564 bo
->va
= slab
->buffer
->va
+ i
* entry_size
;
565 bo
->initial_domain
= domains
;
566 bo
->unique_id
= base_id
+ i
;
567 bo
->u
.slab
.entry
.slab
= &slab
->base
;
568 bo
->u
.slab
.entry
.group_index
= group_index
;
569 bo
->u
.slab
.real
= slab
->buffer
;
571 LIST_ADDTAIL(&bo
->u
.slab
.entry
.head
, &slab
->base
.free
);
577 amdgpu_winsys_bo_reference(&slab
->buffer
, NULL
);
583 void amdgpu_bo_slab_free(void *priv
, struct pb_slab
*pslab
)
585 struct amdgpu_slab
*slab
= amdgpu_slab(pslab
);
587 for (unsigned i
= 0; i
< slab
->base
.num_entries
; ++i
)
588 amdgpu_bo_remove_fences(&slab
->entries
[i
]);
591 amdgpu_winsys_bo_reference(&slab
->buffer
, NULL
);
595 #if DEBUG_SPARSE_COMMITS
597 sparse_dump(struct amdgpu_winsys_bo
*bo
, const char *func
)
599 fprintf(stderr
, "%s: %p (size=%"PRIu64
", num_va_pages=%u) @ %s\n"
601 __func__
, bo
, bo
->base
.size
, bo
->u
.sparse
.num_va_pages
, func
);
603 struct amdgpu_sparse_backing
*span_backing
= NULL
;
604 uint32_t span_first_backing_page
= 0;
605 uint32_t span_first_va_page
= 0;
606 uint32_t va_page
= 0;
609 struct amdgpu_sparse_backing
*backing
= 0;
610 uint32_t backing_page
= 0;
612 if (va_page
< bo
->u
.sparse
.num_va_pages
) {
613 backing
= bo
->u
.sparse
.commitments
[va_page
].backing
;
614 backing_page
= bo
->u
.sparse
.commitments
[va_page
].page
;
618 (backing
!= span_backing
||
619 backing_page
!= span_first_backing_page
+ (va_page
- span_first_va_page
))) {
620 fprintf(stderr
, " %u..%u: backing=%p:%u..%u\n",
621 span_first_va_page
, va_page
- 1, span_backing
,
622 span_first_backing_page
,
623 span_first_backing_page
+ (va_page
- span_first_va_page
) - 1);
628 if (va_page
>= bo
->u
.sparse
.num_va_pages
)
631 if (backing
&& !span_backing
) {
632 span_backing
= backing
;
633 span_first_backing_page
= backing_page
;
634 span_first_va_page
= va_page
;
640 fprintf(stderr
, "Backing:\n");
642 list_for_each_entry(struct amdgpu_sparse_backing
, backing
, &bo
->u
.sparse
.backing
, list
) {
643 fprintf(stderr
, " %p (size=%"PRIu64
")\n", backing
, backing
->bo
->base
.size
);
644 for (unsigned i
= 0; i
< backing
->num_chunks
; ++i
)
645 fprintf(stderr
, " %u..%u\n", backing
->chunks
[i
].begin
, backing
->chunks
[i
].end
);
651 * Attempt to allocate the given number of backing pages. Fewer pages may be
652 * allocated (depending on the fragmentation of existing backing buffers),
653 * which will be reflected by a change to *pnum_pages.
655 static struct amdgpu_sparse_backing
*
656 sparse_backing_alloc(struct amdgpu_winsys_bo
*bo
, uint32_t *pstart_page
, uint32_t *pnum_pages
)
658 struct amdgpu_sparse_backing
*best_backing
;
660 uint32_t best_num_pages
;
666 /* This is a very simple and inefficient best-fit algorithm. */
667 list_for_each_entry(struct amdgpu_sparse_backing
, backing
, &bo
->u
.sparse
.backing
, list
) {
668 for (unsigned idx
= 0; idx
< backing
->num_chunks
; ++idx
) {
669 uint32_t cur_num_pages
= backing
->chunks
[idx
].end
- backing
->chunks
[idx
].begin
;
670 if ((best_num_pages
< *pnum_pages
&& cur_num_pages
> best_num_pages
) ||
671 (best_num_pages
> *pnum_pages
&& cur_num_pages
< best_num_pages
)) {
672 best_backing
= backing
;
674 best_num_pages
= cur_num_pages
;
679 /* Allocate a new backing buffer if necessary. */
681 struct pb_buffer
*buf
;
685 best_backing
= CALLOC_STRUCT(amdgpu_sparse_backing
);
689 best_backing
->max_chunks
= 4;
690 best_backing
->chunks
= CALLOC(best_backing
->max_chunks
,
691 sizeof(*best_backing
->chunks
));
692 if (!best_backing
->chunks
) {
697 assert(bo
->u
.sparse
.num_backing_pages
< DIV_ROUND_UP(bo
->base
.size
, RADEON_SPARSE_PAGE_SIZE
));
699 size
= MIN3(bo
->base
.size
/ 16,
701 bo
->base
.size
- (uint64_t)bo
->u
.sparse
.num_backing_pages
* RADEON_SPARSE_PAGE_SIZE
);
702 size
= MAX2(size
, RADEON_SPARSE_PAGE_SIZE
);
704 buf
= amdgpu_bo_create(&bo
->ws
->base
, size
, RADEON_SPARSE_PAGE_SIZE
,
706 bo
->u
.sparse
.flags
| RADEON_FLAG_NO_SUBALLOC
);
708 FREE(best_backing
->chunks
);
713 /* We might have gotten a bigger buffer than requested via caching. */
714 pages
= buf
->size
/ RADEON_SPARSE_PAGE_SIZE
;
716 best_backing
->bo
= amdgpu_winsys_bo(buf
);
717 best_backing
->num_chunks
= 1;
718 best_backing
->chunks
[0].begin
= 0;
719 best_backing
->chunks
[0].end
= pages
;
721 list_add(&best_backing
->list
, &bo
->u
.sparse
.backing
);
722 bo
->u
.sparse
.num_backing_pages
+= pages
;
725 best_num_pages
= pages
;
728 *pnum_pages
= MIN2(*pnum_pages
, best_num_pages
);
729 *pstart_page
= best_backing
->chunks
[best_idx
].begin
;
730 best_backing
->chunks
[best_idx
].begin
+= *pnum_pages
;
732 if (best_backing
->chunks
[best_idx
].begin
>= best_backing
->chunks
[best_idx
].end
) {
733 memmove(&best_backing
->chunks
[best_idx
], &best_backing
->chunks
[best_idx
+ 1],
734 sizeof(*best_backing
->chunks
) * (best_backing
->num_chunks
- best_idx
- 1));
735 best_backing
->num_chunks
--;
742 sparse_free_backing_buffer(struct amdgpu_winsys_bo
*bo
,
743 struct amdgpu_sparse_backing
*backing
)
745 struct amdgpu_winsys
*ws
= backing
->bo
->ws
;
747 bo
->u
.sparse
.num_backing_pages
-= backing
->bo
->base
.size
/ RADEON_SPARSE_PAGE_SIZE
;
749 simple_mtx_lock(&ws
->bo_fence_lock
);
750 amdgpu_add_fences(backing
->bo
, bo
->num_fences
, bo
->fences
);
751 simple_mtx_unlock(&ws
->bo_fence_lock
);
753 list_del(&backing
->list
);
754 amdgpu_winsys_bo_reference(&backing
->bo
, NULL
);
755 FREE(backing
->chunks
);
760 * Return a range of pages from the given backing buffer back into the
764 sparse_backing_free(struct amdgpu_winsys_bo
*bo
,
765 struct amdgpu_sparse_backing
*backing
,
766 uint32_t start_page
, uint32_t num_pages
)
768 uint32_t end_page
= start_page
+ num_pages
;
770 unsigned high
= backing
->num_chunks
;
772 /* Find the first chunk with begin >= start_page. */
774 unsigned mid
= low
+ (high
- low
) / 2;
776 if (backing
->chunks
[mid
].begin
>= start_page
)
782 assert(low
>= backing
->num_chunks
|| end_page
<= backing
->chunks
[low
].begin
);
783 assert(low
== 0 || backing
->chunks
[low
- 1].end
<= start_page
);
785 if (low
> 0 && backing
->chunks
[low
- 1].end
== start_page
) {
786 backing
->chunks
[low
- 1].end
= end_page
;
788 if (low
< backing
->num_chunks
&& end_page
== backing
->chunks
[low
].begin
) {
789 backing
->chunks
[low
- 1].end
= backing
->chunks
[low
].end
;
790 memmove(&backing
->chunks
[low
], &backing
->chunks
[low
+ 1],
791 sizeof(*backing
->chunks
) * (backing
->num_chunks
- low
- 1));
792 backing
->num_chunks
--;
794 } else if (low
< backing
->num_chunks
&& end_page
== backing
->chunks
[low
].begin
) {
795 backing
->chunks
[low
].begin
= start_page
;
797 if (backing
->num_chunks
>= backing
->max_chunks
) {
798 unsigned new_max_chunks
= 2 * backing
->max_chunks
;
799 struct amdgpu_sparse_backing_chunk
*new_chunks
=
800 REALLOC(backing
->chunks
,
801 sizeof(*backing
->chunks
) * backing
->max_chunks
,
802 sizeof(*backing
->chunks
) * new_max_chunks
);
806 backing
->max_chunks
= new_max_chunks
;
807 backing
->chunks
= new_chunks
;
810 memmove(&backing
->chunks
[low
+ 1], &backing
->chunks
[low
],
811 sizeof(*backing
->chunks
) * (backing
->num_chunks
- low
));
812 backing
->chunks
[low
].begin
= start_page
;
813 backing
->chunks
[low
].end
= end_page
;
814 backing
->num_chunks
++;
817 if (backing
->num_chunks
== 1 && backing
->chunks
[0].begin
== 0 &&
818 backing
->chunks
[0].end
== backing
->bo
->base
.size
/ RADEON_SPARSE_PAGE_SIZE
)
819 sparse_free_backing_buffer(bo
, backing
);
824 static void amdgpu_bo_sparse_destroy(struct pb_buffer
*_buf
)
826 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
829 assert(!bo
->bo
&& bo
->sparse
);
831 r
= amdgpu_bo_va_op_raw(bo
->ws
->dev
, NULL
, 0,
832 (uint64_t)bo
->u
.sparse
.num_va_pages
* RADEON_SPARSE_PAGE_SIZE
,
833 bo
->va
, 0, AMDGPU_VA_OP_CLEAR
);
835 fprintf(stderr
, "amdgpu: clearing PRT VA region on destroy failed (%d)\n", r
);
838 while (!list_empty(&bo
->u
.sparse
.backing
)) {
839 struct amdgpu_sparse_backing
*dummy
= NULL
;
840 sparse_free_backing_buffer(bo
,
841 container_of(bo
->u
.sparse
.backing
.next
,
845 amdgpu_va_range_free(bo
->u
.sparse
.va_handle
);
846 simple_mtx_destroy(&bo
->u
.sparse
.commit_lock
);
847 FREE(bo
->u
.sparse
.commitments
);
851 static const struct pb_vtbl amdgpu_winsys_bo_sparse_vtbl
= {
852 amdgpu_bo_sparse_destroy
853 /* other functions are never called */
856 static struct pb_buffer
*
857 amdgpu_bo_sparse_create(struct amdgpu_winsys
*ws
, uint64_t size
,
858 enum radeon_bo_domain domain
,
859 enum radeon_bo_flag flags
)
861 struct amdgpu_winsys_bo
*bo
;
863 uint64_t va_gap_size
;
866 /* We use 32-bit page numbers; refuse to attempt allocating sparse buffers
867 * that exceed this limit. This is not really a restriction: we don't have
868 * that much virtual address space anyway.
870 if (size
> (uint64_t)INT32_MAX
* RADEON_SPARSE_PAGE_SIZE
)
873 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
877 pipe_reference_init(&bo
->base
.reference
, 1);
878 bo
->base
.alignment
= RADEON_SPARSE_PAGE_SIZE
;
879 bo
->base
.size
= size
;
880 bo
->base
.vtbl
= &amdgpu_winsys_bo_sparse_vtbl
;
882 bo
->initial_domain
= domain
;
883 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
885 bo
->u
.sparse
.flags
= flags
& ~RADEON_FLAG_SPARSE
;
887 bo
->u
.sparse
.num_va_pages
= DIV_ROUND_UP(size
, RADEON_SPARSE_PAGE_SIZE
);
888 bo
->u
.sparse
.commitments
= CALLOC(bo
->u
.sparse
.num_va_pages
,
889 sizeof(*bo
->u
.sparse
.commitments
));
890 if (!bo
->u
.sparse
.commitments
)
891 goto error_alloc_commitments
;
893 simple_mtx_init(&bo
->u
.sparse
.commit_lock
, mtx_plain
);
894 LIST_INITHEAD(&bo
->u
.sparse
.backing
);
896 /* For simplicity, we always map a multiple of the page size. */
897 map_size
= align64(size
, RADEON_SPARSE_PAGE_SIZE
);
898 va_gap_size
= ws
->check_vm
? 4 * RADEON_SPARSE_PAGE_SIZE
: 0;
899 r
= amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
900 map_size
+ va_gap_size
, RADEON_SPARSE_PAGE_SIZE
,
901 0, &bo
->va
, &bo
->u
.sparse
.va_handle
,
902 AMDGPU_VA_RANGE_HIGH
);
906 r
= amdgpu_bo_va_op_raw(bo
->ws
->dev
, NULL
, 0, size
, bo
->va
,
907 AMDGPU_VM_PAGE_PRT
, AMDGPU_VA_OP_MAP
);
914 amdgpu_va_range_free(bo
->u
.sparse
.va_handle
);
916 simple_mtx_destroy(&bo
->u
.sparse
.commit_lock
);
917 FREE(bo
->u
.sparse
.commitments
);
918 error_alloc_commitments
:
924 amdgpu_bo_sparse_commit(struct pb_buffer
*buf
, uint64_t offset
, uint64_t size
,
927 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(buf
);
928 struct amdgpu_sparse_commitment
*comm
;
929 uint32_t va_page
, end_va_page
;
934 assert(offset
% RADEON_SPARSE_PAGE_SIZE
== 0);
935 assert(offset
<= bo
->base
.size
);
936 assert(size
<= bo
->base
.size
- offset
);
937 assert(size
% RADEON_SPARSE_PAGE_SIZE
== 0 || offset
+ size
== bo
->base
.size
);
939 comm
= bo
->u
.sparse
.commitments
;
940 va_page
= offset
/ RADEON_SPARSE_PAGE_SIZE
;
941 end_va_page
= va_page
+ DIV_ROUND_UP(size
, RADEON_SPARSE_PAGE_SIZE
);
943 simple_mtx_lock(&bo
->u
.sparse
.commit_lock
);
945 #if DEBUG_SPARSE_COMMITS
946 sparse_dump(bo
, __func__
);
950 while (va_page
< end_va_page
) {
951 uint32_t span_va_page
;
953 /* Skip pages that are already committed. */
954 if (comm
[va_page
].backing
) {
959 /* Determine length of uncommitted span. */
960 span_va_page
= va_page
;
961 while (va_page
< end_va_page
&& !comm
[va_page
].backing
)
964 /* Fill the uncommitted span with chunks of backing memory. */
965 while (span_va_page
< va_page
) {
966 struct amdgpu_sparse_backing
*backing
;
967 uint32_t backing_start
, backing_size
;
969 backing_size
= va_page
- span_va_page
;
970 backing
= sparse_backing_alloc(bo
, &backing_start
, &backing_size
);
976 r
= amdgpu_bo_va_op_raw(bo
->ws
->dev
, backing
->bo
->bo
,
977 (uint64_t)backing_start
* RADEON_SPARSE_PAGE_SIZE
,
978 (uint64_t)backing_size
* RADEON_SPARSE_PAGE_SIZE
,
979 bo
->va
+ (uint64_t)span_va_page
* RADEON_SPARSE_PAGE_SIZE
,
980 AMDGPU_VM_PAGE_READABLE
|
981 AMDGPU_VM_PAGE_WRITEABLE
|
982 AMDGPU_VM_PAGE_EXECUTABLE
,
983 AMDGPU_VA_OP_REPLACE
);
985 ok
= sparse_backing_free(bo
, backing
, backing_start
, backing_size
);
986 assert(ok
&& "sufficient memory should already be allocated");
992 while (backing_size
) {
993 comm
[span_va_page
].backing
= backing
;
994 comm
[span_va_page
].page
= backing_start
;
1002 r
= amdgpu_bo_va_op_raw(bo
->ws
->dev
, NULL
, 0,
1003 (uint64_t)(end_va_page
- va_page
) * RADEON_SPARSE_PAGE_SIZE
,
1004 bo
->va
+ (uint64_t)va_page
* RADEON_SPARSE_PAGE_SIZE
,
1005 AMDGPU_VM_PAGE_PRT
, AMDGPU_VA_OP_REPLACE
);
1011 while (va_page
< end_va_page
) {
1012 struct amdgpu_sparse_backing
*backing
;
1013 uint32_t backing_start
;
1014 uint32_t span_pages
;
1016 /* Skip pages that are already uncommitted. */
1017 if (!comm
[va_page
].backing
) {
1022 /* Group contiguous spans of pages. */
1023 backing
= comm
[va_page
].backing
;
1024 backing_start
= comm
[va_page
].page
;
1025 comm
[va_page
].backing
= NULL
;
1030 while (va_page
< end_va_page
&&
1031 comm
[va_page
].backing
== backing
&&
1032 comm
[va_page
].page
== backing_start
+ span_pages
) {
1033 comm
[va_page
].backing
= NULL
;
1038 if (!sparse_backing_free(bo
, backing
, backing_start
, span_pages
)) {
1039 /* Couldn't allocate tracking data structures, so we have to leak */
1040 fprintf(stderr
, "amdgpu: leaking PRT backing memory\n");
1047 simple_mtx_unlock(&bo
->u
.sparse
.commit_lock
);
1052 static unsigned eg_tile_split(unsigned tile_split
)
1054 switch (tile_split
) {
1055 case 0: tile_split
= 64; break;
1056 case 1: tile_split
= 128; break;
1057 case 2: tile_split
= 256; break;
1058 case 3: tile_split
= 512; break;
1060 case 4: tile_split
= 1024; break;
1061 case 5: tile_split
= 2048; break;
1062 case 6: tile_split
= 4096; break;
1067 static unsigned eg_tile_split_rev(unsigned eg_tile_split
)
1069 switch (eg_tile_split
) {
1075 case 1024: return 4;
1076 case 2048: return 5;
1077 case 4096: return 6;
1081 static void amdgpu_buffer_get_metadata(struct pb_buffer
*_buf
,
1082 struct radeon_bo_metadata
*md
)
1084 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
1085 struct amdgpu_bo_info info
= {0};
1086 uint64_t tiling_flags
;
1089 assert(bo
->bo
&& "must not be called for slab entries");
1091 r
= amdgpu_bo_query_info(bo
->bo
, &info
);
1095 tiling_flags
= info
.metadata
.tiling_info
;
1097 if (bo
->ws
->info
.chip_class
>= GFX9
) {
1098 md
->u
.gfx9
.swizzle_mode
= AMDGPU_TILING_GET(tiling_flags
, SWIZZLE_MODE
);
1100 md
->u
.legacy
.microtile
= RADEON_LAYOUT_LINEAR
;
1101 md
->u
.legacy
.macrotile
= RADEON_LAYOUT_LINEAR
;
1103 if (AMDGPU_TILING_GET(tiling_flags
, ARRAY_MODE
) == 4) /* 2D_TILED_THIN1 */
1104 md
->u
.legacy
.macrotile
= RADEON_LAYOUT_TILED
;
1105 else if (AMDGPU_TILING_GET(tiling_flags
, ARRAY_MODE
) == 2) /* 1D_TILED_THIN1 */
1106 md
->u
.legacy
.microtile
= RADEON_LAYOUT_TILED
;
1108 md
->u
.legacy
.pipe_config
= AMDGPU_TILING_GET(tiling_flags
, PIPE_CONFIG
);
1109 md
->u
.legacy
.bankw
= 1 << AMDGPU_TILING_GET(tiling_flags
, BANK_WIDTH
);
1110 md
->u
.legacy
.bankh
= 1 << AMDGPU_TILING_GET(tiling_flags
, BANK_HEIGHT
);
1111 md
->u
.legacy
.tile_split
= eg_tile_split(AMDGPU_TILING_GET(tiling_flags
, TILE_SPLIT
));
1112 md
->u
.legacy
.mtilea
= 1 << AMDGPU_TILING_GET(tiling_flags
, MACRO_TILE_ASPECT
);
1113 md
->u
.legacy
.num_banks
= 2 << AMDGPU_TILING_GET(tiling_flags
, NUM_BANKS
);
1114 md
->u
.legacy
.scanout
= AMDGPU_TILING_GET(tiling_flags
, MICRO_TILE_MODE
) == 0; /* DISPLAY */
1117 md
->size_metadata
= info
.metadata
.size_metadata
;
1118 memcpy(md
->metadata
, info
.metadata
.umd_metadata
, sizeof(md
->metadata
));
1121 static void amdgpu_buffer_set_metadata(struct pb_buffer
*_buf
,
1122 struct radeon_bo_metadata
*md
)
1124 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
1125 struct amdgpu_bo_metadata metadata
= {0};
1126 uint64_t tiling_flags
= 0;
1128 assert(bo
->bo
&& "must not be called for slab entries");
1130 if (bo
->ws
->info
.chip_class
>= GFX9
) {
1131 tiling_flags
|= AMDGPU_TILING_SET(SWIZZLE_MODE
, md
->u
.gfx9
.swizzle_mode
);
1133 if (md
->u
.legacy
.macrotile
== RADEON_LAYOUT_TILED
)
1134 tiling_flags
|= AMDGPU_TILING_SET(ARRAY_MODE
, 4); /* 2D_TILED_THIN1 */
1135 else if (md
->u
.legacy
.microtile
== RADEON_LAYOUT_TILED
)
1136 tiling_flags
|= AMDGPU_TILING_SET(ARRAY_MODE
, 2); /* 1D_TILED_THIN1 */
1138 tiling_flags
|= AMDGPU_TILING_SET(ARRAY_MODE
, 1); /* LINEAR_ALIGNED */
1140 tiling_flags
|= AMDGPU_TILING_SET(PIPE_CONFIG
, md
->u
.legacy
.pipe_config
);
1141 tiling_flags
|= AMDGPU_TILING_SET(BANK_WIDTH
, util_logbase2(md
->u
.legacy
.bankw
));
1142 tiling_flags
|= AMDGPU_TILING_SET(BANK_HEIGHT
, util_logbase2(md
->u
.legacy
.bankh
));
1143 if (md
->u
.legacy
.tile_split
)
1144 tiling_flags
|= AMDGPU_TILING_SET(TILE_SPLIT
, eg_tile_split_rev(md
->u
.legacy
.tile_split
));
1145 tiling_flags
|= AMDGPU_TILING_SET(MACRO_TILE_ASPECT
, util_logbase2(md
->u
.legacy
.mtilea
));
1146 tiling_flags
|= AMDGPU_TILING_SET(NUM_BANKS
, util_logbase2(md
->u
.legacy
.num_banks
)-1);
1148 if (md
->u
.legacy
.scanout
)
1149 tiling_flags
|= AMDGPU_TILING_SET(MICRO_TILE_MODE
, 0); /* DISPLAY_MICRO_TILING */
1151 tiling_flags
|= AMDGPU_TILING_SET(MICRO_TILE_MODE
, 1); /* THIN_MICRO_TILING */
1154 metadata
.tiling_info
= tiling_flags
;
1155 metadata
.size_metadata
= md
->size_metadata
;
1156 memcpy(metadata
.umd_metadata
, md
->metadata
, sizeof(md
->metadata
));
1158 amdgpu_bo_set_metadata(bo
->bo
, &metadata
);
1161 static struct pb_buffer
*
1162 amdgpu_bo_create(struct radeon_winsys
*rws
,
1165 enum radeon_bo_domain domain
,
1166 enum radeon_bo_flag flags
)
1168 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
1169 struct amdgpu_winsys_bo
*bo
;
1172 /* VRAM implies WC. This is not optional. */
1173 assert(!(domain
& RADEON_DOMAIN_VRAM
) || flags
& RADEON_FLAG_GTT_WC
);
1175 /* NO_CPU_ACCESS is valid with VRAM only. */
1176 assert(domain
== RADEON_DOMAIN_VRAM
|| !(flags
& RADEON_FLAG_NO_CPU_ACCESS
));
1178 /* Sparse buffers must have NO_CPU_ACCESS set. */
1179 assert(!(flags
& RADEON_FLAG_SPARSE
) || flags
& RADEON_FLAG_NO_CPU_ACCESS
);
1181 /* Sub-allocate small buffers from slabs. */
1182 if (!(flags
& (RADEON_FLAG_NO_SUBALLOC
| RADEON_FLAG_SPARSE
)) &&
1183 size
<= (1 << AMDGPU_SLAB_MAX_SIZE_LOG2
) &&
1184 alignment
<= MAX2(1 << AMDGPU_SLAB_MIN_SIZE_LOG2
, util_next_power_of_two(size
))) {
1185 struct pb_slab_entry
*entry
;
1186 int heap
= radeon_get_heap_index(domain
, flags
);
1188 if (heap
< 0 || heap
>= RADEON_MAX_SLAB_HEAPS
)
1191 entry
= pb_slab_alloc(&ws
->bo_slabs
, size
, heap
);
1193 /* Clear the cache and try again. */
1194 pb_cache_release_all_buffers(&ws
->bo_cache
);
1196 entry
= pb_slab_alloc(&ws
->bo_slabs
, size
, heap
);
1202 bo
= container_of(entry
, bo
, u
.slab
.entry
);
1204 pipe_reference_init(&bo
->base
.reference
, 1);
1210 if (flags
& RADEON_FLAG_SPARSE
) {
1211 assert(RADEON_SPARSE_PAGE_SIZE
% alignment
== 0);
1213 return amdgpu_bo_sparse_create(ws
, size
, domain
, flags
);
1216 /* This flag is irrelevant for the cache. */
1217 flags
&= ~RADEON_FLAG_NO_SUBALLOC
;
1219 /* Align size to page size. This is the minimum alignment for normal
1220 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
1221 * like constant/uniform buffers, can benefit from better and more reuse.
1223 size
= align64(size
, ws
->info
.gart_page_size
);
1224 alignment
= align(alignment
, ws
->info
.gart_page_size
);
1226 bool use_reusable_pool
= flags
& RADEON_FLAG_NO_INTERPROCESS_SHARING
;
1228 if (use_reusable_pool
) {
1229 heap
= radeon_get_heap_index(domain
, flags
);
1230 assert(heap
>= 0 && heap
< RADEON_MAX_CACHED_HEAPS
);
1232 /* Get a buffer from the cache. */
1233 bo
= (struct amdgpu_winsys_bo
*)
1234 pb_cache_reclaim_buffer(&ws
->bo_cache
, size
, alignment
, 0, heap
);
1239 /* Create a new one. */
1240 bo
= amdgpu_create_bo(ws
, size
, alignment
, domain
, flags
, heap
);
1242 /* Clear the cache and try again. */
1243 pb_slabs_reclaim(&ws
->bo_slabs
);
1244 pb_cache_release_all_buffers(&ws
->bo_cache
);
1245 bo
= amdgpu_create_bo(ws
, size
, alignment
, domain
, flags
, heap
);
1250 bo
->u
.real
.use_reusable_pool
= use_reusable_pool
;
1254 static struct pb_buffer
*amdgpu_bo_from_handle(struct radeon_winsys
*rws
,
1255 struct winsys_handle
*whandle
,
1259 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
1260 struct amdgpu_winsys_bo
*bo
;
1261 enum amdgpu_bo_handle_type type
;
1262 struct amdgpu_bo_import_result result
= {0};
1264 amdgpu_va_handle va_handle
;
1265 struct amdgpu_bo_info info
= {0};
1266 enum radeon_bo_domain initial
= 0;
1269 /* Initialize the structure. */
1270 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
1275 switch (whandle
->type
) {
1276 case DRM_API_HANDLE_TYPE_SHARED
:
1277 type
= amdgpu_bo_handle_type_gem_flink_name
;
1279 case DRM_API_HANDLE_TYPE_FD
:
1280 type
= amdgpu_bo_handle_type_dma_buf_fd
;
1286 r
= amdgpu_bo_import(ws
->dev
, type
, whandle
->handle
, &result
);
1290 /* Get initial domains. */
1291 r
= amdgpu_bo_query_info(result
.buf_handle
, &info
);
1295 r
= amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
1296 result
.alloc_size
, 1 << 20, 0, &va
, &va_handle
,
1297 AMDGPU_VA_RANGE_HIGH
);
1301 r
= amdgpu_bo_va_op(result
.buf_handle
, 0, result
.alloc_size
, va
, 0, AMDGPU_VA_OP_MAP
);
1305 if (info
.preferred_heap
& AMDGPU_GEM_DOMAIN_VRAM
)
1306 initial
|= RADEON_DOMAIN_VRAM
;
1307 if (info
.preferred_heap
& AMDGPU_GEM_DOMAIN_GTT
)
1308 initial
|= RADEON_DOMAIN_GTT
;
1311 pipe_reference_init(&bo
->base
.reference
, 1);
1312 bo
->base
.alignment
= info
.phys_alignment
;
1313 bo
->bo
= result
.buf_handle
;
1314 bo
->base
.size
= result
.alloc_size
;
1315 bo
->base
.vtbl
= &amdgpu_winsys_bo_vtbl
;
1318 bo
->u
.real
.va_handle
= va_handle
;
1319 bo
->initial_domain
= initial
;
1320 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
1321 bo
->is_shared
= true;
1324 *stride
= whandle
->stride
;
1326 *offset
= whandle
->offset
;
1328 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
1329 ws
->allocated_vram
+= align64(bo
->base
.size
, ws
->info
.gart_page_size
);
1330 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
1331 ws
->allocated_gtt
+= align64(bo
->base
.size
, ws
->info
.gart_page_size
);
1333 amdgpu_add_buffer_to_global_list(bo
);
1338 amdgpu_va_range_free(va_handle
);
1341 amdgpu_bo_free(result
.buf_handle
);
1348 static bool amdgpu_bo_get_handle(struct pb_buffer
*buffer
,
1349 unsigned stride
, unsigned offset
,
1350 unsigned slice_size
,
1351 struct winsys_handle
*whandle
)
1353 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(buffer
);
1354 enum amdgpu_bo_handle_type type
;
1357 /* Don't allow exports of slab entries and sparse buffers. */
1361 bo
->u
.real
.use_reusable_pool
= false;
1363 switch (whandle
->type
) {
1364 case DRM_API_HANDLE_TYPE_SHARED
:
1365 type
= amdgpu_bo_handle_type_gem_flink_name
;
1367 case DRM_API_HANDLE_TYPE_FD
:
1368 type
= amdgpu_bo_handle_type_dma_buf_fd
;
1370 case DRM_API_HANDLE_TYPE_KMS
:
1371 type
= amdgpu_bo_handle_type_kms
;
1377 r
= amdgpu_bo_export(bo
->bo
, type
, &whandle
->handle
);
1381 whandle
->stride
= stride
;
1382 whandle
->offset
= offset
;
1383 whandle
->offset
+= slice_size
* whandle
->layer
;
1384 bo
->is_shared
= true;
1388 static struct pb_buffer
*amdgpu_bo_from_ptr(struct radeon_winsys
*rws
,
1389 void *pointer
, uint64_t size
)
1391 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
1392 amdgpu_bo_handle buf_handle
;
1393 struct amdgpu_winsys_bo
*bo
;
1395 amdgpu_va_handle va_handle
;
1396 /* Avoid failure when the size is not page aligned */
1397 uint64_t aligned_size
= align64(size
, ws
->info
.gart_page_size
);
1399 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
1403 if (amdgpu_create_bo_from_user_mem(ws
->dev
, pointer
,
1404 aligned_size
, &buf_handle
))
1407 if (amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
1408 aligned_size
, 1 << 12, 0, &va
, &va_handle
,
1409 AMDGPU_VA_RANGE_HIGH
))
1410 goto error_va_alloc
;
1412 if (amdgpu_bo_va_op(buf_handle
, 0, aligned_size
, va
, 0, AMDGPU_VA_OP_MAP
))
1415 /* Initialize it. */
1416 pipe_reference_init(&bo
->base
.reference
, 1);
1417 bo
->bo
= buf_handle
;
1418 bo
->base
.alignment
= 0;
1419 bo
->base
.size
= size
;
1420 bo
->base
.vtbl
= &amdgpu_winsys_bo_vtbl
;
1422 bo
->user_ptr
= pointer
;
1424 bo
->u
.real
.va_handle
= va_handle
;
1425 bo
->initial_domain
= RADEON_DOMAIN_GTT
;
1426 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
1428 ws
->allocated_gtt
+= aligned_size
;
1430 amdgpu_add_buffer_to_global_list(bo
);
1432 return (struct pb_buffer
*)bo
;
1435 amdgpu_va_range_free(va_handle
);
1438 amdgpu_bo_free(buf_handle
);
1445 static bool amdgpu_bo_is_user_ptr(struct pb_buffer
*buf
)
1447 return ((struct amdgpu_winsys_bo
*)buf
)->user_ptr
!= NULL
;
1450 static bool amdgpu_bo_is_suballocated(struct pb_buffer
*buf
)
1452 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)buf
;
1454 return !bo
->bo
&& !bo
->sparse
;
1457 static uint64_t amdgpu_bo_get_va(struct pb_buffer
*buf
)
1459 return ((struct amdgpu_winsys_bo
*)buf
)->va
;
1462 void amdgpu_bo_init_functions(struct amdgpu_winsys
*ws
)
1464 ws
->base
.buffer_set_metadata
= amdgpu_buffer_set_metadata
;
1465 ws
->base
.buffer_get_metadata
= amdgpu_buffer_get_metadata
;
1466 ws
->base
.buffer_map
= amdgpu_bo_map
;
1467 ws
->base
.buffer_unmap
= amdgpu_bo_unmap
;
1468 ws
->base
.buffer_wait
= amdgpu_bo_wait
;
1469 ws
->base
.buffer_create
= amdgpu_bo_create
;
1470 ws
->base
.buffer_from_handle
= amdgpu_bo_from_handle
;
1471 ws
->base
.buffer_from_ptr
= amdgpu_bo_from_ptr
;
1472 ws
->base
.buffer_is_user_ptr
= amdgpu_bo_is_user_ptr
;
1473 ws
->base
.buffer_is_suballocated
= amdgpu_bo_is_suballocated
;
1474 ws
->base
.buffer_get_handle
= amdgpu_bo_get_handle
;
1475 ws
->base
.buffer_commit
= amdgpu_bo_sparse_commit
;
1476 ws
->base
.buffer_get_virtual_address
= amdgpu_bo_get_va
;
1477 ws
->base
.buffer_get_initial_domain
= amdgpu_bo_get_initial_domain
;