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 "pan_device.h"
27 #include "pan_mempool.h"
28
29 /* Knockoff u_upload_mgr. Uploads wherever we left off, allocating new entries
30 * when needed.
31 *
32 * In "owned" mode, a single parent owns the entire pool, and the pool owns all
33 * created BOs. All BOs are tracked and addable as
34 * panfrost_pool_get_bo_handles. Freeing occurs at the level of an entire pool.
35 * This is useful for streaming uploads, where the batch owns the pool.
36 *
37 * In "unowned" mode, the pool is freestanding. It does not track created BOs
38 * or hold references. Instead, the consumer must manage the created BOs. This
39 * is more flexible, enabling non-transient CSO state or shader code to be
40 * packed with conservative lifetime handling.
41 */
42
43 static struct panfrost_bo *
panfrost_pool_alloc_backing(struct panfrost_pool * pool,size_t bo_sz)44 panfrost_pool_alloc_backing(struct panfrost_pool *pool, size_t bo_sz)
45 {
46 /* We don't know what the BO will be used for, so let's flag it
47 * RW and attach it to both the fragment and vertex/tiler jobs.
48 * TODO: if we want fine grained BO assignment we should pass
49 * flags to this function and keep the read/write,
50 * fragment/vertex+tiler pools separate.
51 */
52 struct panfrost_bo *bo = panfrost_bo_create(pool->base.dev, bo_sz,
53 pool->base.create_flags, pool->base.label);
54
55 if (pool->owned)
56 util_dynarray_append(&pool->bos, struct panfrost_bo *, bo);
57 else
58 panfrost_bo_unreference(pool->transient_bo);
59
60 pool->transient_bo = bo;
61 pool->transient_offset = 0;
62
63 return bo;
64 }
65
66 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)67 panfrost_pool_init(struct panfrost_pool *pool, void *memctx,
68 struct panfrost_device *dev,
69 unsigned create_flags, size_t slab_size, const char *label,
70 bool prealloc, bool owned)
71 {
72 memset(pool, 0, sizeof(*pool));
73 pan_pool_init(&pool->base, dev, create_flags, slab_size, label);
74 pool->owned = owned;
75
76 if (owned)
77 util_dynarray_init(&pool->bos, memctx);
78
79 if (prealloc)
80 panfrost_pool_alloc_backing(pool, pool->base.slab_size);
81 }
82
83 void
panfrost_pool_cleanup(struct panfrost_pool * pool)84 panfrost_pool_cleanup(struct panfrost_pool *pool)
85 {
86 if (!pool->owned) {
87 panfrost_bo_unreference(pool->transient_bo);
88 return;
89 }
90
91 util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo)
92 panfrost_bo_unreference(*bo);
93
94 util_dynarray_fini(&pool->bos);
95 }
96
97 void
panfrost_pool_get_bo_handles(struct panfrost_pool * pool,uint32_t * handles)98 panfrost_pool_get_bo_handles(struct panfrost_pool *pool, uint32_t *handles)
99 {
100 assert(pool->owned && "pool does not track BOs in unowned mode");
101
102 unsigned idx = 0;
103 util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo) {
104 assert((*bo)->gem_handle > 0);
105 handles[idx++] = (*bo)->gem_handle;
106
107 /* Update the BO access flags so that panfrost_bo_wait() knows
108 * about all pending accesses.
109 * We only keep the READ/WRITE info since this is all the BO
110 * wait logic cares about.
111 * We also preserve existing flags as this batch might not
112 * be the first one to access the BO.
113 */
114 (*bo)->gpu_access |= PAN_BO_ACCESS_RW;
115 }
116 }
117
118 static struct panfrost_ptr
panfrost_pool_alloc_aligned(struct panfrost_pool * pool,size_t sz,unsigned alignment)119 panfrost_pool_alloc_aligned(struct panfrost_pool *pool, size_t sz, unsigned alignment)
120 {
121 assert(alignment == util_next_power_of_two(alignment));
122
123 /* Find or create a suitable BO */
124 struct panfrost_bo *bo = pool->transient_bo;
125 unsigned offset = ALIGN_POT(pool->transient_offset, alignment);
126
127 /* If we don't fit, allocate a new backing */
128 if (unlikely(bo == NULL || (offset + sz) >= pool->base.slab_size)) {
129 bo = panfrost_pool_alloc_backing(pool,
130 ALIGN_POT(MAX2(pool->base.slab_size, sz), 4096));
131 offset = 0;
132 }
133
134 pool->transient_offset = offset + sz;
135
136 struct panfrost_ptr ret = {
137 .cpu = bo->ptr.cpu + offset,
138 .gpu = bo->ptr.gpu + offset,
139 };
140
141 return ret;
142 }
143 PAN_POOL_ALLOCATOR(struct panfrost_pool, panfrost_pool_alloc_aligned)
144