• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
58    if (pool->owned)
59       util_dynarray_append(&pool->bos, struct panfrost_bo *, bo);
60    else
61       panfrost_bo_unreference(pool->transient_bo);
62 
63    pool->transient_bo = bo;
64    pool->transient_offset = 0;
65 
66    return bo;
67 }
68 
69 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)70 panfrost_pool_init(struct panfrost_pool *pool, void *memctx,
71                    struct panfrost_device *dev, unsigned create_flags,
72                    size_t slab_size, const char *label, bool prealloc,
73                    bool owned)
74 {
75    memset(pool, 0, sizeof(*pool));
76    pan_pool_init(&pool->base, slab_size);
77    pool->dev = dev;
78    pool->create_flags = create_flags;
79    pool->label = label;
80    pool->owned = owned;
81 
82    if (owned)
83       util_dynarray_init(&pool->bos, memctx);
84 
85    if (prealloc)
86       panfrost_pool_alloc_backing(pool, pool->base.slab_size);
87 }
88 
89 void
panfrost_pool_cleanup(struct panfrost_pool * pool)90 panfrost_pool_cleanup(struct panfrost_pool *pool)
91 {
92    if (!pool->owned) {
93       panfrost_bo_unreference(pool->transient_bo);
94       return;
95    }
96 
97    util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo)
98       panfrost_bo_unreference(*bo);
99 
100    util_dynarray_fini(&pool->bos);
101 }
102 
103 void
panfrost_pool_get_bo_handles(struct panfrost_pool * pool,uint32_t * handles)104 panfrost_pool_get_bo_handles(struct panfrost_pool *pool, uint32_t *handles)
105 {
106    assert(pool->owned && "pool does not track BOs in unowned mode");
107 
108    unsigned idx = 0;
109    util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo) {
110       assert(panfrost_bo_handle(*bo) > 0);
111       handles[idx++] = panfrost_bo_handle(*bo);
112 
113       /* Update the BO access flags so that panfrost_bo_wait() knows
114        * about all pending accesses.
115        * We only keep the READ/WRITE info since this is all the BO
116        * wait logic cares about.
117        * We also preserve existing flags as this batch might not
118        * be the first one to access the BO.
119        */
120       (*bo)->gpu_access |= PAN_BO_ACCESS_RW;
121    }
122 }
123 
124 #define PAN_GUARD_SIZE 4096
125 
126 static struct panfrost_ptr
panfrost_pool_alloc_aligned(struct panfrost_pool * pool,size_t sz,unsigned alignment)127 panfrost_pool_alloc_aligned(struct panfrost_pool *pool, size_t sz,
128                             unsigned alignment)
129 {
130    assert(alignment == util_next_power_of_two(alignment));
131 
132    /* Find or create a suitable BO */
133    struct panfrost_bo *bo = pool->transient_bo;
134    unsigned offset = ALIGN_POT(pool->transient_offset, alignment);
135 
136 #ifdef PAN_DBG_OVERFLOW
137    if (unlikely(pool->dev->debug & PAN_DBG_OVERFLOW) &&
138        !(pool->create_flags & PAN_BO_INVISIBLE)) {
139       unsigned aligned = ALIGN_POT(sz, sysconf(_SC_PAGESIZE));
140       unsigned bo_size = aligned + PAN_GUARD_SIZE;
141 
142       bo = panfrost_pool_alloc_backing(pool, bo_size);
143       memset(bo->ptr.cpu, 0xbb, bo_size);
144 
145       /* Place the object as close as possible to the protected
146        * region at the end of the buffer while keeping alignment. */
147       offset = ROUND_DOWN_TO(aligned - sz, alignment);
148 
149       if (mprotect(bo->ptr.cpu + aligned, PAN_GUARD_SIZE, PROT_NONE) == -1)
150          perror("mprotect");
151 
152       pool->transient_bo = NULL;
153    }
154 #endif
155 
156    /* If we don't fit, allocate a new backing */
157    if (unlikely(bo == NULL || (offset + sz) >= pool->base.slab_size)) {
158       bo = panfrost_pool_alloc_backing(
159          pool, ALIGN_POT(MAX2(pool->base.slab_size, sz), 4096));
160       offset = 0;
161    }
162 
163    pool->transient_offset = offset + sz;
164 
165    struct panfrost_ptr ret = {
166       .cpu = bo->ptr.cpu + offset,
167       .gpu = bo->ptr.gpu + offset,
168    };
169 
170    return ret;
171 }
172 PAN_POOL_ALLOCATOR(struct panfrost_pool, panfrost_pool_alloc_aligned)
173