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