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 <genxml/gen_macros.h>
30#include "pan_bo.h"
31
32#include "util/u_dynarray.h"
33
34/* Represents grow-only memory. */
35
36struct pan_pool {
37        /* Parent device for allocation */
38        struct panfrost_device *dev;
39
40        /* Label for created BOs */
41        const char *label;
42
43        /* BO flags to use in the pool */
44        unsigned create_flags;
45
46        /* Minimum size for allocated BOs. */
47        size_t slab_size;
48};
49
50static inline void
51pan_pool_init(struct pan_pool *pool, struct panfrost_device *dev,
52              unsigned create_flags, size_t slab_size, const char *label)
53{
54        pool->dev = dev;
55        pool->create_flags = create_flags;
56        pool->slab_size = slab_size;
57        pool->label = label;
58}
59
60/* Represents a fat pointer for GPU-mapped memory, returned from the transient
61 * allocator and not used for much else */
62
63struct panfrost_ptr
64pan_pool_alloc_aligned(struct pan_pool *pool, size_t sz, unsigned alignment);
65
66#define PAN_POOL_ALLOCATOR(pool_subclass, alloc_func) \
67struct panfrost_ptr \
68pan_pool_alloc_aligned(struct pan_pool *p, size_t sz, unsigned alignment) \
69{ \
70        pool_subclass *pool = container_of(p, pool_subclass, base); \
71        return alloc_func(pool, sz, alignment); \
72}
73
74static inline mali_ptr
75pan_pool_upload_aligned(struct pan_pool *pool, const void *data, size_t sz, unsigned alignment)
76{
77        struct panfrost_ptr transfer = pan_pool_alloc_aligned(pool, sz, alignment);
78        memcpy(transfer.cpu, data, sz);
79        return transfer.gpu;
80}
81
82static inline mali_ptr
83pan_pool_upload(struct pan_pool *pool, const void *data, size_t sz)
84{
85        return pan_pool_upload_aligned(pool, data, sz, sz);
86}
87
88struct pan_desc_alloc_info {
89        unsigned size;
90        unsigned align;
91        unsigned nelems;
92};
93
94#define PAN_DESC_ARRAY(count, name) \
95        { \
96                .size = pan_size(name), \
97                .align = pan_alignment(name), \
98                .nelems = count, \
99        }
100
101#define PAN_DESC(name) PAN_DESC_ARRAY(1, name)
102
103#define PAN_DESC_AGGREGATE(...) \
104        (struct pan_desc_alloc_info[]) { \
105                __VA_ARGS__, \
106                { 0 }, \
107        }
108
109static inline struct panfrost_ptr
110pan_pool_alloc_descs(struct pan_pool *pool,
111                     const struct pan_desc_alloc_info *descs)
112{
113        unsigned size = 0;
114        unsigned align = descs[0].align;
115
116        for (unsigned i = 0; descs[i].size; i++) {
117                assert(!(size & (descs[i].align - 1)));
118                size += descs[i].size * descs[i].nelems;
119        }
120
121        return pan_pool_alloc_aligned(pool, size, align);
122}
123
124#define pan_pool_alloc_desc(pool, name) \
125        pan_pool_alloc_descs(pool, PAN_DESC_AGGREGATE(PAN_DESC(name)))
126
127#define pan_pool_alloc_desc_array(pool, count, name) \
128        pan_pool_alloc_descs(pool, PAN_DESC_AGGREGATE(PAN_DESC_ARRAY(count, name)))
129
130#define pan_pool_alloc_desc_aggregate(pool, ...) \
131        pan_pool_alloc_descs(pool, PAN_DESC_AGGREGATE(__VA_ARGS__))
132
133#endif
134