gallium/radeon: move setting VRAM|GTT into winsyses
[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 unsigned usage,
377 enum radeon_bo_domain initial_domain,
378 unsigned flags,
379 unsigned pb_cache_bucket)
380 {
381 struct amdgpu_bo_alloc_request request = {0};
382 amdgpu_bo_handle buf_handle;
383 uint64_t va = 0;
384 struct amdgpu_winsys_bo *bo;
385 amdgpu_va_handle va_handle;
386 unsigned va_gap_size;
387 int r;
388
389 /* VRAM or GTT must be specified, but not both at the same time. */
390 assert(util_bitcount(initial_domain & RADEON_DOMAIN_VRAM_GTT) == 1);
391
392 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
393 if (!bo) {
394 return NULL;
395 }
396
397 pb_cache_init_entry(&ws->bo_cache, &bo->u.real.cache_entry, &bo->base,
398 pb_cache_bucket);
399 request.alloc_size = size;
400 request.phys_alignment = alignment;
401
402 if (initial_domain & RADEON_DOMAIN_VRAM)
403 request.preferred_heap |= AMDGPU_GEM_DOMAIN_VRAM;
404 if (initial_domain & RADEON_DOMAIN_GTT)
405 request.preferred_heap |= AMDGPU_GEM_DOMAIN_GTT;
406
407 /* If VRAM is just stolen system memory, allow both VRAM and
408 * GTT, whichever has free space. If a buffer is evicted from
409 * VRAM to GTT, it will stay there.
410 *
411 * DRM 3.6.0 has good BO move throttling, so we can allow VRAM-only
412 * placements even with a low amount of stolen VRAM.
413 */
414 if (!ws->info.has_dedicated_vram && ws->info.drm_minor < 6)
415 request.preferred_heap |= AMDGPU_GEM_DOMAIN_GTT;
416
417 if (flags & RADEON_FLAG_NO_CPU_ACCESS)
418 request.flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
419 if (flags & RADEON_FLAG_GTT_WC)
420 request.flags |= AMDGPU_GEM_CREATE_CPU_GTT_USWC;
421 if (flags & RADEON_FLAG_NO_INTERPROCESS_SHARING &&
422 ws->info.drm_minor >= 20)
423 request.flags |= AMDGPU_GEM_CREATE_VM_ALWAYS_VALID;
424
425 r = amdgpu_bo_alloc(ws->dev, &request, &buf_handle);
426 if (r) {
427 fprintf(stderr, "amdgpu: Failed to allocate a buffer:\n");
428 fprintf(stderr, "amdgpu: size : %"PRIu64" bytes\n", size);
429 fprintf(stderr, "amdgpu: alignment : %u bytes\n", alignment);
430 fprintf(stderr, "amdgpu: domains : %u\n", initial_domain);
431 goto error_bo_alloc;
432 }
433
434 va_gap_size = ws->check_vm ? MAX2(4 * alignment, 64 * 1024) : 0;
435 if (size > ws->info.pte_fragment_size)
436 alignment = MAX2(alignment, ws->info.pte_fragment_size);
437 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
438 size + va_gap_size, alignment, 0, &va, &va_handle, 0);
439 if (r)
440 goto error_va_alloc;
441
442 r = amdgpu_bo_va_op(buf_handle, 0, size, va, 0, AMDGPU_VA_OP_MAP);
443 if (r)
444 goto error_va_map;
445
446 pipe_reference_init(&bo->base.reference, 1);
447 bo->base.alignment = alignment;
448 bo->base.usage = usage;
449 bo->base.size = size;
450 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
451 bo->ws = ws;
452 bo->bo = buf_handle;
453 bo->va = va;
454 bo->u.real.va_handle = va_handle;
455 bo->initial_domain = initial_domain;
456 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
457 bo->is_local = !!(request.flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID);
458
459 if (initial_domain & RADEON_DOMAIN_VRAM)
460 ws->allocated_vram += align64(size, ws->info.gart_page_size);
461 else if (initial_domain & RADEON_DOMAIN_GTT)
462 ws->allocated_gtt += align64(size, ws->info.gart_page_size);
463
464 amdgpu_add_buffer_to_global_list(bo);
465
466 return bo;
467
468 error_va_map:
469 amdgpu_va_range_free(va_handle);
470
471 error_va_alloc:
472 amdgpu_bo_free(buf_handle);
473
474 error_bo_alloc:
475 FREE(bo);
476 return NULL;
477 }
478
479 bool amdgpu_bo_can_reclaim(struct pb_buffer *_buf)
480 {
481 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
482
483 if (amdgpu_bo_is_referenced_by_any_cs(bo)) {
484 return false;
485 }
486
487 return amdgpu_bo_wait(_buf, 0, RADEON_USAGE_READWRITE);
488 }
489
490 bool amdgpu_bo_can_reclaim_slab(void *priv, struct pb_slab_entry *entry)
491 {
492 struct amdgpu_winsys_bo *bo = NULL; /* fix container_of */
493 bo = container_of(entry, bo, u.slab.entry);
494
495 return amdgpu_bo_can_reclaim(&bo->base);
496 }
497
498 static void amdgpu_bo_slab_destroy(struct pb_buffer *_buf)
499 {
500 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
501
502 assert(!bo->bo);
503
504 pb_slab_free(&bo->ws->bo_slabs, &bo->u.slab.entry);
505 }
506
507 static const struct pb_vtbl amdgpu_winsys_bo_slab_vtbl = {
508 amdgpu_bo_slab_destroy
509 /* other functions are never called */
510 };
511
512 struct pb_slab *amdgpu_bo_slab_alloc(void *priv, unsigned heap,
513 unsigned entry_size,
514 unsigned group_index)
515 {
516 struct amdgpu_winsys *ws = priv;
517 struct amdgpu_slab *slab = CALLOC_STRUCT(amdgpu_slab);
518 enum radeon_bo_domain domains = radeon_domain_from_heap(heap);
519 enum radeon_bo_flag flags = radeon_flags_from_heap(heap);
520 uint32_t base_id;
521
522 if (!slab)
523 return NULL;
524
525 unsigned slab_size = 1 << AMDGPU_SLAB_BO_SIZE_LOG2;
526 slab->buffer = amdgpu_winsys_bo(amdgpu_bo_create(&ws->base,
527 slab_size, slab_size,
528 domains, flags));
529 if (!slab->buffer)
530 goto fail;
531
532 assert(slab->buffer->bo);
533
534 slab->base.num_entries = slab->buffer->base.size / entry_size;
535 slab->base.num_free = slab->base.num_entries;
536 slab->entries = CALLOC(slab->base.num_entries, sizeof(*slab->entries));
537 if (!slab->entries)
538 goto fail_buffer;
539
540 LIST_INITHEAD(&slab->base.free);
541
542 base_id = __sync_fetch_and_add(&ws->next_bo_unique_id, slab->base.num_entries);
543
544 for (unsigned i = 0; i < slab->base.num_entries; ++i) {
545 struct amdgpu_winsys_bo *bo = &slab->entries[i];
546
547 bo->base.alignment = entry_size;
548 bo->base.usage = slab->buffer->base.usage;
549 bo->base.size = entry_size;
550 bo->base.vtbl = &amdgpu_winsys_bo_slab_vtbl;
551 bo->ws = ws;
552 bo->va = slab->buffer->va + i * entry_size;
553 bo->initial_domain = domains;
554 bo->unique_id = base_id + i;
555 bo->u.slab.entry.slab = &slab->base;
556 bo->u.slab.entry.group_index = group_index;
557 bo->u.slab.real = slab->buffer;
558
559 LIST_ADDTAIL(&bo->u.slab.entry.head, &slab->base.free);
560 }
561
562 return &slab->base;
563
564 fail_buffer:
565 amdgpu_winsys_bo_reference(&slab->buffer, NULL);
566 fail:
567 FREE(slab);
568 return NULL;
569 }
570
571 void amdgpu_bo_slab_free(void *priv, struct pb_slab *pslab)
572 {
573 struct amdgpu_slab *slab = amdgpu_slab(pslab);
574
575 for (unsigned i = 0; i < slab->base.num_entries; ++i)
576 amdgpu_bo_remove_fences(&slab->entries[i]);
577
578 FREE(slab->entries);
579 amdgpu_winsys_bo_reference(&slab->buffer, NULL);
580 FREE(slab);
581 }
582
583 #if DEBUG_SPARSE_COMMITS
584 static void
585 sparse_dump(struct amdgpu_winsys_bo *bo, const char *func)
586 {
587 fprintf(stderr, "%s: %p (size=%"PRIu64", num_va_pages=%u) @ %s\n"
588 "Commitments:\n",
589 __func__, bo, bo->base.size, bo->u.sparse.num_va_pages, func);
590
591 struct amdgpu_sparse_backing *span_backing = NULL;
592 uint32_t span_first_backing_page = 0;
593 uint32_t span_first_va_page = 0;
594 uint32_t va_page = 0;
595
596 for (;;) {
597 struct amdgpu_sparse_backing *backing = 0;
598 uint32_t backing_page = 0;
599
600 if (va_page < bo->u.sparse.num_va_pages) {
601 backing = bo->u.sparse.commitments[va_page].backing;
602 backing_page = bo->u.sparse.commitments[va_page].page;
603 }
604
605 if (span_backing &&
606 (backing != span_backing ||
607 backing_page != span_first_backing_page + (va_page - span_first_va_page))) {
608 fprintf(stderr, " %u..%u: backing=%p:%u..%u\n",
609 span_first_va_page, va_page - 1, span_backing,
610 span_first_backing_page,
611 span_first_backing_page + (va_page - span_first_va_page) - 1);
612
613 span_backing = NULL;
614 }
615
616 if (va_page >= bo->u.sparse.num_va_pages)
617 break;
618
619 if (backing && !span_backing) {
620 span_backing = backing;
621 span_first_backing_page = backing_page;
622 span_first_va_page = va_page;
623 }
624
625 va_page++;
626 }
627
628 fprintf(stderr, "Backing:\n");
629
630 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
631 fprintf(stderr, " %p (size=%"PRIu64")\n", backing, backing->bo->base.size);
632 for (unsigned i = 0; i < backing->num_chunks; ++i)
633 fprintf(stderr, " %u..%u\n", backing->chunks[i].begin, backing->chunks[i].end);
634 }
635 }
636 #endif
637
638 /*
639 * Attempt to allocate the given number of backing pages. Fewer pages may be
640 * allocated (depending on the fragmentation of existing backing buffers),
641 * which will be reflected by a change to *pnum_pages.
642 */
643 static struct amdgpu_sparse_backing *
644 sparse_backing_alloc(struct amdgpu_winsys_bo *bo, uint32_t *pstart_page, uint32_t *pnum_pages)
645 {
646 struct amdgpu_sparse_backing *best_backing;
647 unsigned best_idx;
648 uint32_t best_num_pages;
649
650 best_backing = NULL;
651 best_idx = 0;
652 best_num_pages = 0;
653
654 /* This is a very simple and inefficient best-fit algorithm. */
655 list_for_each_entry(struct amdgpu_sparse_backing, backing, &bo->u.sparse.backing, list) {
656 for (unsigned idx = 0; idx < backing->num_chunks; ++idx) {
657 uint32_t cur_num_pages = backing->chunks[idx].end - backing->chunks[idx].begin;
658 if ((best_num_pages < *pnum_pages && cur_num_pages > best_num_pages) ||
659 (best_num_pages > *pnum_pages && cur_num_pages < best_num_pages)) {
660 best_backing = backing;
661 best_idx = idx;
662 best_num_pages = cur_num_pages;
663 }
664 }
665 }
666
667 /* Allocate a new backing buffer if necessary. */
668 if (!best_backing) {
669 struct pb_buffer *buf;
670 uint64_t size;
671 uint32_t pages;
672
673 best_backing = CALLOC_STRUCT(amdgpu_sparse_backing);
674 if (!best_backing)
675 return NULL;
676
677 best_backing->max_chunks = 4;
678 best_backing->chunks = CALLOC(best_backing->max_chunks,
679 sizeof(*best_backing->chunks));
680 if (!best_backing->chunks) {
681 FREE(best_backing);
682 return NULL;
683 }
684
685 assert(bo->u.sparse.num_backing_pages < DIV_ROUND_UP(bo->base.size, RADEON_SPARSE_PAGE_SIZE));
686
687 size = MIN3(bo->base.size / 16,
688 8 * 1024 * 1024,
689 bo->base.size - (uint64_t)bo->u.sparse.num_backing_pages * RADEON_SPARSE_PAGE_SIZE);
690 size = MAX2(size, RADEON_SPARSE_PAGE_SIZE);
691
692 buf = amdgpu_bo_create(&bo->ws->base, size, RADEON_SPARSE_PAGE_SIZE,
693 bo->initial_domain,
694 bo->u.sparse.flags | RADEON_FLAG_NO_SUBALLOC);
695 if (!buf) {
696 FREE(best_backing->chunks);
697 FREE(best_backing);
698 return NULL;
699 }
700
701 /* We might have gotten a bigger buffer than requested via caching. */
702 pages = buf->size / RADEON_SPARSE_PAGE_SIZE;
703
704 best_backing->bo = amdgpu_winsys_bo(buf);
705 best_backing->num_chunks = 1;
706 best_backing->chunks[0].begin = 0;
707 best_backing->chunks[0].end = pages;
708
709 list_add(&best_backing->list, &bo->u.sparse.backing);
710 bo->u.sparse.num_backing_pages += pages;
711
712 best_idx = 0;
713 best_num_pages = pages;
714 }
715
716 *pnum_pages = MIN2(*pnum_pages, best_num_pages);
717 *pstart_page = best_backing->chunks[best_idx].begin;
718 best_backing->chunks[best_idx].begin += *pnum_pages;
719
720 if (best_backing->chunks[best_idx].begin >= best_backing->chunks[best_idx].end) {
721 memmove(&best_backing->chunks[best_idx], &best_backing->chunks[best_idx + 1],
722 sizeof(*best_backing->chunks) * (best_backing->num_chunks - best_idx - 1));
723 best_backing->num_chunks--;
724 }
725
726 return best_backing;
727 }
728
729 static void
730 sparse_free_backing_buffer(struct amdgpu_winsys_bo *bo,
731 struct amdgpu_sparse_backing *backing)
732 {
733 struct amdgpu_winsys *ws = backing->bo->ws;
734
735 bo->u.sparse.num_backing_pages -= backing->bo->base.size / RADEON_SPARSE_PAGE_SIZE;
736
737 simple_mtx_lock(&ws->bo_fence_lock);
738 amdgpu_add_fences(backing->bo, bo->num_fences, bo->fences);
739 simple_mtx_unlock(&ws->bo_fence_lock);
740
741 list_del(&backing->list);
742 amdgpu_winsys_bo_reference(&backing->bo, NULL);
743 FREE(backing->chunks);
744 FREE(backing);
745 }
746
747 /*
748 * Return a range of pages from the given backing buffer back into the
749 * free structure.
750 */
751 static bool
752 sparse_backing_free(struct amdgpu_winsys_bo *bo,
753 struct amdgpu_sparse_backing *backing,
754 uint32_t start_page, uint32_t num_pages)
755 {
756 uint32_t end_page = start_page + num_pages;
757 unsigned low = 0;
758 unsigned high = backing->num_chunks;
759
760 /* Find the first chunk with begin >= start_page. */
761 while (low < high) {
762 unsigned mid = low + (high - low) / 2;
763
764 if (backing->chunks[mid].begin >= start_page)
765 high = mid;
766 else
767 low = mid + 1;
768 }
769
770 assert(low >= backing->num_chunks || end_page <= backing->chunks[low].begin);
771 assert(low == 0 || backing->chunks[low - 1].end <= start_page);
772
773 if (low > 0 && backing->chunks[low - 1].end == start_page) {
774 backing->chunks[low - 1].end = end_page;
775
776 if (low < backing->num_chunks && end_page == backing->chunks[low].begin) {
777 backing->chunks[low - 1].end = backing->chunks[low].end;
778 memmove(&backing->chunks[low], &backing->chunks[low + 1],
779 sizeof(*backing->chunks) * (backing->num_chunks - low - 1));
780 backing->num_chunks--;
781 }
782 } else if (low < backing->num_chunks && end_page == backing->chunks[low].begin) {
783 backing->chunks[low].begin = start_page;
784 } else {
785 if (backing->num_chunks >= backing->max_chunks) {
786 unsigned new_max_chunks = 2 * backing->max_chunks;
787 struct amdgpu_sparse_backing_chunk *new_chunks =
788 REALLOC(backing->chunks,
789 sizeof(*backing->chunks) * backing->max_chunks,
790 sizeof(*backing->chunks) * new_max_chunks);
791 if (!new_chunks)
792 return false;
793
794 backing->max_chunks = new_max_chunks;
795 backing->chunks = new_chunks;
796 }
797
798 memmove(&backing->chunks[low + 1], &backing->chunks[low],
799 sizeof(*backing->chunks) * (backing->num_chunks - low));
800 backing->chunks[low].begin = start_page;
801 backing->chunks[low].end = end_page;
802 backing->num_chunks++;
803 }
804
805 if (backing->num_chunks == 1 && backing->chunks[0].begin == 0 &&
806 backing->chunks[0].end == backing->bo->base.size / RADEON_SPARSE_PAGE_SIZE)
807 sparse_free_backing_buffer(bo, backing);
808
809 return true;
810 }
811
812 static void amdgpu_bo_sparse_destroy(struct pb_buffer *_buf)
813 {
814 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
815 int r;
816
817 assert(!bo->bo && bo->sparse);
818
819 r = amdgpu_bo_va_op_raw(bo->ws->dev, NULL, 0,
820 (uint64_t)bo->u.sparse.num_va_pages * RADEON_SPARSE_PAGE_SIZE,
821 bo->va, 0, AMDGPU_VA_OP_CLEAR);
822 if (r) {
823 fprintf(stderr, "amdgpu: clearing PRT VA region on destroy failed (%d)\n", r);
824 }
825
826 while (!list_empty(&bo->u.sparse.backing)) {
827 struct amdgpu_sparse_backing *dummy = NULL;
828 sparse_free_backing_buffer(bo,
829 container_of(bo->u.sparse.backing.next,
830 dummy, list));
831 }
832
833 amdgpu_va_range_free(bo->u.sparse.va_handle);
834 simple_mtx_destroy(&bo->u.sparse.commit_lock);
835 FREE(bo->u.sparse.commitments);
836 FREE(bo);
837 }
838
839 static const struct pb_vtbl amdgpu_winsys_bo_sparse_vtbl = {
840 amdgpu_bo_sparse_destroy
841 /* other functions are never called */
842 };
843
844 static struct pb_buffer *
845 amdgpu_bo_sparse_create(struct amdgpu_winsys *ws, uint64_t size,
846 enum radeon_bo_domain domain,
847 enum radeon_bo_flag flags)
848 {
849 struct amdgpu_winsys_bo *bo;
850 uint64_t map_size;
851 uint64_t va_gap_size;
852 int r;
853
854 /* We use 32-bit page numbers; refuse to attempt allocating sparse buffers
855 * that exceed this limit. This is not really a restriction: we don't have
856 * that much virtual address space anyway.
857 */
858 if (size > (uint64_t)INT32_MAX * RADEON_SPARSE_PAGE_SIZE)
859 return NULL;
860
861 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
862 if (!bo)
863 return NULL;
864
865 pipe_reference_init(&bo->base.reference, 1);
866 bo->base.alignment = RADEON_SPARSE_PAGE_SIZE;
867 bo->base.size = size;
868 bo->base.vtbl = &amdgpu_winsys_bo_sparse_vtbl;
869 bo->ws = ws;
870 bo->initial_domain = domain;
871 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
872 bo->sparse = true;
873 bo->u.sparse.flags = flags & ~RADEON_FLAG_SPARSE;
874
875 bo->u.sparse.num_va_pages = DIV_ROUND_UP(size, RADEON_SPARSE_PAGE_SIZE);
876 bo->u.sparse.commitments = CALLOC(bo->u.sparse.num_va_pages,
877 sizeof(*bo->u.sparse.commitments));
878 if (!bo->u.sparse.commitments)
879 goto error_alloc_commitments;
880
881 simple_mtx_init(&bo->u.sparse.commit_lock, mtx_plain);
882 LIST_INITHEAD(&bo->u.sparse.backing);
883
884 /* For simplicity, we always map a multiple of the page size. */
885 map_size = align64(size, RADEON_SPARSE_PAGE_SIZE);
886 va_gap_size = ws->check_vm ? 4 * RADEON_SPARSE_PAGE_SIZE : 0;
887 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
888 map_size + va_gap_size, RADEON_SPARSE_PAGE_SIZE,
889 0, &bo->va, &bo->u.sparse.va_handle, 0);
890 if (r)
891 goto error_va_alloc;
892
893 r = amdgpu_bo_va_op_raw(bo->ws->dev, NULL, 0, size, bo->va,
894 AMDGPU_VM_PAGE_PRT, AMDGPU_VA_OP_MAP);
895 if (r)
896 goto error_va_map;
897
898 return &bo->base;
899
900 error_va_map:
901 amdgpu_va_range_free(bo->u.sparse.va_handle);
902 error_va_alloc:
903 simple_mtx_destroy(&bo->u.sparse.commit_lock);
904 FREE(bo->u.sparse.commitments);
905 error_alloc_commitments:
906 FREE(bo);
907 return NULL;
908 }
909
910 static bool
911 amdgpu_bo_sparse_commit(struct pb_buffer *buf, uint64_t offset, uint64_t size,
912 bool commit)
913 {
914 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(buf);
915 struct amdgpu_sparse_commitment *comm;
916 uint32_t va_page, end_va_page;
917 bool ok = true;
918 int r;
919
920 assert(bo->sparse);
921 assert(offset % RADEON_SPARSE_PAGE_SIZE == 0);
922 assert(offset <= bo->base.size);
923 assert(size <= bo->base.size - offset);
924 assert(size % RADEON_SPARSE_PAGE_SIZE == 0 || offset + size == bo->base.size);
925
926 comm = bo->u.sparse.commitments;
927 va_page = offset / RADEON_SPARSE_PAGE_SIZE;
928 end_va_page = va_page + DIV_ROUND_UP(size, RADEON_SPARSE_PAGE_SIZE);
929
930 simple_mtx_lock(&bo->u.sparse.commit_lock);
931
932 #if DEBUG_SPARSE_COMMITS
933 sparse_dump(bo, __func__);
934 #endif
935
936 if (commit) {
937 while (va_page < end_va_page) {
938 uint32_t span_va_page;
939
940 /* Skip pages that are already committed. */
941 if (comm[va_page].backing) {
942 va_page++;
943 continue;
944 }
945
946 /* Determine length of uncommitted span. */
947 span_va_page = va_page;
948 while (va_page < end_va_page && !comm[va_page].backing)
949 va_page++;
950
951 /* Fill the uncommitted span with chunks of backing memory. */
952 while (span_va_page < va_page) {
953 struct amdgpu_sparse_backing *backing;
954 uint32_t backing_start, backing_size;
955
956 backing_size = va_page - span_va_page;
957 backing = sparse_backing_alloc(bo, &backing_start, &backing_size);
958 if (!backing) {
959 ok = false;
960 goto out;
961 }
962
963 r = amdgpu_bo_va_op_raw(bo->ws->dev, backing->bo->bo,
964 (uint64_t)backing_start * RADEON_SPARSE_PAGE_SIZE,
965 (uint64_t)backing_size * RADEON_SPARSE_PAGE_SIZE,
966 bo->va + (uint64_t)span_va_page * RADEON_SPARSE_PAGE_SIZE,
967 AMDGPU_VM_PAGE_READABLE |
968 AMDGPU_VM_PAGE_WRITEABLE |
969 AMDGPU_VM_PAGE_EXECUTABLE,
970 AMDGPU_VA_OP_REPLACE);
971 if (r) {
972 ok = sparse_backing_free(bo, backing, backing_start, backing_size);
973 assert(ok && "sufficient memory should already be allocated");
974
975 ok = false;
976 goto out;
977 }
978
979 while (backing_size) {
980 comm[span_va_page].backing = backing;
981 comm[span_va_page].page = backing_start;
982 span_va_page++;
983 backing_start++;
984 backing_size--;
985 }
986 }
987 }
988 } else {
989 r = amdgpu_bo_va_op_raw(bo->ws->dev, NULL, 0,
990 (uint64_t)(end_va_page - va_page) * RADEON_SPARSE_PAGE_SIZE,
991 bo->va + (uint64_t)va_page * RADEON_SPARSE_PAGE_SIZE,
992 AMDGPU_VM_PAGE_PRT, AMDGPU_VA_OP_REPLACE);
993 if (r) {
994 ok = false;
995 goto out;
996 }
997
998 while (va_page < end_va_page) {
999 struct amdgpu_sparse_backing *backing;
1000 uint32_t backing_start;
1001 uint32_t span_pages;
1002
1003 /* Skip pages that are already uncommitted. */
1004 if (!comm[va_page].backing) {
1005 va_page++;
1006 continue;
1007 }
1008
1009 /* Group contiguous spans of pages. */
1010 backing = comm[va_page].backing;
1011 backing_start = comm[va_page].page;
1012 comm[va_page].backing = NULL;
1013
1014 span_pages = 1;
1015 va_page++;
1016
1017 while (va_page < end_va_page &&
1018 comm[va_page].backing == backing &&
1019 comm[va_page].page == backing_start + span_pages) {
1020 comm[va_page].backing = NULL;
1021 va_page++;
1022 span_pages++;
1023 }
1024
1025 if (!sparse_backing_free(bo, backing, backing_start, span_pages)) {
1026 /* Couldn't allocate tracking data structures, so we have to leak */
1027 fprintf(stderr, "amdgpu: leaking PRT backing memory\n");
1028 ok = false;
1029 }
1030 }
1031 }
1032 out:
1033
1034 simple_mtx_unlock(&bo->u.sparse.commit_lock);
1035
1036 return ok;
1037 }
1038
1039 static unsigned eg_tile_split(unsigned tile_split)
1040 {
1041 switch (tile_split) {
1042 case 0: tile_split = 64; break;
1043 case 1: tile_split = 128; break;
1044 case 2: tile_split = 256; break;
1045 case 3: tile_split = 512; break;
1046 default:
1047 case 4: tile_split = 1024; break;
1048 case 5: tile_split = 2048; break;
1049 case 6: tile_split = 4096; break;
1050 }
1051 return tile_split;
1052 }
1053
1054 static unsigned eg_tile_split_rev(unsigned eg_tile_split)
1055 {
1056 switch (eg_tile_split) {
1057 case 64: return 0;
1058 case 128: return 1;
1059 case 256: return 2;
1060 case 512: return 3;
1061 default:
1062 case 1024: return 4;
1063 case 2048: return 5;
1064 case 4096: return 6;
1065 }
1066 }
1067
1068 static void amdgpu_buffer_get_metadata(struct pb_buffer *_buf,
1069 struct radeon_bo_metadata *md)
1070 {
1071 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
1072 struct amdgpu_bo_info info = {0};
1073 uint64_t tiling_flags;
1074 int r;
1075
1076 assert(bo->bo && "must not be called for slab entries");
1077
1078 r = amdgpu_bo_query_info(bo->bo, &info);
1079 if (r)
1080 return;
1081
1082 tiling_flags = info.metadata.tiling_info;
1083
1084 if (bo->ws->info.chip_class >= GFX9) {
1085 md->u.gfx9.swizzle_mode = AMDGPU_TILING_GET(tiling_flags, SWIZZLE_MODE);
1086 } else {
1087 md->u.legacy.microtile = RADEON_LAYOUT_LINEAR;
1088 md->u.legacy.macrotile = RADEON_LAYOUT_LINEAR;
1089
1090 if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 4) /* 2D_TILED_THIN1 */
1091 md->u.legacy.macrotile = RADEON_LAYOUT_TILED;
1092 else if (AMDGPU_TILING_GET(tiling_flags, ARRAY_MODE) == 2) /* 1D_TILED_THIN1 */
1093 md->u.legacy.microtile = RADEON_LAYOUT_TILED;
1094
1095 md->u.legacy.pipe_config = AMDGPU_TILING_GET(tiling_flags, PIPE_CONFIG);
1096 md->u.legacy.bankw = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_WIDTH);
1097 md->u.legacy.bankh = 1 << AMDGPU_TILING_GET(tiling_flags, BANK_HEIGHT);
1098 md->u.legacy.tile_split = eg_tile_split(AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT));
1099 md->u.legacy.mtilea = 1 << AMDGPU_TILING_GET(tiling_flags, MACRO_TILE_ASPECT);
1100 md->u.legacy.num_banks = 2 << AMDGPU_TILING_GET(tiling_flags, NUM_BANKS);
1101 md->u.legacy.scanout = AMDGPU_TILING_GET(tiling_flags, MICRO_TILE_MODE) == 0; /* DISPLAY */
1102 }
1103
1104 md->size_metadata = info.metadata.size_metadata;
1105 memcpy(md->metadata, info.metadata.umd_metadata, sizeof(md->metadata));
1106 }
1107
1108 static void amdgpu_buffer_set_metadata(struct pb_buffer *_buf,
1109 struct radeon_bo_metadata *md)
1110 {
1111 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(_buf);
1112 struct amdgpu_bo_metadata metadata = {0};
1113 uint64_t tiling_flags = 0;
1114
1115 assert(bo->bo && "must not be called for slab entries");
1116
1117 if (bo->ws->info.chip_class >= GFX9) {
1118 tiling_flags |= AMDGPU_TILING_SET(SWIZZLE_MODE, md->u.gfx9.swizzle_mode);
1119 } else {
1120 if (md->u.legacy.macrotile == RADEON_LAYOUT_TILED)
1121 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 4); /* 2D_TILED_THIN1 */
1122 else if (md->u.legacy.microtile == RADEON_LAYOUT_TILED)
1123 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 2); /* 1D_TILED_THIN1 */
1124 else
1125 tiling_flags |= AMDGPU_TILING_SET(ARRAY_MODE, 1); /* LINEAR_ALIGNED */
1126
1127 tiling_flags |= AMDGPU_TILING_SET(PIPE_CONFIG, md->u.legacy.pipe_config);
1128 tiling_flags |= AMDGPU_TILING_SET(BANK_WIDTH, util_logbase2(md->u.legacy.bankw));
1129 tiling_flags |= AMDGPU_TILING_SET(BANK_HEIGHT, util_logbase2(md->u.legacy.bankh));
1130 if (md->u.legacy.tile_split)
1131 tiling_flags |= AMDGPU_TILING_SET(TILE_SPLIT, eg_tile_split_rev(md->u.legacy.tile_split));
1132 tiling_flags |= AMDGPU_TILING_SET(MACRO_TILE_ASPECT, util_logbase2(md->u.legacy.mtilea));
1133 tiling_flags |= AMDGPU_TILING_SET(NUM_BANKS, util_logbase2(md->u.legacy.num_banks)-1);
1134
1135 if (md->u.legacy.scanout)
1136 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 0); /* DISPLAY_MICRO_TILING */
1137 else
1138 tiling_flags |= AMDGPU_TILING_SET(MICRO_TILE_MODE, 1); /* THIN_MICRO_TILING */
1139 }
1140
1141 metadata.tiling_info = tiling_flags;
1142 metadata.size_metadata = md->size_metadata;
1143 memcpy(metadata.umd_metadata, md->metadata, sizeof(md->metadata));
1144
1145 amdgpu_bo_set_metadata(bo->bo, &metadata);
1146 }
1147
1148 static struct pb_buffer *
1149 amdgpu_bo_create(struct radeon_winsys *rws,
1150 uint64_t size,
1151 unsigned alignment,
1152 enum radeon_bo_domain domain,
1153 enum radeon_bo_flag flags)
1154 {
1155 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
1156 struct amdgpu_winsys_bo *bo;
1157 unsigned usage = 0, pb_cache_bucket = 0;
1158
1159 /* VRAM implies WC. This is not optional. */
1160 assert(!(domain & RADEON_DOMAIN_VRAM) || flags & RADEON_FLAG_GTT_WC);
1161
1162 /* NO_CPU_ACCESS is valid with VRAM only. */
1163 assert(domain == RADEON_DOMAIN_VRAM || !(flags & RADEON_FLAG_NO_CPU_ACCESS));
1164
1165 /* Sub-allocate small buffers from slabs. */
1166 if (!(flags & (RADEON_FLAG_NO_SUBALLOC | RADEON_FLAG_SPARSE)) &&
1167 size <= (1 << AMDGPU_SLAB_MAX_SIZE_LOG2) &&
1168 alignment <= MAX2(1 << AMDGPU_SLAB_MIN_SIZE_LOG2, util_next_power_of_two(size))) {
1169 struct pb_slab_entry *entry;
1170 int heap = radeon_get_heap_index(domain, flags);
1171
1172 if (heap < 0 || heap >= RADEON_MAX_SLAB_HEAPS)
1173 goto no_slab;
1174
1175 entry = pb_slab_alloc(&ws->bo_slabs, size, heap);
1176 if (!entry) {
1177 /* Clear the cache and try again. */
1178 pb_cache_release_all_buffers(&ws->bo_cache);
1179
1180 entry = pb_slab_alloc(&ws->bo_slabs, size, heap);
1181 }
1182 if (!entry)
1183 return NULL;
1184
1185 bo = NULL;
1186 bo = container_of(entry, bo, u.slab.entry);
1187
1188 pipe_reference_init(&bo->base.reference, 1);
1189
1190 return &bo->base;
1191 }
1192 no_slab:
1193
1194 if (flags & RADEON_FLAG_SPARSE) {
1195 assert(RADEON_SPARSE_PAGE_SIZE % alignment == 0);
1196
1197 flags |= RADEON_FLAG_NO_CPU_ACCESS;
1198
1199 return amdgpu_bo_sparse_create(ws, size, domain, flags);
1200 }
1201
1202 /* This flag is irrelevant for the cache. */
1203 flags &= ~RADEON_FLAG_NO_SUBALLOC;
1204
1205 /* Align size to page size. This is the minimum alignment for normal
1206 * BOs. Aligning this here helps the cached bufmgr. Especially small BOs,
1207 * like constant/uniform buffers, can benefit from better and more reuse.
1208 */
1209 size = align64(size, ws->info.gart_page_size);
1210 alignment = align(alignment, ws->info.gart_page_size);
1211
1212 bool use_reusable_pool = flags & RADEON_FLAG_NO_INTERPROCESS_SHARING;
1213
1214 if (use_reusable_pool) {
1215 int heap = radeon_get_heap_index(domain, flags);
1216 assert(heap >= 0 && heap < RADEON_MAX_CACHED_HEAPS);
1217 usage = 1 << heap; /* Only set one usage bit for each heap. */
1218
1219 pb_cache_bucket = radeon_get_pb_cache_bucket_index(heap);
1220 assert(pb_cache_bucket < ARRAY_SIZE(ws->bo_cache.buckets));
1221
1222 /* Get a buffer from the cache. */
1223 bo = (struct amdgpu_winsys_bo*)
1224 pb_cache_reclaim_buffer(&ws->bo_cache, size, alignment, usage,
1225 pb_cache_bucket);
1226 if (bo)
1227 return &bo->base;
1228 }
1229
1230 /* Create a new one. */
1231 bo = amdgpu_create_bo(ws, size, alignment, usage, domain, flags,
1232 pb_cache_bucket);
1233 if (!bo) {
1234 /* Clear the cache and try again. */
1235 pb_slabs_reclaim(&ws->bo_slabs);
1236 pb_cache_release_all_buffers(&ws->bo_cache);
1237 bo = amdgpu_create_bo(ws, size, alignment, usage, domain, flags,
1238 pb_cache_bucket);
1239 if (!bo)
1240 return NULL;
1241 }
1242
1243 bo->u.real.use_reusable_pool = use_reusable_pool;
1244 return &bo->base;
1245 }
1246
1247 static struct pb_buffer *amdgpu_bo_from_handle(struct radeon_winsys *rws,
1248 struct winsys_handle *whandle,
1249 unsigned *stride,
1250 unsigned *offset)
1251 {
1252 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
1253 struct amdgpu_winsys_bo *bo;
1254 enum amdgpu_bo_handle_type type;
1255 struct amdgpu_bo_import_result result = {0};
1256 uint64_t va;
1257 amdgpu_va_handle va_handle;
1258 struct amdgpu_bo_info info = {0};
1259 enum radeon_bo_domain initial = 0;
1260 int r;
1261
1262 /* Initialize the structure. */
1263 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
1264 if (!bo) {
1265 return NULL;
1266 }
1267
1268 switch (whandle->type) {
1269 case DRM_API_HANDLE_TYPE_SHARED:
1270 type = amdgpu_bo_handle_type_gem_flink_name;
1271 break;
1272 case DRM_API_HANDLE_TYPE_FD:
1273 type = amdgpu_bo_handle_type_dma_buf_fd;
1274 break;
1275 default:
1276 return NULL;
1277 }
1278
1279 r = amdgpu_bo_import(ws->dev, type, whandle->handle, &result);
1280 if (r)
1281 goto error;
1282
1283 /* Get initial domains. */
1284 r = amdgpu_bo_query_info(result.buf_handle, &info);
1285 if (r)
1286 goto error_query;
1287
1288 r = amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
1289 result.alloc_size, 1 << 20, 0, &va, &va_handle, 0);
1290 if (r)
1291 goto error_query;
1292
1293 r = amdgpu_bo_va_op(result.buf_handle, 0, result.alloc_size, va, 0, AMDGPU_VA_OP_MAP);
1294 if (r)
1295 goto error_va_map;
1296
1297 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_VRAM)
1298 initial |= RADEON_DOMAIN_VRAM;
1299 if (info.preferred_heap & AMDGPU_GEM_DOMAIN_GTT)
1300 initial |= RADEON_DOMAIN_GTT;
1301
1302
1303 pipe_reference_init(&bo->base.reference, 1);
1304 bo->base.alignment = info.phys_alignment;
1305 bo->bo = result.buf_handle;
1306 bo->base.size = result.alloc_size;
1307 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
1308 bo->ws = ws;
1309 bo->va = va;
1310 bo->u.real.va_handle = va_handle;
1311 bo->initial_domain = initial;
1312 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
1313 bo->is_shared = true;
1314
1315 if (stride)
1316 *stride = whandle->stride;
1317 if (offset)
1318 *offset = whandle->offset;
1319
1320 if (bo->initial_domain & RADEON_DOMAIN_VRAM)
1321 ws->allocated_vram += align64(bo->base.size, ws->info.gart_page_size);
1322 else if (bo->initial_domain & RADEON_DOMAIN_GTT)
1323 ws->allocated_gtt += align64(bo->base.size, ws->info.gart_page_size);
1324
1325 amdgpu_add_buffer_to_global_list(bo);
1326
1327 return &bo->base;
1328
1329 error_va_map:
1330 amdgpu_va_range_free(va_handle);
1331
1332 error_query:
1333 amdgpu_bo_free(result.buf_handle);
1334
1335 error:
1336 FREE(bo);
1337 return NULL;
1338 }
1339
1340 static bool amdgpu_bo_get_handle(struct pb_buffer *buffer,
1341 unsigned stride, unsigned offset,
1342 unsigned slice_size,
1343 struct winsys_handle *whandle)
1344 {
1345 struct amdgpu_winsys_bo *bo = amdgpu_winsys_bo(buffer);
1346 enum amdgpu_bo_handle_type type;
1347 int r;
1348
1349 /* Don't allow exports of slab entries and sparse buffers. */
1350 if (!bo->bo)
1351 return false;
1352
1353 bo->u.real.use_reusable_pool = false;
1354
1355 switch (whandle->type) {
1356 case DRM_API_HANDLE_TYPE_SHARED:
1357 type = amdgpu_bo_handle_type_gem_flink_name;
1358 break;
1359 case DRM_API_HANDLE_TYPE_FD:
1360 type = amdgpu_bo_handle_type_dma_buf_fd;
1361 break;
1362 case DRM_API_HANDLE_TYPE_KMS:
1363 type = amdgpu_bo_handle_type_kms;
1364 break;
1365 default:
1366 return false;
1367 }
1368
1369 r = amdgpu_bo_export(bo->bo, type, &whandle->handle);
1370 if (r)
1371 return false;
1372
1373 whandle->stride = stride;
1374 whandle->offset = offset;
1375 whandle->offset += slice_size * whandle->layer;
1376 bo->is_shared = true;
1377 return true;
1378 }
1379
1380 static struct pb_buffer *amdgpu_bo_from_ptr(struct radeon_winsys *rws,
1381 void *pointer, uint64_t size)
1382 {
1383 struct amdgpu_winsys *ws = amdgpu_winsys(rws);
1384 amdgpu_bo_handle buf_handle;
1385 struct amdgpu_winsys_bo *bo;
1386 uint64_t va;
1387 amdgpu_va_handle va_handle;
1388
1389 bo = CALLOC_STRUCT(amdgpu_winsys_bo);
1390 if (!bo)
1391 return NULL;
1392
1393 if (amdgpu_create_bo_from_user_mem(ws->dev, pointer, size, &buf_handle))
1394 goto error;
1395
1396 if (amdgpu_va_range_alloc(ws->dev, amdgpu_gpu_va_range_general,
1397 size, 1 << 12, 0, &va, &va_handle, 0))
1398 goto error_va_alloc;
1399
1400 if (amdgpu_bo_va_op(buf_handle, 0, size, va, 0, AMDGPU_VA_OP_MAP))
1401 goto error_va_map;
1402
1403 /* Initialize it. */
1404 pipe_reference_init(&bo->base.reference, 1);
1405 bo->bo = buf_handle;
1406 bo->base.alignment = 0;
1407 bo->base.size = size;
1408 bo->base.vtbl = &amdgpu_winsys_bo_vtbl;
1409 bo->ws = ws;
1410 bo->user_ptr = pointer;
1411 bo->va = va;
1412 bo->u.real.va_handle = va_handle;
1413 bo->initial_domain = RADEON_DOMAIN_GTT;
1414 bo->unique_id = __sync_fetch_and_add(&ws->next_bo_unique_id, 1);
1415
1416 ws->allocated_gtt += align64(bo->base.size, ws->info.gart_page_size);
1417
1418 amdgpu_add_buffer_to_global_list(bo);
1419
1420 return (struct pb_buffer*)bo;
1421
1422 error_va_map:
1423 amdgpu_va_range_free(va_handle);
1424
1425 error_va_alloc:
1426 amdgpu_bo_free(buf_handle);
1427
1428 error:
1429 FREE(bo);
1430 return NULL;
1431 }
1432
1433 static bool amdgpu_bo_is_user_ptr(struct pb_buffer *buf)
1434 {
1435 return ((struct amdgpu_winsys_bo*)buf)->user_ptr != NULL;
1436 }
1437
1438 static bool amdgpu_bo_is_suballocated(struct pb_buffer *buf)
1439 {
1440 struct amdgpu_winsys_bo *bo = (struct amdgpu_winsys_bo*)buf;
1441
1442 return !bo->bo && !bo->sparse;
1443 }
1444
1445 static uint64_t amdgpu_bo_get_va(struct pb_buffer *buf)
1446 {
1447 return ((struct amdgpu_winsys_bo*)buf)->va;
1448 }
1449
1450 void amdgpu_bo_init_functions(struct amdgpu_winsys *ws)
1451 {
1452 ws->base.buffer_set_metadata = amdgpu_buffer_set_metadata;
1453 ws->base.buffer_get_metadata = amdgpu_buffer_get_metadata;
1454 ws->base.buffer_map = amdgpu_bo_map;
1455 ws->base.buffer_unmap = amdgpu_bo_unmap;
1456 ws->base.buffer_wait = amdgpu_bo_wait;
1457 ws->base.buffer_create = amdgpu_bo_create;
1458 ws->base.buffer_from_handle = amdgpu_bo_from_handle;
1459 ws->base.buffer_from_ptr = amdgpu_bo_from_ptr;
1460 ws->base.buffer_is_user_ptr = amdgpu_bo_is_user_ptr;
1461 ws->base.buffer_is_suballocated = amdgpu_bo_is_suballocated;
1462 ws->base.buffer_get_handle = amdgpu_bo_get_handle;
1463 ws->base.buffer_commit = amdgpu_bo_sparse_commit;
1464 ws->base.buffer_get_virtual_address = amdgpu_bo_get_va;
1465 ws->base.buffer_get_initial_domain = amdgpu_bo_get_initial_domain;
1466 }