winsys/amdgpu: allow non page-aligned size bo creation from pointer
[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 #include "amdgpu_cs.h"
29
30 #include "util/os_time.h"
31 #include "state_tracker/drm_driver.h"
32 #include <amdgpu_drm.h>
33 #include <xf86drm.h>
34 #include <stdio.h>
35 #include <inttypes.h>
36
37 #ifndef AMDGPU_GEM_CREATE_VM_ALWAYS_VALID
38 #define AMDGPU_GEM_CREATE_VM_ALWAYS_VALID (1 << 6)
39 #endif
40
41 /* Set to 1 for verbose output showing committed sparse buffer ranges. */
42 #define DEBUG_SPARSE_COMMITS 0
43
44 struct amdgpu_sparse_backing_chunk {
45 uint32_t begin, end;
46 };
47
48 static struct pb_buffer *
49 amdgpu_bo_create(struct radeon_winsys *rws,
50 uint64_t size,
51 unsigned alignment,
52 enum radeon_bo_domain domain,
53 enum radeon_bo_flag flags);
54
55 static bool amdgpu_bo_wait(struct pb_buffer *_buf, uint64_t timeout,
56 enum radeon_bo_usage usage)
57 {
58 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
59 struct amdgpu_winsys *ws = bo->ws;
60 int64_t abs_timeout;
61
62 if (timeout == 0) {
63 if (p_atomic_read(&bo->num_active_ioctls))
64 return false;
65
66 } else {
67 abs_timeout = os_time_get_absolute_timeout(timeout);
68
69 /* Wait if any ioctl is being submitted with this buffer. */
70 if (!os_wait_until_zero_abs_timeout(&bo->num_active_ioctls, abs_timeout))
71 return false;
72 }
73
74 if (bo->is_shared) {
75 /* We can't use user fences for shared buffers, because user fences
76 * are local to this process only. If we want to wait for all buffer
77 * uses in all processes, we have to use amdgpu_bo_wait_for_idle.
78 */
79 bool buffer_busy = true;
80 int r;
81
82 r = amdgpu_bo_wait_for_idle(bo->bo, timeout, &buffer_busy);
83 if (r)
84 fprintf(stderr, "%s: amdgpu_bo_wait_for_idle failed %i\n", __func__,
85 r);
86 return !buffer_busy;
87 }
88
89 if (timeout == 0) {
90 unsigned idle_fences;
91 bool buffer_idle;
92
93 simple_mtx_lock(&ws->bo_fence_lock);
94
95 for (idle_fences = 0; idle_fences < bo->num_fences; ++idle_fences) {
96 if (!amdgpu_fence_wait(bo->fences[idle_fences], 0, false))
97 break;
98 }
99
100 /* Release the idle fences to avoid checking them again later. */
101 for (unsigned i = 0; i < idle_fences; ++i)
102 amdgpu_fence_reference(&bo->fences[i], NULL);
103
104 memmove(&bo->fences[0], &bo->fences[idle_fences],
105 (bo->num_fences - idle_fences) * sizeof(*bo->fences));
106 bo->num_fences -= idle_fences;
107
108 buffer_idle = !bo->num_fences;
109 simple_mtx_unlock(&ws->bo_fence_lock);
110
111 return buffer_idle;
112 } else {
113 bool buffer_idle = true;
114
115 simple_mtx_lock(&ws->bo_fence_lock);
116 while (bo->num_fences && buffer_idle) {
117 struct pipe_fence_handle *fence = NULL;
118 bool fence_idle = false;
119
120 amdgpu_fence_reference(&fence, bo->fences[0]);
121
122 /* Wait for the fence. */
123 simple_mtx_unlock(&ws->bo_fence_lock);
124 if (amdgpu_fence_wait(fence, abs_timeout, true))
125 fence_idle = true;
126 else
127 buffer_idle = false;
128 simple_mtx_lock(&ws->bo_fence_lock);
129
130 /* Release an idle fence to avoid checking it again later, keeping in
131 * mind that the fence array may have been modified by other threads.
132 */
133 if (fence_idle && bo->num_fences && bo->fences[0] == fence) {
134 amdgpu_fence_reference(&bo->fences[0], NULL);
135 memmove(&bo->fences[0], &bo->fences[1],
136 (bo->num_fences - 1) * sizeof(*bo->fences));
137 bo->num_fences--;
138 }
139
140 amdgpu_fence_reference(&fence, NULL);
141 }
142 simple_mtx_unlock(&ws->bo_fence_lock);
143
144 return buffer_idle;
145 }
146 }
147
148 static enum radeon_bo_domain amdgpu_bo_get_initial_domain(
149 struct pb_buffer *buf)
150 {
151 return ((struct amdgpu_winsys_bo*)buf)->initial_domain;
152 }
153
154 static void amdgpu_bo_remove_fences(struct amdgpu_winsys_bo *bo)
155 {
156 for (unsigned i = 0; i < bo->num_fences; ++i)
157 amdgpu_fence_reference(&bo->fences[i], NULL);
158
159 FREE(bo->fences);
160 bo->num_fences = 0;
161 bo->max_fences = 0;
162 }
163
164 void amdgpu_bo_destroy(struct pb_buffer *_buf)
165 {
166 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
167
168 assert(bo->bo && "must not be called for slab entries");
169
170 if (bo->ws->debug_all_bos) {
171 simple_mtx_lock(&bo->ws->global_bo_list_lock);
172 LIST_DEL(&bo->u.real.global_list_item);
173 bo->ws->num_buffers--;
174 simple_mtx_unlock(&bo->ws->global_bo_list_lock);
175 }
176
177 amdgpu_bo_va_op(bo->bo, 0, bo->base.size, bo->va, 0, AMDGPU_VA_OP_UNMAP);
178 amdgpu_va_range_free(bo->u.real.va_handle);
179 amdgpu_bo_free(bo->bo);
180
181 amdgpu_bo_remove_fences(bo);
182
183 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
184 bo->ws->allocated_vram -= align64(bo->base.size, bo->ws->info.gart_page_size);
185 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
186 bo->ws->allocated_gtt -= align64(bo->base.size, bo->ws->info.gart_page_size);
187
188 if (bo->u.real.map_count >= 1) {
189 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
190 bo->ws->mapped_vram -= bo->base.size;
191 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
192 bo->ws->mapped_gtt -= bo->base.size;
193 bo->ws->num_mapped_buffers--;
194 }
195
196 FREE(bo);
197 }
198
199 static void amdgpu_bo_destroy_or_cache(struct pb_buffer *_buf)
200 {
201 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
202
203 assert(bo->bo); /* slab buffers have a separate vtbl */
204
205 if (bo->u.real.use_reusable_pool)
206 pb_cache_add_buffer(&bo->u.real.cache_entry);
207 else
208 amdgpu_bo_destroy(_buf);
209 }
210
211 static void *amdgpu_bo_map(struct pb_buffer *buf,
212 struct radeon_winsys_cs *rcs,
213 enum pipe_transfer_usage usage)
214 {
215 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
216 struct amdgpu_winsys_bo *real;
217 struct amdgpu_cs *cs = (struct amdgpu_cs*)rcs;
218 int r;
219 void *cpu = NULL;
220 uint64_t offset = 0;
221
222 assert(!bo->sparse);
223
224 /* If it's not unsynchronized bo_map, flush CS if needed and then wait. */
225 if (!(usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
226 /* DONTBLOCK doesn't make sense with UNSYNCHRONIZED. */
227 if (usage & PIPE_TRANSFER_DONTBLOCK) {
228 if (!(usage & PIPE_TRANSFER_WRITE)) {
229 /* Mapping for read.
230 *
231 * Since we are mapping for read, we don't need to wait
232 * if the GPU is using the buffer for read too
233 * (neither one is changing it).
234 *
235 * Only check whether the buffer is being used for write. */
236 if (cs && amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo,
237 RADEON_USAGE_WRITE)) {
238 cs->flush_cs(cs->flush_data, PIPE_FLUSH_ASYNC, NULL);
239 return NULL;
240 }
241
242 if (!amdgpu_bo_wait((struct pb_buffer*)bo, 0,
243 RADEON_USAGE_WRITE)) {
244 return NULL;
245 }
246 } else {
247 if (cs && amdgpu_bo_is_referenced_by_cs(cs, bo)) {
248 cs->flush_cs(cs->flush_data, PIPE_FLUSH_ASYNC, NULL);
249 return NULL;
250 }
251
252 if (!amdgpu_bo_wait((struct pb_buffer*)bo, 0,
253 RADEON_USAGE_READWRITE)) {
254 return NULL;
255 }
256 }
257 } else {
258 uint64_t time = os_time_get_nano();
259
260 if (!(usage & PIPE_TRANSFER_WRITE)) {
261 /* Mapping for read.
262 *
263 * Since we are mapping for read, we don't need to wait
264 * if the GPU is using the buffer for read too
265 * (neither one is changing it).
266 *
267 * Only check whether the buffer is being used for write. */
268 if (cs) {
269 if (amdgpu_bo_is_referenced_by_cs_with_usage(cs, bo,
270 RADEON_USAGE_WRITE)) {
271 cs->flush_cs(cs->flush_data, 0, NULL);
272 } else {
273 /* Try to avoid busy-waiting in amdgpu_bo_wait. */
274 if (p_atomic_read(&bo->num_active_ioctls))
275 amdgpu_cs_sync_flush(rcs);
276 }
277 }
278
279 amdgpu_bo_wait((struct pb_buffer*)bo, PIPE_TIMEOUT_INFINITE,
280 RADEON_USAGE_WRITE);
281 } else {
282 /* Mapping for write. */
283 if (cs) {
284 if (amdgpu_bo_is_referenced_by_cs(cs, bo)) {
285 cs->flush_cs(cs->flush_data, 0, NULL);
286 } else {
287 /* Try to avoid busy-waiting in amdgpu_bo_wait. */
288 if (p_atomic_read(&bo->num_active_ioctls))
289 amdgpu_cs_sync_flush(rcs);
290 }
291 }
292
293 amdgpu_bo_wait((struct pb_buffer*)bo, PIPE_TIMEOUT_INFINITE,
294 RADEON_USAGE_READWRITE);
295 }
296
297 bo->ws->buffer_wait_time += os_time_get_nano() - time;
298 }
299 }
300
301 /* If the buffer is created from user memory, return the user pointer. */
302 if (bo->user_ptr)
303 return bo->user_ptr;
304
305 if (bo->bo) {
306 real = bo;
307 } else {
308 real = bo->u.slab.real;
309 offset = bo->va - real->va;
310 }
311
312 r = amdgpu_bo_cpu_map(real->bo, &cpu);
313 if (r) {
314 /* Clear the cache and try again. */
315 pb_cache_release_all_buffers(&real->ws->bo_cache);
316 r = amdgpu_bo_cpu_map(real->bo, &cpu);
317 if (r)
318 return NULL;
319 }
320
321 if (p_atomic_inc_return(&real->u.real.map_count) == 1) {
322 if (real->initial_domain & RADEON_DOMAIN_VRAM)
323 real->ws->mapped_vram += real->base.size;
324 else if (real->initial_domain & RADEON_DOMAIN_GTT)
325 real->ws->mapped_gtt += real->base.size;
326 real->ws->num_mapped_buffers++;
327 }
328 return (uint8_t*)cpu + offset;
329 }
330
331 static void amdgpu_bo_unmap(struct pb_buffer *buf)
332 {
333 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
334 struct amdgpu_winsys_bo *real;
335
336 assert(!bo->sparse);
337
338 if (bo->user_ptr)
339 return;
340
341 real = bo->bo ? bo : bo->u.slab.real;
342
343 if (p_atomic_dec_zero(&real->u.real.map_count)) {
344 if (real->initial_domain & RADEON_DOMAIN_VRAM)
345 real->ws->mapped_vram -= real->base.size;
346 else if (real->initial_domain & RADEON_DOMAIN_GTT)
347 real->ws->mapped_gtt -= real->base.size;
348 real->ws->num_mapped_buffers--;
349 }
350
351 amdgpu_bo_cpu_unmap(real->bo);
352 }
353
354 static const struct pb_vtbl amdgpu_winsys_bo_vtbl = {
355 amdgpu_bo_destroy_or_cache
356 /* other functions are never called */
357 };
358
359 static void amdgpu_add_buffer_to_global_list(struct amdgpu_winsys_bo *bo)
360 {
361 struct amdgpu_winsys *ws = bo->ws;
362
363 assert(bo->bo);
364
365 if (ws->debug_all_bos) {
366 simple_mtx_lock(&ws->global_bo_list_lock);
367 LIST_ADDTAIL(&bo->u.real.global_list_item, &ws->global_bo_list);
368 ws->num_buffers++;
369 simple_mtx_unlock(&ws->global_bo_list_lock);
370 }
371 }
372
373 static struct amdgpu_winsys_bo *amdgpu_create_bo(struct amdgpu_winsys *ws,
374 uint64_t size,
375 unsigned alignment,
376 enum radeon_bo_domain initial_domain,
377 unsigned flags,
378 int heap)
379 {
380 struct amdgpu_bo_alloc_request request = {0};
381 amdgpu_bo_handle buf_handle;
382 uint64_t va = 0;
383 struct amdgpu_winsys_bo *bo;
384 amdgpu_va_handle va_handle;
385 unsigned va_gap_size;
386 int r;
387
388 /* VRAM or GTT must be specified, but not both at the same time. */
389 assert(util_bitcount(initial_domain & RADEON_DOMAIN_VRAM_GTT) == 1);
390
391 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
392 if (!bo) {
393 return NULL;
394 }
395
396 if (heap >= 0) {
397 pb_cache_init_entry(&ws->bo_cache, &bo->u.real.cache_entry, &bo->base,
398 heap);
399 }
400 request.alloc_size = size;
401 request.phys_alignment = alignment;
402
403 if (initial_domain & RADEON_DOMAIN_VRAM)
404 request.preferred_heap |= AMDGPU_GEM_DOMAIN_VRAM;
405 if (initial_domain & RADEON_DOMAIN_GTT)
406 request.preferred_heap |= AMDGPU_GEM_DOMAIN_GTT;
407
408 /* If VRAM is just stolen system memory, allow both VRAM and
409 * GTT, whichever has free space. If a buffer is evicted from
410 * VRAM to GTT, it will stay there.
411 *
412 * DRM 3.6.0 has good BO move throttling, so we can allow VRAM-only
413 * placements even with a low amount of stolen VRAM.
414 */
415 if (!ws->info.has_dedicated_vram && ws->info.drm_minor < 6)
416 request.preferred_heap |= AMDGPU_GEM_DOMAIN_GTT;
417
418 if (flags & RADEON_FLAG_NO_CPU_ACCESS)
419 request.flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
420 if (flags & RADEON_FLAG_GTT_WC)
421 request.flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
422 /* TODO: Enable this once the kernel handles it efficiently. */
423 /*if (flags & RADEON_FLAG_NO_INTERPROCESS_SHARING &&
424 ws->info.drm_minor >= 20)
425 request.flags |= AMDGPU_GEM_CREATE_VM_ALWAYS_VALID;*/
426
427 r = amdgpu_bo_alloc(ws->dev, &request, &buf_handle);
428 if (r) {
429 fprintf(stderr, "amdgpu: Failed to allocate a buffer:\n");
430 fprintf(stderr, "amdgpu: size : %"PRIu64" bytes\n", size);
431 fprintf(stderr, "amdgpu: alignment : %u bytes\n", alignment);
432 fprintf(stderr, "amdgpu: domains : %u\n", initial_domain);
433 goto error_bo_alloc;
434 }
435
436 va_gap_size = ws->check_vm ? MAX2(4 * alignment, 64 * 1024) : 0;
437 if (size > ws->info.pte_fragment_size)
438 alignment = MAX2(alignment, ws->info.pte_fragment_size);
439 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
440 size + va_gap_size, alignment, 0, &va, &va_handle, 0);
441 if (r)
442 goto error_va_alloc;
443
444 unsigned vm_flags = AMDGPU_VM_PAGE_READABLE |
445 AMDGPU_VM_PAGE_EXECUTABLE;
446
447 if (!(flags & RADEON_FLAG_READ_ONLY))
448 vm_flags |= AMDGPU_VM_PAGE_WRITEABLE;
449
450 r = amdgpu_bo_va_op_raw(ws->dev, buf_handle, 0, size, va, vm_flags,
451 AMDGPU_VA_OP_MAP);
452 if (r)
453 goto error_va_map;
454
455 pipe_reference_init(&bo->base.reference, 1);
456 bo->base.alignment = alignment;
457 bo->base.usage = 0;
458 bo->base.size = size;
459 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
460 bo->ws = ws;
461 bo->bo = buf_handle;
462 bo->va = va;
463 bo->u.real.va_handle = va_handle;
464 bo->initial_domain = initial_domain;
465 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
466 bo->is_local = !!(request.flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID);
467
468 if (initial_domain & RADEON_DOMAIN_VRAM)
469 ws->allocated_vram += align64(size, ws->info.gart_page_size);
470 else if (initial_domain & RADEON_DOMAIN_GTT)
471 ws->allocated_gtt += align64(size, ws->info.gart_page_size);
472
473 amdgpu_add_buffer_to_global_list(bo);
474
475 return bo;
476
477 error_va_map:
478 amdgpu_va_range_free(va_handle);
479
480 error_va_alloc:
481 amdgpu_bo_free(buf_handle);
482
483 error_bo_alloc:
484 FREE(bo);
485 return NULL;
486 }
487
488 bool amdgpu_bo_can_reclaim(struct pb_buffer *_buf)
489 {
490 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
491
492 if (amdgpu_bo_is_referenced_by_any_cs(bo)) {
493 return false;
494 }
495
496 return amdgpu_bo_wait(_buf, 0, RADEON_USAGE_READWRITE);
497 }
498
499 bool amdgpu_bo_can_reclaim_slab(void *priv, struct pb_slab_entry *entry)
500 {
501 struct amdgpu_winsys_bo *bo = NULL; /* fix container_of */
502 bo = container_of(entry, bo, u.slab.entry);
503
504 return amdgpu_bo_can_reclaim(&bo->base);
505 }
506
507 static void amdgpu_bo_slab_destroy(struct pb_buffer *_buf)
508 {
509 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
510
511 assert(!bo->bo);
512
513 pb_slab_free(&bo->ws->bo_slabs, &bo->u.slab.entry);
514 }
515
516 static const struct pb_vtbl amdgpu_winsys_bo_slab_vtbl = {
517 amdgpu_bo_slab_destroy
518 /* other functions are never called */
519 };
520
521 struct pb_slab *amdgpu_bo_slab_alloc(void *priv, unsigned heap,
522 unsigned entry_size,
523 unsigned group_index)
524 {
525 struct amdgpu_winsys *ws = priv;
526 struct amdgpu_slab *slab = CALLOC_STRUCT(amdgpu_slab);
527 enum radeon_bo_domain domains = radeon_domain_from_heap(heap);
528 enum radeon_bo_flag flags = radeon_flags_from_heap(heap);
529 uint32_t base_id;
530
531 if (!slab)
532 return NULL;
533
534 unsigned slab_size = 1 << AMDGPU_SLAB_BO_SIZE_LOG2;
535 slab->buffer = amdgpu_winsys_bo(amdgpu_bo_create(&ws->base,
536 slab_size, slab_size,
537 domains, flags));
538 if (!slab->buffer)
539 goto fail;
540
541 assert(slab->buffer->bo);
542
543 slab->base.num_entries = slab->buffer->base.size / entry_size;
544 slab->base.num_free = slab->base.num_entries;
545 slab->entries = CALLOC(slab->base.num_entries, sizeof(*slab->entries));
546 if (!slab->entries)
547 goto fail_buffer;
548
549 LIST_INITHEAD(&slab->base.free);
550
551 base_id = __sync_fetch_and_add(&ws->next_bo_unique_id, slab->base.num_entries);
552
553 for (unsigned i = 0; i < slab->base.num_entries; ++i) {
554 struct amdgpu_winsys_bo *bo = &slab->entries[i];
555
556 bo->base.alignment = entry_size;
557 bo->base.usage = slab->buffer->base.usage;
558 bo->base.size = entry_size;
559 bo->base.vtbl = &amdgpu_winsys_bo_slab_vtbl;
560 bo->ws = ws;
561 bo->va = slab->buffer->va + i * entry_size;
562 bo->initial_domain = domains;
563 bo->unique_id = base_id + i;
564 bo->u.slab.entry.slab = &slab->base;
565 bo->u.slab.entry.group_index = group_index;
566 bo->u.slab.real = slab->buffer;
567
568 LIST_ADDTAIL(&bo->u.slab.entry.head, &slab->base.free);
569 }
570
571 return &slab->base;
572
573 fail_buffer:
574 amdgpu_winsys_bo_reference(&slab->buffer, NULL);
575 fail:
576 FREE(slab);
577 return NULL;
578 }
579
580 void amdgpu_bo_slab_free(void *priv, struct pb_slab *pslab)
581 {
582 struct amdgpu_slab *slab = amdgpu_slab(pslab);
583
584 for (unsigned i = 0; i < slab->base.num_entries; ++i)
585 amdgpu_bo_remove_fences(&slab->entries[i]);
586
587 FREE(slab->entries);
588 amdgpu_winsys_bo_reference(&slab->buffer, NULL);
589 FREE(slab);
590 }
591
592 #if DEBUG_SPARSE_COMMITS
593 static void
594 sparse_dump(struct amdgpu_winsys_bo *bo, const char *func)
595 {
596 fprintf(stderr, "%s: %p (size=%"PRIu64", num_va_pages=%u) @ %s\n"
597 "Commitments:\n",
598 __func__, bo, bo->base.size, bo->u.sparse.num_va_pages, func);
599
600 struct amdgpu_sparse_backing *span_backing = NULL;
601 uint32_t span_first_backing_page = 0;
602 uint32_t span_first_va_page = 0;
603 uint32_t va_page = 0;
604
605 for (;;) {
606 struct amdgpu_sparse_backing *backing = 0;
607 uint32_t backing_page = 0;
608
609 if (va_page < bo->u.sparse.num_va_pages) {
610 backing = bo->u.sparse.commitments[va_page].backing;
611 backing_page = bo->u.sparse.commitments[va_page].page;
612 }
613
614 if (span_backing &&
615 (backing != span_backing ||
616 backing_page != span_first_backing_page + (va_page - span_first_va_page))) {
617 fprintf(stderr, " %u..%u: backing=%p:%u..%u\n",
618 span_first_va_page, va_page - 1, span_backing,
619 span_first_backing_page,
620 span_first_backing_page + (va_page - span_first_va_page) - 1);
621
622 span_backing = NULL;
623 }
624
625 if (va_page >= bo->u.sparse.num_va_pages)
626 break;
627
628 if (backing && !span_backing) {
629 span_backing = backing;
630 span_first_backing_page = backing_page;
631 span_first_va_page = va_page;
632 }
633
634 va_page++;
635 }
636
637 fprintf(stderr, "Backing:\n");
638
639 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
640 fprintf(stderr, " %p (size=%"PRIu64")\n", backing, backing->bo->base.size);
641 for (unsigned i = 0; i < backing->num_chunks; ++i)
642 fprintf(stderr, " %u..%u\n", backing->chunks[i].begin, backing->chunks[i].end);
643 }
644 }
645 #endif
646
647 /*
648 * Attempt to allocate the given number of backing pages. Fewer pages may be
649 * allocated (depending on the fragmentation of existing backing buffers),
650 * which will be reflected by a change to *pnum_pages.
651 */
652 static struct amdgpu_sparse_backing *
653 sparse_backing_alloc(struct amdgpu_winsys_bo *bo, uint32_t *pstart_page, uint32_t *pnum_pages)
654 {
655 struct amdgpu_sparse_backing *best_backing;
656 unsigned best_idx;
657 uint32_t best_num_pages;
658
659 best_backing = NULL;
660 best_idx = 0;
661 best_num_pages = 0;
662
663 /* This is a very simple and inefficient best-fit algorithm. */
664 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
665 for (unsigned idx = 0; idx < backing->num_chunks; ++idx) {
666 uint32_t cur_num_pages = backing->chunks[idx].end - backing->chunks[idx].begin;
667 if ((best_num_pages < *pnum_pages && cur_num_pages > best_num_pages) ||
668 (best_num_pages > *pnum_pages && cur_num_pages < best_num_pages)) {
669 best_backing = backing;
670 best_idx = idx;
671 best_num_pages = cur_num_pages;
672 }
673 }
674 }
675
676 /* Allocate a new backing buffer if necessary. */
677 if (!best_backing) {
678 struct pb_buffer *buf;
679 uint64_t size;
680 uint32_t pages;
681
682 best_backing = CALLOC_STRUCT(amdgpu_sparse_backing);
683 if (!best_backing)
684 return NULL;
685
686 best_backing->max_chunks = 4;
687 best_backing->chunks = CALLOC(best_backing->max_chunks,
688 sizeof(*best_backing->chunks));
689 if (!best_backing->chunks) {
690 FREE(best_backing);
691 return NULL;
692 }
693
694 assert(bo->u.sparse.num_backing_pages < DIV_ROUND_UP(bo->base.size, RADEON_SPARSE_PAGE_SIZE));
695
696 size = MIN3(bo->base.size / 16,
697 8 * 1024 * 1024,
698 bo->base.size - (uint64_t)bo->u.sparse.num_backing_pages * RADEON_SPARSE_PAGE_SIZE);
699 size = MAX2(size, RADEON_SPARSE_PAGE_SIZE);
700
701 buf = amdgpu_bo_create(&bo->ws->base, size, RADEON_SPARSE_PAGE_SIZE,
702 bo->initial_domain,
703 bo->u.sparse.flags | RADEON_FLAG_NO_SUBALLOC);
704 if (!buf) {
705 FREE(best_backing->chunks);
706 FREE(best_backing);
707 return NULL;
708 }
709
710 /* We might have gotten a bigger buffer than requested via caching. */
711 pages = buf->size / RADEON_SPARSE_PAGE_SIZE;
712
713 best_backing->bo = amdgpu_winsys_bo(buf);
714 best_backing->num_chunks = 1;
715 best_backing->chunks[0].begin = 0;
716 best_backing->chunks[0].end = pages;
717
718 list_add(&best_backing->list, &bo->u.sparse.backing);
719 bo->u.sparse.num_backing_pages += pages;
720
721 best_idx = 0;
722 best_num_pages = pages;
723 }
724
725 *pnum_pages = MIN2(*pnum_pages, best_num_pages);
726 *pstart_page = best_backing->chunks[best_idx].begin;
727 best_backing->chunks[best_idx].begin += *pnum_pages;
728
729 if (best_backing->chunks[best_idx].begin >= best_backing->chunks[best_idx].end) {
730 memmove(&best_backing->chunks[best_idx], &best_backing->chunks[best_idx + 1],
731 sizeof(*best_backing->chunks) * (best_backing->num_chunks - best_idx - 1));
732 best_backing->num_chunks--;
733 }
734
735 return best_backing;
736 }
737
738 static void
739 sparse_free_backing_buffer(struct amdgpu_winsys_bo *bo,
740 struct amdgpu_sparse_backing *backing)
741 {
742 struct amdgpu_winsys *ws = backing->bo->ws;
743
744 bo->u.sparse.num_backing_pages -= backing->bo->base.size / RADEON_SPARSE_PAGE_SIZE;
745
746 simple_mtx_lock(&ws->bo_fence_lock);
747 amdgpu_add_fences(backing->bo, bo->num_fences, bo->fences);
748 simple_mtx_unlock(&ws->bo_fence_lock);
749
750 list_del(&backing->list);
751 amdgpu_winsys_bo_reference(&backing->bo, NULL);
752 FREE(backing->chunks);
753 FREE(backing);
754 }
755
756 /*
757 * Return a range of pages from the given backing buffer back into the
758 * free structure.
759 */
760 static bool
761 sparse_backing_free(struct amdgpu_winsys_bo *bo,
762 struct amdgpu_sparse_backing *backing,
763 uint32_t start_page, uint32_t num_pages)
764 {
765 uint32_t end_page = start_page + num_pages;
766 unsigned low = 0;
767 unsigned high = backing->num_chunks;
768
769 /* Find the first chunk with begin >= start_page. */
770 while (low < high) {
771 unsigned mid = low + (high - low) / 2;
772
773 if (backing->chunks[mid].begin >= start_page)
774 high = mid;
775 else
776 low = mid + 1;
777 }
778
779 assert(low >= backing->num_chunks || end_page <= backing->chunks[low].begin);
780 assert(low == 0 || backing->chunks[low - 1].end <= start_page);
781
782 if (low > 0 && backing->chunks[low - 1].end == start_page) {
783 backing->chunks[low - 1].end = end_page;
784
785 if (low < backing->num_chunks && end_page == backing->chunks[low].begin) {
786 backing->chunks[low - 1].end = backing->chunks[low].end;
787 memmove(&backing->chunks[low], &backing->chunks[low + 1],
788 sizeof(*backing->chunks) * (backing->num_chunks - low - 1));
789 backing->num_chunks--;
790 }
791 } else if (low < backing->num_chunks && end_page == backing->chunks[low].begin) {
792 backing->chunks[low].begin = start_page;
793 } else {
794 if (backing->num_chunks >= backing->max_chunks) {
795 unsigned new_max_chunks = 2 * backing->max_chunks;
796 struct amdgpu_sparse_backing_chunk *new_chunks =
797 REALLOC(backing->chunks,
798 sizeof(*backing->chunks) * backing->max_chunks,
799 sizeof(*backing->chunks) * new_max_chunks);
800 if (!new_chunks)
801 return false;
802
803 backing->max_chunks = new_max_chunks;
804 backing->chunks = new_chunks;
805 }
806
807 memmove(&backing->chunks[low + 1], &backing->chunks[low],
808 sizeof(*backing->chunks) * (backing->num_chunks - low));
809 backing->chunks[low].begin = start_page;
810 backing->chunks[low].end = end_page;
811 backing->num_chunks++;
812 }
813
814 if (backing->num_chunks == 1 && backing->chunks[0].begin == 0 &&
815 backing->chunks[0].end == backing->bo->base.size / RADEON_SPARSE_PAGE_SIZE)
816 sparse_free_backing_buffer(bo, backing);
817
818 return true;
819 }
820
821 static void amdgpu_bo_sparse_destroy(struct pb_buffer *_buf)
822 {
823 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
824 int r;
825
826 assert(!bo->bo && bo->sparse);
827
828 r = amdgpu_bo_va_op_raw(bo->ws->dev, NULL, 0,
829 (uint64_t)bo->u.sparse.num_va_pages * RADEON_SPARSE_PAGE_SIZE,
830 bo->va, 0, AMDGPU_VA_OP_CLEAR);
831 if (r) {
832 fprintf(stderr, "amdgpu: clearing PRT VA region on destroy failed (%d)\n", r);
833 }
834
835 while (!list_empty(&bo->u.sparse.backing)) {
836 struct amdgpu_sparse_backing *dummy = NULL;
837 sparse_free_backing_buffer(bo,
838 container_of(bo->u.sparse.backing.next,
839 dummy, list));
840 }
841
842 amdgpu_va_range_free(bo->u.sparse.va_handle);
843 simple_mtx_destroy(&bo->u.sparse.commit_lock);
844 FREE(bo->u.sparse.commitments);
845 FREE(bo);
846 }
847
848 static const struct pb_vtbl amdgpu_winsys_bo_sparse_vtbl = {
849 amdgpu_bo_sparse_destroy
850 /* other functions are never called */
851 };
852
853 static struct pb_buffer *
854 amdgpu_bo_sparse_create(struct amdgpu_winsys *ws, uint64_t size,
855 enum radeon_bo_domain domain,
856 enum radeon_bo_flag flags)
857 {
858 struct amdgpu_winsys_bo *bo;
859 uint64_t map_size;
860 uint64_t va_gap_size;
861 int r;
862
863 /* We use 32-bit page numbers; refuse to attempt allocating sparse buffers
864 * that exceed this limit. This is not really a restriction: we don't have
865 * that much virtual address space anyway.
866 */
867 if (size > (uint64_t)INT32_MAX * RADEON_SPARSE_PAGE_SIZE)
868 return NULL;
869
870 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
871 if (!bo)
872 return NULL;
873
874 pipe_reference_init(&bo->base.reference, 1);
875 bo->base.alignment = RADEON_SPARSE_PAGE_SIZE;
876 bo->base.size = size;
877 bo->base.vtbl = &amdgpu_winsys_bo_sparse_vtbl;
878 bo->ws = ws;
879 bo->initial_domain = domain;
880 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
881 bo->sparse = true;
882 bo->u.sparse.flags = flags & ~RADEON_FLAG_SPARSE;
883
884 bo->u.sparse.num_va_pages = DIV_ROUND_UP(size, RADEON_SPARSE_PAGE_SIZE);
885 bo->u.sparse.commitments = CALLOC(bo->u.sparse.num_va_pages,
886 sizeof(*bo->u.sparse.commitments));
887 if (!bo->u.sparse.commitments)
888 goto error_alloc_commitments;
889
890 simple_mtx_init(&bo->u.sparse.commit_lock, mtx_plain);
891 LIST_INITHEAD(&bo->u.sparse.backing);
892
893 /* For simplicity, we always map a multiple of the page size. */
894 map_size = align64(size, RADEON_SPARSE_PAGE_SIZE);
895 va_gap_size = ws->check_vm ? 4 * RADEON_SPARSE_PAGE_SIZE : 0;
896 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
897 map_size + va_gap_size, RADEON_SPARSE_PAGE_SIZE,
898 0, &bo->va, &bo->u.sparse.va_handle, 0);
899 if (r)
900 goto error_va_alloc;
901
902 r = amdgpu_bo_va_op_raw(bo->ws->dev, NULL, 0, size, bo->va,
903 AMDGPU_VM_PAGE_PRT, AMDGPU_VA_OP_MAP);
904 if (r)
905 goto error_va_map;
906
907 return &bo->base;
908
909 error_va_map:
910 amdgpu_va_range_free(bo->u.sparse.va_handle);
911 error_va_alloc:
912 simple_mtx_destroy(&bo->u.sparse.commit_lock);
913 FREE(bo->u.sparse.commitments);
914 error_alloc_commitments:
915 FREE(bo);
916 return NULL;
917 }
918
919 static bool
920 amdgpu_bo_sparse_commit(struct pb_buffer *buf, uint64_t offset, uint64_t size,
921 bool commit)
922 {
923 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(buf);
924 struct amdgpu_sparse_commitment *comm;
925 uint32_t va_page, end_va_page;
926 bool ok = true;
927 int r;
928
929 assert(bo->sparse);
930 assert(offset % RADEON_SPARSE_PAGE_SIZE == 0);
931 assert(offset <= bo->base.size);
932 assert(size <= bo->base.size - offset);
933 assert(size % RADEON_SPARSE_PAGE_SIZE == 0 || offset + size == bo->base.size);
934
935 comm = bo->u.sparse.commitments;
936 va_page = offset / RADEON_SPARSE_PAGE_SIZE;
937 end_va_page = va_page + DIV_ROUND_UP(size, RADEON_SPARSE_PAGE_SIZE);
938
939 simple_mtx_lock(&bo->u.sparse.commit_lock);
940
941 #if DEBUG_SPARSE_COMMITS
942 sparse_dump(bo, __func__);
943 #endif
944
945 if (commit) {
946 while (va_page < end_va_page) {
947 uint32_t span_va_page;
948
949 /* Skip pages that are already committed. */
950 if (comm[va_page].backing) {
951 va_page++;
952 continue;
953 }
954
955 /* Determine length of uncommitted span. */
956 span_va_page = va_page;
957 while (va_page < end_va_page && !comm[va_page].backing)
958 va_page++;
959
960 /* Fill the uncommitted span with chunks of backing memory. */
961 while (span_va_page < va_page) {
962 struct amdgpu_sparse_backing *backing;
963 uint32_t backing_start, backing_size;
964
965 backing_size = va_page - span_va_page;
966 backing = sparse_backing_alloc(bo, &backing_start, &backing_size);
967 if (!backing) {
968 ok = false;
969 goto out;
970 }
971
972 r = amdgpu_bo_va_op_raw(bo->ws->dev, backing->bo->bo,
973 (uint64_t)backing_start * RADEON_SPARSE_PAGE_SIZE,
974 (uint64_t)backing_size * RADEON_SPARSE_PAGE_SIZE,
975 bo->va + (uint64_t)span_va_page * RADEON_SPARSE_PAGE_SIZE,
976 AMDGPU_VM_PAGE_READABLE |
977 AMDGPU_VM_PAGE_WRITEABLE |
978 AMDGPU_VM_PAGE_EXECUTABLE,
979 AMDGPU_VA_OP_REPLACE);
980 if (r) {
981 ok = sparse_backing_free(bo, backing, backing_start, backing_size);
982 assert(ok && "sufficient memory should already be allocated");
983
984 ok = false;
985 goto out;
986 }
987
988 while (backing_size) {
989 comm[span_va_page].backing = backing;
990 comm[span_va_page].page = backing_start;
991 span_va_page++;
992 backing_start++;
993 backing_size--;
994 }
995 }
996 }
997 } else {
998 r = amdgpu_bo_va_op_raw(bo->ws->dev, NULL, 0,
999 (uint64_t)(end_va_page - va_page) * RADEON_SPARSE_PAGE_SIZE,
1000 bo->va + (uint64_t)va_page * RADEON_SPARSE_PAGE_SIZE,
1001 AMDGPU_VM_PAGE_PRT, AMDGPU_VA_OP_REPLACE);
1002 if (r) {
1003 ok = false;
1004 goto out;
1005 }
1006
1007 while (va_page < end_va_page) {
1008 struct amdgpu_sparse_backing *backing;
1009 uint32_t backing_start;
1010 uint32_t span_pages;
1011
1012 /* Skip pages that are already uncommitted. */
1013 if (!comm[va_page].backing) {
1014 va_page++;
1015 continue;
1016 }
1017
1018 /* Group contiguous spans of pages. */
1019 backing = comm[va_page].backing;
1020 backing_start = comm[va_page].page;
1021 comm[va_page].backing = NULL;
1022
1023 span_pages = 1;
1024 va_page++;
1025
1026 while (va_page < end_va_page &&
1027 comm[va_page].backing == backing &&
1028 comm[va_page].page == backing_start + span_pages) {
1029 comm[va_page].backing = NULL;
1030 va_page++;
1031 span_pages++;
1032 }
1033
1034 if (!sparse_backing_free(bo, backing, backing_start, span_pages)) {
1035 /* Couldn't allocate tracking data structures, so we have to leak */
1036 fprintf(stderr, "amdgpu: leaking PRT backing memory\n");
1037 ok = false;
1038 }
1039 }
1040 }
1041 out:
1042
1043 simple_mtx_unlock(&bo->u.sparse.commit_lock);
1044
1045 return ok;
1046 }
1047
1048 static unsigned eg_tile_split(unsigned tile_split)
1049 {
1050 switch (tile_split) {
1051 case 0: tile_split = 64; break;
1052 case 1: tile_split = 128; break;
1053 case 2: tile_split = 256; break;
1054 case 3: tile_split = 512; break;
1055 default:
1056 case 4: tile_split = 1024; break;
1057 case 5: tile_split = 2048; break;
1058 case 6: tile_split = 4096; break;
1059 }
1060 return tile_split;
1061 }
1062
1063 static unsigned eg_tile_split_rev(unsigned eg_tile_split)
1064 {
1065 switch (eg_tile_split) {
1066 case 64: return 0;
1067 case 128: return 1;
1068 case 256: return 2;
1069 case 512: return 3;
1070 default:
1071 case 1024: return 4;
1072 case 2048: return 5;
1073 case 4096: return 6;
1074 }
1075 }
1076
1077 static void amdgpu_buffer_get_metadata(struct pb_buffer *_buf,
1078 struct radeon_bo_metadata *md)
1079 {
1080 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
1081 struct amdgpu_bo_info info = {0};
1082 uint64_t tiling_flags;
1083 int r;
1084
1085 assert(bo->bo && "must not be called for slab entries");
1086
1087 r = amdgpu_bo_query_info(bo->bo, &info);
1088 if (r)
1089 return;
1090
1091 tiling_flags = info.metadata.tiling_info;
1092
1093 if (bo->ws->info.chip_class >= GFX9) {
1094 md->u.gfx9.swizzle_mode = AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
1095 } else {
1096 md->u.legacy.microtile = RADEON_LAYOUT_LINEAR;
1097 md->u.legacy.macrotile = RADEON_LAYOUT_LINEAR;
1098
1099 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 4) /* 2D_TILED_THIN1 */
1100 md->u.legacy.macrotile = RADEON_LAYOUT_TILED;
1101 else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 2) /* 1D_TILED_THIN1 */
1102 md->u.legacy.microtile = RADEON_LAYOUT_TILED;
1103
1104 md->u.legacy.pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
1105 md->u.legacy.bankw = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
1106 md->u.legacy.bankh = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
1107 md->u.legacy.tile_split = eg_tile_split(AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT));
1108 md->u.legacy.mtilea = 1 << AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
1109 md->u.legacy.num_banks = 2 << AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
1110 md->u.legacy.scanout = AMDGPU_TILING_GET(tiling_flags, MICRO_TILE_MODE) == 0; /* DISPLAY */
1111 }
1112
1113 md->size_metadata = info.metadata.size_metadata;
1114 memcpy(md->metadata, info.metadata.umd_metadata, sizeof(md->metadata));
1115 }
1116
1117 static void amdgpu_buffer_set_metadata(struct pb_buffer *_buf,
1118 struct radeon_bo_metadata *md)
1119 {
1120 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
1121 struct amdgpu_bo_metadata metadata = {0};
1122 uint64_t tiling_flags = 0;
1123
1124 assert(bo->bo && "must not be called for slab entries");
1125
1126 if (bo->ws->info.chip_class >= GFX9) {
1127 tiling_flags |= AMDGPU_TILING_SET(SWIZZLE_MODE, md->u.gfx9.swizzle_mode);
1128 } else {
1129 if (md->u.legacy.macrotile == RADEON_LAYOUT_TILED)
1130 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 4); /* 2D_TILED_THIN1 */
1131 else if (md->u.legacy.microtile == RADEON_LAYOUT_TILED)
1132 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 2); /* 1D_TILED_THIN1 */
1133 else
1134 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 1); /* LINEAR_ALIGNED */
1135
1136 tiling_flags |= AMDGPU_TILING_SET(PIPE_CONFIG, md->u.legacy.pipe_config);
1137 tiling_flags |= AMDGPU_TILING_SET(BANK_WIDTH, util_logbase2(md->u.legacy.bankw));
1138 tiling_flags |= AMDGPU_TILING_SET(BANK_HEIGHT, util_logbase2(md->u.legacy.bankh));
1139 if (md->u.legacy.tile_split)
1140 tiling_flags |= AMDGPU_TILING_SET(TILE_SPLIT, eg_tile_split_rev(md->u.legacy.tile_split));
1141 tiling_flags |= AMDGPU_TILING_SET(MACRO_TILE_ASPECT, util_logbase2(md->u.legacy.mtilea));
1142 tiling_flags |= AMDGPU_TILING_SET(NUM_BANKS, util_logbase2(md->u.legacy.num_banks)-1);
1143
1144 if (md->u.legacy.scanout)
1145 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 0); /* DISPLAY_MICRO_TILING */
1146 else
1147 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 1); /* THIN_MICRO_TILING */
1148 }
1149
1150 metadata.tiling_info = tiling_flags;
1151 metadata.size_metadata = md->size_metadata;
1152 memcpy(metadata.umd_metadata, md->metadata, sizeof(md->metadata));
1153
1154 amdgpu_bo_set_metadata(bo->bo, &metadata);
1155 }
1156
1157 static struct pb_buffer *
1158 amdgpu_bo_create(struct radeon_winsys *rws,
1159 uint64_t size,
1160 unsigned alignment,
1161 enum radeon_bo_domain domain,
1162 enum radeon_bo_flag flags)
1163 {
1164 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
1165 struct amdgpu_winsys_bo *bo;
1166 int heap = -1;
1167
1168 /* VRAM implies WC. This is not optional. */
1169 assert(!(domain & RADEON_DOMAIN_VRAM) || flags & RADEON_FLAG_GTT_WC);
1170
1171 /* NO_CPU_ACCESS is valid with VRAM only. */
1172 assert(domain == RADEON_DOMAIN_VRAM || !(flags & RADEON_FLAG_NO_CPU_ACCESS));
1173
1174 /* Sparse buffers must have NO_CPU_ACCESS set. */
1175 assert(!(flags & RADEON_FLAG_SPARSE) || flags & RADEON_FLAG_NO_CPU_ACCESS);
1176
1177 /* Sub-allocate small buffers from slabs. */
1178 if (!(flags & (RADEON_FLAG_NO_SUBALLOC | RADEON_FLAG_SPARSE)) &&
1179 size <= (1 << AMDGPU_SLAB_MAX_SIZE_LOG2) &&
1180 alignment <= MAX2(1 << AMDGPU_SLAB_MIN_SIZE_LOG2, util_next_power_of_two(size))) {
1181 struct pb_slab_entry *entry;
1182 int heap = radeon_get_heap_index(domain, flags);
1183
1184 if (heap < 0 || heap >= RADEON_MAX_SLAB_HEAPS)
1185 goto no_slab;
1186
1187 entry = pb_slab_alloc(&ws->bo_slabs, size, heap);
1188 if (!entry) {
1189 /* Clear the cache and try again. */
1190 pb_cache_release_all_buffers(&ws->bo_cache);
1191
1192 entry = pb_slab_alloc(&ws->bo_slabs, size, heap);
1193 }
1194 if (!entry)
1195 return NULL;
1196
1197 bo = NULL;
1198 bo = container_of(entry, bo, u.slab.entry);
1199
1200 pipe_reference_init(&bo->base.reference, 1);
1201
1202 return &bo->base;
1203 }
1204 no_slab:
1205
1206 if (flags & RADEON_FLAG_SPARSE) {
1207 assert(RADEON_SPARSE_PAGE_SIZE % alignment == 0);
1208
1209 return amdgpu_bo_sparse_create(ws, size, domain, flags);
1210 }
1211
1212 /* This flag is irrelevant for the cache. */
1213 flags &= ~RADEON_FLAG_NO_SUBALLOC;
1214
1215 /* Align size to page size. This is the minimum alignment for normal
1216 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
1217 * like constant/uniform buffers, can benefit from better and more reuse.
1218 */
1219 size = align64(size, ws->info.gart_page_size);
1220 alignment = align(alignment, ws->info.gart_page_size);
1221
1222 bool use_reusable_pool = flags & RADEON_FLAG_NO_INTERPROCESS_SHARING;
1223
1224 if (use_reusable_pool) {
1225 heap = radeon_get_heap_index(domain, flags);
1226 assert(heap >= 0 && heap < RADEON_MAX_CACHED_HEAPS);
1227
1228 /* Get a buffer from the cache. */
1229 bo = (struct amdgpu_winsys_bo*)
1230 pb_cache_reclaim_buffer(&ws->bo_cache, size, alignment, 0, heap);
1231 if (bo)
1232 return &bo->base;
1233 }
1234
1235 /* Create a new one. */
1236 bo = amdgpu_create_bo(ws, size, alignment, domain, flags, heap);
1237 if (!bo) {
1238 /* Clear the cache and try again. */
1239 pb_slabs_reclaim(&ws->bo_slabs);
1240 pb_cache_release_all_buffers(&ws->bo_cache);
1241 bo = amdgpu_create_bo(ws, size, alignment, domain, flags, heap);
1242 if (!bo)
1243 return NULL;
1244 }
1245
1246 bo->u.real.use_reusable_pool = use_reusable_pool;
1247 return &bo->base;
1248 }
1249
1250 static struct pb_buffer *amdgpu_bo_from_handle(struct radeon_winsys *rws,
1251 struct winsys_handle *whandle,
1252 unsigned *stride,
1253 unsigned *offset)
1254 {
1255 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
1256 struct amdgpu_winsys_bo *bo;
1257 enum amdgpu_bo_handle_type type;
1258 struct amdgpu_bo_import_result result = {0};
1259 uint64_t va;
1260 amdgpu_va_handle va_handle;
1261 struct amdgpu_bo_info info = {0};
1262 enum radeon_bo_domain initial = 0;
1263 int r;
1264
1265 /* Initialize the structure. */
1266 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
1267 if (!bo) {
1268 return NULL;
1269 }
1270
1271 switch (whandle->type) {
1272 case DRM_API_HANDLE_TYPE_SHARED:
1273 type = amdgpu_bo_handle_type_gem_flink_name;
1274 break;
1275 case DRM_API_HANDLE_TYPE_FD:
1276 type = amdgpu_bo_handle_type_dma_buf_fd;
1277 break;
1278 default:
1279 return NULL;
1280 }
1281
1282 r = amdgpu_bo_import(ws->dev, type, whandle->handle, &result);
1283 if (r)
1284 goto error;
1285
1286 /* Get initial domains. */
1287 r = amdgpu_bo_query_info(result.buf_handle, &info);
1288 if (r)
1289 goto error_query;
1290
1291 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
1292 result.alloc_size, 1 << 20, 0, &va, &va_handle, 0);
1293 if (r)
1294 goto error_query;
1295
1296 r = amdgpu_bo_va_op(result.buf_handle, 0, result.alloc_size, va, 0, AMDGPU_VA_OP_MAP);
1297 if (r)
1298 goto error_va_map;
1299
1300 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_VRAM)
1301 initial |= RADEON_DOMAIN_VRAM;
1302 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_GTT)
1303 initial |= RADEON_DOMAIN_GTT;
1304
1305
1306 pipe_reference_init(&bo->base.reference, 1);
1307 bo->base.alignment = info.phys_alignment;
1308 bo->bo = result.buf_handle;
1309 bo->base.size = result.alloc_size;
1310 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
1311 bo->ws = ws;
1312 bo->va = va;
1313 bo->u.real.va_handle = va_handle;
1314 bo->initial_domain = initial;
1315 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
1316 bo->is_shared = true;
1317
1318 if (stride)
1319 *stride = whandle->stride;
1320 if (offset)
1321 *offset = whandle->offset;
1322
1323 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
1324 ws->allocated_vram += align64(bo->base.size, ws->info.gart_page_size);
1325 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
1326 ws->allocated_gtt += align64(bo->base.size, ws->info.gart_page_size);
1327
1328 amdgpu_add_buffer_to_global_list(bo);
1329
1330 return &bo->base;
1331
1332 error_va_map:
1333 amdgpu_va_range_free(va_handle);
1334
1335 error_query:
1336 amdgpu_bo_free(result.buf_handle);
1337
1338 error:
1339 FREE(bo);
1340 return NULL;
1341 }
1342
1343 static bool amdgpu_bo_get_handle(struct pb_buffer *buffer,
1344 unsigned stride, unsigned offset,
1345 unsigned slice_size,
1346 struct winsys_handle *whandle)
1347 {
1348 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(buffer);
1349 enum amdgpu_bo_handle_type type;
1350 int r;
1351
1352 /* Don't allow exports of slab entries and sparse buffers. */
1353 if (!bo->bo)
1354 return false;
1355
1356 bo->u.real.use_reusable_pool = false;
1357
1358 switch (whandle->type) {
1359 case DRM_API_HANDLE_TYPE_SHARED:
1360 type = amdgpu_bo_handle_type_gem_flink_name;
1361 break;
1362 case DRM_API_HANDLE_TYPE_FD:
1363 type = amdgpu_bo_handle_type_dma_buf_fd;
1364 break;
1365 case DRM_API_HANDLE_TYPE_KMS:
1366 type = amdgpu_bo_handle_type_kms;
1367 break;
1368 default:
1369 return false;
1370 }
1371
1372 r = amdgpu_bo_export(bo->bo, type, &whandle->handle);
1373 if (r)
1374 return false;
1375
1376 whandle->stride = stride;
1377 whandle->offset = offset;
1378 whandle->offset += slice_size * whandle->layer;
1379 bo->is_shared = true;
1380 return true;
1381 }
1382
1383 static struct pb_buffer *amdgpu_bo_from_ptr(struct radeon_winsys *rws,
1384 void *pointer, uint64_t size)
1385 {
1386 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
1387 amdgpu_bo_handle buf_handle;
1388 struct amdgpu_winsys_bo *bo;
1389 uint64_t va;
1390 amdgpu_va_handle va_handle;
1391 /* Avoid failure when the size is not page aligned */
1392 uint64_t aligned_size = align64(size, ws->info.gart_page_size);
1393
1394 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
1395 if (!bo)
1396 return NULL;
1397
1398 if (amdgpu_create_bo_from_user_mem(ws->dev, pointer,
1399 aligned_size, &buf_handle))
1400 goto error;
1401
1402 if (amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
1403 aligned_size, 1 << 12, 0, &va, &va_handle, 0))
1404 goto error_va_alloc;
1405
1406 if (amdgpu_bo_va_op(buf_handle, 0, aligned_size, va, 0, AMDGPU_VA_OP_MAP))
1407 goto error_va_map;
1408
1409 /* Initialize it. */
1410 pipe_reference_init(&bo->base.reference, 1);
1411 bo->bo = buf_handle;
1412 bo->base.alignment = 0;
1413 bo->base.size = size;
1414 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
1415 bo->ws = ws;
1416 bo->user_ptr = pointer;
1417 bo->va = va;
1418 bo->u.real.va_handle = va_handle;
1419 bo->initial_domain = RADEON_DOMAIN_GTT;
1420 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
1421
1422 ws->allocated_gtt += aligned_size;
1423
1424 amdgpu_add_buffer_to_global_list(bo);
1425
1426 return (struct pb_buffer*)bo;
1427
1428 error_va_map:
1429 amdgpu_va_range_free(va_handle);
1430
1431 error_va_alloc:
1432 amdgpu_bo_free(buf_handle);
1433
1434 error:
1435 FREE(bo);
1436 return NULL;
1437 }
1438
1439 static bool amdgpu_bo_is_user_ptr(struct pb_buffer *buf)
1440 {
1441 return ((struct amdgpu_winsys_bo*)buf)->user_ptr != NULL;
1442 }
1443
1444 static bool amdgpu_bo_is_suballocated(struct pb_buffer *buf)
1445 {
1446 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
1447
1448 return !bo->bo && !bo->sparse;
1449 }
1450
1451 static uint64_t amdgpu_bo_get_va(struct pb_buffer *buf)
1452 {
1453 return ((struct amdgpu_winsys_bo*)buf)->va;
1454 }
1455
1456 void amdgpu_bo_init_functions(struct amdgpu_winsys *ws)
1457 {
1458 ws->base.buffer_set_metadata = amdgpu_buffer_set_metadata;
1459 ws->base.buffer_get_metadata = amdgpu_buffer_get_metadata;
1460 ws->base.buffer_map = amdgpu_bo_map;
1461 ws->base.buffer_unmap = amdgpu_bo_unmap;
1462 ws->base.buffer_wait = amdgpu_bo_wait;
1463 ws->base.buffer_create = amdgpu_bo_create;
1464 ws->base.buffer_from_handle = amdgpu_bo_from_handle;
1465 ws->base.buffer_from_ptr = amdgpu_bo_from_ptr;
1466 ws->base.buffer_is_user_ptr = amdgpu_bo_is_user_ptr;
1467 ws->base.buffer_is_suballocated = amdgpu_bo_is_suballocated;
1468 ws->base.buffer_get_handle = amdgpu_bo_get_handle;
1469 ws->base.buffer_commit = amdgpu_bo_sparse_commit;
1470 ws->base.buffer_get_virtual_address = amdgpu_bo_get_va;
1471 ws->base.buffer_get_initial_domain = amdgpu_bo_get_initial_domain;
1472 }