intel/genxml,isl: Add gen12 stencil buffer changes
[mesa.git] / src / intel / vulkan / anv_allocator.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <assert.h>
28 #include <sys/mman.h>
29
30 #include "anv_private.h"
31
32 #include "util/hash_table.h"
33 #include "util/simple_mtx.h"
34 #include "util/anon_file.h"
35
36 #ifdef HAVE_VALGRIND
37 #define VG_NOACCESS_READ(__ptr) ({ \
38 VALGRIND_MAKE_MEM_DEFINED((__ptr), sizeof(*(__ptr))); \
39 __typeof(*(__ptr)) __val = *(__ptr); \
40 VALGRIND_MAKE_MEM_NOACCESS((__ptr), sizeof(*(__ptr)));\
41 __val; \
42 })
43 #define VG_NOACCESS_WRITE(__ptr, __val) ({ \
44 VALGRIND_MAKE_MEM_UNDEFINED((__ptr), sizeof(*(__ptr))); \
45 *(__ptr) = (__val); \
46 VALGRIND_MAKE_MEM_NOACCESS((__ptr), sizeof(*(__ptr))); \
47 })
48 #else
49 #define VG_NOACCESS_READ(__ptr) (*(__ptr))
50 #define VG_NOACCESS_WRITE(__ptr, __val) (*(__ptr) = (__val))
51 #endif
52
53 #ifndef MAP_POPULATE
54 #define MAP_POPULATE 0
55 #endif
56
57 /* Design goals:
58 *
59 * - Lock free (except when resizing underlying bos)
60 *
61 * - Constant time allocation with typically only one atomic
62 *
63 * - Multiple allocation sizes without fragmentation
64 *
65 * - Can grow while keeping addresses and offset of contents stable
66 *
67 * - All allocations within one bo so we can point one of the
68 * STATE_BASE_ADDRESS pointers at it.
69 *
70 * The overall design is a two-level allocator: top level is a fixed size, big
71 * block (8k) allocator, which operates out of a bo. Allocation is done by
72 * either pulling a block from the free list or growing the used range of the
73 * bo. Growing the range may run out of space in the bo which we then need to
74 * grow. Growing the bo is tricky in a multi-threaded, lockless environment:
75 * we need to keep all pointers and contents in the old map valid. GEM bos in
76 * general can't grow, but we use a trick: we create a memfd and use ftruncate
77 * to grow it as necessary. We mmap the new size and then create a gem bo for
78 * it using the new gem userptr ioctl. Without heavy-handed locking around
79 * our allocation fast-path, there isn't really a way to munmap the old mmap,
80 * so we just keep it around until garbage collection time. While the block
81 * allocator is lockless for normal operations, we block other threads trying
82 * to allocate while we're growing the map. It sholdn't happen often, and
83 * growing is fast anyway.
84 *
85 * At the next level we can use various sub-allocators. The state pool is a
86 * pool of smaller, fixed size objects, which operates much like the block
87 * pool. It uses a free list for freeing objects, but when it runs out of
88 * space it just allocates a new block from the block pool. This allocator is
89 * intended for longer lived state objects such as SURFACE_STATE and most
90 * other persistent state objects in the API. We may need to track more info
91 * with these object and a pointer back to the CPU object (eg VkImage). In
92 * those cases we just allocate a slightly bigger object and put the extra
93 * state after the GPU state object.
94 *
95 * The state stream allocator works similar to how the i965 DRI driver streams
96 * all its state. Even with Vulkan, we need to emit transient state (whether
97 * surface state base or dynamic state base), and for that we can just get a
98 * block and fill it up. These cases are local to a command buffer and the
99 * sub-allocator need not be thread safe. The streaming allocator gets a new
100 * block when it runs out of space and chains them together so they can be
101 * easily freed.
102 */
103
104 /* Allocations are always at least 64 byte aligned, so 1 is an invalid value.
105 * We use it to indicate the free list is empty. */
106 #define EMPTY UINT32_MAX
107
108 #define PAGE_SIZE 4096
109
110 struct anv_mmap_cleanup {
111 void *map;
112 size_t size;
113 uint32_t gem_handle;
114 };
115
116 #define ANV_MMAP_CLEANUP_INIT ((struct anv_mmap_cleanup){0})
117
118 static inline uint32_t
119 ilog2_round_up(uint32_t value)
120 {
121 assert(value != 0);
122 return 32 - __builtin_clz(value - 1);
123 }
124
125 static inline uint32_t
126 round_to_power_of_two(uint32_t value)
127 {
128 return 1 << ilog2_round_up(value);
129 }
130
131 struct anv_state_table_cleanup {
132 void *map;
133 size_t size;
134 };
135
136 #define ANV_STATE_TABLE_CLEANUP_INIT ((struct anv_state_table_cleanup){0})
137 #define ANV_STATE_ENTRY_SIZE (sizeof(struct anv_free_entry))
138
139 static VkResult
140 anv_state_table_expand_range(struct anv_state_table *table, uint32_t size);
141
142 VkResult
143 anv_state_table_init(struct anv_state_table *table,
144 struct anv_device *device,
145 uint32_t initial_entries)
146 {
147 VkResult result;
148
149 table->device = device;
150
151 /* Just make it 2GB up-front. The Linux kernel won't actually back it
152 * with pages until we either map and fault on one of them or we use
153 * userptr and send a chunk of it off to the GPU.
154 */
155 table->fd = os_create_anonymous_file(BLOCK_POOL_MEMFD_SIZE, "state table");
156 if (table->fd == -1) {
157 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
158 goto fail_fd;
159 }
160
161 if (!u_vector_init(&table->cleanups,
162 round_to_power_of_two(sizeof(struct anv_state_table_cleanup)),
163 128)) {
164 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
165 goto fail_fd;
166 }
167
168 table->state.next = 0;
169 table->state.end = 0;
170 table->size = 0;
171
172 uint32_t initial_size = initial_entries * ANV_STATE_ENTRY_SIZE;
173 result = anv_state_table_expand_range(table, initial_size);
174 if (result != VK_SUCCESS)
175 goto fail_cleanups;
176
177 return VK_SUCCESS;
178
179 fail_cleanups:
180 u_vector_finish(&table->cleanups);
181 fail_fd:
182 close(table->fd);
183
184 return result;
185 }
186
187 static VkResult
188 anv_state_table_expand_range(struct anv_state_table *table, uint32_t size)
189 {
190 void *map;
191 struct anv_state_table_cleanup *cleanup;
192
193 /* Assert that we only ever grow the pool */
194 assert(size >= table->state.end);
195
196 /* Make sure that we don't go outside the bounds of the memfd */
197 if (size > BLOCK_POOL_MEMFD_SIZE)
198 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
199
200 cleanup = u_vector_add(&table->cleanups);
201 if (!cleanup)
202 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
203
204 *cleanup = ANV_STATE_TABLE_CLEANUP_INIT;
205
206 /* Just leak the old map until we destroy the pool. We can't munmap it
207 * without races or imposing locking on the block allocate fast path. On
208 * the whole the leaked maps adds up to less than the size of the
209 * current map. MAP_POPULATE seems like the right thing to do, but we
210 * should try to get some numbers.
211 */
212 map = mmap(NULL, size, PROT_READ | PROT_WRITE,
213 MAP_SHARED | MAP_POPULATE, table->fd, 0);
214 if (map == MAP_FAILED) {
215 return vk_errorf(table->device->instance, table->device,
216 VK_ERROR_OUT_OF_HOST_MEMORY, "mmap failed: %m");
217 }
218
219 cleanup->map = map;
220 cleanup->size = size;
221
222 table->map = map;
223 table->size = size;
224
225 return VK_SUCCESS;
226 }
227
228 static VkResult
229 anv_state_table_grow(struct anv_state_table *table)
230 {
231 VkResult result = VK_SUCCESS;
232
233 uint32_t used = align_u32(table->state.next * ANV_STATE_ENTRY_SIZE,
234 PAGE_SIZE);
235 uint32_t old_size = table->size;
236
237 /* The block pool is always initialized to a nonzero size and this function
238 * is always called after initialization.
239 */
240 assert(old_size > 0);
241
242 uint32_t required = MAX2(used, old_size);
243 if (used * 2 <= required) {
244 /* If we're in this case then this isn't the firsta allocation and we
245 * already have enough space on both sides to hold double what we
246 * have allocated. There's nothing for us to do.
247 */
248 goto done;
249 }
250
251 uint32_t size = old_size * 2;
252 while (size < required)
253 size *= 2;
254
255 assert(size > table->size);
256
257 result = anv_state_table_expand_range(table, size);
258
259 done:
260 return result;
261 }
262
263 void
264 anv_state_table_finish(struct anv_state_table *table)
265 {
266 struct anv_state_table_cleanup *cleanup;
267
268 u_vector_foreach(cleanup, &table->cleanups) {
269 if (cleanup->map)
270 munmap(cleanup->map, cleanup->size);
271 }
272
273 u_vector_finish(&table->cleanups);
274
275 close(table->fd);
276 }
277
278 VkResult
279 anv_state_table_add(struct anv_state_table *table, uint32_t *idx,
280 uint32_t count)
281 {
282 struct anv_block_state state, old, new;
283 VkResult result;
284
285 assert(idx);
286
287 while(1) {
288 state.u64 = __sync_fetch_and_add(&table->state.u64, count);
289 if (state.next + count <= state.end) {
290 assert(table->map);
291 struct anv_free_entry *entry = &table->map[state.next];
292 for (int i = 0; i < count; i++) {
293 entry[i].state.idx = state.next + i;
294 }
295 *idx = state.next;
296 return VK_SUCCESS;
297 } else if (state.next <= state.end) {
298 /* We allocated the first block outside the pool so we have to grow
299 * the pool. pool_state->next acts a mutex: threads who try to
300 * allocate now will get block indexes above the current limit and
301 * hit futex_wait below.
302 */
303 new.next = state.next + count;
304 do {
305 result = anv_state_table_grow(table);
306 if (result != VK_SUCCESS)
307 return result;
308 new.end = table->size / ANV_STATE_ENTRY_SIZE;
309 } while (new.end < new.next);
310
311 old.u64 = __sync_lock_test_and_set(&table->state.u64, new.u64);
312 if (old.next != state.next)
313 futex_wake(&table->state.end, INT_MAX);
314 } else {
315 futex_wait(&table->state.end, state.end, NULL);
316 continue;
317 }
318 }
319 }
320
321 void
322 anv_free_list_push(union anv_free_list *list,
323 struct anv_state_table *table,
324 uint32_t first, uint32_t count)
325 {
326 union anv_free_list current, old, new;
327 uint32_t last = first;
328
329 for (uint32_t i = 1; i < count; i++, last++)
330 table->map[last].next = last + 1;
331
332 old = *list;
333 do {
334 current = old;
335 table->map[last].next = current.offset;
336 new.offset = first;
337 new.count = current.count + 1;
338 old.u64 = __sync_val_compare_and_swap(&list->u64, current.u64, new.u64);
339 } while (old.u64 != current.u64);
340 }
341
342 struct anv_state *
343 anv_free_list_pop(union anv_free_list *list,
344 struct anv_state_table *table)
345 {
346 union anv_free_list current, new, old;
347
348 current.u64 = list->u64;
349 while (current.offset != EMPTY) {
350 __sync_synchronize();
351 new.offset = table->map[current.offset].next;
352 new.count = current.count + 1;
353 old.u64 = __sync_val_compare_and_swap(&list->u64, current.u64, new.u64);
354 if (old.u64 == current.u64) {
355 struct anv_free_entry *entry = &table->map[current.offset];
356 return &entry->state;
357 }
358 current = old;
359 }
360
361 return NULL;
362 }
363
364 /* All pointers in the ptr_free_list are assumed to be page-aligned. This
365 * means that the bottom 12 bits should all be zero.
366 */
367 #define PFL_COUNT(x) ((uintptr_t)(x) & 0xfff)
368 #define PFL_PTR(x) ((void *)((uintptr_t)(x) & ~(uintptr_t)0xfff))
369 #define PFL_PACK(ptr, count) ({ \
370 (void *)(((uintptr_t)(ptr) & ~(uintptr_t)0xfff) | ((count) & 0xfff)); \
371 })
372
373 static bool
374 anv_ptr_free_list_pop(void **list, void **elem)
375 {
376 void *current = *list;
377 while (PFL_PTR(current) != NULL) {
378 void **next_ptr = PFL_PTR(current);
379 void *new_ptr = VG_NOACCESS_READ(next_ptr);
380 unsigned new_count = PFL_COUNT(current) + 1;
381 void *new = PFL_PACK(new_ptr, new_count);
382 void *old = __sync_val_compare_and_swap(list, current, new);
383 if (old == current) {
384 *elem = PFL_PTR(current);
385 return true;
386 }
387 current = old;
388 }
389
390 return false;
391 }
392
393 static void
394 anv_ptr_free_list_push(void **list, void *elem)
395 {
396 void *old, *current;
397 void **next_ptr = elem;
398
399 /* The pointer-based free list requires that the pointer be
400 * page-aligned. This is because we use the bottom 12 bits of the
401 * pointer to store a counter to solve the ABA concurrency problem.
402 */
403 assert(((uintptr_t)elem & 0xfff) == 0);
404
405 old = *list;
406 do {
407 current = old;
408 VG_NOACCESS_WRITE(next_ptr, PFL_PTR(current));
409 unsigned new_count = PFL_COUNT(current) + 1;
410 void *new = PFL_PACK(elem, new_count);
411 old = __sync_val_compare_and_swap(list, current, new);
412 } while (old != current);
413 }
414
415 static VkResult
416 anv_block_pool_expand_range(struct anv_block_pool *pool,
417 uint32_t center_bo_offset, uint32_t size);
418
419 VkResult
420 anv_block_pool_init(struct anv_block_pool *pool,
421 struct anv_device *device,
422 uint64_t start_address,
423 uint32_t initial_size,
424 uint64_t bo_flags)
425 {
426 VkResult result;
427
428 pool->device = device;
429 pool->bo_flags = bo_flags;
430 pool->nbos = 0;
431 pool->size = 0;
432 pool->center_bo_offset = 0;
433 pool->start_address = gen_canonical_address(start_address);
434 pool->map = NULL;
435
436 /* This pointer will always point to the first BO in the list */
437 pool->bo = &pool->bos[0];
438
439 anv_bo_init(pool->bo, 0, 0);
440
441 if (!(pool->bo_flags & EXEC_OBJECT_PINNED)) {
442 /* Just make it 2GB up-front. The Linux kernel won't actually back it
443 * with pages until we either map and fault on one of them or we use
444 * userptr and send a chunk of it off to the GPU.
445 */
446 pool->fd = os_create_anonymous_file(BLOCK_POOL_MEMFD_SIZE, "block pool");
447 if (pool->fd == -1)
448 return vk_error(VK_ERROR_INITIALIZATION_FAILED);
449 } else {
450 pool->fd = -1;
451 }
452
453 if (!u_vector_init(&pool->mmap_cleanups,
454 round_to_power_of_two(sizeof(struct anv_mmap_cleanup)),
455 128)) {
456 result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
457 goto fail_fd;
458 }
459
460 pool->state.next = 0;
461 pool->state.end = 0;
462 pool->back_state.next = 0;
463 pool->back_state.end = 0;
464
465 result = anv_block_pool_expand_range(pool, 0, initial_size);
466 if (result != VK_SUCCESS)
467 goto fail_mmap_cleanups;
468
469 /* Make the entire pool available in the front of the pool. If back
470 * allocation needs to use this space, the "ends" will be re-arranged.
471 */
472 pool->state.end = pool->size;
473
474 return VK_SUCCESS;
475
476 fail_mmap_cleanups:
477 u_vector_finish(&pool->mmap_cleanups);
478 fail_fd:
479 if (!(pool->bo_flags & EXEC_OBJECT_PINNED))
480 close(pool->fd);
481
482 return result;
483 }
484
485 void
486 anv_block_pool_finish(struct anv_block_pool *pool)
487 {
488 struct anv_mmap_cleanup *cleanup;
489 const bool use_softpin = !!(pool->bo_flags & EXEC_OBJECT_PINNED);
490
491 u_vector_foreach(cleanup, &pool->mmap_cleanups) {
492 if (use_softpin)
493 anv_gem_munmap(cleanup->map, cleanup->size);
494 else
495 munmap(cleanup->map, cleanup->size);
496
497 if (cleanup->gem_handle)
498 anv_gem_close(pool->device, cleanup->gem_handle);
499 }
500
501 u_vector_finish(&pool->mmap_cleanups);
502 if (!(pool->bo_flags & EXEC_OBJECT_PINNED))
503 close(pool->fd);
504 }
505
506 static VkResult
507 anv_block_pool_expand_range(struct anv_block_pool *pool,
508 uint32_t center_bo_offset, uint32_t size)
509 {
510 void *map;
511 uint32_t gem_handle;
512 struct anv_mmap_cleanup *cleanup;
513 const bool use_softpin = !!(pool->bo_flags & EXEC_OBJECT_PINNED);
514
515 /* Assert that we only ever grow the pool */
516 assert(center_bo_offset >= pool->back_state.end);
517 assert(size - center_bo_offset >= pool->state.end);
518
519 /* Assert that we don't go outside the bounds of the memfd */
520 assert(center_bo_offset <= BLOCK_POOL_MEMFD_CENTER);
521 assert(use_softpin ||
522 size - center_bo_offset <=
523 BLOCK_POOL_MEMFD_SIZE - BLOCK_POOL_MEMFD_CENTER);
524
525 cleanup = u_vector_add(&pool->mmap_cleanups);
526 if (!cleanup)
527 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
528
529 *cleanup = ANV_MMAP_CLEANUP_INIT;
530
531 uint32_t newbo_size = size - pool->size;
532 if (use_softpin) {
533 gem_handle = anv_gem_create(pool->device, newbo_size);
534 map = anv_gem_mmap(pool->device, gem_handle, 0, newbo_size, 0);
535 if (map == MAP_FAILED)
536 return vk_errorf(pool->device->instance, pool->device,
537 VK_ERROR_MEMORY_MAP_FAILED, "gem mmap failed: %m");
538 assert(center_bo_offset == 0);
539 } else {
540 /* Just leak the old map until we destroy the pool. We can't munmap it
541 * without races or imposing locking on the block allocate fast path. On
542 * the whole the leaked maps adds up to less than the size of the
543 * current map. MAP_POPULATE seems like the right thing to do, but we
544 * should try to get some numbers.
545 */
546 map = mmap(NULL, size, PROT_READ | PROT_WRITE,
547 MAP_SHARED | MAP_POPULATE, pool->fd,
548 BLOCK_POOL_MEMFD_CENTER - center_bo_offset);
549 if (map == MAP_FAILED)
550 return vk_errorf(pool->device->instance, pool->device,
551 VK_ERROR_MEMORY_MAP_FAILED, "mmap failed: %m");
552
553 /* Now that we mapped the new memory, we can write the new
554 * center_bo_offset back into pool and update pool->map. */
555 pool->center_bo_offset = center_bo_offset;
556 pool->map = map + center_bo_offset;
557 gem_handle = anv_gem_userptr(pool->device, map, size);
558 if (gem_handle == 0) {
559 munmap(map, size);
560 return vk_errorf(pool->device->instance, pool->device,
561 VK_ERROR_TOO_MANY_OBJECTS, "userptr failed: %m");
562 }
563 }
564
565 cleanup->map = map;
566 cleanup->size = use_softpin ? newbo_size : size;
567 cleanup->gem_handle = gem_handle;
568
569 /* Regular objects are created I915_CACHING_CACHED on LLC platforms and
570 * I915_CACHING_NONE on non-LLC platforms. However, userptr objects are
571 * always created as I915_CACHING_CACHED, which on non-LLC means
572 * snooped.
573 *
574 * On platforms that support softpin, we are not going to use userptr
575 * anymore, but we still want to rely on the snooped states. So make sure
576 * everything is set to I915_CACHING_CACHED.
577 */
578 if (!pool->device->info.has_llc)
579 anv_gem_set_caching(pool->device, gem_handle, I915_CACHING_CACHED);
580
581 /* For block pool BOs we have to be a bit careful about where we place them
582 * in the GTT. There are two documented workarounds for state base address
583 * placement : Wa32bitGeneralStateOffset and Wa32bitInstructionBaseOffset
584 * which state that those two base addresses do not support 48-bit
585 * addresses and need to be placed in the bottom 32-bit range.
586 * Unfortunately, this is not quite accurate.
587 *
588 * The real problem is that we always set the size of our state pools in
589 * STATE_BASE_ADDRESS to 0xfffff (the maximum) even though the BO is most
590 * likely significantly smaller. We do this because we do not no at the
591 * time we emit STATE_BASE_ADDRESS whether or not we will need to expand
592 * the pool during command buffer building so we don't actually have a
593 * valid final size. If the address + size, as seen by STATE_BASE_ADDRESS
594 * overflows 48 bits, the GPU appears to treat all accesses to the buffer
595 * as being out of bounds and returns zero. For dynamic state, this
596 * usually just leads to rendering corruptions, but shaders that are all
597 * zero hang the GPU immediately.
598 *
599 * The easiest solution to do is exactly what the bogus workarounds say to
600 * do: restrict these buffers to 32-bit addresses. We could also pin the
601 * BO to some particular location of our choosing, but that's significantly
602 * more work than just not setting a flag. So, we explicitly DO NOT set
603 * the EXEC_OBJECT_SUPPORTS_48B_ADDRESS flag and the kernel does all of the
604 * hard work for us.
605 */
606 struct anv_bo *bo;
607 uint32_t bo_size;
608 uint64_t bo_offset;
609
610 assert(pool->nbos < ANV_MAX_BLOCK_POOL_BOS);
611
612 if (use_softpin) {
613 /* With softpin, we add a new BO to the pool, and set its offset to right
614 * where the previous BO ends (the end of the pool).
615 */
616 bo = &pool->bos[pool->nbos++];
617 bo_size = newbo_size;
618 bo_offset = pool->start_address + pool->size;
619 } else {
620 /* Without softpin, we just need one BO, and we already have a pointer to
621 * it. Simply "allocate" it from our array if we didn't do it before.
622 * The offset doesn't matter since we are not pinning the BO anyway.
623 */
624 if (pool->nbos == 0)
625 pool->nbos++;
626 bo = pool->bo;
627 bo_size = size;
628 bo_offset = 0;
629 }
630
631 anv_bo_init(bo, gem_handle, bo_size);
632 bo->offset = bo_offset;
633 bo->flags = pool->bo_flags;
634 bo->map = map;
635 pool->size = size;
636
637 return VK_SUCCESS;
638 }
639
640 static struct anv_bo *
641 anv_block_pool_get_bo(struct anv_block_pool *pool, int32_t *offset)
642 {
643 struct anv_bo *bo, *bo_found = NULL;
644 int32_t cur_offset = 0;
645
646 assert(offset);
647
648 if (!(pool->bo_flags & EXEC_OBJECT_PINNED))
649 return pool->bo;
650
651 anv_block_pool_foreach_bo(bo, pool) {
652 if (*offset < cur_offset + bo->size) {
653 bo_found = bo;
654 break;
655 }
656 cur_offset += bo->size;
657 }
658
659 assert(bo_found != NULL);
660 *offset -= cur_offset;
661
662 return bo_found;
663 }
664
665 /** Returns current memory map of the block pool.
666 *
667 * The returned pointer points to the map for the memory at the specified
668 * offset. The offset parameter is relative to the "center" of the block pool
669 * rather than the start of the block pool BO map.
670 */
671 void*
672 anv_block_pool_map(struct anv_block_pool *pool, int32_t offset)
673 {
674 if (pool->bo_flags & EXEC_OBJECT_PINNED) {
675 struct anv_bo *bo = anv_block_pool_get_bo(pool, &offset);
676 return bo->map + offset;
677 } else {
678 return pool->map + offset;
679 }
680 }
681
682 /** Grows and re-centers the block pool.
683 *
684 * We grow the block pool in one or both directions in such a way that the
685 * following conditions are met:
686 *
687 * 1) The size of the entire pool is always a power of two.
688 *
689 * 2) The pool only grows on both ends. Neither end can get
690 * shortened.
691 *
692 * 3) At the end of the allocation, we have about twice as much space
693 * allocated for each end as we have used. This way the pool doesn't
694 * grow too far in one direction or the other.
695 *
696 * 4) If the _alloc_back() has never been called, then the back portion of
697 * the pool retains a size of zero. (This makes it easier for users of
698 * the block pool that only want a one-sided pool.)
699 *
700 * 5) We have enough space allocated for at least one more block in
701 * whichever side `state` points to.
702 *
703 * 6) The center of the pool is always aligned to both the block_size of
704 * the pool and a 4K CPU page.
705 */
706 static uint32_t
707 anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state)
708 {
709 VkResult result = VK_SUCCESS;
710
711 pthread_mutex_lock(&pool->device->mutex);
712
713 assert(state == &pool->state || state == &pool->back_state);
714
715 /* Gather a little usage information on the pool. Since we may have
716 * threadsd waiting in queue to get some storage while we resize, it's
717 * actually possible that total_used will be larger than old_size. In
718 * particular, block_pool_alloc() increments state->next prior to
719 * calling block_pool_grow, so this ensures that we get enough space for
720 * which ever side tries to grow the pool.
721 *
722 * We align to a page size because it makes it easier to do our
723 * calculations later in such a way that we state page-aigned.
724 */
725 uint32_t back_used = align_u32(pool->back_state.next, PAGE_SIZE);
726 uint32_t front_used = align_u32(pool->state.next, PAGE_SIZE);
727 uint32_t total_used = front_used + back_used;
728
729 assert(state == &pool->state || back_used > 0);
730
731 uint32_t old_size = pool->size;
732
733 /* The block pool is always initialized to a nonzero size and this function
734 * is always called after initialization.
735 */
736 assert(old_size > 0);
737
738 /* The back_used and front_used may actually be smaller than the actual
739 * requirement because they are based on the next pointers which are
740 * updated prior to calling this function.
741 */
742 uint32_t back_required = MAX2(back_used, pool->center_bo_offset);
743 uint32_t front_required = MAX2(front_used, old_size - pool->center_bo_offset);
744
745 if (back_used * 2 <= back_required && front_used * 2 <= front_required) {
746 /* If we're in this case then this isn't the firsta allocation and we
747 * already have enough space on both sides to hold double what we
748 * have allocated. There's nothing for us to do.
749 */
750 goto done;
751 }
752
753 uint32_t size = old_size * 2;
754 while (size < back_required + front_required)
755 size *= 2;
756
757 assert(size > pool->size);
758
759 /* We compute a new center_bo_offset such that, when we double the size
760 * of the pool, we maintain the ratio of how much is used by each side.
761 * This way things should remain more-or-less balanced.
762 */
763 uint32_t center_bo_offset;
764 if (back_used == 0) {
765 /* If we're in this case then we have never called alloc_back(). In
766 * this case, we want keep the offset at 0 to make things as simple
767 * as possible for users that don't care about back allocations.
768 */
769 center_bo_offset = 0;
770 } else {
771 /* Try to "center" the allocation based on how much is currently in
772 * use on each side of the center line.
773 */
774 center_bo_offset = ((uint64_t)size * back_used) / total_used;
775
776 /* Align down to a multiple of the page size */
777 center_bo_offset &= ~(PAGE_SIZE - 1);
778
779 assert(center_bo_offset >= back_used);
780
781 /* Make sure we don't shrink the back end of the pool */
782 if (center_bo_offset < back_required)
783 center_bo_offset = back_required;
784
785 /* Make sure that we don't shrink the front end of the pool */
786 if (size - center_bo_offset < front_required)
787 center_bo_offset = size - front_required;
788 }
789
790 assert(center_bo_offset % PAGE_SIZE == 0);
791
792 result = anv_block_pool_expand_range(pool, center_bo_offset, size);
793
794 pool->bo->flags = pool->bo_flags;
795
796 done:
797 pthread_mutex_unlock(&pool->device->mutex);
798
799 if (result == VK_SUCCESS) {
800 /* Return the appropriate new size. This function never actually
801 * updates state->next. Instead, we let the caller do that because it
802 * needs to do so in order to maintain its concurrency model.
803 */
804 if (state == &pool->state) {
805 return pool->size - pool->center_bo_offset;
806 } else {
807 assert(pool->center_bo_offset > 0);
808 return pool->center_bo_offset;
809 }
810 } else {
811 return 0;
812 }
813 }
814
815 static uint32_t
816 anv_block_pool_alloc_new(struct anv_block_pool *pool,
817 struct anv_block_state *pool_state,
818 uint32_t block_size, uint32_t *padding)
819 {
820 struct anv_block_state state, old, new;
821
822 /* Most allocations won't generate any padding */
823 if (padding)
824 *padding = 0;
825
826 while (1) {
827 state.u64 = __sync_fetch_and_add(&pool_state->u64, block_size);
828 if (state.next + block_size <= state.end) {
829 return state.next;
830 } else if (state.next <= state.end) {
831 if (pool->bo_flags & EXEC_OBJECT_PINNED && state.next < state.end) {
832 /* We need to grow the block pool, but still have some leftover
833 * space that can't be used by that particular allocation. So we
834 * add that as a "padding", and return it.
835 */
836 uint32_t leftover = state.end - state.next;
837
838 /* If there is some leftover space in the pool, the caller must
839 * deal with it.
840 */
841 assert(leftover == 0 || padding);
842 if (padding)
843 *padding = leftover;
844 state.next += leftover;
845 }
846
847 /* We allocated the first block outside the pool so we have to grow
848 * the pool. pool_state->next acts a mutex: threads who try to
849 * allocate now will get block indexes above the current limit and
850 * hit futex_wait below.
851 */
852 new.next = state.next + block_size;
853 do {
854 new.end = anv_block_pool_grow(pool, pool_state);
855 } while (new.end < new.next);
856
857 old.u64 = __sync_lock_test_and_set(&pool_state->u64, new.u64);
858 if (old.next != state.next)
859 futex_wake(&pool_state->end, INT_MAX);
860 return state.next;
861 } else {
862 futex_wait(&pool_state->end, state.end, NULL);
863 continue;
864 }
865 }
866 }
867
868 int32_t
869 anv_block_pool_alloc(struct anv_block_pool *pool,
870 uint32_t block_size, uint32_t *padding)
871 {
872 uint32_t offset;
873
874 offset = anv_block_pool_alloc_new(pool, &pool->state, block_size, padding);
875
876 return offset;
877 }
878
879 /* Allocates a block out of the back of the block pool.
880 *
881 * This will allocated a block earlier than the "start" of the block pool.
882 * The offsets returned from this function will be negative but will still
883 * be correct relative to the block pool's map pointer.
884 *
885 * If you ever use anv_block_pool_alloc_back, then you will have to do
886 * gymnastics with the block pool's BO when doing relocations.
887 */
888 int32_t
889 anv_block_pool_alloc_back(struct anv_block_pool *pool,
890 uint32_t block_size)
891 {
892 int32_t offset = anv_block_pool_alloc_new(pool, &pool->back_state,
893 block_size, NULL);
894
895 /* The offset we get out of anv_block_pool_alloc_new() is actually the
896 * number of bytes downwards from the middle to the end of the block.
897 * We need to turn it into a (negative) offset from the middle to the
898 * start of the block.
899 */
900 assert(offset >= 0);
901 return -(offset + block_size);
902 }
903
904 VkResult
905 anv_state_pool_init(struct anv_state_pool *pool,
906 struct anv_device *device,
907 uint64_t start_address,
908 uint32_t block_size,
909 uint64_t bo_flags)
910 {
911 VkResult result = anv_block_pool_init(&pool->block_pool, device,
912 start_address,
913 block_size * 16,
914 bo_flags);
915 if (result != VK_SUCCESS)
916 return result;
917
918 result = anv_state_table_init(&pool->table, device, 64);
919 if (result != VK_SUCCESS) {
920 anv_block_pool_finish(&pool->block_pool);
921 return result;
922 }
923
924 assert(util_is_power_of_two_or_zero(block_size));
925 pool->block_size = block_size;
926 pool->back_alloc_free_list = ANV_FREE_LIST_EMPTY;
927 for (unsigned i = 0; i < ANV_STATE_BUCKETS; i++) {
928 pool->buckets[i].free_list = ANV_FREE_LIST_EMPTY;
929 pool->buckets[i].block.next = 0;
930 pool->buckets[i].block.end = 0;
931 }
932 VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
933
934 return VK_SUCCESS;
935 }
936
937 void
938 anv_state_pool_finish(struct anv_state_pool *pool)
939 {
940 VG(VALGRIND_DESTROY_MEMPOOL(pool));
941 anv_state_table_finish(&pool->table);
942 anv_block_pool_finish(&pool->block_pool);
943 }
944
945 static uint32_t
946 anv_fixed_size_state_pool_alloc_new(struct anv_fixed_size_state_pool *pool,
947 struct anv_block_pool *block_pool,
948 uint32_t state_size,
949 uint32_t block_size,
950 uint32_t *padding)
951 {
952 struct anv_block_state block, old, new;
953 uint32_t offset;
954
955 /* We don't always use anv_block_pool_alloc(), which would set *padding to
956 * zero for us. So if we have a pointer to padding, we must zero it out
957 * ourselves here, to make sure we always return some sensible value.
958 */
959 if (padding)
960 *padding = 0;
961
962 /* If our state is large, we don't need any sub-allocation from a block.
963 * Instead, we just grab whole (potentially large) blocks.
964 */
965 if (state_size >= block_size)
966 return anv_block_pool_alloc(block_pool, state_size, padding);
967
968 restart:
969 block.u64 = __sync_fetch_and_add(&pool->block.u64, state_size);
970
971 if (block.next < block.end) {
972 return block.next;
973 } else if (block.next == block.end) {
974 offset = anv_block_pool_alloc(block_pool, block_size, padding);
975 new.next = offset + state_size;
976 new.end = offset + block_size;
977 old.u64 = __sync_lock_test_and_set(&pool->block.u64, new.u64);
978 if (old.next != block.next)
979 futex_wake(&pool->block.end, INT_MAX);
980 return offset;
981 } else {
982 futex_wait(&pool->block.end, block.end, NULL);
983 goto restart;
984 }
985 }
986
987 static uint32_t
988 anv_state_pool_get_bucket(uint32_t size)
989 {
990 unsigned size_log2 = ilog2_round_up(size);
991 assert(size_log2 <= ANV_MAX_STATE_SIZE_LOG2);
992 if (size_log2 < ANV_MIN_STATE_SIZE_LOG2)
993 size_log2 = ANV_MIN_STATE_SIZE_LOG2;
994 return size_log2 - ANV_MIN_STATE_SIZE_LOG2;
995 }
996
997 static uint32_t
998 anv_state_pool_get_bucket_size(uint32_t bucket)
999 {
1000 uint32_t size_log2 = bucket + ANV_MIN_STATE_SIZE_LOG2;
1001 return 1 << size_log2;
1002 }
1003
1004 /** Helper to push a chunk into the state table.
1005 *
1006 * It creates 'count' entries into the state table and update their sizes,
1007 * offsets and maps, also pushing them as "free" states.
1008 */
1009 static void
1010 anv_state_pool_return_blocks(struct anv_state_pool *pool,
1011 uint32_t chunk_offset, uint32_t count,
1012 uint32_t block_size)
1013 {
1014 /* Disallow returning 0 chunks */
1015 assert(count != 0);
1016
1017 /* Make sure we always return chunks aligned to the block_size */
1018 assert(chunk_offset % block_size == 0);
1019
1020 uint32_t st_idx;
1021 UNUSED VkResult result = anv_state_table_add(&pool->table, &st_idx, count);
1022 assert(result == VK_SUCCESS);
1023 for (int i = 0; i < count; i++) {
1024 /* update states that were added back to the state table */
1025 struct anv_state *state_i = anv_state_table_get(&pool->table,
1026 st_idx + i);
1027 state_i->alloc_size = block_size;
1028 state_i->offset = chunk_offset + block_size * i;
1029 state_i->map = anv_block_pool_map(&pool->block_pool, state_i->offset);
1030 }
1031
1032 uint32_t block_bucket = anv_state_pool_get_bucket(block_size);
1033 anv_free_list_push(&pool->buckets[block_bucket].free_list,
1034 &pool->table, st_idx, count);
1035 }
1036
1037 /** Returns a chunk of memory back to the state pool.
1038 *
1039 * Do a two-level split. If chunk_size is bigger than divisor
1040 * (pool->block_size), we return as many divisor sized blocks as we can, from
1041 * the end of the chunk.
1042 *
1043 * The remaining is then split into smaller blocks (starting at small_size if
1044 * it is non-zero), with larger blocks always being taken from the end of the
1045 * chunk.
1046 */
1047 static void
1048 anv_state_pool_return_chunk(struct anv_state_pool *pool,
1049 uint32_t chunk_offset, uint32_t chunk_size,
1050 uint32_t small_size)
1051 {
1052 uint32_t divisor = pool->block_size;
1053 uint32_t nblocks = chunk_size / divisor;
1054 uint32_t rest = chunk_size - nblocks * divisor;
1055
1056 if (nblocks > 0) {
1057 /* First return divisor aligned and sized chunks. We start returning
1058 * larger blocks from the end fo the chunk, since they should already be
1059 * aligned to divisor. Also anv_state_pool_return_blocks() only accepts
1060 * aligned chunks.
1061 */
1062 uint32_t offset = chunk_offset + rest;
1063 anv_state_pool_return_blocks(pool, offset, nblocks, divisor);
1064 }
1065
1066 chunk_size = rest;
1067 divisor /= 2;
1068
1069 if (small_size > 0 && small_size < divisor)
1070 divisor = small_size;
1071
1072 uint32_t min_size = 1 << ANV_MIN_STATE_SIZE_LOG2;
1073
1074 /* Just as before, return larger divisor aligned blocks from the end of the
1075 * chunk first.
1076 */
1077 while (chunk_size > 0 && divisor >= min_size) {
1078 nblocks = chunk_size / divisor;
1079 rest = chunk_size - nblocks * divisor;
1080 if (nblocks > 0) {
1081 anv_state_pool_return_blocks(pool, chunk_offset + rest,
1082 nblocks, divisor);
1083 chunk_size = rest;
1084 }
1085 divisor /= 2;
1086 }
1087 }
1088
1089 static struct anv_state
1090 anv_state_pool_alloc_no_vg(struct anv_state_pool *pool,
1091 uint32_t size, uint32_t align)
1092 {
1093 uint32_t bucket = anv_state_pool_get_bucket(MAX2(size, align));
1094
1095 struct anv_state *state;
1096 uint32_t alloc_size = anv_state_pool_get_bucket_size(bucket);
1097 int32_t offset;
1098
1099 /* Try free list first. */
1100 state = anv_free_list_pop(&pool->buckets[bucket].free_list,
1101 &pool->table);
1102 if (state) {
1103 assert(state->offset >= 0);
1104 goto done;
1105 }
1106
1107 /* Try to grab a chunk from some larger bucket and split it up */
1108 for (unsigned b = bucket + 1; b < ANV_STATE_BUCKETS; b++) {
1109 state = anv_free_list_pop(&pool->buckets[b].free_list, &pool->table);
1110 if (state) {
1111 unsigned chunk_size = anv_state_pool_get_bucket_size(b);
1112 int32_t chunk_offset = state->offset;
1113
1114 /* First lets update the state we got to its new size. offset and map
1115 * remain the same.
1116 */
1117 state->alloc_size = alloc_size;
1118
1119 /* Now return the unused part of the chunk back to the pool as free
1120 * blocks
1121 *
1122 * There are a couple of options as to what we do with it:
1123 *
1124 * 1) We could fully split the chunk into state.alloc_size sized
1125 * pieces. However, this would mean that allocating a 16B
1126 * state could potentially split a 2MB chunk into 512K smaller
1127 * chunks. This would lead to unnecessary fragmentation.
1128 *
1129 * 2) The classic "buddy allocator" method would have us split the
1130 * chunk in half and return one half. Then we would split the
1131 * remaining half in half and return one half, and repeat as
1132 * needed until we get down to the size we want. However, if
1133 * you are allocating a bunch of the same size state (which is
1134 * the common case), this means that every other allocation has
1135 * to go up a level and every fourth goes up two levels, etc.
1136 * This is not nearly as efficient as it could be if we did a
1137 * little more work up-front.
1138 *
1139 * 3) Split the difference between (1) and (2) by doing a
1140 * two-level split. If it's bigger than some fixed block_size,
1141 * we split it into block_size sized chunks and return all but
1142 * one of them. Then we split what remains into
1143 * state.alloc_size sized chunks and return them.
1144 *
1145 * We choose something close to option (3), which is implemented with
1146 * anv_state_pool_return_chunk(). That is done by returning the
1147 * remaining of the chunk, with alloc_size as a hint of the size that
1148 * we want the smaller chunk split into.
1149 */
1150 anv_state_pool_return_chunk(pool, chunk_offset + alloc_size,
1151 chunk_size - alloc_size, alloc_size);
1152 goto done;
1153 }
1154 }
1155
1156 uint32_t padding;
1157 offset = anv_fixed_size_state_pool_alloc_new(&pool->buckets[bucket],
1158 &pool->block_pool,
1159 alloc_size,
1160 pool->block_size,
1161 &padding);
1162 /* Everytime we allocate a new state, add it to the state pool */
1163 uint32_t idx;
1164 UNUSED VkResult result = anv_state_table_add(&pool->table, &idx, 1);
1165 assert(result == VK_SUCCESS);
1166
1167 state = anv_state_table_get(&pool->table, idx);
1168 state->offset = offset;
1169 state->alloc_size = alloc_size;
1170 state->map = anv_block_pool_map(&pool->block_pool, offset);
1171
1172 if (padding > 0) {
1173 uint32_t return_offset = offset - padding;
1174 anv_state_pool_return_chunk(pool, return_offset, padding, 0);
1175 }
1176
1177 done:
1178 return *state;
1179 }
1180
1181 struct anv_state
1182 anv_state_pool_alloc(struct anv_state_pool *pool, uint32_t size, uint32_t align)
1183 {
1184 if (size == 0)
1185 return ANV_STATE_NULL;
1186
1187 struct anv_state state = anv_state_pool_alloc_no_vg(pool, size, align);
1188 VG(VALGRIND_MEMPOOL_ALLOC(pool, state.map, size));
1189 return state;
1190 }
1191
1192 struct anv_state
1193 anv_state_pool_alloc_back(struct anv_state_pool *pool)
1194 {
1195 struct anv_state *state;
1196 uint32_t alloc_size = pool->block_size;
1197
1198 state = anv_free_list_pop(&pool->back_alloc_free_list, &pool->table);
1199 if (state) {
1200 assert(state->offset < 0);
1201 goto done;
1202 }
1203
1204 int32_t offset;
1205 offset = anv_block_pool_alloc_back(&pool->block_pool,
1206 pool->block_size);
1207 uint32_t idx;
1208 UNUSED VkResult result = anv_state_table_add(&pool->table, &idx, 1);
1209 assert(result == VK_SUCCESS);
1210
1211 state = anv_state_table_get(&pool->table, idx);
1212 state->offset = offset;
1213 state->alloc_size = alloc_size;
1214 state->map = anv_block_pool_map(&pool->block_pool, state->offset);
1215
1216 done:
1217 VG(VALGRIND_MEMPOOL_ALLOC(pool, state->map, state->alloc_size));
1218 return *state;
1219 }
1220
1221 static void
1222 anv_state_pool_free_no_vg(struct anv_state_pool *pool, struct anv_state state)
1223 {
1224 assert(util_is_power_of_two_or_zero(state.alloc_size));
1225 unsigned bucket = anv_state_pool_get_bucket(state.alloc_size);
1226
1227 if (state.offset < 0) {
1228 assert(state.alloc_size == pool->block_size);
1229 anv_free_list_push(&pool->back_alloc_free_list,
1230 &pool->table, state.idx, 1);
1231 } else {
1232 anv_free_list_push(&pool->buckets[bucket].free_list,
1233 &pool->table, state.idx, 1);
1234 }
1235 }
1236
1237 void
1238 anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state)
1239 {
1240 if (state.alloc_size == 0)
1241 return;
1242
1243 VG(VALGRIND_MEMPOOL_FREE(pool, state.map));
1244 anv_state_pool_free_no_vg(pool, state);
1245 }
1246
1247 struct anv_state_stream_block {
1248 struct anv_state block;
1249
1250 /* The next block */
1251 struct anv_state_stream_block *next;
1252
1253 #ifdef HAVE_VALGRIND
1254 /* A pointer to the first user-allocated thing in this block. This is
1255 * what valgrind sees as the start of the block.
1256 */
1257 void *_vg_ptr;
1258 #endif
1259 };
1260
1261 /* The state stream allocator is a one-shot, single threaded allocator for
1262 * variable sized blocks. We use it for allocating dynamic state.
1263 */
1264 void
1265 anv_state_stream_init(struct anv_state_stream *stream,
1266 struct anv_state_pool *state_pool,
1267 uint32_t block_size)
1268 {
1269 stream->state_pool = state_pool;
1270 stream->block_size = block_size;
1271
1272 stream->block = ANV_STATE_NULL;
1273
1274 stream->block_list = NULL;
1275
1276 /* Ensure that next + whatever > block_size. This way the first call to
1277 * state_stream_alloc fetches a new block.
1278 */
1279 stream->next = block_size;
1280
1281 VG(VALGRIND_CREATE_MEMPOOL(stream, 0, false));
1282 }
1283
1284 void
1285 anv_state_stream_finish(struct anv_state_stream *stream)
1286 {
1287 struct anv_state_stream_block *next = stream->block_list;
1288 while (next != NULL) {
1289 struct anv_state_stream_block sb = VG_NOACCESS_READ(next);
1290 VG(VALGRIND_MEMPOOL_FREE(stream, sb._vg_ptr));
1291 VG(VALGRIND_MAKE_MEM_UNDEFINED(next, stream->block_size));
1292 anv_state_pool_free_no_vg(stream->state_pool, sb.block);
1293 next = sb.next;
1294 }
1295
1296 VG(VALGRIND_DESTROY_MEMPOOL(stream));
1297 }
1298
1299 struct anv_state
1300 anv_state_stream_alloc(struct anv_state_stream *stream,
1301 uint32_t size, uint32_t alignment)
1302 {
1303 if (size == 0)
1304 return ANV_STATE_NULL;
1305
1306 assert(alignment <= PAGE_SIZE);
1307
1308 uint32_t offset = align_u32(stream->next, alignment);
1309 if (offset + size > stream->block.alloc_size) {
1310 uint32_t block_size = stream->block_size;
1311 if (block_size < size)
1312 block_size = round_to_power_of_two(size);
1313
1314 stream->block = anv_state_pool_alloc_no_vg(stream->state_pool,
1315 block_size, PAGE_SIZE);
1316
1317 struct anv_state_stream_block *sb = stream->block.map;
1318 VG_NOACCESS_WRITE(&sb->block, stream->block);
1319 VG_NOACCESS_WRITE(&sb->next, stream->block_list);
1320 stream->block_list = sb;
1321 VG(VG_NOACCESS_WRITE(&sb->_vg_ptr, NULL));
1322
1323 VG(VALGRIND_MAKE_MEM_NOACCESS(stream->block.map, stream->block_size));
1324
1325 /* Reset back to the start plus space for the header */
1326 stream->next = sizeof(*sb);
1327
1328 offset = align_u32(stream->next, alignment);
1329 assert(offset + size <= stream->block.alloc_size);
1330 }
1331
1332 struct anv_state state = stream->block;
1333 state.offset += offset;
1334 state.alloc_size = size;
1335 state.map += offset;
1336
1337 stream->next = offset + size;
1338
1339 #ifdef HAVE_VALGRIND
1340 struct anv_state_stream_block *sb = stream->block_list;
1341 void *vg_ptr = VG_NOACCESS_READ(&sb->_vg_ptr);
1342 if (vg_ptr == NULL) {
1343 vg_ptr = state.map;
1344 VG_NOACCESS_WRITE(&sb->_vg_ptr, vg_ptr);
1345 VALGRIND_MEMPOOL_ALLOC(stream, vg_ptr, size);
1346 } else {
1347 void *state_end = state.map + state.alloc_size;
1348 /* This only updates the mempool. The newly allocated chunk is still
1349 * marked as NOACCESS. */
1350 VALGRIND_MEMPOOL_CHANGE(stream, vg_ptr, vg_ptr, state_end - vg_ptr);
1351 /* Mark the newly allocated chunk as undefined */
1352 VALGRIND_MAKE_MEM_UNDEFINED(state.map, state.alloc_size);
1353 }
1354 #endif
1355
1356 return state;
1357 }
1358
1359 struct bo_pool_bo_link {
1360 struct bo_pool_bo_link *next;
1361 struct anv_bo bo;
1362 };
1363
1364 void
1365 anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device,
1366 uint64_t bo_flags)
1367 {
1368 pool->device = device;
1369 pool->bo_flags = bo_flags;
1370 memset(pool->free_list, 0, sizeof(pool->free_list));
1371
1372 VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
1373 }
1374
1375 void
1376 anv_bo_pool_finish(struct anv_bo_pool *pool)
1377 {
1378 for (unsigned i = 0; i < ARRAY_SIZE(pool->free_list); i++) {
1379 struct bo_pool_bo_link *link = PFL_PTR(pool->free_list[i]);
1380 while (link != NULL) {
1381 struct bo_pool_bo_link link_copy = VG_NOACCESS_READ(link);
1382
1383 anv_gem_munmap(link_copy.bo.map, link_copy.bo.size);
1384 anv_vma_free(pool->device, &link_copy.bo);
1385 anv_gem_close(pool->device, link_copy.bo.gem_handle);
1386 link = link_copy.next;
1387 }
1388 }
1389
1390 VG(VALGRIND_DESTROY_MEMPOOL(pool));
1391 }
1392
1393 VkResult
1394 anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo, uint32_t size)
1395 {
1396 VkResult result;
1397
1398 const unsigned size_log2 = size < 4096 ? 12 : ilog2_round_up(size);
1399 const unsigned pow2_size = 1 << size_log2;
1400 const unsigned bucket = size_log2 - 12;
1401 assert(bucket < ARRAY_SIZE(pool->free_list));
1402
1403 void *next_free_void;
1404 if (anv_ptr_free_list_pop(&pool->free_list[bucket], &next_free_void)) {
1405 struct bo_pool_bo_link *next_free = next_free_void;
1406 *bo = VG_NOACCESS_READ(&next_free->bo);
1407 assert(bo->gem_handle);
1408 assert(bo->map == next_free);
1409 assert(size <= bo->size);
1410
1411 VG(VALGRIND_MEMPOOL_ALLOC(pool, bo->map, size));
1412
1413 return VK_SUCCESS;
1414 }
1415
1416 struct anv_bo new_bo;
1417
1418 result = anv_bo_init_new(&new_bo, pool->device, pow2_size);
1419 if (result != VK_SUCCESS)
1420 return result;
1421
1422 new_bo.flags = pool->bo_flags;
1423
1424 if (!anv_vma_alloc(pool->device, &new_bo))
1425 return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
1426
1427 assert(new_bo.size == pow2_size);
1428
1429 new_bo.map = anv_gem_mmap(pool->device, new_bo.gem_handle, 0, pow2_size, 0);
1430 if (new_bo.map == MAP_FAILED) {
1431 anv_gem_close(pool->device, new_bo.gem_handle);
1432 anv_vma_free(pool->device, &new_bo);
1433 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
1434 }
1435
1436 /* We are removing the state flushes, so lets make sure that these buffers
1437 * are cached/snooped.
1438 */
1439 if (!pool->device->info.has_llc) {
1440 anv_gem_set_caching(pool->device, new_bo.gem_handle,
1441 I915_CACHING_CACHED);
1442 }
1443
1444 *bo = new_bo;
1445
1446 VG(VALGRIND_MEMPOOL_ALLOC(pool, bo->map, size));
1447
1448 return VK_SUCCESS;
1449 }
1450
1451 void
1452 anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo_in)
1453 {
1454 /* Make a copy in case the anv_bo happens to be storred in the BO */
1455 struct anv_bo bo = *bo_in;
1456
1457 VG(VALGRIND_MEMPOOL_FREE(pool, bo.map));
1458
1459 struct bo_pool_bo_link *link = bo.map;
1460 VG_NOACCESS_WRITE(&link->bo, bo);
1461
1462 assert(util_is_power_of_two_or_zero(bo.size));
1463 const unsigned size_log2 = ilog2_round_up(bo.size);
1464 const unsigned bucket = size_log2 - 12;
1465 assert(bucket < ARRAY_SIZE(pool->free_list));
1466
1467 anv_ptr_free_list_push(&pool->free_list[bucket], link);
1468 }
1469
1470 // Scratch pool
1471
1472 void
1473 anv_scratch_pool_init(struct anv_device *device, struct anv_scratch_pool *pool)
1474 {
1475 memset(pool, 0, sizeof(*pool));
1476 }
1477
1478 void
1479 anv_scratch_pool_finish(struct anv_device *device, struct anv_scratch_pool *pool)
1480 {
1481 for (unsigned s = 0; s < MESA_SHADER_STAGES; s++) {
1482 for (unsigned i = 0; i < 16; i++) {
1483 struct anv_scratch_bo *bo = &pool->bos[i][s];
1484 if (bo->exists > 0) {
1485 anv_vma_free(device, &bo->bo);
1486 anv_gem_close(device, bo->bo.gem_handle);
1487 }
1488 }
1489 }
1490 }
1491
1492 struct anv_bo *
1493 anv_scratch_pool_alloc(struct anv_device *device, struct anv_scratch_pool *pool,
1494 gl_shader_stage stage, unsigned per_thread_scratch)
1495 {
1496 if (per_thread_scratch == 0)
1497 return NULL;
1498
1499 unsigned scratch_size_log2 = ffs(per_thread_scratch / 2048);
1500 assert(scratch_size_log2 < 16);
1501
1502 struct anv_scratch_bo *bo = &pool->bos[scratch_size_log2][stage];
1503
1504 /* We can use "exists" to shortcut and ignore the critical section */
1505 if (bo->exists)
1506 return &bo->bo;
1507
1508 pthread_mutex_lock(&device->mutex);
1509
1510 __sync_synchronize();
1511 if (bo->exists) {
1512 pthread_mutex_unlock(&device->mutex);
1513 return &bo->bo;
1514 }
1515
1516 const struct anv_physical_device *physical_device =
1517 &device->instance->physicalDevice;
1518 const struct gen_device_info *devinfo = &physical_device->info;
1519
1520 const unsigned subslices = MAX2(physical_device->subslice_total, 1);
1521
1522 unsigned scratch_ids_per_subslice;
1523 if (devinfo->gen >= 11) {
1524 /* The MEDIA_VFE_STATE docs say:
1525 *
1526 * "Starting with this configuration, the Maximum Number of
1527 * Threads must be set to (#EU * 8) for GPGPU dispatches.
1528 *
1529 * Although there are only 7 threads per EU in the configuration,
1530 * the FFTID is calculated as if there are 8 threads per EU,
1531 * which in turn requires a larger amount of Scratch Space to be
1532 * allocated by the driver."
1533 */
1534 scratch_ids_per_subslice = 8 * 8;
1535 } else if (devinfo->is_haswell) {
1536 /* WaCSScratchSize:hsw
1537 *
1538 * Haswell's scratch space address calculation appears to be sparse
1539 * rather than tightly packed. The Thread ID has bits indicating
1540 * which subslice, EU within a subslice, and thread within an EU it
1541 * is. There's a maximum of two slices and two subslices, so these
1542 * can be stored with a single bit. Even though there are only 10 EUs
1543 * per subslice, this is stored in 4 bits, so there's an effective
1544 * maximum value of 16 EUs. Similarly, although there are only 7
1545 * threads per EU, this is stored in a 3 bit number, giving an
1546 * effective maximum value of 8 threads per EU.
1547 *
1548 * This means that we need to use 16 * 8 instead of 10 * 7 for the
1549 * number of threads per subslice.
1550 */
1551 scratch_ids_per_subslice = 16 * 8;
1552 } else if (devinfo->is_cherryview) {
1553 /* Cherryview devices have either 6 or 8 EUs per subslice, and each EU
1554 * has 7 threads. The 6 EU devices appear to calculate thread IDs as if
1555 * it had 8 EUs.
1556 */
1557 scratch_ids_per_subslice = 8 * 7;
1558 } else {
1559 scratch_ids_per_subslice = devinfo->max_cs_threads;
1560 }
1561
1562 uint32_t max_threads[] = {
1563 [MESA_SHADER_VERTEX] = devinfo->max_vs_threads,
1564 [MESA_SHADER_TESS_CTRL] = devinfo->max_tcs_threads,
1565 [MESA_SHADER_TESS_EVAL] = devinfo->max_tes_threads,
1566 [MESA_SHADER_GEOMETRY] = devinfo->max_gs_threads,
1567 [MESA_SHADER_FRAGMENT] = devinfo->max_wm_threads,
1568 [MESA_SHADER_COMPUTE] = scratch_ids_per_subslice * subslices,
1569 };
1570
1571 uint32_t size = per_thread_scratch * max_threads[stage];
1572
1573 anv_bo_init_new(&bo->bo, device, size);
1574
1575 /* Even though the Scratch base pointers in 3DSTATE_*S are 64 bits, they
1576 * are still relative to the general state base address. When we emit
1577 * STATE_BASE_ADDRESS, we set general state base address to 0 and the size
1578 * to the maximum (1 page under 4GB). This allows us to just place the
1579 * scratch buffers anywhere we wish in the bottom 32 bits of address space
1580 * and just set the scratch base pointer in 3DSTATE_*S using a relocation.
1581 * However, in order to do so, we need to ensure that the kernel does not
1582 * place the scratch BO above the 32-bit boundary.
1583 *
1584 * NOTE: Technically, it can't go "anywhere" because the top page is off
1585 * limits. However, when EXEC_OBJECT_SUPPORTS_48B_ADDRESS is set, the
1586 * kernel allocates space using
1587 *
1588 * end = min_t(u64, end, (1ULL << 32) - I915_GTT_PAGE_SIZE);
1589 *
1590 * so nothing will ever touch the top page.
1591 */
1592 assert(!(bo->bo.flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS));
1593
1594 if (device->instance->physicalDevice.has_exec_async)
1595 bo->bo.flags |= EXEC_OBJECT_ASYNC;
1596
1597 if (device->instance->physicalDevice.use_softpin)
1598 bo->bo.flags |= EXEC_OBJECT_PINNED;
1599
1600 anv_vma_alloc(device, &bo->bo);
1601
1602 /* Set the exists last because it may be read by other threads */
1603 __sync_synchronize();
1604 bo->exists = true;
1605
1606 pthread_mutex_unlock(&device->mutex);
1607
1608 return &bo->bo;
1609 }
1610
1611 struct anv_cached_bo {
1612 struct anv_bo bo;
1613
1614 uint32_t refcount;
1615 };
1616
1617 VkResult
1618 anv_bo_cache_init(struct anv_bo_cache *cache)
1619 {
1620 cache->bo_map = _mesa_pointer_hash_table_create(NULL);
1621 if (!cache->bo_map)
1622 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1623
1624 if (pthread_mutex_init(&cache->mutex, NULL)) {
1625 _mesa_hash_table_destroy(cache->bo_map, NULL);
1626 return vk_errorf(NULL, NULL, VK_ERROR_OUT_OF_HOST_MEMORY,
1627 "pthread_mutex_init failed: %m");
1628 }
1629
1630 return VK_SUCCESS;
1631 }
1632
1633 void
1634 anv_bo_cache_finish(struct anv_bo_cache *cache)
1635 {
1636 _mesa_hash_table_destroy(cache->bo_map, NULL);
1637 pthread_mutex_destroy(&cache->mutex);
1638 }
1639
1640 static struct anv_cached_bo *
1641 anv_bo_cache_lookup_locked(struct anv_bo_cache *cache, uint32_t gem_handle)
1642 {
1643 struct hash_entry *entry =
1644 _mesa_hash_table_search(cache->bo_map,
1645 (const void *)(uintptr_t)gem_handle);
1646 if (!entry)
1647 return NULL;
1648
1649 struct anv_cached_bo *bo = (struct anv_cached_bo *)entry->data;
1650 assert(bo->bo.gem_handle == gem_handle);
1651
1652 return bo;
1653 }
1654
1655 UNUSED static struct anv_bo *
1656 anv_bo_cache_lookup(struct anv_bo_cache *cache, uint32_t gem_handle)
1657 {
1658 pthread_mutex_lock(&cache->mutex);
1659
1660 struct anv_cached_bo *bo = anv_bo_cache_lookup_locked(cache, gem_handle);
1661
1662 pthread_mutex_unlock(&cache->mutex);
1663
1664 return bo ? &bo->bo : NULL;
1665 }
1666
1667 #define ANV_BO_CACHE_SUPPORTED_FLAGS \
1668 (EXEC_OBJECT_WRITE | \
1669 EXEC_OBJECT_ASYNC | \
1670 EXEC_OBJECT_SUPPORTS_48B_ADDRESS | \
1671 EXEC_OBJECT_PINNED | \
1672 ANV_BO_EXTERNAL)
1673
1674 VkResult
1675 anv_bo_cache_alloc(struct anv_device *device,
1676 struct anv_bo_cache *cache,
1677 uint64_t size, uint64_t bo_flags,
1678 struct anv_bo **bo_out)
1679 {
1680 assert(bo_flags == (bo_flags & ANV_BO_CACHE_SUPPORTED_FLAGS));
1681
1682 struct anv_cached_bo *bo =
1683 vk_alloc(&device->alloc, sizeof(struct anv_cached_bo), 8,
1684 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1685 if (!bo)
1686 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1687
1688 bo->refcount = 1;
1689
1690 /* The kernel is going to give us whole pages anyway */
1691 size = align_u64(size, 4096);
1692
1693 VkResult result = anv_bo_init_new(&bo->bo, device, size);
1694 if (result != VK_SUCCESS) {
1695 vk_free(&device->alloc, bo);
1696 return result;
1697 }
1698
1699 bo->bo.flags = bo_flags;
1700
1701 if (!anv_vma_alloc(device, &bo->bo)) {
1702 anv_gem_close(device, bo->bo.gem_handle);
1703 vk_free(&device->alloc, bo);
1704 return vk_errorf(device->instance, NULL,
1705 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1706 "failed to allocate virtual address for BO");
1707 }
1708
1709 assert(bo->bo.gem_handle);
1710
1711 pthread_mutex_lock(&cache->mutex);
1712
1713 _mesa_hash_table_insert(cache->bo_map,
1714 (void *)(uintptr_t)bo->bo.gem_handle, bo);
1715
1716 pthread_mutex_unlock(&cache->mutex);
1717
1718 *bo_out = &bo->bo;
1719
1720 return VK_SUCCESS;
1721 }
1722
1723 VkResult
1724 anv_bo_cache_import_host_ptr(struct anv_device *device,
1725 struct anv_bo_cache *cache,
1726 void *host_ptr, uint32_t size,
1727 uint64_t bo_flags, struct anv_bo **bo_out)
1728 {
1729 assert(bo_flags == (bo_flags & ANV_BO_CACHE_SUPPORTED_FLAGS));
1730 assert((bo_flags & ANV_BO_EXTERNAL) == 0);
1731
1732 uint32_t gem_handle = anv_gem_userptr(device, host_ptr, size);
1733 if (!gem_handle)
1734 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
1735
1736 pthread_mutex_lock(&cache->mutex);
1737
1738 struct anv_cached_bo *bo = anv_bo_cache_lookup_locked(cache, gem_handle);
1739 if (bo) {
1740 /* VK_EXT_external_memory_host doesn't require handling importing the
1741 * same pointer twice at the same time, but we don't get in the way. If
1742 * kernel gives us the same gem_handle, only succeed if the flags match.
1743 */
1744 if (bo_flags != bo->bo.flags) {
1745 pthread_mutex_unlock(&cache->mutex);
1746 return vk_errorf(device->instance, NULL,
1747 VK_ERROR_INVALID_EXTERNAL_HANDLE,
1748 "same host pointer imported two different ways");
1749 }
1750 __sync_fetch_and_add(&bo->refcount, 1);
1751 } else {
1752 bo = vk_alloc(&device->alloc, sizeof(struct anv_cached_bo), 8,
1753 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1754 if (!bo) {
1755 anv_gem_close(device, gem_handle);
1756 pthread_mutex_unlock(&cache->mutex);
1757 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1758 }
1759
1760 bo->refcount = 1;
1761
1762 anv_bo_init(&bo->bo, gem_handle, size);
1763 bo->bo.flags = bo_flags;
1764
1765 if (!anv_vma_alloc(device, &bo->bo)) {
1766 anv_gem_close(device, bo->bo.gem_handle);
1767 pthread_mutex_unlock(&cache->mutex);
1768 vk_free(&device->alloc, bo);
1769 return vk_errorf(device->instance, NULL,
1770 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1771 "failed to allocate virtual address for BO");
1772 }
1773
1774 _mesa_hash_table_insert(cache->bo_map, (void *)(uintptr_t)gem_handle, bo);
1775 }
1776
1777 pthread_mutex_unlock(&cache->mutex);
1778 *bo_out = &bo->bo;
1779
1780 return VK_SUCCESS;
1781 }
1782
1783 VkResult
1784 anv_bo_cache_import(struct anv_device *device,
1785 struct anv_bo_cache *cache,
1786 int fd, uint64_t bo_flags,
1787 struct anv_bo **bo_out)
1788 {
1789 assert(bo_flags == (bo_flags & ANV_BO_CACHE_SUPPORTED_FLAGS));
1790 assert(bo_flags & ANV_BO_EXTERNAL);
1791
1792 pthread_mutex_lock(&cache->mutex);
1793
1794 uint32_t gem_handle = anv_gem_fd_to_handle(device, fd);
1795 if (!gem_handle) {
1796 pthread_mutex_unlock(&cache->mutex);
1797 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
1798 }
1799
1800 struct anv_cached_bo *bo = anv_bo_cache_lookup_locked(cache, gem_handle);
1801 if (bo) {
1802 /* We have to be careful how we combine flags so that it makes sense.
1803 * Really, though, if we get to this case and it actually matters, the
1804 * client has imported a BO twice in different ways and they get what
1805 * they have coming.
1806 */
1807 uint64_t new_flags = ANV_BO_EXTERNAL;
1808 new_flags |= (bo->bo.flags | bo_flags) & EXEC_OBJECT_WRITE;
1809 new_flags |= (bo->bo.flags & bo_flags) & EXEC_OBJECT_ASYNC;
1810 new_flags |= (bo->bo.flags & bo_flags) & EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
1811 new_flags |= (bo->bo.flags | bo_flags) & EXEC_OBJECT_PINNED;
1812
1813 /* It's theoretically possible for a BO to get imported such that it's
1814 * both pinned and not pinned. The only way this can happen is if it
1815 * gets imported as both a semaphore and a memory object and that would
1816 * be an application error. Just fail out in that case.
1817 */
1818 if ((bo->bo.flags & EXEC_OBJECT_PINNED) !=
1819 (bo_flags & EXEC_OBJECT_PINNED)) {
1820 pthread_mutex_unlock(&cache->mutex);
1821 return vk_errorf(device->instance, NULL,
1822 VK_ERROR_INVALID_EXTERNAL_HANDLE,
1823 "The same BO was imported two different ways");
1824 }
1825
1826 /* It's also theoretically possible that someone could export a BO from
1827 * one heap and import it into another or to import the same BO into two
1828 * different heaps. If this happens, we could potentially end up both
1829 * allowing and disallowing 48-bit addresses. There's not much we can
1830 * do about it if we're pinning so we just throw an error and hope no
1831 * app is actually that stupid.
1832 */
1833 if ((new_flags & EXEC_OBJECT_PINNED) &&
1834 (bo->bo.flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) !=
1835 (bo_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS)) {
1836 pthread_mutex_unlock(&cache->mutex);
1837 return vk_errorf(device->instance, NULL,
1838 VK_ERROR_INVALID_EXTERNAL_HANDLE,
1839 "The same BO was imported on two different heaps");
1840 }
1841
1842 bo->bo.flags = new_flags;
1843
1844 __sync_fetch_and_add(&bo->refcount, 1);
1845 } else {
1846 off_t size = lseek(fd, 0, SEEK_END);
1847 if (size == (off_t)-1) {
1848 anv_gem_close(device, gem_handle);
1849 pthread_mutex_unlock(&cache->mutex);
1850 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
1851 }
1852
1853 bo = vk_alloc(&device->alloc, sizeof(struct anv_cached_bo), 8,
1854 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
1855 if (!bo) {
1856 anv_gem_close(device, gem_handle);
1857 pthread_mutex_unlock(&cache->mutex);
1858 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1859 }
1860
1861 bo->refcount = 1;
1862
1863 anv_bo_init(&bo->bo, gem_handle, size);
1864 bo->bo.flags = bo_flags;
1865
1866 if (!anv_vma_alloc(device, &bo->bo)) {
1867 anv_gem_close(device, bo->bo.gem_handle);
1868 pthread_mutex_unlock(&cache->mutex);
1869 vk_free(&device->alloc, bo);
1870 return vk_errorf(device->instance, NULL,
1871 VK_ERROR_OUT_OF_DEVICE_MEMORY,
1872 "failed to allocate virtual address for BO");
1873 }
1874
1875 _mesa_hash_table_insert(cache->bo_map, (void *)(uintptr_t)gem_handle, bo);
1876 }
1877
1878 pthread_mutex_unlock(&cache->mutex);
1879 *bo_out = &bo->bo;
1880
1881 return VK_SUCCESS;
1882 }
1883
1884 VkResult
1885 anv_bo_cache_export(struct anv_device *device,
1886 struct anv_bo_cache *cache,
1887 struct anv_bo *bo_in, int *fd_out)
1888 {
1889 assert(anv_bo_cache_lookup(cache, bo_in->gem_handle) == bo_in);
1890 struct anv_cached_bo *bo = (struct anv_cached_bo *)bo_in;
1891
1892 /* This BO must have been flagged external in order for us to be able
1893 * to export it. This is done based on external options passed into
1894 * anv_AllocateMemory.
1895 */
1896 assert(bo->bo.flags & ANV_BO_EXTERNAL);
1897
1898 int fd = anv_gem_handle_to_fd(device, bo->bo.gem_handle);
1899 if (fd < 0)
1900 return vk_error(VK_ERROR_TOO_MANY_OBJECTS);
1901
1902 *fd_out = fd;
1903
1904 return VK_SUCCESS;
1905 }
1906
1907 static bool
1908 atomic_dec_not_one(uint32_t *counter)
1909 {
1910 uint32_t old, val;
1911
1912 val = *counter;
1913 while (1) {
1914 if (val == 1)
1915 return false;
1916
1917 old = __sync_val_compare_and_swap(counter, val, val - 1);
1918 if (old == val)
1919 return true;
1920
1921 val = old;
1922 }
1923 }
1924
1925 void
1926 anv_bo_cache_release(struct anv_device *device,
1927 struct anv_bo_cache *cache,
1928 struct anv_bo *bo_in)
1929 {
1930 assert(anv_bo_cache_lookup(cache, bo_in->gem_handle) == bo_in);
1931 struct anv_cached_bo *bo = (struct anv_cached_bo *)bo_in;
1932
1933 /* Try to decrement the counter but don't go below one. If this succeeds
1934 * then the refcount has been decremented and we are not the last
1935 * reference.
1936 */
1937 if (atomic_dec_not_one(&bo->refcount))
1938 return;
1939
1940 pthread_mutex_lock(&cache->mutex);
1941
1942 /* We are probably the last reference since our attempt to decrement above
1943 * failed. However, we can't actually know until we are inside the mutex.
1944 * Otherwise, someone could import the BO between the decrement and our
1945 * taking the mutex.
1946 */
1947 if (unlikely(__sync_sub_and_fetch(&bo->refcount, 1) > 0)) {
1948 /* Turns out we're not the last reference. Unlock and bail. */
1949 pthread_mutex_unlock(&cache->mutex);
1950 return;
1951 }
1952
1953 struct hash_entry *entry =
1954 _mesa_hash_table_search(cache->bo_map,
1955 (const void *)(uintptr_t)bo->bo.gem_handle);
1956 assert(entry);
1957 _mesa_hash_table_remove(cache->bo_map, entry);
1958
1959 if (bo->bo.map)
1960 anv_gem_munmap(bo->bo.map, bo->bo.size);
1961
1962 anv_vma_free(device, &bo->bo);
1963
1964 anv_gem_close(device, bo->bo.gem_handle);
1965
1966 /* Don't unlock until we've actually closed the BO. The whole point of
1967 * the BO cache is to ensure that we correctly handle races with creating
1968 * and releasing GEM handles and we don't want to let someone import the BO
1969 * again between mutex unlock and closing the GEM handle.
1970 */
1971 pthread_mutex_unlock(&cache->mutex);
1972
1973 vk_free(&device->alloc, bo);
1974 }