asahi: Label BOs internally
[platform/upstream/mesa.git] / src / asahi / lib / pool.h
1 /*
2  * © Copyright 2017-2018 Alyssa Rosenzweig
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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  */
24
25 #ifndef __AGX_POOL_H__
26 #define __AGX_POOL_H__
27
28 #include <stddef.h>
29 #include "asahi/lib/agx_pack.h"
30 #include "agx_bo.h"
31
32 #include "util/u_dynarray.h"
33
34 /* Represents a pool of memory that can only grow, used to allocate objects
35  * with the same lifetime as the pool itself. In OpenGL, a pool is owned by the
36  * batch for transient structures. In Vulkan, it may be owned by e.g. the
37  * command pool */
38
39 struct agx_pool {
40    /* Parent device for allocation */
41    struct agx_device *dev;
42
43    /* BOs allocated by this pool */
44    struct util_dynarray bos;
45
46    /* Current transient BO */
47    struct agx_bo *transient_bo;
48
49    /* Within the topmost transient BO, how much has been used? */
50    unsigned transient_offset;
51
52    /* BO flags to use in the pool */
53    unsigned create_flags;
54 };
55
56 void
57 agx_pool_init(struct agx_pool *pool, struct agx_device *dev,
58       unsigned create_flags, bool prealloc);
59
60 void
61 agx_pool_cleanup(struct agx_pool *pool);
62
63 static inline unsigned
64 agx_pool_num_bos(struct agx_pool *pool)
65 {
66    return util_dynarray_num_elements(&pool->bos, struct agx_bo *);
67 }
68
69 void
70 agx_pool_get_bo_handles(struct agx_pool *pool, uint32_t *handles);
71
72 /* Represents a fat pointer for GPU-mapped memory, returned from the transient
73  * allocator and not used for much else */
74
75 struct agx_ptr
76 agx_pool_alloc_aligned_with_bo(struct agx_pool *pool, size_t sz,
77                                unsigned alignment, struct agx_bo **bo);
78
79 static inline struct agx_ptr
80 agx_pool_alloc_aligned(struct agx_pool *pool, size_t sz, unsigned alignment)
81 {
82    return agx_pool_alloc_aligned_with_bo(pool, sz, alignment, NULL);
83 }
84
85 uint64_t
86 agx_pool_upload(struct agx_pool *pool, const void *data, size_t sz);
87
88 uint64_t
89 agx_pool_upload_aligned_with_bo(struct agx_pool *pool, const void *data,
90                                 size_t sz, unsigned alignment,
91                                 struct agx_bo **bo);
92
93 static inline uint64_t
94 agx_pool_upload_aligned(struct agx_pool *pool, const void *data, size_t sz, unsigned alignment)
95 {
96    return agx_pool_upload_aligned_with_bo(pool, data, sz, alignment, NULL);
97 }
98
99 struct agx_desc_alloc_info {
100    unsigned size;
101    unsigned align;
102    unsigned nelems;
103 };
104
105 #define AGX_DESC_ARRAY(count, name) \
106         { \
107                 .size = MALI_ ## name ## _LENGTH, \
108                 .align = MALI_ ## name ## _ALIGN, \
109                 .nelems = count, \
110         }
111
112 #define AGX_DESC(name) AGX_DESC_ARRAY(1, name)
113
114 #define AGX_DESC_AGGREGATE(...) \
115         (struct agx_desc_alloc_info[]) { \
116                 __VA_ARGS__, \
117                 { 0 }, \
118         }
119
120 static inline struct agx_ptr
121 agx_pool_alloc_descs(struct agx_pool *pool,
122                           const struct agx_desc_alloc_info *descs)
123 {
124    unsigned size = 0;
125    unsigned align = descs[0].align;
126
127    for (unsigned i = 0; descs[i].size; i++) {
128       assert(!(size & (descs[i].align - 1)));
129       size += descs[i].size * descs[i].nelems;
130    }
131
132    return agx_pool_alloc_aligned(pool, size, align);
133 }
134
135 #define agx_pool_alloc_desc(pool, name) \
136         agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(AGX_DESC(name)))
137
138 #define agx_pool_alloc_desc_array(pool, count, name) \
139         agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(AGX_DESC_ARRAY(count, name)))
140
141 #define agx_pool_alloc_desc_aggregate(pool, ...) \
142         agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(__VA_ARGS__))
143
144 #endif