1 /*
2 * Copyright © 2022 Collabora Ltd. and Red Hat Inc.
3 * SPDX-License-Identifier: MIT
4 */
5 #ifndef NVK_HEAP_H
6 #define NVK_HEAP_H 1
7
8 #include "nvk_private.h"
9
10 #include "util/simple_mtx.h"
11 #include "util/vma.h"
12 #include "nvkmd/nvkmd.h"
13
14 struct nvk_device;
15
16 #define NVK_HEAP_MIN_SIZE_LOG2 16
17 #define NVK_HEAP_MAX_SIZE_LOG2 32
18 #define NVK_HEAP_MIN_SIZE (1ull << NVK_HEAP_MIN_SIZE_LOG2)
19 #define NVK_HEAP_MAX_SIZE (1ull << NVK_HEAP_MAX_SIZE_LOG2)
20 #define NVK_HEAP_MAX_BO_COUNT (NVK_HEAP_MAX_SIZE_LOG2 - \
21 NVK_HEAP_MIN_SIZE_LOG2 + 1)
22
23 struct nvk_heap_mem {
24 struct nvkmd_mem *mem;
25 uint64_t addr;
26 };
27
28 struct nvk_heap {
29 enum nvkmd_mem_flags mem_flags;
30 enum nvkmd_mem_map_flags map_flags;
31 uint32_t overalloc;
32
33 simple_mtx_t mutex;
34 struct util_vma_heap heap;
35
36 /* VA for contiguous heaps, NULL otherwise */
37 struct nvkmd_va *contig_va;
38
39 uint64_t total_size;
40
41 uint32_t mem_count;
42 struct nvk_heap_mem mem[NVK_HEAP_MAX_BO_COUNT];
43 };
44
45 VkResult nvk_heap_init(struct nvk_device *dev, struct nvk_heap *heap,
46 enum nvkmd_mem_flags mem_flags,
47 enum nvkmd_mem_map_flags map_flags,
48 uint32_t overalloc, bool contiguous);
49
50 void nvk_heap_finish(struct nvk_device *dev, struct nvk_heap *heap);
51
52 VkResult nvk_heap_alloc(struct nvk_device *dev, struct nvk_heap *heap,
53 uint64_t size, uint32_t alignment,
54 uint64_t *addr_out, void **map_out);
55
56 VkResult nvk_heap_upload(struct nvk_device *dev, struct nvk_heap *heap,
57 const void *data, size_t size, uint32_t alignment,
58 uint64_t *addr_out);
59
60 void nvk_heap_free(struct nvk_device *dev, struct nvk_heap *heap,
61 uint64_t addr, uint64_t size);
62
63 static inline uint64_t
nvk_heap_contiguous_base_address(struct nvk_heap * heap)64 nvk_heap_contiguous_base_address(struct nvk_heap *heap)
65 {
66 assert(heap->contig_va != NULL);
67 return heap->contig_va->addr;
68 }
69
70 #endif /* define NVK_HEAP_H */
71