23dba284425d7884336b8c8db1a36820d95f4198
[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 /* Represents a pool of memory that can only grow, used to allocate objects
32 * with the same lifetime as the pool itself. In OpenGL, a pool is owned by the
33 * batch for transient structures. In Vulkan, it may be owned by e.g. the
34 * command pool */
35
36 struct pan_pool {
37 /* Parent device for allocation */
38 struct panfrost_device *dev;
39
40 /* panfrost_bo -> access_flags owned by the pool */
41 struct hash_table *bos;
42
43 /* Current transient BO */
44 struct panfrost_bo *transient_bo;
45
46 /* Within the topmost transient BO, how much has been used? */
47 unsigned transient_offset;
48
49 /* BO flags to use in the pool */
50 unsigned create_flags;
51 };
52
53 void
54 panfrost_pool_init(struct pan_pool *pool, void *memctx,
55 struct panfrost_device *dev, unsigned create_flags,
56 bool prealloc);
57
58 /* Represents a fat pointer for GPU-mapped memory, returned from the transient
59 * allocator and not used for much else */
60
61 struct panfrost_transfer {
62 uint8_t *cpu;
63 mali_ptr gpu;
64 };
65
66 struct panfrost_transfer
67 panfrost_pool_alloc_aligned(struct pan_pool *pool, size_t sz, unsigned alignment);
68
69 /* Default to self-alignment */
70
71 static inline struct panfrost_transfer
72 panfrost_pool_alloc(struct pan_pool *pool, size_t sz)
73 {
74 assert(sz == util_next_power_of_two(sz));
75 return panfrost_pool_alloc_aligned(pool, sz, sz);
76 }
77
78 struct panfrost_transfer
79 panfrost_pool_alloc(struct pan_pool *pool, size_t sz);
80
81 mali_ptr
82 panfrost_pool_upload(struct pan_pool *pool, const void *data, size_t sz);
83
84 mali_ptr
85 panfrost_pool_upload_aligned(struct pan_pool *pool, const void *data, size_t sz, unsigned alignment);
86
87 #endif