xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/panfrost/pan_mempool.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * © Copyright 2018 Alyssa Rosenzweig
3  * Copyright (C) 2019 Collabora, Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  */
25 
26 #include <unistd.h>
27 #include <sys/mman.h>
28 
29 #include "pan_device.h"
30 #include "pan_mempool.h"
31 
32 /* Knockoff u_upload_mgr. Uploads wherever we left off, allocating new entries
33  * when needed.
34  *
35  * In "owned" mode, a single parent owns the entire pool, and the pool owns all
36  * created BOs. All BOs are tracked and addable as
37  * panfrost_pool_get_bo_handles. Freeing occurs at the level of an entire pool.
38  * This is useful for streaming uploads, where the batch owns the pool.
39  *
40  * In "unowned" mode, the pool is freestanding. It does not track created BOs
41  * or hold references. Instead, the consumer must manage the created BOs. This
42  * is more flexible, enabling non-transient CSO state or shader code to be
43  * packed with conservative lifetime handling.
44  */
45 
46 static struct panfrost_bo *
panfrost_pool_alloc_backing(struct panfrost_pool * pool,size_t bo_sz)47 panfrost_pool_alloc_backing(struct panfrost_pool *pool, size_t bo_sz)
48 {
49    /* We don't know what the BO will be used for, so let's flag it
50     * RW and attach it to both the fragment and vertex/tiler jobs.
51     * TODO: if we want fine grained BO assignment we should pass
52     * flags to this function and keep the read/write,
53     * fragment/vertex+tiler pools separate.
54     */
55    struct panfrost_bo *bo =
56       panfrost_bo_create(pool->dev, bo_sz, pool->create_flags, pool->label);
57    assert(bo);
58 
59    if (pool->owned)
60       util_dynarray_append(&pool->bos, struct panfrost_bo *, bo);
61    else
62       panfrost_bo_unreference(pool->transient_bo);
63 
64    pool->transient_bo = bo;
65    pool->transient_offset = 0;
66 
67    return bo;
68 }
69 
70 void
panfrost_pool_init(struct panfrost_pool * pool,void * memctx,struct panfrost_device * dev,unsigned create_flags,size_t slab_size,const char * label,bool prealloc,bool owned)71 panfrost_pool_init(struct panfrost_pool *pool, void *memctx,
72                    struct panfrost_device *dev, unsigned create_flags,
73                    size_t slab_size, const char *label, bool prealloc,
74                    bool owned)
75 {
76    memset(pool, 0, sizeof(*pool));
77    pan_pool_init(&pool->base, slab_size);
78    pool->dev = dev;
79    pool->create_flags = create_flags;
80    pool->label = label;
81    pool->owned = owned;
82 
83    if (owned)
84       util_dynarray_init(&pool->bos, memctx);
85 
86    if (prealloc)
87       panfrost_pool_alloc_backing(pool, pool->base.slab_size);
88 }
89 
90 void
panfrost_pool_cleanup(struct panfrost_pool * pool)91 panfrost_pool_cleanup(struct panfrost_pool *pool)
92 {
93    if (!pool->owned) {
94       panfrost_bo_unreference(pool->transient_bo);
95       return;
96    }
97 
98    util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo)
99       panfrost_bo_unreference(*bo);
100 
101    util_dynarray_fini(&pool->bos);
102 }
103 
104 void
panfrost_pool_get_bo_handles(struct panfrost_pool * pool,uint32_t * handles)105 panfrost_pool_get_bo_handles(struct panfrost_pool *pool, uint32_t *handles)
106 {
107    assert(pool->owned && "pool does not track BOs in unowned mode");
108 
109    unsigned idx = 0;
110    util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo) {
111       assert(panfrost_bo_handle(*bo) > 0);
112       handles[idx++] = panfrost_bo_handle(*bo);
113 
114       /* Update the BO access flags so that panfrost_bo_wait() knows
115        * about all pending accesses.
116        * We only keep the READ/WRITE info since this is all the BO
117        * wait logic cares about.
118        * We also preserve existing flags as this batch might not
119        * be the first one to access the BO.
120        */
121       (*bo)->gpu_access |= PAN_BO_ACCESS_RW;
122    }
123 }
124 
125 #define PAN_GUARD_SIZE 4096
126 
127 static struct panfrost_ptr
panfrost_pool_alloc_aligned(struct panfrost_pool * pool,size_t sz,unsigned alignment)128 panfrost_pool_alloc_aligned(struct panfrost_pool *pool, size_t sz,
129                             unsigned alignment)
130 {
131    assert(alignment == util_next_power_of_two(alignment));
132 
133    /* Find or create a suitable BO */
134    struct panfrost_bo *bo = pool->transient_bo;
135    unsigned offset = ALIGN_POT(pool->transient_offset, alignment);
136 
137 #ifdef PAN_DBG_OVERFLOW
138    if (unlikely(pool->dev->debug & PAN_DBG_OVERFLOW) &&
139        !(pool->create_flags & PAN_BO_INVISIBLE)) {
140       unsigned aligned = ALIGN_POT(sz, sysconf(_SC_PAGESIZE));
141       unsigned bo_size = aligned + PAN_GUARD_SIZE;
142 
143       bo = panfrost_pool_alloc_backing(pool, bo_size);
144       memset(bo->ptr.cpu, 0xbb, bo_size);
145 
146       /* Place the object as close as possible to the protected
147        * region at the end of the buffer while keeping alignment. */
148       offset = ROUND_DOWN_TO(aligned - sz, alignment);
149 
150       if (mprotect(bo->ptr.cpu + aligned, PAN_GUARD_SIZE, PROT_NONE) == -1)
151          perror("mprotect");
152 
153       pool->transient_bo = NULL;
154    }
155 #endif
156 
157    /* If we don't fit, allocate a new backing */
158    if (unlikely(bo == NULL || (offset + sz) >= pool->base.slab_size)) {
159       bo = panfrost_pool_alloc_backing(
160          pool, ALIGN_POT(MAX2(pool->base.slab_size, sz), 4096));
161       offset = 0;
162    }
163 
164    pool->transient_offset = offset + sz;
165 
166    struct panfrost_ptr ret = {
167       .cpu = bo->ptr.cpu + offset,
168       .gpu = bo->ptr.gpu + offset,
169    };
170 
171    return ret;
172 }
173 PAN_POOL_ALLOCATOR(struct panfrost_pool, panfrost_pool_alloc_aligned)
174