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
agx_pool_num_bos(struct agx_pool * pool)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(struct agx_pool *pool, size_t sz, unsigned alignment);
77
78 uint64_t
79 agx_pool_upload(struct agx_pool *pool, const void *data, size_t sz);
80
81 uint64_t
82 agx_pool_upload_aligned(struct agx_pool *pool, const void *data, size_t sz, unsigned alignment);
83
84 struct agx_desc_alloc_info {
85 unsigned size;
86 unsigned align;
87 unsigned nelems;
88 };
89
90 #define AGX_DESC_ARRAY(count, name) \
91 { \
92 .size = MALI_ ## name ## _LENGTH, \
93 .align = MALI_ ## name ## _ALIGN, \
94 .nelems = count, \
95 }
96
97 #define AGX_DESC(name) AGX_DESC_ARRAY(1, name)
98
99 #define AGX_DESC_AGGREGATE(...) \
100 (struct agx_desc_alloc_info[]) { \
101 __VA_ARGS__, \
102 { 0 }, \
103 }
104
105 static inline struct agx_ptr
agx_pool_alloc_descs(struct agx_pool * pool,const struct agx_desc_alloc_info * descs)106 agx_pool_alloc_descs(struct agx_pool *pool,
107 const struct agx_desc_alloc_info *descs)
108 {
109 unsigned size = 0;
110 unsigned align = descs[0].align;
111
112 for (unsigned i = 0; descs[i].size; i++) {
113 assert(!(size & (descs[i].align - 1)));
114 size += descs[i].size * descs[i].nelems;
115 }
116
117 return agx_pool_alloc_aligned(pool, size, align);
118 }
119
120 #define agx_pool_alloc_desc(pool, name) \
121 agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(AGX_DESC(name)))
122
123 #define agx_pool_alloc_desc_array(pool, count, name) \
124 agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(AGX_DESC_ARRAY(count, name)))
125
126 #define agx_pool_alloc_desc_aggregate(pool, ...) \
127 agx_pool_alloc_descs(pool, AGX_DESC_AGGREGATE(__VA_ARGS__))
128
129 #endif
130