Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / 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 #define _DEFAULT_SOURCE
25
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <values.h>
30 #include <assert.h>
31 #include <linux/futex.h>
32 #include <linux/memfd.h>
33 #include <sys/time.h>
34 #include <sys/mman.h>
35 #include <sys/syscall.h>
36
37 #include "anv_private.h"
38
39 #ifdef HAVE_VALGRIND
40 #define VG_NOACCESS_READ(__ptr) ({ \
41 VALGRIND_MAKE_MEM_DEFINED((__ptr), sizeof(*(__ptr))); \
42 __typeof(*(__ptr)) __val = *(__ptr); \
43 VALGRIND_MAKE_MEM_NOACCESS((__ptr), sizeof(*(__ptr)));\
44 __val; \
45 })
46 #define VG_NOACCESS_WRITE(__ptr, __val) ({ \
47 VALGRIND_MAKE_MEM_UNDEFINED((__ptr), sizeof(*(__ptr))); \
48 *(__ptr) = (__val); \
49 VALGRIND_MAKE_MEM_NOACCESS((__ptr), sizeof(*(__ptr))); \
50 })
51 #else
52 #define VG_NOACCESS_READ(__ptr) (*(__ptr))
53 #define VG_NOACCESS_WRITE(__ptr, __val) (*(__ptr) = (__val))
54 #endif
55
56 /* Design goals:
57 *
58 * - Lock free (except when resizing underlying bos)
59 *
60 * - Constant time allocation with typically only one atomic
61 *
62 * - Multiple allocation sizes without fragmentation
63 *
64 * - Can grow while keeping addresses and offset of contents stable
65 *
66 * - All allocations within one bo so we can point one of the
67 * STATE_BASE_ADDRESS pointers at it.
68 *
69 * The overall design is a two-level allocator: top level is a fixed size, big
70 * block (8k) allocator, which operates out of a bo. Allocation is done by
71 * either pulling a block from the free list or growing the used range of the
72 * bo. Growing the range may run out of space in the bo which we then need to
73 * grow. Growing the bo is tricky in a multi-threaded, lockless environment:
74 * we need to keep all pointers and contents in the old map valid. GEM bos in
75 * general can't grow, but we use a trick: we create a memfd and use ftruncate
76 * to grow it as necessary. We mmap the new size and then create a gem bo for
77 * it using the new gem userptr ioctl. Without heavy-handed locking around
78 * our allocation fast-path, there isn't really a way to munmap the old mmap,
79 * so we just keep it around until garbage collection time. While the block
80 * allocator is lockless for normal operations, we block other threads trying
81 * to allocate while we're growing the map. It sholdn't happen often, and
82 * growing is fast anyway.
83 *
84 * At the next level we can use various sub-allocators. The state pool is a
85 * pool of smaller, fixed size objects, which operates much like the block
86 * pool. It uses a free list for freeing objects, but when it runs out of
87 * space it just allocates a new block from the block pool. This allocator is
88 * intended for longer lived state objects such as SURFACE_STATE and most
89 * other persistent state objects in the API. We may need to track more info
90 * with these object and a pointer back to the CPU object (eg VkImage). In
91 * those cases we just allocate a slightly bigger object and put the extra
92 * state after the GPU state object.
93 *
94 * The state stream allocator works similar to how the i965 DRI driver streams
95 * all its state. Even with Vulkan, we need to emit transient state (whether
96 * surface state base or dynamic state base), and for that we can just get a
97 * block and fill it up. These cases are local to a command buffer and the
98 * sub-allocator need not be thread safe. The streaming allocator gets a new
99 * block when it runs out of space and chains them together so they can be
100 * easily freed.
101 */
102
103 /* Allocations are always at least 64 byte aligned, so 1 is an invalid value.
104 * We use it to indicate the free list is empty. */
105 #define EMPTY 1
106
107 struct anv_mmap_cleanup {
108 void *map;
109 size_t size;
110 uint32_t gem_handle;
111 };
112
113 #define ANV_MMAP_CLEANUP_INIT ((struct anv_mmap_cleanup){0})
114
115 static inline long
116 sys_futex(void *addr1, int op, int val1,
117 struct timespec *timeout, void *addr2, int val3)
118 {
119 return syscall(SYS_futex, addr1, op, val1, timeout, addr2, val3);
120 }
121
122 static inline int
123 futex_wake(uint32_t *addr, int count)
124 {
125 return sys_futex(addr, FUTEX_WAKE, count, NULL, NULL, 0);
126 }
127
128 static inline int
129 futex_wait(uint32_t *addr, int32_t value)
130 {
131 return sys_futex(addr, FUTEX_WAIT, value, NULL, NULL, 0);
132 }
133
134 static inline int
135 memfd_create(const char *name, unsigned int flags)
136 {
137 return syscall(SYS_memfd_create, name, flags);
138 }
139
140 static inline uint32_t
141 ilog2_round_up(uint32_t value)
142 {
143 assert(value != 0);
144 return 32 - __builtin_clz(value - 1);
145 }
146
147 static inline uint32_t
148 round_to_power_of_two(uint32_t value)
149 {
150 return 1 << ilog2_round_up(value);
151 }
152
153 static bool
154 anv_free_list_pop(union anv_free_list *list, void **map, int32_t *offset)
155 {
156 union anv_free_list current, new, old;
157
158 current.u64 = list->u64;
159 while (current.offset != EMPTY) {
160 /* We have to add a memory barrier here so that the list head (and
161 * offset) gets read before we read the map pointer. This way we
162 * know that the map pointer is valid for the given offset at the
163 * point where we read it.
164 */
165 __sync_synchronize();
166
167 int32_t *next_ptr = *map + current.offset;
168 new.offset = VG_NOACCESS_READ(next_ptr);
169 new.count = current.count + 1;
170 old.u64 = __sync_val_compare_and_swap(&list->u64, current.u64, new.u64);
171 if (old.u64 == current.u64) {
172 *offset = current.offset;
173 return true;
174 }
175 current = old;
176 }
177
178 return false;
179 }
180
181 static void
182 anv_free_list_push(union anv_free_list *list, void *map, int32_t offset)
183 {
184 union anv_free_list current, old, new;
185 int32_t *next_ptr = map + offset;
186
187 old = *list;
188 do {
189 current = old;
190 VG_NOACCESS_WRITE(next_ptr, current.offset);
191 new.offset = offset;
192 new.count = current.count + 1;
193 old.u64 = __sync_val_compare_and_swap(&list->u64, current.u64, new.u64);
194 } while (old.u64 != current.u64);
195 }
196
197 /* All pointers in the ptr_free_list are assumed to be page-aligned. This
198 * means that the bottom 12 bits should all be zero.
199 */
200 #define PFL_COUNT(x) ((uintptr_t)(x) & 0xfff)
201 #define PFL_PTR(x) ((void *)((uintptr_t)(x) & ~0xfff))
202 #define PFL_PACK(ptr, count) ({ \
203 assert(((uintptr_t)(ptr) & 0xfff) == 0); \
204 (void *)((uintptr_t)(ptr) | (uintptr_t)((count) & 0xfff)); \
205 })
206
207 static bool
208 anv_ptr_free_list_pop(void **list, void **elem)
209 {
210 void *current = *list;
211 while (PFL_PTR(current) != NULL) {
212 void **next_ptr = PFL_PTR(current);
213 void *new_ptr = VG_NOACCESS_READ(next_ptr);
214 unsigned new_count = PFL_COUNT(current) + 1;
215 void *new = PFL_PACK(new_ptr, new_count);
216 void *old = __sync_val_compare_and_swap(list, current, new);
217 if (old == current) {
218 *elem = PFL_PTR(current);
219 return true;
220 }
221 current = old;
222 }
223
224 return false;
225 }
226
227 static void
228 anv_ptr_free_list_push(void **list, void *elem)
229 {
230 void *old, *current;
231 void **next_ptr = elem;
232
233 old = *list;
234 do {
235 current = old;
236 VG_NOACCESS_WRITE(next_ptr, PFL_PTR(current));
237 unsigned new_count = PFL_COUNT(current) + 1;
238 void *new = PFL_PACK(elem, new_count);
239 old = __sync_val_compare_and_swap(list, current, new);
240 } while (old != current);
241 }
242
243 static uint32_t
244 anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state);
245
246 void
247 anv_block_pool_init(struct anv_block_pool *pool,
248 struct anv_device *device, uint32_t block_size)
249 {
250 assert(util_is_power_of_two(block_size));
251
252 pool->device = device;
253 pool->bo.gem_handle = 0;
254 pool->bo.offset = 0;
255 pool->bo.size = 0;
256 pool->block_size = block_size;
257 pool->free_list = ANV_FREE_LIST_EMPTY;
258 pool->back_free_list = ANV_FREE_LIST_EMPTY;
259
260 pool->fd = memfd_create("block pool", MFD_CLOEXEC);
261 if (pool->fd == -1)
262 return;
263
264 /* Just make it 2GB up-front. The Linux kernel won't actually back it
265 * with pages until we either map and fault on one of them or we use
266 * userptr and send a chunk of it off to the GPU.
267 */
268 if (ftruncate(pool->fd, BLOCK_POOL_MEMFD_SIZE) == -1)
269 return;
270
271 anv_vector_init(&pool->mmap_cleanups,
272 round_to_power_of_two(sizeof(struct anv_mmap_cleanup)), 128);
273
274 pool->state.next = 0;
275 pool->state.end = 0;
276 pool->back_state.next = 0;
277 pool->back_state.end = 0;
278
279 /* Immediately grow the pool so we'll have a backing bo. */
280 pool->state.end = anv_block_pool_grow(pool, &pool->state);
281 }
282
283 void
284 anv_block_pool_finish(struct anv_block_pool *pool)
285 {
286 struct anv_mmap_cleanup *cleanup;
287
288 anv_vector_foreach(cleanup, &pool->mmap_cleanups) {
289 if (cleanup->map)
290 munmap(cleanup->map, cleanup->size);
291 if (cleanup->gem_handle)
292 anv_gem_close(pool->device, cleanup->gem_handle);
293 }
294
295 anv_vector_finish(&pool->mmap_cleanups);
296
297 close(pool->fd);
298 }
299
300 #define PAGE_SIZE 4096
301
302 /** Grows and re-centers the block pool.
303 *
304 * We grow the block pool in one or both directions in such a way that the
305 * following conditions are met:
306 *
307 * 1) The size of the entire pool is always a power of two.
308 *
309 * 2) The pool only grows on both ends. Neither end can get
310 * shortened.
311 *
312 * 3) At the end of the allocation, we have about twice as much space
313 * allocated for each end as we have used. This way the pool doesn't
314 * grow too far in one direction or the other.
315 *
316 * 4) If the _alloc_back() has never been called, then the back portion of
317 * the pool retains a size of zero. (This makes it easier for users of
318 * the block pool that only want a one-sided pool.)
319 *
320 * 5) We have enough space allocated for at least one more block in
321 * whichever side `state` points to.
322 *
323 * 6) The center of the pool is always aligned to both the block_size of
324 * the pool and a 4K CPU page.
325 */
326 static uint32_t
327 anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state)
328 {
329 size_t size;
330 void *map;
331 int gem_handle;
332 struct anv_mmap_cleanup *cleanup;
333
334 pthread_mutex_lock(&pool->device->mutex);
335
336 assert(state == &pool->state || state == &pool->back_state);
337
338 /* Gather a little usage information on the pool. Since we may have
339 * threadsd waiting in queue to get some storage while we resize, it's
340 * actually possible that total_used will be larger than old_size. In
341 * particular, block_pool_alloc() increments state->next prior to
342 * calling block_pool_grow, so this ensures that we get enough space for
343 * which ever side tries to grow the pool.
344 *
345 * We align to a page size because it makes it easier to do our
346 * calculations later in such a way that we state page-aigned.
347 */
348 uint32_t back_used = align_u32(pool->back_state.next, PAGE_SIZE);
349 uint32_t front_used = align_u32(pool->state.next, PAGE_SIZE);
350 uint32_t total_used = front_used + back_used;
351
352 assert(state == &pool->state || back_used > 0);
353
354 size_t old_size = pool->bo.size;
355
356 if (old_size != 0 &&
357 back_used * 2 <= pool->center_bo_offset &&
358 front_used * 2 <= (old_size - pool->center_bo_offset)) {
359 /* If we're in this case then this isn't the firsta allocation and we
360 * already have enough space on both sides to hold double what we
361 * have allocated. There's nothing for us to do.
362 */
363 goto done;
364 }
365
366 if (old_size == 0) {
367 /* This is the first allocation */
368 size = MAX2(32 * pool->block_size, PAGE_SIZE);
369 } else {
370 size = old_size * 2;
371 }
372
373 /* We can't have a block pool bigger than 1GB because we use signed
374 * 32-bit offsets in the free list and we don't want overflow. We
375 * should never need a block pool bigger than 1GB anyway.
376 */
377 assert(size <= (1u << 31));
378
379 /* We compute a new center_bo_offset such that, when we double the size
380 * of the pool, we maintain the ratio of how much is used by each side.
381 * This way things should remain more-or-less balanced.
382 */
383 uint32_t center_bo_offset;
384 if (back_used == 0) {
385 /* If we're in this case then we have never called alloc_back(). In
386 * this case, we want keep the offset at 0 to make things as simple
387 * as possible for users that don't care about back allocations.
388 */
389 center_bo_offset = 0;
390 } else {
391 /* Try to "center" the allocation based on how much is currently in
392 * use on each side of the center line.
393 */
394 center_bo_offset = ((uint64_t)size * back_used) / total_used;
395
396 /* Align down to a multiple of both the block size and page size */
397 uint32_t granularity = MAX2(pool->block_size, PAGE_SIZE);
398 assert(util_is_power_of_two(granularity));
399 center_bo_offset &= ~(granularity - 1);
400
401 assert(center_bo_offset >= back_used);
402
403 /* Make sure we don't shrink the back end of the pool */
404 if (center_bo_offset < pool->back_state.end)
405 center_bo_offset = pool->back_state.end;
406
407 /* Make sure that we don't shrink the front end of the pool */
408 if (size - center_bo_offset < pool->state.end)
409 center_bo_offset = size - pool->state.end;
410 }
411
412 assert(center_bo_offset % pool->block_size == 0);
413 assert(center_bo_offset % PAGE_SIZE == 0);
414
415 /* Assert that we only ever grow the pool */
416 assert(center_bo_offset >= pool->back_state.end);
417 assert(size - center_bo_offset >= pool->state.end);
418
419 cleanup = anv_vector_add(&pool->mmap_cleanups);
420 if (!cleanup)
421 goto fail;
422 *cleanup = ANV_MMAP_CLEANUP_INIT;
423
424 /* Just leak the old map until we destroy the pool. We can't munmap it
425 * without races or imposing locking on the block allocate fast path. On
426 * the whole the leaked maps adds up to less than the size of the
427 * current map. MAP_POPULATE seems like the right thing to do, but we
428 * should try to get some numbers.
429 */
430 map = mmap(NULL, size, PROT_READ | PROT_WRITE,
431 MAP_SHARED | MAP_POPULATE, pool->fd,
432 BLOCK_POOL_MEMFD_CENTER - center_bo_offset);
433 cleanup->map = map;
434 cleanup->size = size;
435
436 if (map == MAP_FAILED)
437 goto fail;
438
439 gem_handle = anv_gem_userptr(pool->device, map, size);
440 if (gem_handle == 0)
441 goto fail;
442 cleanup->gem_handle = gem_handle;
443
444 /* Now that we successfull allocated everything, we can write the new
445 * values back into pool. */
446 pool->map = map + center_bo_offset;
447 pool->center_bo_offset = center_bo_offset;
448 pool->bo.gem_handle = gem_handle;
449 pool->bo.size = size;
450 pool->bo.map = map;
451 pool->bo.index = 0;
452
453 done:
454 pthread_mutex_unlock(&pool->device->mutex);
455
456 /* Return the appropreate new size. This function never actually
457 * updates state->next. Instead, we let the caller do that because it
458 * needs to do so in order to maintain its concurrency model.
459 */
460 if (state == &pool->state) {
461 return pool->bo.size - pool->center_bo_offset;
462 } else {
463 assert(pool->center_bo_offset > 0);
464 return pool->center_bo_offset;
465 }
466
467 fail:
468 pthread_mutex_unlock(&pool->device->mutex);
469
470 return 0;
471 }
472
473 static uint32_t
474 anv_block_pool_alloc_new(struct anv_block_pool *pool,
475 struct anv_block_state *pool_state)
476 {
477 struct anv_block_state state, old, new;
478
479 while (1) {
480 state.u64 = __sync_fetch_and_add(&pool_state->u64, pool->block_size);
481 if (state.next < state.end) {
482 assert(pool->map);
483 return state.next;
484 } else if (state.next == state.end) {
485 /* We allocated the first block outside the pool, we have to grow it.
486 * pool_state->next acts a mutex: threads who try to allocate now will
487 * get block indexes above the current limit and hit futex_wait
488 * below. */
489 new.next = state.next + pool->block_size;
490 new.end = anv_block_pool_grow(pool, pool_state);
491 assert(new.end >= new.next && new.end % pool->block_size == 0);
492 old.u64 = __sync_lock_test_and_set(&pool_state->u64, new.u64);
493 if (old.next != state.next)
494 futex_wake(&pool_state->end, INT_MAX);
495 return state.next;
496 } else {
497 futex_wait(&pool_state->end, state.end);
498 continue;
499 }
500 }
501 }
502
503 int32_t
504 anv_block_pool_alloc(struct anv_block_pool *pool)
505 {
506 int32_t offset;
507
508 /* Try free list first. */
509 if (anv_free_list_pop(&pool->free_list, &pool->map, &offset)) {
510 assert(offset >= 0);
511 assert(pool->map);
512 return offset;
513 }
514
515 return anv_block_pool_alloc_new(pool, &pool->state);
516 }
517
518 /* Allocates a block out of the back of the block pool.
519 *
520 * This will allocated a block earlier than the "start" of the block pool.
521 * The offsets returned from this function will be negative but will still
522 * be correct relative to the block pool's map pointer.
523 *
524 * If you ever use anv_block_pool_alloc_back, then you will have to do
525 * gymnastics with the block pool's BO when doing relocations.
526 */
527 int32_t
528 anv_block_pool_alloc_back(struct anv_block_pool *pool)
529 {
530 int32_t offset;
531
532 /* Try free list first. */
533 if (anv_free_list_pop(&pool->back_free_list, &pool->map, &offset)) {
534 assert(offset < 0);
535 assert(pool->map);
536 return offset;
537 }
538
539 offset = anv_block_pool_alloc_new(pool, &pool->back_state);
540
541 /* The offset we get out of anv_block_pool_alloc_new() is actually the
542 * number of bytes downwards from the middle to the end of the block.
543 * We need to turn it into a (negative) offset from the middle to the
544 * start of the block.
545 */
546 assert(offset >= 0);
547 return -(offset + pool->block_size);
548 }
549
550 void
551 anv_block_pool_free(struct anv_block_pool *pool, int32_t offset)
552 {
553 if (offset < 0) {
554 anv_free_list_push(&pool->back_free_list, pool->map, offset);
555 } else {
556 anv_free_list_push(&pool->free_list, pool->map, offset);
557 }
558 }
559
560 static void
561 anv_fixed_size_state_pool_init(struct anv_fixed_size_state_pool *pool,
562 size_t state_size)
563 {
564 /* At least a cache line and must divide the block size. */
565 assert(state_size >= 64 && util_is_power_of_two(state_size));
566
567 pool->state_size = state_size;
568 pool->free_list = ANV_FREE_LIST_EMPTY;
569 pool->block.next = 0;
570 pool->block.end = 0;
571 }
572
573 static uint32_t
574 anv_fixed_size_state_pool_alloc(struct anv_fixed_size_state_pool *pool,
575 struct anv_block_pool *block_pool)
576 {
577 int32_t offset;
578 struct anv_block_state block, old, new;
579
580 /* Try free list first. */
581 if (anv_free_list_pop(&pool->free_list, &block_pool->map, &offset)) {
582 assert(offset >= 0);
583 return offset;
584 }
585
586 /* If free list was empty (or somebody raced us and took the items) we
587 * allocate a new item from the end of the block */
588 restart:
589 block.u64 = __sync_fetch_and_add(&pool->block.u64, pool->state_size);
590
591 if (block.next < block.end) {
592 return block.next;
593 } else if (block.next == block.end) {
594 offset = anv_block_pool_alloc(block_pool);
595 new.next = offset + pool->state_size;
596 new.end = offset + block_pool->block_size;
597 old.u64 = __sync_lock_test_and_set(&pool->block.u64, new.u64);
598 if (old.next != block.next)
599 futex_wake(&pool->block.end, INT_MAX);
600 return offset;
601 } else {
602 futex_wait(&pool->block.end, block.end);
603 goto restart;
604 }
605 }
606
607 static void
608 anv_fixed_size_state_pool_free(struct anv_fixed_size_state_pool *pool,
609 struct anv_block_pool *block_pool,
610 uint32_t offset)
611 {
612 anv_free_list_push(&pool->free_list, block_pool->map, offset);
613 }
614
615 void
616 anv_state_pool_init(struct anv_state_pool *pool,
617 struct anv_block_pool *block_pool)
618 {
619 pool->block_pool = block_pool;
620 for (unsigned i = 0; i < ANV_STATE_BUCKETS; i++) {
621 size_t size = 1 << (ANV_MIN_STATE_SIZE_LOG2 + i);
622 anv_fixed_size_state_pool_init(&pool->buckets[i], size);
623 }
624 VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
625 }
626
627 void
628 anv_state_pool_finish(struct anv_state_pool *pool)
629 {
630 VG(VALGRIND_DESTROY_MEMPOOL(pool));
631 }
632
633 struct anv_state
634 anv_state_pool_alloc(struct anv_state_pool *pool, size_t size, size_t align)
635 {
636 unsigned size_log2 = ilog2_round_up(size < align ? align : size);
637 assert(size_log2 <= ANV_MAX_STATE_SIZE_LOG2);
638 if (size_log2 < ANV_MIN_STATE_SIZE_LOG2)
639 size_log2 = ANV_MIN_STATE_SIZE_LOG2;
640 unsigned bucket = size_log2 - ANV_MIN_STATE_SIZE_LOG2;
641
642 struct anv_state state;
643 state.alloc_size = 1 << size_log2;
644 state.offset = anv_fixed_size_state_pool_alloc(&pool->buckets[bucket],
645 pool->block_pool);
646 state.map = pool->block_pool->map + state.offset;
647 VG(VALGRIND_MEMPOOL_ALLOC(pool, state.map, size));
648 return state;
649 }
650
651 void
652 anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state)
653 {
654 assert(util_is_power_of_two(state.alloc_size));
655 unsigned size_log2 = ilog2_round_up(state.alloc_size);
656 assert(size_log2 >= ANV_MIN_STATE_SIZE_LOG2 &&
657 size_log2 <= ANV_MAX_STATE_SIZE_LOG2);
658 unsigned bucket = size_log2 - ANV_MIN_STATE_SIZE_LOG2;
659
660 VG(VALGRIND_MEMPOOL_FREE(pool, state.map));
661 anv_fixed_size_state_pool_free(&pool->buckets[bucket],
662 pool->block_pool, state.offset);
663 }
664
665 #define NULL_BLOCK 1
666 struct stream_block {
667 uint32_t next;
668
669 /* The map for the BO at the time the block was givne to us */
670 void *current_map;
671
672 #ifdef HAVE_VALGRIND
673 void *_vg_ptr;
674 #endif
675 };
676
677 /* The state stream allocator is a one-shot, single threaded allocator for
678 * variable sized blocks. We use it for allocating dynamic state.
679 */
680 void
681 anv_state_stream_init(struct anv_state_stream *stream,
682 struct anv_block_pool *block_pool)
683 {
684 stream->block_pool = block_pool;
685 stream->next = 0;
686 stream->end = 0;
687 stream->current_block = NULL_BLOCK;
688
689 VG(VALGRIND_CREATE_MEMPOOL(stream, 0, false));
690 }
691
692 void
693 anv_state_stream_finish(struct anv_state_stream *stream)
694 {
695 struct stream_block *sb;
696 uint32_t block, next_block;
697
698 block = stream->current_block;
699 while (block != NULL_BLOCK) {
700 assert(block % stream->block_pool->block_size == 0);
701 sb = stream->block_pool->map + block;
702 next_block = VG_NOACCESS_READ(&sb->next);
703 VG(VALGRIND_MEMPOOL_FREE(stream, VG_NOACCESS_READ(&sb->_vg_ptr)));
704 anv_block_pool_free(stream->block_pool, block);
705 block = next_block;
706 }
707
708 VG(VALGRIND_DESTROY_MEMPOOL(stream));
709 }
710
711 struct anv_state
712 anv_state_stream_alloc(struct anv_state_stream *stream,
713 uint32_t size, uint32_t alignment)
714 {
715 struct stream_block *sb;
716 struct anv_state state;
717 uint32_t block;
718
719 state.offset = align_u32(stream->next, alignment);
720 if (state.offset + size > stream->end) {
721 block = anv_block_pool_alloc(stream->block_pool);
722 void *current_map = stream->block_pool->map;
723 sb = current_map + block;
724 VG_NOACCESS_WRITE(&sb->current_map, current_map);
725 VG_NOACCESS_WRITE(&sb->next, stream->current_block);
726 VG(VG_NOACCESS_WRITE(&sb->_vg_ptr, 0));
727 stream->current_block = block;
728 stream->next = block + sizeof(*sb);
729 stream->end = block + stream->block_pool->block_size;
730 state.offset = align_u32(stream->next, alignment);
731 assert(state.offset + size <= stream->end);
732 }
733
734 sb = stream->block_pool->map + stream->current_block;
735 void *current_map = VG_NOACCESS_READ(&sb->current_map);
736
737 state.map = current_map + state.offset;
738 state.alloc_size = size;
739
740 #ifdef HAVE_VALGRIND
741 void *vg_ptr = VG_NOACCESS_READ(&sb->_vg_ptr);
742 if (vg_ptr == NULL) {
743 vg_ptr = state.map;
744 VG_NOACCESS_WRITE(&sb->_vg_ptr, vg_ptr);
745 VALGRIND_MEMPOOL_ALLOC(stream, vg_ptr, size);
746 } else {
747 ptrdiff_t vg_offset = vg_ptr - current_map;
748 assert(vg_offset >= stream->current_block &&
749 vg_offset < stream->end);
750 VALGRIND_MEMPOOL_CHANGE(stream, vg_ptr, vg_ptr,
751 (state.offset + size) - vg_offset);
752 }
753 #endif
754
755 stream->next = state.offset + size;
756
757 return state;
758 }
759
760 struct bo_pool_bo_link {
761 struct bo_pool_bo_link *next;
762 struct anv_bo bo;
763 };
764
765 void
766 anv_bo_pool_init(struct anv_bo_pool *pool,
767 struct anv_device *device, uint32_t bo_size)
768 {
769 pool->device = device;
770 pool->bo_size = bo_size;
771 pool->free_list = NULL;
772
773 VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
774 }
775
776 void
777 anv_bo_pool_finish(struct anv_bo_pool *pool)
778 {
779 struct bo_pool_bo_link *link = PFL_PTR(pool->free_list);
780 while (link != NULL) {
781 struct bo_pool_bo_link link_copy = VG_NOACCESS_READ(link);
782
783 anv_gem_munmap(link_copy.bo.map, pool->bo_size);
784 anv_gem_close(pool->device, link_copy.bo.gem_handle);
785 link = link_copy.next;
786 }
787
788 VG(VALGRIND_DESTROY_MEMPOOL(pool));
789 }
790
791 VkResult
792 anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo)
793 {
794 VkResult result;
795
796 void *next_free_void;
797 if (anv_ptr_free_list_pop(&pool->free_list, &next_free_void)) {
798 struct bo_pool_bo_link *next_free = next_free_void;
799 *bo = VG_NOACCESS_READ(&next_free->bo);
800 assert(bo->map == next_free);
801 assert(bo->size == pool->bo_size);
802
803 VG(VALGRIND_MEMPOOL_ALLOC(pool, bo->map, pool->bo_size));
804
805 return VK_SUCCESS;
806 }
807
808 struct anv_bo new_bo;
809
810 result = anv_bo_init_new(&new_bo, pool->device, pool->bo_size);
811 if (result != VK_SUCCESS)
812 return result;
813
814 assert(new_bo.size == pool->bo_size);
815
816 new_bo.map = anv_gem_mmap(pool->device, new_bo.gem_handle, 0, pool->bo_size);
817 if (new_bo.map == NULL) {
818 anv_gem_close(pool->device, new_bo.gem_handle);
819 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
820 }
821
822 *bo = new_bo;
823
824 VG(VALGRIND_MEMPOOL_ALLOC(pool, bo->map, pool->bo_size));
825
826 return VK_SUCCESS;
827 }
828
829 void
830 anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo)
831 {
832 struct bo_pool_bo_link *link = bo->map;
833 link->bo = *bo;
834
835 VG(VALGRIND_MEMPOOL_FREE(pool, bo->map));
836 anv_ptr_free_list_push(&pool->free_list, link);
837 }