Debian Codegraph @

src/panfrost/vulkan/csf/panvk_cmd_buffer.h

raw
1 /*
2 * Copyright © 2024 Collabora Ltd.
3 * Copyright © 2025 Arm Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #ifndef PANVK_CMD_BUFFER_H
8 #define PANVK_CMD_BUFFER_H
9
10 #ifndef PAN_ARCH
11 #error "PAN_ARCH must be defined"
12 #endif
13
14 #include <stdint.h>
15
16 #include "genxml/cs_builder.h"
17
18 #include "panvk_cmd_desc_state.h"
19 #include "panvk_cmd_dispatch.h"
20 #include "panvk_cmd_draw.h"
21 #include "panvk_cmd_push_constant.h"
22 #include "panvk_queue.h"
23
24 #include "vk_command_buffer.h"
25 #include "vk_synchronization.h"
26
27 #include "util/list.h"
28 #include "util/perf/u_trace.h"
29
30 struct panvk_sync_scope {
31 VkPipelineStageFlags2 stages;
32 VkAccessFlags2 access;
33 };
34
35 #define MAX_VBS 16
36 #define MAX_RTS 8
37 #define MAX_LAYERS_PER_TILER_DESC 8
38
39 struct panvk_cs_sync32 {
40 uint32_t seqno;
41 uint32_t error;
42 };
43
44 struct panvk_cs_sync64 {
45 uint64_t seqno;
46 uint32_t error;
47 uint32_t pad;
48 };
49
50 struct panvk_cs_desc_ringbuf {
51 uint64_t syncobj;
52 uint64_t ptr;
53 uint32_t pos;
54 uint32_t pad;
55 };
56
57 enum panvk_incremental_rendering_pass {
58 PANVK_IR_FIRST_PASS,
59 PANVK_IR_MIDDLE_PASS,
60 PANVK_IR_LAST_PASS,
61 PANVK_IR_PASS_COUNT
62 };
63
64 static inline uint32_t
65 get_tiler_oom_handler_idx(bool has_zs_ext, uint32_t rt_count)
66 {
67 assert(rt_count >= 1 && rt_count <= MAX_RTS);
68 uint32_t idx = has_zs_ext * MAX_RTS + (rt_count - 1);
69 assert(idx < 2 * MAX_RTS);
70 return idx;
71 }
72
73 static inline uint32_t
74 get_fbd_size(bool has_zs_ext, uint32_t rt_count)
75 {
76 assert(rt_count >= 1 && rt_count <= MAX_RTS);
77 uint32_t fbd_size = pan_size(FRAMEBUFFER);
78 if (has_zs_ext)
79 fbd_size += pan_size(ZS_CRC_EXTENSION);
80 fbd_size += pan_size(RENDER_TARGET) * rt_count;
81 return fbd_size;
82 }
83
84 /* 512k of render descriptors that can be used when
85 * VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT is set on the command buffer. */
86 #define RENDER_DESC_RINGBUF_SIZE (512 * 1024)
87
88 /* Helper defines to get specific fields in the tiler_oom_ctx. */
89 #define TILER_OOM_CTX_FIELD_OFFSET(_name) \
90 offsetof(struct panvk_cs_subqueue_context, tiler_oom_ctx._name)
91
92 struct panvk_cs_timestamp_query {
93 struct cs_single_link_list_node node;
94 uint64_t reports;
95 uint64_t avail;
96 };
97
98 struct panvk_cs_occlusion_query {
99 struct cs_single_link_list_node node;
100 uint64_t syncobj;
101 };
102
103 struct panvk_cs_subqueue_context {
104 uint64_t syncobjs;
105 #if PAN_ARCH == 10
106 /* must follow syncobjs immediately for cs_load_to */
107 uint32_t iter_sb;
108 #else
109 uint32_t pad;
110 #endif
111 uint32_t last_error;
112 uint64_t reg_dump_addr;
113 struct {
114 struct panvk_cs_desc_ringbuf desc_ringbuf;
115 uint64_t tiler_heap;
116 uint64_t geom_buf;
117 struct cs_single_link_list oq_chain;
118 /* Timestamp queries that need to happen after the current rp. */
119 struct cs_single_link_list ts_chain;
120 struct cs_single_link_list ts_done_chain;
121 } render;
122 struct {
123 uint32_t counter;
124 /* Base pointer to regular FBD for layer 0 */
125 uint64_t layer_fbd_ptr;
126 /* Pointer to scratch FBD used in the event of IR */
127 uint64_t ir_scratch_fbd_ptr;
128 /* FBD+DBD+RTDs IR descs to be copied to the scratch FBD when
129 * IR is triggered.
130 */
131 uint64_t ir_descs[PANVK_IR_PASS_COUNT];
132 uint32_t td_count;
133 uint32_t layer_count;
134 } tiler_oom_ctx;
135 struct {
136 struct {
137 uint64_t cs;
138 } tracebuf;
139 } debug;
140 /* Non-zero when draws should execute, zero when they should be
141 * skipped. Written by the primary before cs_call, read by inherited
142 * secondaries at each draw/dispatch.
143 */
144 uint32_t cond_render_flag;
145 } __attribute__((aligned(64)));
146
147 struct panvk_cache_flush_info {
148 enum mali_cs_flush_mode l2;
149 enum mali_cs_flush_mode lsc;
150 enum mali_cs_other_flush_mode others;
151 };
152
153 struct panvk_cs_deps {
154 bool needs_fb_barrier;
155
156 struct {
157 uint32_t wait_sb_mask;
158 struct panvk_cache_flush_info cache_flush;
159 } src[PANVK_SUBQUEUE_COUNT];
160
161 struct {
162 uint32_t wait_subqueue_mask;
163 bool conditional;
164 enum mali_cs_condition cond;
165 struct cs_index cond_value;
166 } dst[PANVK_SUBQUEUE_COUNT];
167 bool needs_layout_transitions;
168 };
169
170 enum panvk_sb_ids {
171 PANVK_SB_LS = 0,
172 PANVK_SB_IMM_FLUSH = 0,
173 PANVK_SB_DEFERRED_SYNC = 1,
174 PANVK_SB_DEFERRED_FLUSH = 2,
175 PANVK_SB_ITER_START = 3,
176 PANVK_SB_ITER_COUNT = 5,
177 };
178
179 #define SB_IMM_MASK 0
180 #define SB_MASK(nm) BITFIELD_BIT(PANVK_SB_##nm)
181 #define SB_ID(nm) PANVK_SB_##nm
182 #define SB_ITER(x) (PANVK_SB_ITER_START + (x))
183 #define SB_WAIT_ITER(x) BITFIELD_BIT(PANVK_SB_ITER_START + (x))
184
185 /* We use different resource registers for different types of compute shader
186 * dispatch, to avoid dirtying all the registers when switching from one to
187 * the other */
188 #define PANVK_COMPUTE_SRT MALI_COMPUTE_SR_SRT_0
189 #define PANVK_COMPUTE_FAU MALI_COMPUTE_SR_FAU_0
190 #define PANVK_COMPUTE_SPD MALI_COMPUTE_SR_SPD_0
191 #define PANVK_COMPUTE_TSD MALI_COMPUTE_SR_TSD_0
192 #define PANVK_COMPUTE_RES_SEL cs_shader_res_sel(0, 0, 0, 0)
193
194 #define PANVK_PRECOMP_SRT MALI_COMPUTE_SR_SRT_1
195 #define PANVK_PRECOMP_FAU MALI_COMPUTE_SR_FAU_1
196 #define PANVK_PRECOMP_SPD MALI_COMPUTE_SR_SPD_1
197 #define PANVK_PRECOMP_TSD MALI_COMPUTE_SR_TSD_1
198 #define PANVK_PRECOMP_RES_SEL cs_shader_res_sel(1, 1, 1, 1)
199
200 enum panvk_cs_regs {
201 /* RUN_IDVS staging regs. */
202 PANVK_CS_REG_RUN_IDVS_SR_START = 0,
203
204 #if PAN_ARCH >= 12
205 PANVK_CS_REG_RUN_IDVS_SR_END = 65,
206 #elif PAN_ARCH == 11
207 PANVK_CS_REG_RUN_IDVS_SR_END = 63,
208 #else
209 PANVK_CS_REG_RUN_IDVS_SR_END = 60,
210 #endif
211
212 /* RUN_FRAGMENT staging regs.
213 * SW ABI:
214 * - r38:39 contain the pointer to the first tiler descriptor. This is
215 * needed to gather completed heap chunks after a run_fragment.
216 */
217 PANVK_CS_REG_RUN_FRAGMENT_SR_START = 38,
218 PANVK_CS_REG_RUN_FRAGMENT_SR_END = 46,
219
220 /* RUN_COMPUTE staging regs. */
221 PANVK_CS_REG_RUN_COMPUTE_SR_START = 0,
222 PANVK_CS_REG_RUN_COMPUTE_SR_END = 39,
223
224 /* Range of registers that can be used to store temporary data on
225 * all queues. Note that some queues have extra space they can use
226 * as scratch space.*/
227 PANVK_CS_REG_SCRATCH_START = 66,
228
229 /* On v12+, we have 128 registers so that gives us way more space to work with */
230 #if PAN_ARCH >= 12
231 PANVK_CS_REG_SCRATCH_END = 115,
232
233 /* Driver context. */
234 PANVK_CS_REG_PROGRESS_SEQNO_START = 116,
235 PANVK_CS_REG_PROGRESS_SEQNO_END = 121,
236 PANVK_CS_REG_SUBQUEUE_CTX_START = 122,
237 PANVK_CS_REG_SUBQUEUE_CTX_END = 123,
238 #else
239 PANVK_CS_REG_SCRATCH_END = 83,
240
241 /* Driver context. */
242 PANVK_CS_REG_PROGRESS_SEQNO_START = 84,
243 PANVK_CS_REG_PROGRESS_SEQNO_END = 89,
244 PANVK_CS_REG_SUBQUEUE_CTX_START = 90,
245 PANVK_CS_REG_SUBQUEUE_CTX_END = 91,
246 #endif
247 };
248
249 #define CS_REG_SCRATCH_COUNT \
250 (PANVK_CS_REG_SCRATCH_END - PANVK_CS_REG_SCRATCH_START + 1)
251
252 static inline struct cs_index
253 cs_scratch_reg_tuple(struct cs_builder *b, unsigned start, unsigned count)
254 {
255 assert(start + count <= CS_REG_SCRATCH_COUNT);
256 return cs_reg_tuple(b, PANVK_CS_REG_SCRATCH_START + start, count);
257 }
258
259 static inline struct cs_index
260 cs_scratch_reg32(struct cs_builder *b, unsigned reg)
261 {
262 return cs_scratch_reg_tuple(b, reg, 1);
263 }
264
265 static inline struct cs_index
266 cs_scratch_reg64(struct cs_builder *b, unsigned reg)
267 {
268 assert(reg % 2 == 0);
269 return cs_scratch_reg_tuple(b, reg, 2);
270 }
271
272 static inline struct cs_index
273 cs_subqueue_ctx_reg(struct cs_builder *b)
274 {
275 return cs_reg64(b, PANVK_CS_REG_SUBQUEUE_CTX_START);
276 }
277
278 static inline struct cs_index
279 cs_progress_seqno_reg(struct cs_builder *b, enum panvk_subqueue_id subqueue)
280 {
281 assert(PANVK_CS_REG_PROGRESS_SEQNO_START + (subqueue * 2) <
282 PANVK_CS_REG_PROGRESS_SEQNO_END);
283 return cs_reg64(b, PANVK_CS_REG_PROGRESS_SEQNO_START + (subqueue * 2));
284 }
285
286 struct panvk_cs_reg_upd_context {
287 reg_perm_cb_t reg_perm;
288 struct panvk_cs_reg_upd_context *next;
289 };
290
291 struct panvk_cs_state {
292 struct cs_builder builder;
293
294 /* Used to debug register writes in invalid contexts. */
295 struct {
296 struct panvk_cs_reg_upd_context *upd_ctx_stack;
297 reg_perm_cb_t base_perm;
298 } reg_access;
299
300 /* Sync point relative to the beginning of the command buffer.
301 * Needs to be offset with the subqueue sync point. */
302 int32_t relative_sync_point;
303
304 struct cs_tracing_ctx tracing;
305 };
306
307 static inline struct panvk_cs_reg_upd_context *
308 panvk_cs_reg_ctx_push(struct cs_builder *b,
309 struct panvk_cs_reg_upd_context *ctx,
310 reg_perm_cb_t reg_perm)
311 {
312 struct panvk_cs_state *cs_state =
313 container_of(b, struct panvk_cs_state, builder);
314
315 ctx->reg_perm = reg_perm;
316 ctx->next = cs_state->reg_access.upd_ctx_stack;
317 cs_state->reg_access.upd_ctx_stack = ctx;
318 return ctx;
319 }
320
321 static inline void
322 panvk_cs_reg_ctx_pop(struct cs_builder *b, struct panvk_cs_reg_upd_context *ctx)
323 {
324 struct panvk_cs_state *cs_state =
325 container_of(b, struct panvk_cs_state, builder);
326
327 assert(cs_state->reg_access.upd_ctx_stack == ctx);
328
329 cs_state->reg_access.upd_ctx_stack = ctx->next;
330 }
331
332 struct panvk_cs_reg_range {
333 unsigned start;
334 unsigned end;
335 };
336
337 #define PANVK_CS_REG_RANGE(__name) \
338 { \
339 .start = PANVK_CS_REG_##__name##_START, \
340 .end = PANVK_CS_REG_##__name##_END, \
341 }
342
343 #define panvk_cs_reg_blacklist(__name, ...) \
344 static inline enum cs_reg_perm panvk_cs_##__name##_reg_perm( \
345 struct cs_builder *b, unsigned reg) \
346 { \
347 const struct panvk_cs_reg_range ranges[] = { \
348 __VA_ARGS__, \
349 }; \
350 \
351 for (unsigned i = 0; i < ARRAY_SIZE(ranges); i++) { \
352 if (reg >= ranges[i].start && reg <= ranges[i].end) \
353 return CS_REG_RD; \
354 } \
355 \
356 return CS_REG_RW; \
357 }
358
359 panvk_cs_reg_blacklist(vt, PANVK_CS_REG_RANGE(RUN_IDVS_SR),
360 PANVK_CS_REG_RANGE(PROGRESS_SEQNO),
361 PANVK_CS_REG_RANGE(SUBQUEUE_CTX));
362 panvk_cs_reg_blacklist(frag, PANVK_CS_REG_RANGE(RUN_FRAGMENT_SR),
363 PANVK_CS_REG_RANGE(PROGRESS_SEQNO),
364 PANVK_CS_REG_RANGE(SUBQUEUE_CTX));
365 panvk_cs_reg_blacklist(compute, PANVK_CS_REG_RANGE(RUN_COMPUTE_SR),
366 PANVK_CS_REG_RANGE(PROGRESS_SEQNO),
367 PANVK_CS_REG_RANGE(SUBQUEUE_CTX));
368
369 #define panvk_cs_reg_whitelist(__name, ...) \
370 static inline enum cs_reg_perm panvk_cs_##__name##_reg_perm( \
371 struct cs_builder *b, unsigned reg) \
372 { \
373 const struct panvk_cs_reg_range ranges[] = { \
374 __VA_ARGS__, \
375 }; \
376 \
377 for (unsigned i = 0; i < ARRAY_SIZE(ranges); i++) { \
378 if (reg >= ranges[i].start && reg <= ranges[i].end) \
379 return CS_REG_RW; \
380 } \
381 \
382 return CS_REG_RD; \
383 }
384
385 #define panvk_cs_reg_upd_ctx(__b, __name) \
386 for (struct panvk_cs_reg_upd_context __reg_upd_ctx, \
387 *reg_upd_ctxp = panvk_cs_reg_ctx_push(__b, &__reg_upd_ctx, \
388 panvk_cs_##__name##_reg_perm); \
389 reg_upd_ctxp; \
390 panvk_cs_reg_ctx_pop(__b, &__reg_upd_ctx), reg_upd_ctxp = NULL)
391
392 panvk_cs_reg_whitelist(progress_seqno, PANVK_CS_REG_RANGE(PROGRESS_SEQNO));
393 #define cs_update_progress_seqno(__b) panvk_cs_reg_upd_ctx(__b, progress_seqno)
394
395 panvk_cs_reg_whitelist(compute_ctx, PANVK_CS_REG_RANGE(RUN_COMPUTE_SR));
396 #define cs_update_compute_ctx(__b) panvk_cs_reg_upd_ctx(__b, compute_ctx)
397
398 panvk_cs_reg_whitelist(frag_ctx, PANVK_CS_REG_RANGE(RUN_FRAGMENT_SR));
399 #define cs_update_frag_ctx(__b) panvk_cs_reg_upd_ctx(__b, frag_ctx)
400
401 panvk_cs_reg_whitelist(vt_ctx, PANVK_CS_REG_RANGE(RUN_IDVS_SR));
402 #define cs_update_vt_ctx(__b) panvk_cs_reg_upd_ctx(__b, vt_ctx)
403
404 panvk_cs_reg_whitelist(cmdbuf_regs, {PANVK_CS_REG_RUN_IDVS_SR_START,
405 PANVK_CS_REG_SCRATCH_END});
406 #define cs_update_cmdbuf_regs(__b) panvk_cs_reg_upd_ctx(__b, cmdbuf_regs)
407
408 struct panvk_tls_state {
409 struct pan_ptr desc;
410 struct pan_tls_info info;
411 unsigned max_wg_count;
412 };
413
414 struct panvk_cond_render_state {
415 bool enabled;
416 bool inherited;
417 uint64_t addr;
418 enum mali_cs_condition exec_cond;
419 };
420
421 struct panvk_cmd_buffer {
422 struct vk_command_buffer vk;
423 VkCommandBufferUsageFlags flags;
424 struct panvk_pool cs_pool;
425 struct panvk_pool desc_pool;
426 struct panvk_pool tls_pool;
427 struct list_head push_sets;
428
429 struct {
430 struct u_trace uts[PANVK_SUBQUEUE_COUNT];
431 } utrace;
432
433 struct {
434 struct panvk_cmd_graphics_state gfx;
435 struct panvk_cmd_compute_state compute;
436 struct panvk_push_constant_state push_constants;
437 struct panvk_cs_state cs[PANVK_SUBQUEUE_COUNT];
438 struct panvk_tls_state tls;
439 bool contains_timestamp_queries;
440
441 struct panvk_cond_render_state cond_render;
442 } state;
443 };
444
445 VK_DEFINE_HANDLE_CASTS(panvk_cmd_buffer, vk.base, VkCommandBuffer,
446 VK_OBJECT_TYPE_COMMAND_BUFFER)
447
448 struct panvk_cond_render_ctx {
449 bool active;
450 struct cs_if_else if_else;
451 };
452
453 static inline struct panvk_cond_render_ctx *
454 panvk_cond_render_begin(struct panvk_cmd_buffer *cmdbuf, struct cs_builder *b,
455 struct panvk_cond_render_ctx *ctx)
456 {
457 ctx->active =
458 cmdbuf->state.cond_render.enabled || cmdbuf->state.cond_render.inherited;
459
460 if (ctx->active) {
461 struct cs_index pred_val = cs_scratch_reg32(b, 16);
462
463 if (cmdbuf->state.cond_render.enabled) {
464 /* Direct: load predicate from the buffer. */
465 struct cs_index pred_addr = cs_scratch_reg64(b, 14);
466
467 cs_move64_to(b, pred_addr, cmdbuf->state.cond_render.addr);
468 cs_load32_to(b, pred_val, pred_addr, 0);
469 cs_if_start(b, &ctx->if_else, cmdbuf->state.cond_render.exec_cond,
470 pred_val);
471 } else {
472 /* Inherited: load flag from subqueue context. */
473 cs_load32_to(b, pred_val, cs_subqueue_ctx_reg(b),
474 offsetof(struct panvk_cs_subqueue_context,
475 cond_render_flag));
476 cs_if_start(b, &ctx->if_else, MALI_CS_CONDITION_NEQUAL, pred_val);
477 }
478 }
479
480 return ctx;
481 }
482
483 static inline void
484 panvk_cond_render_end(struct cs_builder *b, struct panvk_cond_render_ctx *ctx)
485 {
486 if (ctx->active)
487 cs_if_end(b, &ctx->if_else);
488 }
489
490 /* Wrap GPU commands (draws/dispatches) with conditional rendering.
491 * When conditional rendering is inactive, the body executes with zero
492 * overhead. When active, the body is wrapped in a cs_if that loads
493 * the predicate from the buffer and branches over the commands if
494 * the condition says to skip.
495 */
496 #define panvk_cond_render(cmdbuf, b) \
497 for (struct panvk_cond_render_ctx __cond_ctx, \
498 *__cond_p = panvk_cond_render_begin(cmdbuf, b, &__cond_ctx); \
499 __cond_p != NULL; panvk_cond_render_end(b, __cond_p), __cond_p = NULL)
500
501 static bool
502 inherits_render_ctx(struct panvk_cmd_buffer *cmdbuf)
503 {
504 return (cmdbuf->vk.level == VK_COMMAND_BUFFER_LEVEL_SECONDARY &&
505 (cmdbuf->flags &
506 VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT)) ||
507 (cmdbuf->state.gfx.render.flags & VK_RENDERING_RESUMING_BIT);
508 }
509
510 static inline struct cs_builder *
511 panvk_get_cs_builder(struct panvk_cmd_buffer *cmdbuf, uint32_t subqueue)
512 {
513 return &cmdbuf->state.cs[subqueue].builder;
514 }
515
516 static inline struct panvk_descriptor_state *
517 panvk_cmd_get_desc_state(struct panvk_cmd_buffer *cmdbuf,
518 VkPipelineBindPoint bindpoint)
519 {
520 switch (bindpoint) {
521 case VK_PIPELINE_BIND_POINT_GRAPHICS:
522 return &cmdbuf->state.gfx.desc_state;
523
524 case VK_PIPELINE_BIND_POINT_COMPUTE:
525 return &cmdbuf->state.compute.desc_state;
526
527 default:
528 assert(!"Unsupported bind point");
529 return NULL;
530 }
531 }
532
533 static bool
534 panvk_cache_flush_is_nop(const struct panvk_cache_flush_info *cache_flush)
535 {
536 return cache_flush->l2 == MALI_CS_FLUSH_MODE_NONE &&
537 cache_flush->lsc == MALI_CS_FLUSH_MODE_NONE &&
538 cache_flush->others == MALI_CS_OTHER_FLUSH_MODE_NONE;
539 }
540
541 extern const struct vk_command_buffer_ops panvk_per_arch(cmd_buffer_ops);
542
543 void panvk_per_arch(cmd_fb_barrier)(struct panvk_cmd_buffer *cmdbuf);
544
545 #if PAN_ARCH == 10
546 /* Match against all possible iter_sb values. The constant iter_sb value for
547 * the current match arm is in '__val'. */
548 #define cs_match_iter_sb(__b, __val, __iter_sb, __scratch) \
549 cs_match(__b, __iter_sb, __scratch) \
550 for (unsigned __val = 0; __val < PANVK_SB_ITER_COUNT; __val++) \
551 cs_case(__b, SB_ITER(__val))
552 #endif
553
554 #if PAN_ARCH >= 11
555 struct cs_iter_sb_update_ctx {
556 struct cs_builder *b;
557 uint16_t all_iters_mask;
558
559 struct {
560 struct cs_index next_sb;
561 struct cs_index sb_mask;
562 } regs;
563 };
564
565 static inline struct cs_iter_sb_update_ctx
566 cs_iter_sb_update_start(struct panvk_cmd_buffer *cmdbuf,
567 enum panvk_subqueue_id subqueue,
568 struct cs_index scratch_regs)
569 {
570 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
571 struct cs_builder *b = panvk_get_cs_builder(cmdbuf, subqueue);
572 struct cs_index next_sb = cs_extract32(b, scratch_regs, 0);
573 struct cs_iter_sb_update_ctx ctx = {
574 .b = b,
575 .all_iters_mask = dev->csf.sb.all_iters_mask,
576 .regs = {
577 .next_sb = next_sb,
578 .sb_mask = cs_extract32(b, scratch_regs, 1),
579 },
580 };
581
582 cs_next_sb_entry(b, next_sb, MALI_CS_SCOREBOARD_TYPE_ENDPOINT,
583 MALI_CS_NEXT_SB_ENTRY_FORMAT_INDEX);
584
585 return ctx;
586 }
587
588 static inline void
589 cs_iter_sb_update_end(struct cs_iter_sb_update_ctx *ctx)
590 {
591 struct cs_builder *b = ctx->b;
592 struct cs_index next_sb = ctx->regs.next_sb;
593 struct cs_index sb_mask = ctx->regs.sb_mask;
594 uint16_t all_iters_mask = ctx->all_iters_mask;
595
596 /* Setup indirect scoreboard wait mask now for indirect defer */
597 cs_move32_to(b, sb_mask, 0);
598 cs_bit_set32(b, sb_mask, sb_mask, next_sb);
599 cs_set_state(b, MALI_CS_SET_STATE_TYPE_SB_MASK_WAIT, sb_mask);
600
601 /* Prevent direct re-use of the current SB to avoid conflict between
602 * wait(current),signal(next) (can't wait on an SB we signal).
603 */
604 cs_move32_to(b, sb_mask, all_iters_mask);
605 cs_bit_clear32(b, sb_mask, sb_mask, next_sb);
606 cs_set_state(b, MALI_CS_SET_STATE_TYPE_SB_MASK_STREAM, sb_mask);
607
608 ctx->b = NULL;
609 }
610
611 #define cs_iter_sb_update(__cmdbuf, __subq, __scratch_regs, __upd_ctx) \
612 for (struct cs_iter_sb_update_ctx __upd_ctx = \
613 cs_iter_sb_update_start(__cmdbuf, __subq, __scratch_regs); \
614 __upd_ctx.b; cs_iter_sb_update_end(&__upd_ctx))
615
616 #else
617 struct cs_iter_sb_update_ctx {
618 struct cs_builder *b;
619 uint8_t cur_sb;
620 uint8_t next_sb;
621
622 struct {
623 struct cs_index next_sb;
624 struct cs_index cmp_scratch;
625 } regs;
626 };
627
628 static inline struct cs_iter_sb_update_ctx
629 cs_iter_sb_update_start(struct panvk_cmd_buffer *cmdbuf,
630 enum panvk_subqueue_id subqueue,
631 struct cs_index scratch_regs)
632 {
633 struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device);
634 struct cs_builder *b = panvk_get_cs_builder(cmdbuf, subqueue);
635 struct cs_index next_sb = cs_extract32(b, scratch_regs, 0);
636 struct cs_index cmp_scratch = cs_extract32(b, scratch_regs, 1);
637 struct cs_iter_sb_update_ctx ctx = {
638 .b = b,
639 .regs = {
640 .next_sb = next_sb,
641 .cmp_scratch = cmp_scratch,
642 },
643 };
644
645 cs_load32_to(b, next_sb, cs_subqueue_ctx_reg(b),
646 offsetof(struct panvk_cs_subqueue_context, iter_sb));
647
648 /* Select next scoreboard entry and wrap around if we get past the limit */
649 cs_add32(b, next_sb, next_sb, 1);
650 cs_add32(b, cmp_scratch, next_sb, -SB_ITER(dev->csf.sb.iter_count));
651
652 cs_if(b, MALI_CS_CONDITION_GEQUAL, cmp_scratch) {
653 cs_move32_to(b, next_sb, SB_ITER(0));
654 }
655
656 cs_store32(b, next_sb, cs_subqueue_ctx_reg(b),
657 offsetof(struct panvk_cs_subqueue_context, iter_sb));
658 cs_flush_stores(b);
659
660 return ctx;
661 }
662
663 static inline void
664 cs_iter_sb_update_end(struct cs_iter_sb_update_ctx *ctx)
665 {
666 ctx->b = NULL;
667 }
668
669 static void
670 cs_iter_sb_update_first_case(struct cs_iter_sb_update_ctx *ctx)
671 {
672 ctx->cur_sb = PANVK_SB_ITER_COUNT - 1;
673 ctx->next_sb = 0;
674 }
675
676 static void
677 cs_iter_sb_update_next_case(struct cs_iter_sb_update_ctx *ctx)
678 {
679 ctx->cur_sb = (ctx->cur_sb + 1) % PANVK_SB_ITER_COUNT;
680 ctx->next_sb++;
681 }
682
683 static inline bool
684 cs_iter_sb_update_case_preamble(struct cs_iter_sb_update_ctx *ctx)
685 {
686 struct cs_builder *b = ctx->b;
687
688 cs_wait_slot(b, SB_ITER(ctx->next_sb));
689 cs_select_endpoint_sb(b, SB_ITER(ctx->next_sb));
690 return false;
691 }
692
693 #define cs_iter_sb_update_case(__upd_ctx) \
694 cs_case(__upd_ctx.b, SB_ITER(__upd_ctx.next_sb)) \
695 for (bool __done = cs_iter_sb_update_case_preamble(&__upd_ctx); !__done; \
696 __done = true)
697
698 #define cs_iter_sb_update(__cmdbuf, __subq, __scratch_regs, __upd_ctx) \
699 for (struct cs_iter_sb_update_ctx __upd_ctx = \
700 cs_iter_sb_update_start(__cmdbuf, __subq, __scratch_regs); \
701 __upd_ctx.b; cs_iter_sb_update_end(&__upd_ctx)) \
702 cs_match((__upd_ctx).b, __upd_ctx.regs.next_sb, \
703 __upd_ctx.regs.cmp_scratch) \
704 for (cs_iter_sb_update_first_case(&__upd_ctx); \
705 __upd_ctx.next_sb < PANVK_SB_ITER_COUNT; \
706 cs_iter_sb_update_next_case(&__upd_ctx)) \
707 cs_iter_sb_update_case(__upd_ctx)
708
709 #endif
710
711 static inline void
712 cs_next_iter_sb(struct panvk_cmd_buffer *cmdbuf,
713 enum panvk_subqueue_id subqueue, struct cs_index scratch_regs)
714 {
715 /* Scoreboard transitions on the fragment subqueue is more complex than just
716 * updating the scoreboard slot, so make sure we never hit that path on a
717 * fragment subqueue. See issue_fragment_jobs() for more details.
718 */
719 assert(subqueue != PANVK_SUBQUEUE_FRAGMENT);
720
721 cs_iter_sb_update(cmdbuf, subqueue, scratch_regs, _) {
722 /* We only want to move to the new scoreboard, so nothing to do here. */
723 }
724 }
725
726 enum panvk_barrier_stage {
727 PANVK_BARRIER_STAGE_FIRST,
728 PANVK_BARRIER_STAGE_AFTER_LAYOUT_TRANSITION,
729 };
730
731 void panvk_per_arch(add_cs_deps)(
732 struct panvk_cmd_buffer *cmdbuf,
733 enum panvk_barrier_stage barrier_stage,
734 const VkDependencyInfo *in,
735 struct panvk_cs_deps *out,
736 bool is_set_event);
737
738 VkResult panvk_per_arch(cmd_prepare_exec_cmd_for_draws)(
739 struct panvk_cmd_buffer *primary, struct panvk_cmd_buffer *secondary);
740
741 void panvk_per_arch(cmd_inherit_render_state)(
742 struct panvk_cmd_buffer *cmdbuf, const VkCommandBufferBeginInfo *pBeginInfo);
743
744 static inline void
745 panvk_per_arch(calculate_task_axis_and_increment)(
746 const struct panvk_shader_variant *shader,
747 struct panvk_physical_device *phys_dev, const struct pan_compute_dim *wg_dim,
748 unsigned *task_axis, unsigned *task_increment)
749 {
750 /* Pick the task_axis and task_increment to maximize thread
751 * utilization. */
752 const struct pan_kmod_dev_props *props = &phys_dev->kmod.dev->props;
753 const unsigned max_thread_cnt =
754 pan_compute_max_thread_count(props, shader->info.work_reg_count);
755 const unsigned threads_per_wg = shader->cs.local_size.x *
756 shader->cs.local_size.y *
757 shader->cs.local_size.z;
758 const unsigned wg_count[3] = {wg_dim->x, wg_dim->y, wg_dim->z};
759 const unsigned total_wgs = wg_dim->x * wg_dim->y * wg_dim->z;
760 const unsigned total_cores = util_bitcount64(phys_dev->compute_core_mask);
761 /* Split workgroups among cores evenly. */
762 const unsigned wgs_per_core = DIV_ROUND_UP(total_wgs, total_cores);
763 unsigned threads_per_task;
764 unsigned wgs_per_task;
765
766 if (!total_wgs) {
767 *task_axis = MALI_TASK_AXIS_X;
768 *task_increment = 1;
769 return;
770 }
771
772 /* We used to maximize threads_per_task, but that is ideal when the system
773 * has a single gpu client. When there are multiple gpu clients, we want
774 * smaller threads_per_task such that cores can be more fairly shared among
775 * the clients.
776 */
777 threads_per_task = DIV_ROUND_UP(max_thread_cnt, props->max_tasks_per_core);
778
779 wgs_per_task = threads_per_task / threads_per_wg;
780 wgs_per_task = CLAMP(wgs_per_task, 1, wgs_per_core);
781
782 *task_axis = MALI_TASK_AXIS_X;
783 *task_increment = wgs_per_task;
784 for (unsigned i = 0; i < 2; i++) {
785 if (*task_increment <= wg_count[i])
786 break;
787
788 (*task_axis)++;
789 *task_increment /= wg_count[i];
790 }
791
792 assert(*task_axis <= MALI_TASK_AXIS_Z);
793 assert(*task_increment > 0);
794 }
795
796 static VkPipelineStageFlags2
797 panvk_get_subqueue_stages(enum panvk_subqueue_id subqueue)
798 {
799 switch (subqueue) {
800 case PANVK_SUBQUEUE_VERTEX_TILER:
801 return VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT |
802 VK_PIPELINE_STAGE_2_INDEX_INPUT_BIT |
803 VK_PIPELINE_STAGE_2_VERTEX_ATTRIBUTE_INPUT_BIT |
804 VK_PIPELINE_STAGE_2_VERTEX_SHADER_BIT |
805 VK_PIPELINE_STAGE_2_CONDITIONAL_RENDERING_BIT_EXT;
806 case PANVK_SUBQUEUE_FRAGMENT:
807 return VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT |
808 VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT |
809 VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT |
810 VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT |
811 VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_RESOLVE_BIT |
812 VK_PIPELINE_STAGE_2_BLIT_BIT | VK_PIPELINE_STAGE_2_CLEAR_BIT |
813 VK_PIPELINE_STAGE_2_CONDITIONAL_RENDERING_BIT_EXT;
814 case PANVK_SUBQUEUE_COMPUTE:
815 return VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT |
816 VK_PIPELINE_STAGE_2_COPY_BIT |
817 VK_PIPELINE_STAGE_2_CONDITIONAL_RENDERING_BIT_EXT;
818 default:
819 UNREACHABLE("Invalid subqueue id");
820 }
821 }
822
823 /* Hint for vk_stages_to_subqueue_mask if stages refer to first or second
824 * synchronization scope
825 */
826 enum sync_scope {
827 /* First scope (sometimes called 'source' in Vulkan)
828 * is the first stage(s) of an execution dependency chain.
829 */
830 SYNC_SCOPE_FIRST,
831 /* Second scope (sometimes called 'destination' in Vulkan)
832 * is the second stage(s) of an execution dependency chain.
833 */
834 SYNC_SCOPE_SECOND,
835 };
836
837 static uint32_t
838 vk_stages_to_subqueue_mask(VkPipelineStageFlags2 vk_stages,
839 enum sync_scope scope)
840 {
841 /* Handle other compound stages by expanding. */
842 switch (scope) {
843 case SYNC_SCOPE_FIRST:
844 vk_stages = vk_expand_src_stage_flags2(vk_stages);
845 break;
846 case SYNC_SCOPE_SECOND:
847 vk_stages = vk_expand_dst_stage_flags2(vk_stages);
848 break;
849 default:
850 UNREACHABLE("Invalid sync_scope");
851 }
852
853 VkPipelineStageFlags2 flags[PANVK_SUBQUEUE_COUNT];
854 for (uint32_t sq = 0; sq < PANVK_SUBQUEUE_COUNT; ++sq)
855 flags[sq] = panvk_get_subqueue_stages(sq);
856
857 uint32_t result = 0;
858 if (flags[PANVK_SUBQUEUE_VERTEX_TILER] & vk_stages)
859 result |= BITFIELD_BIT(PANVK_SUBQUEUE_VERTEX_TILER);
860
861 if (flags[PANVK_SUBQUEUE_FRAGMENT] & vk_stages)
862 result |= BITFIELD_BIT(PANVK_SUBQUEUE_FRAGMENT);
863
864 if (flags[PANVK_SUBQUEUE_COMPUTE] & vk_stages)
865 result |= BITFIELD_BIT(PANVK_SUBQUEUE_COMPUTE);
866
867 return result;
868 }
869
870 void panvk_per_arch(emit_barrier)(struct panvk_cmd_buffer *cmdbuf,
871 struct panvk_cs_deps deps);
872
873 #endif /* PANVK_CMD_BUFFER_H */