vc4: Ignore WADDRs from the other half of the instruction when merging.
[mesa.git] / src / gallium / drivers / vc4 / vc4_qpu_disasm.c
1 /*
2 * Copyright © 2014 Broadcom
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdbool.h>
25 #include <stdio.h>
26
27 #include "vc4_qpu.h"
28 #include "vc4_qpu_defines.h"
29
30 static const char *qpu_add_opcodes[] = {
31 [QPU_A_NOP] = "nop",
32 [QPU_A_FADD] = "fadd",
33 [QPU_A_FSUB] = "fsub",
34 [QPU_A_FMIN] = "fmin",
35 [QPU_A_FMAX] = "fmax",
36 [QPU_A_FMINABS] = "fminabs",
37 [QPU_A_FMAXABS] = "fmaxabs",
38 [QPU_A_FTOI] = "ftoi",
39 [QPU_A_ITOF] = "itof",
40 [QPU_A_ADD] = "add",
41 [QPU_A_SUB] = "sub",
42 [QPU_A_SHR] = "shr",
43 [QPU_A_ASR] = "asr",
44 [QPU_A_ROR] = "ror",
45 [QPU_A_SHL] = "shl",
46 [QPU_A_MIN] = "min",
47 [QPU_A_MAX] = "max",
48 [QPU_A_AND] = "and",
49 [QPU_A_OR] = "or",
50 [QPU_A_XOR] = "xor",
51 [QPU_A_NOT] = "not",
52 [QPU_A_CLZ] = "clz",
53 [QPU_A_V8ADDS] = "v8adds",
54 [QPU_A_V8SUBS] = "v8subs",
55 };
56
57 static const char *qpu_mul_opcodes[] = {
58 [QPU_M_NOP] = "nop",
59 [QPU_M_FMUL] = "fmul",
60 [QPU_M_MUL24] = "mul24",
61 [QPU_M_V8MULD] = "v8muld",
62 [QPU_M_V8MIN] = "v8min",
63 [QPU_M_V8MAX] = "v8max",
64 [QPU_M_V8ADDS] = "v8adds",
65 [QPU_M_V8SUBS] = "v8subs",
66 };
67
68 static const char *qpu_sig[] = {
69 [QPU_SIG_SW_BREAKPOINT] = "sig_brk",
70 [QPU_SIG_NONE] = "",
71 [QPU_SIG_THREAD_SWITCH] = "sig_switch",
72 [QPU_SIG_PROG_END] = "sig_end",
73 [QPU_SIG_WAIT_FOR_SCOREBOARD] = "sig_wait_score",
74 [QPU_SIG_SCOREBOARD_UNLOCK] = "sig_unlock_score",
75 [QPU_SIG_LAST_THREAD_SWITCH] = "sig_thread_switch",
76 [QPU_SIG_COVERAGE_LOAD] = "sig_coverage_load",
77 [QPU_SIG_COLOR_LOAD] = "sig_color_load",
78 [QPU_SIG_COLOR_LOAD_END] = "sig_color_load_end",
79 [QPU_SIG_LOAD_TMU0] = "load_tmu0",
80 [QPU_SIG_LOAD_TMU1] = "load_tmu1",
81 [QPU_SIG_ALPHA_MASK_LOAD] = "sig_alpha_mask_load",
82 [QPU_SIG_SMALL_IMM] = "sig_small_imm",
83 [QPU_SIG_LOAD_IMM] = "sig_load_imm",
84 [QPU_SIG_BRANCH] = "sig_branch",
85 };
86
87 static const char *qpu_pack_mul[] = {
88 [QPU_PACK_MUL_NOP] = "",
89 [QPU_PACK_MUL_8888] = "8888",
90 [QPU_PACK_MUL_8A] = "8a",
91 [QPU_PACK_MUL_8B] = "8b",
92 [QPU_PACK_MUL_8C] = "8c",
93 [QPU_PACK_MUL_8D] = "8d",
94 };
95
96 static const char *qpu_unpack_r4[] = {
97 [QPU_UNPACK_R4_NOP] = "",
98 [QPU_UNPACK_R4_F16A_TO_F32] = "f16a",
99 [QPU_UNPACK_R4_F16B_TO_F32] = "f16b",
100 [QPU_UNPACK_R4_8D_REP] = "8d_rep",
101 [QPU_UNPACK_R4_8A] = "8a",
102 [QPU_UNPACK_R4_8B] = "8b",
103 [QPU_UNPACK_R4_8C] = "8c",
104 [QPU_UNPACK_R4_8D] = "8d",
105 };
106
107 static const char *special_read_a[] = {
108 "uni",
109 NULL,
110 NULL,
111 "vary",
112 NULL,
113 NULL,
114 "elem",
115 "nop",
116 NULL,
117 "x_pix",
118 "ms_flags",
119 NULL,
120 NULL,
121 NULL,
122 NULL,
123 NULL,
124 "vpm_read",
125 "vpm_ld_busy",
126 "vpm_ld_wait",
127 "mutex_acq"
128 };
129
130 static const char *special_read_b[] = {
131 "uni",
132 NULL,
133 NULL,
134 "vary",
135 NULL,
136 NULL,
137 "qpu",
138 "nop",
139 NULL,
140 "y_pix",
141 "rev_flag",
142 NULL,
143 NULL,
144 NULL,
145 NULL,
146 NULL,
147 "vpm_read",
148 "vpm_st_busy",
149 "vpm_st_wait",
150 "mutex_acq"
151 };
152
153 /**
154 * This has the B-file descriptions for register writes.
155 *
156 * Since only a couple of regs are different between A and B, the A overrides
157 * are in get_special_write_desc().
158 */
159 static const char *special_write[] = {
160 [QPU_W_ACC0] = "r0",
161 [QPU_W_ACC1] = "r1",
162 [QPU_W_ACC2] = "r2",
163 [QPU_W_ACC3] = "r3",
164 [QPU_W_TMU_NOSWAP] = "tmu_noswap",
165 [QPU_W_ACC5] = "r5",
166 [QPU_W_HOST_INT] = "host_int",
167 [QPU_W_NOP] = "nop",
168 [QPU_W_UNIFORMS_ADDRESS] = "uniforms_addr",
169 [QPU_W_QUAD_XY] = "quad_y",
170 [QPU_W_MS_FLAGS] = "ms_flags",
171 [QPU_W_TLB_STENCIL_SETUP] = "tlb_stencil_setup",
172 [QPU_W_TLB_Z] = "tlb_z",
173 [QPU_W_TLB_COLOR_MS] = "tlb_color_ms",
174 [QPU_W_TLB_COLOR_ALL] = "tlb_color_all",
175 [QPU_W_VPM] = "vpm",
176 [QPU_W_VPMVCD_SETUP] = "vw_setup",
177 [QPU_W_VPM_ADDR] = "vw_addr",
178 [QPU_W_MUTEX_RELEASE] = "mutex_release",
179 [QPU_W_SFU_RECIP] = "sfu_recip",
180 [QPU_W_SFU_RECIPSQRT] = "sfu_recipsqrt",
181 [QPU_W_SFU_EXP] = "sfu_exp",
182 [QPU_W_SFU_LOG] = "sfu_log",
183 [QPU_W_TMU0_S] = "tmu0_s",
184 [QPU_W_TMU0_T] = "tmu0_t",
185 [QPU_W_TMU0_R] = "tmu0_r",
186 [QPU_W_TMU0_B] = "tmu0_b",
187 [QPU_W_TMU1_S] = "tmu1_s",
188 [QPU_W_TMU1_T] = "tmu1_t",
189 [QPU_W_TMU1_R] = "tmu1_r",
190 [QPU_W_TMU1_B] = "tmu1_b",
191 };
192
193 static const char *qpu_pack_a[] = {
194 [QPU_PACK_A_NOP] = "",
195 [QPU_PACK_A_16A] = ".16a",
196 [QPU_PACK_A_16B] = ".16b",
197 [QPU_PACK_A_8888] = ".8888",
198 [QPU_PACK_A_8A] = ".8a",
199 [QPU_PACK_A_8B] = ".8b",
200 [QPU_PACK_A_8C] = ".8c",
201 [QPU_PACK_A_8D] = ".8d",
202
203 [QPU_PACK_A_32_SAT] = ".sat",
204 [QPU_PACK_A_16A_SAT] = ".16a.sat",
205 [QPU_PACK_A_16B_SAT] = ".16b.sat",
206 [QPU_PACK_A_8888_SAT] = ".8888.sat",
207 [QPU_PACK_A_8A_SAT] = ".8a.sat",
208 [QPU_PACK_A_8B_SAT] = ".8b.sat",
209 [QPU_PACK_A_8C_SAT] = ".8c.sat",
210 [QPU_PACK_A_8D_SAT] = ".8d.sat",
211 };
212
213 static const char *qpu_condflags[] = {
214 [QPU_COND_NEVER] = ".never",
215 [QPU_COND_ALWAYS] = "",
216 [QPU_COND_ZS] = ".zs",
217 [QPU_COND_ZC] = ".zc",
218 [QPU_COND_NS] = ".ns",
219 [QPU_COND_NC] = ".nc",
220 [QPU_COND_CS] = ".cs",
221 [QPU_COND_CC] = ".cc",
222 };
223
224 #define DESC(array, index) \
225 ((index > ARRAY_SIZE(array) || !(array)[index]) ? \
226 "???" : (array)[index])
227
228 static const char *
229 get_special_write_desc(int reg, bool is_a)
230 {
231 if (is_a) {
232 switch (reg) {
233 case QPU_W_QUAD_XY:
234 return "quad_x";
235 case QPU_W_VPMVCD_SETUP:
236 return "vr_setup";
237 case QPU_W_VPM_ADDR:
238 return "vr_addr";
239 }
240 }
241
242 return special_write[reg];
243 }
244
245 static void
246 print_alu_dst(uint64_t inst, bool is_mul)
247 {
248 bool is_a = is_mul == ((inst & QPU_WS) != 0);
249 uint32_t waddr = (is_mul ?
250 QPU_GET_FIELD(inst, QPU_WADDR_MUL) :
251 QPU_GET_FIELD(inst, QPU_WADDR_ADD));
252 const char *file = is_a ? "a" : "b";
253 uint32_t pack = QPU_GET_FIELD(inst, QPU_PACK);
254
255 if (waddr <= 31)
256 fprintf(stderr, "r%s%d", file, waddr);
257 else if (get_special_write_desc(waddr, is_a))
258 fprintf(stderr, "%s", get_special_write_desc(waddr, is_a));
259 else
260 fprintf(stderr, "%s%d?", file, waddr);
261
262 if (is_mul && (inst & QPU_PM)) {
263 fprintf(stderr, ".%s", DESC(qpu_pack_mul, pack));
264 } else if (is_a && !(inst & QPU_PM)) {
265 fprintf(stderr, "%s", DESC(qpu_pack_a, pack));
266 }
267 }
268
269 static void
270 print_alu_src(uint64_t inst, uint32_t mux)
271 {
272 bool is_a = mux != QPU_MUX_B;
273 const char *file = is_a ? "a" : "b";
274 uint32_t raddr = (is_a ?
275 QPU_GET_FIELD(inst, QPU_RADDR_A) :
276 QPU_GET_FIELD(inst, QPU_RADDR_B));
277 uint32_t unpack = QPU_GET_FIELD(inst, QPU_UNPACK);
278
279 if (mux <= QPU_MUX_R5)
280 fprintf(stderr, "r%d", mux);
281 else if (!is_a &&
282 QPU_GET_FIELD(inst, QPU_SIG) == QPU_SIG_SMALL_IMM) {
283 uint32_t si = QPU_GET_FIELD(inst, QPU_SMALL_IMM);
284 if (si <= 15)
285 fprintf(stderr, "%d", si);
286 else if (si <= 31)
287 fprintf(stderr, "%d", -16 + (si - 16));
288 else if (si <= 39)
289 fprintf(stderr, "%.1f", (float)(1 << (si - 32)));
290 else if (si <= 47)
291 fprintf(stderr, "%f", 1.0f / (256 / (si - 39)));
292 else
293 fprintf(stderr, "???");
294 } else if (raddr <= 31)
295 fprintf(stderr, "r%s%d", file, raddr);
296 else {
297 if (is_a)
298 fprintf(stderr, "%s", DESC(special_read_a, raddr - 32));
299 else
300 fprintf(stderr, "%s", DESC(special_read_b, raddr - 32));
301 }
302
303 if (mux == QPU_MUX_R4 && (inst & QPU_PM) &&
304 unpack != QPU_UNPACK_R4_NOP) {
305 fprintf(stderr, ".%s", DESC(qpu_unpack_r4, unpack));
306 }
307 }
308
309 static void
310 print_add_op(uint64_t inst)
311 {
312 uint32_t op_add = QPU_GET_FIELD(inst, QPU_OP_ADD);
313 uint32_t cond = QPU_GET_FIELD(inst, QPU_COND_ADD);
314 bool is_mov = (op_add == QPU_A_OR &&
315 QPU_GET_FIELD(inst, QPU_ADD_A) ==
316 QPU_GET_FIELD(inst, QPU_ADD_B));
317
318 fprintf(stderr, "%s%s%s ",
319 is_mov ? "mov" : DESC(qpu_add_opcodes, op_add),
320 ((inst & QPU_SF) && op_add != QPU_A_NOP) ? ".sf" : "",
321 op_add != QPU_A_NOP ? DESC(qpu_condflags, cond) : "");
322
323 print_alu_dst(inst, false);
324 fprintf(stderr, ", ");
325
326 print_alu_src(inst, QPU_GET_FIELD(inst, QPU_ADD_A));
327
328 if (!is_mov) {
329 fprintf(stderr, ", ");
330
331 print_alu_src(inst, QPU_GET_FIELD(inst, QPU_ADD_B));
332 }
333 }
334
335 static void
336 print_mul_op(uint64_t inst)
337 {
338 uint32_t op_add = QPU_GET_FIELD(inst, QPU_OP_ADD);
339 uint32_t op_mul = QPU_GET_FIELD(inst, QPU_OP_MUL);
340 uint32_t cond = QPU_GET_FIELD(inst, QPU_COND_MUL);
341 bool is_mov = (op_mul == QPU_M_V8MIN &&
342 QPU_GET_FIELD(inst, QPU_MUL_A) ==
343 QPU_GET_FIELD(inst, QPU_MUL_B));
344
345 fprintf(stderr, "%s%s%s ",
346 is_mov ? "mov" : DESC(qpu_mul_opcodes, op_mul),
347 ((inst & QPU_SF) && op_add == QPU_A_NOP) ? ".sf" : "",
348 op_mul != QPU_M_NOP ? DESC(qpu_condflags, cond) : "");
349
350 print_alu_dst(inst, true);
351 fprintf(stderr, ", ");
352
353 print_alu_src(inst, QPU_GET_FIELD(inst, QPU_MUL_A));
354
355 if (!is_mov) {
356 fprintf(stderr, ", ");
357 print_alu_src(inst, QPU_GET_FIELD(inst, QPU_MUL_B));
358 }
359 }
360
361 static void
362 print_load_imm(uint64_t inst)
363 {
364 uint32_t imm = inst;
365 uint32_t waddr_add = QPU_GET_FIELD(inst, QPU_WADDR_ADD);
366 uint32_t waddr_mul = QPU_GET_FIELD(inst, QPU_WADDR_MUL);
367 uint32_t cond_add = QPU_GET_FIELD(inst, QPU_COND_ADD);
368 uint32_t cond_mul = QPU_GET_FIELD(inst, QPU_COND_MUL);
369
370 fprintf(stderr, "load_imm ");
371 print_alu_dst(inst, false);
372 fprintf(stderr, "%s, ", (waddr_add != QPU_W_NOP ?
373 DESC(qpu_condflags, cond_add) : ""));
374 print_alu_dst(inst, true);
375 fprintf(stderr, "%s, ", (waddr_mul != QPU_W_NOP ?
376 DESC(qpu_condflags, cond_mul) : ""));
377 fprintf(stderr, "0x%08x (%f)", imm, uif(imm));
378 }
379
380 void
381 vc4_qpu_disasm(const uint64_t *instructions, int num_instructions)
382 {
383 for (int i = 0; i < num_instructions; i++) {
384 uint64_t inst = instructions[i];
385 uint32_t sig = QPU_GET_FIELD(inst, QPU_SIG);
386
387 switch (sig) {
388 case QPU_SIG_BRANCH:
389 fprintf(stderr, "branch\n");
390 break;
391 case QPU_SIG_LOAD_IMM:
392 print_load_imm(inst);
393 break;
394 default:
395 if (sig != QPU_SIG_NONE)
396 fprintf(stderr, "%s ", DESC(qpu_sig, sig));
397 print_add_op(inst);
398 fprintf(stderr, " ; ");
399 print_mul_op(inst);
400
401 if (num_instructions != 1)
402 fprintf(stderr, "\n");
403 break;
404 }
405 }
406 }