90f3a9f35d0be53f411ec370562ee7df1a6ee586
[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 return r ? NULL : cpu;
233 }
234
235 static void amdgpu_bo_unmap(struct pb_buffer *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 *ws,
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(&ws->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(ws->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(ws->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->ws = ws;
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(&ws->next_bo_unique_id, 1);
311
312 if (initial_domain & RADEON_DOMAIN_VRAM)
313 ws->allocated_vram += align(size, ws->gart_page_size);
314 else if (initial_domain & RADEON_DOMAIN_GTT)
315 ws->allocated_gtt += align(size, ws->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 pb_buffer *
450 amdgpu_bo_create(struct radeon_winsys *rws,
451 unsigned size,
452 unsigned alignment,
453 boolean use_reusable_pool,
454 enum radeon_bo_domain domain,
455 enum radeon_bo_flag flags)
456 {
457 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
458 struct amdgpu_winsys_bo *bo;
459 unsigned usage = 0;
460
461 /* Don't use VRAM if the GPU doesn't have much. This is only the initial
462 * domain. The kernel is free to move the buffer if it wants to.
463 *
464 * 64MB means no VRAM by todays standards.
465 */
466 if (domain & RADEON_DOMAIN_VRAM && ws->info.vram_size <= 64*1024*1024) {
467 domain = RADEON_DOMAIN_GTT;
468 flags = RADEON_FLAG_GTT_WC;
469 }
470
471 /* Align size to page size. This is the minimum alignment for normal
472 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
473 * like constant/uniform buffers, can benefit from better and more reuse.
474 */
475 size = align(size, ws->gart_page_size);
476
477 /* Only set one usage bit each for domains and flags, or the cache manager
478 * might consider different sets of domains / flags compatible
479 */
480 if (domain == RADEON_DOMAIN_VRAM_GTT)
481 usage = 1 << 2;
482 else
483 usage = domain >> 1;
484 assert(flags < sizeof(usage) * 8 - 3);
485 usage |= 1 << (flags + 3);
486
487 /* Get a buffer from the cache. */
488 if (use_reusable_pool) {
489 bo = (struct amdgpu_winsys_bo*)
490 pb_cache_reclaim_buffer(&ws->bo_cache, size, alignment,
491 usage);
492 if (bo)
493 return &bo->base;
494 }
495
496 /* Create a new one. */
497 bo = amdgpu_create_bo(ws, size, alignment, usage, domain, flags);
498 if (!bo) {
499 /* Clear the cache and try again. */
500 pb_cache_release_all_buffers(&ws->bo_cache);
501 bo = amdgpu_create_bo(ws, size, alignment, usage, domain, flags);
502 if (!bo)
503 return NULL;
504 }
505
506 bo->use_reusable_pool = use_reusable_pool;
507 return &bo->base;
508 }
509
510 static struct pb_buffer *amdgpu_bo_from_handle(struct radeon_winsys *rws,
511 struct winsys_handle *whandle,
512 unsigned *stride)
513 {
514 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
515 struct amdgpu_winsys_bo *bo;
516 enum amdgpu_bo_handle_type type;
517 struct amdgpu_bo_import_result result = {0};
518 uint64_t va;
519 amdgpu_va_handle va_handle;
520 struct amdgpu_bo_info info = {0};
521 enum radeon_bo_domain initial = 0;
522 int r;
523
524 /* Initialize the structure. */
525 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
526 if (!bo) {
527 return NULL;
528 }
529
530 switch (whandle->type) {
531 case DRM_API_HANDLE_TYPE_SHARED:
532 type = amdgpu_bo_handle_type_gem_flink_name;
533 break;
534 case DRM_API_HANDLE_TYPE_FD:
535 type = amdgpu_bo_handle_type_dma_buf_fd;
536 break;
537 default:
538 return NULL;
539 }
540
541 r = amdgpu_bo_import(ws->dev, type, whandle->handle, &result);
542 if (r)
543 goto error;
544
545 /* Get initial domains. */
546 r = amdgpu_bo_query_info(result.buf_handle, &info);
547 if (r)
548 goto error_query;
549
550 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
551 result.alloc_size, 1 << 20, 0, &va, &va_handle, 0);
552 if (r)
553 goto error_query;
554
555 r = amdgpu_bo_va_op(result.buf_handle, 0, result.alloc_size, va, 0, AMDGPU_VA_OP_MAP);
556 if (r)
557 goto error_va_map;
558
559 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_VRAM)
560 initial |= RADEON_DOMAIN_VRAM;
561 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_GTT)
562 initial |= RADEON_DOMAIN_GTT;
563
564
565 pipe_reference_init(&bo->base.reference, 1);
566 bo->base.alignment = info.phys_alignment;
567 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
568 bo->bo = result.buf_handle;
569 bo->base.size = result.alloc_size;
570 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
571 bo->ws = ws;
572 bo->va = va;
573 bo->va_handle = va_handle;
574 bo->initial_domain = initial;
575 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
576 bo->is_shared = true;
577
578 if (stride)
579 *stride = whandle->stride;
580
581 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
582 ws->allocated_vram += align(bo->base.size, ws->gart_page_size);
583 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
584 ws->allocated_gtt += align(bo->base.size, ws->gart_page_size);
585
586 return &bo->base;
587
588 error_va_map:
589 amdgpu_va_range_free(va_handle);
590
591 error_query:
592 amdgpu_bo_free(result.buf_handle);
593
594 error:
595 FREE(bo);
596 return NULL;
597 }
598
599 static boolean amdgpu_bo_get_handle(struct pb_buffer *buffer,
600 unsigned stride,
601 struct winsys_handle *whandle)
602 {
603 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(buffer);
604 enum amdgpu_bo_handle_type type;
605 int r;
606
607 bo->use_reusable_pool = false;
608
609 switch (whandle->type) {
610 case DRM_API_HANDLE_TYPE_SHARED:
611 type = amdgpu_bo_handle_type_gem_flink_name;
612 break;
613 case DRM_API_HANDLE_TYPE_FD:
614 type = amdgpu_bo_handle_type_dma_buf_fd;
615 break;
616 case DRM_API_HANDLE_TYPE_KMS:
617 type = amdgpu_bo_handle_type_kms;
618 break;
619 default:
620 return FALSE;
621 }
622
623 r = amdgpu_bo_export(bo->bo, type, &whandle->handle);
624 if (r)
625 return FALSE;
626
627 whandle->stride = stride;
628 bo->is_shared = true;
629 return TRUE;
630 }
631
632 static struct pb_buffer *amdgpu_bo_from_ptr(struct radeon_winsys *rws,
633 void *pointer, unsigned size)
634 {
635 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
636 amdgpu_bo_handle buf_handle;
637 struct amdgpu_winsys_bo *bo;
638 uint64_t va;
639 amdgpu_va_handle va_handle;
640
641 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
642 if (!bo)
643 return NULL;
644
645 if (amdgpu_create_bo_from_user_mem(ws->dev, pointer, size, &buf_handle))
646 goto error;
647
648 if (amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
649 size, 1 << 12, 0, &va, &va_handle, 0))
650 goto error_va_alloc;
651
652 if (amdgpu_bo_va_op(buf_handle, 0, size, va, 0, AMDGPU_VA_OP_MAP))
653 goto error_va_map;
654
655 /* Initialize it. */
656 pipe_reference_init(&bo->base.reference, 1);
657 bo->bo = buf_handle;
658 bo->base.alignment = 0;
659 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
660 bo->base.size = size;
661 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
662 bo->ws = ws;
663 bo->user_ptr = pointer;
664 bo->va = va;
665 bo->va_handle = va_handle;
666 bo->initial_domain = RADEON_DOMAIN_GTT;
667 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
668
669 ws->allocated_gtt += align(bo->base.size, ws->gart_page_size);
670
671 return (struct pb_buffer*)bo;
672
673 error_va_map:
674 amdgpu_va_range_free(va_handle);
675
676 error_va_alloc:
677 amdgpu_bo_free(buf_handle);
678
679 error:
680 FREE(bo);
681 return NULL;
682 }
683
684 static uint64_t amdgpu_bo_get_va(struct pb_buffer *buf)
685 {
686 return ((struct amdgpu_winsys_bo*)buf)->va;
687 }
688
689 void amdgpu_bo_init_functions(struct amdgpu_winsys *ws)
690 {
691 ws->base.buffer_set_tiling = amdgpu_bo_set_tiling;
692 ws->base.buffer_get_tiling = amdgpu_bo_get_tiling;
693 ws->base.buffer_map = amdgpu_bo_map;
694 ws->base.buffer_unmap = amdgpu_bo_unmap;
695 ws->base.buffer_wait = amdgpu_bo_wait;
696 ws->base.buffer_create = amdgpu_bo_create;
697 ws->base.buffer_from_handle = amdgpu_bo_from_handle;
698 ws->base.buffer_from_ptr = amdgpu_bo_from_ptr;
699 ws->base.buffer_get_handle = amdgpu_bo_get_handle;
700 ws->base.buffer_get_virtual_address = amdgpu_bo_get_va;
701 ws->base.buffer_get_initial_domain = amdgpu_bo_get_initial_domain;
702 }