Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/panfrost/vulkan/panvk_mempool.c
4560 views
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 "panvk_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
* panvk_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 *
44
panvk_pool_alloc_backing(struct panvk_pool *pool, size_t bo_sz)
45
{
46
/* If there's a free BO in our BO pool, let's pick it. */
47
if (pool->bo_pool &&
48
util_dynarray_num_elements(&pool->bo_pool->free_bos, struct panfrost_bo *))
49
return util_dynarray_pop(&pool->bo_pool->free_bos, struct panfrost_bo *);
50
51
/* We don't know what the BO will be used for, so let's flag it
52
* RW and attach it to both the fragment and vertex/tiler jobs.
53
* TODO: if we want fine grained BO assignment we should pass
54
* flags to this function and keep the read/write,
55
* fragment/vertex+tiler pools separate.
56
*/
57
struct panfrost_bo *bo = panfrost_bo_create(pool->base.dev, bo_sz,
58
pool->base.create_flags,
59
pool->base.label);
60
61
util_dynarray_append(&pool->bos, struct panfrost_bo *, bo);
62
pool->transient_bo = bo;
63
pool->transient_offset = 0;
64
65
return bo;
66
}
67
68
static struct panfrost_ptr
69
panvk_pool_alloc_aligned(struct panvk_pool *pool, size_t sz, unsigned alignment)
70
{
71
assert(alignment == util_next_power_of_two(alignment));
72
73
/* Find or create a suitable BO */
74
struct panfrost_bo *bo = pool->transient_bo;
75
unsigned offset = ALIGN_POT(pool->transient_offset, alignment);
76
77
/* If we don't fit, allocate a new backing */
78
if (unlikely(bo == NULL || (offset + sz) >= pool->base.slab_size)) {
79
bo = panvk_pool_alloc_backing(pool,
80
ALIGN_POT(MAX2(pool->base.slab_size, sz),
81
4096));
82
offset = 0;
83
}
84
85
pool->transient_offset = offset + sz;
86
87
struct panfrost_ptr ret = {
88
.cpu = bo->ptr.cpu + offset,
89
.gpu = bo->ptr.gpu + offset,
90
};
91
92
return ret;
93
}
94
PAN_POOL_ALLOCATOR(struct panvk_pool, panvk_pool_alloc_aligned)
95
96
void
97
panvk_pool_init(struct panvk_pool *pool,
98
struct panfrost_device *dev, struct panvk_bo_pool *bo_pool,
99
unsigned create_flags, size_t slab_size, const char *label,
100
bool prealloc)
101
{
102
memset(pool, 0, sizeof(*pool));
103
pan_pool_init(&pool->base, dev, create_flags, slab_size, label);
104
pool->bo_pool = bo_pool;
105
106
util_dynarray_init(&pool->bos, NULL);
107
108
if (prealloc)
109
panvk_pool_alloc_backing(pool, pool->base.slab_size);
110
}
111
112
void
113
panvk_pool_reset(struct panvk_pool *pool)
114
{
115
if (pool->bo_pool) {
116
unsigned num_bos = panvk_pool_num_bos(pool);
117
void *ptr = util_dynarray_grow(&pool->bo_pool->free_bos,
118
struct panfrost_bo *, num_bos);
119
memcpy(ptr, util_dynarray_begin(&pool->bos),
120
num_bos * sizeof(struct panfrost_bo *));
121
} else {
122
util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo)
123
panfrost_bo_unreference(*bo);
124
}
125
126
util_dynarray_clear(&pool->bos);
127
}
128
129
void
130
panvk_pool_cleanup(struct panvk_pool *pool)
131
{
132
panvk_pool_reset(pool);
133
util_dynarray_fini(&pool->bos);
134
}
135
136
void
137
panvk_pool_get_bo_handles(struct panvk_pool *pool, uint32_t *handles)
138
{
139
unsigned idx = 0;
140
util_dynarray_foreach(&pool->bos, struct panfrost_bo *, bo) {
141
assert((*bo)->gem_handle > 0);
142
handles[idx++] = (*bo)->gem_handle;
143
}
144
}
145
146