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 bool amdgpu_bo_wait(struct pb_buffer
*_buf
, uint64_t timeout
,
42 enum radeon_bo_usage usage
)
44 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
45 struct amdgpu_winsys
*ws
= bo
->ws
;
50 if (p_atomic_read(&bo
->num_active_ioctls
))
54 abs_timeout
= os_time_get_absolute_timeout(timeout
);
56 /* Wait if any ioctl is being submitted with this buffer. */
57 if (!os_wait_until_zero_abs_timeout(&bo
->num_active_ioctls
, abs_timeout
))
62 /* We can't use user fences for shared buffers, because user fences
63 * are local to this process only. If we want to wait for all buffer
64 * uses in all processes, we have to use amdgpu_bo_wait_for_idle.
66 bool buffer_busy
= true;
69 r
= amdgpu_bo_wait_for_idle(bo
->bo
, timeout
, &buffer_busy
);
71 fprintf(stderr
, "%s: amdgpu_bo_wait_for_idle failed %i\n", __func__
,
77 pipe_mutex_lock(ws
->bo_fence_lock
);
78 for (i
= 0; i
< RING_LAST
; i
++)
80 if (amdgpu_fence_wait(bo
->fence
[i
], 0, false)) {
81 /* Release the idle fence to avoid checking it again later. */
82 amdgpu_fence_reference(&bo
->fence
[i
], NULL
);
84 pipe_mutex_unlock(ws
->bo_fence_lock
);
88 pipe_mutex_unlock(ws
->bo_fence_lock
);
92 struct pipe_fence_handle
*fence
[RING_LAST
] = {};
93 bool fence_idle
[RING_LAST
] = {};
94 bool buffer_idle
= true;
96 /* Take references to all fences, so that we can wait for them
97 * without the lock. */
98 pipe_mutex_lock(ws
->bo_fence_lock
);
99 for (i
= 0; i
< RING_LAST
; i
++)
100 amdgpu_fence_reference(&fence
[i
], bo
->fence
[i
]);
101 pipe_mutex_unlock(ws
->bo_fence_lock
);
103 /* Now wait for the fences. */
104 for (i
= 0; i
< RING_LAST
; i
++) {
106 if (amdgpu_fence_wait(fence
[i
], abs_timeout
, true))
107 fence_idle
[i
] = true;
113 /* Release idle fences to avoid checking them again later. */
114 pipe_mutex_lock(ws
->bo_fence_lock
);
115 for (i
= 0; i
< RING_LAST
; i
++) {
116 if (fence
[i
] == bo
->fence
[i
] && fence_idle
[i
])
117 amdgpu_fence_reference(&bo
->fence
[i
], NULL
);
119 amdgpu_fence_reference(&fence
[i
], NULL
);
121 pipe_mutex_unlock(ws
->bo_fence_lock
);
127 static enum radeon_bo_domain
amdgpu_bo_get_initial_domain(
128 struct pb_buffer
*buf
)
130 return ((struct amdgpu_winsys_bo
*)buf
)->initial_domain
;
133 void amdgpu_bo_destroy(struct pb_buffer
*_buf
)
135 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
138 pipe_mutex_lock(bo
->ws
->global_bo_list_lock
);
139 LIST_DEL(&bo
->global_list_item
);
140 bo
->ws
->num_buffers
--;
141 pipe_mutex_unlock(bo
->ws
->global_bo_list_lock
);
143 amdgpu_bo_va_op(bo
->bo
, 0, bo
->base
.size
, bo
->va
, 0, AMDGPU_VA_OP_UNMAP
);
144 amdgpu_va_range_free(bo
->va_handle
);
145 amdgpu_bo_free(bo
->bo
);
147 for (i
= 0; i
< RING_LAST
; i
++)
148 amdgpu_fence_reference(&bo
->fence
[i
], NULL
);
150 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
151 bo
->ws
->allocated_vram
-= align64(bo
->base
.size
, bo
->ws
->info
.gart_page_size
);
152 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
153 bo
->ws
->allocated_gtt
-= align64(bo
->base
.size
, bo
->ws
->info
.gart_page_size
);
157 static void amdgpu_bo_destroy_or_cache(struct pb_buffer
*_buf
)
159 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
161 if (bo
->use_reusable_pool
)
162 pb_cache_add_buffer(&bo
->cache_entry
);
164 amdgpu_bo_destroy(_buf
);
167 static void *amdgpu_bo_map(struct pb_buffer
*buf
,
168 struct radeon_winsys_cs
*rcs
,
169 enum pipe_transfer_usage usage
)
171 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)buf
;
172 struct amdgpu_cs
*cs
= (struct amdgpu_cs
*)rcs
;
176 /* If it's not unsynchronized bo_map, flush CS if needed and then wait. */
177 if (!(usage
& PIPE_TRANSFER_UNSYNCHRONIZED
)) {
178 /* DONTBLOCK doesn't make sense with UNSYNCHRONIZED. */
179 if (usage
& PIPE_TRANSFER_DONTBLOCK
) {
180 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
183 * Since we are mapping for read, we don't need to wait
184 * if the GPU is using the buffer for read too
185 * (neither one is changing it).
187 * Only check whether the buffer is being used for write. */
188 if (cs
&& amdgpu_bo_is_referenced_by_cs_with_usage(cs
, bo
,
189 RADEON_USAGE_WRITE
)) {
190 cs
->flush_cs(cs
->flush_data
, RADEON_FLUSH_ASYNC
, NULL
);
194 if (!amdgpu_bo_wait((struct pb_buffer
*)bo
, 0,
195 RADEON_USAGE_WRITE
)) {
199 if (cs
&& amdgpu_bo_is_referenced_by_cs(cs
, bo
)) {
200 cs
->flush_cs(cs
->flush_data
, RADEON_FLUSH_ASYNC
, NULL
);
204 if (!amdgpu_bo_wait((struct pb_buffer
*)bo
, 0,
205 RADEON_USAGE_READWRITE
)) {
210 uint64_t time
= os_time_get_nano();
212 if (!(usage
& PIPE_TRANSFER_WRITE
)) {
215 * Since we are mapping for read, we don't need to wait
216 * if the GPU is using the buffer for read too
217 * (neither one is changing it).
219 * Only check whether the buffer is being used for write. */
220 if (cs
&& amdgpu_bo_is_referenced_by_cs_with_usage(cs
, bo
,
221 RADEON_USAGE_WRITE
)) {
222 cs
->flush_cs(cs
->flush_data
, 0, NULL
);
224 /* Try to avoid busy-waiting in amdgpu_bo_wait. */
225 if (p_atomic_read(&bo
->num_active_ioctls
))
226 amdgpu_cs_sync_flush(rcs
);
228 amdgpu_bo_wait((struct pb_buffer
*)bo
, PIPE_TIMEOUT_INFINITE
,
231 /* Mapping for write. */
233 if (amdgpu_bo_is_referenced_by_cs(cs
, bo
)) {
234 cs
->flush_cs(cs
->flush_data
, 0, NULL
);
236 /* Try to avoid busy-waiting in amdgpu_bo_wait. */
237 if (p_atomic_read(&bo
->num_active_ioctls
))
238 amdgpu_cs_sync_flush(rcs
);
242 amdgpu_bo_wait((struct pb_buffer
*)bo
, PIPE_TIMEOUT_INFINITE
,
243 RADEON_USAGE_READWRITE
);
246 bo
->ws
->buffer_wait_time
+= os_time_get_nano() - time
;
250 /* If the buffer is created from user memory, return the user pointer. */
254 r
= amdgpu_bo_cpu_map(bo
->bo
, &cpu
);
256 /* Clear the cache and try again. */
257 pb_cache_release_all_buffers(&bo
->ws
->bo_cache
);
258 r
= amdgpu_bo_cpu_map(bo
->bo
, &cpu
);
260 return r
? NULL
: cpu
;
263 static void amdgpu_bo_unmap(struct pb_buffer
*buf
)
265 struct amdgpu_winsys_bo
*bo
= (struct amdgpu_winsys_bo
*)buf
;
267 amdgpu_bo_cpu_unmap(bo
->bo
);
270 static const struct pb_vtbl amdgpu_winsys_bo_vtbl
= {
271 amdgpu_bo_destroy_or_cache
272 /* other functions are never called */
275 static void amdgpu_add_buffer_to_global_list(struct amdgpu_winsys_bo
*bo
)
277 struct amdgpu_winsys
*ws
= bo
->ws
;
279 pipe_mutex_lock(ws
->global_bo_list_lock
);
280 LIST_ADDTAIL(&bo
->global_list_item
, &ws
->global_bo_list
);
282 pipe_mutex_unlock(ws
->global_bo_list_lock
);
285 static struct amdgpu_winsys_bo
*amdgpu_create_bo(struct amdgpu_winsys
*ws
,
289 enum radeon_bo_domain initial_domain
,
292 struct amdgpu_bo_alloc_request request
= {0};
293 amdgpu_bo_handle buf_handle
;
295 struct amdgpu_winsys_bo
*bo
;
296 amdgpu_va_handle va_handle
;
297 unsigned va_gap_size
;
300 assert(initial_domain
& RADEON_DOMAIN_VRAM_GTT
);
301 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
306 pb_cache_init_entry(&ws
->bo_cache
, &bo
->cache_entry
, &bo
->base
);
307 request
.alloc_size
= size
;
308 request
.phys_alignment
= alignment
;
310 if (initial_domain
& RADEON_DOMAIN_VRAM
)
311 request
.preferred_heap
|= AMDGPU_GEM_DOMAIN_VRAM
;
312 if (initial_domain
& RADEON_DOMAIN_GTT
)
313 request
.preferred_heap
|= AMDGPU_GEM_DOMAIN_GTT
;
315 if (flags
& RADEON_FLAG_CPU_ACCESS
)
316 request
.flags
|= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
;
317 if (flags
& RADEON_FLAG_NO_CPU_ACCESS
)
318 request
.flags
|= AMDGPU_GEM_CREATE_NO_CPU_ACCESS
;
319 if (flags
& RADEON_FLAG_GTT_WC
)
320 request
.flags
|= AMDGPU_GEM_CREATE_CPU_GTT_USWC
;
322 r
= amdgpu_bo_alloc(ws
->dev
, &request
, &buf_handle
);
324 fprintf(stderr
, "amdgpu: Failed to allocate a buffer:\n");
325 fprintf(stderr
, "amdgpu: size : %"PRIu64
" bytes\n", size
);
326 fprintf(stderr
, "amdgpu: alignment : %u bytes\n", alignment
);
327 fprintf(stderr
, "amdgpu: domains : %u\n", initial_domain
);
331 va_gap_size
= ws
->check_vm
? MAX2(4 * alignment
, 64 * 1024) : 0;
332 r
= amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
333 size
+ va_gap_size
, alignment
, 0, &va
, &va_handle
, 0);
337 r
= amdgpu_bo_va_op(buf_handle
, 0, size
, va
, 0, AMDGPU_VA_OP_MAP
);
341 pipe_reference_init(&bo
->base
.reference
, 1);
342 bo
->base
.alignment
= alignment
;
343 bo
->base
.usage
= usage
;
344 bo
->base
.size
= size
;
345 bo
->base
.vtbl
= &amdgpu_winsys_bo_vtbl
;
349 bo
->va_handle
= va_handle
;
350 bo
->initial_domain
= initial_domain
;
351 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
353 if (initial_domain
& RADEON_DOMAIN_VRAM
)
354 ws
->allocated_vram
+= align64(size
, ws
->info
.gart_page_size
);
355 else if (initial_domain
& RADEON_DOMAIN_GTT
)
356 ws
->allocated_gtt
+= align64(size
, ws
->info
.gart_page_size
);
358 amdgpu_add_buffer_to_global_list(bo
);
363 amdgpu_va_range_free(va_handle
);
366 amdgpu_bo_free(buf_handle
);
373 bool amdgpu_bo_can_reclaim(struct pb_buffer
*_buf
)
375 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
377 if (amdgpu_bo_is_referenced_by_any_cs(bo
)) {
381 return amdgpu_bo_wait(_buf
, 0, RADEON_USAGE_READWRITE
);
384 static unsigned eg_tile_split(unsigned tile_split
)
386 switch (tile_split
) {
387 case 0: tile_split
= 64; break;
388 case 1: tile_split
= 128; break;
389 case 2: tile_split
= 256; break;
390 case 3: tile_split
= 512; break;
392 case 4: tile_split
= 1024; break;
393 case 5: tile_split
= 2048; break;
394 case 6: tile_split
= 4096; break;
399 static unsigned eg_tile_split_rev(unsigned eg_tile_split
)
401 switch (eg_tile_split
) {
413 static void amdgpu_buffer_get_metadata(struct pb_buffer
*_buf
,
414 struct radeon_bo_metadata
*md
)
416 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
417 struct amdgpu_bo_info info
= {0};
418 uint32_t tiling_flags
;
421 r
= amdgpu_bo_query_info(bo
->bo
, &info
);
425 tiling_flags
= info
.metadata
.tiling_info
;
427 md
->microtile
= RADEON_LAYOUT_LINEAR
;
428 md
->macrotile
= RADEON_LAYOUT_LINEAR
;
430 if (AMDGPU_TILING_GET(tiling_flags
, ARRAY_MODE
) == 4) /* 2D_TILED_THIN1 */
431 md
->macrotile
= RADEON_LAYOUT_TILED
;
432 else if (AMDGPU_TILING_GET(tiling_flags
, ARRAY_MODE
) == 2) /* 1D_TILED_THIN1 */
433 md
->microtile
= RADEON_LAYOUT_TILED
;
435 md
->pipe_config
= AMDGPU_TILING_GET(tiling_flags
, PIPE_CONFIG
);
436 md
->bankw
= 1 << AMDGPU_TILING_GET(tiling_flags
, BANK_WIDTH
);
437 md
->bankh
= 1 << AMDGPU_TILING_GET(tiling_flags
, BANK_HEIGHT
);
438 md
->tile_split
= eg_tile_split(AMDGPU_TILING_GET(tiling_flags
, TILE_SPLIT
));
439 md
->mtilea
= 1 << AMDGPU_TILING_GET(tiling_flags
, MACRO_TILE_ASPECT
);
440 md
->num_banks
= 2 << AMDGPU_TILING_GET(tiling_flags
, NUM_BANKS
);
441 md
->scanout
= AMDGPU_TILING_GET(tiling_flags
, MICRO_TILE_MODE
) == 0; /* DISPLAY */
443 md
->size_metadata
= info
.metadata
.size_metadata
;
444 memcpy(md
->metadata
, info
.metadata
.umd_metadata
, sizeof(md
->metadata
));
447 static void amdgpu_buffer_set_metadata(struct pb_buffer
*_buf
,
448 struct radeon_bo_metadata
*md
)
450 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(_buf
);
451 struct amdgpu_bo_metadata metadata
= {0};
452 uint32_t tiling_flags
= 0;
454 if (md
->macrotile
== RADEON_LAYOUT_TILED
)
455 tiling_flags
|= AMDGPU_TILING_SET(ARRAY_MODE
, 4); /* 2D_TILED_THIN1 */
456 else if (md
->microtile
== RADEON_LAYOUT_TILED
)
457 tiling_flags
|= AMDGPU_TILING_SET(ARRAY_MODE
, 2); /* 1D_TILED_THIN1 */
459 tiling_flags
|= AMDGPU_TILING_SET(ARRAY_MODE
, 1); /* LINEAR_ALIGNED */
461 tiling_flags
|= AMDGPU_TILING_SET(PIPE_CONFIG
, md
->pipe_config
);
462 tiling_flags
|= AMDGPU_TILING_SET(BANK_WIDTH
, util_logbase2(md
->bankw
));
463 tiling_flags
|= AMDGPU_TILING_SET(BANK_HEIGHT
, util_logbase2(md
->bankh
));
465 tiling_flags
|= AMDGPU_TILING_SET(TILE_SPLIT
, eg_tile_split_rev(md
->tile_split
));
466 tiling_flags
|= AMDGPU_TILING_SET(MACRO_TILE_ASPECT
, util_logbase2(md
->mtilea
));
467 tiling_flags
|= AMDGPU_TILING_SET(NUM_BANKS
, util_logbase2(md
->num_banks
)-1);
470 tiling_flags
|= AMDGPU_TILING_SET(MICRO_TILE_MODE
, 0); /* DISPLAY_MICRO_TILING */
472 tiling_flags
|= AMDGPU_TILING_SET(MICRO_TILE_MODE
, 1); /* THIN_MICRO_TILING */
474 metadata
.tiling_info
= tiling_flags
;
475 metadata
.size_metadata
= md
->size_metadata
;
476 memcpy(metadata
.umd_metadata
, md
->metadata
, sizeof(md
->metadata
));
478 amdgpu_bo_set_metadata(bo
->bo
, &metadata
);
481 static struct pb_buffer
*
482 amdgpu_bo_create(struct radeon_winsys
*rws
,
485 enum radeon_bo_domain domain
,
486 enum radeon_bo_flag flags
)
488 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
489 struct amdgpu_winsys_bo
*bo
;
492 /* Align size to page size. This is the minimum alignment for normal
493 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
494 * like constant/uniform buffers, can benefit from better and more reuse.
496 size
= align64(size
, ws
->info
.gart_page_size
);
497 alignment
= align(alignment
, ws
->info
.gart_page_size
);
499 /* Only set one usage bit each for domains and flags, or the cache manager
500 * might consider different sets of domains / flags compatible
502 if (domain
== RADEON_DOMAIN_VRAM_GTT
)
506 assert(flags
< sizeof(usage
) * 8 - 3);
507 usage
|= 1 << (flags
+ 3);
509 /* Get a buffer from the cache. */
510 bo
= (struct amdgpu_winsys_bo
*)
511 pb_cache_reclaim_buffer(&ws
->bo_cache
, size
, alignment
, usage
);
515 /* Create a new one. */
516 bo
= amdgpu_create_bo(ws
, size
, alignment
, usage
, domain
, flags
);
518 /* Clear the cache and try again. */
519 pb_cache_release_all_buffers(&ws
->bo_cache
);
520 bo
= amdgpu_create_bo(ws
, size
, alignment
, usage
, domain
, flags
);
525 bo
->use_reusable_pool
= true;
529 static struct pb_buffer
*amdgpu_bo_from_handle(struct radeon_winsys
*rws
,
530 struct winsys_handle
*whandle
,
534 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
535 struct amdgpu_winsys_bo
*bo
;
536 enum amdgpu_bo_handle_type type
;
537 struct amdgpu_bo_import_result result
= {0};
539 amdgpu_va_handle va_handle
;
540 struct amdgpu_bo_info info
= {0};
541 enum radeon_bo_domain initial
= 0;
544 /* Initialize the structure. */
545 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
550 switch (whandle
->type
) {
551 case DRM_API_HANDLE_TYPE_SHARED
:
552 type
= amdgpu_bo_handle_type_gem_flink_name
;
554 case DRM_API_HANDLE_TYPE_FD
:
555 type
= amdgpu_bo_handle_type_dma_buf_fd
;
561 r
= amdgpu_bo_import(ws
->dev
, type
, whandle
->handle
, &result
);
565 /* Get initial domains. */
566 r
= amdgpu_bo_query_info(result
.buf_handle
, &info
);
570 r
= amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
571 result
.alloc_size
, 1 << 20, 0, &va
, &va_handle
, 0);
575 r
= amdgpu_bo_va_op(result
.buf_handle
, 0, result
.alloc_size
, va
, 0, AMDGPU_VA_OP_MAP
);
579 if (info
.preferred_heap
& AMDGPU_GEM_DOMAIN_VRAM
)
580 initial
|= RADEON_DOMAIN_VRAM
;
581 if (info
.preferred_heap
& AMDGPU_GEM_DOMAIN_GTT
)
582 initial
|= RADEON_DOMAIN_GTT
;
585 pipe_reference_init(&bo
->base
.reference
, 1);
586 bo
->base
.alignment
= info
.phys_alignment
;
587 bo
->bo
= result
.buf_handle
;
588 bo
->base
.size
= result
.alloc_size
;
589 bo
->base
.vtbl
= &amdgpu_winsys_bo_vtbl
;
592 bo
->va_handle
= va_handle
;
593 bo
->initial_domain
= initial
;
594 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
595 bo
->is_shared
= true;
598 *stride
= whandle
->stride
;
600 *offset
= whandle
->offset
;
602 if (bo
->initial_domain
& RADEON_DOMAIN_VRAM
)
603 ws
->allocated_vram
+= align64(bo
->base
.size
, ws
->info
.gart_page_size
);
604 else if (bo
->initial_domain
& RADEON_DOMAIN_GTT
)
605 ws
->allocated_gtt
+= align64(bo
->base
.size
, ws
->info
.gart_page_size
);
607 amdgpu_add_buffer_to_global_list(bo
);
612 amdgpu_va_range_free(va_handle
);
615 amdgpu_bo_free(result
.buf_handle
);
622 static bool amdgpu_bo_get_handle(struct pb_buffer
*buffer
,
623 unsigned stride
, unsigned offset
,
625 struct winsys_handle
*whandle
)
627 struct amdgpu_winsys_bo
*bo
= amdgpu_winsys_bo(buffer
);
628 enum amdgpu_bo_handle_type type
;
631 bo
->use_reusable_pool
= false;
633 switch (whandle
->type
) {
634 case DRM_API_HANDLE_TYPE_SHARED
:
635 type
= amdgpu_bo_handle_type_gem_flink_name
;
637 case DRM_API_HANDLE_TYPE_FD
:
638 type
= amdgpu_bo_handle_type_dma_buf_fd
;
640 case DRM_API_HANDLE_TYPE_KMS
:
641 type
= amdgpu_bo_handle_type_kms
;
647 r
= amdgpu_bo_export(bo
->bo
, type
, &whandle
->handle
);
651 whandle
->stride
= stride
;
652 whandle
->offset
= offset
;
653 whandle
->offset
+= slice_size
* whandle
->layer
;
654 bo
->is_shared
= true;
658 static struct pb_buffer
*amdgpu_bo_from_ptr(struct radeon_winsys
*rws
,
659 void *pointer
, uint64_t size
)
661 struct amdgpu_winsys
*ws
= amdgpu_winsys(rws
);
662 amdgpu_bo_handle buf_handle
;
663 struct amdgpu_winsys_bo
*bo
;
665 amdgpu_va_handle va_handle
;
667 bo
= CALLOC_STRUCT(amdgpu_winsys_bo
);
671 if (amdgpu_create_bo_from_user_mem(ws
->dev
, pointer
, size
, &buf_handle
))
674 if (amdgpu_va_range_alloc(ws
->dev
, amdgpu_gpu_va_range_general
,
675 size
, 1 << 12, 0, &va
, &va_handle
, 0))
678 if (amdgpu_bo_va_op(buf_handle
, 0, size
, va
, 0, AMDGPU_VA_OP_MAP
))
682 pipe_reference_init(&bo
->base
.reference
, 1);
684 bo
->base
.alignment
= 0;
685 bo
->base
.size
= size
;
686 bo
->base
.vtbl
= &amdgpu_winsys_bo_vtbl
;
688 bo
->user_ptr
= pointer
;
690 bo
->va_handle
= va_handle
;
691 bo
->initial_domain
= RADEON_DOMAIN_GTT
;
692 bo
->unique_id
= __sync_fetch_and_add(&ws
->next_bo_unique_id
, 1);
694 ws
->allocated_gtt
+= align64(bo
->base
.size
, ws
->info
.gart_page_size
);
696 amdgpu_add_buffer_to_global_list(bo
);
698 return (struct pb_buffer
*)bo
;
701 amdgpu_va_range_free(va_handle
);
704 amdgpu_bo_free(buf_handle
);
711 static bool amdgpu_bo_is_user_ptr(struct pb_buffer
*buf
)
713 return ((struct amdgpu_winsys_bo
*)buf
)->user_ptr
!= NULL
;
716 static uint64_t amdgpu_bo_get_va(struct pb_buffer
*buf
)
718 return ((struct amdgpu_winsys_bo
*)buf
)->va
;
721 void amdgpu_bo_init_functions(struct amdgpu_winsys
*ws
)
723 ws
->base
.buffer_set_metadata
= amdgpu_buffer_set_metadata
;
724 ws
->base
.buffer_get_metadata
= amdgpu_buffer_get_metadata
;
725 ws
->base
.buffer_map
= amdgpu_bo_map
;
726 ws
->base
.buffer_unmap
= amdgpu_bo_unmap
;
727 ws
->base
.buffer_wait
= amdgpu_bo_wait
;
728 ws
->base
.buffer_create
= amdgpu_bo_create
;
729 ws
->base
.buffer_from_handle
= amdgpu_bo_from_handle
;
730 ws
->base
.buffer_from_ptr
= amdgpu_bo_from_ptr
;
731 ws
->base
.buffer_is_user_ptr
= amdgpu_bo_is_user_ptr
;
732 ws
->base
.buffer_get_handle
= amdgpu_bo_get_handle
;
733 ws
->base
.buffer_get_virtual_address
= amdgpu_bo_get_va
;
734 ws
->base
.buffer_get_initial_domain
= amdgpu_bo_get_initial_domain
;