panfrost: Store transient BOs in a dynamic array
[mesa.git] / src / panfrost / lib / pan_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 __PAN_POOL_H__
26 #define __PAN_POOL_H__
27
28 #include <stddef.h>
29 #include <midgard_pack.h>
30
31 #include "util/u_dynarray.h"
32
33 /* Represents a pool of memory that can only grow, used to allocate objects
34 * with the same lifetime as the pool itself. In OpenGL, a pool is owned by the
35 * batch for transient structures. In Vulkan, it may be owned by e.g. the
36 * command pool */
37
38 struct pan_pool {
39 /* Parent device for allocation */
40 struct panfrost_device *dev;
41
42 /* BOs allocated by this pool */
43 struct util_dynarray bos;
44
45 /* Current transient BO */
46 struct panfrost_bo *transient_bo;
47
48 /* Within the topmost transient BO, how much has been used? */
49 unsigned transient_offset;
50
51 /* BO flags to use in the pool */
52 unsigned create_flags;
53 };
54
55 void
56 panfrost_pool_init(struct pan_pool *pool, void *memctx,
57 struct panfrost_device *dev, unsigned create_flags,
58 bool prealloc);
59
60 void
61 panfrost_pool_cleanup(struct pan_pool *pool);
62
63 static inline unsigned
64 panfrost_pool_num_bos(struct pan_pool *pool)
65 {
66 return util_dynarray_num_elements(&pool->bos, struct panfrost_bo *);
67 }
68
69 void
70 panfrost_pool_get_bo_handles(struct pan_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 panfrost_transfer {
76 uint8_t *cpu;
77 mali_ptr gpu;
78 };
79
80 struct panfrost_transfer
81 panfrost_pool_alloc_aligned(struct pan_pool *pool, size_t sz, unsigned alignment);
82
83 /* Default to self-alignment */
84
85 static inline struct panfrost_transfer
86 panfrost_pool_alloc(struct pan_pool *pool, size_t sz)
87 {
88 assert(sz == util_next_power_of_two(sz));
89 return panfrost_pool_alloc_aligned(pool, sz, sz);
90 }
91
92 struct panfrost_transfer
93 panfrost_pool_alloc(struct pan_pool *pool, size_t sz);
94
95 mali_ptr
96 panfrost_pool_upload(struct pan_pool *pool, const void *data, size_t sz);
97
98 mali_ptr
99 panfrost_pool_upload_aligned(struct pan_pool *pool, const void *data, size_t sz, unsigned alignment);
100
101 #endif