Merge remote-tracking branch 'public/master' into vulkan
[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 #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) & ~(uintptr_t)0xfff))
202 #define PFL_PACK(ptr, count) ({ \
203 (void *)(((uintptr_t)(ptr) & ~(uintptr_t)0xfff) | ((count) & 0xfff)); \
204 })
205
206 static bool
207 anv_ptr_free_list_pop(void **list, void **elem)
208 {
209 void *current = *list;
210 while (PFL_PTR(current) != NULL) {
211 void **next_ptr = PFL_PTR(current);
212 void *new_ptr = VG_NOACCESS_READ(next_ptr);
213 unsigned new_count = PFL_COUNT(current) + 1;
214 void *new = PFL_PACK(new_ptr, new_count);
215 void *old = __sync_val_compare_and_swap(list, current, new);
216 if (old == current) {
217 *elem = PFL_PTR(current);
218 return true;
219 }
220 current = old;
221 }
222
223 return false;
224 }
225
226 static void
227 anv_ptr_free_list_push(void **list, void *elem)
228 {
229 void *old, *current;
230 void **next_ptr = elem;
231
232 /* The pointer-based free list requires that the pointer be
233 * page-aligned. This is because we use the bottom 12 bits of the
234 * pointer to store a counter to solve the ABA concurrency problem.
235 */
236 assert(((uintptr_t)elem & 0xfff) == 0);
237
238 old = *list;
239 do {
240 current = old;
241 VG_NOACCESS_WRITE(next_ptr, PFL_PTR(current));
242 unsigned new_count = PFL_COUNT(current) + 1;
243 void *new = PFL_PACK(elem, new_count);
244 old = __sync_val_compare_and_swap(list, current, new);
245 } while (old != current);
246 }
247
248 static uint32_t
249 anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state);
250
251 void
252 anv_block_pool_init(struct anv_block_pool *pool,
253 struct anv_device *device, uint32_t block_size)
254 {
255 assert(util_is_power_of_two(block_size));
256
257 pool->device = device;
258 pool->bo.gem_handle = 0;
259 pool->bo.offset = 0;
260 pool->bo.size = 0;
261 pool->bo.is_winsys_bo = false;
262 pool->block_size = block_size;
263 pool->free_list = ANV_FREE_LIST_EMPTY;
264 pool->back_free_list = ANV_FREE_LIST_EMPTY;
265
266 pool->fd = memfd_create("block pool", MFD_CLOEXEC);
267 if (pool->fd == -1)
268 return;
269
270 /* Just make it 2GB up-front. The Linux kernel won't actually back it
271 * with pages until we either map and fault on one of them or we use
272 * userptr and send a chunk of it off to the GPU.
273 */
274 if (ftruncate(pool->fd, BLOCK_POOL_MEMFD_SIZE) == -1)
275 return;
276
277 anv_vector_init(&pool->mmap_cleanups,
278 round_to_power_of_two(sizeof(struct anv_mmap_cleanup)), 128);
279
280 pool->state.next = 0;
281 pool->state.end = 0;
282 pool->back_state.next = 0;
283 pool->back_state.end = 0;
284
285 /* Immediately grow the pool so we'll have a backing bo. */
286 pool->state.end = anv_block_pool_grow(pool, &pool->state);
287 }
288
289 void
290 anv_block_pool_finish(struct anv_block_pool *pool)
291 {
292 struct anv_mmap_cleanup *cleanup;
293
294 anv_vector_foreach(cleanup, &pool->mmap_cleanups) {
295 if (cleanup->map)
296 munmap(cleanup->map, cleanup->size);
297 if (cleanup->gem_handle)
298 anv_gem_close(pool->device, cleanup->gem_handle);
299 }
300
301 anv_vector_finish(&pool->mmap_cleanups);
302
303 close(pool->fd);
304 }
305
306 #define PAGE_SIZE 4096
307
308 /** Grows and re-centers the block pool.
309 *
310 * We grow the block pool in one or both directions in such a way that the
311 * following conditions are met:
312 *
313 * 1) The size of the entire pool is always a power of two.
314 *
315 * 2) The pool only grows on both ends. Neither end can get
316 * shortened.
317 *
318 * 3) At the end of the allocation, we have about twice as much space
319 * allocated for each end as we have used. This way the pool doesn't
320 * grow too far in one direction or the other.
321 *
322 * 4) If the _alloc_back() has never been called, then the back portion of
323 * the pool retains a size of zero. (This makes it easier for users of
324 * the block pool that only want a one-sided pool.)
325 *
326 * 5) We have enough space allocated for at least one more block in
327 * whichever side `state` points to.
328 *
329 * 6) The center of the pool is always aligned to both the block_size of
330 * the pool and a 4K CPU page.
331 */
332 static uint32_t
333 anv_block_pool_grow(struct anv_block_pool *pool, struct anv_block_state *state)
334 {
335 size_t size;
336 void *map;
337 uint32_t gem_handle;
338 struct anv_mmap_cleanup *cleanup;
339
340 pthread_mutex_lock(&pool->device->mutex);
341
342 assert(state == &pool->state || state == &pool->back_state);
343
344 /* Gather a little usage information on the pool. Since we may have
345 * threadsd waiting in queue to get some storage while we resize, it's
346 * actually possible that total_used will be larger than old_size. In
347 * particular, block_pool_alloc() increments state->next prior to
348 * calling block_pool_grow, so this ensures that we get enough space for
349 * which ever side tries to grow the pool.
350 *
351 * We align to a page size because it makes it easier to do our
352 * calculations later in such a way that we state page-aigned.
353 */
354 uint32_t back_used = align_u32(pool->back_state.next, PAGE_SIZE);
355 uint32_t front_used = align_u32(pool->state.next, PAGE_SIZE);
356 uint32_t total_used = front_used + back_used;
357
358 assert(state == &pool->state || back_used > 0);
359
360 size_t old_size = pool->bo.size;
361
362 if (old_size != 0 &&
363 back_used * 2 <= pool->center_bo_offset &&
364 front_used * 2 <= (old_size - pool->center_bo_offset)) {
365 /* If we're in this case then this isn't the firsta allocation and we
366 * already have enough space on both sides to hold double what we
367 * have allocated. There's nothing for us to do.
368 */
369 goto done;
370 }
371
372 if (old_size == 0) {
373 /* This is the first allocation */
374 size = MAX2(32 * pool->block_size, PAGE_SIZE);
375 } else {
376 size = old_size * 2;
377 }
378
379 /* We can't have a block pool bigger than 1GB because we use signed
380 * 32-bit offsets in the free list and we don't want overflow. We
381 * should never need a block pool bigger than 1GB anyway.
382 */
383 assert(size <= (1u << 31));
384
385 /* We compute a new center_bo_offset such that, when we double the size
386 * of the pool, we maintain the ratio of how much is used by each side.
387 * This way things should remain more-or-less balanced.
388 */
389 uint32_t center_bo_offset;
390 if (back_used == 0) {
391 /* If we're in this case then we have never called alloc_back(). In
392 * this case, we want keep the offset at 0 to make things as simple
393 * as possible for users that don't care about back allocations.
394 */
395 center_bo_offset = 0;
396 } else {
397 /* Try to "center" the allocation based on how much is currently in
398 * use on each side of the center line.
399 */
400 center_bo_offset = ((uint64_t)size * back_used) / total_used;
401
402 /* Align down to a multiple of both the block size and page size */
403 uint32_t granularity = MAX2(pool->block_size, PAGE_SIZE);
404 assert(util_is_power_of_two(granularity));
405 center_bo_offset &= ~(granularity - 1);
406
407 assert(center_bo_offset >= back_used);
408
409 /* Make sure we don't shrink the back end of the pool */
410 if (center_bo_offset < pool->back_state.end)
411 center_bo_offset = pool->back_state.end;
412
413 /* Make sure that we don't shrink the front end of the pool */
414 if (size - center_bo_offset < pool->state.end)
415 center_bo_offset = size - pool->state.end;
416 }
417
418 assert(center_bo_offset % pool->block_size == 0);
419 assert(center_bo_offset % PAGE_SIZE == 0);
420
421 /* Assert that we only ever grow the pool */
422 assert(center_bo_offset >= pool->back_state.end);
423 assert(size - center_bo_offset >= pool->state.end);
424
425 cleanup = anv_vector_add(&pool->mmap_cleanups);
426 if (!cleanup)
427 goto fail;
428 *cleanup = ANV_MMAP_CLEANUP_INIT;
429
430 /* Just leak the old map until we destroy the pool. We can't munmap it
431 * without races or imposing locking on the block allocate fast path. On
432 * the whole the leaked maps adds up to less than the size of the
433 * current map. MAP_POPULATE seems like the right thing to do, but we
434 * should try to get some numbers.
435 */
436 map = mmap(NULL, size, PROT_READ | PROT_WRITE,
437 MAP_SHARED | MAP_POPULATE, pool->fd,
438 BLOCK_POOL_MEMFD_CENTER - center_bo_offset);
439 cleanup->map = map;
440 cleanup->size = size;
441
442 if (map == MAP_FAILED)
443 goto fail;
444
445 gem_handle = anv_gem_userptr(pool->device, map, size);
446 if (gem_handle == 0)
447 goto fail;
448 cleanup->gem_handle = gem_handle;
449
450 #if 0
451 /* Regular objects are created I915_CACHING_CACHED on LLC platforms and
452 * I915_CACHING_NONE on non-LLC platforms. However, userptr objects are
453 * always created as I915_CACHING_CACHED, which on non-LLC means
454 * snooped. That can be useful but comes with a bit of overheard. Since
455 * we're eplicitly clflushing and don't want the overhead we need to turn
456 * it off. */
457 if (!pool->device->info.has_llc) {
458 anv_gem_set_caching(pool->device, gem_handle, I915_CACHING_NONE);
459 anv_gem_set_domain(pool->device, gem_handle,
460 I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
461 }
462 #endif
463
464 /* Now that we successfull allocated everything, we can write the new
465 * values back into pool. */
466 pool->map = map + center_bo_offset;
467 pool->center_bo_offset = center_bo_offset;
468 pool->bo.gem_handle = gem_handle;
469 pool->bo.size = size;
470 pool->bo.map = map;
471 pool->bo.index = 0;
472
473 done:
474 pthread_mutex_unlock(&pool->device->mutex);
475
476 /* Return the appropreate new size. This function never actually
477 * updates state->next. Instead, we let the caller do that because it
478 * needs to do so in order to maintain its concurrency model.
479 */
480 if (state == &pool->state) {
481 return pool->bo.size - pool->center_bo_offset;
482 } else {
483 assert(pool->center_bo_offset > 0);
484 return pool->center_bo_offset;
485 }
486
487 fail:
488 pthread_mutex_unlock(&pool->device->mutex);
489
490 return 0;
491 }
492
493 static uint32_t
494 anv_block_pool_alloc_new(struct anv_block_pool *pool,
495 struct anv_block_state *pool_state)
496 {
497 struct anv_block_state state, old, new;
498
499 while (1) {
500 state.u64 = __sync_fetch_and_add(&pool_state->u64, pool->block_size);
501 if (state.next < state.end) {
502 assert(pool->map);
503 return state.next;
504 } else if (state.next == state.end) {
505 /* We allocated the first block outside the pool, we have to grow it.
506 * pool_state->next acts a mutex: threads who try to allocate now will
507 * get block indexes above the current limit and hit futex_wait
508 * below. */
509 new.next = state.next + pool->block_size;
510 new.end = anv_block_pool_grow(pool, pool_state);
511 assert(new.end >= new.next && new.end % pool->block_size == 0);
512 old.u64 = __sync_lock_test_and_set(&pool_state->u64, new.u64);
513 if (old.next != state.next)
514 futex_wake(&pool_state->end, INT_MAX);
515 return state.next;
516 } else {
517 futex_wait(&pool_state->end, state.end);
518 continue;
519 }
520 }
521 }
522
523 int32_t
524 anv_block_pool_alloc(struct anv_block_pool *pool)
525 {
526 int32_t offset;
527
528 /* Try free list first. */
529 if (anv_free_list_pop(&pool->free_list, &pool->map, &offset)) {
530 assert(offset >= 0);
531 assert(pool->map);
532 return offset;
533 }
534
535 return anv_block_pool_alloc_new(pool, &pool->state);
536 }
537
538 /* Allocates a block out of the back of the block pool.
539 *
540 * This will allocated a block earlier than the "start" of the block pool.
541 * The offsets returned from this function will be negative but will still
542 * be correct relative to the block pool's map pointer.
543 *
544 * If you ever use anv_block_pool_alloc_back, then you will have to do
545 * gymnastics with the block pool's BO when doing relocations.
546 */
547 int32_t
548 anv_block_pool_alloc_back(struct anv_block_pool *pool)
549 {
550 int32_t offset;
551
552 /* Try free list first. */
553 if (anv_free_list_pop(&pool->back_free_list, &pool->map, &offset)) {
554 assert(offset < 0);
555 assert(pool->map);
556 return offset;
557 }
558
559 offset = anv_block_pool_alloc_new(pool, &pool->back_state);
560
561 /* The offset we get out of anv_block_pool_alloc_new() is actually the
562 * number of bytes downwards from the middle to the end of the block.
563 * We need to turn it into a (negative) offset from the middle to the
564 * start of the block.
565 */
566 assert(offset >= 0);
567 return -(offset + pool->block_size);
568 }
569
570 void
571 anv_block_pool_free(struct anv_block_pool *pool, int32_t offset)
572 {
573 if (offset < 0) {
574 anv_free_list_push(&pool->back_free_list, pool->map, offset);
575 } else {
576 anv_free_list_push(&pool->free_list, pool->map, offset);
577 }
578 }
579
580 static void
581 anv_fixed_size_state_pool_init(struct anv_fixed_size_state_pool *pool,
582 size_t state_size)
583 {
584 /* At least a cache line and must divide the block size. */
585 assert(state_size >= 64 && util_is_power_of_two(state_size));
586
587 pool->state_size = state_size;
588 pool->free_list = ANV_FREE_LIST_EMPTY;
589 pool->block.next = 0;
590 pool->block.end = 0;
591 }
592
593 static uint32_t
594 anv_fixed_size_state_pool_alloc(struct anv_fixed_size_state_pool *pool,
595 struct anv_block_pool *block_pool)
596 {
597 int32_t offset;
598 struct anv_block_state block, old, new;
599
600 /* Try free list first. */
601 if (anv_free_list_pop(&pool->free_list, &block_pool->map, &offset)) {
602 assert(offset >= 0);
603 return offset;
604 }
605
606 /* If free list was empty (or somebody raced us and took the items) we
607 * allocate a new item from the end of the block */
608 restart:
609 block.u64 = __sync_fetch_and_add(&pool->block.u64, pool->state_size);
610
611 if (block.next < block.end) {
612 return block.next;
613 } else if (block.next == block.end) {
614 offset = anv_block_pool_alloc(block_pool);
615 new.next = offset + pool->state_size;
616 new.end = offset + block_pool->block_size;
617 old.u64 = __sync_lock_test_and_set(&pool->block.u64, new.u64);
618 if (old.next != block.next)
619 futex_wake(&pool->block.end, INT_MAX);
620 return offset;
621 } else {
622 futex_wait(&pool->block.end, block.end);
623 goto restart;
624 }
625 }
626
627 static void
628 anv_fixed_size_state_pool_free(struct anv_fixed_size_state_pool *pool,
629 struct anv_block_pool *block_pool,
630 uint32_t offset)
631 {
632 anv_free_list_push(&pool->free_list, block_pool->map, offset);
633 }
634
635 void
636 anv_state_pool_init(struct anv_state_pool *pool,
637 struct anv_block_pool *block_pool)
638 {
639 pool->block_pool = block_pool;
640 for (unsigned i = 0; i < ANV_STATE_BUCKETS; i++) {
641 size_t size = 1 << (ANV_MIN_STATE_SIZE_LOG2 + i);
642 anv_fixed_size_state_pool_init(&pool->buckets[i], size);
643 }
644 VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
645 }
646
647 void
648 anv_state_pool_finish(struct anv_state_pool *pool)
649 {
650 VG(VALGRIND_DESTROY_MEMPOOL(pool));
651 }
652
653 struct anv_state
654 anv_state_pool_alloc(struct anv_state_pool *pool, size_t size, size_t align)
655 {
656 unsigned size_log2 = ilog2_round_up(size < align ? align : size);
657 assert(size_log2 <= ANV_MAX_STATE_SIZE_LOG2);
658 if (size_log2 < ANV_MIN_STATE_SIZE_LOG2)
659 size_log2 = ANV_MIN_STATE_SIZE_LOG2;
660 unsigned bucket = size_log2 - ANV_MIN_STATE_SIZE_LOG2;
661
662 struct anv_state state;
663 state.alloc_size = 1 << size_log2;
664 state.offset = anv_fixed_size_state_pool_alloc(&pool->buckets[bucket],
665 pool->block_pool);
666 state.map = pool->block_pool->map + state.offset;
667 VG(VALGRIND_MEMPOOL_ALLOC(pool, state.map, size));
668 return state;
669 }
670
671 void
672 anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state)
673 {
674 assert(util_is_power_of_two(state.alloc_size));
675 unsigned size_log2 = ilog2_round_up(state.alloc_size);
676 assert(size_log2 >= ANV_MIN_STATE_SIZE_LOG2 &&
677 size_log2 <= ANV_MAX_STATE_SIZE_LOG2);
678 unsigned bucket = size_log2 - ANV_MIN_STATE_SIZE_LOG2;
679
680 VG(VALGRIND_MEMPOOL_FREE(pool, state.map));
681 anv_fixed_size_state_pool_free(&pool->buckets[bucket],
682 pool->block_pool, state.offset);
683 }
684
685 #define NULL_BLOCK 1
686 struct anv_state_stream_block {
687 /* The next block */
688 struct anv_state_stream_block *next;
689
690 /* The offset into the block pool at which this block starts */
691 uint32_t offset;
692
693 #ifdef HAVE_VALGRIND
694 /* A pointer to the first user-allocated thing in this block. This is
695 * what valgrind sees as the start of the block.
696 */
697 void *_vg_ptr;
698 #endif
699 };
700
701 /* The state stream allocator is a one-shot, single threaded allocator for
702 * variable sized blocks. We use it for allocating dynamic state.
703 */
704 void
705 anv_state_stream_init(struct anv_state_stream *stream,
706 struct anv_block_pool *block_pool)
707 {
708 stream->block_pool = block_pool;
709 stream->block = NULL;
710
711 /* Ensure that next + whatever > end. This way the first call to
712 * state_stream_alloc fetches a new block.
713 */
714 stream->next = 1;
715 stream->end = 0;
716
717 VG(VALGRIND_CREATE_MEMPOOL(stream, 0, false));
718 }
719
720 void
721 anv_state_stream_finish(struct anv_state_stream *stream)
722 {
723 VG(const uint32_t block_size = stream->block_pool->block_size);
724
725 struct anv_state_stream_block *next = stream->block;
726 while (next != NULL) {
727 VG(VALGRIND_MAKE_MEM_DEFINED(next, sizeof(*next)));
728 struct anv_state_stream_block sb = VG_NOACCESS_READ(next);
729 VG(VALGRIND_MEMPOOL_FREE(stream, sb._vg_ptr));
730 VG(VALGRIND_MAKE_MEM_UNDEFINED(next, block_size));
731 anv_block_pool_free(stream->block_pool, sb.offset);
732 next = sb.next;
733 }
734
735 VG(VALGRIND_DESTROY_MEMPOOL(stream));
736 }
737
738 struct anv_state
739 anv_state_stream_alloc(struct anv_state_stream *stream,
740 uint32_t size, uint32_t alignment)
741 {
742 struct anv_state_stream_block *sb = stream->block;
743
744 struct anv_state state;
745
746 state.offset = align_u32(stream->next, alignment);
747 if (state.offset + size > stream->end) {
748 uint32_t block = anv_block_pool_alloc(stream->block_pool);
749 sb = stream->block_pool->map + block;
750
751 VG(VALGRIND_MAKE_MEM_UNDEFINED(sb, sizeof(*sb)));
752 sb->next = stream->block;
753 sb->offset = block;
754 VG(sb->_vg_ptr = NULL);
755 VG(VALGRIND_MAKE_MEM_NOACCESS(sb, stream->block_pool->block_size));
756
757 stream->block = sb;
758 stream->start = block;
759 stream->next = block + sizeof(*sb);
760 stream->end = block + stream->block_pool->block_size;
761
762 state.offset = align_u32(stream->next, alignment);
763 assert(state.offset + size <= stream->end);
764 }
765
766 assert(state.offset > stream->start);
767 state.map = (void *)sb + (state.offset - stream->start);
768 state.alloc_size = size;
769
770 #ifdef HAVE_VALGRIND
771 void *vg_ptr = VG_NOACCESS_READ(&sb->_vg_ptr);
772 if (vg_ptr == NULL) {
773 vg_ptr = state.map;
774 VG_NOACCESS_WRITE(&sb->_vg_ptr, vg_ptr);
775 VALGRIND_MEMPOOL_ALLOC(stream, vg_ptr, size);
776 } else {
777 void *state_end = state.map + state.alloc_size;
778 /* This only updates the mempool. The newly allocated chunk is still
779 * marked as NOACCESS. */
780 VALGRIND_MEMPOOL_CHANGE(stream, vg_ptr, vg_ptr, state_end - vg_ptr);
781 /* Mark the newly allocated chunk as undefined */
782 VALGRIND_MAKE_MEM_UNDEFINED(state.map, state.alloc_size);
783 }
784 #endif
785
786 stream->next = state.offset + size;
787
788 return state;
789 }
790
791 struct bo_pool_bo_link {
792 struct bo_pool_bo_link *next;
793 struct anv_bo bo;
794 };
795
796 void
797 anv_bo_pool_init(struct anv_bo_pool *pool, struct anv_device *device)
798 {
799 pool->device = device;
800 memset(pool->free_list, 0, sizeof(pool->free_list));
801
802 VG(VALGRIND_CREATE_MEMPOOL(pool, 0, false));
803 }
804
805 void
806 anv_bo_pool_finish(struct anv_bo_pool *pool)
807 {
808 for (unsigned i = 0; i < ARRAY_SIZE(pool->free_list); i++) {
809 struct bo_pool_bo_link *link = PFL_PTR(pool->free_list[i]);
810 while (link != NULL) {
811 struct bo_pool_bo_link link_copy = VG_NOACCESS_READ(link);
812
813 anv_gem_munmap(link_copy.bo.map, link_copy.bo.size);
814 anv_gem_close(pool->device, link_copy.bo.gem_handle);
815 link = link_copy.next;
816 }
817 }
818
819 VG(VALGRIND_DESTROY_MEMPOOL(pool));
820 }
821
822 VkResult
823 anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo, uint32_t size)
824 {
825 VkResult result;
826
827 const unsigned size_log2 = size < 4096 ? 12 : ilog2_round_up(size);
828 const unsigned pow2_size = 1 << size_log2;
829 const unsigned bucket = size_log2 - 12;
830 assert(bucket < ARRAY_SIZE(pool->free_list));
831
832 void *next_free_void;
833 if (anv_ptr_free_list_pop(&pool->free_list[bucket], &next_free_void)) {
834 struct bo_pool_bo_link *next_free = next_free_void;
835 *bo = VG_NOACCESS_READ(&next_free->bo);
836 assert(bo->map == next_free);
837 assert(size <= bo->size);
838
839 VG(VALGRIND_MEMPOOL_ALLOC(pool, bo->map, size));
840
841 return VK_SUCCESS;
842 }
843
844 struct anv_bo new_bo;
845
846 result = anv_bo_init_new(&new_bo, pool->device, pow2_size);
847 if (result != VK_SUCCESS)
848 return result;
849
850 assert(new_bo.size == pow2_size);
851
852 new_bo.map = anv_gem_mmap(pool->device, new_bo.gem_handle, 0, pow2_size, 0);
853 if (new_bo.map == NULL) {
854 anv_gem_close(pool->device, new_bo.gem_handle);
855 return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
856 }
857
858 *bo = new_bo;
859
860 VG(VALGRIND_MEMPOOL_ALLOC(pool, bo->map, size));
861
862 return VK_SUCCESS;
863 }
864
865 void
866 anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo_in)
867 {
868 /* Make a copy in case the anv_bo happens to be storred in the BO */
869 struct anv_bo bo = *bo_in;
870 struct bo_pool_bo_link *link = bo.map;
871 link->bo = bo;
872
873 assert(util_is_power_of_two(bo.size));
874 const unsigned size_log2 = ilog2_round_up(bo.size);
875 const unsigned bucket = size_log2 - 12;
876 assert(bucket < ARRAY_SIZE(pool->free_list));
877
878 VG(VALGRIND_MEMPOOL_FREE(pool, bo.map));
879 anv_ptr_free_list_push(&pool->free_list[bucket], link);
880 }