Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/freedreno/vulkan/tu_cs.c
4565 views
1
/*
2
* Copyright © 2019 Google LLC
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
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
* DEALINGS IN THE SOFTWARE.
22
*/
23
24
#include "tu_cs.h"
25
26
/**
27
* Initialize a command stream.
28
*/
29
void
30
tu_cs_init(struct tu_cs *cs,
31
struct tu_device *device,
32
enum tu_cs_mode mode,
33
uint32_t initial_size)
34
{
35
assert(mode != TU_CS_MODE_EXTERNAL);
36
37
memset(cs, 0, sizeof(*cs));
38
39
cs->device = device;
40
cs->mode = mode;
41
cs->next_bo_size = initial_size;
42
}
43
44
/**
45
* Initialize a command stream as a wrapper to an external buffer.
46
*/
47
void
48
tu_cs_init_external(struct tu_cs *cs, uint32_t *start, uint32_t *end)
49
{
50
memset(cs, 0, sizeof(*cs));
51
52
cs->mode = TU_CS_MODE_EXTERNAL;
53
cs->start = cs->reserved_end = cs->cur = start;
54
cs->end = end;
55
}
56
57
/**
58
* Finish and release all resources owned by a command stream.
59
*/
60
void
61
tu_cs_finish(struct tu_cs *cs)
62
{
63
for (uint32_t i = 0; i < cs->bo_count; ++i) {
64
tu_bo_finish(cs->device, cs->bos[i]);
65
free(cs->bos[i]);
66
}
67
68
free(cs->entries);
69
free(cs->bos);
70
}
71
72
/**
73
* Get the offset of the command packets emitted since the last call to
74
* tu_cs_add_entry.
75
*/
76
static uint32_t
77
tu_cs_get_offset(const struct tu_cs *cs)
78
{
79
assert(cs->bo_count);
80
return cs->start - (uint32_t *) cs->bos[cs->bo_count - 1]->map;
81
}
82
83
/*
84
* Allocate and add a BO to a command stream. Following command packets will
85
* be emitted to the new BO.
86
*/
87
static VkResult
88
tu_cs_add_bo(struct tu_cs *cs, uint32_t size)
89
{
90
/* no BO for TU_CS_MODE_EXTERNAL */
91
assert(cs->mode != TU_CS_MODE_EXTERNAL);
92
93
/* no dangling command packet */
94
assert(tu_cs_is_empty(cs));
95
96
/* grow cs->bos if needed */
97
if (cs->bo_count == cs->bo_capacity) {
98
uint32_t new_capacity = MAX2(4, 2 * cs->bo_capacity);
99
struct tu_bo **new_bos =
100
realloc(cs->bos, new_capacity * sizeof(struct tu_bo *));
101
if (!new_bos)
102
return VK_ERROR_OUT_OF_HOST_MEMORY;
103
104
cs->bo_capacity = new_capacity;
105
cs->bos = new_bos;
106
}
107
108
struct tu_bo *new_bo = malloc(sizeof(struct tu_bo));
109
if (!new_bo)
110
return VK_ERROR_OUT_OF_HOST_MEMORY;
111
112
VkResult result =
113
tu_bo_init_new(cs->device, new_bo, size * sizeof(uint32_t),
114
TU_BO_ALLOC_GPU_READ_ONLY | TU_BO_ALLOC_ALLOW_DUMP);
115
if (result != VK_SUCCESS) {
116
free(new_bo);
117
return result;
118
}
119
120
result = tu_bo_map(cs->device, new_bo);
121
if (result != VK_SUCCESS) {
122
tu_bo_finish(cs->device, new_bo);
123
free(new_bo);
124
return result;
125
}
126
127
cs->bos[cs->bo_count++] = new_bo;
128
129
cs->start = cs->cur = cs->reserved_end = (uint32_t *) new_bo->map;
130
cs->end = cs->start + new_bo->size / sizeof(uint32_t);
131
132
return VK_SUCCESS;
133
}
134
135
/**
136
* Reserve an IB entry.
137
*/
138
static VkResult
139
tu_cs_reserve_entry(struct tu_cs *cs)
140
{
141
/* entries are only for TU_CS_MODE_GROW */
142
assert(cs->mode == TU_CS_MODE_GROW);
143
144
/* grow cs->entries if needed */
145
if (cs->entry_count == cs->entry_capacity) {
146
uint32_t new_capacity = MAX2(4, cs->entry_capacity * 2);
147
struct tu_cs_entry *new_entries =
148
realloc(cs->entries, new_capacity * sizeof(struct tu_cs_entry));
149
if (!new_entries)
150
return VK_ERROR_OUT_OF_HOST_MEMORY;
151
152
cs->entry_capacity = new_capacity;
153
cs->entries = new_entries;
154
}
155
156
return VK_SUCCESS;
157
}
158
159
/**
160
* Add an IB entry for the command packets emitted since the last call to this
161
* function.
162
*/
163
static void
164
tu_cs_add_entry(struct tu_cs *cs)
165
{
166
/* entries are only for TU_CS_MODE_GROW */
167
assert(cs->mode == TU_CS_MODE_GROW);
168
169
/* disallow empty entry */
170
assert(!tu_cs_is_empty(cs));
171
172
/*
173
* because we disallow empty entry, tu_cs_add_bo and tu_cs_reserve_entry
174
* must both have been called
175
*/
176
assert(cs->bo_count);
177
assert(cs->entry_count < cs->entry_capacity);
178
179
/* add an entry for [cs->start, cs->cur] */
180
cs->entries[cs->entry_count++] = (struct tu_cs_entry) {
181
.bo = cs->bos[cs->bo_count - 1],
182
.size = tu_cs_get_size(cs) * sizeof(uint32_t),
183
.offset = tu_cs_get_offset(cs) * sizeof(uint32_t),
184
};
185
186
cs->start = cs->cur;
187
}
188
189
/**
190
* same behavior as tu_cs_emit_call but without the indirect
191
*/
192
VkResult
193
tu_cs_add_entries(struct tu_cs *cs, struct tu_cs *target)
194
{
195
VkResult result;
196
197
assert(cs->mode == TU_CS_MODE_GROW);
198
assert(target->mode == TU_CS_MODE_GROW);
199
200
if (!tu_cs_is_empty(cs))
201
tu_cs_add_entry(cs);
202
203
for (unsigned i = 0; i < target->entry_count; i++) {
204
result = tu_cs_reserve_entry(cs);
205
if (result != VK_SUCCESS)
206
return result;
207
cs->entries[cs->entry_count++] = target->entries[i];
208
}
209
210
return VK_SUCCESS;
211
}
212
213
/**
214
* Begin (or continue) command packet emission. This does nothing but sanity
215
* checks currently. \a cs must not be in TU_CS_MODE_SUB_STREAM mode.
216
*/
217
void
218
tu_cs_begin(struct tu_cs *cs)
219
{
220
assert(cs->mode != TU_CS_MODE_SUB_STREAM);
221
assert(tu_cs_is_empty(cs));
222
}
223
224
/**
225
* End command packet emission. This adds an IB entry when \a cs is in
226
* TU_CS_MODE_GROW mode.
227
*/
228
void
229
tu_cs_end(struct tu_cs *cs)
230
{
231
assert(cs->mode != TU_CS_MODE_SUB_STREAM);
232
233
if (cs->mode == TU_CS_MODE_GROW && !tu_cs_is_empty(cs))
234
tu_cs_add_entry(cs);
235
}
236
237
/**
238
* Begin command packet emission to a sub-stream. \a cs must be in
239
* TU_CS_MODE_SUB_STREAM mode.
240
*
241
* Return \a sub_cs which is in TU_CS_MODE_EXTERNAL mode. tu_cs_begin and
242
* tu_cs_reserve_space are implied and \a sub_cs is ready for command packet
243
* emission.
244
*/
245
VkResult
246
tu_cs_begin_sub_stream(struct tu_cs *cs, uint32_t size, struct tu_cs *sub_cs)
247
{
248
assert(cs->mode == TU_CS_MODE_SUB_STREAM);
249
assert(size);
250
251
VkResult result = tu_cs_reserve_space(cs, size);
252
if (result != VK_SUCCESS)
253
return result;
254
255
tu_cs_init_external(sub_cs, cs->cur, cs->reserved_end);
256
tu_cs_begin(sub_cs);
257
result = tu_cs_reserve_space(sub_cs, size);
258
assert(result == VK_SUCCESS);
259
260
return VK_SUCCESS;
261
}
262
263
/**
264
* Allocate count*size dwords, aligned to size dwords.
265
* \a cs must be in TU_CS_MODE_SUB_STREAM mode.
266
*
267
*/
268
VkResult
269
tu_cs_alloc(struct tu_cs *cs,
270
uint32_t count,
271
uint32_t size,
272
struct tu_cs_memory *memory)
273
{
274
assert(cs->mode == TU_CS_MODE_SUB_STREAM);
275
assert(size && size <= 1024);
276
277
if (!count)
278
return VK_SUCCESS;
279
280
/* TODO: smarter way to deal with alignment? */
281
282
VkResult result = tu_cs_reserve_space(cs, count * size + (size-1));
283
if (result != VK_SUCCESS)
284
return result;
285
286
struct tu_bo *bo = cs->bos[cs->bo_count - 1];
287
size_t offset = align(tu_cs_get_offset(cs), size);
288
289
memory->map = bo->map + offset * sizeof(uint32_t);
290
memory->iova = bo->iova + offset * sizeof(uint32_t);
291
292
cs->start = cs->cur = (uint32_t*) bo->map + offset + count * size;
293
294
return VK_SUCCESS;
295
}
296
297
/**
298
* End command packet emission to a sub-stream. \a sub_cs becomes invalid
299
* after this call.
300
*
301
* Return an IB entry for the sub-stream. The entry has the same lifetime as
302
* \a cs.
303
*/
304
struct tu_cs_entry
305
tu_cs_end_sub_stream(struct tu_cs *cs, struct tu_cs *sub_cs)
306
{
307
assert(cs->mode == TU_CS_MODE_SUB_STREAM);
308
assert(cs->bo_count);
309
assert(sub_cs->start == cs->cur && sub_cs->end == cs->reserved_end);
310
tu_cs_sanity_check(sub_cs);
311
312
tu_cs_end(sub_cs);
313
314
cs->cur = sub_cs->cur;
315
316
struct tu_cs_entry entry = {
317
.bo = cs->bos[cs->bo_count - 1],
318
.size = tu_cs_get_size(cs) * sizeof(uint32_t),
319
.offset = tu_cs_get_offset(cs) * sizeof(uint32_t),
320
};
321
322
cs->start = cs->cur;
323
324
return entry;
325
}
326
327
/**
328
* Reserve space from a command stream for \a reserved_size uint32_t values.
329
* This never fails when \a cs has mode TU_CS_MODE_EXTERNAL.
330
*/
331
VkResult
332
tu_cs_reserve_space(struct tu_cs *cs, uint32_t reserved_size)
333
{
334
if (tu_cs_get_space(cs) < reserved_size) {
335
if (cs->mode == TU_CS_MODE_EXTERNAL) {
336
unreachable("cannot grow external buffer");
337
return VK_ERROR_OUT_OF_HOST_MEMORY;
338
}
339
340
/* add an entry for the exiting command packets */
341
if (!tu_cs_is_empty(cs)) {
342
/* no direct command packet for TU_CS_MODE_SUB_STREAM */
343
assert(cs->mode != TU_CS_MODE_SUB_STREAM);
344
345
tu_cs_add_entry(cs);
346
}
347
348
if (cs->cond_flags) {
349
/* Subtract one here to account for the DWORD field itself. */
350
*cs->cond_dwords = cs->cur - cs->cond_dwords - 1;
351
352
/* space for CP_COND_REG_EXEC in next bo */
353
reserved_size += 3;
354
}
355
356
/* switch to a new BO */
357
uint32_t new_size = MAX2(cs->next_bo_size, reserved_size);
358
VkResult result = tu_cs_add_bo(cs, new_size);
359
if (result != VK_SUCCESS)
360
return result;
361
362
/* if inside a condition, emit a new CP_COND_REG_EXEC */
363
if (cs->cond_flags) {
364
cs->reserved_end = cs->cur + reserved_size;
365
366
tu_cs_emit_pkt7(cs, CP_COND_REG_EXEC, 2);
367
tu_cs_emit(cs, cs->cond_flags);
368
369
cs->cond_dwords = cs->cur;
370
371
/* Emit dummy DWORD field here */
372
tu_cs_emit(cs, CP_COND_REG_EXEC_1_DWORDS(0));
373
}
374
375
/* double the size for the next bo, also there is an upper
376
* bound on IB size, which appears to be 0x0fffff
377
*/
378
new_size = MIN2(new_size << 1, 0x0fffff);
379
if (cs->next_bo_size < new_size)
380
cs->next_bo_size = new_size;
381
}
382
383
assert(tu_cs_get_space(cs) >= reserved_size);
384
cs->reserved_end = cs->cur + reserved_size;
385
386
if (cs->mode == TU_CS_MODE_GROW) {
387
/* reserve an entry for the next call to this function or tu_cs_end */
388
return tu_cs_reserve_entry(cs);
389
}
390
391
return VK_SUCCESS;
392
}
393
394
/**
395
* Reset a command stream to its initial state. This discards all comand
396
* packets in \a cs, but does not necessarily release all resources.
397
*/
398
void
399
tu_cs_reset(struct tu_cs *cs)
400
{
401
if (cs->mode == TU_CS_MODE_EXTERNAL) {
402
assert(!cs->bo_count && !cs->entry_count);
403
cs->reserved_end = cs->cur = cs->start;
404
return;
405
}
406
407
for (uint32_t i = 0; i + 1 < cs->bo_count; ++i) {
408
tu_bo_finish(cs->device, cs->bos[i]);
409
free(cs->bos[i]);
410
}
411
412
if (cs->bo_count) {
413
cs->bos[0] = cs->bos[cs->bo_count - 1];
414
cs->bo_count = 1;
415
416
cs->start = cs->cur = cs->reserved_end = (uint32_t *) cs->bos[0]->map;
417
cs->end = cs->start + cs->bos[0]->size / sizeof(uint32_t);
418
}
419
420
cs->entry_count = 0;
421
}
422
423