Debian Codegraph @

src/panfrost/vulkan/csf/panvk_vX_cmd_buffer.c

raw
1 /*
2 * Copyright © 2021 Collabora Ltd.
3 *
4 * Derived from tu_cmd_buffer.c which is:
5 * Copyright © 2016 Red Hat.
6 * Copyright © 2016 Bas Nieuwenhuizen
7 * Copyright © 2015 Intel Corporation
8 *
9 * SPDX-License-Identifier: MIT
10 */
11
12 #include "drm-uapi/panthor_drm.h"
13
14 #include "genxml/gen_macros.h"
15
16 #include "panvk_buffer.h"
17 #include "panvk_cmd_alloc.h"
18 #include "panvk_cmd_buffer.h"
19 #include "panvk_cmd_desc_state.h"
20 #include "panvk_cmd_pool.h"
21 #include "panvk_cmd_push_constant.h"
22 #include "panvk_cmd_ts.h"
23 #include "panvk_device.h"
24 #include "panvk_entrypoints.h"
25 #include "panvk_instance.h"
26 #include "panvk_instr.h"
27 #include "panvk_meta.h"
28 #include "panvk_physical_device.h"
29 #include "panvk_priv_bo.h"
30 #include "panvk_tracepoints.h"
31 #include "panvk_utrace.h"
32
33 #include "pan_desc.h"
34 #include "pan_encoder.h"
35 #include "pan_props.h"
36 #include "pan_samples.h"
37
38 #include "util/bitscan.h"
39 #include "vk_descriptor_update_template.h"
40 #include "vk_format.h"
41 #include "vk_synchronization.h"
42
43 static void
44 emit_tls(struct panvk_cmd_buffer *cmdbuf)
45 {
46 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
47 struct panvk_physical_device *phys_dev =
48 to_panvk_physical_device(dev->vk.physical);
49 unsigned core_id_range;
50 pan_query_core_count(&phys_dev->kmod.dev->props, &core_id_range);
51
52 if (cmdbuf->state.tls.info.tls.size) {
53 unsigned thread_tls_alloc =
54 pan_query_thread_tls_alloc(&phys_dev->kmod.dev->props);
55 unsigned size = pan_get_total_stack_size(cmdbuf->state.tls.info.tls.size,
56 thread_tls_alloc, core_id_range);
57
58 cmdbuf->state.tls.info.tls.ptr =
59 panvk_cmd_alloc_dev_mem(cmdbuf, tls, size, 4096).gpu;
60 }
61
62 assert(!cmdbuf->state.tls.info.wls.size);
63
64 if (cmdbuf->state.tls.desc.cpu) {
65 GENX(pan_emit_tls)(&cmdbuf->state.tls.info, cmdbuf->state.tls.desc.cpu);
66 }
67 }
68
69 /**
70 * Write all sync point updates to seqno registers and reset the relative sync
71 * points to 0.
72 */
73 static void
74 flush_sync_points(struct panvk_cmd_buffer *cmdbuf)
75 {
76 for (uint32_t i = 0; i < ARRAY_SIZE(cmdbuf->state.cs); i++) {
77 struct cs_builder *b = panvk_get_cs_builder(cmdbuf, i);
78
79 if (!cs_is_valid(b)) {
80 vk_command_buffer_set_error(&cmdbuf->vk,
81 VK_ERROR_OUT_OF_DEVICE_MEMORY);
82 return;
83 }
84
85 cs_update_progress_seqno(b) {
86 for (uint32_t j = 0; j < PANVK_SUBQUEUE_COUNT; j++) {
87 uint32_t rel_sync_point = cmdbuf->state.cs[j].relative_sync_point;
88
89 if (!rel_sync_point)
90 continue;
91
92 cs_add64(b, cs_progress_seqno_reg(b, j), cs_progress_seqno_reg(b, j),
93 rel_sync_point);
94 }
95 }
96 }
97
98 for (uint32_t i = 0; i < ARRAY_SIZE(cmdbuf->state.cs); i++)
99 cmdbuf->state.cs[i].relative_sync_point = 0;
100 }
101
102 static void
103 finish_cs(struct panvk_cmd_buffer *cmdbuf, uint32_t subqueue)
104 {
105 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
106 struct cs_builder *b = panvk_get_cs_builder(cmdbuf, subqueue);
107
108 cs_wait_slots(b, dev->csf.sb.all_mask);
109
110 /* save CS error if non-zero */
111 if (cmdbuf->vk.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
112 struct cs_index sync_addr = cs_scratch_reg64(b, 0);
113 struct cs_index error = cs_scratch_reg32(b, 2);
114
115 cs_load64_to(b, sync_addr, cs_subqueue_ctx_reg(b),
116 offsetof(struct panvk_cs_subqueue_context, syncobjs));
117 cs_load32_to(b, error, sync_addr,
118 sizeof(struct panvk_cs_sync64) * subqueue +
119 offsetof(struct panvk_cs_sync64, error));
120 cs_flush_loads(b);
121
122 cs_if(b, MALI_CS_CONDITION_NEQUAL, error) {
123 cs_store32(b, error, cs_subqueue_ctx_reg(b),
124 offsetof(struct panvk_cs_subqueue_context, last_error));
125 cs_flush_stores(b);
126 }
127 }
128
129 /* We need a clean because descriptor/CS memory can be returned to the
130 * command pool where they get recycled. If we don't clean dirty cache lines,
131 * those cache lines might get evicted asynchronously and their content
132 * pushed back to main memory after the CPU has written new stuff there. */
133 struct cs_index flush_id = cs_scratch_reg32(b, 0);
134
135 panvk_per_arch(panvk_instr_begin_work)(subqueue, cmdbuf,
136 PANVK_INSTR_WORK_TYPE_FLUSH_CACHE);
137 cs_move32_to(b, flush_id, 0);
138 cs_flush_caches(b, MALI_CS_FLUSH_MODE_CLEAN, MALI_CS_FLUSH_MODE_CLEAN,
139 MALI_CS_OTHER_FLUSH_MODE_NONE, flush_id,
140 cs_defer(SB_IMM_MASK, SB_ID(IMM_FLUSH)));
141 cs_wait_slot(b, SB_ID(IMM_FLUSH));
142 struct panvk_instr_end_args instr_info_flush = {
143 .flush_cache = {
144 .l2 = MALI_CS_FLUSH_MODE_CLEAN,
145 .lsc = MALI_CS_FLUSH_MODE_CLEAN,
146 .other = MALI_CS_OTHER_FLUSH_MODE_NONE,
147 }};
148 panvk_per_arch(panvk_instr_end_work)(
149 subqueue, cmdbuf, PANVK_INSTR_WORK_TYPE_FLUSH_CACHE, &instr_info_flush);
150
151 /* If this is a secondary command buffer, we don't poison the reg file to
152 * preserve the render pass context. We also don't poison the reg file if the
153 * last render pass was suspended. In practice we could preserve only the
154 * registers that matter, but this is a debug feature so let's keep things
155 * simple with this all-or-nothing approach. */
156 if (PANVK_DEBUG(CS) &&
157 cmdbuf->vk.level != VK_COMMAND_BUFFER_LEVEL_SECONDARY &&
158 !cmdbuf->state.gfx.render.suspended) {
159 cs_update_cmdbuf_regs(b) {
160 /* Poison all cmdbuf registers to make sure we don't inherit state from
161 * a previously executed cmdbuf. */
162 for (uint32_t i = 0; i <= PANVK_CS_REG_SCRATCH_END; i++)
163 cs_move32_to(b, cs_reg32(b, i), 0xdead | i << 24);
164 }
165 }
166
167 struct panvk_instr_end_args instr_info_cmdbuf = {.cmdbuf = {
168 .flags = cmdbuf->flags,
169 }};
170 panvk_per_arch(panvk_instr_end_work)(
171 subqueue, cmdbuf, PANVK_INSTR_WORK_TYPE_CMDBUF, &instr_info_cmdbuf);
172
173 cs_end(&cmdbuf->state.cs[subqueue].builder);
174 }
175
176 static void
177 finish_queries(struct panvk_cmd_buffer *cmdbuf)
178 {
179 enum panvk_subqueue_id signal_queue = PANVK_QUERY_TS_INFO_SUBQUEUE;
180
181 struct cs_builder *b = panvk_get_cs_builder(cmdbuf, signal_queue);
182 struct cs_index next = cs_scratch_reg64(b, 6);
183 struct cs_index syncobj = cs_scratch_reg64(b, 2);
184 struct cs_index signal_val = cs_scratch_reg32(b, 4);
185
186 cs_load64_to(
187 b, next, cs_subqueue_ctx_reg(b),
188 offsetof(struct panvk_cs_subqueue_context, render.ts_done_chain.head));
189
190 /* If there are queries to signal, wait for other subqueues before
191 * signalling the syncobjs. */
192 struct panvk_cs_deps deps = {0};
193 deps.dst[signal_queue].wait_subqueue_mask =
194 BITFIELD_MASK(PANVK_SUBQUEUE_COUNT) & ~BITFIELD_BIT(signal_queue);
195 deps.dst[signal_queue].conditional = true;
196 deps.dst[signal_queue].cond_value = next;
197 deps.dst[signal_queue].cond = MALI_CS_CONDITION_NEQUAL;
198 /* Wait for DEFERRED_SYNC in addition to LS so that we don't overtake the
199 * deferred SYNC_ADDs added after frag jobs. */
200 u_foreach_bit(i, deps.dst[signal_queue].wait_subqueue_mask)
201 deps.src[i].wait_sb_mask = SB_MASK(LS) | SB_MASK(DEFERRED_SYNC);
202 panvk_per_arch(emit_barrier)(cmdbuf, deps);
203
204 cs_single_link_list_for_each_from(b, next, struct panvk_cs_timestamp_query,
205 node) {
206 cs_load64_to(b, syncobj, next,
207 offsetof(struct panvk_cs_timestamp_query, avail));
208
209 cs_move32_to(b, signal_val, 1);
210 cs_sync32_set(b, true, MALI_CS_SYNC_SCOPE_CSG, signal_val, syncobj,
211 cs_defer(SB_IMM_MASK, SB_ID(DEFERRED_SYNC)));
212 }
213
214 cs_move64_to(b, next, 0);
215 cs_store64(
216 b, next, cs_subqueue_ctx_reg(b),
217 offsetof(struct panvk_cs_subqueue_context, render.ts_done_chain.head));
218 cs_flush_stores(b);
219 }
220
221 VKAPI_ATTR VkResult VKAPI_CALL
222 panvk_per_arch(EndCommandBuffer)(VkCommandBuffer commandBuffer)
223 {
224 VK_FROM_HANDLE(panvk_cmd_buffer, cmdbuf, commandBuffer);
225
226 /* Finishing queries requires a barrier. We don't want to do that more
227 * often than necessary. At the end of a primary is usually enough.
228 * Additionally, simultaneous use secondaries also need to flush if they
229 * contain timestamp query writes to avoid adding the same node more than
230 * once into panvk_cs_subqueue_context::render.ts_chain. */
231 const bool sim_use_sec_with_ts =
232 cmdbuf->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY &&
233 (cmdbuf->flags & VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT) &&
234 cmdbuf->state.contains_timestamp_queries;
235 if (cmdbuf->vk.level == VK_COMMAND_BUFFER_LEVEL_PRIMARY ||
236 unlikely(sim_use_sec_with_ts))
237 finish_queries(cmdbuf);
238
239 emit_tls(cmdbuf);
240 flush_sync_points(cmdbuf);
241
242 for (uint32_t i = 0; i < ARRAY_SIZE(cmdbuf->state.cs); i++) {
243 struct cs_builder *b = &cmdbuf->state.cs[i].builder;
244
245 if (!cs_is_valid(b)) {
246 vk_command_buffer_set_error(&cmdbuf->vk,
247 VK_ERROR_OUT_OF_DEVICE_MEMORY);
248 } else {
249 finish_cs(cmdbuf, i);
250 }
251 }
252
253 panvk_pool_flush_maps(&cmdbuf->cs_pool);
254 panvk_pool_flush_maps(&cmdbuf->desc_pool);
255
256 return vk_command_buffer_end(&cmdbuf->vk);
257 }
258
259 static void
260 add_execution_dependency(uint32_t wait_masks[static PANVK_SUBQUEUE_COUNT],
261 VkPipelineStageFlags2 src_stages,
262 VkPipelineStageFlags2 dst_stages)
263 {
264 /* convert stages to subqueues */
265 uint32_t src_subqueues = 0;
266 uint32_t dst_subqueues = 0;
267 for (uint32_t i = 0; i < PANVK_SUBQUEUE_COUNT; i++) {
268 const VkPipelineStageFlags2 subqueue_stages = panvk_get_subqueue_stages(i);
269 if (src_stages & subqueue_stages)
270 src_subqueues |= BITFIELD_BIT(i);
271 if (dst_stages & subqueue_stages)
272 dst_subqueues |= BITFIELD_BIT(i);
273 }
274
275 const bool dst_host = dst_stages & VK_PIPELINE_STAGE_2_HOST_BIT;
276
277 /* nothing to wait */
278 if (!src_subqueues || (!dst_subqueues && !dst_host))
279 return;
280
281 u_foreach_bit(i, dst_subqueues) {
282 /* each dst subqueue should wait for all src subqueues */
283 uint32_t wait_mask = src_subqueues;
284
285 switch (i) {
286 case PANVK_SUBQUEUE_VERTEX_TILER:
287 /* Indirect draw buffers are read from the command stream, and
288 * load/store operations are synchronized with the LS scoreboard
289 * immediately after the read, so no need to wait in that case.
290 */
291 if ((src_stages & panvk_get_subqueue_stages(i)) ==
292 VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT)
293 wait_mask &= ~BITFIELD_BIT(i);
294 break;
295 case PANVK_SUBQUEUE_FRAGMENT:
296 /* The fragment subqueue always waits for the tiler subqueue already.
297 * Explicit waits can be skipped.
298 */
299 wait_mask &= ~BITFIELD_BIT(PANVK_SUBQUEUE_VERTEX_TILER);
300 break;
301 default:
302 break;
303 }
304
305 wait_masks[i] |= wait_mask;
306 }
307
308 /* The host does not wait for src subqueues. All src subqueues should
309 * self-wait instead.
310 *
311 * Also, our callers currently expect src subqueues to self-wait when there
312 * are dst subqueues. Until that changes, make all src subqueues self-wait.
313 */
314 if (dst_host || dst_subqueues) {
315 u_foreach_bit(i, src_subqueues)
316 wait_masks[i] |= BITFIELD_BIT(i);
317 }
318 }
319
320 static void
321 add_memory_dependency(struct panvk_cache_flush_info *cache_flush,
322 VkAccessFlags2 src_access, VkAccessFlags2 dst_access)
323 {
324 /* Note on the cache organization:
325 *
326 * - L2 cache is unified, so all changes to this cache are automatically
327 * visible to all GPU sub-components (shader cores, tiler, ...). This
328 * means we only need to flush when the host (AKA CPU) is involved.
329 * - LS caches (which are basically just read-write L1 caches) are coherent
330 * with each other and with the L2 cache, so again, we only need to flush
331 * when the host is involved.
332 * - Other read-only L1 caches (like the ones in front of the texture unit)
333 * are not coherent with the LS or L2 caches, and thus need to be
334 * invalidated any time a write happens.
335 *
336 * Translating to the Vulkan memory model:
337 *
338 * - The device domain is the L2 cache.
339 * - An availability operation from device writes to the device domain is
340 * nop.
341 * - A visibility operation from the device domain to device accesses that
342 * are coherent with L2/LS is nop.
343 * - A visibility operation from the device domain to device accesses that
344 * are incoherent with L2/LS invalidates the other RO L1 caches.
345 * - A host-to-device domain operation invalidates all caches.
346 * - A device-to-host domain operation flushes L2/LS.
347 */
348 const VkAccessFlags2 ro_l1_access =
349 VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT |
350 VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
351 VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_SHADER_SAMPLED_READ_BIT |
352 VK_ACCESS_2_INPUT_ATTACHMENT_READ_BIT;
353
354 /* visibility op */
355 if (dst_access & ro_l1_access)
356 cache_flush->others |= MALI_CS_OTHER_FLUSH_MODE_INVALIDATE;
357
358 /* host-to-device domain op */
359 if (src_access & VK_ACCESS_2_HOST_WRITE_BIT) {
360 cache_flush->l2 |= MALI_CS_FLUSH_MODE_CLEAN_AND_INVALIDATE;
361 cache_flush->lsc |= MALI_CS_FLUSH_MODE_CLEAN_AND_INVALIDATE;
362 cache_flush->others |= MALI_CS_OTHER_FLUSH_MODE_INVALIDATE;
363 }
364
365 /* device-to-host domain op */
366 if (dst_access & (VK_ACCESS_2_HOST_READ_BIT | VK_ACCESS_2_HOST_WRITE_BIT)) {
367 cache_flush->l2 |= MALI_CS_FLUSH_MODE_CLEAN;
368 cache_flush->lsc |= MALI_CS_FLUSH_MODE_CLEAN;
369 }
370 }
371
372 static void
373 collect_cache_flush_info(enum panvk_subqueue_id subqueue,
374 struct panvk_cache_flush_info *cache_flush,
375 VkAccessFlags2 src_access, VkAccessFlags2 dst_access)
376 {
377 /* limit access to the subqueue and host */
378 const VkPipelineStageFlags2 subqueue_stages =
379 panvk_get_subqueue_stages(subqueue) | VK_PIPELINE_STAGE_2_HOST_BIT;
380 src_access = vk_filter_src_access_flags2(subqueue_stages, src_access);
381 dst_access = vk_filter_dst_access_flags2(subqueue_stages, dst_access);
382
383 add_memory_dependency(cache_flush, src_access, dst_access);
384 }
385
386 static void
387 collect_cs_deps(struct panvk_cmd_buffer *cmdbuf, const VkDependencyInfo *info,
388 struct panvk_sync_scope src, struct panvk_sync_scope dst,
389 struct panvk_cs_deps *deps)
390 {
391 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
392 uint32_t wait_masks[PANVK_SUBQUEUE_COUNT] = {0};
393 add_execution_dependency(wait_masks, src.stages, dst.stages);
394
395 if (cmdbuf->state.gfx.render.tiler || inherits_render_ctx(cmdbuf)) {
396 if (info->dependencyFlags & VK_DEPENDENCY_BY_REGION_BIT) {
397 /* Instead of doing an actual fragment job dependency, use a FB
398 * barrier instead.
399 */
400 deps->needs_fb_barrier = true;
401 wait_masks[PANVK_SUBQUEUE_FRAGMENT] &=
402 ~BITFIELD_BIT(PANVK_SUBQUEUE_FRAGMENT);
403 }
404
405 /* From the Vulkan 1.4.335 spec:
406 *
407 * VUID-vkCmdPipelineBarrier-dependencyFlags-07891
408 *
409 * "If vkCmdPipelineBarrier is called within a render pass
410 * instance, and the source stage masks of any memory barriers
411 * include framebuffer-space stages, then dependencyFlags must
412 * include VK_DEPENDENCY_BY_REGION_BIT"
413 */
414 for (uint32_t i = 0; i < PANVK_SUBQUEUE_COUNT; i++)
415 assert(!(wait_masks[i] & BITFIELD_BIT(PANVK_SUBQUEUE_FRAGMENT)));
416
417 /* skip the tiler subqueue self-wait because we use the same
418 * scoreboard slot for the idvs jobs
419 */
420 wait_masks[PANVK_SUBQUEUE_VERTEX_TILER] &=
421 ~BITFIELD_BIT(PANVK_SUBQUEUE_VERTEX_TILER);
422 }
423
424 for (uint32_t i = 0; i < PANVK_SUBQUEUE_COUNT; i++) {
425 if (wait_masks[i] & BITFIELD_BIT(i)) {
426 /* We need to self-wait for all previously submitted jobs, and given
427 * the iterator scoreboard is a moving target, we just wait for the
428 * whole dynamic scoreboard range.
429 */
430 deps->src[i].wait_sb_mask |= dev->csf.sb.all_iters_mask;
431 }
432
433 collect_cache_flush_info(i, &deps->src[i].cache_flush, src.access,
434 dst.access);
435
436 deps->dst[i].wait_subqueue_mask |= wait_masks[i];
437 }
438 }
439
440 static void
441 normalize_dependency(struct panvk_sync_scope *src,
442 struct panvk_sync_scope *dst,
443 struct panvk_sync_scope transition,
444 uint32_t src_qfi, uint32_t dst_qfi,
445 enum panvk_barrier_stage barrier_stage)
446 {
447 switch (barrier_stage) {
448 case PANVK_BARRIER_STAGE_FIRST:
449 if (transition.stages) {
450 /* We need to do layout transition, so we want to sync src with layout
451 * transition, and then later layout transition with dst.
452 */
453 *dst = transition;
454 }
455 break;
456 case PANVK_BARRIER_STAGE_AFTER_LAYOUT_TRANSITION:
457 /* If transition.stages is empty, there was no layout transition and so we
458 * won't be waiting for anything.
459 */
460 *src = transition;
461 break;
462 }
463
464 /* Perform queue family ownership transfer if src and dst are unequal. */
465 if (src_qfi != dst_qfi) {
466 /* Only normalize if we're actually syncing acquire, and not layout
467 * transition, with dst.
468 */
469 if (barrier_stage == PANVK_BARRIER_STAGE_FIRST) {
470 /* queue family acquire operation */
471 switch (src_qfi) {
472 case VK_QUEUE_FAMILY_EXTERNAL:
473 /* no execution dependency and no availability operation */
474 *src = (struct panvk_sync_scope){VK_PIPELINE_STAGE_2_NONE, VK_ACCESS_2_NONE};
475 break;
476 case VK_QUEUE_FAMILY_FOREIGN_EXT:
477 /* treat the foreign queue as the host */
478 *src = (struct panvk_sync_scope){VK_PIPELINE_STAGE_2_HOST_BIT, VK_ACCESS_2_HOST_WRITE_BIT};
479 break;
480 default:
481 break;
482 }
483 }
484
485 /* Only normalize if we're actually syncing the latest of either src or
486 * layout transition, with release.
487 */
488 if ((barrier_stage == PANVK_BARRIER_STAGE_FIRST && !transition.stages) ||
489 (barrier_stage == PANVK_BARRIER_STAGE_AFTER_LAYOUT_TRANSITION && transition.stages)) {
490 /* queue family release operation */
491 switch (dst_qfi) {
492 case VK_QUEUE_FAMILY_EXTERNAL:
493 /* no execution dependency and no visibility operation */
494 *dst = (struct panvk_sync_scope){VK_PIPELINE_STAGE_2_NONE, VK_ACCESS_2_NONE};
495 break;
496 case VK_QUEUE_FAMILY_FOREIGN_EXT:
497 /* treat the foreign queue as the host */
498 *dst = (struct panvk_sync_scope){VK_PIPELINE_STAGE_2_HOST_BIT, VK_ACCESS_2_HOST_WRITE_BIT};
499 break;
500 default:
501 break;
502 }
503 }
504 }
505
506 src->stages = vk_expand_src_stage_flags2(src->stages);
507 src->access = vk_filter_src_access_flags2(src->stages, src->access);
508 dst->stages = vk_expand_dst_stage_flags2(dst->stages);
509 dst->access = vk_filter_dst_access_flags2(dst->stages, dst->access);
510 }
511
512 void
513 panvk_per_arch(add_cs_deps)(struct panvk_cmd_buffer *cmdbuf,
514 enum panvk_barrier_stage barrier_stage,
515 const VkDependencyInfo *in,
516 struct panvk_cs_deps *out,
517 bool is_set_event)
518 {
519 bool is_asymmetric_event =
520 is_set_event &&
521 in->dependencyFlags & VK_DEPENDENCY_ASYMMETRIC_EVENT_BIT_KHR;
522
523 /* As per Vulkan spec, this should look like a vkCmdSetEvent */
524 assert(!is_asymmetric_event ||
525 (in->memoryBarrierCount == 1 && in->bufferMemoryBarrierCount == 0 &&
526 in->imageMemoryBarrierCount == 0));
527
528 for (uint32_t i = 0; i < in->memoryBarrierCount; i++) {
529 const VkMemoryBarrier2 *barrier = &in->pMemoryBarriers[i];
530 struct panvk_sync_scope src = {barrier->srcStageMask, barrier->srcAccessMask};
531 struct panvk_sync_scope dst = {barrier->dstStageMask, barrier->dstAccessMask};
532
533 /* In case of asymmetric event, we need to use the src stages mask just
534 * like vkCmdSetEvent */
535 if (is_asymmetric_event)
536 dst.stages = src.stages;
537
538 normalize_dependency(&src, &dst, (struct panvk_sync_scope){0},
539 VK_QUEUE_FAMILY_IGNORED,
540 VK_QUEUE_FAMILY_IGNORED,
541 barrier_stage);
542
543 collect_cs_deps(cmdbuf, in, src, dst, out);
544 }
545
546 for (uint32_t i = 0; i < in->bufferMemoryBarrierCount; i++) {
547 const VkBufferMemoryBarrier2 *barrier = &in->pBufferMemoryBarriers[i];
548 struct panvk_sync_scope src = {barrier->srcStageMask, barrier->srcAccessMask};
549 struct panvk_sync_scope dst = {barrier->dstStageMask, barrier->dstAccessMask};
550 normalize_dependency(&src, &dst, (struct panvk_sync_scope){0},
551 barrier->srcQueueFamilyIndex,
552 barrier->dstQueueFamilyIndex,
553 barrier_stage);
554
555 collect_cs_deps(cmdbuf, in, src, dst, out);
556 }
557
558 for (uint32_t i = 0; i < in->imageMemoryBarrierCount; i++) {
559 const VkImageMemoryBarrier2 *barrier = &in->pImageMemoryBarriers[i];
560 struct panvk_sync_scope src = {barrier->srcStageMask, barrier->srcAccessMask};
561 struct panvk_sync_scope dst = {barrier->dstStageMask, barrier->dstAccessMask};
562 struct panvk_sync_scope transition;
563 panvk_per_arch(transition_image_layout_sync_scope)(barrier,
564 &transition.stages, &transition.access);
565 normalize_dependency(&src, &dst, transition,
566 barrier->srcQueueFamilyIndex,
567 barrier->dstQueueFamilyIndex,
568 barrier_stage);
569
570 collect_cs_deps(cmdbuf, in, src, dst, out);
571
572 if (barrier_stage == PANVK_BARRIER_STAGE_FIRST && transition.stages)
573 out->needs_layout_transitions = true;
574 }
575 }
576
577 static void
578 emit_barrier_insert_waits(struct cs_builder *b, struct panvk_cmd_buffer *cmdbuf,
579 struct panvk_cs_deps *deps, enum panvk_subqueue_id i,
580 struct cs_index tmp_regs)
581 {
582 assert(tmp_regs.size == 4);
583 u_foreach_bit(j, deps->dst[i].wait_subqueue_mask) {
584 struct panvk_cs_state *cs_state = &cmdbuf->state.cs[j];
585 struct cs_index sync_addr = cs_reg64(b, tmp_regs.reg);
586 struct cs_index wait_val = cs_reg64(b, tmp_regs.reg + 2);
587
588 cs_load64_to(b, sync_addr, cs_subqueue_ctx_reg(b),
589 offsetof(struct panvk_cs_subqueue_context, syncobjs));
590 cs_add64(b, sync_addr, sync_addr, sizeof(struct panvk_cs_sync64) * j);
591
592 cs_add64(b, wait_val, cs_progress_seqno_reg(b, j),
593 cs_state->relative_sync_point);
594
595 panvk_instr_sync64_wait(cmdbuf, i, false, MALI_CS_CONDITION_GREATER,
596 wait_val, sync_addr);
597 }
598 }
599
600 void
601 panvk_per_arch(emit_barrier)(struct panvk_cmd_buffer *cmdbuf,
602 struct panvk_cs_deps deps)
603 {
604 uint32_t wait_subqueue_mask = 0;
605 uint32_t utrace_subqueue_mask = 0;
606 for (uint32_t i = 0; i < PANVK_SUBQUEUE_COUNT; i++) {
607 /* no need to perform both types of waits on the same subqueue */
608 if (deps.src[i].wait_sb_mask)
609 deps.dst[i].wait_subqueue_mask &= ~BITFIELD_BIT(i);
610 assert(!(deps.dst[i].wait_subqueue_mask & BITFIELD_BIT(i)));
611
612 wait_subqueue_mask |= deps.dst[i].wait_subqueue_mask;
613
614 if (deps.src[i].wait_sb_mask || deps.dst[i].wait_subqueue_mask ||
615 !panvk_cache_flush_is_nop(&deps.src[i].cache_flush))
616 utrace_subqueue_mask |= BITFIELD_BIT(i);
617 }
618
619 u_foreach_bit(i, utrace_subqueue_mask)
620 panvk_per_arch(panvk_instr_begin_work)(i, cmdbuf,
621 PANVK_INSTR_WORK_TYPE_BARRIER);
622
623 for (uint32_t i = 0; i < PANVK_SUBQUEUE_COUNT; i++) {
624 struct cs_builder *b = panvk_get_cs_builder(cmdbuf, i);
625 struct panvk_cs_state *cs_state = &cmdbuf->state.cs[i];
626
627 if (deps.src[i].wait_sb_mask)
628 cs_wait_slots(b, deps.src[i].wait_sb_mask);
629
630 struct panvk_cache_flush_info cache_flush = deps.src[i].cache_flush;
631 if (!panvk_cache_flush_is_nop(&cache_flush)) {
632 struct cs_index flush_id = cs_scratch_reg32(b, 0);
633
634 panvk_per_arch(panvk_instr_begin_work)(
635 i, cmdbuf, PANVK_INSTR_WORK_TYPE_FLUSH_CACHE);
636 cs_move32_to(b, flush_id, 0);
637 cs_flush_caches(b, cache_flush.l2, cache_flush.lsc, cache_flush.others,
638 flush_id, cs_defer(SB_IMM_MASK, SB_ID(IMM_FLUSH)));
639 cs_wait_slot(b, SB_ID(IMM_FLUSH));
640 struct panvk_instr_end_args instr_info_flush = {
641 .flush_cache = {
642 .l2 = cache_flush.l2,
643 .lsc = cache_flush.lsc,
644 .other = cache_flush.others,
645 }};
646 panvk_per_arch(panvk_instr_end_work)(
647 i, cmdbuf, PANVK_INSTR_WORK_TYPE_FLUSH_CACHE, &instr_info_flush);
648 }
649
650 /* If no one waits on us, there's no point signaling the sync object. */
651 if (wait_subqueue_mask & BITFIELD_BIT(i)) {
652 struct cs_index sync_addr = cs_scratch_reg64(b, 0);
653 struct cs_index add_val = cs_scratch_reg64(b, 2);
654
655 assert(deps.src[i].wait_sb_mask);
656
657 cs_load64_to(b, sync_addr, cs_subqueue_ctx_reg(b),
658 offsetof(struct panvk_cs_subqueue_context, syncobjs));
659 cs_add64(b, sync_addr, sync_addr, sizeof(struct panvk_cs_sync64) * i);
660 cs_move64_to(b, add_val, 1);
661 panvk_instr_sync64_add(cmdbuf, i, true, MALI_CS_SYNC_SCOPE_CSG,
662 add_val, sync_addr, cs_now());
663 ++cs_state->relative_sync_point;
664 }
665 }
666
667 for (uint32_t i = 0; i < PANVK_SUBQUEUE_COUNT; i++) {
668 struct cs_builder *b = panvk_get_cs_builder(cmdbuf, i);
669 struct cs_index tmp_regs = cs_scratch_reg_tuple(b, 0, 4);
670
671 if (deps.dst[i].conditional) {
672 assert(deps.dst[i].cond_value.reg >= tmp_regs.reg + tmp_regs.size ||
673 deps.dst[i].cond_value.reg + deps.dst[i].cond_value.size <=
674 tmp_regs.reg);
675 cs_if(b, deps.dst[i].cond, deps.dst[i].cond_value)
676 emit_barrier_insert_waits(b, cmdbuf, &deps, i, tmp_regs);
677 } else {
678 emit_barrier_insert_waits(b, cmdbuf, &deps, i, tmp_regs);
679 }
680 }
681
682 u_foreach_bit(i, utrace_subqueue_mask) {
683 struct panvk_instr_end_args info = {
684 .barrier = {
685 .wait_sb_mask = deps.src[i].wait_sb_mask,
686 .wait_subqueue_mask = deps.dst[i].wait_subqueue_mask,
687 }};
688 panvk_per_arch(panvk_instr_end_work)(
689 i, cmdbuf, PANVK_INSTR_WORK_TYPE_BARRIER, &info);
690 }
691 }
692
693 VKAPI_ATTR void VKAPI_CALL
694 panvk_per_arch(CmdPipelineBarrier2)(VkCommandBuffer commandBuffer,
695 const VkDependencyInfo *pDependencyInfo)
696 {
697 VK_FROM_HANDLE(panvk_cmd_buffer, cmdbuf, commandBuffer);
698 struct panvk_cs_deps deps = {0};
699
700 panvk_per_arch(add_cs_deps)(cmdbuf, PANVK_BARRIER_STAGE_FIRST, pDependencyInfo, &deps, false);
701
702 if (deps.needs_fb_barrier)
703 panvk_per_arch(cmd_fb_barrier)(cmdbuf);
704
705 panvk_per_arch(emit_barrier)(cmdbuf, deps);
706
707 if (deps.needs_layout_transitions) {
708 for (uint32_t i = 0; i < pDependencyInfo->imageMemoryBarrierCount; i++) {
709 const VkImageMemoryBarrier2 *barrier = &pDependencyInfo->pImageMemoryBarriers[i];
710
711 panvk_per_arch(cmd_transition_image_layout)(commandBuffer, barrier);
712 }
713
714 struct panvk_cs_deps trans_deps = {0};
715
716 panvk_per_arch(add_cs_deps)(
717 cmdbuf, PANVK_BARRIER_STAGE_AFTER_LAYOUT_TRANSITION,
718 pDependencyInfo, &trans_deps, false);
719
720 assert(!trans_deps.needs_fb_barrier);
721
722 panvk_per_arch(emit_barrier)(cmdbuf, trans_deps);
723 }
724 }
725
726 static struct cs_buffer
727 alloc_cs_buffer(void *cookie)
728 {
729 struct panvk_cmd_buffer *cmdbuf = cookie;
730 const unsigned capacity = 64 * 1024 / sizeof(uint64_t);
731
732 struct pan_ptr ptr = panvk_cmd_alloc_dev_mem(cmdbuf, cs, capacity * 8, 64);
733
734 return (struct cs_buffer){
735 .cpu = ptr.cpu,
736 .gpu = ptr.gpu,
737 .capacity = capacity,
738 };
739 }
740
741 static enum cs_reg_perm
742 cs_reg_perm(struct cs_builder *b, unsigned reg)
743 {
744 struct panvk_cs_state *cs_state =
745 container_of(b, struct panvk_cs_state, builder);
746 struct panvk_cs_reg_upd_context *upd_ctx;
747
748 for (upd_ctx = cs_state->reg_access.upd_ctx_stack; upd_ctx;
749 upd_ctx = upd_ctx->next) {
750 if (upd_ctx->reg_perm(b, reg) == CS_REG_RW)
751 return CS_REG_RW;
752 }
753
754 return cs_state->reg_access.base_perm(b, reg);
755 }
756
757 static void
758 init_cs_builders(struct panvk_cmd_buffer *cmdbuf)
759 {
760 struct panvk_physical_device *phys_dev =
761 to_panvk_physical_device(cmdbuf->vk.base.device->physical);
762 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
763 const reg_perm_cb_t base_reg_perms[PANVK_SUBQUEUE_COUNT] = {
764 [PANVK_SUBQUEUE_VERTEX_TILER] = panvk_cs_vt_reg_perm,
765 [PANVK_SUBQUEUE_FRAGMENT] = panvk_cs_frag_reg_perm,
766 [PANVK_SUBQUEUE_COMPUTE] = panvk_cs_compute_reg_perm,
767 };
768
769 const struct drm_panthor_csif_info *csif_info =
770 panthor_kmod_get_csif_props(dev->kmod.dev);
771
772 for (uint32_t i = 0; i < ARRAY_SIZE(cmdbuf->state.cs); i++) {
773 struct cs_builder *b = &cmdbuf->state.cs[i].builder;
774 /* Lazy allocation of the root CS. */
775 struct cs_buffer root_cs = {0};
776
777 struct cs_builder_conf conf = {
778 .nr_registers = csif_info->cs_reg_count,
779 .nr_kernel_registers = MAX2(csif_info->unpreserved_cs_reg_count, 4),
780 .compute_ep_limit =
781 PAN_ARCH >= 12 ? phys_dev->kmod.dev->props.max_tasks_per_core : 0,
782 .alloc_buffer = alloc_cs_buffer,
783 .cookie = cmdbuf,
784 .ls_sb_slot = SB_ID(LS),
785 };
786
787 if (PANVK_DEBUG(CS)) {
788 cmdbuf->state.cs[i].reg_access.upd_ctx_stack = NULL;
789 cmdbuf->state.cs[i].reg_access.base_perm = base_reg_perms[i];
790 conf.reg_perm = cs_reg_perm;
791 }
792
793 cs_builder_init(b, &conf, root_cs);
794
795 if (PANVK_DEBUG(TRACE)) {
796 cmdbuf->state.cs[i].tracing = (struct cs_tracing_ctx){
797 .enabled = true,
798 .ctx_reg = cs_subqueue_ctx_reg(b),
799 .tracebuf_addr_offset =
800 offsetof(struct panvk_cs_subqueue_context, debug.tracebuf.cs),
801 };
802 }
803 }
804 }
805
806 static void
807 panvk_reset_cmdbuf(struct vk_command_buffer *vk_cmdbuf,
808 VkCommandBufferResetFlags flags)
809 {
810 struct panvk_cmd_buffer *cmdbuf =
811 container_of(vk_cmdbuf, struct panvk_cmd_buffer, vk);
812 struct panvk_cmd_pool *pool =
813 container_of(vk_cmdbuf->pool, struct panvk_cmd_pool, vk);
814 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
815
816 vk_command_buffer_reset(&cmdbuf->vk);
817
818 panvk_pool_reset(&cmdbuf->cs_pool);
819 panvk_pool_reset(&cmdbuf->desc_pool);
820 panvk_pool_reset(&cmdbuf->tls_pool);
821 list_splicetail(&cmdbuf->push_sets, &pool->push_sets);
822 list_inithead(&cmdbuf->push_sets);
823
824 for (uint32_t i = 0; i < ARRAY_SIZE(cmdbuf->utrace.uts); i++) {
825 struct u_trace *ut = &cmdbuf->utrace.uts[i];
826 u_trace_fini(ut);
827 u_trace_init(ut, &dev->utrace.utctx);
828 }
829
830 for (uint32_t i = 0; i < ARRAY_SIZE(cmdbuf->state.cs); i++)
831 cs_builder_fini(&cmdbuf->state.cs[i].builder);
832
833 memset(&cmdbuf->state, 0, sizeof(cmdbuf->state));
834 init_cs_builders(cmdbuf);
835 }
836
837 static void
838 panvk_destroy_cmdbuf(struct vk_command_buffer *vk_cmdbuf)
839 {
840 struct panvk_cmd_buffer *cmdbuf =
841 container_of(vk_cmdbuf, struct panvk_cmd_buffer, vk);
842 struct panvk_cmd_pool *pool =
843 container_of(vk_cmdbuf->pool, struct panvk_cmd_pool, vk);
844 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
845
846 for (uint32_t i = 0; i < ARRAY_SIZE(cmdbuf->utrace.uts); i++)
847 u_trace_fini(&cmdbuf->utrace.uts[i]);
848
849 for (uint32_t i = 0; i < ARRAY_SIZE(cmdbuf->state.cs); i++)
850 cs_builder_fini(&cmdbuf->state.cs[i].builder);
851
852 panvk_pool_cleanup(&cmdbuf->cs_pool);
853 panvk_pool_cleanup(&cmdbuf->desc_pool);
854 panvk_pool_cleanup(&cmdbuf->tls_pool);
855 list_splicetail(&cmdbuf->push_sets, &pool->push_sets);
856 vk_command_buffer_finish(&cmdbuf->vk);
857 vk_free(&dev->vk.alloc, cmdbuf);
858 }
859
860 static VkResult
861 panvk_create_cmdbuf(struct vk_command_pool *vk_pool, VkCommandBufferLevel level,
862 struct vk_command_buffer **cmdbuf_out)
863 {
864 struct panvk_device *device =
865 container_of(vk_pool->base.device, struct panvk_device, vk);
866 struct panvk_cmd_pool *pool =
867 container_of(vk_pool, struct panvk_cmd_pool, vk);
868 struct panvk_cmd_buffer *cmdbuf;
869
870 cmdbuf = vk_zalloc(&device->vk.alloc, sizeof(*cmdbuf), 8,
871 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
872 if (!cmdbuf)
873 return panvk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
874
875 VkResult result = vk_command_buffer_init(
876 &pool->vk, &cmdbuf->vk, &panvk_per_arch(cmd_buffer_ops), level);
877 if (result != VK_SUCCESS) {
878 vk_free(&device->vk.alloc, cmdbuf);
879 return result;
880 }
881
882 list_inithead(&cmdbuf->push_sets);
883 cmdbuf->vk.dynamic_graphics_state.vi = &cmdbuf->state.gfx.dynamic.vi;
884 cmdbuf->vk.dynamic_graphics_state.ms.sample_locations =
885 &cmdbuf->state.gfx.dynamic.sl;
886
887 struct panvk_pool_properties cs_pool_props = {
888 .create_flags =
889 panvk_device_adjust_bo_flags(device, PAN_KMOD_BO_FLAG_WB_MMAP),
890 .slab_size = 64 * 1024,
891 .label = "Command buffer CS pool",
892 .prealloc = false,
893 .owns_bos = true,
894 .needs_locking = false,
895 };
896 panvk_pool_init(&cmdbuf->cs_pool, device, &pool->cs_bo_pool, NULL, &cs_pool_props);
897
898 struct panvk_pool_properties desc_pool_props = {
899 .create_flags =
900 panvk_device_adjust_bo_flags(device, PAN_KMOD_BO_FLAG_WB_MMAP),
901 .slab_size = 64 * 1024,
902 .label = "Command buffer descriptor pool",
903 .prealloc = false,
904 .owns_bos = true,
905 .needs_locking = false,
906 };
907 panvk_pool_init(&cmdbuf->desc_pool, device, &pool->desc_bo_pool, NULL,
908 &desc_pool_props);
909
910 struct panvk_pool_properties tls_pool_props = {
911 .create_flags =
912 panvk_device_adjust_bo_flags(device, PAN_KMOD_BO_FLAG_NO_MMAP),
913 .slab_size = 64 * 1024,
914 .label = "TLS pool",
915 .prealloc = false,
916 .owns_bos = true,
917 .needs_locking = false,
918 };
919 panvk_pool_init(&cmdbuf->tls_pool, device, &pool->tls_bo_pool, &pool->tls_big_bo_pool,
920 &tls_pool_props);
921
922 for (uint32_t i = 0; i < ARRAY_SIZE(cmdbuf->utrace.uts); i++)
923 u_trace_init(&cmdbuf->utrace.uts[i], &device->utrace.utctx);
924
925 init_cs_builders(cmdbuf);
926 *cmdbuf_out = &cmdbuf->vk;
927 return VK_SUCCESS;
928 }
929
930 const struct vk_command_buffer_ops panvk_per_arch(cmd_buffer_ops) = {
931 .create = panvk_create_cmdbuf,
932 .reset = panvk_reset_cmdbuf,
933 .destroy = panvk_destroy_cmdbuf,
934 };
935
936 VKAPI_ATTR VkResult VKAPI_CALL
937 panvk_per_arch(BeginCommandBuffer)(VkCommandBuffer commandBuffer,
938 const VkCommandBufferBeginInfo *pBeginInfo)
939 {
940 VK_FROM_HANDLE(panvk_cmd_buffer, cmdbuf, commandBuffer);
941
942 vk_command_buffer_begin(&cmdbuf->vk, pBeginInfo);
943 cmdbuf->flags = pBeginInfo->flags;
944
945 if (PANVK_DEBUG(FORCE_SIMULTANEOUS)) {
946 cmdbuf->flags |= VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT;
947 cmdbuf->flags &= ~VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
948 }
949
950 panvk_per_arch(cmd_inherit_render_state)(cmdbuf, pBeginInfo);
951
952 if (cmdbuf->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
953 const VkCommandBufferInheritanceConditionalRenderingInfoEXT *cond_info =
954 vk_find_struct_const(
955 pBeginInfo->pInheritanceInfo->pNext,
956 COMMAND_BUFFER_INHERITANCE_CONDITIONAL_RENDERING_INFO_EXT);
957
958 if (cond_info && cond_info->conditionalRenderingEnable)
959 cmdbuf->state.cond_render.inherited = true;
960 }
961
962 for (uint32_t i = 0; i < PANVK_SUBQUEUE_COUNT; i++)
963 panvk_per_arch(panvk_instr_begin_work)(i, cmdbuf,
964 PANVK_INSTR_WORK_TYPE_CMDBUF);
965
966 return VK_SUCCESS;
967 }
968
969 static void
970 panvk_cmd_invalidate_state(struct panvk_cmd_buffer *cmdbuf)
971 {
972 /* From the Vulkan 1.3.275 spec:
973 *
974 * "...There is one exception to this rule - if the primary command
975 * buffer is inside a render pass instance, then the render pass and
976 * subpass state is not disturbed by executing secondary command
977 * buffers."
978 *
979 * We need to reset everything EXCEPT the render pass state.
980 */
981 struct panvk_rendering_state render_save = cmdbuf->state.gfx.render;
982 memset(&cmdbuf->state.gfx, 0, sizeof(cmdbuf->state.gfx));
983 cmdbuf->state.gfx.render = render_save;
984
985 vk_dynamic_graphics_state_dirty_all(&cmdbuf->vk.dynamic_graphics_state);
986 gfx_state_set_all_dirty(cmdbuf);
987 }
988
989 VKAPI_ATTR void VKAPI_CALL
990 panvk_per_arch(CmdExecuteCommands)(VkCommandBuffer commandBuffer,
991 uint32_t commandBufferCount,
992 const VkCommandBuffer *pCommandBuffers)
993 {
994 VK_FROM_HANDLE(panvk_cmd_buffer, primary, commandBuffer);
995
996 if (commandBufferCount == 0)
997 return;
998
999 /* Write out any pending seqno changes to registers before calling
1000 * secondary command buffers. */
1001 flush_sync_points(primary);
1002
1003 for (uint32_t i = 0; i < commandBufferCount; i++) {
1004 VK_FROM_HANDLE(panvk_cmd_buffer, secondary, pCommandBuffers[i]);
1005
1006 /* make sure the CS context is setup properly
1007 * to inherit the primary command buffer state
1008 */
1009 primary->state.tls.info.tls.size =
1010 MAX2(primary->state.tls.info.tls.size,
1011 secondary->state.tls.info.tls.size);
1012 panvk_per_arch(cmd_prepare_exec_cmd_for_draws)(primary, secondary);
1013
1014 /* Write the conditional rendering flag into the subqueue context
1015 * for inherited secondaries. The secondary's draws load from this
1016 * field and skip when it is zero.
1017 */
1018 if (secondary->state.cond_render.inherited) {
1019 for (uint32_t j = 0; j < ARRAY_SIZE(primary->state.cs); j++) {
1020 struct cs_builder *sec_b = panvk_get_cs_builder(secondary, j);
1021 if (cs_is_empty(sec_b))
1022 continue;
1023
1024 struct cs_builder *prim_b = panvk_get_cs_builder(primary, j);
1025 struct cs_index flag_val = cs_scratch_reg32(prim_b, 0);
1026
1027 /* When the caller itself inherited, the flag is already
1028 * in the subqueue context from our caller.
1029 */
1030 if (primary->state.cond_render.inherited)
1031 continue;
1032
1033 if (primary->state.cond_render.enabled) {
1034 /* Primary has direct conditional rendering, evaluate
1035 * the predicate and normalize to a 0/non-zero flag.
1036 */
1037 struct cs_index pred_addr = cs_scratch_reg64(prim_b, 2);
1038
1039 cs_move64_to(prim_b, pred_addr,
1040 primary->state.cond_render.addr);
1041 cs_load32_to(prim_b, flag_val, pred_addr, 0);
1042
1043 if (primary->state.cond_render.exec_cond ==
1044 MALI_CS_CONDITION_EQUAL) {
1045 /* Inverted: render when pred == 0, so flip. */
1046 cs_if(prim_b, MALI_CS_CONDITION_NEQUAL, flag_val)
1047 cs_move32_to(prim_b, flag_val, 0);
1048 cs_else(prim_b)
1049 cs_move32_to(prim_b, flag_val, 1);
1050 }
1051 } else {
1052 /* No conditional rendering active, always render. */
1053 cs_move32_to(prim_b, flag_val, 1);
1054 }
1055
1056 cs_store32(prim_b, flag_val, cs_subqueue_ctx_reg(prim_b),
1057 offsetof(struct panvk_cs_subqueue_context,
1058 cond_render_flag));
1059 cs_flush_stores(prim_b);
1060 }
1061 }
1062
1063 for (uint32_t j = 0; j < ARRAY_SIZE(primary->state.cs); j++) {
1064 struct cs_builder *sec_b = panvk_get_cs_builder(secondary, j);
1065 assert(cs_is_valid(sec_b));
1066 if (!cs_is_empty(sec_b)) {
1067 struct cs_builder *prim_b = panvk_get_cs_builder(primary, j);
1068 struct cs_index addr = cs_scratch_reg64(prim_b, 0);
1069 struct cs_index size = cs_scratch_reg32(prim_b, 2);
1070 cs_move64_to(prim_b, addr, cs_root_chunk_gpu_addr(sec_b));
1071 cs_move32_to(prim_b, size, cs_root_chunk_size(sec_b));
1072 cs_call(prim_b, addr, size);
1073 prim_b->req_resource_mask |= sec_b->req_resource_mask;
1074
1075 struct u_trace *prim_ut = &primary->utrace.uts[j];
1076 struct u_trace *sec_ut = &secondary->utrace.uts[j];
1077 u_trace_clone_append(u_trace_begin_iterator(sec_ut),
1078 u_trace_end_iterator(sec_ut), prim_ut, prim_b,
1079 panvk_per_arch(utrace_copy_buffer));
1080 }
1081 }
1082
1083 /* We need to propagate the suspending state of the secondary command
1084 * buffer if we want to avoid poisoning the reg file when the secondary
1085 * command buffer suspended the render pass. */
1086 primary->state.gfx.render.suspended =
1087 secondary->state.gfx.render.suspended;
1088
1089 /* Inherit the occlusion query state so that if the secondary contains
1090 * begin/end, but the renderpass ends in the primary, the primary can
1091 * correctly detect that an oq has ended in EndRendering. */
1092 primary->state.gfx.render.oq = secondary->state.gfx.render.oq;
1093
1094 /* If the render context we passed to the secondary command buffer got
1095 * invalidated, reset the FB/tiler descs and treat things as if we
1096 * suspended the render pass, since those descriptors have been
1097 * re-emitted by the secondary command buffer already. */
1098 if (secondary->state.gfx.render.invalidate_inherited_ctx) {
1099 memset(&primary->state.gfx.render.fbds, 0,
1100 sizeof(primary->state.gfx.render.fbds));
1101 primary->state.gfx.render.tiler = 0;
1102 primary->state.gfx.render.flags |= VK_RENDERING_RESUMING_BIT;
1103 }
1104 }
1105
1106 /* From the Vulkan 1.3.275 spec:
1107 *
1108 * "When secondary command buffer(s) are recorded to execute on a
1109 * primary command buffer, the secondary command buffer inherits no
1110 * state from the primary command buffer, and all state of the primary
1111 * command buffer is undefined after an execute secondary command buffer
1112 * command is recorded. There is one exception to this rule - if the
1113 * primary command buffer is inside a render pass instance, then the
1114 * render pass and subpass state is not disturbed by executing secondary
1115 * command buffers. For state dependent commands (such as draws and
1116 * dispatches), any state consumed by those commands must not be
1117 * undefined."
1118 *
1119 * Therefore, it's the client's job to reset all the state in the primary
1120 * after the secondary executes. However, if we're doing any internal
1121 * dirty tracking, we may miss the fact that a secondary has messed with
1122 * GPU state if we don't invalidate all our internal tracking.
1123 */
1124 panvk_cmd_invalidate_state(primary);
1125 }