panfrost/midgard/disasm: Check for certain tag errors
[mesa.git] / src / panfrost / midgard / disassemble.c
1 /* Author(s):
2 * Connor Abbott
3 * Alyssa Rosenzweig
4 *
5 * Copyright (c) 2013 Connor Abbott (connor@abbott.cx)
6 * Copyright (c) 2018 Alyssa Rosenzweig (alyssa@rosenzweig.io)
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <assert.h>
30 #include <inttypes.h>
31 #include <ctype.h>
32 #include <string.h>
33 #include "midgard.h"
34 #include "midgard-parse.h"
35 #include "midgard_ops.h"
36 #include "disassemble.h"
37 #include "helpers.h"
38 #include "util/half_float.h"
39 #include "util/u_math.h"
40
41 #define DEFINE_CASE(define, str) case define: { printf(str); break; }
42
43 static bool is_instruction_int = false;
44
45 /* Prints a short form of the tag for branching, the minimum needed to be
46 * legible and unambiguous */
47
48 static void
49 print_tag_short(unsigned tag)
50 {
51 switch (midgard_word_types[tag]) {
52 case midgard_word_type_texture:
53 printf("tex/%X", tag);
54 break;
55
56 case midgard_word_type_load_store:
57 printf("ldst");
58 break;
59
60 case midgard_word_type_alu:
61 printf("alu%d/%X", midgard_word_size[tag], tag);
62 break;
63
64 default:
65 printf("%s%X", (tag > 0) ? "" : "unk", tag);
66 break;
67 }
68 }
69
70 static void
71 print_alu_opcode(midgard_alu_op op)
72 {
73 bool int_op = false;
74
75 if (alu_opcode_props[op].name) {
76 printf("%s", alu_opcode_props[op].name);
77
78 int_op = midgard_is_integer_op(op);
79 } else
80 printf("alu_op_%02X", op);
81
82 /* For constant analysis */
83 is_instruction_int = int_op;
84 }
85
86 static void
87 print_ld_st_opcode(midgard_load_store_op op)
88 {
89 if (load_store_opcode_names[op])
90 printf("%s", load_store_opcode_names[op]);
91 else
92 printf("ldst_op_%02X", op);
93 }
94
95 static bool is_embedded_constant_half = false;
96 static bool is_embedded_constant_int = false;
97
98 static char
99 prefix_for_bits(unsigned bits)
100 {
101 switch (bits) {
102 case 8:
103 return 'q';
104 case 16:
105 return 'h';
106 case 64:
107 return 'd';
108 default:
109 return 0;
110 }
111 }
112
113 static void
114 print_reg(unsigned reg, unsigned bits)
115 {
116 /* Perform basic static analysis for expanding constants correctly */
117
118 if (reg == 26) {
119 is_embedded_constant_int = is_instruction_int;
120 is_embedded_constant_half = (bits < 32);
121 }
122
123 char prefix = prefix_for_bits(bits);
124
125 if (prefix)
126 putchar(prefix);
127
128 printf("r%u", reg);
129 }
130
131 static char *outmod_names_float[4] = {
132 "",
133 ".pos",
134 ".unk2",
135 ".sat"
136 };
137
138 static char *outmod_names_int[4] = {
139 ".isat",
140 ".usat",
141 "",
142 ".hi"
143 };
144
145 static char *srcmod_names_int[4] = {
146 "sext(",
147 "zext(",
148 "",
149 "("
150 };
151
152 static void
153 print_outmod(unsigned outmod, bool is_int)
154 {
155 printf("%s", is_int ? outmod_names_int[outmod] :
156 outmod_names_float[outmod]);
157 }
158
159 static void
160 print_quad_word(uint32_t *words, unsigned tabs)
161 {
162 unsigned i;
163
164 for (i = 0; i < 4; i++)
165 printf("0x%08X%s ", words[i], i == 3 ? "" : ",");
166
167 printf("\n");
168 }
169
170 static const char components[16] = "xyzwefghijklmnop";
171
172 /* Helper to print 4 chars of a swizzle */
173 static void
174 print_swizzle_helper(unsigned swizzle, bool upper)
175 {
176 for (unsigned i = 0; i < 4; ++i) {
177 unsigned c = (swizzle >> (i * 2)) & 3;
178 c += upper*4;
179 printf("%c", components[c]);
180 }
181 }
182
183 /* Helper to print 8 chars of a swizzle, duplicating over */
184 static void
185 print_swizzle_helper_8(unsigned swizzle, bool upper)
186 {
187 for (unsigned i = 0; i < 4; ++i) {
188 unsigned c = (swizzle >> (i * 2)) & 3;
189 c *= 2;
190 c += upper*8;
191 printf("%c%c", components[c], components[c+1]);
192 }
193 }
194
195 static void
196 print_swizzle_vec16(unsigned swizzle, bool rep_high, bool rep_low,
197 midgard_dest_override override)
198 {
199 printf(".");
200
201 if (override == midgard_dest_override_upper) {
202 if (rep_high)
203 printf(" /* rep_high */ ");
204 if (rep_low)
205 printf(" /* rep_low */ ");
206
207 if (!rep_high && rep_low)
208 print_swizzle_helper_8(swizzle, true);
209 else
210 print_swizzle_helper_8(swizzle, false);
211 } else {
212 print_swizzle_helper_8(swizzle, rep_high & 1);
213 print_swizzle_helper_8(swizzle, !rep_low & 1);
214 }
215 }
216
217 static void
218 print_swizzle_vec8(unsigned swizzle, bool rep_high, bool rep_low)
219 {
220 printf(".");
221
222 print_swizzle_helper(swizzle, rep_high & 1);
223 print_swizzle_helper(swizzle, !rep_low & 1);
224 }
225
226 static void
227 print_swizzle_vec4(unsigned swizzle, bool rep_high, bool rep_low)
228 {
229 if (rep_high)
230 printf(" /* rep_high */ ");
231 if (rep_low)
232 printf(" /* rep_low */ ");
233
234 if (swizzle == 0xE4) return; /* xyzw */
235
236 printf(".");
237 print_swizzle_helper(swizzle, 0);
238 }
239 static void
240 print_swizzle_vec2(unsigned swizzle, bool rep_high, bool rep_low)
241 {
242 if (rep_high)
243 printf(" /* rep_high */ ");
244 if (rep_low)
245 printf(" /* rep_low */ ");
246
247 if (swizzle == 0xE4) return; /* XY */
248
249 printf(".");
250
251 for (unsigned i = 0; i < 4; i += 2) {
252 unsigned a = (swizzle >> (i * 2)) & 3;
253 unsigned b = (swizzle >> ((i+1) * 2)) & 3;
254
255 /* Normally we're adjacent, but if there's an issue, don't make
256 * it ambiguous */
257
258 if (a & 0x1)
259 printf("[%c%c]", components[a], components[b]);
260 else if (a == b)
261 printf("%c", components[a >> 1]);
262 else if (b == (a + 1))
263 printf("%c", "XY"[a >> 1]);
264 else
265 printf("[%c%c]", components[a], components[b]);
266 }
267 }
268
269 static int
270 bits_for_mode(midgard_reg_mode mode)
271 {
272 switch (mode) {
273 case midgard_reg_mode_8:
274 return 8;
275 case midgard_reg_mode_16:
276 return 16;
277 case midgard_reg_mode_32:
278 return 32;
279 case midgard_reg_mode_64:
280 return 64;
281 default:
282 return 0;
283 }
284 }
285
286 static int
287 bits_for_mode_halved(midgard_reg_mode mode, bool half)
288 {
289 unsigned bits = bits_for_mode(mode);
290
291 if (half)
292 bits >>= 1;
293
294 return bits;
295 }
296
297 static void
298 print_vector_src(unsigned src_binary,
299 midgard_reg_mode mode, unsigned reg,
300 midgard_dest_override override, bool is_int)
301 {
302 midgard_vector_alu_src *src = (midgard_vector_alu_src *)&src_binary;
303
304 /* Modifiers change meaning depending on the op's context */
305
306 midgard_int_mod int_mod = src->mod;
307
308 if (is_int) {
309 printf("%s", srcmod_names_int[int_mod]);
310 } else {
311 if (src->mod & MIDGARD_FLOAT_MOD_NEG)
312 printf("-");
313
314 if (src->mod & MIDGARD_FLOAT_MOD_ABS)
315 printf("abs(");
316 }
317
318 //register
319 unsigned bits = bits_for_mode_halved(mode, src->half);
320 print_reg(reg, bits);
321
322 //swizzle
323 if (bits == 16)
324 print_swizzle_vec8(src->swizzle, src->rep_high, src->rep_low);
325 else if (bits == 8)
326 print_swizzle_vec16(src->swizzle, src->rep_high, src->rep_low, override);
327 else if (bits == 32)
328 print_swizzle_vec4(src->swizzle, src->rep_high, src->rep_low);
329 else if (bits == 64)
330 print_swizzle_vec2(src->swizzle, src->rep_high, src->rep_low);
331
332 /* Since we wrapped with a function-looking thing */
333
334 if (is_int && int_mod == midgard_int_shift)
335 printf(") << %d", bits);
336 else if ((is_int && (int_mod != midgard_int_normal))
337 || (!is_int && src->mod & MIDGARD_FLOAT_MOD_ABS))
338 printf(")");
339 }
340
341 static uint16_t
342 decode_vector_imm(unsigned src2_reg, unsigned imm)
343 {
344 uint16_t ret;
345 ret = src2_reg << 11;
346 ret |= (imm & 0x7) << 8;
347 ret |= (imm >> 3) & 0xFF;
348 return ret;
349 }
350
351 static void
352 print_immediate(uint16_t imm)
353 {
354 if (is_instruction_int)
355 printf("#%d", imm);
356 else
357 printf("#%g", _mesa_half_to_float(imm));
358 }
359
360 static unsigned
361 print_dest(unsigned reg, midgard_reg_mode mode, midgard_dest_override override)
362 {
363 /* Depending on the mode and override, we determine the type of
364 * destination addressed. Absent an override, we address just the
365 * type of the operation itself */
366
367 unsigned bits = bits_for_mode(mode);
368
369 if (override != midgard_dest_override_none)
370 bits /= 2;
371
372 print_reg(reg, bits);
373
374 return bits;
375 }
376
377 static void
378 print_mask_vec16(uint8_t mask, midgard_dest_override override)
379 {
380 printf(".");
381
382 if (override == midgard_dest_override_none) {
383 for (unsigned i = 0; i < 8; i++) {
384 if (mask & (1 << i))
385 printf("%c%c",
386 components[i*2 + 0],
387 components[i*2 + 1]);
388 }
389 } else {
390 bool upper = (override == midgard_dest_override_upper);
391
392 for (unsigned i = 0; i < 8; i++) {
393 if (mask & (1 << i))
394 printf("%c", components[i + (upper ? 8 : 0)]);
395 }
396 }
397 }
398
399 /* For 16-bit+ masks, we read off from the 8-bit mask field. For 16-bit (vec8),
400 * it's just one bit per channel, easy peasy. For 32-bit (vec4), it's one bit
401 * per channel with one duplicate bit in the middle. For 64-bit (vec2), it's
402 * one-bit per channel with _3_ duplicate bits in the middle. Basically, just
403 * subdividing the 128-bit word in 16-bit increments. For 64-bit, we uppercase
404 * the mask to make it obvious what happened */
405
406 static void
407 print_mask(uint8_t mask, unsigned bits, midgard_dest_override override)
408 {
409 if (bits == 8) {
410 print_mask_vec16(mask, override);
411 return;
412 }
413
414 /* Skip 'complete' masks */
415
416 if (bits >= 32 && mask == 0xFF) return;
417
418 if (bits == 16) {
419 if (mask == 0x0F)
420 return;
421 else if (mask == 0xF0) {
422 printf("'");
423 return;
424 }
425 }
426
427 printf(".");
428
429 unsigned skip = (bits / 16);
430 bool uppercase = bits > 32;
431 bool tripped = false;
432
433 for (unsigned i = 0; i < 8; i += skip) {
434 bool a = (mask & (1 << i)) != 0;
435
436 for (unsigned j = 1; j < skip; ++j) {
437 bool dupe = (mask & (1 << (i + j))) != 0;
438 tripped |= (dupe != a);
439 }
440
441 if (a) {
442 char c = components[i / skip];
443
444 if (uppercase)
445 c = toupper(c);
446
447 printf("%c", c);
448 }
449 }
450
451 if (tripped)
452 printf(" /* %X */", mask);
453 }
454
455 /* Prints the 4-bit masks found in texture and load/store ops, as opposed to
456 * the 8-bit masks found in (vector) ALU ops */
457
458 static void
459 print_mask_4(unsigned mask)
460 {
461 if (mask == 0xF) return;
462
463 printf(".");
464
465 for (unsigned i = 0; i < 4; ++i) {
466 bool a = (mask & (1 << i)) != 0;
467 if (a)
468 printf("%c", components[i]);
469 }
470 }
471
472 static void
473 print_vector_field(const char *name, uint16_t *words, uint16_t reg_word,
474 unsigned tabs)
475 {
476 midgard_reg_info *reg_info = (midgard_reg_info *)&reg_word;
477 midgard_vector_alu *alu_field = (midgard_vector_alu *) words;
478 midgard_reg_mode mode = alu_field->reg_mode;
479 unsigned override = alu_field->dest_override;
480
481 /* For now, prefix instruction names with their unit, until we
482 * understand how this works on a deeper level */
483 printf("%s.", name);
484
485 print_alu_opcode(alu_field->op);
486
487 /* Postfix with the size to disambiguate if necessary */
488 char postfix = prefix_for_bits(bits_for_mode(mode));
489 bool size_ambiguous = override != midgard_dest_override_none;
490
491 if (size_ambiguous)
492 printf("%c", postfix ? postfix : 'r');
493
494 /* Print the outmod, if there is one */
495 print_outmod(alu_field->outmod,
496 midgard_is_integer_out_op(alu_field->op));
497
498 printf(" ");
499
500 /* Mask denoting status of 8-lanes */
501 uint8_t mask = alu_field->mask;
502
503 /* First, print the destination */
504 unsigned dest_size =
505 print_dest(reg_info->out_reg, mode, alu_field->dest_override);
506
507 /* Apply the destination override to the mask */
508
509 if (mode == midgard_reg_mode_32 || mode == midgard_reg_mode_64) {
510 if (override == midgard_dest_override_lower)
511 mask &= 0x0F;
512 else if (override == midgard_dest_override_upper)
513 mask &= 0xF0;
514 } else if (mode == midgard_reg_mode_16
515 && override == midgard_dest_override_lower) {
516 /* stub */
517 }
518
519 if (override != midgard_dest_override_none) {
520 bool modeable = (mode != midgard_reg_mode_8);
521 bool known = override != 0x3; /* Unused value */
522
523 if (!(modeable && known))
524 printf("/* do%d */ ", override);
525 }
526
527 print_mask(mask, dest_size, override);
528
529 printf(", ");
530
531 bool is_int = midgard_is_integer_op(alu_field->op);
532 print_vector_src(alu_field->src1, mode, reg_info->src1_reg, override, is_int);
533
534 printf(", ");
535
536 if (reg_info->src2_imm) {
537 uint16_t imm = decode_vector_imm(reg_info->src2_reg, alu_field->src2 >> 2);
538 print_immediate(imm);
539 } else {
540 print_vector_src(alu_field->src2, mode,
541 reg_info->src2_reg, override, is_int);
542 }
543
544 printf("\n");
545 }
546
547 static void
548 print_scalar_src(unsigned src_binary, unsigned reg)
549 {
550 midgard_scalar_alu_src *src = (midgard_scalar_alu_src *)&src_binary;
551
552 if (src->negate)
553 printf("-");
554
555 if (src->abs)
556 printf("abs(");
557
558 print_reg(reg, src->full ? 32 : 16);
559
560 unsigned c = src->component;
561
562 if (src->full) {
563 assert((c & 1) == 0);
564 c >>= 1;
565 }
566
567 printf(".%c", components[c]);
568
569 if (src->abs)
570 printf(")");
571
572 }
573
574 static uint16_t
575 decode_scalar_imm(unsigned src2_reg, unsigned imm)
576 {
577 uint16_t ret;
578 ret = src2_reg << 11;
579 ret |= (imm & 3) << 9;
580 ret |= (imm & 4) << 6;
581 ret |= (imm & 0x38) << 2;
582 ret |= imm >> 6;
583 return ret;
584 }
585
586 static void
587 print_scalar_field(const char *name, uint16_t *words, uint16_t reg_word,
588 unsigned tabs)
589 {
590 midgard_reg_info *reg_info = (midgard_reg_info *)&reg_word;
591 midgard_scalar_alu *alu_field = (midgard_scalar_alu *) words;
592
593 if (alu_field->unknown)
594 printf("scalar ALU unknown bit set\n");
595
596 printf("%s.", name);
597 print_alu_opcode(alu_field->op);
598 print_outmod(alu_field->outmod,
599 midgard_is_integer_out_op(alu_field->op));
600 printf(" ");
601
602 bool full = alu_field->output_full;
603 print_reg(reg_info->out_reg, full ? 32 : 16);
604 unsigned c = alu_field->output_component;
605
606 if (full) {
607 assert((c & 1) == 0);
608 c >>= 1;
609 }
610
611 printf(".%c, ", components[c]);
612
613 print_scalar_src(alu_field->src1, reg_info->src1_reg);
614
615 printf(", ");
616
617 if (reg_info->src2_imm) {
618 uint16_t imm = decode_scalar_imm(reg_info->src2_reg,
619 alu_field->src2);
620 print_immediate(imm);
621 } else
622 print_scalar_src(alu_field->src2, reg_info->src2_reg);
623
624 printf("\n");
625 }
626
627 static void
628 print_branch_op(int op)
629 {
630 switch (op) {
631 case midgard_jmp_writeout_op_branch_uncond:
632 printf("uncond.");
633 break;
634
635 case midgard_jmp_writeout_op_branch_cond:
636 printf("cond.");
637 break;
638
639 case midgard_jmp_writeout_op_writeout:
640 printf("write.");
641 break;
642
643 case midgard_jmp_writeout_op_tilebuffer_pending:
644 printf("tilebuffer.");
645 break;
646
647 case midgard_jmp_writeout_op_discard:
648 printf("discard.");
649 break;
650
651 default:
652 printf("unk%d.", op);
653 break;
654 }
655 }
656
657 static void
658 print_branch_cond(int cond)
659 {
660 switch (cond) {
661 case midgard_condition_write0:
662 printf("write0");
663 break;
664
665 case midgard_condition_false:
666 printf("false");
667 break;
668
669 case midgard_condition_true:
670 printf("true");
671 break;
672
673 case midgard_condition_always:
674 printf("always");
675 break;
676
677 default:
678 printf("unk%X", cond);
679 break;
680 }
681 }
682
683 static void
684 print_compact_branch_writeout_field(uint16_t word)
685 {
686 midgard_jmp_writeout_op op = word & 0x7;
687
688 switch (op) {
689 case midgard_jmp_writeout_op_branch_uncond: {
690 midgard_branch_uncond br_uncond;
691 memcpy((char *) &br_uncond, (char *) &word, sizeof(br_uncond));
692 printf("br.uncond ");
693
694 if (br_uncond.unknown != 1)
695 printf("unknown:%d, ", br_uncond.unknown);
696
697 if (br_uncond.offset >= 0)
698 printf("+");
699
700 printf("%d -> ", br_uncond.offset);
701 print_tag_short(br_uncond.dest_tag);
702 printf("\n");
703
704 break;
705 }
706
707 case midgard_jmp_writeout_op_branch_cond:
708 case midgard_jmp_writeout_op_writeout:
709 case midgard_jmp_writeout_op_discard:
710 default: {
711 midgard_branch_cond br_cond;
712 memcpy((char *) &br_cond, (char *) &word, sizeof(br_cond));
713
714 printf("br.");
715
716 print_branch_op(br_cond.op);
717 print_branch_cond(br_cond.cond);
718
719 printf(" ");
720
721 if (br_cond.offset >= 0)
722 printf("+");
723
724 printf("%d -> ", br_cond.offset);
725 print_tag_short(br_cond.dest_tag);
726 printf("\n");
727
728 break;
729 }
730 }
731 }
732
733 static void
734 print_extended_branch_writeout_field(uint8_t *words)
735 {
736 midgard_branch_extended br;
737 memcpy((char *) &br, (char *) words, sizeof(br));
738
739 printf("brx.");
740
741 print_branch_op(br.op);
742
743 /* Condition repeated 8 times in all known cases. Check this. */
744
745 unsigned cond = br.cond & 0x3;
746
747 for (unsigned i = 0; i < 16; i += 2) {
748 assert(((br.cond >> i) & 0x3) == cond);
749 }
750
751 print_branch_cond(cond);
752
753 if (br.unknown)
754 printf(".unknown%d", br.unknown);
755
756 printf(" ");
757
758 if (br.offset >= 0)
759 printf("+");
760
761 printf("%d -> ", br.offset);
762 print_tag_short(br.dest_tag);
763 printf("\n");
764 }
765
766 static unsigned
767 num_alu_fields_enabled(uint32_t control_word)
768 {
769 unsigned ret = 0;
770
771 if ((control_word >> 17) & 1)
772 ret++;
773
774 if ((control_word >> 19) & 1)
775 ret++;
776
777 if ((control_word >> 21) & 1)
778 ret++;
779
780 if ((control_word >> 23) & 1)
781 ret++;
782
783 if ((control_word >> 25) & 1)
784 ret++;
785
786 return ret;
787 }
788
789 static float
790 float_bitcast(uint32_t integer)
791 {
792 union {
793 uint32_t i;
794 float f;
795 } v;
796
797 v.i = integer;
798 return v.f;
799 }
800
801 static void
802 print_alu_word(uint32_t *words, unsigned num_quad_words,
803 unsigned tabs)
804 {
805 uint32_t control_word = words[0];
806 uint16_t *beginning_ptr = (uint16_t *)(words + 1);
807 unsigned num_fields = num_alu_fields_enabled(control_word);
808 uint16_t *word_ptr = beginning_ptr + num_fields;
809 unsigned num_words = 2 + num_fields;
810
811 if ((control_word >> 16) & 1)
812 printf("unknown bit 16 enabled\n");
813
814 if ((control_word >> 17) & 1) {
815 print_vector_field("vmul", word_ptr, *beginning_ptr, tabs);
816 beginning_ptr += 1;
817 word_ptr += 3;
818 num_words += 3;
819 }
820
821 if ((control_word >> 18) & 1)
822 printf("unknown bit 18 enabled\n");
823
824 if ((control_word >> 19) & 1) {
825 print_scalar_field("sadd", word_ptr, *beginning_ptr, tabs);
826 beginning_ptr += 1;
827 word_ptr += 2;
828 num_words += 2;
829 }
830
831 if ((control_word >> 20) & 1)
832 printf("unknown bit 20 enabled\n");
833
834 if ((control_word >> 21) & 1) {
835 print_vector_field("vadd", word_ptr, *beginning_ptr, tabs);
836 beginning_ptr += 1;
837 word_ptr += 3;
838 num_words += 3;
839 }
840
841 if ((control_word >> 22) & 1)
842 printf("unknown bit 22 enabled\n");
843
844 if ((control_word >> 23) & 1) {
845 print_scalar_field("smul", word_ptr, *beginning_ptr, tabs);
846 beginning_ptr += 1;
847 word_ptr += 2;
848 num_words += 2;
849 }
850
851 if ((control_word >> 24) & 1)
852 printf("unknown bit 24 enabled\n");
853
854 if ((control_word >> 25) & 1) {
855 print_vector_field("lut", word_ptr, *beginning_ptr, tabs);
856 beginning_ptr += 1;
857 word_ptr += 3;
858 num_words += 3;
859 }
860
861 if ((control_word >> 26) & 1) {
862 print_compact_branch_writeout_field(*word_ptr);
863 word_ptr += 1;
864 num_words += 1;
865 }
866
867 if ((control_word >> 27) & 1) {
868 print_extended_branch_writeout_field((uint8_t *) word_ptr);
869 word_ptr += 3;
870 num_words += 3;
871 }
872
873 if (num_quad_words > (num_words + 7) / 8) {
874 assert(num_quad_words == (num_words + 15) / 8);
875 //Assume that the extra quadword is constants
876 void *consts = words + (4 * num_quad_words - 4);
877
878 if (is_embedded_constant_int) {
879 if (is_embedded_constant_half) {
880 int16_t *sconsts = (int16_t *) consts;
881 printf("sconstants %d, %d, %d, %d\n",
882 sconsts[0],
883 sconsts[1],
884 sconsts[2],
885 sconsts[3]);
886 } else {
887 int32_t *iconsts = (int32_t *) consts;
888 printf("iconstants %d, %d, %d, %d\n",
889 iconsts[0],
890 iconsts[1],
891 iconsts[2],
892 iconsts[3]);
893 }
894 } else {
895 if (is_embedded_constant_half) {
896 uint16_t *hconsts = (uint16_t *) consts;
897 printf("hconstants %g, %g, %g, %g\n",
898 _mesa_half_to_float(hconsts[0]),
899 _mesa_half_to_float(hconsts[1]),
900 _mesa_half_to_float(hconsts[2]),
901 _mesa_half_to_float(hconsts[3]));
902 } else {
903 uint32_t *fconsts = (uint32_t *) consts;
904 printf("fconstants %g, %g, %g, %g\n",
905 float_bitcast(fconsts[0]),
906 float_bitcast(fconsts[1]),
907 float_bitcast(fconsts[2]),
908 float_bitcast(fconsts[3]));
909 }
910
911 }
912 }
913 }
914
915 static void
916 print_varying_parameters(midgard_load_store_word *word)
917 {
918 midgard_varying_parameter param;
919 unsigned v = word->varying_parameters;
920 memcpy(&param, &v, sizeof(param));
921
922 if (param.is_varying) {
923 /* If a varying, there are qualifiers */
924 if (param.flat)
925 printf(".flat");
926
927 if (param.interpolation != midgard_interp_default) {
928 if (param.interpolation == midgard_interp_centroid)
929 printf(".centroid");
930 else
931 printf(".interp%d", param.interpolation);
932 }
933
934 if (param.modifier != midgard_varying_mod_none) {
935 if (param.modifier == midgard_varying_mod_perspective_w)
936 printf(".perspectivew");
937 else if (param.modifier == midgard_varying_mod_perspective_z)
938 printf(".perspectivez");
939 else
940 printf(".mod%d", param.modifier);
941 }
942 } else if (param.flat || param.interpolation || param.modifier) {
943 printf(" /* is_varying not set but varying metadata attached */");
944 }
945
946 if (param.zero0 || param.zero1 || param.zero2)
947 printf(" /* zero tripped, %d %d %d */ ", param.zero0, param.zero1, param.zero2);
948 }
949
950 static bool
951 is_op_varying(unsigned op)
952 {
953 switch (op) {
954 case midgard_op_st_vary_16:
955 case midgard_op_st_vary_32:
956 case midgard_op_st_vary_32i:
957 case midgard_op_st_vary_32u:
958 case midgard_op_ld_vary_16:
959 case midgard_op_ld_vary_32:
960 case midgard_op_ld_vary_32i:
961 case midgard_op_ld_vary_32u:
962 return true;
963 }
964
965 return false;
966 }
967
968 static void
969 print_load_store_instr(uint64_t data,
970 unsigned tabs)
971 {
972 midgard_load_store_word *word = (midgard_load_store_word *) &data;
973
974 print_ld_st_opcode(word->op);
975
976 if (is_op_varying(word->op))
977 print_varying_parameters(word);
978
979 printf(" r%d", word->reg);
980 print_mask_4(word->mask);
981
982 int address = word->address;
983
984 if (word->op == midgard_op_ld_uniform_32) {
985 /* Uniforms use their own addressing scheme */
986
987 int lo = word->varying_parameters >> 7;
988 int hi = word->address;
989
990 /* TODO: Combine fields logically */
991 address = (hi << 3) | lo;
992 }
993
994 printf(", %d", address);
995
996 print_swizzle_vec4(word->swizzle, false, false);
997
998 printf(", 0x%X /* %X */\n", word->unknown, word->varying_parameters);
999 }
1000
1001 static void
1002 print_load_store_word(uint32_t *word, unsigned tabs)
1003 {
1004 midgard_load_store *load_store = (midgard_load_store *) word;
1005
1006 if (load_store->word1 != 3) {
1007 print_load_store_instr(load_store->word1, tabs);
1008 }
1009
1010 if (load_store->word2 != 3) {
1011 print_load_store_instr(load_store->word2, tabs);
1012 }
1013 }
1014
1015 static void
1016 print_texture_reg(bool full, bool select, bool upper)
1017 {
1018 if (full)
1019 printf("r%d", REG_TEX_BASE + select);
1020 else
1021 printf("hr%d", (REG_TEX_BASE + select) * 2 + upper);
1022
1023 if (full && upper)
1024 printf("// error: out full / upper mutually exclusive\n");
1025
1026 }
1027
1028 static void
1029 print_texture_reg_triple(unsigned triple)
1030 {
1031 bool full = triple & 1;
1032 bool select = triple & 2;
1033 bool upper = triple & 4;
1034
1035 print_texture_reg(full, select, upper);
1036 }
1037
1038 static void
1039 print_texture_format(int format)
1040 {
1041 /* Act like a modifier */
1042 printf(".");
1043
1044 switch (format) {
1045 DEFINE_CASE(MALI_TEX_1D, "1d");
1046 DEFINE_CASE(MALI_TEX_2D, "2d");
1047 DEFINE_CASE(MALI_TEX_3D, "3d");
1048 DEFINE_CASE(MALI_TEX_CUBE, "cube");
1049
1050 default:
1051 unreachable("Bad format");
1052 }
1053 }
1054
1055 static void
1056 print_texture_op(unsigned op, bool gather)
1057 {
1058 /* Act like a bare name, like ESSL functions */
1059
1060 if (gather) {
1061 printf("textureGather");
1062
1063 unsigned component = op >> 4;
1064 unsigned bottom = op & 0xF;
1065
1066 if (bottom != 0x2)
1067 printf("_unk%d", bottom);
1068
1069 printf(".%c", components[component]);
1070 return;
1071 }
1072
1073 switch (op) {
1074 DEFINE_CASE(TEXTURE_OP_NORMAL, "texture");
1075 DEFINE_CASE(TEXTURE_OP_LOD, "textureLod");
1076 DEFINE_CASE(TEXTURE_OP_TEXEL_FETCH, "texelFetch");
1077
1078 default:
1079 printf("tex_%d", op);
1080 break;
1081 }
1082 }
1083
1084 static bool
1085 texture_op_takes_bias(unsigned op)
1086 {
1087 return op == TEXTURE_OP_NORMAL;
1088 }
1089
1090 static char
1091 sampler_type_name(enum mali_sampler_type t)
1092 {
1093 switch (t) {
1094 case MALI_SAMPLER_FLOAT:
1095 return 'f';
1096 case MALI_SAMPLER_UNSIGNED:
1097 return 'u';
1098 case MALI_SAMPLER_SIGNED:
1099 return 'i';
1100 default:
1101 return '?';
1102 }
1103
1104 }
1105
1106 #undef DEFINE_CASE
1107
1108 static void
1109 print_texture_word(uint32_t *word, unsigned tabs)
1110 {
1111 midgard_texture_word *texture = (midgard_texture_word *) word;
1112
1113 /* Broad category of texture operation in question */
1114 print_texture_op(texture->op, texture->is_gather);
1115
1116 /* Specific format in question */
1117 print_texture_format(texture->format);
1118
1119 assert(texture->zero == 0);
1120
1121 /* Instruction "modifiers" parallel the ALU instructions. */
1122
1123 if (texture->shadow)
1124 printf(".shadow");
1125
1126 if (texture->cont)
1127 printf(".cont");
1128
1129 if (texture->last)
1130 printf(".last");
1131
1132 printf(" ");
1133
1134 print_texture_reg(texture->out_full, texture->out_reg_select, texture->out_upper);
1135 print_mask_4(texture->mask);
1136 printf(", ");
1137
1138 printf("texture%d, ", texture->texture_handle);
1139
1140 /* Print the type, GL style */
1141 printf("%c", sampler_type_name(texture->sampler_type));
1142 printf("sampler%d", texture->sampler_handle);
1143 print_swizzle_vec4(texture->swizzle, false, false);
1144 printf(", ");
1145
1146 print_texture_reg(texture->in_reg_full, texture->in_reg_select, texture->in_reg_upper);
1147 print_swizzle_vec4(texture->in_reg_swizzle, false, false);
1148
1149 /* There is *always* an offset attached. Of
1150 * course, that offset is just immediate #0 for a
1151 * GLES call that doesn't take an offset. If there
1152 * is a non-negative non-zero offset, this is
1153 * specified in immediate offset mode, with the
1154 * values in the offset_* fields as immediates. If
1155 * this is a negative offset, we instead switch to
1156 * a register offset mode, where the offset_*
1157 * fields become register triplets */
1158
1159 if (texture->offset_register) {
1160 printf(" + ");
1161 print_texture_reg_triple(texture->offset_x);
1162
1163 /* The less questions you ask, the better. */
1164
1165 unsigned swizzle_lo, swizzle_hi;
1166 unsigned orig_y = texture->offset_y;
1167 unsigned orig_z = texture->offset_z;
1168
1169 memcpy(&swizzle_lo, &orig_y, sizeof(unsigned));
1170 memcpy(&swizzle_hi, &orig_z, sizeof(unsigned));
1171
1172 /* Duplicate hi swizzle over */
1173 assert(swizzle_hi < 4);
1174 swizzle_hi = (swizzle_hi << 2) | swizzle_hi;
1175
1176 unsigned swiz = (swizzle_lo << 4) | swizzle_hi;
1177 unsigned reversed = util_bitreverse(swiz) >> 24;
1178 print_swizzle_vec4(reversed, false, false);
1179
1180 printf(", ");
1181 } else if (texture->offset_x || texture->offset_y || texture->offset_z) {
1182 /* Only select ops allow negative immediate offsets, verify */
1183
1184 bool neg_x = texture->offset_x < 0;
1185 bool neg_y = texture->offset_y < 0;
1186 bool neg_z = texture->offset_z < 0;
1187 bool any_neg = neg_x || neg_y || neg_z;
1188
1189 if (any_neg && texture->op != TEXTURE_OP_TEXEL_FETCH)
1190 printf("/* invalid negative */ ");
1191
1192 /* Regardless, just print the immediate offset */
1193
1194 printf(" + <%d, %d, %d>, ",
1195 texture->offset_x,
1196 texture->offset_y,
1197 texture->offset_z);
1198 } else {
1199 printf(", ");
1200 }
1201
1202 char lod_operand = texture_op_takes_bias(texture->op) ? '+' : '=';
1203
1204 if (texture->lod_register) {
1205 midgard_tex_register_select sel;
1206 uint8_t raw = texture->bias;
1207 memcpy(&sel, &raw, sizeof(raw));
1208
1209 unsigned c = (sel.component_hi << 1) | sel.component_lo;
1210
1211 printf("lod %c ", lod_operand);
1212 print_texture_reg(sel.full, sel.select, sel.upper);
1213 printf(".%c, ", components[c]);
1214
1215 if (!sel.component_hi)
1216 printf(" /* gradient? */");
1217
1218 if (texture->bias_int)
1219 printf(" /* bias_int = 0x%X */", texture->bias_int);
1220
1221 if (sel.zero)
1222 printf(" /* sel.zero = 0x%X */", sel.zero);
1223 } else if (texture->op == TEXTURE_OP_TEXEL_FETCH) {
1224 /* For texel fetch, the int LOD is in the fractional place and
1225 * there is no fraction / possibility of bias. We *always* have
1226 * an explicit LOD, even if it's zero. */
1227
1228 if (texture->bias_int)
1229 printf(" /* bias_int = 0x%X */ ", texture->bias_int);
1230
1231 printf("lod = %d, ", texture->bias);
1232 } else if (texture->bias || texture->bias_int) {
1233 signed bias_int = texture->bias_int;
1234 float bias_frac = texture->bias / 256.0f;
1235 float bias = bias_int + bias_frac;
1236
1237 bool is_bias = texture_op_takes_bias(texture->op);
1238 char sign = (bias >= 0.0) ? '+' : '-';
1239 char operand = is_bias ? sign : '=';
1240
1241 printf("lod %c %f, ", operand, fabsf(bias));
1242 }
1243
1244 printf("\n");
1245
1246 /* While not zero in general, for these simple instructions the
1247 * following unknowns are zero, so we don't include them */
1248
1249 if (texture->unknown2 ||
1250 texture->unknown4 ||
1251 texture->unknownA ||
1252 texture->unknown8) {
1253 printf("// unknown2 = 0x%x\n", texture->unknown2);
1254 printf("// unknown4 = 0x%x\n", texture->unknown4);
1255 printf("// unknownA = 0x%x\n", texture->unknownA);
1256 printf("// unknown8 = 0x%x\n", texture->unknown8);
1257 }
1258 }
1259
1260 void
1261 disassemble_midgard(uint8_t *code, size_t size)
1262 {
1263 uint32_t *words = (uint32_t *) code;
1264 unsigned num_words = size / 4;
1265 int tabs = 0;
1266
1267 bool prefetch_flag = false;
1268
1269 int last_next_tag = -1;
1270
1271 unsigned i = 0;
1272
1273 while (i < num_words) {
1274 unsigned tag = words[i] & 0xF;
1275 unsigned next_tag = (words[i] >> 4) & 0xF;
1276 unsigned num_quad_words = midgard_word_size[tag];
1277
1278 /* Check the tag */
1279 if (last_next_tag > 1) {
1280 if (last_next_tag != tag) {
1281 printf("/* TAG ERROR got ");
1282 print_tag_short(tag);
1283 printf(" expected ");
1284 print_tag_short(last_next_tag);
1285 printf(" */ ");
1286 }
1287 } else {
1288 /* TODO: Check ALU case */
1289 }
1290
1291 last_next_tag = next_tag;
1292
1293 switch (midgard_word_types[tag]) {
1294 case midgard_word_type_texture:
1295 print_texture_word(&words[i], tabs);
1296 break;
1297
1298 case midgard_word_type_load_store:
1299 print_load_store_word(&words[i], tabs);
1300 break;
1301
1302 case midgard_word_type_alu:
1303 print_alu_word(&words[i], num_quad_words, tabs);
1304
1305 if (prefetch_flag)
1306 return;
1307
1308 /* Reset word static analysis state */
1309 is_embedded_constant_half = false;
1310 is_embedded_constant_int = false;
1311
1312 break;
1313
1314 default:
1315 printf("Unknown word type %u:\n", words[i] & 0xF);
1316 num_quad_words = 1;
1317 print_quad_word(&words[i], tabs);
1318 printf("\n");
1319 break;
1320 }
1321
1322 printf("\n");
1323
1324 unsigned next = (words[i] & 0xF0) >> 4;
1325
1326 i += 4 * num_quad_words;
1327
1328 /* Break based on instruction prefetch flag */
1329
1330 if (i < num_words && next == 1) {
1331 prefetch_flag = true;
1332
1333 if (midgard_word_types[words[i] & 0xF] != midgard_word_type_alu)
1334 return;
1335 }
1336 }
1337
1338 return;
1339 }