gallium/radeon: do not reallocate user memory buffers
[mesa.git] / src / gallium / winsys / amdgpu / drm / amdgpu_bo.c
1 /*
2 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
3 * Copyright © 2015 Advanced Micro Devices, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
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.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27 /*
28 * Authors:
29 * Marek Olšák <maraeo@gmail.com>
30 */
31
32 #include "amdgpu_cs.h"
33
34 #include "os/os_time.h"
35 #include "state_tracker/drm_driver.h"
36 #include <amdgpu_drm.h>
37 #include <xf86drm.h>
38 #include <stdio.h>
39
40 static inline struct amdgpu_winsys_bo *amdgpu_winsys_bo(struct pb_buffer *bo)
41 {
42 return (struct amdgpu_winsys_bo *)bo;
43 }
44
45 static bool amdgpu_bo_wait(struct pb_buffer *_buf, uint64_t timeout,
46 enum radeon_bo_usage usage)
47 {
48 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
49 struct amdgpu_winsys *ws = bo->ws;
50 int i;
51
52 if (bo->is_shared) {
53 /* We can't use user fences for shared buffers, because user fences
54 * are local to this process only. If we want to wait for all buffer
55 * uses in all processes, we have to use amdgpu_bo_wait_for_idle.
56 */
57 bool buffer_busy = true;
58 int r;
59
60 r = amdgpu_bo_wait_for_idle(bo->bo, timeout, &buffer_busy);
61 if (r)
62 fprintf(stderr, "%s: amdgpu_bo_wait_for_idle failed %i\n", __func__,
63 r);
64 return !buffer_busy;
65 }
66
67 if (timeout == 0) {
68 /* Timeout == 0 is quite simple. */
69 pipe_mutex_lock(ws->bo_fence_lock);
70 for (i = 0; i < RING_LAST; i++)
71 if (bo->fence[i]) {
72 if (amdgpu_fence_wait(bo->fence[i], 0, false)) {
73 /* Release the idle fence to avoid checking it again later. */
74 amdgpu_fence_reference(&bo->fence[i], NULL);
75 } else {
76 pipe_mutex_unlock(ws->bo_fence_lock);
77 return false;
78 }
79 }
80 pipe_mutex_unlock(ws->bo_fence_lock);
81 return true;
82
83 } else {
84 struct pipe_fence_handle *fence[RING_LAST] = {};
85 bool fence_idle[RING_LAST] = {};
86 bool buffer_idle = true;
87 int64_t abs_timeout = os_time_get_absolute_timeout(timeout);
88
89 /* Take references to all fences, so that we can wait for them
90 * without the lock. */
91 pipe_mutex_lock(ws->bo_fence_lock);
92 for (i = 0; i < RING_LAST; i++)
93 amdgpu_fence_reference(&fence[i], bo->fence[i]);
94 pipe_mutex_unlock(ws->bo_fence_lock);
95
96 /* Now wait for the fences. */
97 for (i = 0; i < RING_LAST; i++) {
98 if (fence[i]) {
99 if (amdgpu_fence_wait(fence[i], abs_timeout, true))
100 fence_idle[i] = true;
101 else
102 buffer_idle = false;
103 }
104 }
105
106 /* Release idle fences to avoid checking them again later. */
107 pipe_mutex_lock(ws->bo_fence_lock);
108 for (i = 0; i < RING_LAST; i++) {
109 if (fence[i] == bo->fence[i] && fence_idle[i])
110 amdgpu_fence_reference(&bo->fence[i], NULL);
111
112 amdgpu_fence_reference(&fence[i], NULL);
113 }
114 pipe_mutex_unlock(ws->bo_fence_lock);
115
116 return buffer_idle;
117 }
118 }
119
120 static enum radeon_bo_domain amdgpu_bo_get_initial_domain(
121 struct pb_buffer *buf)
122 {
123 return ((struct amdgpu_winsys_bo*)buf)->initial_domain;
124 }
125
126 void amdgpu_bo_destroy(struct pb_buffer *_buf)
127 {
128 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
129 int i;
130
131 amdgpu_bo_va_op(bo->bo, 0, bo->base.size, bo->va, 0, AMDGPU_VA_OP_UNMAP);
132 amdgpu_va_range_free(bo->va_handle);
133 amdgpu_bo_free(bo->bo);
134
135 for (i = 0; i < RING_LAST; i++)
136 amdgpu_fence_reference(&bo->fence[i], NULL);
137
138 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
139 bo->ws->allocated_vram -= align(bo->base.size, bo->ws->gart_page_size);
140 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
141 bo->ws->allocated_gtt -= align(bo->base.size, bo->ws->gart_page_size);
142 FREE(bo);
143 }
144
145 static void amdgpu_bo_destroy_or_cache(struct pb_buffer *_buf)
146 {
147 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
148
149 if (bo->use_reusable_pool)
150 pb_cache_add_buffer(&bo->cache_entry);
151 else
152 amdgpu_bo_destroy(_buf);
153 }
154
155 static void *amdgpu_bo_map(struct pb_buffer *buf,
156 struct radeon_winsys_cs *rcs,
157 enum pipe_transfer_usage usage)
158 {
159 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
160 struct amdgpu_cs *cs = (struct amdgpu_cs*)rcs;
161 int r;
162 void *cpu = NULL;
163
164 /* If it's not unsynchronized bo_map, flush CS if needed and then wait. */
165 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
166 /* DONTBLOCK doesn't make sense with UNSYNCHRONIZED. */
167 if (usage & PIPE_TRANSFER_DONTBLOCK) {
168 if (!(usage & PIPE_TRANSFER_WRITE)) {
169 /* Mapping for read.
170 *
171 * Since we are mapping for read, we don't need to wait
172 * if the GPU is using the buffer for read too
173 * (neither one is changing it).
174 *
175 * Only check whether the buffer is being used for write. */
176 if (cs && amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo,
177 RADEON_USAGE_WRITE)) {
178 cs->flush_cs(cs->flush_data, RADEON_FLUSH_ASYNC, NULL);
179 return NULL;
180 }
181
182 if (!amdgpu_bo_wait((struct pb_buffer*)bo, 0,
183 RADEON_USAGE_WRITE)) {
184 return NULL;
185 }
186 } else {
187 if (cs && amdgpu_bo_is_referenced_by_cs(cs, bo)) {
188 cs->flush_cs(cs->flush_data, RADEON_FLUSH_ASYNC, NULL);
189 return NULL;
190 }
191
192 if (!amdgpu_bo_wait((struct pb_buffer*)bo, 0,
193 RADEON_USAGE_READWRITE)) {
194 return NULL;
195 }
196 }
197 } else {
198 uint64_t time = os_time_get_nano();
199
200 if (!(usage & PIPE_TRANSFER_WRITE)) {
201 /* Mapping for read.
202 *
203 * Since we are mapping for read, we don't need to wait
204 * if the GPU is using the buffer for read too
205 * (neither one is changing it).
206 *
207 * Only check whether the buffer is being used for write. */
208 if (cs && amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo,
209 RADEON_USAGE_WRITE)) {
210 cs->flush_cs(cs->flush_data, 0, NULL);
211 }
212 amdgpu_bo_wait((struct pb_buffer*)bo, PIPE_TIMEOUT_INFINITE,
213 RADEON_USAGE_WRITE);
214 } else {
215 /* Mapping for write. */
216 if (cs && amdgpu_bo_is_referenced_by_cs(cs, bo))
217 cs->flush_cs(cs->flush_data, 0, NULL);
218
219 amdgpu_bo_wait((struct pb_buffer*)bo, PIPE_TIMEOUT_INFINITE,
220 RADEON_USAGE_READWRITE);
221 }
222
223 bo->ws->buffer_wait_time += os_time_get_nano() - time;
224 }
225 }
226
227 /* If the buffer is created from user memory, return the user pointer. */
228 if (bo->user_ptr)
229 return bo->user_ptr;
230
231 r = amdgpu_bo_cpu_map(bo->bo, &cpu);
232 if (r) {
233 /* Clear the cache and try again. */
234 pb_cache_release_all_buffers(&bo->ws->bo_cache);
235 r = amdgpu_bo_cpu_map(bo->bo, &cpu);
236 }
237 return r ? NULL : cpu;
238 }
239
240 static void amdgpu_bo_unmap(struct pb_buffer *buf)
241 {
242 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
243
244 amdgpu_bo_cpu_unmap(bo->bo);
245 }
246
247 static const struct pb_vtbl amdgpu_winsys_bo_vtbl = {
248 amdgpu_bo_destroy_or_cache
249 /* other functions are never called */
250 };
251
252 static struct amdgpu_winsys_bo *amdgpu_create_bo(struct amdgpu_winsys *ws,
253 unsigned size,
254 unsigned alignment,
255 unsigned usage,
256 enum radeon_bo_domain initial_domain,
257 unsigned flags)
258 {
259 struct amdgpu_bo_alloc_request request = {0};
260 amdgpu_bo_handle buf_handle;
261 uint64_t va = 0;
262 struct amdgpu_winsys_bo *bo;
263 amdgpu_va_handle va_handle;
264 int r;
265
266 assert(initial_domain & RADEON_DOMAIN_VRAM_GTT);
267 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
268 if (!bo) {
269 return NULL;
270 }
271
272 pb_cache_init_entry(&ws->bo_cache, &bo->cache_entry, &bo->base);
273 request.alloc_size = size;
274 request.phys_alignment = alignment;
275
276 if (initial_domain & RADEON_DOMAIN_VRAM) {
277 request.preferred_heap |= AMDGPU_GEM_DOMAIN_VRAM;
278 if (flags & RADEON_FLAG_CPU_ACCESS)
279 request.flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
280 }
281 if (initial_domain & RADEON_DOMAIN_GTT) {
282 request.preferred_heap |= AMDGPU_GEM_DOMAIN_GTT;
283 if (flags & RADEON_FLAG_GTT_WC)
284 request.flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
285 }
286
287 r = amdgpu_bo_alloc(ws->dev, &request, &buf_handle);
288 if (r) {
289 fprintf(stderr, "amdgpu: Failed to allocate a buffer:\n");
290 fprintf(stderr, "amdgpu: size : %d bytes\n", size);
291 fprintf(stderr, "amdgpu: alignment : %d bytes\n", alignment);
292 fprintf(stderr, "amdgpu: domains : %d\n", initial_domain);
293 goto error_bo_alloc;
294 }
295
296 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
297 size, alignment, 0, &va, &va_handle, 0);
298 if (r)
299 goto error_va_alloc;
300
301 r = amdgpu_bo_va_op(buf_handle, 0, size, va, 0, AMDGPU_VA_OP_MAP);
302 if (r)
303 goto error_va_map;
304
305 pipe_reference_init(&bo->base.reference, 1);
306 bo->base.alignment = alignment;
307 bo->base.usage = usage;
308 bo->base.size = size;
309 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
310 bo->ws = ws;
311 bo->bo = buf_handle;
312 bo->va = va;
313 bo->va_handle = va_handle;
314 bo->initial_domain = initial_domain;
315 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
316
317 if (initial_domain & RADEON_DOMAIN_VRAM)
318 ws->allocated_vram += align(size, ws->gart_page_size);
319 else if (initial_domain & RADEON_DOMAIN_GTT)
320 ws->allocated_gtt += align(size, ws->gart_page_size);
321
322 return bo;
323
324 error_va_map:
325 amdgpu_va_range_free(va_handle);
326
327 error_va_alloc:
328 amdgpu_bo_free(buf_handle);
329
330 error_bo_alloc:
331 FREE(bo);
332 return NULL;
333 }
334
335 bool amdgpu_bo_can_reclaim(struct pb_buffer *_buf)
336 {
337 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
338
339 if (amdgpu_bo_is_referenced_by_any_cs(bo)) {
340 return false;
341 }
342
343 return amdgpu_bo_wait(_buf, 0, RADEON_USAGE_READWRITE);
344 }
345
346 static unsigned eg_tile_split(unsigned tile_split)
347 {
348 switch (tile_split) {
349 case 0: tile_split = 64; break;
350 case 1: tile_split = 128; break;
351 case 2: tile_split = 256; break;
352 case 3: tile_split = 512; break;
353 default:
354 case 4: tile_split = 1024; break;
355 case 5: tile_split = 2048; break;
356 case 6: tile_split = 4096; break;
357 }
358 return tile_split;
359 }
360
361 static unsigned eg_tile_split_rev(unsigned eg_tile_split)
362 {
363 switch (eg_tile_split) {
364 case 64: return 0;
365 case 128: return 1;
366 case 256: return 2;
367 case 512: return 3;
368 default:
369 case 1024: return 4;
370 case 2048: return 5;
371 case 4096: return 6;
372 }
373 }
374
375 static void amdgpu_bo_get_tiling(struct pb_buffer *_buf,
376 enum radeon_bo_layout *microtiled,
377 enum radeon_bo_layout *macrotiled,
378 unsigned *bankw, unsigned *bankh,
379 unsigned *tile_split,
380 unsigned *stencil_tile_split,
381 unsigned *mtilea,
382 bool *scanout)
383 {
384 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
385 struct amdgpu_bo_info info = {0};
386 uint32_t tiling_flags;
387 int r;
388
389 r = amdgpu_bo_query_info(bo->bo, &info);
390 if (r)
391 return;
392
393 tiling_flags = info.metadata.tiling_info;
394
395 *microtiled = RADEON_LAYOUT_LINEAR;
396 *macrotiled = RADEON_LAYOUT_LINEAR;
397
398 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 4) /* 2D_TILED_THIN1 */
399 *macrotiled = RADEON_LAYOUT_TILED;
400 else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 2) /* 1D_TILED_THIN1 */
401 *microtiled = RADEON_LAYOUT_TILED;
402
403 if (bankw && tile_split && mtilea && tile_split) {
404 *bankw = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
405 *bankh = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
406 *tile_split = eg_tile_split(AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT));
407 *mtilea = 1 << AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
408 }
409 if (scanout)
410 *scanout = AMDGPU_TILING_GET(tiling_flags, MICRO_TILE_MODE) == 0; /* DISPLAY */
411 }
412
413 static void amdgpu_bo_set_tiling(struct pb_buffer *_buf,
414 struct radeon_winsys_cs *rcs,
415 enum radeon_bo_layout microtiled,
416 enum radeon_bo_layout macrotiled,
417 unsigned pipe_config,
418 unsigned bankw, unsigned bankh,
419 unsigned tile_split,
420 unsigned stencil_tile_split,
421 unsigned mtilea, unsigned num_banks,
422 uint32_t pitch,
423 bool scanout)
424 {
425 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
426 struct amdgpu_bo_metadata metadata = {0};
427 uint32_t tiling_flags = 0;
428
429 if (macrotiled == RADEON_LAYOUT_TILED)
430 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 4); /* 2D_TILED_THIN1 */
431 else if (microtiled == RADEON_LAYOUT_TILED)
432 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 2); /* 1D_TILED_THIN1 */
433 else
434 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 1); /* LINEAR_ALIGNED */
435
436 tiling_flags |= AMDGPU_TILING_SET(PIPE_CONFIG, pipe_config);
437 tiling_flags |= AMDGPU_TILING_SET(BANK_WIDTH, util_logbase2(bankw));
438 tiling_flags |= AMDGPU_TILING_SET(BANK_HEIGHT, util_logbase2(bankh));
439 if (tile_split)
440 tiling_flags |= AMDGPU_TILING_SET(TILE_SPLIT, eg_tile_split_rev(tile_split));
441 tiling_flags |= AMDGPU_TILING_SET(MACRO_TILE_ASPECT, util_logbase2(mtilea));
442 tiling_flags |= AMDGPU_TILING_SET(NUM_BANKS, util_logbase2(num_banks)-1);
443
444 if (scanout)
445 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 0); /* DISPLAY_MICRO_TILING */
446 else
447 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 1); /* THIN_MICRO_TILING */
448
449 metadata.tiling_info = tiling_flags;
450
451 amdgpu_bo_set_metadata(bo->bo, &metadata);
452 }
453
454 static struct pb_buffer *
455 amdgpu_bo_create(struct radeon_winsys *rws,
456 unsigned size,
457 unsigned alignment,
458 boolean use_reusable_pool,
459 enum radeon_bo_domain domain,
460 enum radeon_bo_flag flags)
461 {
462 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
463 struct amdgpu_winsys_bo *bo;
464 unsigned usage = 0;
465
466 /* Don't use VRAM if the GPU doesn't have much. This is only the initial
467 * domain. The kernel is free to move the buffer if it wants to.
468 *
469 * 64MB means no VRAM by todays standards.
470 */
471 if (domain & RADEON_DOMAIN_VRAM && ws->info.vram_size <= 64*1024*1024) {
472 domain = RADEON_DOMAIN_GTT;
473 flags = RADEON_FLAG_GTT_WC;
474 }
475
476 /* Align size to page size. This is the minimum alignment for normal
477 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
478 * like constant/uniform buffers, can benefit from better and more reuse.
479 */
480 size = align(size, ws->gart_page_size);
481
482 /* Only set one usage bit each for domains and flags, or the cache manager
483 * might consider different sets of domains / flags compatible
484 */
485 if (domain == RADEON_DOMAIN_VRAM_GTT)
486 usage = 1 << 2;
487 else
488 usage = domain >> 1;
489 assert(flags < sizeof(usage) * 8 - 3);
490 usage |= 1 << (flags + 3);
491
492 /* Get a buffer from the cache. */
493 if (use_reusable_pool) {
494 bo = (struct amdgpu_winsys_bo*)
495 pb_cache_reclaim_buffer(&ws->bo_cache, size, alignment,
496 usage);
497 if (bo)
498 return &bo->base;
499 }
500
501 /* Create a new one. */
502 bo = amdgpu_create_bo(ws, size, alignment, usage, domain, flags);
503 if (!bo) {
504 /* Clear the cache and try again. */
505 pb_cache_release_all_buffers(&ws->bo_cache);
506 bo = amdgpu_create_bo(ws, size, alignment, usage, domain, flags);
507 if (!bo)
508 return NULL;
509 }
510
511 bo->use_reusable_pool = use_reusable_pool;
512 return &bo->base;
513 }
514
515 static struct pb_buffer *amdgpu_bo_from_handle(struct radeon_winsys *rws,
516 struct winsys_handle *whandle,
517 unsigned *stride)
518 {
519 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
520 struct amdgpu_winsys_bo *bo;
521 enum amdgpu_bo_handle_type type;
522 struct amdgpu_bo_import_result result = {0};
523 uint64_t va;
524 amdgpu_va_handle va_handle;
525 struct amdgpu_bo_info info = {0};
526 enum radeon_bo_domain initial = 0;
527 int r;
528
529 /* Initialize the structure. */
530 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
531 if (!bo) {
532 return NULL;
533 }
534
535 switch (whandle->type) {
536 case DRM_API_HANDLE_TYPE_SHARED:
537 type = amdgpu_bo_handle_type_gem_flink_name;
538 break;
539 case DRM_API_HANDLE_TYPE_FD:
540 type = amdgpu_bo_handle_type_dma_buf_fd;
541 break;
542 default:
543 return NULL;
544 }
545
546 r = amdgpu_bo_import(ws->dev, type, whandle->handle, &result);
547 if (r)
548 goto error;
549
550 /* Get initial domains. */
551 r = amdgpu_bo_query_info(result.buf_handle, &info);
552 if (r)
553 goto error_query;
554
555 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
556 result.alloc_size, 1 << 20, 0, &va, &va_handle, 0);
557 if (r)
558 goto error_query;
559
560 r = amdgpu_bo_va_op(result.buf_handle, 0, result.alloc_size, va, 0, AMDGPU_VA_OP_MAP);
561 if (r)
562 goto error_va_map;
563
564 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_VRAM)
565 initial |= RADEON_DOMAIN_VRAM;
566 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_GTT)
567 initial |= RADEON_DOMAIN_GTT;
568
569
570 pipe_reference_init(&bo->base.reference, 1);
571 bo->base.alignment = info.phys_alignment;
572 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
573 bo->bo = result.buf_handle;
574 bo->base.size = result.alloc_size;
575 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
576 bo->ws = ws;
577 bo->va = va;
578 bo->va_handle = va_handle;
579 bo->initial_domain = initial;
580 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
581 bo->is_shared = true;
582
583 if (stride)
584 *stride = whandle->stride;
585
586 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
587 ws->allocated_vram += align(bo->base.size, ws->gart_page_size);
588 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
589 ws->allocated_gtt += align(bo->base.size, ws->gart_page_size);
590
591 return &bo->base;
592
593 error_va_map:
594 amdgpu_va_range_free(va_handle);
595
596 error_query:
597 amdgpu_bo_free(result.buf_handle);
598
599 error:
600 FREE(bo);
601 return NULL;
602 }
603
604 static boolean amdgpu_bo_get_handle(struct pb_buffer *buffer,
605 unsigned stride,
606 struct winsys_handle *whandle)
607 {
608 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(buffer);
609 enum amdgpu_bo_handle_type type;
610 int r;
611
612 bo->use_reusable_pool = false;
613
614 switch (whandle->type) {
615 case DRM_API_HANDLE_TYPE_SHARED:
616 type = amdgpu_bo_handle_type_gem_flink_name;
617 break;
618 case DRM_API_HANDLE_TYPE_FD:
619 type = amdgpu_bo_handle_type_dma_buf_fd;
620 break;
621 case DRM_API_HANDLE_TYPE_KMS:
622 type = amdgpu_bo_handle_type_kms;
623 break;
624 default:
625 return FALSE;
626 }
627
628 r = amdgpu_bo_export(bo->bo, type, &whandle->handle);
629 if (r)
630 return FALSE;
631
632 whandle->stride = stride;
633 bo->is_shared = true;
634 return TRUE;
635 }
636
637 static struct pb_buffer *amdgpu_bo_from_ptr(struct radeon_winsys *rws,
638 void *pointer, unsigned size)
639 {
640 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
641 amdgpu_bo_handle buf_handle;
642 struct amdgpu_winsys_bo *bo;
643 uint64_t va;
644 amdgpu_va_handle va_handle;
645
646 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
647 if (!bo)
648 return NULL;
649
650 if (amdgpu_create_bo_from_user_mem(ws->dev, pointer, size, &buf_handle))
651 goto error;
652
653 if (amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
654 size, 1 << 12, 0, &va, &va_handle, 0))
655 goto error_va_alloc;
656
657 if (amdgpu_bo_va_op(buf_handle, 0, size, va, 0, AMDGPU_VA_OP_MAP))
658 goto error_va_map;
659
660 /* Initialize it. */
661 pipe_reference_init(&bo->base.reference, 1);
662 bo->bo = buf_handle;
663 bo->base.alignment = 0;
664 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
665 bo->base.size = size;
666 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
667 bo->ws = ws;
668 bo->user_ptr = pointer;
669 bo->va = va;
670 bo->va_handle = va_handle;
671 bo->initial_domain = RADEON_DOMAIN_GTT;
672 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
673
674 ws->allocated_gtt += align(bo->base.size, ws->gart_page_size);
675
676 return (struct pb_buffer*)bo;
677
678 error_va_map:
679 amdgpu_va_range_free(va_handle);
680
681 error_va_alloc:
682 amdgpu_bo_free(buf_handle);
683
684 error:
685 FREE(bo);
686 return NULL;
687 }
688
689 static bool amdgpu_bo_is_user_ptr(struct pb_buffer *buf)
690 {
691 return ((struct amdgpu_winsys_bo*)buf)->user_ptr != NULL;
692 }
693
694 static uint64_t amdgpu_bo_get_va(struct pb_buffer *buf)
695 {
696 return ((struct amdgpu_winsys_bo*)buf)->va;
697 }
698
699 void amdgpu_bo_init_functions(struct amdgpu_winsys *ws)
700 {
701 ws->base.buffer_set_tiling = amdgpu_bo_set_tiling;
702 ws->base.buffer_get_tiling = amdgpu_bo_get_tiling;
703 ws->base.buffer_map = amdgpu_bo_map;
704 ws->base.buffer_unmap = amdgpu_bo_unmap;
705 ws->base.buffer_wait = amdgpu_bo_wait;
706 ws->base.buffer_create = amdgpu_bo_create;
707 ws->base.buffer_from_handle = amdgpu_bo_from_handle;
708 ws->base.buffer_from_ptr = amdgpu_bo_from_ptr;
709 ws->base.buffer_is_user_ptr = amdgpu_bo_is_user_ptr;
710 ws->base.buffer_get_handle = amdgpu_bo_get_handle;
711 ws->base.buffer_get_virtual_address = amdgpu_bo_get_va;
712 ws->base.buffer_get_initial_domain = amdgpu_bo_get_initial_domain;
713 }