Debian Codegraph @

src/panfrost/genxml/cs_builder.h

raw
1 /*
2 * Copyright (C) 2022 Collabora Ltd.
3 * Copyright (C) 2025 Arm Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #pragma once
8
9 #if !defined(PAN_ARCH) || PAN_ARCH < 10
10 #error "cs_builder.h requires PAN_ARCH >= 10"
11 #endif
12
13 #include "gen_macros.h"
14
15 #include "util/bitset.h"
16 #include "util/u_dynarray.h"
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22 /* Before 5th Gen, RUN_IDVS could use a selector but as we only hardcode the same
23 * configuration, we match v12+ naming here */
24
25 #if PAN_ARCH <= 11
26 #define MALI_IDVS_SR_VERTEX_SRT MALI_IDVS_SR_SRT_0
27 #define MALI_IDVS_SR_FRAGMENT_SRT MALI_IDVS_SR_SRT_2
28 #define MALI_IDVS_SR_VERTEX_FAU MALI_IDVS_SR_FAU_0
29 #define MALI_IDVS_SR_FRAGMENT_FAU MALI_IDVS_SR_FAU_2
30 #define MALI_IDVS_SR_VERTEX_POS_SPD MALI_IDVS_SR_SPD_0
31 #define MALI_IDVS_SR_VERTEX_VARY_SPD MALI_IDVS_SR_SPD_1
32 #define MALI_IDVS_SR_FRAGMENT_SPD MALI_IDVS_SR_SPD_2
33 #endif
34
35 #if PAN_ARCH == 10
36 #define CS_MAX_SB_COUNT 8
37 #else
38 #define CS_MAX_SB_COUNT 16
39 #endif
40
41 /*
42 * cs_builder implements a builder for CSF command streams. It manages the
43 * allocation and overflow behaviour of queues and provides helpers for emitting
44 * commands to run on the CSF pipe.
45 *
46 * Users are responsible for the CS buffer allocation and must initialize the
47 * command stream with an initial buffer using cs_builder_init(). The CS can
48 * be extended with new buffers allocated with cs_builder_conf::alloc_buffer()
49 * if the builder runs out of memory.
50 */
51
52 struct cs_buffer {
53 /* CPU pointer */
54 uint64_t *cpu;
55
56 /* GPU pointer */
57 uint64_t gpu;
58
59 /* Capacity in number of 64-bit instructions */
60 uint32_t capacity;
61 };
62
63 /**
64 * This is used to check that:
65 * 1. registers are not used as a source after being loaded without a
66 * WAIT(<ls_scoreboard>) in the middle
67 * 2. registers are not reused (used as a destination) after they served as a
68 * STORE() source without a WAIT(<ls_scoreboard>) in the middle
69 */
70 struct cs_load_store_tracker {
71 BITSET_DECLARE(pending_loads, 256);
72 bool pending_stores;
73 };
74
75 /**
76 * This is used to determine which registers as been written to (a.k.a. used
77 * as an instruction's destination).
78 */
79 struct cs_dirty_tracker {
80 BITSET_DECLARE(regs, 256);
81 };
82
83 enum cs_reg_perm {
84 CS_REG_NO_ACCESS = 0,
85 CS_REG_RD = BITFIELD_BIT(1),
86 CS_REG_WR = BITFIELD_BIT(2),
87 CS_REG_RW = CS_REG_RD | CS_REG_WR,
88 };
89
90 struct cs_builder;
91
92 typedef enum cs_reg_perm (*reg_perm_cb_t)(struct cs_builder *b, unsigned reg);
93
94 struct cs_builder_conf {
95 /* Number of 32-bit registers in the hardware register file */
96 uint8_t nr_registers;
97
98 /* Number of 32-bit registers used by the kernel at submission time */
99 uint8_t nr_kernel_registers;
100
101 /* RUN_COMPUTE.ep_limit. Must be 0 before v12. */
102 uint8_t compute_ep_limit;
103
104 /* CS buffer allocator */
105 struct cs_buffer (*alloc_buffer)(void *cookie);
106
107 /* Optional dirty registers tracker. */
108 struct cs_dirty_tracker *dirty_tracker;
109
110 /* Optional register access checker. */
111 reg_perm_cb_t reg_perm;
112
113 /* Cookie passed back to alloc_buffer() */
114 void *cookie;
115
116 /* SB slot used for load/store instructions. */
117 uint8_t ls_sb_slot;
118 };
119
120 /* The CS is formed of one or more CS chunks linked with JUMP instructions.
121 * The builder keeps track of the current chunk and the position inside this
122 * chunk, so it can emit new instructions, and decide when a new chunk needs
123 * to be allocated.
124 */
125 struct cs_chunk {
126 /* CS buffer object backing this chunk */
127 struct cs_buffer buffer;
128
129 union {
130 /* Current position in the buffer object when the chunk is active. */
131 uint32_t pos;
132
133 /* Chunk size when the chunk was wrapped. */
134 uint32_t size;
135 };
136 };
137
138 /* Monolithic sequence of instruction. Must live in a virtually contiguous
139 * portion of code.
140 */
141 struct cs_block {
142 /* Used to insert the block in the block stack. */
143 struct cs_block *next;
144 };
145
146 #define CS_LABEL_INVALID_POS ~0u
147
148 /* Labels can only be used inside a cs_block. They can be defined and
149 * referenced before they are set to point to a specific position
150 * in the block. */
151 struct cs_label {
152 /* The last reference we have seen pointing to this block before
153 * it was set. If set to CS_LABEL_INVALID_POS, no forward reference
154 * pointing to this label exist.
155 */
156 uint32_t last_forward_ref;
157
158 /* The label target. If set to CS_LABEL_INVALID_POS, the label has
159 * not been set yet.
160 */
161 uint32_t target;
162 };
163
164 /* CS if/else block. */
165 struct cs_if_else {
166 struct cs_block block;
167 struct cs_label end_label;
168 struct cs_load_store_tracker *orig_ls_state;
169 struct cs_load_store_tracker ls_state;
170 };
171
172 struct cs_maybe {
173 /* Link to the next pending cs_maybe for the block stack */
174 struct cs_maybe *next_pending;
175 /* Position of patch block relative to blocks.instrs */
176 uint32_t patch_pos;
177
178 /* CPU address of patch block in the chunk */
179 uint64_t *patch_addr;
180 /* Original contents of the patch block, before replacing with NOPs */
181 uint32_t num_instrs;
182 uint64_t instrs[];
183 };
184
185 struct cs_builder {
186 /* CS builder configuration */
187 struct cs_builder_conf conf;
188
189 /* True if an allocation failed, making the whole CS invalid. */
190 bool invalid;
191
192 /* Initial (root) CS chunk. */
193 struct cs_chunk root_chunk;
194
195 /* Current CS chunk. */
196 struct cs_chunk cur_chunk;
197
198 /* Current load/store tracker. */
199 struct cs_load_store_tracker *cur_ls_tracker;
200
201 struct cs_load_store_tracker root_ls_tracker;
202
203 /* ralloc context used for cs_maybe allocations */
204 void *maybe_ctx;
205
206 /* Mask of resources required by this CS. */
207 uint32_t req_resource_mask;
208
209 /* Temporary storage for inner blocks that need to be built
210 * and copied in one monolithic sequence of instructions with no
211 * jump in the middle.
212 */
213 struct {
214 struct cs_block *stack;
215 struct util_dynarray instrs;
216 struct cs_if_else pending_if;
217 /* Linked list of cs_maybe that were emitted inside the current stack */
218 struct cs_maybe *pending_maybes;
219 unsigned last_load_ip_target;
220 } blocks;
221
222 /* Move immediate instruction at the end of the last CS chunk that needs to
223 * be patched with the final length of the current CS chunk in order to
224 * facilitate correct overflow behaviour.
225 */
226 uint32_t *length_patch;
227
228 /* Used as temporary storage when the allocator couldn't allocate a new
229 * CS chunk.
230 */
231 uint64_t discard_instr_slot;
232 };
233
234 static inline void
235 cs_builder_init(struct cs_builder *b, const struct cs_builder_conf *conf,
236 struct cs_buffer root_buffer)
237 {
238 memset(b, 0, sizeof(*b));
239 util_dynarray_init(&b->blocks.instrs, NULL);
240 b->conf = *conf;
241 b->root_chunk.buffer = root_buffer;
242 b->cur_chunk.buffer = root_buffer;
243 b->cur_ls_tracker = &b->root_ls_tracker,
244
245 memset(b->cur_ls_tracker, 0, sizeof(*b->cur_ls_tracker));
246
247 /* We need at least 3 registers for CS chunk linking. Assume the kernel needs
248 * at least that too.
249 */
250 b->conf.nr_kernel_registers = MAX2(b->conf.nr_kernel_registers, 3);
251
252 b->blocks.instrs = UTIL_DYNARRAY_INIT;
253 }
254
255 static inline void
256 cs_builder_fini(struct cs_builder *b)
257 {
258 util_dynarray_fini(&b->blocks.instrs);
259 ralloc_free(b->maybe_ctx);
260 }
261
262 static inline bool
263 cs_is_valid(struct cs_builder *b)
264 {
265 return !b->invalid;
266 }
267
268 static inline bool
269 cs_is_empty(struct cs_builder *b)
270 {
271 return b->cur_chunk.pos == 0 &&
272 b->root_chunk.buffer.gpu == b->cur_chunk.buffer.gpu;
273 }
274
275 static inline uint64_t
276 cs_root_chunk_gpu_addr(struct cs_builder *b)
277 {
278 return b->root_chunk.buffer.gpu;
279 }
280
281 static inline uint32_t
282 cs_root_chunk_size(struct cs_builder *b)
283 {
284 /* Make sure cs_end() was called. */
285 struct cs_chunk empty_chunk;
286 memset(&empty_chunk, 0, sizeof(empty_chunk));
287 assert(!memcmp(&b->cur_chunk, &empty_chunk, sizeof(b->cur_chunk)));
288
289 return b->root_chunk.size * sizeof(uint64_t);
290 }
291
292 /*
293 * Wrap the current queue. External users shouldn't call this function
294 * directly, they should call cs_end() when they are done building
295 * the command stream, which will in turn call cs_wrap_queue().
296 *
297 * Internally, this is also used to finalize internal CS chunks when
298 * allocating new sub-chunks. See cs_alloc_chunk() for details.
299 *
300 * This notably requires patching the previous chunk with the length
301 * we ended up emitting for this chunk.
302 */
303 static inline void
304 cs_wrap_chunk(struct cs_builder *b)
305 {
306 if (!cs_is_valid(b))
307 return;
308
309 if (b->length_patch) {
310 *b->length_patch = (b->cur_chunk.pos * 8);
311 b->length_patch = NULL;
312 }
313
314 if (b->root_chunk.buffer.gpu == b->cur_chunk.buffer.gpu)
315 b->root_chunk.size = b->cur_chunk.size;
316 }
317
318 enum cs_index_type {
319 CS_INDEX_REGISTER = 0,
320 CS_INDEX_UNDEF,
321 };
322
323 struct cs_index {
324 enum cs_index_type type;
325
326 /* Number of 32-bit words in the index, must be nonzero */
327 uint8_t size;
328
329 union {
330 uint64_t imm;
331 uint8_t reg;
332 };
333 };
334
335 static inline struct cs_index
336 cs_undef(void)
337 {
338 return (struct cs_index){
339 .type = CS_INDEX_UNDEF,
340 };
341 }
342
343 static inline uint8_t
344 cs_to_reg_tuple(struct cs_index idx, ASSERTED unsigned expected_size)
345 {
346 assert(idx.type == CS_INDEX_REGISTER);
347 assert(idx.size == expected_size);
348
349 return idx.reg;
350 }
351
352 static inline void cs_flush_load_to(struct cs_builder *b, struct cs_index to,
353 uint16_t mask);
354
355 static inline unsigned
356 cs_src_tuple(struct cs_builder *b, struct cs_index src, ASSERTED unsigned count,
357 uint16_t mask)
358 {
359 unsigned reg = cs_to_reg_tuple(src, count);
360
361 if (unlikely(b->conf.reg_perm)) {
362 for (unsigned i = reg; i < reg + count; i++) {
363 if (mask & BITFIELD_BIT(i - reg)) {
364 assert((b->conf.reg_perm(b, i) & CS_REG_RD) ||
365 !"Trying to read a restricted register");
366 }
367 }
368 }
369
370 cs_flush_load_to(b, src, mask);
371
372 return reg;
373 }
374
375 static inline unsigned
376 cs_src32(struct cs_builder *b, struct cs_index src)
377 {
378 return cs_src_tuple(b, src, 1, BITFIELD_MASK(1));
379 }
380
381 static inline unsigned
382 cs_src64(struct cs_builder *b, struct cs_index src)
383 {
384 return cs_src_tuple(b, src, 2, BITFIELD_MASK(2));
385 }
386
387 static inline unsigned
388 cs_dst_tuple(struct cs_builder *b, struct cs_index dst, ASSERTED unsigned count,
389 uint16_t mask)
390 {
391 unsigned reg = cs_to_reg_tuple(dst, count);
392
393 /* A load followed by another op with the same dst register can overwrite
394 * the result of that following op if there is no wait. For example:
395 * load(dst, addr)
396 * move(dst, v)
397 */
398 cs_flush_load_to(b, dst, mask);
399
400 if (unlikely(b->conf.reg_perm)) {
401 for (unsigned i = reg; i < reg + count; i++) {
402 if (mask & BITFIELD_BIT(i - reg)) {
403 assert((b->conf.reg_perm(b, i) & CS_REG_WR) ||
404 !"Trying to write a restricted register");
405 }
406 }
407 }
408
409 if (unlikely(b->conf.dirty_tracker)) {
410 for (unsigned i = reg; i < reg + count; i++) {
411 if (mask & BITFIELD_BIT(i - reg))
412 BITSET_SET(b->conf.dirty_tracker->regs, i);
413 }
414 }
415
416 return reg;
417 }
418
419 static inline unsigned
420 cs_dst32(struct cs_builder *b, struct cs_index dst)
421 {
422 return cs_dst_tuple(b, dst, 1, BITFIELD_MASK(1));
423 }
424
425 static inline unsigned
426 cs_dst64(struct cs_builder *b, struct cs_index dst)
427 {
428 return cs_dst_tuple(b, dst, 2, BITFIELD_MASK(2));
429 }
430
431 #define CS_MAX_REG_TUPLE_SIZE 16
432
433 static inline struct cs_index
434 cs_reg_tuple(ASSERTED struct cs_builder *b, uint8_t reg, uint8_t size)
435 {
436 assert(reg + size <= b->conf.nr_registers - b->conf.nr_kernel_registers &&
437 "overflowed register file");
438 assert(size <= CS_MAX_REG_TUPLE_SIZE && "unsupported");
439
440 return (struct cs_index){
441 .type = CS_INDEX_REGISTER,
442 .size = size,
443 .reg = reg,
444 };
445 }
446
447 static inline struct cs_index
448 cs_reg32(struct cs_builder *b, unsigned reg)
449 {
450 return cs_reg_tuple(b, reg, 1);
451 }
452
453 static inline struct cs_index
454 cs_reg64(struct cs_builder *b, unsigned reg)
455 {
456 assert((reg % 2) == 0 && "unaligned 64-bit reg");
457 return cs_reg_tuple(b, reg, 2);
458 }
459
460 #define cs_sr_reg_tuple(__b, __cmd, __name, __size) \
461 cs_reg_tuple((__b), MALI_##__cmd##_SR_##__name, (__size))
462 #define cs_sr_reg32(__b, __cmd, __name) \
463 cs_reg32((__b), MALI_##__cmd##_SR_##__name)
464 #define cs_sr_reg64(__b, __cmd, __name) \
465 cs_reg64((__b), MALI_##__cmd##_SR_##__name)
466
467 /*
468 * The top of the register file is reserved for cs_builder internal use. We
469 * need 3 spare registers for handling command queue overflow. These are
470 * available here.
471 */
472 static inline uint8_t
473 cs_overflow_address_reg(struct cs_builder *b)
474 {
475 return b->conf.nr_registers - 2;
476 }
477
478 static inline uint8_t
479 cs_overflow_length_reg(struct cs_builder *b)
480 {
481 return b->conf.nr_registers - 3;
482 }
483
484 static inline struct cs_index
485 cs_extract_tuple(struct cs_builder *b, struct cs_index idx, unsigned word,
486 unsigned size)
487 {
488 assert(idx.type == CS_INDEX_REGISTER && "unsupported");
489 assert(word + size <= idx.size && "overrun");
490
491 return cs_reg_tuple(b, idx.reg + word, size);
492 }
493
494 static inline struct cs_index
495 cs_extract32(struct cs_builder *b, struct cs_index idx, unsigned word)
496 {
497 return cs_extract_tuple(b, idx, word, 1);
498 }
499
500 static inline struct cs_index
501 cs_extract64(struct cs_builder *b, struct cs_index idx, unsigned word)
502 {
503 assert(!(word & 1) && "not properly aligned");
504 return cs_extract_tuple(b, idx, word, 2);
505 }
506
507 static inline struct cs_block *
508 cs_cur_block(struct cs_builder *b)
509 {
510 return b->blocks.stack;
511 }
512
513 #define JUMP_SEQ_INSTR_COUNT 4
514
515 static inline bool
516 cs_reserve_instrs(struct cs_builder *b, uint32_t num_instrs)
517 {
518 /* Don't call this function with num_instrs=0. */
519 assert(num_instrs > 0);
520 assert(cs_cur_block(b) == NULL);
521
522 /* If an allocation failure happened before, we just discard all following
523 * instructions.
524 */
525 if (unlikely(!cs_is_valid(b)))
526 return false;
527
528 /* Make sure we have sufficient capacity if we wont allocate more */
529 if (b->conf.alloc_buffer == NULL) {
530 if (unlikely(b->cur_chunk.size + num_instrs > b->cur_chunk.buffer.capacity)) {
531 assert(!"Out of CS space");
532 b->invalid = true;
533 return false;
534 }
535
536 return true;
537 }
538
539 /* Lazy root chunk allocation. */
540 if (unlikely(!b->root_chunk.buffer.cpu)) {
541 b->root_chunk.buffer = b->conf.alloc_buffer(b->conf.cookie);
542 b->cur_chunk.buffer = b->root_chunk.buffer;
543 if (!b->cur_chunk.buffer.cpu) {
544 b->invalid = true;
545 return false;
546 }
547 }
548
549 /* Make sure the instruction sequence fits in a single chunk. */
550 assert(b->cur_chunk.buffer.capacity >= num_instrs);
551
552 /* If the current chunk runs out of space, allocate a new one and jump to it.
553 * We actually do this a few instructions before running out, because the
554 * sequence to jump to a new queue takes multiple instructions.
555 */
556 bool jump_to_next_chunk =
557 (b->cur_chunk.size + num_instrs + JUMP_SEQ_INSTR_COUNT) >
558 b->cur_chunk.buffer.capacity;
559 if (unlikely(jump_to_next_chunk)) {
560 /* Now, allocate a new chunk */
561 struct cs_buffer newbuf = b->conf.alloc_buffer(b->conf.cookie);
562
563 /* Allocation failure, from now on, all new instructions will be
564 * discarded.
565 */
566 if (unlikely(!newbuf.cpu)) {
567 b->invalid = true;
568 return false;
569 }
570
571 uint64_t *ptr = b->cur_chunk.buffer.cpu + (b->cur_chunk.pos++);
572
573 pan_cast_and_pack(ptr, CS_MOVE48, I) {
574 I.destination = cs_overflow_address_reg(b);
575 I.immediate = newbuf.gpu;
576 }
577
578 ptr = b->cur_chunk.buffer.cpu + (b->cur_chunk.pos++);
579
580 pan_cast_and_pack(ptr, CS_MOVE32, I) {
581 I.destination = cs_overflow_length_reg(b);
582 }
583
584 /* The length will be patched in later */
585 uint32_t *length_patch = (uint32_t *)ptr;
586
587 ptr = b->cur_chunk.buffer.cpu + (b->cur_chunk.pos++);
588
589 pan_cast_and_pack(ptr, CS_JUMP, I) {
590 I.length = cs_overflow_length_reg(b);
591 I.address = cs_overflow_address_reg(b);
592 }
593
594 /* Now that we've emitted everything, finish up the previous queue */
595 cs_wrap_chunk(b);
596
597 /* And make this one current */
598 b->length_patch = length_patch;
599 b->cur_chunk.buffer = newbuf;
600 b->cur_chunk.pos = 0;
601 }
602
603 return true;
604 }
605
606 static inline void *
607 cs_alloc_ins_block(struct cs_builder *b, uint32_t num_instrs)
608 {
609 if (cs_cur_block(b))
610 return util_dynarray_grow(&b->blocks.instrs, uint64_t, num_instrs);
611
612 if (!cs_reserve_instrs(b, num_instrs))
613 return NULL;
614
615 assert(b->cur_chunk.size + num_instrs - 1 < b->cur_chunk.buffer.capacity);
616 uint32_t pos = b->cur_chunk.pos;
617 b->cur_chunk.pos += num_instrs;
618 return b->cur_chunk.buffer.cpu + pos;
619 }
620
621 static inline void
622 cs_flush_block_instrs(struct cs_builder *b)
623 {
624 if (cs_cur_block(b) != NULL)
625 return;
626
627 uint32_t num_instrs =
628 util_dynarray_num_elements(&b->blocks.instrs, uint64_t);
629 if (!num_instrs)
630 return;
631
632 /* If LOAD_IP is the last instruction in the block, we reserve one more
633 * slot to make sure the next instruction won't point to a CS chunk linking
634 * sequence. */
635 if (unlikely(b->blocks.last_load_ip_target >= num_instrs)) {
636 if (!cs_reserve_instrs(b, num_instrs + 1))
637 return;
638 }
639
640 void *buffer = cs_alloc_ins_block(b, num_instrs);
641
642 if (likely(buffer != NULL)) {
643 /* We wait until block instrs are copied to the chunk buffer to calculate
644 * patch_addr, in case we end up allocating a new chunk */
645 while (b->blocks.pending_maybes) {
646 b->blocks.pending_maybes->patch_addr =
647 (uint64_t *) buffer + b->blocks.pending_maybes->patch_pos;
648 b->blocks.pending_maybes = b->blocks.pending_maybes->next_pending;
649 }
650
651 /* If we have a LOAD_IP chain, we need to patch each LOAD_IP
652 * instruction before we copy the block to the final memory
653 * region. */
654 while (unlikely(b->blocks.last_load_ip_target)) {
655 uint64_t *instr = util_dynarray_element(
656 &b->blocks.instrs, uint64_t, b->blocks.last_load_ip_target - 1);
657 unsigned prev_load_ip_target = *instr & BITFIELD_MASK(32);
658 uint64_t ip =
659 b->cur_chunk.buffer.gpu +
660 ((b->cur_chunk.pos - num_instrs + b->blocks.last_load_ip_target) *
661 sizeof(uint64_t));
662
663 /* Drop the prev_load_ip_target value and replace it by the final
664 * IP. */
665 *instr &= ~BITFIELD64_MASK(32);
666 *instr |= ip;
667
668 b->blocks.last_load_ip_target = prev_load_ip_target;
669 }
670
671 memcpy(buffer, b->blocks.instrs.data, b->blocks.instrs.size);
672 }
673
674 util_dynarray_clear(&b->blocks.instrs);
675 }
676
677 static inline uint32_t
678 cs_block_next_pos(struct cs_builder *b)
679 {
680 assert(cs_cur_block(b) != NULL);
681
682 return util_dynarray_num_elements(&b->blocks.instrs, uint64_t);
683 }
684
685 static inline void
686 cs_label_init(struct cs_label *label)
687 {
688 label->last_forward_ref = CS_LABEL_INVALID_POS;
689 label->target = CS_LABEL_INVALID_POS;
690 }
691
692 static inline void
693 cs_set_label(struct cs_builder *b, struct cs_label *label)
694 {
695 assert(label->target == CS_LABEL_INVALID_POS);
696 label->target = cs_block_next_pos(b);
697
698 for (uint32_t next_forward_ref, forward_ref = label->last_forward_ref;
699 forward_ref != CS_LABEL_INVALID_POS; forward_ref = next_forward_ref) {
700 uint64_t *ins =
701 util_dynarray_element(&b->blocks.instrs, uint64_t, forward_ref);
702
703 assert(forward_ref < label->target);
704 assert(label->target - forward_ref <= INT16_MAX);
705
706 /* Save the next forward reference to this target before overwritting
707 * it with the final offset.
708 */
709 int16_t offset = *ins & BITFIELD64_MASK(16);
710
711 next_forward_ref =
712 offset > 0 ? forward_ref - offset : CS_LABEL_INVALID_POS;
713
714 assert(next_forward_ref == CS_LABEL_INVALID_POS ||
715 next_forward_ref < forward_ref);
716
717 *ins &= ~BITFIELD64_MASK(16);
718 *ins |= label->target - forward_ref - 1;
719 }
720 }
721
722 static inline void
723 cs_flush_pending_if(struct cs_builder *b)
724 {
725 if (likely(cs_cur_block(b) != &b->blocks.pending_if.block))
726 return;
727
728 cs_set_label(b, &b->blocks.pending_if.end_label);
729 b->blocks.stack = b->blocks.pending_if.block.next;
730 cs_flush_block_instrs(b);
731 }
732
733 static inline void *
734 cs_alloc_ins(struct cs_builder *b)
735 {
736 /* If an instruction is emitted after an if_end(), it flushes the pending if,
737 * causing further cs_else_start() instructions to be invalid. */
738 cs_flush_pending_if(b);
739
740 return cs_alloc_ins_block(b, 1) ?: &b->discard_instr_slot;
741 }
742
743 /* Call this when you are done building a command stream and want to prepare
744 * it for submission.
745 */
746 static inline void
747 cs_end(struct cs_builder *b)
748 {
749 if (!cs_is_valid(b))
750 return;
751
752 cs_flush_pending_if(b);
753 cs_wrap_chunk(b);
754
755 /* This prevents adding instructions after that point. */
756 memset(&b->cur_chunk, 0, sizeof(b->cur_chunk));
757 }
758
759 /*
760 * Helper to emit a new instruction into the command queue. The allocation needs
761 * to be separated out being pan_pack can evaluate its argument multiple times,
762 * yet cs_alloc has side effects.
763 */
764 #define cs_emit(b, T, cfg) pan_cast_and_pack(cs_alloc_ins(b), CS_##T, cfg)
765
766 /* Asynchronous operations take a mask of scoreboard slots to wait on
767 * before executing the instruction, and signal a scoreboard slot when
768 * the operation is complete.
769 * On v11 and later, asynchronous operations can also wait on a scoreboard
770 * mask and signal a scoreboard slot indirectly instead (set via SET_STATE)
771 * A wait_mask of zero means the operation is synchronous, and signal_slot
772 * is ignored in that case.
773 */
774 struct cs_async_op {
775 uint16_t wait_mask;
776 uint8_t signal_slot;
777 #if PAN_ARCH >= 11
778 bool indirect;
779 #endif
780 };
781
782 static inline struct cs_async_op
783 cs_defer(uint16_t wait_mask, uint8_t signal_slot)
784 {
785 /* The scoreboard slot to signal is incremented before the wait operation,
786 * waiting on it would cause an infinite wait.
787 */
788 assert(!(wait_mask & BITFIELD_BIT(signal_slot)));
789
790 return (struct cs_async_op){
791 .wait_mask = wait_mask,
792 .signal_slot = signal_slot,
793 };
794 }
795
796 static inline struct cs_async_op
797 cs_now(void)
798 {
799 return (struct cs_async_op){
800 .wait_mask = 0,
801 .signal_slot = 0xff,
802 };
803 }
804
805 #if PAN_ARCH >= 11
806 static inline struct cs_async_op
807 cs_defer_indirect(void)
808 {
809 return (struct cs_async_op){
810 .wait_mask = 0xff,
811 .signal_slot = 0xff,
812 .indirect = true,
813 };
814 }
815 #endif
816
817 static inline bool
818 cs_instr_is_asynchronous(enum mali_cs_opcode opcode, uint16_t wait_mask)
819 {
820 switch (opcode) {
821 case MALI_CS_OPCODE_FLUSH_CACHE2:
822 case MALI_CS_OPCODE_FINISH_TILING:
823 case MALI_CS_OPCODE_LOAD_MULTIPLE:
824 case MALI_CS_OPCODE_STORE_MULTIPLE:
825 case MALI_CS_OPCODE_RUN_COMPUTE:
826 case MALI_CS_OPCODE_RUN_COMPUTE_INDIRECT:
827 case MALI_CS_OPCODE_RUN_FRAGMENT:
828 case MALI_CS_OPCODE_RUN_FULLSCREEN:
829 #if PAN_ARCH >= 12
830 case MALI_CS_OPCODE_RUN_IDVS2:
831 #else
832 case MALI_CS_OPCODE_RUN_IDVS:
833 #if PAN_ARCH == 10
834 case MALI_CS_OPCODE_RUN_TILING:
835 #endif
836 #endif
837
838 /* Always asynchronous. */
839 return true;
840
841 case MALI_CS_OPCODE_FINISH_FRAGMENT:
842 case MALI_CS_OPCODE_SYNC_ADD32:
843 case MALI_CS_OPCODE_SYNC_SET32:
844 case MALI_CS_OPCODE_SYNC_ADD64:
845 case MALI_CS_OPCODE_SYNC_SET64:
846 case MALI_CS_OPCODE_STORE_STATE:
847 case MALI_CS_OPCODE_TRACE_POINT:
848 case MALI_CS_OPCODE_HEAP_OPERATION:
849 #if PAN_ARCH >= 11
850 case MALI_CS_OPCODE_SHARED_SB_INC:
851 #endif
852 /* Asynchronous only if wait_mask != 0. */
853 return wait_mask != 0;
854
855 default:
856 return false;
857 }
858 }
859
860 /* TODO: was the signal_slot comparison bugged? */
861 #if PAN_ARCH == 10
862 #define cs_apply_async(I, async) \
863 do { \
864 I.wait_mask = async.wait_mask; \
865 I.signal_slot = cs_instr_is_asynchronous(I.opcode, I.wait_mask) \
866 ? async.signal_slot \
867 : 0; \
868 assert(I.signal_slot != 0xff || \
869 !"Can't use cs_now() on pure async instructions"); \
870 } while (0)
871 #else
872 #define cs_apply_async(I, async) \
873 do { \
874 if (async.indirect) { \
875 I.defer_mode = MALI_CS_DEFER_MODE_DEFER_INDIRECT; \
876 } else { \
877 I.defer_mode = MALI_CS_DEFER_MODE_DEFER_IMMEDIATE; \
878 I.wait_mask = async.wait_mask; \
879 I.signal_slot = cs_instr_is_asynchronous(I.opcode, I.wait_mask) \
880 ? async.signal_slot \
881 : 0; \
882 assert(I.signal_slot != 0xff || \
883 !"Can't use cs_now() on pure async instructions"); \
884 } \
885 } while (0)
886 #endif
887
888 static inline void
889 cs_move32_to(struct cs_builder *b, struct cs_index dest, unsigned imm)
890 {
891 cs_emit(b, MOVE32, I) {
892 I.destination = cs_dst32(b, dest);
893 I.immediate = imm;
894 }
895 }
896
897 static inline void
898 cs_move48_to(struct cs_builder *b, struct cs_index dest, uint64_t imm)
899 {
900 cs_emit(b, MOVE48, I) {
901 I.destination = cs_dst64(b, dest);
902 I.immediate = imm;
903 }
904 }
905
906 static inline void
907 cs_load_ip_to(struct cs_builder *b, struct cs_index dest)
908 {
909 /* If a load_ip instruction is emitted after an if_end(), it flushes the
910 * pending if, causing further cs_else_start() instructions to be invalid.
911 */
912 cs_flush_pending_if(b);
913
914 if (likely(cs_cur_block(b) == NULL)) {
915 if (!cs_reserve_instrs(b, 2))
916 return;
917
918 /* We make IP point to the instruction right after our MOVE. */
919 uint64_t ip =
920 b->cur_chunk.buffer.gpu + (sizeof(uint64_t) * (b->cur_chunk.pos + 1));
921 cs_move48_to(b, dest, ip);
922 } else {
923 cs_move48_to(b, dest, b->blocks.last_load_ip_target);
924 b->blocks.last_load_ip_target =
925 util_dynarray_num_elements(&b->blocks.instrs, uint64_t);
926 }
927 }
928
929 static inline void
930 cs_wait_slots(struct cs_builder *b, unsigned wait_mask)
931 {
932 struct cs_load_store_tracker *ls_tracker = b->cur_ls_tracker;
933 assert(ls_tracker != NULL);
934
935 cs_emit(b, WAIT, I) {
936 I.wait_mask = wait_mask;
937 }
938
939 /* We don't do advanced tracking of cs_defer(), and assume that
940 * load/store will be flushed with an explicit wait on the load/store
941 * scoreboard. */
942 if (wait_mask & BITFIELD_BIT(b->conf.ls_sb_slot)) {
943 BITSET_CLEAR_RANGE(ls_tracker->pending_loads, 0, 255);
944 ls_tracker->pending_stores = false;
945 }
946 }
947
948 static inline void
949 cs_wait_slot(struct cs_builder *b, unsigned slot)
950 {
951 assert(slot < CS_MAX_SB_COUNT && "invalid slot");
952
953 cs_wait_slots(b, BITFIELD_BIT(slot));
954 }
955
956 static inline void
957 cs_flush_load_to(struct cs_builder *b, struct cs_index to, uint16_t mask)
958 {
959 struct cs_load_store_tracker *ls_tracker = b->cur_ls_tracker;
960 assert(ls_tracker != NULL);
961
962 unsigned count = util_last_bit(mask);
963 unsigned reg = cs_to_reg_tuple(to, count);
964
965 for (unsigned i = reg; i < reg + count; i++) {
966 if ((mask & BITFIELD_BIT(i - reg)) &&
967 BITSET_TEST(ls_tracker->pending_loads, i)) {
968 cs_wait_slots(b, BITFIELD_BIT(b->conf.ls_sb_slot));
969 break;
970 }
971 }
972 }
973
974 static inline void
975 cs_flush_loads(struct cs_builder *b)
976 {
977 struct cs_load_store_tracker *ls_tracker = b->cur_ls_tracker;
978 assert(ls_tracker != NULL);
979
980 if (!BITSET_IS_EMPTY(ls_tracker->pending_loads))
981 cs_wait_slots(b, BITFIELD_BIT(b->conf.ls_sb_slot));
982 }
983
984 static inline void
985 cs_flush_stores(struct cs_builder *b)
986 {
987 struct cs_load_store_tracker *ls_tracker = b->cur_ls_tracker;
988 assert(ls_tracker != NULL);
989
990 if (ls_tracker->pending_stores)
991 cs_wait_slots(b, BITFIELD_BIT(b->conf.ls_sb_slot));
992 }
993
994 static inline void
995 cs_block_start(struct cs_builder *b, struct cs_block *block)
996 {
997 cs_flush_pending_if(b);
998 block->next = b->blocks.stack;
999 b->blocks.stack = block;
1000 }
1001
1002 static inline void
1003 cs_block_end(struct cs_builder *b, struct cs_block *block)
1004 {
1005 cs_flush_pending_if(b);
1006
1007 assert(cs_cur_block(b) == block);
1008
1009 b->blocks.stack = block->next;
1010
1011 cs_flush_block_instrs(b);
1012 }
1013
1014 static inline void
1015 cs_branch(struct cs_builder *b, int offset, enum mali_cs_condition cond,
1016 struct cs_index val)
1017 {
1018 cs_emit(b, BRANCH, I) {
1019 I.offset = offset;
1020 I.condition = cond;
1021 I.value = cs_src32(b, val);
1022 }
1023 }
1024
1025 static inline void
1026 cs_branch_label_cond32(struct cs_builder *b, struct cs_label *label,
1027 enum mali_cs_condition cond, struct cs_index val)
1028 {
1029 assert(cs_cur_block(b) != NULL);
1030
1031 /* Call cs_src before cs_block_next_pos because cs_src can emit an extra
1032 * WAIT instruction if there is a pending load.
1033 */
1034 uint32_t val_reg = cond != MALI_CS_CONDITION_ALWAYS ? cs_src32(b, val) : 0;
1035
1036 if (label->target == CS_LABEL_INVALID_POS) {
1037 uint32_t branch_ins_pos = cs_block_next_pos(b);
1038
1039 /* Instead of emitting a BRANCH with the final offset, we record the
1040 * diff between the current branch, and the previous branch that was
1041 * referencing this unset label. This way we build a single link list
1042 * that can be walked when the label is set with cs_set_label().
1043 * We use -1 as the end-of-list marker.
1044 */
1045 int16_t offset = -1;
1046 if (label->last_forward_ref != CS_LABEL_INVALID_POS) {
1047 assert(label->last_forward_ref < branch_ins_pos);
1048 assert(branch_ins_pos - label->last_forward_ref <= INT16_MAX);
1049 offset = branch_ins_pos - label->last_forward_ref;
1050 }
1051
1052 cs_emit(b, BRANCH, I) {
1053 I.offset = offset;
1054 I.condition = cond;
1055 I.value = val_reg;
1056 }
1057
1058 label->last_forward_ref = branch_ins_pos;
1059 } else {
1060 int32_t offset = label->target - cs_block_next_pos(b) - 1;
1061
1062 /* The branch target is encoded in a 16-bit signed integer, make sure we
1063 * don't underflow.
1064 */
1065 assert(offset >= INT16_MIN);
1066
1067 /* Backward references are easy, we can emit them immediately. */
1068 cs_emit(b, BRANCH, I) {
1069 I.offset = offset;
1070 I.condition = cond;
1071 I.value = val_reg;
1072 }
1073 }
1074 }
1075
1076 static inline enum mali_cs_condition
1077 cs_invert_cond(enum mali_cs_condition cond)
1078 {
1079 switch (cond) {
1080 case MALI_CS_CONDITION_LEQUAL:
1081 return MALI_CS_CONDITION_GREATER;
1082 case MALI_CS_CONDITION_EQUAL:
1083 return MALI_CS_CONDITION_NEQUAL;
1084 case MALI_CS_CONDITION_LESS:
1085 return MALI_CS_CONDITION_GEQUAL;
1086 case MALI_CS_CONDITION_GREATER:
1087 return MALI_CS_CONDITION_LEQUAL;
1088 case MALI_CS_CONDITION_NEQUAL:
1089 return MALI_CS_CONDITION_EQUAL;
1090 case MALI_CS_CONDITION_GEQUAL:
1091 return MALI_CS_CONDITION_LESS;
1092 case MALI_CS_CONDITION_ALWAYS:
1093 UNREACHABLE("cannot invert ALWAYS");
1094 default:
1095 UNREACHABLE("invalid cond");
1096 }
1097 }
1098
1099 static inline void
1100 cs_branch_label_cond64(struct cs_builder *b, struct cs_label *label,
1101 enum mali_cs_condition cond, struct cs_index val)
1102 {
1103 struct cs_label false_label;
1104 cs_label_init(&false_label);
1105
1106 struct cs_index val_lo = cs_extract32(b, val, 0);
1107 struct cs_index val_hi = cs_extract32(b, val, 1);
1108
1109 switch (cond) {
1110 case MALI_CS_CONDITION_ALWAYS:
1111 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_ALWAYS, cs_undef());
1112 break;
1113 case MALI_CS_CONDITION_LEQUAL:
1114 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_LESS, val_hi);
1115 cs_branch_label_cond32(b, &false_label, MALI_CS_CONDITION_NEQUAL, val_hi);
1116 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_EQUAL, val_lo);
1117 break;
1118 case MALI_CS_CONDITION_GREATER:
1119 cs_branch_label_cond32(b, &false_label, MALI_CS_CONDITION_LESS, val_hi);
1120 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_NEQUAL, val_hi);
1121 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_NEQUAL, val_lo);
1122 break;
1123 case MALI_CS_CONDITION_GEQUAL:
1124 cs_branch_label_cond32(b, &false_label, MALI_CS_CONDITION_LESS, val_hi);
1125 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_ALWAYS, cs_undef());
1126 break;
1127 case MALI_CS_CONDITION_LESS:
1128 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_LESS, val_hi);
1129 break;
1130 case MALI_CS_CONDITION_NEQUAL:
1131 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_NEQUAL, val_lo);
1132 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_NEQUAL, val_hi);
1133 break;
1134 case MALI_CS_CONDITION_EQUAL:
1135 cs_branch_label_cond32(b, &false_label, MALI_CS_CONDITION_NEQUAL, val_lo);
1136 cs_branch_label_cond32(b, label, MALI_CS_CONDITION_EQUAL, val_hi);
1137 break;
1138 default:
1139 UNREACHABLE("unsupported 64bit condition");
1140 }
1141
1142 cs_set_label(b, &false_label);
1143 }
1144
1145 static inline void
1146 cs_branch_label(struct cs_builder *b, struct cs_label *label,
1147 enum mali_cs_condition cond, struct cs_index val)
1148 {
1149 if (val.size == 2) {
1150 cs_branch_label_cond64(b, label, cond, val);
1151 } else {
1152 cs_branch_label_cond32(b, label, cond, val);
1153 }
1154 }
1155
1156 static inline struct cs_if_else *
1157 cs_if_start(struct cs_builder *b, struct cs_if_else *if_else,
1158 enum mali_cs_condition cond, struct cs_index val)
1159 {
1160 cs_block_start(b, &if_else->block);
1161 cs_label_init(&if_else->end_label);
1162 cs_branch_label(b, &if_else->end_label, cs_invert_cond(cond), val);
1163
1164 if_else->orig_ls_state = b->cur_ls_tracker;
1165 if_else->ls_state = *if_else->orig_ls_state;
1166 b->cur_ls_tracker = &if_else->ls_state;
1167
1168 return if_else;
1169 }
1170
1171 static inline void
1172 cs_if_end(struct cs_builder *b, struct cs_if_else *if_else)
1173 {
1174 assert(cs_cur_block(b) == &if_else->block);
1175
1176 b->blocks.pending_if.block.next = if_else->block.next;
1177 b->blocks.stack = &b->blocks.pending_if.block;
1178 b->blocks.pending_if.end_label = if_else->end_label;
1179
1180 b->blocks.pending_if.orig_ls_state = if_else->orig_ls_state;
1181 b->blocks.pending_if.ls_state = if_else->ls_state;
1182
1183 BITSET_OR(if_else->orig_ls_state->pending_loads,
1184 if_else->orig_ls_state->pending_loads,
1185 if_else->ls_state.pending_loads);
1186 if_else->orig_ls_state->pending_stores |= if_else->ls_state.pending_stores;
1187 b->cur_ls_tracker = if_else->orig_ls_state;
1188 }
1189
1190 static inline struct cs_if_else *
1191 cs_else_start(struct cs_builder *b, struct cs_if_else *if_else)
1192 {
1193 assert(cs_cur_block(b) == &b->blocks.pending_if.block);
1194
1195 if_else->block.next = b->blocks.pending_if.block.next;
1196 b->blocks.stack = &if_else->block;
1197 cs_label_init(&if_else->end_label);
1198 cs_branch_label(b, &if_else->end_label, MALI_CS_CONDITION_ALWAYS,
1199 cs_undef());
1200 cs_set_label(b, &b->blocks.pending_if.end_label);
1201 cs_label_init(&b->blocks.pending_if.end_label);
1202
1203 /* Restore the ls_tracker state from before the if block. */
1204 if_else->orig_ls_state = b->blocks.pending_if.orig_ls_state;
1205 if_else->ls_state = *if_else->orig_ls_state;
1206 b->cur_ls_tracker = &if_else->ls_state;
1207
1208 return if_else;
1209 }
1210
1211 static inline void
1212 cs_else_end(struct cs_builder *b, struct cs_if_else *if_else)
1213 {
1214 struct cs_load_store_tracker if_ls_state = b->blocks.pending_if.ls_state;
1215
1216 cs_set_label(b, &if_else->end_label);
1217 cs_block_end(b, &if_else->block);
1218
1219 BITSET_OR(if_else->orig_ls_state->pending_loads, if_ls_state.pending_loads,
1220 if_else->ls_state.pending_loads);
1221 if_else->orig_ls_state->pending_stores =
1222 if_ls_state.pending_stores | if_else->ls_state.pending_stores;
1223 b->cur_ls_tracker = if_else->orig_ls_state;
1224 }
1225
1226 #define cs_if(__b, __cond, __val) \
1227 for (struct cs_if_else __storage, \
1228 *__if_else = cs_if_start(__b, &__storage, __cond, __val); \
1229 __if_else != NULL; cs_if_end(__b, __if_else), __if_else = NULL)
1230
1231 #define cs_else(__b) \
1232 for (struct cs_if_else __storage, \
1233 *__if_else = cs_else_start(__b, &__storage); \
1234 __if_else != NULL; cs_else_end(__b, __if_else), __if_else = NULL)
1235
1236 struct cs_loop {
1237 struct cs_label start, end;
1238 struct cs_block block;
1239 enum mali_cs_condition cond;
1240 struct cs_index val;
1241 struct cs_load_store_tracker *orig_ls_state;
1242 /* On continue we need to compare the original loads to the current ones.
1243 * orig_ls_state can get updated from inside the loop. */
1244 struct cs_load_store_tracker orig_ls_state_copy;
1245 struct cs_load_store_tracker ls_state;
1246 };
1247
1248 static inline void
1249 cs_loop_diverge_ls_update(struct cs_builder *b, struct cs_loop *loop)
1250 {
1251 assert(loop->orig_ls_state);
1252
1253 BITSET_OR(loop->orig_ls_state->pending_loads,
1254 loop->orig_ls_state->pending_loads,
1255 b->cur_ls_tracker->pending_loads);
1256 loop->orig_ls_state->pending_stores |= b->cur_ls_tracker->pending_stores;
1257 }
1258
1259 static inline bool
1260 cs_loop_continue_need_flush(struct cs_builder *b, struct cs_loop *loop)
1261 {
1262 assert(loop->orig_ls_state);
1263
1264 /* We need to flush on continue/loop-again, if there are loads to registers
1265 * that did not already have loads in-flight before the loop.
1266 * Those would have already gotten WAITs inserted because they were marked
1267 * already.
1268 */
1269 BITSET_DECLARE(new_pending_loads, 256);
1270 BITSET_ANDNOT(new_pending_loads, b->cur_ls_tracker->pending_loads,
1271 loop->orig_ls_state_copy.pending_loads);
1272 return !BITSET_IS_EMPTY(new_pending_loads);
1273 }
1274
1275 static inline struct cs_loop *
1276 cs_loop_init(struct cs_builder *b, struct cs_loop *loop,
1277 enum mali_cs_condition cond, struct cs_index val)
1278 {
1279 *loop = (struct cs_loop){
1280 .cond = cond,
1281 .val = val,
1282 };
1283
1284 cs_block_start(b, &loop->block);
1285 cs_label_init(&loop->start);
1286 cs_label_init(&loop->end);
1287 return loop;
1288 }
1289
1290 static inline struct cs_loop *
1291 cs_while_start(struct cs_builder *b, struct cs_loop *loop,
1292 enum mali_cs_condition cond, struct cs_index val)
1293 {
1294 cs_loop_init(b, loop, cond, val);
1295
1296 /* Do an initial check on the condition, and if it's false, jump to
1297 * the end of the loop block. For 'while(true)' loops, skip the
1298 * conditional branch.
1299 */
1300 if (cond != MALI_CS_CONDITION_ALWAYS)
1301 cs_branch_label(b, &loop->end, cs_invert_cond(cond), val);
1302
1303 /* The loops ls tracker only needs to track the actual loop body, not the
1304 * check to skip the whole body. */
1305 loop->orig_ls_state = b->cur_ls_tracker;
1306 loop->orig_ls_state_copy = *loop->orig_ls_state;
1307 loop->ls_state = *loop->orig_ls_state;
1308 b->cur_ls_tracker = &loop->ls_state;
1309
1310 cs_set_label(b, &loop->start);
1311
1312 return loop;
1313 }
1314
1315 static inline void
1316 cs_loop_conditional_continue(struct cs_builder *b, struct cs_loop *loop,
1317 enum mali_cs_condition cond, struct cs_index val)
1318 {
1319 cs_flush_pending_if(b);
1320
1321 if (cs_loop_continue_need_flush(b, loop))
1322 cs_flush_loads(b);
1323
1324 cs_branch_label(b, &loop->start, cond, val);
1325 cs_loop_diverge_ls_update(b, loop);
1326 }
1327
1328 static inline void
1329 cs_loop_conditional_break(struct cs_builder *b, struct cs_loop *loop,
1330 enum mali_cs_condition cond, struct cs_index val)
1331 {
1332 cs_flush_pending_if(b);
1333 cs_branch_label(b, &loop->end, cond, val);
1334 cs_loop_diverge_ls_update(b, loop);
1335 }
1336
1337 static inline void
1338 cs_while_end(struct cs_builder *b, struct cs_loop *loop)
1339 {
1340 cs_flush_pending_if(b);
1341
1342 if (cs_loop_continue_need_flush(b, loop))
1343 cs_flush_loads(b);
1344
1345 cs_branch_label(b, &loop->start, loop->cond, loop->val);
1346 cs_set_label(b, &loop->end);
1347 cs_block_end(b, &loop->block);
1348
1349 if (unlikely(loop->orig_ls_state)) {
1350 BITSET_OR(loop->orig_ls_state->pending_loads,
1351 loop->orig_ls_state->pending_loads,
1352 loop->ls_state.pending_loads);
1353 loop->orig_ls_state->pending_stores |= loop->ls_state.pending_stores;
1354 b->cur_ls_tracker = loop->orig_ls_state;
1355 }
1356 }
1357
1358 #define cs_while(__b, __cond, __val) \
1359 for (struct cs_loop __loop_storage, \
1360 *__loop = cs_while_start(__b, &__loop_storage, __cond, __val); \
1361 __loop != NULL; cs_while_end(__b, __loop), __loop = NULL)
1362
1363 #define cs_continue(__b) \
1364 cs_loop_conditional_continue(__b, __loop, MALI_CS_CONDITION_ALWAYS, \
1365 cs_undef())
1366
1367 #define cs_break(__b) \
1368 cs_loop_conditional_break(__b, __loop, MALI_CS_CONDITION_ALWAYS, cs_undef())
1369
1370 /* cs_maybe is an abstraction for retroactively patching cs contents. When the
1371 * block is closed, its original contents are recorded and then replaced with
1372 * NOP instructions. The caller can then use the cs_patch_maybe to restore the
1373 * original contents at a later point. This can be useful in situations where
1374 * not enough information is available during recording to determine what
1375 * instructions should be emitted at the time, but it will be known at some
1376 * point before submission. */
1377
1378 struct cs_maybe_state {
1379 struct cs_block block;
1380 uint32_t patch_pos;
1381 struct cs_load_store_tracker ls_state;
1382 struct cs_load_store_tracker *orig_ls_state;
1383 };
1384
1385 static inline struct cs_maybe_state *
1386 cs_maybe_start(struct cs_builder *b, struct cs_maybe_state *state)
1387 {
1388 cs_block_start(b, &state->block);
1389 state->patch_pos = cs_block_next_pos(b);
1390
1391 /* store the original ls_tracker state so that we can revert to it after
1392 * the maybe-block */
1393 state->orig_ls_state = b->cur_ls_tracker;
1394 state->ls_state = *b->cur_ls_tracker;
1395 b->cur_ls_tracker = &state->ls_state;
1396
1397 return state;
1398 }
1399
1400 static inline void
1401 cs_maybe_end(struct cs_builder *b, struct cs_maybe_state *state,
1402 struct cs_maybe **maybe)
1403 {
1404 assert(cs_cur_block(b) == &state->block);
1405
1406 /* Flush any new loads and stores */
1407 BITSET_DECLARE(new_loads, 256);
1408 BITSET_ANDNOT(new_loads, b->cur_ls_tracker->pending_loads,
1409 state->orig_ls_state->pending_loads);
1410 bool new_stores =
1411 b->cur_ls_tracker->pending_stores && !state->orig_ls_state->pending_stores;
1412 if (!BITSET_IS_EMPTY(new_loads) || new_stores)
1413 cs_wait_slots(b, BITFIELD_BIT(b->conf.ls_sb_slot));
1414
1415 /* Restore the original ls tracker state */
1416 b->cur_ls_tracker = state->orig_ls_state;
1417
1418 uint32_t num_instrs = cs_block_next_pos(b) - state->patch_pos;
1419 size_t size = num_instrs * sizeof(uint64_t);
1420 uint64_t *instrs = (uint64_t *) b->blocks.instrs.data + state->patch_pos;
1421
1422 if (!b->maybe_ctx)
1423 b->maybe_ctx = ralloc_context(NULL);
1424
1425 *maybe = (struct cs_maybe *)
1426 ralloc_size(b->maybe_ctx, sizeof(struct cs_maybe) + size);
1427 (*maybe)->next_pending = b->blocks.pending_maybes;
1428 b->blocks.pending_maybes = *maybe;
1429 (*maybe)->patch_pos = state->patch_pos;
1430 (*maybe)->num_instrs = num_instrs;
1431 /* patch_addr will be computed later in cs_flush_block_instrs, when the
1432 * outermost block is closed */
1433 (*maybe)->patch_addr = NULL;
1434
1435 /* Save the emitted instructions in the patch block */
1436 memcpy((*maybe)->instrs, instrs, size);
1437 /* Replace instructions in the patch block with NOPs */
1438 memset(instrs, 0, size);
1439
1440 cs_block_end(b, &state->block);
1441 }
1442
1443 #define cs_maybe(__b, __maybe) \
1444 for (struct cs_maybe_state __storage, \
1445 *__state = cs_maybe_start(__b, &__storage); \
1446 __state != NULL; cs_maybe_end(__b, __state, __maybe), \
1447 __state = NULL)
1448
1449 /* Must be called before cs_end */
1450 static inline void
1451 cs_patch_maybe(struct cs_builder *b, struct cs_maybe *maybe)
1452 {
1453 if (maybe->patch_addr) {
1454 /* Called after outer block was closed */
1455 memcpy(maybe->patch_addr, maybe->instrs,
1456 maybe->num_instrs * sizeof(uint64_t));
1457 } else {
1458 /* Called before outer block was closed */
1459 memcpy((uint64_t *)b->blocks.instrs.data + maybe->patch_pos,
1460 maybe->instrs, maybe->num_instrs * sizeof(uint64_t));
1461 }
1462 }
1463
1464 struct cs_single_link_list_node {
1465 uint64_t next;
1466 };
1467
1468 struct cs_single_link_list {
1469 uint64_t head;
1470 uint64_t tail;
1471 };
1472
1473 /* Pseudoinstructions follow */
1474
1475 static inline void
1476 cs_move64_to(struct cs_builder *b, struct cs_index dest, uint64_t imm)
1477 {
1478 if (imm < (1ull << 48)) {
1479 /* Zero extends */
1480 cs_move48_to(b, dest, imm);
1481 } else {
1482 cs_move32_to(b, cs_extract32(b, dest, 0), imm);
1483 cs_move32_to(b, cs_extract32(b, dest, 1), imm >> 32);
1484 }
1485 }
1486
1487 struct cs_shader_res_sel {
1488 uint8_t srt, fau, spd, tsd;
1489 };
1490
1491 static inline struct cs_shader_res_sel
1492 cs_shader_res_sel(uint8_t srt, uint8_t fau, uint8_t spd, uint8_t tsd)
1493 {
1494 return (struct cs_shader_res_sel){
1495 .srt = srt,
1496 .fau = fau,
1497 .spd = spd,
1498 .tsd = tsd,
1499 };
1500 }
1501
1502 enum cs_res_id {
1503 CS_COMPUTE_RES = BITFIELD_BIT(0),
1504 CS_FRAG_RES = BITFIELD_BIT(1),
1505 CS_TILER_RES = BITFIELD_BIT(2),
1506 CS_IDVS_RES = BITFIELD_BIT(3),
1507 };
1508
1509 static inline void
1510 cs_run_compute(struct cs_builder *b, unsigned task_increment,
1511 enum mali_task_axis task_axis, struct cs_shader_res_sel res_sel)
1512 {
1513 /* Staging regs */
1514 cs_flush_loads(b);
1515
1516 b->req_resource_mask |= CS_COMPUTE_RES;
1517
1518 cs_emit(b, RUN_COMPUTE, I) {
1519 I.task_increment = task_increment;
1520 I.task_axis = task_axis;
1521 #if PAN_ARCH >= 12
1522 I.ep_limit = b->conf.compute_ep_limit;
1523 #else
1524 assert(b->conf.compute_ep_limit == 0);
1525 #endif
1526 I.srt_select = res_sel.srt;
1527 I.spd_select = res_sel.spd;
1528 I.tsd_select = res_sel.tsd;
1529 I.fau_select = res_sel.fau;
1530 }
1531 }
1532
1533 #if PAN_ARCH == 10
1534 static inline void
1535 cs_run_tiling(struct cs_builder *b, uint32_t flags_override,
1536 struct cs_shader_res_sel res_sel)
1537 {
1538 /* Staging regs */
1539 cs_flush_loads(b);
1540
1541 b->req_resource_mask |= CS_TILER_RES;
1542
1543 cs_emit(b, RUN_TILING, I) {
1544 I.flags_override = flags_override;
1545 I.srt_select = res_sel.srt;
1546 I.spd_select = res_sel.spd;
1547 I.tsd_select = res_sel.tsd;
1548 I.fau_select = res_sel.fau;
1549 }
1550 }
1551 #endif
1552
1553 #if PAN_ARCH >= 12
1554 static inline void
1555 cs_run_idvs2(struct cs_builder *b, uint32_t flags_override, bool malloc_enable,
1556 struct cs_index draw_id,
1557 enum mali_idvs_shading_mode vertex_shading_mode)
1558 {
1559 /* Staging regs */
1560 cs_flush_loads(b);
1561
1562 b->req_resource_mask |= CS_IDVS_RES;
1563
1564 cs_emit(b, RUN_IDVS2, I) {
1565 I.flags_override = flags_override;
1566 I.malloc_enable = malloc_enable;
1567 I.vertex_shading_mode = vertex_shading_mode;
1568
1569 if (draw_id.type == CS_INDEX_UNDEF) {
1570 I.draw_id_register_enable = false;
1571 } else {
1572 I.draw_id_register_enable = true;
1573 I.draw_id = cs_src32(b, draw_id);
1574 }
1575 }
1576 }
1577 #else
1578 static inline void
1579 cs_run_idvs(struct cs_builder *b, uint32_t flags_override, bool malloc_enable,
1580 struct cs_shader_res_sel varying_sel,
1581 struct cs_shader_res_sel frag_sel, struct cs_index draw_id)
1582 {
1583 /* Staging regs */
1584 cs_flush_loads(b);
1585
1586 b->req_resource_mask |= CS_IDVS_RES;
1587
1588 cs_emit(b, RUN_IDVS, I) {
1589 I.flags_override = flags_override;
1590 I.malloc_enable = malloc_enable;
1591
1592 if (draw_id.type == CS_INDEX_UNDEF) {
1593 I.draw_id_register_enable = false;
1594 } else {
1595 I.draw_id_register_enable = true;
1596 I.draw_id = cs_src32(b, draw_id);
1597 }
1598
1599 assert(varying_sel.spd == 1);
1600 assert(varying_sel.fau == 0 || varying_sel.fau == 1);
1601 assert(varying_sel.srt == 0 || varying_sel.srt == 1);
1602 assert(varying_sel.tsd == 0 || varying_sel.tsd == 1);
1603 I.varying_fau_select = varying_sel.fau == 1;
1604 I.varying_srt_select = varying_sel.srt == 1;
1605 I.varying_tsd_select = varying_sel.tsd == 1;
1606
1607 assert(frag_sel.spd == 2);
1608 assert(frag_sel.fau == 2);
1609 assert(frag_sel.srt == 2 || frag_sel.srt == 0);
1610 assert(frag_sel.tsd == 2 || frag_sel.tsd == 0);
1611 I.fragment_srt_select = frag_sel.srt == 2;
1612 I.fragment_tsd_select = frag_sel.tsd == 2;
1613 }
1614 }
1615 #endif
1616
1617 static inline void
1618 cs_run_fragment(struct cs_builder *b, bool enable_tem,
1619 enum mali_tile_render_order tile_order)
1620 {
1621 /* Staging regs */
1622 cs_flush_loads(b);
1623
1624 b->req_resource_mask |= CS_FRAG_RES;
1625
1626 cs_emit(b, RUN_FRAGMENT, I) {
1627 I.enable_tem = enable_tem;
1628 I.tile_order = tile_order;
1629 }
1630 }
1631
1632 static inline void
1633 cs_run_fullscreen(struct cs_builder *b, uint32_t flags_override,
1634 struct cs_index dcd)
1635 {
1636 /* Staging regs */
1637 cs_flush_loads(b);
1638
1639 b->req_resource_mask |= CS_TILER_RES;
1640
1641 cs_emit(b, RUN_FULLSCREEN, I) {
1642 I.flags_override = flags_override;
1643 I.dcd = cs_src64(b, dcd);
1644 }
1645 }
1646
1647 static inline void
1648 cs_finish_tiling(struct cs_builder *b)
1649 {
1650 b->req_resource_mask |= CS_TILER_RES;
1651
1652 cs_emit(b, FINISH_TILING, I)
1653 ;
1654 }
1655
1656 static inline void
1657 cs_finish_fragment(struct cs_builder *b, bool increment_frag_completed,
1658 struct cs_index first_free_heap_chunk,
1659 struct cs_index last_free_heap_chunk,
1660 struct cs_async_op async)
1661 {
1662 cs_emit(b, FINISH_FRAGMENT, I) {
1663 I.increment_fragment_completed = increment_frag_completed;
1664 cs_apply_async(I, async);
1665 I.first_heap_chunk = cs_src64(b, first_free_heap_chunk);
1666 I.last_heap_chunk = cs_src64(b, last_free_heap_chunk);
1667 }
1668 }
1669
1670 static inline void
1671 cs_add32(struct cs_builder *b, struct cs_index dest, struct cs_index src,
1672 int32_t imm)
1673 {
1674 cs_emit(b, ADD_IMM32, I) {
1675 I.destination = cs_dst32(b, dest);
1676 I.source = cs_src32(b, src);
1677 I.immediate = imm;
1678 }
1679 }
1680
1681 static inline void
1682 cs_add64(struct cs_builder *b, struct cs_index dest, struct cs_index src,
1683 int32_t imm)
1684 {
1685 cs_emit(b, ADD_IMM64, I) {
1686 I.destination = cs_dst64(b, dest);
1687 I.source = cs_src64(b, src);
1688 I.immediate = imm;
1689 }
1690 }
1691
1692 static inline void
1693 cs_umin32(struct cs_builder *b, struct cs_index dest, struct cs_index src0,
1694 struct cs_index src1)
1695 {
1696 cs_emit(b, UMIN32, I) {
1697 I.destination = cs_dst32(b, dest);
1698 I.source_0 = cs_src32(b, src0);
1699 I.source_1 = cs_src32(b, src1);
1700 }
1701 }
1702
1703 static inline void
1704 cs_move_reg32(struct cs_builder *b, struct cs_index dest, struct cs_index src)
1705 {
1706 #if PAN_ARCH >= 11
1707 cs_emit(b, MOVE_REG32, I) {
1708 I.destination = cs_dst32(b, dest);
1709 I.source = cs_src32(b, src);
1710 }
1711 #else
1712 cs_add32(b, dest, src, 0);
1713 #endif
1714 }
1715
1716 static inline void
1717 cs_move_reg64(struct cs_builder *b, struct cs_index dest, struct cs_index src)
1718 {
1719 cs_move_reg32(b, cs_extract32(b, dest, 0), cs_extract32(b, src, 0));
1720 cs_move_reg32(b, cs_extract32(b, dest, 1), cs_extract32(b, src, 1));
1721 }
1722
1723 #if PAN_ARCH >= 11
1724 static inline void
1725 cs_and32(struct cs_builder *b, struct cs_index dest, struct cs_index src0,
1726 struct cs_index src1)
1727 {
1728 cs_emit(b, AND32, I) {
1729 I.destination = cs_dst32(b, dest);
1730 I.source_0 = cs_src32(b, src0);
1731 I.source_1 = cs_src32(b, src1);
1732 }
1733 }
1734
1735 static inline void
1736 cs_or32(struct cs_builder *b, struct cs_index dest, struct cs_index src0,
1737 struct cs_index src1)
1738 {
1739 cs_emit(b, OR32, I) {
1740 I.destination = cs_dst32(b, dest);
1741 I.source_0 = cs_src32(b, src0);
1742 I.source_1 = cs_src32(b, src1);
1743 }
1744 }
1745
1746 static inline void
1747 cs_xor32(struct cs_builder *b, struct cs_index dest, struct cs_index src0,
1748 struct cs_index src1)
1749 {
1750 cs_emit(b, XOR32, I) {
1751 I.destination = cs_dst32(b, dest);
1752 I.source_0 = cs_src32(b, src0);
1753 I.source_1 = cs_src32(b, src1);
1754 }
1755 }
1756
1757 static inline void
1758 cs_not32(struct cs_builder *b, struct cs_index dest, struct cs_index src)
1759 {
1760 cs_emit(b, NOT32, I) {
1761 I.destination = cs_dst32(b, dest);
1762 I.source = cs_src32(b, src);
1763 }
1764 }
1765
1766 static inline void
1767 cs_bit_set32(struct cs_builder *b, struct cs_index dest, struct cs_index src0,
1768 struct cs_index src1)
1769 {
1770 cs_emit(b, BIT_SET32, I) {
1771 I.destination = cs_dst32(b, dest);
1772 I.source_0 = cs_src32(b, src0);
1773 I.source_1 = cs_src32(b, src1);
1774 }
1775 }
1776
1777 static inline void
1778 cs_bit_clear32(struct cs_builder *b, struct cs_index dest, struct cs_index src0,
1779 struct cs_index src1)
1780 {
1781 cs_emit(b, BIT_CLEAR32, I) {
1782 I.destination = cs_dst32(b, dest);
1783 I.source_0 = cs_src32(b, src0);
1784 I.source_1 = cs_src32(b, src1);
1785 }
1786 }
1787
1788 static inline void
1789 cs_set_state(struct cs_builder *b, enum mali_cs_set_state_type state,
1790 struct cs_index src)
1791 {
1792 cs_emit(b, SET_STATE, I) {
1793 I.state = state;
1794 I.source = cs_src32(b, src);
1795 }
1796 }
1797
1798 static inline void
1799 cs_next_sb_entry(struct cs_builder *b, struct cs_index dest,
1800 enum mali_cs_scoreboard_type sb_type,
1801 enum mali_cs_next_sb_entry_format format)
1802 {
1803 cs_emit(b, NEXT_SB_ENTRY, I) {
1804 I.destination = cs_dst32(b, dest);
1805 I.sb_type = sb_type;
1806 I.format = format;
1807 }
1808 }
1809
1810 /*
1811 * Wait indirectly on a scoreboard (set via SET_STATE.SB_MASK_WAIT)
1812 */
1813 static inline void
1814 cs_wait_indirect(struct cs_builder *b)
1815 {
1816 cs_emit(b, WAIT, I) {
1817 I.wait_mode = MALI_CS_WAIT_MODE_INDIRECT;
1818 }
1819 }
1820 #endif
1821
1822 static inline void
1823 cs_load_to(struct cs_builder *b, struct cs_index dest, struct cs_index address,
1824 unsigned mask, int offset)
1825 {
1826 unsigned count = util_last_bit(mask);
1827 unsigned base_reg = cs_dst_tuple(b, dest, count, mask);
1828
1829 cs_emit(b, LOAD_MULTIPLE, I) {
1830 I.base_register = base_reg;
1831 I.address = cs_src64(b, address);
1832 I.mask = mask;
1833 I.offset = offset;
1834 }
1835
1836 for (unsigned i = 0; i < count; i++) {
1837 if (mask & BITFIELD_BIT(i))
1838 BITSET_SET(b->cur_ls_tracker->pending_loads, base_reg + i);
1839 }
1840 }
1841
1842 static inline void
1843 cs_load32_to(struct cs_builder *b, struct cs_index dest,
1844 struct cs_index address, int offset)
1845 {
1846 cs_load_to(b, dest, address, BITFIELD_MASK(1), offset);
1847 }
1848
1849 static inline void
1850 cs_load64_to(struct cs_builder *b, struct cs_index dest,
1851 struct cs_index address, int offset)
1852 {
1853 cs_load_to(b, dest, address, BITFIELD_MASK(2), offset);
1854 }
1855
1856 static inline void
1857 cs_store(struct cs_builder *b, struct cs_index data, struct cs_index address,
1858 unsigned mask, int offset)
1859 {
1860 unsigned count = util_last_bit(mask);
1861 unsigned base_reg = cs_src_tuple(b, data, count, mask);
1862
1863 cs_emit(b, STORE_MULTIPLE, I) {
1864 I.base_register = base_reg;
1865 I.address = cs_src64(b, address);
1866 I.mask = mask;
1867 I.offset = offset;
1868 }
1869
1870 for (unsigned i = 0; i < count; i++) {
1871 b->cur_ls_tracker->pending_stores |= mask & BITFIELD_BIT(i);
1872 }
1873 }
1874
1875 static inline void
1876 cs_store32(struct cs_builder *b, struct cs_index data, struct cs_index address,
1877 int offset)
1878 {
1879 cs_store(b, data, address, BITFIELD_MASK(1), offset);
1880 }
1881
1882 static inline void
1883 cs_store64(struct cs_builder *b, struct cs_index data, struct cs_index address,
1884 int offset)
1885 {
1886 cs_store(b, data, address, BITFIELD_MASK(2), offset);
1887 }
1888
1889 #if PAN_ARCH < 11
1890 /*
1891 * Select which scoreboard entry will track endpoint tasks and other tasks
1892 * respectively. Pass to cs_wait to wait later.
1893 */
1894 static inline void
1895 cs_set_scoreboard_entry(struct cs_builder *b, unsigned ep, unsigned other)
1896 {
1897 assert(ep < CS_MAX_SB_COUNT && "invalid slot");
1898 assert(other < CS_MAX_SB_COUNT && "invalid slot");
1899
1900 cs_emit(b, SET_SB_ENTRY, I) {
1901 I.endpoint_entry = ep;
1902 I.other_entry = other;
1903 }
1904
1905 /* We assume the load/store scoreboard entry is static to keep things
1906 * simple. */
1907 assert(b->conf.ls_sb_slot == other);
1908 }
1909 #else
1910 static inline void
1911 cs_set_state_imm32(struct cs_builder *b, enum mali_cs_set_state_type state,
1912 unsigned value)
1913 {
1914 cs_emit(b, SET_STATE_IMM32, I) {
1915 I.state = state;
1916 I.value = value;
1917 }
1918
1919 /* We assume the load/store scoreboard entry is static to keep things
1920 * simple. */
1921 if (state == MALI_CS_SET_STATE_TYPE_SB_SEL_OTHER)
1922 assert(b->conf.ls_sb_slot == value);
1923 }
1924 #endif
1925
1926 /*
1927 * Select which scoreboard entry will track endpoint tasks.
1928 * On v10, this also set the "other" SB to the ls_sb_slot passed at config
1929 * time, because there's no way to set those things independently.
1930 * Pass to cs_wait to wait later.
1931 */
1932 static inline void
1933 cs_select_endpoint_sb(struct cs_builder *b, unsigned ep)
1934 {
1935 #if PAN_ARCH == 10
1936 cs_set_scoreboard_entry(b, ep, b->conf.ls_sb_slot);
1937 #else
1938 cs_set_state_imm32(b, MALI_CS_SET_STATE_TYPE_SB_SEL_ENDPOINT, ep);
1939 #endif
1940 }
1941
1942 static inline void
1943 cs_set_exception_handler(struct cs_builder *b,
1944 enum mali_cs_exception_type exception_type,
1945 struct cs_index address, struct cs_index length)
1946 {
1947 cs_emit(b, SET_EXCEPTION_HANDLER, I) {
1948 I.exception_type = exception_type;
1949 I.address = cs_src64(b, address);
1950 I.length = cs_src32(b, length);
1951 }
1952 }
1953
1954 static inline void
1955 cs_call(struct cs_builder *b, struct cs_index address, struct cs_index length)
1956 {
1957 cs_emit(b, CALL, I) {
1958 I.address = cs_src64(b, address);
1959 I.length = cs_src32(b, length);
1960 }
1961 }
1962
1963 static inline void
1964 cs_jump(struct cs_builder *b, struct cs_index address, struct cs_index length)
1965 {
1966 cs_emit(b, JUMP, I) {
1967 I.address = cs_src64(b, address);
1968 I.length = cs_src32(b, length);
1969 }
1970 }
1971
1972 static inline void
1973 cs_req_res(struct cs_builder *b, uint32_t res_mask)
1974 {
1975 cs_emit(b, REQ_RESOURCE, I) {
1976 I.compute = res_mask & CS_COMPUTE_RES;
1977 I.tiler = res_mask & CS_TILER_RES;
1978 I.idvs = res_mask & CS_IDVS_RES;
1979 I.fragment = res_mask & CS_FRAG_RES;
1980 }
1981 }
1982
1983 static inline void
1984 cs_flush_caches(struct cs_builder *b, enum mali_cs_flush_mode l2,
1985 enum mali_cs_flush_mode lsc,
1986 enum mali_cs_other_flush_mode others, struct cs_index flush_id,
1987 struct cs_async_op async)
1988 {
1989 cs_emit(b, FLUSH_CACHE2, I) {
1990 I.l2_flush_mode = l2;
1991 I.lsc_flush_mode = lsc;
1992 I.other_flush_mode = others;
1993 I.latest_flush_id = cs_src32(b, flush_id);
1994 cs_apply_async(I, async);
1995 }
1996 }
1997
1998 #define CS_SYNC_OPS(__cnt_width) \
1999 static inline void cs_sync##__cnt_width##_set( \
2000 struct cs_builder *b, bool propagate_error, \
2001 enum mali_cs_sync_scope scope, struct cs_index val, \
2002 struct cs_index addr, struct cs_async_op async) \
2003 { \
2004 cs_emit(b, SYNC_SET##__cnt_width, I) { \
2005 I.error_propagate = propagate_error; \
2006 I.scope = scope; \
2007 I.data = cs_src##__cnt_width(b, val); \
2008 I.address = cs_src64(b, addr); \
2009 cs_apply_async(I, async); \
2010 } \
2011 } \
2012 \
2013 static inline void cs_sync##__cnt_width##_add( \
2014 struct cs_builder *b, bool propagate_error, \
2015 enum mali_cs_sync_scope scope, struct cs_index val, \
2016 struct cs_index addr, struct cs_async_op async) \
2017 { \
2018 cs_emit(b, SYNC_ADD##__cnt_width, I) { \
2019 I.error_propagate = propagate_error; \
2020 I.scope = scope; \
2021 I.data = cs_src##__cnt_width(b, val); \
2022 I.address = cs_src64(b, addr); \
2023 cs_apply_async(I, async); \
2024 } \
2025 } \
2026 \
2027 static inline void cs_sync##__cnt_width##_wait( \
2028 struct cs_builder *b, bool reject_error, enum mali_cs_condition cond, \
2029 struct cs_index ref, struct cs_index addr) \
2030 { \
2031 assert(cond == MALI_CS_CONDITION_LEQUAL || \
2032 cond == MALI_CS_CONDITION_GREATER); \
2033 cs_emit(b, SYNC_WAIT##__cnt_width, I) { \
2034 I.error_reject = reject_error; \
2035 I.condition = cond; \
2036 I.data = cs_src##__cnt_width(b, ref); \
2037 I.address = cs_src64(b, addr); \
2038 } \
2039 }
2040
2041 CS_SYNC_OPS(32)
2042 CS_SYNC_OPS(64)
2043
2044 static inline void
2045 cs_store_state(struct cs_builder *b, struct cs_index address, int offset,
2046 enum mali_cs_state state, struct cs_async_op async)
2047 {
2048 cs_emit(b, STORE_STATE, I) {
2049 I.offset = offset;
2050 I.state = state;
2051 I.address = cs_src64(b, address);
2052 cs_apply_async(I, async);
2053 }
2054 }
2055
2056 static inline void
2057 cs_prot_region(struct cs_builder *b, unsigned size)
2058 {
2059 cs_emit(b, PROT_REGION, I) {
2060 I.size = size;
2061 }
2062 }
2063
2064 static inline void
2065 cs_run_compute_indirect(struct cs_builder *b, unsigned wg_per_task,
2066 struct cs_shader_res_sel res_sel)
2067 {
2068 /* Staging regs */
2069 cs_flush_loads(b);
2070
2071 b->req_resource_mask |= CS_COMPUTE_RES;
2072
2073 cs_emit(b, RUN_COMPUTE_INDIRECT, I) {
2074 I.workgroups_per_task = wg_per_task;
2075 #if PAN_ARCH >= 12
2076 I.ep_limit = b->conf.compute_ep_limit;
2077 #else
2078 assert(b->conf.compute_ep_limit == 0);
2079 #endif
2080 I.srt_select = res_sel.srt;
2081 I.spd_select = res_sel.spd;
2082 I.tsd_select = res_sel.tsd;
2083 I.fau_select = res_sel.fau;
2084 }
2085 }
2086
2087 static inline void
2088 cs_error_barrier(struct cs_builder *b)
2089 {
2090 cs_emit(b, ERROR_BARRIER, _)
2091 ;
2092 }
2093
2094 static inline void
2095 cs_heap_set(struct cs_builder *b, struct cs_index address)
2096 {
2097 cs_emit(b, HEAP_SET, I) {
2098 I.address = cs_src64(b, address);
2099 }
2100 }
2101
2102 static inline void
2103 cs_heap_operation(struct cs_builder *b, enum mali_cs_heap_operation operation,
2104 struct cs_async_op async)
2105 {
2106 cs_emit(b, HEAP_OPERATION, I) {
2107 I.operation = operation;
2108 cs_apply_async(I, async);
2109 }
2110 }
2111
2112 static inline void
2113 cs_vt_start(struct cs_builder *b, struct cs_async_op async)
2114 {
2115 cs_heap_operation(b, MALI_CS_HEAP_OPERATION_VERTEX_TILER_STARTED, async);
2116 }
2117
2118 static inline void
2119 cs_vt_end(struct cs_builder *b, struct cs_async_op async)
2120 {
2121 cs_heap_operation(b, MALI_CS_HEAP_OPERATION_VERTEX_TILER_COMPLETED, async);
2122 }
2123
2124 static inline void
2125 cs_frag_end(struct cs_builder *b, struct cs_async_op async)
2126 {
2127 cs_heap_operation(b, MALI_CS_HEAP_OPERATION_FRAGMENT_COMPLETED, async);
2128 }
2129
2130 static inline void
2131 cs_trace_point(struct cs_builder *b, struct cs_index regs,
2132 struct cs_async_op async)
2133 {
2134 cs_emit(b, TRACE_POINT, I) {
2135 I.base_register =
2136 cs_src_tuple(b, regs, regs.size, (uint16_t)BITFIELD_MASK(regs.size));
2137 I.register_count = regs.size;
2138 cs_apply_async(I, async);
2139 }
2140 }
2141
2142 struct cs_match {
2143 struct cs_block block;
2144 struct cs_label break_label;
2145 struct cs_block case_block;
2146 struct cs_label next_case_label;
2147 struct cs_index val;
2148 struct cs_index scratch_reg;
2149 struct cs_load_store_tracker case_ls_state;
2150 struct cs_load_store_tracker ls_state;
2151 struct cs_load_store_tracker *orig_ls_state;
2152 bool default_emitted;
2153 };
2154
2155 static inline struct cs_match *
2156 cs_match_start(struct cs_builder *b, struct cs_match *match,
2157 struct cs_index val, struct cs_index scratch_reg)
2158 {
2159 *match = (struct cs_match){
2160 .val = val,
2161 .scratch_reg = scratch_reg,
2162 .orig_ls_state = b->cur_ls_tracker,
2163 };
2164
2165 cs_block_start(b, &match->block);
2166 cs_label_init(&match->break_label);
2167 cs_label_init(&match->next_case_label);
2168
2169 return match;
2170 }
2171
2172 static inline void
2173 cs_match_case_ls_set(struct cs_builder *b, struct cs_match *match)
2174 {
2175 if (unlikely(match->orig_ls_state)) {
2176 match->case_ls_state = *match->orig_ls_state;
2177 b->cur_ls_tracker = &match->case_ls_state;
2178 }
2179 }
2180
2181 static inline void
2182 cs_match_case_ls_get(struct cs_match *match)
2183 {
2184 if (unlikely(match->orig_ls_state)) {
2185 BITSET_OR(match->ls_state.pending_loads,
2186 match->case_ls_state.pending_loads,
2187 match->ls_state.pending_loads);
2188 match->ls_state.pending_stores |= match->case_ls_state.pending_stores;
2189 }
2190 }
2191
2192 static inline void
2193 cs_match_case(struct cs_builder *b, struct cs_match *match, uint32_t id)
2194 {
2195 assert(!match->default_emitted || !"default case must be last");
2196 if (match->next_case_label.last_forward_ref != CS_LABEL_INVALID_POS) {
2197 cs_branch_label(b, &match->break_label, MALI_CS_CONDITION_ALWAYS,
2198 cs_undef());
2199 cs_block_end(b, &match->case_block);
2200 cs_match_case_ls_get(match);
2201 cs_set_label(b, &match->next_case_label);
2202 cs_label_init(&match->next_case_label);
2203 }
2204
2205 if (id)
2206 cs_add32(b, match->scratch_reg, match->val, -id);
2207
2208 cs_branch_label(b, &match->next_case_label, MALI_CS_CONDITION_NEQUAL,
2209 id ? match->scratch_reg : match->val);
2210
2211 cs_match_case_ls_set(b, match);
2212 cs_block_start(b, &match->case_block);
2213 }
2214
2215 static inline void
2216 cs_match_default(struct cs_builder *b, struct cs_match *match)
2217 {
2218 assert(match->next_case_label.last_forward_ref != CS_LABEL_INVALID_POS ||
2219 !"default case requires at least one other case");
2220 cs_branch_label(b, &match->break_label, MALI_CS_CONDITION_ALWAYS,
2221 cs_undef());
2222
2223 if (cs_cur_block(b) == &match->case_block) {
2224 cs_block_end(b, &match->case_block);
2225 cs_match_case_ls_get(match);
2226 }
2227
2228 cs_set_label(b, &match->next_case_label);
2229 cs_label_init(&match->next_case_label);
2230 cs_match_case_ls_set(b, match);
2231 cs_block_start(b, &match->case_block);
2232 match->default_emitted = true;
2233 }
2234
2235 static inline void
2236 cs_match_end(struct cs_builder *b, struct cs_match *match)
2237 {
2238 if (cs_cur_block(b) == &match->case_block) {
2239 cs_match_case_ls_get(match);
2240 cs_block_end(b, &match->case_block);
2241 }
2242
2243 if (unlikely(match->orig_ls_state)) {
2244 if (!match->default_emitted) {
2245 /* If we don't have a default, assume we don't handle all possible cases
2246 * and the match load/store state with the original load/store state.
2247 */
2248 BITSET_OR(match->orig_ls_state->pending_loads,
2249 match->ls_state.pending_loads,
2250 match->orig_ls_state->pending_loads);
2251 match->orig_ls_state->pending_stores |= match->ls_state.pending_stores;
2252 } else {
2253 *match->orig_ls_state = match->ls_state;
2254 }
2255
2256 b->cur_ls_tracker = match->orig_ls_state;
2257 }
2258
2259 cs_set_label(b, &match->next_case_label);
2260 cs_set_label(b, &match->break_label);
2261
2262 cs_block_end(b, &match->block);
2263 }
2264
2265 #define cs_match(__b, __val, __scratch) \
2266 for (struct cs_match __match_storage, \
2267 *__match = cs_match_start(__b, &__match_storage, __val, __scratch); \
2268 __match != NULL; cs_match_end(__b, &__match_storage), __match = NULL)
2269
2270 #define cs_case(__b, __ref) \
2271 for (bool __case_defined = ({ \
2272 cs_match_case(__b, __match, __ref); \
2273 false; \
2274 }); \
2275 !__case_defined; __case_defined = true)
2276
2277 #define cs_default(__b) \
2278 for (bool __default_defined = ({ \
2279 cs_match_default(__b, __match); \
2280 false; \
2281 }); \
2282 !__default_defined; __default_defined = true)
2283
2284 static inline void
2285 cs_nop(struct cs_builder *b)
2286 {
2287 cs_emit(b, NOP, I)
2288 ;
2289 }
2290
2291 struct cs_function_ctx {
2292 struct cs_index ctx_reg;
2293 unsigned dump_addr_offset;
2294 };
2295
2296 struct cs_function {
2297 struct cs_block block;
2298 struct cs_dirty_tracker dirty;
2299 struct cs_function_ctx ctx;
2300 unsigned dump_size;
2301 uint64_t address;
2302 uint32_t length;
2303 };
2304
2305 static inline struct cs_function *
2306 cs_function_start(struct cs_builder *b,
2307 struct cs_function *function,
2308 struct cs_function_ctx ctx)
2309 {
2310 assert(cs_cur_block(b) == NULL);
2311 assert(b->conf.dirty_tracker == NULL);
2312
2313 *function = (struct cs_function){
2314 .ctx = ctx,
2315 };
2316
2317 cs_block_start(b, &function->block);
2318
2319 b->conf.dirty_tracker = &function->dirty;
2320
2321 return function;
2322 }
2323
2324 #define SAVE_RESTORE_MAX_OPS (256 / 16)
2325
2326 static inline void
2327 cs_function_end(struct cs_builder *b,
2328 struct cs_function *function)
2329 {
2330 struct cs_index ranges[SAVE_RESTORE_MAX_OPS];
2331 uint16_t masks[SAVE_RESTORE_MAX_OPS];
2332 unsigned num_ranges = 0;
2333 uint32_t num_instrs =
2334 util_dynarray_num_elements(&b->blocks.instrs, uint64_t);
2335 struct cs_index addr_reg = {
2336 .type = CS_INDEX_REGISTER,
2337 .size = 2,
2338 .reg = (uint8_t) (b->conf.nr_registers - 2),
2339 };
2340
2341 /* Manual cs_block_end() without an instruction flush. We do that to insert
2342 * the preamble without having to move memory in b->blocks.instrs. The flush
2343 * will be done after the preamble has been emitted. */
2344 assert(cs_cur_block(b) == &function->block);
2345 assert(function->block.next == NULL);
2346 b->blocks.stack = NULL;
2347
2348 if (!num_instrs)
2349 return;
2350
2351 /* Try to minimize number of load/store by grouping them */
2352 unsigned nregs = b->conf.nr_registers - b->conf.nr_kernel_registers;
2353 unsigned pos, last = 0;
2354
2355 BITSET_FOREACH_SET(pos, function->dirty.regs, nregs) {
2356 unsigned range = MIN2(nregs - pos, 16);
2357 unsigned word = BITSET_BITWORD(pos);
2358 unsigned bit = pos % BITSET_WORDBITS;
2359 unsigned remaining_bits = BITSET_WORDBITS - bit;
2360
2361 if (pos < last)
2362 continue;
2363
2364 masks[num_ranges] = function->dirty.regs[word] >> bit;
2365 if (remaining_bits < range)
2366 masks[num_ranges] |= function->dirty.regs[word + 1] << remaining_bits;
2367 masks[num_ranges] &= BITFIELD_MASK(range);
2368
2369 ranges[num_ranges] =
2370 cs_reg_tuple(b, pos, util_last_bit(masks[num_ranges]));
2371 num_ranges++;
2372 last = pos + range;
2373 }
2374
2375 function->dump_size = BITSET_COUNT(function->dirty.regs) * sizeof(uint32_t);
2376
2377 /* Make sure the current chunk is able to accommodate the block
2378 * instructions as well as the preamble and postamble.
2379 * Adding 4 instructions (2x wait_slot and the move for the address) as
2380 * the move might actually be translated to two MOVE32 instructions. */
2381 num_instrs += (num_ranges * 2) + 4;
2382
2383 /* Align things on a cache-line in case the buffer contains more than one
2384 * function (64 bytes = 8 instructions). */
2385 uint32_t padded_num_instrs = ALIGN_POT(num_instrs, 8);
2386
2387 if (!cs_reserve_instrs(b, padded_num_instrs))
2388 return;
2389
2390 function->address =
2391 b->cur_chunk.buffer.gpu + (b->cur_chunk.pos * sizeof(uint64_t));
2392
2393 /* Preamble: backup modified registers */
2394 if (num_ranges > 0) {
2395 unsigned offset = 0;
2396
2397 cs_load64_to(b, addr_reg, function->ctx.ctx_reg,
2398 function->ctx.dump_addr_offset);
2399
2400 for (unsigned i = 0; i < num_ranges; ++i) {
2401 unsigned reg_count = util_bitcount(masks[i]);
2402
2403 cs_store(b, ranges[i], addr_reg, masks[i], offset);
2404 offset += reg_count * 4;
2405 }
2406
2407 cs_flush_stores(b);
2408 }
2409
2410 /* Now that the preamble is emitted, we can flush the instructions we have in
2411 * our function block. */
2412 cs_flush_block_instrs(b);
2413
2414 /* Postamble: restore modified registers */
2415 if (num_ranges > 0) {
2416 unsigned offset = 0;
2417
2418 cs_load64_to(b, addr_reg, function->ctx.ctx_reg,
2419 function->ctx.dump_addr_offset);
2420
2421 for (unsigned i = 0; i < num_ranges; ++i) {
2422 unsigned reg_count = util_bitcount(masks[i]);
2423
2424 cs_load_to(b, ranges[i], addr_reg, masks[i], offset);
2425 offset += reg_count * 4;
2426 }
2427
2428 cs_flush_loads(b);
2429 }
2430
2431 /* Fill the rest of the buffer with NOPs. */
2432 for (; num_instrs < padded_num_instrs; num_instrs++)
2433 cs_nop(b);
2434
2435 function->length = padded_num_instrs;
2436 }
2437
2438 #define cs_function_def(__b, __function, __ctx) \
2439 for (struct cs_function *__tmp = cs_function_start(__b, __function, __ctx); \
2440 __tmp != NULL; \
2441 cs_function_end(__b, __function), __tmp = NULL)
2442
2443 struct cs_tracing_ctx {
2444 bool enabled;
2445 struct cs_index ctx_reg;
2446 unsigned tracebuf_addr_offset;
2447 };
2448
2449 static inline void
2450 cs_trace_preamble(struct cs_builder *b, const struct cs_tracing_ctx *ctx,
2451 struct cs_index scratch_regs, unsigned trace_size)
2452 {
2453 assert(trace_size > 0 && util_is_aligned(trace_size, 64) &&
2454 trace_size < INT16_MAX);
2455 assert(scratch_regs.size >= 4 && !(scratch_regs.reg & 1));
2456
2457 struct cs_index tracebuf_addr = cs_reg64(b, scratch_regs.reg);
2458
2459 /* We always update the tracebuf position first, so we can easily detect OOB
2460 * access. Use cs_trace_field_offset() to get an offset taking this
2461 * pre-increment into account. */
2462 cs_load64_to(b, tracebuf_addr, ctx->ctx_reg, ctx->tracebuf_addr_offset);
2463 cs_add64(b, tracebuf_addr, tracebuf_addr, trace_size);
2464 cs_store64(b, tracebuf_addr, ctx->ctx_reg, ctx->tracebuf_addr_offset);
2465 cs_flush_stores(b);
2466 }
2467
2468 #define cs_trace_field_offset(__type, __field) \
2469 (int16_t)(offsetof(struct cs_##__type##_trace, __field) - \
2470 sizeof(struct cs_##__type##_trace))
2471
2472 struct cs_run_fragment_trace {
2473 uint64_t ip;
2474 uint32_t sr[7];
2475 } __attribute__((aligned(64)));
2476
2477 static inline void
2478 cs_trace_run_fragment(struct cs_builder *b, const struct cs_tracing_ctx *ctx,
2479 struct cs_index scratch_regs, bool enable_tem,
2480 enum mali_tile_render_order tile_order)
2481 {
2482 if (likely(!ctx->enabled)) {
2483 cs_run_fragment(b, enable_tem, tile_order);
2484 return;
2485 }
2486
2487 struct cs_index tracebuf_addr = cs_reg64(b, scratch_regs.reg);
2488 struct cs_index data = cs_reg64(b, scratch_regs.reg + 2);
2489
2490 cs_trace_preamble(b, ctx, scratch_regs,
2491 sizeof(struct cs_run_fragment_trace));
2492
2493 /* cs_run_xx() must immediately follow cs_load_ip_to() otherwise the IP
2494 * won't point to the right instruction. */
2495 cs_load_ip_to(b, data);
2496 cs_run_fragment(b, enable_tem, tile_order);
2497 cs_store64(b, data, tracebuf_addr, cs_trace_field_offset(run_fragment, ip));
2498
2499 cs_store(b, cs_reg_tuple(b, 40, 7), tracebuf_addr, BITFIELD_MASK(7),
2500 cs_trace_field_offset(run_fragment, sr));
2501 cs_flush_stores(b);
2502 }
2503
2504 #if PAN_ARCH >= 13
2505 #define CS_RUN_FULLSCREEN_SR_MASK \
2506 (BITFIELD64_RANGE(40, 4) | BITFIELD64_RANGE(56, 4) | BITFIELD64_RANGE(61, 3))
2507 #define CS_RUN_FULLSCREEN_SR_COUNT 11
2508 #elif PAN_ARCH >= 11
2509 #define CS_RUN_FULLSCREEN_SR_MASK \
2510 (BITFIELD64_RANGE(40, 4) | BITFIELD64_BIT(56) | BITFIELD64_RANGE(61, 3))
2511 #define CS_RUN_FULLSCREEN_SR_COUNT 8
2512 #else
2513 #define CS_RUN_FULLSCREEN_SR_MASK \
2514 (BITFIELD64_RANGE(40, 4) | BITFIELD64_BIT(56))
2515 #define CS_RUN_FULLSCREEN_SR_COUNT 5
2516 #endif
2517
2518 struct cs_run_fullscreen_trace {
2519 uint64_t ip;
2520 uint64_t dcd;
2521 uint32_t sr[CS_RUN_FULLSCREEN_SR_COUNT];
2522 } __attribute__((aligned(64)));
2523
2524 static inline void
2525 cs_trace_run_fullscreen(struct cs_builder *b, const struct cs_tracing_ctx *ctx,
2526 struct cs_index scratch_regs, uint32_t flags_override,
2527 struct cs_index dcd)
2528 {
2529 if (likely(!ctx->enabled)) {
2530 cs_run_fullscreen(b, flags_override, dcd);
2531 return;
2532 }
2533
2534 struct cs_index tracebuf_addr = cs_reg64(b, scratch_regs.reg);
2535 struct cs_index data = cs_reg64(b, scratch_regs.reg + 2);
2536
2537 cs_trace_preamble(b, ctx, scratch_regs,
2538 sizeof(struct cs_run_fullscreen_trace));
2539
2540 /* cs_run_xx() must immediately follow cs_load_ip_to() otherwise the IP
2541 * won't point to the right instruction. */
2542 cs_load_ip_to(b, data);
2543 cs_run_fullscreen(b, flags_override, dcd);
2544 cs_store64(b, data, tracebuf_addr,
2545 cs_trace_field_offset(run_fullscreen, ip));
2546
2547 cs_store64(b, dcd, tracebuf_addr,
2548 cs_trace_field_offset(run_fullscreen, dcd));
2549
2550 ASSERTED unsigned sr_count = 0;
2551 unsigned sr_offset = cs_trace_field_offset(run_fullscreen, sr);
2552 for (unsigned i = 0; i < 64; i += 16) {
2553 unsigned mask = (CS_RUN_FULLSCREEN_SR_MASK >> i) & BITFIELD_MASK(16);
2554 if (!mask)
2555 continue;
2556
2557 cs_store(b, cs_reg_tuple(b, i, util_last_bit(mask)),
2558 tracebuf_addr, mask, sr_offset);
2559 sr_offset += util_bitcount(mask) * sizeof(uint32_t);
2560 sr_count += util_bitcount(mask);
2561 }
2562 assert(sr_count == CS_RUN_FULLSCREEN_SR_COUNT);
2563
2564 cs_flush_stores(b);
2565 }
2566
2567 #if PAN_ARCH >= 12
2568 struct cs_run_idvs2_trace {
2569 uint64_t ip;
2570 uint32_t draw_id;
2571 uint32_t pad;
2572 uint32_t sr[66];
2573 } __attribute__((aligned(64)));
2574
2575 static inline void
2576 cs_trace_run_idvs2(struct cs_builder *b, const struct cs_tracing_ctx *ctx,
2577 struct cs_index scratch_regs, uint32_t flags_override,
2578 bool malloc_enable, struct cs_index draw_id,
2579 enum mali_idvs_shading_mode vertex_shading_mode)
2580 {
2581 if (likely(!ctx->enabled)) {
2582 cs_run_idvs2(b, flags_override, malloc_enable, draw_id,
2583 vertex_shading_mode);
2584 return;
2585 }
2586
2587 struct cs_index tracebuf_addr = cs_reg64(b, scratch_regs.reg);
2588 struct cs_index data = cs_reg64(b, scratch_regs.reg + 2);
2589
2590 cs_trace_preamble(b, ctx, scratch_regs, sizeof(struct cs_run_idvs2_trace));
2591
2592 /* cs_run_xx() must immediately follow cs_load_ip_to() otherwise the IP
2593 * won't point to the right instruction. */
2594 cs_load_ip_to(b, data);
2595 cs_run_idvs2(b, flags_override, malloc_enable, draw_id, vertex_shading_mode);
2596 cs_store64(b, data, tracebuf_addr, cs_trace_field_offset(run_idvs2, ip));
2597
2598 if (draw_id.type != CS_INDEX_UNDEF)
2599 cs_store32(b, draw_id, tracebuf_addr,
2600 cs_trace_field_offset(run_idvs2, draw_id));
2601
2602 for (unsigned i = 0; i < 64; i += 16)
2603 cs_store(b, cs_reg_tuple(b, i, 16), tracebuf_addr, BITFIELD_MASK(16),
2604 cs_trace_field_offset(run_idvs2, sr[0]) + i * sizeof(uint32_t));
2605 cs_store(b, cs_reg_tuple(b, 64, 2), tracebuf_addr, BITFIELD_MASK(2),
2606 cs_trace_field_offset(run_idvs2, sr[64]));
2607 cs_flush_stores(b);
2608 }
2609 #else
2610 struct cs_run_idvs_trace {
2611 uint64_t ip;
2612 uint32_t draw_id;
2613 uint32_t pad;
2614 uint32_t sr[61];
2615 } __attribute__((aligned(64)));
2616
2617 static inline void
2618 cs_trace_run_idvs(struct cs_builder *b, const struct cs_tracing_ctx *ctx,
2619 struct cs_index scratch_regs, uint32_t flags_override,
2620 bool malloc_enable, struct cs_shader_res_sel varying_sel,
2621 struct cs_shader_res_sel frag_sel, struct cs_index draw_id)
2622 {
2623 if (likely(!ctx->enabled)) {
2624 cs_run_idvs(b, flags_override, malloc_enable, varying_sel, frag_sel,
2625 draw_id);
2626 return;
2627 }
2628
2629 struct cs_index tracebuf_addr = cs_reg64(b, scratch_regs.reg);
2630 struct cs_index data = cs_reg64(b, scratch_regs.reg + 2);
2631
2632 cs_trace_preamble(b, ctx, scratch_regs, sizeof(struct cs_run_idvs_trace));
2633
2634 /* cs_run_xx() must immediately follow cs_load_ip_to() otherwise the IP
2635 * won't point to the right instruction. */
2636 cs_load_ip_to(b, data);
2637 cs_run_idvs(b, flags_override, malloc_enable, varying_sel, frag_sel,
2638 draw_id);
2639 cs_store64(b, data, tracebuf_addr, cs_trace_field_offset(run_idvs, ip));
2640
2641 if (draw_id.type != CS_INDEX_UNDEF)
2642 cs_store32(b, draw_id, tracebuf_addr,
2643 cs_trace_field_offset(run_idvs, draw_id));
2644
2645 for (unsigned i = 0; i < 48; i += 16)
2646 cs_store(b, cs_reg_tuple(b, i, 16), tracebuf_addr, BITFIELD_MASK(16),
2647 cs_trace_field_offset(run_idvs, sr[0]) + i * sizeof(uint32_t));
2648 cs_store(b, cs_reg_tuple(b, 48, 13), tracebuf_addr, BITFIELD_MASK(13),
2649 cs_trace_field_offset(run_idvs, sr[48]));
2650 cs_flush_stores(b);
2651 }
2652 #endif
2653
2654 struct cs_run_compute_trace {
2655 uint64_t ip;
2656 uint32_t sr[40];
2657 } __attribute__((aligned(64)));
2658
2659 static inline void
2660 cs_trace_run_compute(struct cs_builder *b, const struct cs_tracing_ctx *ctx,
2661 struct cs_index scratch_regs, unsigned task_increment,
2662 enum mali_task_axis task_axis,
2663 struct cs_shader_res_sel res_sel)
2664 {
2665 if (likely(!ctx->enabled)) {
2666 cs_run_compute(b, task_increment, task_axis, res_sel);
2667 return;
2668 }
2669
2670 struct cs_index tracebuf_addr = cs_reg64(b, scratch_regs.reg);
2671 struct cs_index data = cs_reg64(b, scratch_regs.reg + 2);
2672
2673 cs_trace_preamble(b, ctx, scratch_regs, sizeof(struct cs_run_compute_trace));
2674
2675 /* cs_run_xx() must immediately follow cs_load_ip_to() otherwise the IP
2676 * won't point to the right instruction. */
2677 cs_load_ip_to(b, data);
2678 cs_run_compute(b, task_increment, task_axis, res_sel);
2679 cs_store64(b, data, tracebuf_addr, cs_trace_field_offset(run_compute, ip));
2680
2681 for (unsigned i = 0; i < 32; i += 16)
2682 cs_store(b, cs_reg_tuple(b, i, 16), tracebuf_addr, BITFIELD_MASK(16),
2683 cs_trace_field_offset(run_compute, sr[0]) + i * sizeof(uint32_t));
2684 cs_store(b, cs_reg_tuple(b, 32, 8), tracebuf_addr, BITFIELD_MASK(8),
2685 cs_trace_field_offset(run_compute, sr[32]));
2686 cs_flush_stores(b);
2687 }
2688
2689 static inline void
2690 cs_trace_run_compute_indirect(struct cs_builder *b,
2691 const struct cs_tracing_ctx *ctx,
2692 struct cs_index scratch_regs,
2693 unsigned wg_per_task,
2694 struct cs_shader_res_sel res_sel)
2695 {
2696 if (likely(!ctx->enabled)) {
2697 cs_run_compute_indirect(b, wg_per_task, res_sel);
2698 return;
2699 }
2700
2701 struct cs_index tracebuf_addr = cs_reg64(b, scratch_regs.reg);
2702 struct cs_index data = cs_reg64(b, scratch_regs.reg + 2);
2703
2704 cs_trace_preamble(b, ctx, scratch_regs, sizeof(struct cs_run_compute_trace));
2705
2706 /* cs_run_xx() must immediately follow cs_load_ip_to() otherwise the IP
2707 * won't point to the right instruction. */
2708 cs_load_ip_to(b, data);
2709 cs_run_compute_indirect(b, wg_per_task, res_sel);
2710 cs_store64(b, data, tracebuf_addr, cs_trace_field_offset(run_compute, ip));
2711
2712 for (unsigned i = 0; i < 32; i += 16)
2713 cs_store(b, cs_reg_tuple(b, i, 16), tracebuf_addr, BITFIELD_MASK(16),
2714 cs_trace_field_offset(run_compute, sr[0]) + i * sizeof(uint32_t));
2715 cs_store(b, cs_reg_tuple(b, 32, 8), tracebuf_addr, BITFIELD_MASK(8),
2716 cs_trace_field_offset(run_compute, sr[32]));
2717 cs_flush_stores(b);
2718 }
2719
2720 #define cs_single_link_list_for_each_from(b, current, node_type, base_name) \
2721 cs_while(b, MALI_CS_CONDITION_NEQUAL, current) \
2722 for (bool done = false; !done; \
2723 cs_load64_to(b, current, current, \
2724 offsetof(node_type, base_name.next)), \
2725 done = true)
2726
2727 /**
2728 * Append an item to a cs_single_link_list.
2729 *
2730 * @param b The current cs_builder.
2731 * @param list_base CS register with the base address for the list pointer.
2732 * @param list_offset Offset added to list_base.
2733 * @param new_node_gpu GPU address of the node to insert.
2734 * @param base_offset Offset of cs_single_link_list_node in the new node.
2735 * @param head_tail Temporary register pair used in the function.
2736 */
2737 static inline void
2738 cs_single_link_list_add_tail(struct cs_builder *b, struct cs_index list_base,
2739 int list_offset, struct cs_index new_node_gpu,
2740 int base_offset, struct cs_index head_tail)
2741 {
2742 assert(head_tail.size == 4);
2743 assert(head_tail.reg % 2 == 0);
2744
2745 STATIC_ASSERT(offsetof(struct cs_single_link_list, tail) ==
2746 offsetof(struct cs_single_link_list, head) + sizeof(uint64_t));
2747 struct cs_index head = cs_reg64(b, head_tail.reg);
2748 struct cs_index tail = cs_reg64(b, head_tail.reg + 2);
2749
2750 /* Offset of the next pointer inside the node pointed to by new_node_gpu. */
2751 const int offset_next =
2752 base_offset + offsetof(struct cs_single_link_list_node, next);
2753
2754 STATIC_ASSERT(offsetof(struct cs_single_link_list, head) == 0);
2755 cs_load_to(b, head_tail, list_base, BITFIELD_MASK(4), list_offset);
2756
2757 /* If the list is empty (head == NULL), set the head, otherwise append to the
2758 * last node. */
2759 cs_if(b, MALI_CS_CONDITION_EQUAL, head)
2760 cs_add64(b, head, new_node_gpu, 0);
2761 cs_else(b)
2762 cs_store64(b, new_node_gpu, tail, offset_next);
2763
2764 cs_add64(b, tail, new_node_gpu, 0);
2765 cs_store(b, head_tail, list_base, BITFIELD_MASK(4), list_offset);
2766 cs_flush_stores(b);
2767 }
2768
2769 #ifdef __cplusplus
2770 }
2771 #endif