winsys/amdgpu: use pb_cache instead of pb_cache_manager
[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->rws;
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 radeon_winsys_cs_handle *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->rws->allocated_vram -= align(bo->base.size, bo->rws->gart_page_size);
140 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
141 bo->rws->allocated_gtt -= align(bo->base.size, bo->rws->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 radeon_winsys_cs_handle *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->rws->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 return r ? NULL : cpu;
233 }
234
235 static void amdgpu_bo_unmap(struct radeon_winsys_cs_handle *buf)
236 {
237 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
238
239 amdgpu_bo_cpu_unmap(bo->bo);
240 }
241
242 static const struct pb_vtbl amdgpu_winsys_bo_vtbl = {
243 amdgpu_bo_destroy_or_cache
244 /* other functions are never called */
245 };
246
247 static struct amdgpu_winsys_bo *amdgpu_create_bo(struct amdgpu_winsys *rws,
248 unsigned size,
249 unsigned alignment,
250 unsigned usage,
251 enum radeon_bo_domain initial_domain,
252 unsigned flags)
253 {
254 struct amdgpu_bo_alloc_request request = {0};
255 amdgpu_bo_handle buf_handle;
256 uint64_t va = 0;
257 struct amdgpu_winsys_bo *bo;
258 amdgpu_va_handle va_handle;
259 int r;
260
261 assert(initial_domain & RADEON_DOMAIN_VRAM_GTT);
262 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
263 if (!bo) {
264 return NULL;
265 }
266
267 pb_cache_init_entry(&rws->bo_cache, &bo->cache_entry, &bo->base);
268 request.alloc_size = size;
269 request.phys_alignment = alignment;
270
271 if (initial_domain & RADEON_DOMAIN_VRAM) {
272 request.preferred_heap |= AMDGPU_GEM_DOMAIN_VRAM;
273 if (flags & RADEON_FLAG_CPU_ACCESS)
274 request.flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
275 }
276 if (initial_domain & RADEON_DOMAIN_GTT) {
277 request.preferred_heap |= AMDGPU_GEM_DOMAIN_GTT;
278 if (flags & RADEON_FLAG_GTT_WC)
279 request.flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
280 }
281
282 r = amdgpu_bo_alloc(rws->dev, &request, &buf_handle);
283 if (r) {
284 fprintf(stderr, "amdgpu: Failed to allocate a buffer:\n");
285 fprintf(stderr, "amdgpu: size : %d bytes\n", size);
286 fprintf(stderr, "amdgpu: alignment : %d bytes\n", alignment);
287 fprintf(stderr, "amdgpu: domains : %d\n", initial_domain);
288 goto error_bo_alloc;
289 }
290
291 r = amdgpu_va_range_alloc(rws->dev, amdgpu_gpu_va_range_general,
292 size, alignment, 0, &va, &va_handle, 0);
293 if (r)
294 goto error_va_alloc;
295
296 r = amdgpu_bo_va_op(buf_handle, 0, size, va, 0, AMDGPU_VA_OP_MAP);
297 if (r)
298 goto error_va_map;
299
300 pipe_reference_init(&bo->base.reference, 1);
301 bo->base.alignment = alignment;
302 bo->base.usage = usage;
303 bo->base.size = size;
304 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
305 bo->rws = rws;
306 bo->bo = buf_handle;
307 bo->va = va;
308 bo->va_handle = va_handle;
309 bo->initial_domain = initial_domain;
310 bo->unique_id = __sync_fetch_and_add(&rws->next_bo_unique_id, 1);
311
312 if (initial_domain & RADEON_DOMAIN_VRAM)
313 rws->allocated_vram += align(size, rws->gart_page_size);
314 else if (initial_domain & RADEON_DOMAIN_GTT)
315 rws->allocated_gtt += align(size, rws->gart_page_size);
316
317 return bo;
318
319 error_va_map:
320 amdgpu_va_range_free(va_handle);
321
322 error_va_alloc:
323 amdgpu_bo_free(buf_handle);
324
325 error_bo_alloc:
326 FREE(bo);
327 return NULL;
328 }
329
330 bool amdgpu_bo_can_reclaim(struct pb_buffer *_buf)
331 {
332 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
333
334 if (amdgpu_bo_is_referenced_by_any_cs(bo)) {
335 return false;
336 }
337
338 return amdgpu_bo_wait(_buf, 0, RADEON_USAGE_READWRITE);
339 }
340
341 static unsigned eg_tile_split(unsigned tile_split)
342 {
343 switch (tile_split) {
344 case 0: tile_split = 64; break;
345 case 1: tile_split = 128; break;
346 case 2: tile_split = 256; break;
347 case 3: tile_split = 512; break;
348 default:
349 case 4: tile_split = 1024; break;
350 case 5: tile_split = 2048; break;
351 case 6: tile_split = 4096; break;
352 }
353 return tile_split;
354 }
355
356 static unsigned eg_tile_split_rev(unsigned eg_tile_split)
357 {
358 switch (eg_tile_split) {
359 case 64: return 0;
360 case 128: return 1;
361 case 256: return 2;
362 case 512: return 3;
363 default:
364 case 1024: return 4;
365 case 2048: return 5;
366 case 4096: return 6;
367 }
368 }
369
370 static void amdgpu_bo_get_tiling(struct pb_buffer *_buf,
371 enum radeon_bo_layout *microtiled,
372 enum radeon_bo_layout *macrotiled,
373 unsigned *bankw, unsigned *bankh,
374 unsigned *tile_split,
375 unsigned *stencil_tile_split,
376 unsigned *mtilea,
377 bool *scanout)
378 {
379 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
380 struct amdgpu_bo_info info = {0};
381 uint32_t tiling_flags;
382 int r;
383
384 r = amdgpu_bo_query_info(bo->bo, &info);
385 if (r)
386 return;
387
388 tiling_flags = info.metadata.tiling_info;
389
390 *microtiled = RADEON_LAYOUT_LINEAR;
391 *macrotiled = RADEON_LAYOUT_LINEAR;
392
393 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 4) /* 2D_TILED_THIN1 */
394 *macrotiled = RADEON_LAYOUT_TILED;
395 else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 2) /* 1D_TILED_THIN1 */
396 *microtiled = RADEON_LAYOUT_TILED;
397
398 if (bankw && tile_split && mtilea && tile_split) {
399 *bankw = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
400 *bankh = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
401 *tile_split = eg_tile_split(AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT));
402 *mtilea = 1 << AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
403 }
404 if (scanout)
405 *scanout = AMDGPU_TILING_GET(tiling_flags, MICRO_TILE_MODE) == 0; /* DISPLAY */
406 }
407
408 static void amdgpu_bo_set_tiling(struct pb_buffer *_buf,
409 struct radeon_winsys_cs *rcs,
410 enum radeon_bo_layout microtiled,
411 enum radeon_bo_layout macrotiled,
412 unsigned pipe_config,
413 unsigned bankw, unsigned bankh,
414 unsigned tile_split,
415 unsigned stencil_tile_split,
416 unsigned mtilea, unsigned num_banks,
417 uint32_t pitch,
418 bool scanout)
419 {
420 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
421 struct amdgpu_bo_metadata metadata = {0};
422 uint32_t tiling_flags = 0;
423
424 if (macrotiled == RADEON_LAYOUT_TILED)
425 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 4); /* 2D_TILED_THIN1 */
426 else if (microtiled == RADEON_LAYOUT_TILED)
427 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 2); /* 1D_TILED_THIN1 */
428 else
429 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 1); /* LINEAR_ALIGNED */
430
431 tiling_flags |= AMDGPU_TILING_SET(PIPE_CONFIG, pipe_config);
432 tiling_flags |= AMDGPU_TILING_SET(BANK_WIDTH, util_logbase2(bankw));
433 tiling_flags |= AMDGPU_TILING_SET(BANK_HEIGHT, util_logbase2(bankh));
434 if (tile_split)
435 tiling_flags |= AMDGPU_TILING_SET(TILE_SPLIT, eg_tile_split_rev(tile_split));
436 tiling_flags |= AMDGPU_TILING_SET(MACRO_TILE_ASPECT, util_logbase2(mtilea));
437 tiling_flags |= AMDGPU_TILING_SET(NUM_BANKS, util_logbase2(num_banks)-1);
438
439 if (scanout)
440 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 0); /* DISPLAY_MICRO_TILING */
441 else
442 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 1); /* THIN_MICRO_TILING */
443
444 metadata.tiling_info = tiling_flags;
445
446 amdgpu_bo_set_metadata(bo->bo, &metadata);
447 }
448
449 static struct radeon_winsys_cs_handle *amdgpu_get_cs_handle(struct pb_buffer *_buf)
450 {
451 /* return a direct pointer to amdgpu_winsys_bo. */
452 return (struct radeon_winsys_cs_handle*)_buf;
453 }
454
455 static struct pb_buffer *
456 amdgpu_bo_create(struct radeon_winsys *rws,
457 unsigned size,
458 unsigned alignment,
459 boolean use_reusable_pool,
460 enum radeon_bo_domain domain,
461 enum radeon_bo_flag flags)
462 {
463 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
464 struct amdgpu_winsys_bo *bo;
465 unsigned usage = 0;
466
467 /* Don't use VRAM if the GPU doesn't have much. This is only the initial
468 * domain. The kernel is free to move the buffer if it wants to.
469 *
470 * 64MB means no VRAM by todays standards.
471 */
472 if (domain & RADEON_DOMAIN_VRAM && ws->info.vram_size <= 64*1024*1024) {
473 domain = RADEON_DOMAIN_GTT;
474 flags = RADEON_FLAG_GTT_WC;
475 }
476
477 /* Align size to page size. This is the minimum alignment for normal
478 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
479 * like constant/uniform buffers, can benefit from better and more reuse.
480 */
481 size = align(size, ws->gart_page_size);
482
483 /* Only set one usage bit each for domains and flags, or the cache manager
484 * might consider different sets of domains / flags compatible
485 */
486 if (domain == RADEON_DOMAIN_VRAM_GTT)
487 usage = 1 << 2;
488 else
489 usage = domain >> 1;
490 assert(flags < sizeof(usage) * 8 - 3);
491 usage |= 1 << (flags + 3);
492
493 /* Get a buffer from the cache. */
494 if (use_reusable_pool) {
495 bo = (struct amdgpu_winsys_bo*)
496 pb_cache_reclaim_buffer(&ws->bo_cache, size, alignment,
497 usage);
498 if (bo)
499 return &bo->base;
500 }
501
502 /* Create a new one. */
503 bo = amdgpu_create_bo(ws, size, alignment, usage, domain, flags);
504 if (!bo)
505 return NULL;
506
507 bo->use_reusable_pool = use_reusable_pool;
508 return &bo->base;
509 }
510
511 static struct pb_buffer *amdgpu_bo_from_handle(struct radeon_winsys *rws,
512 struct winsys_handle *whandle,
513 unsigned *stride)
514 {
515 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
516 struct amdgpu_winsys_bo *bo;
517 enum amdgpu_bo_handle_type type;
518 struct amdgpu_bo_import_result result = {0};
519 uint64_t va;
520 amdgpu_va_handle va_handle;
521 struct amdgpu_bo_info info = {0};
522 enum radeon_bo_domain initial = 0;
523 int r;
524
525 /* Initialize the structure. */
526 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
527 if (!bo) {
528 return NULL;
529 }
530
531 switch (whandle->type) {
532 case DRM_API_HANDLE_TYPE_SHARED:
533 type = amdgpu_bo_handle_type_gem_flink_name;
534 break;
535 case DRM_API_HANDLE_TYPE_FD:
536 type = amdgpu_bo_handle_type_dma_buf_fd;
537 break;
538 default:
539 return NULL;
540 }
541
542 r = amdgpu_bo_import(ws->dev, type, whandle->handle, &result);
543 if (r)
544 goto error;
545
546 /* Get initial domains. */
547 r = amdgpu_bo_query_info(result.buf_handle, &info);
548 if (r)
549 goto error_query;
550
551 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
552 result.alloc_size, 1 << 20, 0, &va, &va_handle, 0);
553 if (r)
554 goto error_query;
555
556 r = amdgpu_bo_va_op(result.buf_handle, 0, result.alloc_size, va, 0, AMDGPU_VA_OP_MAP);
557 if (r)
558 goto error_va_map;
559
560 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_VRAM)
561 initial |= RADEON_DOMAIN_VRAM;
562 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_GTT)
563 initial |= RADEON_DOMAIN_GTT;
564
565
566 pipe_reference_init(&bo->base.reference, 1);
567 bo->base.alignment = info.phys_alignment;
568 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
569 bo->bo = result.buf_handle;
570 bo->base.size = result.alloc_size;
571 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
572 bo->rws = ws;
573 bo->va = va;
574 bo->va_handle = va_handle;
575 bo->initial_domain = initial;
576 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
577 bo->is_shared = true;
578
579 if (stride)
580 *stride = whandle->stride;
581
582 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
583 ws->allocated_vram += align(bo->base.size, ws->gart_page_size);
584 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
585 ws->allocated_gtt += align(bo->base.size, ws->gart_page_size);
586
587 return &bo->base;
588
589 error_va_map:
590 amdgpu_va_range_free(va_handle);
591
592 error_query:
593 amdgpu_bo_free(result.buf_handle);
594
595 error:
596 FREE(bo);
597 return NULL;
598 }
599
600 static boolean amdgpu_bo_get_handle(struct pb_buffer *buffer,
601 unsigned stride,
602 struct winsys_handle *whandle)
603 {
604 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(buffer);
605 enum amdgpu_bo_handle_type type;
606 int r;
607
608 bo->use_reusable_pool = false;
609
610 switch (whandle->type) {
611 case DRM_API_HANDLE_TYPE_SHARED:
612 type = amdgpu_bo_handle_type_gem_flink_name;
613 break;
614 case DRM_API_HANDLE_TYPE_FD:
615 type = amdgpu_bo_handle_type_dma_buf_fd;
616 break;
617 case DRM_API_HANDLE_TYPE_KMS:
618 type = amdgpu_bo_handle_type_kms;
619 break;
620 default:
621 return FALSE;
622 }
623
624 r = amdgpu_bo_export(bo->bo, type, &whandle->handle);
625 if (r)
626 return FALSE;
627
628 whandle->stride = stride;
629 bo->is_shared = true;
630 return TRUE;
631 }
632
633 static struct pb_buffer *amdgpu_bo_from_ptr(struct radeon_winsys *rws,
634 void *pointer, unsigned size)
635 {
636 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
637 amdgpu_bo_handle buf_handle;
638 struct amdgpu_winsys_bo *bo;
639 uint64_t va;
640 amdgpu_va_handle va_handle;
641
642 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
643 if (!bo)
644 return NULL;
645
646 if (amdgpu_create_bo_from_user_mem(ws->dev, pointer, size, &buf_handle))
647 goto error;
648
649 if (amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
650 size, 1 << 12, 0, &va, &va_handle, 0))
651 goto error_va_alloc;
652
653 if (amdgpu_bo_va_op(buf_handle, 0, size, va, 0, AMDGPU_VA_OP_MAP))
654 goto error_va_map;
655
656 /* Initialize it. */
657 pipe_reference_init(&bo->base.reference, 1);
658 bo->bo = buf_handle;
659 bo->base.alignment = 0;
660 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
661 bo->base.size = size;
662 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
663 bo->rws = ws;
664 bo->user_ptr = pointer;
665 bo->va = va;
666 bo->va_handle = va_handle;
667 bo->initial_domain = RADEON_DOMAIN_GTT;
668 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
669
670 ws->allocated_gtt += align(bo->base.size, ws->gart_page_size);
671
672 return (struct pb_buffer*)bo;
673
674 error_va_map:
675 amdgpu_va_range_free(va_handle);
676
677 error_va_alloc:
678 amdgpu_bo_free(buf_handle);
679
680 error:
681 FREE(bo);
682 return NULL;
683 }
684
685 static uint64_t amdgpu_bo_get_va(struct radeon_winsys_cs_handle *buf)
686 {
687 return ((struct amdgpu_winsys_bo*)buf)->va;
688 }
689
690 void amdgpu_bo_init_functions(struct amdgpu_winsys *ws)
691 {
692 ws->base.buffer_get_cs_handle = amdgpu_get_cs_handle;
693 ws->base.buffer_set_tiling = amdgpu_bo_set_tiling;
694 ws->base.buffer_get_tiling = amdgpu_bo_get_tiling;
695 ws->base.buffer_map = amdgpu_bo_map;
696 ws->base.buffer_unmap = amdgpu_bo_unmap;
697 ws->base.buffer_wait = amdgpu_bo_wait;
698 ws->base.buffer_create = amdgpu_bo_create;
699 ws->base.buffer_from_handle = amdgpu_bo_from_handle;
700 ws->base.buffer_from_ptr = amdgpu_bo_from_ptr;
701 ws->base.buffer_get_handle = amdgpu_bo_get_handle;
702 ws->base.buffer_get_virtual_address = amdgpu_bo_get_va;
703 ws->base.buffer_get_initial_domain = amdgpu_bo_get_initial_domain;
704 }