winsys/amdgpu: Process RADEON_FLAG_* independently from RADEON_DOMAIN_*
[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 pipe_mutex_lock(bo->ws->global_bo_list_lock);
132 LIST_DEL(&bo->global_list_item);
133 bo->ws->num_buffers--;
134 pipe_mutex_unlock(bo->ws->global_bo_list_lock);
135
136 amdgpu_bo_va_op(bo->bo, 0, bo->base.size, bo->va, 0, AMDGPU_VA_OP_UNMAP);
137 amdgpu_va_range_free(bo->va_handle);
138 amdgpu_bo_free(bo->bo);
139
140 for (i = 0; i < RING_LAST; i++)
141 amdgpu_fence_reference(&bo->fence[i], NULL);
142
143 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
144 bo->ws->allocated_vram -= align(bo->base.size, bo->ws->gart_page_size);
145 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
146 bo->ws->allocated_gtt -= align(bo->base.size, bo->ws->gart_page_size);
147 FREE(bo);
148 }
149
150 static void amdgpu_bo_destroy_or_cache(struct pb_buffer *_buf)
151 {
152 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
153
154 if (bo->use_reusable_pool)
155 pb_cache_add_buffer(&bo->cache_entry);
156 else
157 amdgpu_bo_destroy(_buf);
158 }
159
160 static void *amdgpu_bo_map(struct pb_buffer *buf,
161 struct radeon_winsys_cs *rcs,
162 enum pipe_transfer_usage usage)
163 {
164 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
165 struct amdgpu_cs *cs = (struct amdgpu_cs*)rcs;
166 int r;
167 void *cpu = NULL;
168
169 /* If it's not unsynchronized bo_map, flush CS if needed and then wait. */
170 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
171 /* DONTBLOCK doesn't make sense with UNSYNCHRONIZED. */
172 if (usage & PIPE_TRANSFER_DONTBLOCK) {
173 if (!(usage & PIPE_TRANSFER_WRITE)) {
174 /* Mapping for read.
175 *
176 * Since we are mapping for read, we don't need to wait
177 * if the GPU is using the buffer for read too
178 * (neither one is changing it).
179 *
180 * Only check whether the buffer is being used for write. */
181 if (cs && amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo,
182 RADEON_USAGE_WRITE)) {
183 cs->flush_cs(cs->flush_data, RADEON_FLUSH_ASYNC, NULL);
184 return NULL;
185 }
186
187 if (!amdgpu_bo_wait((struct pb_buffer*)bo, 0,
188 RADEON_USAGE_WRITE)) {
189 return NULL;
190 }
191 } else {
192 if (cs && amdgpu_bo_is_referenced_by_cs(cs, bo)) {
193 cs->flush_cs(cs->flush_data, RADEON_FLUSH_ASYNC, NULL);
194 return NULL;
195 }
196
197 if (!amdgpu_bo_wait((struct pb_buffer*)bo, 0,
198 RADEON_USAGE_READWRITE)) {
199 return NULL;
200 }
201 }
202 } else {
203 uint64_t time = os_time_get_nano();
204
205 if (!(usage & PIPE_TRANSFER_WRITE)) {
206 /* Mapping for read.
207 *
208 * Since we are mapping for read, we don't need to wait
209 * if the GPU is using the buffer for read too
210 * (neither one is changing it).
211 *
212 * Only check whether the buffer is being used for write. */
213 if (cs && amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo,
214 RADEON_USAGE_WRITE)) {
215 cs->flush_cs(cs->flush_data, 0, NULL);
216 }
217 amdgpu_bo_wait((struct pb_buffer*)bo, PIPE_TIMEOUT_INFINITE,
218 RADEON_USAGE_WRITE);
219 } else {
220 /* Mapping for write. */
221 if (cs && amdgpu_bo_is_referenced_by_cs(cs, bo))
222 cs->flush_cs(cs->flush_data, 0, NULL);
223
224 amdgpu_bo_wait((struct pb_buffer*)bo, PIPE_TIMEOUT_INFINITE,
225 RADEON_USAGE_READWRITE);
226 }
227
228 bo->ws->buffer_wait_time += os_time_get_nano() - time;
229 }
230 }
231
232 /* If the buffer is created from user memory, return the user pointer. */
233 if (bo->user_ptr)
234 return bo->user_ptr;
235
236 r = amdgpu_bo_cpu_map(bo->bo, &cpu);
237 if (r) {
238 /* Clear the cache and try again. */
239 pb_cache_release_all_buffers(&bo->ws->bo_cache);
240 r = amdgpu_bo_cpu_map(bo->bo, &cpu);
241 }
242 return r ? NULL : cpu;
243 }
244
245 static void amdgpu_bo_unmap(struct pb_buffer *buf)
246 {
247 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
248
249 amdgpu_bo_cpu_unmap(bo->bo);
250 }
251
252 static const struct pb_vtbl amdgpu_winsys_bo_vtbl = {
253 amdgpu_bo_destroy_or_cache
254 /* other functions are never called */
255 };
256
257 static void amdgpu_add_buffer_to_global_list(struct amdgpu_winsys_bo *bo)
258 {
259 struct amdgpu_winsys *ws = bo->ws;
260
261 pipe_mutex_lock(ws->global_bo_list_lock);
262 LIST_ADDTAIL(&bo->global_list_item, &ws->global_bo_list);
263 ws->num_buffers++;
264 pipe_mutex_unlock(ws->global_bo_list_lock);
265 }
266
267 static struct amdgpu_winsys_bo *amdgpu_create_bo(struct amdgpu_winsys *ws,
268 unsigned size,
269 unsigned alignment,
270 unsigned usage,
271 enum radeon_bo_domain initial_domain,
272 unsigned flags)
273 {
274 struct amdgpu_bo_alloc_request request = {0};
275 amdgpu_bo_handle buf_handle;
276 uint64_t va = 0;
277 struct amdgpu_winsys_bo *bo;
278 amdgpu_va_handle va_handle;
279 int r;
280
281 assert(initial_domain & RADEON_DOMAIN_VRAM_GTT);
282 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
283 if (!bo) {
284 return NULL;
285 }
286
287 pb_cache_init_entry(&ws->bo_cache, &bo->cache_entry, &bo->base);
288 request.alloc_size = size;
289 request.phys_alignment = alignment;
290
291 if (initial_domain & RADEON_DOMAIN_VRAM)
292 request.preferred_heap |= AMDGPU_GEM_DOMAIN_VRAM;
293 if (initial_domain & RADEON_DOMAIN_GTT)
294 request.preferred_heap |= AMDGPU_GEM_DOMAIN_GTT;
295
296 if (flags & RADEON_FLAG_CPU_ACCESS)
297 request.flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
298 if (flags & RADEON_FLAG_NO_CPU_ACCESS)
299 request.flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
300 if (flags & RADEON_FLAG_GTT_WC)
301 request.flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
302
303 r = amdgpu_bo_alloc(ws->dev, &request, &buf_handle);
304 if (r) {
305 fprintf(stderr, "amdgpu: Failed to allocate a buffer:\n");
306 fprintf(stderr, "amdgpu: size : %d bytes\n", size);
307 fprintf(stderr, "amdgpu: alignment : %d bytes\n", alignment);
308 fprintf(stderr, "amdgpu: domains : %d\n", initial_domain);
309 goto error_bo_alloc;
310 }
311
312 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
313 size, alignment, 0, &va, &va_handle, 0);
314 if (r)
315 goto error_va_alloc;
316
317 r = amdgpu_bo_va_op(buf_handle, 0, size, va, 0, AMDGPU_VA_OP_MAP);
318 if (r)
319 goto error_va_map;
320
321 pipe_reference_init(&bo->base.reference, 1);
322 bo->base.alignment = alignment;
323 bo->base.usage = usage;
324 bo->base.size = size;
325 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
326 bo->ws = ws;
327 bo->bo = buf_handle;
328 bo->va = va;
329 bo->va_handle = va_handle;
330 bo->initial_domain = initial_domain;
331 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
332
333 if (initial_domain & RADEON_DOMAIN_VRAM)
334 ws->allocated_vram += align(size, ws->gart_page_size);
335 else if (initial_domain & RADEON_DOMAIN_GTT)
336 ws->allocated_gtt += align(size, ws->gart_page_size);
337
338 amdgpu_add_buffer_to_global_list(bo);
339
340 return bo;
341
342 error_va_map:
343 amdgpu_va_range_free(va_handle);
344
345 error_va_alloc:
346 amdgpu_bo_free(buf_handle);
347
348 error_bo_alloc:
349 FREE(bo);
350 return NULL;
351 }
352
353 bool amdgpu_bo_can_reclaim(struct pb_buffer *_buf)
354 {
355 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
356
357 if (amdgpu_bo_is_referenced_by_any_cs(bo)) {
358 return false;
359 }
360
361 return amdgpu_bo_wait(_buf, 0, RADEON_USAGE_READWRITE);
362 }
363
364 static unsigned eg_tile_split(unsigned tile_split)
365 {
366 switch (tile_split) {
367 case 0: tile_split = 64; break;
368 case 1: tile_split = 128; break;
369 case 2: tile_split = 256; break;
370 case 3: tile_split = 512; break;
371 default:
372 case 4: tile_split = 1024; break;
373 case 5: tile_split = 2048; break;
374 case 6: tile_split = 4096; break;
375 }
376 return tile_split;
377 }
378
379 static unsigned eg_tile_split_rev(unsigned eg_tile_split)
380 {
381 switch (eg_tile_split) {
382 case 64: return 0;
383 case 128: return 1;
384 case 256: return 2;
385 case 512: return 3;
386 default:
387 case 1024: return 4;
388 case 2048: return 5;
389 case 4096: return 6;
390 }
391 }
392
393 static void amdgpu_bo_get_tiling(struct pb_buffer *_buf,
394 enum radeon_bo_layout *microtiled,
395 enum radeon_bo_layout *macrotiled,
396 unsigned *bankw, unsigned *bankh,
397 unsigned *tile_split,
398 unsigned *stencil_tile_split,
399 unsigned *mtilea,
400 bool *scanout)
401 {
402 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
403 struct amdgpu_bo_info info = {0};
404 uint32_t tiling_flags;
405 int r;
406
407 r = amdgpu_bo_query_info(bo->bo, &info);
408 if (r)
409 return;
410
411 tiling_flags = info.metadata.tiling_info;
412
413 *microtiled = RADEON_LAYOUT_LINEAR;
414 *macrotiled = RADEON_LAYOUT_LINEAR;
415
416 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 4) /* 2D_TILED_THIN1 */
417 *macrotiled = RADEON_LAYOUT_TILED;
418 else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 2) /* 1D_TILED_THIN1 */
419 *microtiled = RADEON_LAYOUT_TILED;
420
421 if (bankw && tile_split && mtilea && tile_split) {
422 *bankw = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
423 *bankh = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
424 *tile_split = eg_tile_split(AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT));
425 *mtilea = 1 << AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
426 }
427 if (scanout)
428 *scanout = AMDGPU_TILING_GET(tiling_flags, MICRO_TILE_MODE) == 0; /* DISPLAY */
429 }
430
431 static void amdgpu_bo_set_tiling(struct pb_buffer *_buf,
432 struct radeon_winsys_cs *rcs,
433 enum radeon_bo_layout microtiled,
434 enum radeon_bo_layout macrotiled,
435 unsigned pipe_config,
436 unsigned bankw, unsigned bankh,
437 unsigned tile_split,
438 unsigned stencil_tile_split,
439 unsigned mtilea, unsigned num_banks,
440 uint32_t pitch,
441 bool scanout)
442 {
443 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
444 struct amdgpu_bo_metadata metadata = {0};
445 uint32_t tiling_flags = 0;
446
447 if (macrotiled == RADEON_LAYOUT_TILED)
448 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 4); /* 2D_TILED_THIN1 */
449 else if (microtiled == RADEON_LAYOUT_TILED)
450 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 2); /* 1D_TILED_THIN1 */
451 else
452 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 1); /* LINEAR_ALIGNED */
453
454 tiling_flags |= AMDGPU_TILING_SET(PIPE_CONFIG, pipe_config);
455 tiling_flags |= AMDGPU_TILING_SET(BANK_WIDTH, util_logbase2(bankw));
456 tiling_flags |= AMDGPU_TILING_SET(BANK_HEIGHT, util_logbase2(bankh));
457 if (tile_split)
458 tiling_flags |= AMDGPU_TILING_SET(TILE_SPLIT, eg_tile_split_rev(tile_split));
459 tiling_flags |= AMDGPU_TILING_SET(MACRO_TILE_ASPECT, util_logbase2(mtilea));
460 tiling_flags |= AMDGPU_TILING_SET(NUM_BANKS, util_logbase2(num_banks)-1);
461
462 if (scanout)
463 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 0); /* DISPLAY_MICRO_TILING */
464 else
465 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 1); /* THIN_MICRO_TILING */
466
467 metadata.tiling_info = tiling_flags;
468
469 amdgpu_bo_set_metadata(bo->bo, &metadata);
470 }
471
472 static struct pb_buffer *
473 amdgpu_bo_create(struct radeon_winsys *rws,
474 unsigned size,
475 unsigned alignment,
476 boolean use_reusable_pool,
477 enum radeon_bo_domain domain,
478 enum radeon_bo_flag flags)
479 {
480 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
481 struct amdgpu_winsys_bo *bo;
482 unsigned usage = 0;
483
484 /* Don't use VRAM if the GPU doesn't have much. This is only the initial
485 * domain. The kernel is free to move the buffer if it wants to.
486 *
487 * 64MB means no VRAM by todays standards.
488 */
489 if (domain & RADEON_DOMAIN_VRAM && ws->info.vram_size <= 64*1024*1024) {
490 domain = RADEON_DOMAIN_GTT;
491 flags = RADEON_FLAG_GTT_WC;
492 }
493
494 /* Align size to page size. This is the minimum alignment for normal
495 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
496 * like constant/uniform buffers, can benefit from better and more reuse.
497 */
498 size = align(size, ws->gart_page_size);
499
500 /* Only set one usage bit each for domains and flags, or the cache manager
501 * might consider different sets of domains / flags compatible
502 */
503 if (domain == RADEON_DOMAIN_VRAM_GTT)
504 usage = 1 << 2;
505 else
506 usage = domain >> 1;
507 assert(flags < sizeof(usage) * 8 - 3);
508 usage |= 1 << (flags + 3);
509
510 /* Get a buffer from the cache. */
511 if (use_reusable_pool) {
512 bo = (struct amdgpu_winsys_bo*)
513 pb_cache_reclaim_buffer(&ws->bo_cache, size, alignment,
514 usage);
515 if (bo)
516 return &bo->base;
517 }
518
519 /* Create a new one. */
520 bo = amdgpu_create_bo(ws, size, alignment, usage, domain, flags);
521 if (!bo) {
522 /* Clear the cache and try again. */
523 pb_cache_release_all_buffers(&ws->bo_cache);
524 bo = amdgpu_create_bo(ws, size, alignment, usage, domain, flags);
525 if (!bo)
526 return NULL;
527 }
528
529 bo->use_reusable_pool = use_reusable_pool;
530 return &bo->base;
531 }
532
533 static struct pb_buffer *amdgpu_bo_from_handle(struct radeon_winsys *rws,
534 struct winsys_handle *whandle,
535 unsigned *stride)
536 {
537 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
538 struct amdgpu_winsys_bo *bo;
539 enum amdgpu_bo_handle_type type;
540 struct amdgpu_bo_import_result result = {0};
541 uint64_t va;
542 amdgpu_va_handle va_handle;
543 struct amdgpu_bo_info info = {0};
544 enum radeon_bo_domain initial = 0;
545 int r;
546
547 /* Initialize the structure. */
548 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
549 if (!bo) {
550 return NULL;
551 }
552
553 switch (whandle->type) {
554 case DRM_API_HANDLE_TYPE_SHARED:
555 type = amdgpu_bo_handle_type_gem_flink_name;
556 break;
557 case DRM_API_HANDLE_TYPE_FD:
558 type = amdgpu_bo_handle_type_dma_buf_fd;
559 break;
560 default:
561 return NULL;
562 }
563
564 r = amdgpu_bo_import(ws->dev, type, whandle->handle, &result);
565 if (r)
566 goto error;
567
568 /* Get initial domains. */
569 r = amdgpu_bo_query_info(result.buf_handle, &info);
570 if (r)
571 goto error_query;
572
573 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
574 result.alloc_size, 1 << 20, 0, &va, &va_handle, 0);
575 if (r)
576 goto error_query;
577
578 r = amdgpu_bo_va_op(result.buf_handle, 0, result.alloc_size, va, 0, AMDGPU_VA_OP_MAP);
579 if (r)
580 goto error_va_map;
581
582 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_VRAM)
583 initial |= RADEON_DOMAIN_VRAM;
584 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_GTT)
585 initial |= RADEON_DOMAIN_GTT;
586
587
588 pipe_reference_init(&bo->base.reference, 1);
589 bo->base.alignment = info.phys_alignment;
590 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
591 bo->bo = result.buf_handle;
592 bo->base.size = result.alloc_size;
593 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
594 bo->ws = ws;
595 bo->va = va;
596 bo->va_handle = va_handle;
597 bo->initial_domain = initial;
598 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
599 bo->is_shared = true;
600
601 if (stride)
602 *stride = whandle->stride;
603
604 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
605 ws->allocated_vram += align(bo->base.size, ws->gart_page_size);
606 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
607 ws->allocated_gtt += align(bo->base.size, ws->gart_page_size);
608
609 amdgpu_add_buffer_to_global_list(bo);
610
611 return &bo->base;
612
613 error_va_map:
614 amdgpu_va_range_free(va_handle);
615
616 error_query:
617 amdgpu_bo_free(result.buf_handle);
618
619 error:
620 FREE(bo);
621 return NULL;
622 }
623
624 static boolean amdgpu_bo_get_handle(struct pb_buffer *buffer,
625 unsigned stride,
626 struct winsys_handle *whandle)
627 {
628 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(buffer);
629 enum amdgpu_bo_handle_type type;
630 int r;
631
632 bo->use_reusable_pool = false;
633
634 switch (whandle->type) {
635 case DRM_API_HANDLE_TYPE_SHARED:
636 type = amdgpu_bo_handle_type_gem_flink_name;
637 break;
638 case DRM_API_HANDLE_TYPE_FD:
639 type = amdgpu_bo_handle_type_dma_buf_fd;
640 break;
641 case DRM_API_HANDLE_TYPE_KMS:
642 type = amdgpu_bo_handle_type_kms;
643 break;
644 default:
645 return FALSE;
646 }
647
648 r = amdgpu_bo_export(bo->bo, type, &whandle->handle);
649 if (r)
650 return FALSE;
651
652 whandle->stride = stride;
653 bo->is_shared = true;
654 return TRUE;
655 }
656
657 static struct pb_buffer *amdgpu_bo_from_ptr(struct radeon_winsys *rws,
658 void *pointer, unsigned size)
659 {
660 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
661 amdgpu_bo_handle buf_handle;
662 struct amdgpu_winsys_bo *bo;
663 uint64_t va;
664 amdgpu_va_handle va_handle;
665
666 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
667 if (!bo)
668 return NULL;
669
670 if (amdgpu_create_bo_from_user_mem(ws->dev, pointer, size, &buf_handle))
671 goto error;
672
673 if (amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
674 size, 1 << 12, 0, &va, &va_handle, 0))
675 goto error_va_alloc;
676
677 if (amdgpu_bo_va_op(buf_handle, 0, size, va, 0, AMDGPU_VA_OP_MAP))
678 goto error_va_map;
679
680 /* Initialize it. */
681 pipe_reference_init(&bo->base.reference, 1);
682 bo->bo = buf_handle;
683 bo->base.alignment = 0;
684 bo->base.usage = PB_USAGE_GPU_WRITE | PB_USAGE_GPU_READ;
685 bo->base.size = size;
686 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
687 bo->ws = ws;
688 bo->user_ptr = pointer;
689 bo->va = va;
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);
693
694 ws->allocated_gtt += align(bo->base.size, ws->gart_page_size);
695
696 amdgpu_add_buffer_to_global_list(bo);
697
698 return (struct pb_buffer*)bo;
699
700 error_va_map:
701 amdgpu_va_range_free(va_handle);
702
703 error_va_alloc:
704 amdgpu_bo_free(buf_handle);
705
706 error:
707 FREE(bo);
708 return NULL;
709 }
710
711 static bool amdgpu_bo_is_user_ptr(struct pb_buffer *buf)
712 {
713 return ((struct amdgpu_winsys_bo*)buf)->user_ptr != NULL;
714 }
715
716 static uint64_t amdgpu_bo_get_va(struct pb_buffer *buf)
717 {
718 return ((struct amdgpu_winsys_bo*)buf)->va;
719 }
720
721 void amdgpu_bo_init_functions(struct amdgpu_winsys *ws)
722 {
723 ws->base.buffer_set_tiling = amdgpu_bo_set_tiling;
724 ws->base.buffer_get_tiling = amdgpu_bo_get_tiling;
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;
735 }