intel: aubinator: print boolean fields to true with colors
[mesa.git] / src / intel / tools / aubinator.c
1 /*
2 * Copyright © 2016 Intel Corporation
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 <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <getopt.h>
28
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <errno.h>
34 #include <inttypes.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <sys/mman.h>
39
40 #include "util/macros.h"
41
42 #include "decoder.h"
43 #include "intel_aub.h"
44 #include "gen_disasm.h"
45
46 /* Below is the only command missing from intel_aub.h in libdrm
47 * So, reuse intel_aub.h from libdrm and #define the
48 * AUB_MI_BATCH_BUFFER_END as below
49 */
50 #define AUB_MI_BATCH_BUFFER_END (0x0500 << 16)
51
52 #define CSI "\e["
53 #define BLUE_HEADER CSI "0;44m"
54 #define GREEN_HEADER CSI "1;42m"
55 #define NORMAL CSI "0m"
56
57 /* options */
58
59 static bool option_full_decode = true;
60 static bool option_print_offsets = true;
61 static enum { COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER } option_color;
62
63 /* state */
64
65 struct gen_disasm *disasm;
66
67 uint64_t gtt_size, gtt_end;
68 void *gtt;
69 uint64_t general_state_base;
70 uint64_t surface_state_base;
71 uint64_t dynamic_state_base;
72 uint64_t instruction_base;
73 uint64_t instruction_bound;
74
75 static inline uint32_t
76 field(uint32_t value, int start, int end)
77 {
78 uint32_t mask;
79
80 mask = ~0U >> (31 - end + start);
81
82 return (value >> start) & mask;
83 }
84
85 struct brw_instruction;
86
87 static inline int
88 valid_offset(uint32_t offset)
89 {
90 return offset < gtt_end;
91 }
92
93 static void
94 print_dword_val(struct gen_field_iterator *iter, uint64_t offset,
95 int *dword_num)
96 {
97 struct gen_field *f;
98
99 f = iter->group->fields[iter->i - 1];
100 const int dword = f->start / 32;
101
102 if (*dword_num != dword) {
103 printf("0x%08"PRIx64": 0x%08x : Dword %d\n",
104 offset + 4 * dword, iter->p[dword], dword);
105 *dword_num = dword;
106 }
107 }
108
109 static char *
110 print_iterator_values(struct gen_field_iterator *iter, int *idx)
111 {
112 char *token = NULL;
113 if (strstr(iter->value, "struct") == NULL) {
114 printf(" %s: %s\n", iter->name, iter->value);
115 } else {
116 token = strtok(iter->value, " ");
117 if (token != NULL) {
118 token = strtok(NULL, " ");
119 *idx = atoi(strtok(NULL, ">"));
120 } else {
121 token = NULL;
122 }
123 printf(" %s:<struct %s>\n", iter->name, token);
124 }
125 return token;
126 }
127
128 static void
129 decode_structure(struct gen_spec *spec, struct gen_group *strct,
130 const uint32_t *p)
131 {
132 struct gen_field_iterator iter;
133 char *token = NULL;
134 int idx = 0, dword_num = 0;
135 uint64_t offset = 0;
136
137 if (option_print_offsets)
138 offset = (void *) p - gtt;
139 else
140 offset = 0;
141
142 gen_field_iterator_init(&iter, strct, p,
143 option_color == COLOR_ALWAYS);
144 while (gen_field_iterator_next(&iter)) {
145 idx = 0;
146 print_dword_val(&iter, offset, &dword_num);
147 token = print_iterator_values(&iter, &idx);
148 if (token != NULL) {
149 struct gen_group *struct_val = gen_spec_find_struct(spec, token);
150 decode_structure(spec, struct_val, &p[idx]);
151 token = NULL;
152 }
153 }
154 }
155
156 static void
157 handle_struct_decode(struct gen_spec *spec, char *struct_name, uint32_t *p)
158 {
159 if (struct_name == NULL)
160 return;
161 struct gen_group *struct_val = gen_spec_find_struct(spec, struct_name);
162 decode_structure(spec, struct_val, p);
163 }
164
165 static void
166 dump_binding_table(struct gen_spec *spec, uint32_t offset)
167 {
168 uint32_t *pointers, i;
169 uint64_t start;
170 struct gen_group *surface_state;
171
172 surface_state = gen_spec_find_struct(spec, "RENDER_SURFACE_STATE");
173 if (surface_state == NULL) {
174 printf("did not find RENDER_SURFACE_STATE info\n");
175 return;
176 }
177
178 start = surface_state_base + offset;
179 pointers = gtt + start;
180 for (i = 0; i < 16; i++) {
181 if (pointers[i] == 0)
182 continue;
183 start = pointers[i] + surface_state_base;
184 if (!valid_offset(start)) {
185 printf("pointer %u: %08x <not valid>\n",
186 i, pointers[i]);
187 continue;
188 } else {
189 printf("pointer %u: %08x\n", i, pointers[i]);
190 }
191
192 decode_structure(spec, surface_state, gtt + start);
193 }
194 }
195
196 static void
197 handle_3dstate_index_buffer(struct gen_spec *spec, uint32_t *p)
198 {
199 void *start;
200 uint32_t length, i, type, size;
201
202 start = gtt + p[2];
203 type = (p[1] >> 8) & 3;
204 size = 1 << type;
205 length = p[4] / size;
206 if (length > 10)
207 length = 10;
208
209 printf("\t");
210
211 for (i = 0; i < length; i++) {
212 switch (type) {
213 case 0:
214 printf("%3d ", ((uint8_t *)start)[i]);
215 break;
216 case 1:
217 printf("%3d ", ((uint16_t *)start)[i]);
218 break;
219 case 2:
220 printf("%3d ", ((uint32_t *)start)[i]);
221 break;
222 }
223 }
224 if (length < p[4] / size)
225 printf("...\n");
226 else
227 printf("\n");
228 }
229
230 static inline uint64_t
231 get_qword(uint32_t *p)
232 {
233 return ((uint64_t) p[1] << 32) | p[0];
234 }
235
236 static void
237 handle_state_base_address(struct gen_spec *spec, uint32_t *p)
238 {
239 uint64_t mask = ~((1 << 12) - 1);
240
241 if (gen_spec_get_gen(spec) >= gen_make_gen(8,0)) {
242 if (p[1] & 1)
243 general_state_base = get_qword(&p[1]) & mask;
244 if (p[4] & 1)
245 surface_state_base = get_qword(&p[4]) & mask;
246 if (p[6] & 1)
247 dynamic_state_base = get_qword(&p[6]) & mask;
248 if (p[10] & 1)
249 instruction_base = get_qword(&p[10]) & mask;
250 if (p[15] & 1)
251 instruction_bound = p[15] & mask;
252 } else {
253 if (p[2] & 1)
254 surface_state_base = p[2] & mask;
255 if (p[3] & 1)
256 dynamic_state_base = p[3] & mask;
257 if (p[5] & 1)
258 instruction_base = p[5] & mask;
259 if (p[9] & 1)
260 instruction_bound = p[9] & mask;
261 }
262 }
263
264 static void
265 dump_samplers(struct gen_spec *spec, uint32_t offset)
266 {
267 uint32_t i;
268 uint64_t start;
269 struct gen_group *sampler_state;
270
271 sampler_state = gen_spec_find_struct(spec, "SAMPLER_STATE");
272
273 start = dynamic_state_base + offset;
274 for (i = 0; i < 4; i++) {
275 printf("sampler state %d\n", i);
276 decode_structure(spec, sampler_state, gtt + start + i * 16);
277 }
278 }
279
280 static void
281 handle_media_interface_descriptor_load(struct gen_spec *spec, uint32_t *p)
282 {
283 int i, length = p[2] / 32;
284 struct gen_group *descriptor_structure;
285 uint32_t *descriptors;
286 uint64_t start;
287 struct brw_instruction *insns;
288
289 descriptor_structure =
290 gen_spec_find_struct(spec, "INTERFACE_DESCRIPTOR_DATA");
291 if (descriptor_structure == NULL) {
292 printf("did not find INTERFACE_DESCRIPTOR_DATA info\n");
293 return;
294 }
295
296 start = dynamic_state_base + p[3];
297 descriptors = gtt + start;
298 for (i = 0; i < length; i++, descriptors += 8) {
299 printf("descriptor %u: %08x\n", i, *descriptors);
300 decode_structure(spec, descriptor_structure, descriptors);
301
302 start = instruction_base + descriptors[0];
303 if (!valid_offset(start)) {
304 printf("kernel: %08"PRIx64" <not valid>\n", start);
305 continue;
306 } else {
307 printf("kernel: %08"PRIx64"\n", start);
308 }
309
310 insns = (struct brw_instruction *) (gtt + start);
311 gen_disasm_disassemble(disasm, insns, 0, stdout);
312
313 dump_samplers(spec, descriptors[3] & ~0x1f);
314 dump_binding_table(spec, descriptors[4] & ~0x1f);
315 }
316 }
317
318 /* Heuristic to determine whether a uint32_t is probably actually a float
319 * (http://stackoverflow.com/a/2953466)
320 */
321
322 static bool
323 probably_float(uint32_t bits)
324 {
325 int exp = ((bits & 0x7f800000U) >> 23) - 127;
326 uint32_t mant = bits & 0x007fffff;
327
328 /* +- 0.0 */
329 if (exp == -127 && mant == 0)
330 return true;
331
332 /* +- 1 billionth to 1 billion */
333 if (-30 <= exp && exp <= 30)
334 return true;
335
336 /* some value with only a few binary digits */
337 if ((mant & 0x0000ffff) == 0)
338 return true;
339
340 return false;
341 }
342
343 static void
344 handle_3dstate_vertex_buffers(struct gen_spec *spec, uint32_t *p)
345 {
346 uint32_t *end, *s, *dw, *dwend;
347 uint64_t offset;
348 int n, i, count, stride;
349
350 end = (p[0] & 0xff) + p + 2;
351 for (s = &p[1], n = 0; s < end; s += 4, n++) {
352 if (gen_spec_get_gen(spec) >= gen_make_gen(8, 0)) {
353 offset = *(uint64_t *) &s[1];
354 dwend = gtt + offset + s[3];
355 } else {
356 offset = s[1];
357 dwend = gtt + s[2] + 1;
358 }
359
360 stride = field(s[0], 0, 11);
361 count = 0;
362 printf("vertex buffer %d, size %d\n", n, s[3]);
363 for (dw = gtt + offset, i = 0; dw < dwend && i < 256; dw++) {
364 if (count == 0 && count % (8 * 4) == 0)
365 printf(" ");
366
367 if (probably_float(*dw))
368 printf(" %8.2f", *(float *) dw);
369 else
370 printf(" 0x%08x", *dw);
371
372 i++;
373 count += 4;
374
375 if (count == stride) {
376 printf("\n");
377 count = 0;
378 } else if (count % (8 * 4) == 0) {
379 printf("\n");
380 } else {
381 printf(" ");
382 }
383 }
384 if (count > 0 && count % (8 * 4) != 0)
385 printf("\n");
386 }
387 }
388
389 static void
390 handle_3dstate_vs(struct gen_spec *spec, uint32_t *p)
391 {
392 uint64_t start;
393 struct brw_instruction *insns;
394 int vs_enable;
395
396 if (gen_spec_get_gen(spec) >= gen_make_gen(8, 0)) {
397 start = get_qword(&p[1]);
398 vs_enable = p[7] & 1;
399 } else {
400 start = p[1];
401 vs_enable = p[5] & 1;
402 }
403
404 if (vs_enable) {
405 printf("instruction_base %08"PRIx64", start %08"PRIx64"\n",
406 instruction_base, start);
407
408 insns = (struct brw_instruction *) (gtt + instruction_base + start);
409 gen_disasm_disassemble(disasm, insns, 0, stdout);
410 }
411 }
412
413 static void
414 handle_3dstate_hs(struct gen_spec *spec, uint32_t *p)
415 {
416 uint64_t start;
417 struct brw_instruction *insns;
418 int hs_enable;
419
420 if (gen_spec_get_gen(spec) >= gen_make_gen(8, 0)) {
421 start = get_qword(&p[4]);
422 } else {
423 start = p[4];
424 }
425
426 hs_enable = p[2] & 0x80000000;
427
428 if (hs_enable) {
429 printf("instruction_base %08"PRIx64", start %08"PRIx64"\n",
430 instruction_base, start);
431
432 insns = (struct brw_instruction *) (gtt + instruction_base + start);
433 gen_disasm_disassemble(disasm, insns, 0, stdout);
434 }
435 }
436
437 static void
438 handle_3dstate_constant(struct gen_spec *spec, uint32_t *p)
439 {
440 int i, j, length;
441 uint32_t *dw;
442 float *f;
443
444 for (i = 0; i < 4; i++) {
445 length = (p[1 + i / 2] >> (i & 1) * 16) & 0xffff;
446 f = (float *) (gtt + p[3 + i * 2] + dynamic_state_base);
447 dw = (uint32_t *) f;
448 for (j = 0; j < length * 8; j++) {
449 if (probably_float(dw[j]))
450 printf(" %04.3f", f[j]);
451 else
452 printf(" 0x%08x", dw[j]);
453
454 if ((j & 7) == 7)
455 printf("\n");
456 }
457 }
458 }
459
460 static void
461 handle_3dstate_ps(struct gen_spec *spec, uint32_t *p)
462 {
463 uint32_t mask = ~((1 << 6) - 1);
464 uint64_t start;
465 struct brw_instruction *insns;
466 static const char unused[] = "unused";
467 static const char *pixel_type[3] = {"8 pixel", "16 pixel", "32 pixel"};
468 const char *k0, *k1, *k2;
469 uint32_t k_mask, k1_offset, k2_offset;
470
471 if (gen_spec_get_gen(spec) >= gen_make_gen(8, 0)) {
472 k_mask = p[6] & 7;
473 k1_offset = 8;
474 k2_offset = 10;
475 } else {
476 k_mask = p[4] & 7;
477 k1_offset = 6;
478 k2_offset = 7;
479 }
480
481 #define DISPATCH_8 1
482 #define DISPATCH_16 2
483 #define DISPATCH_32 4
484
485 switch (k_mask) {
486 case DISPATCH_8:
487 k0 = pixel_type[0];
488 k1 = unused;
489 k2 = unused;
490 break;
491 case DISPATCH_16:
492 k0 = pixel_type[1];
493 k1 = unused;
494 k2 = unused;
495 break;
496 case DISPATCH_8 | DISPATCH_16:
497 k0 = pixel_type[0];
498 k1 = unused;
499 k2 = pixel_type[1];
500 break;
501 case DISPATCH_32:
502 k0 = pixel_type[2];
503 k1 = unused;
504 k2 = unused;
505 break;
506 case DISPATCH_16 | DISPATCH_32:
507 k0 = unused;
508 k1 = pixel_type[2];
509 k2 = pixel_type[1];
510 break;
511 case DISPATCH_8 | DISPATCH_16 | DISPATCH_32:
512 k0 = pixel_type[0];
513 k1 = pixel_type[2];
514 k2 = pixel_type[1];
515 break;
516 default:
517 k0 = unused;
518 k1 = unused;
519 k2 = unused;
520 break;
521 }
522
523 start = instruction_base + (p[1] & mask);
524 printf(" Kernel[0] %s\n", k0);
525 if (k0 != unused) {
526 insns = (struct brw_instruction *) (gtt + start);
527 gen_disasm_disassemble(disasm, insns, 0, stdout);
528 }
529
530 start = instruction_base + (p[k1_offset] & mask);
531 printf(" Kernel[1] %s\n", k1);
532 if (k1 != unused) {
533 insns = (struct brw_instruction *) (gtt + start);
534 gen_disasm_disassemble(disasm, insns, 0, stdout);
535 }
536
537 start = instruction_base + (p[k2_offset] & mask);
538 printf(" Kernel[2] %s\n", k2);
539 if (k2 != unused) {
540 insns = (struct brw_instruction *) (gtt + start);
541 gen_disasm_disassemble(disasm, insns, 0, stdout);
542 }
543 }
544
545 static void
546 handle_3dstate_binding_table_pointers(struct gen_spec *spec, uint32_t *p)
547 {
548 dump_binding_table(spec, p[1]);
549 }
550
551 static void
552 handle_3dstate_sampler_state_pointers(struct gen_spec *spec, uint32_t *p)
553 {
554 dump_samplers(spec, p[1]);
555 }
556
557 static void
558 handle_3dstate_viewport_state_pointers_cc(struct gen_spec *spec, uint32_t *p)
559 {
560 uint64_t start;
561 struct gen_group *cc_viewport;
562
563 cc_viewport = gen_spec_find_struct(spec, "CC_VIEWPORT");
564
565 start = dynamic_state_base + (p[1] & ~0x1fu);
566 for (uint32_t i = 0; i < 4; i++) {
567 printf("viewport %d\n", i);
568 decode_structure(spec, cc_viewport, gtt + start + i * 8);
569 }
570 }
571
572 static void
573 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec *spec,
574 uint32_t *p)
575 {
576 uint64_t start;
577 struct gen_group *sf_clip_viewport;
578
579 sf_clip_viewport = gen_spec_find_struct(spec, "SF_CLIP_VIEWPORT");
580
581 start = dynamic_state_base + (p[1] & ~0x3fu);
582 for (uint32_t i = 0; i < 4; i++) {
583 printf("viewport %d\n", i);
584 decode_structure(spec, sf_clip_viewport, gtt + start + i * 64);
585 }
586 }
587
588 static void
589 handle_3dstate_blend_state_pointers(struct gen_spec *spec, uint32_t *p)
590 {
591 uint64_t start;
592 struct gen_group *blend_state;
593
594 blend_state = gen_spec_find_struct(spec, "BLEND_STATE");
595
596 start = dynamic_state_base + (p[1] & ~0x3fu);
597 decode_structure(spec, blend_state, gtt + start);
598 }
599
600 static void
601 handle_3dstate_cc_state_pointers(struct gen_spec *spec, uint32_t *p)
602 {
603 uint64_t start;
604 struct gen_group *cc_state;
605
606 cc_state = gen_spec_find_struct(spec, "COLOR_CALC_STATE");
607
608 start = dynamic_state_base + (p[1] & ~0x3fu);
609 decode_structure(spec, cc_state, gtt + start);
610 }
611
612 static void
613 handle_3dstate_scissor_state_pointers(struct gen_spec *spec, uint32_t *p)
614 {
615 uint64_t start;
616 struct gen_group *scissor_rect;
617
618 scissor_rect = gen_spec_find_struct(spec, "SCISSOR_RECT");
619
620 start = dynamic_state_base + (p[1] & ~0x1fu);
621 decode_structure(spec, scissor_rect, gtt + start);
622 }
623
624 static void
625 handle_load_register_imm(struct gen_spec *spec, uint32_t *p)
626 {
627 struct gen_group *reg = gen_spec_find_register(spec, p[1]);
628
629 if (reg != NULL) {
630 printf("register %s (0x%x): 0x%x\n",
631 reg->name, reg->register_offset, p[2]);
632 decode_structure(spec, reg, &p[2]);
633 }
634 }
635
636 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
637
638 #define STATE_BASE_ADDRESS 0x61010000
639
640 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
641
642 #define _3DSTATE_INDEX_BUFFER 0x780a0000
643 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
644
645 #define _3DSTATE_VS 0x78100000
646 #define _3DSTATE_GS 0x78110000
647 #define _3DSTATE_HS 0x781b0000
648 #define _3DSTATE_DS 0x781d0000
649
650 #define _3DSTATE_CONSTANT_VS 0x78150000
651 #define _3DSTATE_CONSTANT_GS 0x78160000
652 #define _3DSTATE_CONSTANT_PS 0x78170000
653 #define _3DSTATE_CONSTANT_HS 0x78190000
654 #define _3DSTATE_CONSTANT_DS 0x781A0000
655
656 #define _3DSTATE_PS 0x78200000
657
658 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
659 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
660 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
661 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
662 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
663
664 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
665 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
666 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
667
668 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
669 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
670 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
671 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
672 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
673
674 #define _MI_LOAD_REGISTER_IMM 0x11000000
675
676 struct custom_handler {
677 uint32_t opcode;
678 void (*handle)(struct gen_spec *spec, uint32_t *p);
679 } custom_handlers[] = {
680 { STATE_BASE_ADDRESS, handle_state_base_address },
681 { MEDIA_INTERFACE_DESCRIPTOR_LOAD, handle_media_interface_descriptor_load },
682 { _3DSTATE_VERTEX_BUFFERS, handle_3dstate_vertex_buffers },
683 { _3DSTATE_INDEX_BUFFER, handle_3dstate_index_buffer },
684 { _3DSTATE_VS, handle_3dstate_vs },
685 { _3DSTATE_GS, handle_3dstate_vs },
686 { _3DSTATE_DS, handle_3dstate_vs },
687 { _3DSTATE_HS, handle_3dstate_hs },
688 { _3DSTATE_CONSTANT_VS, handle_3dstate_constant },
689 { _3DSTATE_CONSTANT_GS, handle_3dstate_constant },
690 { _3DSTATE_CONSTANT_PS, handle_3dstate_constant },
691 { _3DSTATE_CONSTANT_HS, handle_3dstate_constant },
692 { _3DSTATE_CONSTANT_DS, handle_3dstate_constant },
693 { _3DSTATE_PS, handle_3dstate_ps },
694
695 { _3DSTATE_BINDING_TABLE_POINTERS_VS, handle_3dstate_binding_table_pointers },
696 { _3DSTATE_BINDING_TABLE_POINTERS_HS, handle_3dstate_binding_table_pointers },
697 { _3DSTATE_BINDING_TABLE_POINTERS_DS, handle_3dstate_binding_table_pointers },
698 { _3DSTATE_BINDING_TABLE_POINTERS_GS, handle_3dstate_binding_table_pointers },
699 { _3DSTATE_BINDING_TABLE_POINTERS_PS, handle_3dstate_binding_table_pointers },
700
701 { _3DSTATE_SAMPLER_STATE_POINTERS_VS, handle_3dstate_sampler_state_pointers },
702 { _3DSTATE_SAMPLER_STATE_POINTERS_GS, handle_3dstate_sampler_state_pointers },
703 { _3DSTATE_SAMPLER_STATE_POINTERS_PS, handle_3dstate_sampler_state_pointers },
704
705 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC, handle_3dstate_viewport_state_pointers_cc },
706 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP, handle_3dstate_viewport_state_pointers_sf_clip },
707 { _3DSTATE_BLEND_STATE_POINTERS, handle_3dstate_blend_state_pointers },
708 { _3DSTATE_CC_STATE_POINTERS, handle_3dstate_cc_state_pointers },
709 { _3DSTATE_SCISSOR_STATE_POINTERS, handle_3dstate_scissor_state_pointers },
710 { _MI_LOAD_REGISTER_IMM, handle_load_register_imm }
711 };
712
713 static void
714 parse_commands(struct gen_spec *spec, uint32_t *cmds, int size, int engine)
715 {
716 uint32_t *p, *end = cmds + size / 4;
717 unsigned int length, i;
718 struct gen_group *inst;
719
720 for (p = cmds; p < end; p += length) {
721 inst = gen_spec_find_instruction(spec, p);
722 if (inst == NULL) {
723 printf("unknown instruction %08x\n", p[0]);
724 length = (p[0] & 0xff) + 2;
725 continue;
726 }
727 length = gen_group_get_length(inst, p);
728
729 const char *color, *reset_color = NORMAL;
730 uint64_t offset;
731
732 if (option_full_decode) {
733 if ((p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START ||
734 (p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END)
735 color = GREEN_HEADER;
736 else
737 color = BLUE_HEADER;
738 } else
739 color = NORMAL;
740
741 if (option_color == COLOR_NEVER) {
742 color = "";
743 reset_color = "";
744 }
745
746 if (option_print_offsets)
747 offset = (void *) p - gtt;
748 else
749 offset = 0;
750
751 printf("%s0x%08"PRIx64": 0x%08x: %-80s%s\n",
752 color, offset, p[0],
753 gen_group_get_name(inst), reset_color);
754
755 if (option_full_decode) {
756 struct gen_field_iterator iter;
757 char *token = NULL;
758 int idx = 0, dword_num = 0;
759 gen_field_iterator_init(&iter, inst, p,
760 option_color == COLOR_ALWAYS);
761 while (gen_field_iterator_next(&iter)) {
762 idx = 0;
763 print_dword_val(&iter, offset, &dword_num);
764 if (dword_num > 0)
765 token = print_iterator_values(&iter, &idx);
766 if (token != NULL) {
767 printf("0x%08"PRIx64": 0x%08x : Dword %d\n",
768 offset + 4 * idx, p[idx], idx);
769 handle_struct_decode(spec,token, &p[idx]);
770 token = NULL;
771 }
772 }
773
774 for (i = 0; i < ARRAY_LENGTH(custom_handlers); i++) {
775 if (gen_group_get_opcode(inst) ==
776 custom_handlers[i].opcode)
777 custom_handlers[i].handle(spec, p);
778 }
779 }
780
781 if ((p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START) {
782 uint64_t start;
783 if (gen_spec_get_gen(spec) >= gen_make_gen(8,0))
784 start = get_qword(&p[1]);
785 else
786 start = p[1];
787
788 parse_commands(spec, gtt + start, 1 << 20, engine);
789 } else if ((p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END) {
790 break;
791 }
792 }
793 }
794
795 #define GEN_ENGINE_RENDER 1
796 #define GEN_ENGINE_BLITTER 2
797
798 static void
799 handle_trace_block(struct gen_spec *spec, uint32_t *p)
800 {
801 int operation = p[1] & AUB_TRACE_OPERATION_MASK;
802 int type = p[1] & AUB_TRACE_TYPE_MASK;
803 int address_space = p[1] & AUB_TRACE_ADDRESS_SPACE_MASK;
804 uint64_t offset = p[3];
805 uint32_t size = p[4];
806 int header_length = p[0] & 0xffff;
807 uint32_t *data = p + header_length + 2;
808 int engine = GEN_ENGINE_RENDER;
809
810 if (gen_spec_get_gen(spec) >= gen_make_gen(8,0))
811 offset += (uint64_t) p[5] << 32;
812
813 switch (operation) {
814 case AUB_TRACE_OP_DATA_WRITE:
815 if (address_space != AUB_TRACE_MEMTYPE_GTT)
816 break;
817 if (gtt_size < offset + size) {
818 fprintf(stderr, "overflow gtt space: %s\n", strerror(errno));
819 exit(EXIT_FAILURE);
820 }
821 memcpy((char *) gtt + offset, data, size);
822 if (gtt_end < offset + size)
823 gtt_end = offset + size;
824 break;
825 case AUB_TRACE_OP_COMMAND_WRITE:
826 switch (type) {
827 case AUB_TRACE_TYPE_RING_PRB0:
828 engine = GEN_ENGINE_RENDER;
829 break;
830 case AUB_TRACE_TYPE_RING_PRB2:
831 engine = GEN_ENGINE_BLITTER;
832 break;
833 default:
834 printf("command write to unknown ring %d\n", type);
835 break;
836 }
837
838 parse_commands(spec, data, size, engine);
839 gtt_end = 0;
840 break;
841 }
842 }
843
844 struct aub_file {
845 FILE *stream;
846
847 uint32_t *map, *end, *cursor;
848 uint32_t *mem_end;
849 };
850
851 static struct aub_file *
852 aub_file_open(const char *filename)
853 {
854 struct aub_file *file;
855 struct stat sb;
856 int fd;
857
858 file = calloc(1, sizeof *file);
859 fd = open(filename, O_RDONLY);
860 if (fd == -1) {
861 fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno));
862 exit(EXIT_FAILURE);
863 }
864
865 if (fstat(fd, &sb) == -1) {
866 fprintf(stderr, "stat failed: %s\n", strerror(errno));
867 exit(EXIT_FAILURE);
868 }
869
870 file->map = mmap(NULL, sb.st_size,
871 PROT_READ, MAP_SHARED, fd, 0);
872 if (file->map == MAP_FAILED) {
873 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
874 exit(EXIT_FAILURE);
875 }
876
877 file->cursor = file->map;
878 file->end = file->map + sb.st_size / 4;
879
880 return file;
881 }
882
883 static struct aub_file *
884 aub_file_stdin(void)
885 {
886 struct aub_file *file;
887
888 file = calloc(1, sizeof *file);
889 file->stream = stdin;
890
891 return file;
892 }
893
894 #define TYPE(dw) (((dw) >> 29) & 7)
895 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
896 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
897
898 #define MAKE_HEADER(type, opcode, subopcode) \
899 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
900
901 #define TYPE_AUB 0x7
902
903 /* Classic AUB opcodes */
904 #define OPCODE_AUB 0x01
905 #define SUBOPCODE_HEADER 0x05
906 #define SUBOPCODE_BLOCK 0x41
907 #define SUBOPCODE_BMP 0x1e
908
909 /* Newer version AUB opcode */
910 #define OPCODE_NEW_AUB 0x2e
911 #define SUBOPCODE_VERSION 0x00
912 #define SUBOPCODE_REG_WRITE 0x03
913 #define SUBOPCODE_MEM_POLL 0x05
914 #define SUBOPCODE_MEM_WRITE 0x06
915
916 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
917
918 struct {
919 const char *name;
920 uint32_t gen;
921 } device_map[] = {
922 { "bwr", MAKE_GEN(4, 0) },
923 { "cln", MAKE_GEN(4, 0) },
924 { "blc", MAKE_GEN(4, 0) },
925 { "ctg", MAKE_GEN(4, 0) },
926 { "el", MAKE_GEN(4, 0) },
927 { "il", MAKE_GEN(4, 0) },
928 { "sbr", MAKE_GEN(6, 0) },
929 { "ivb", MAKE_GEN(7, 0) },
930 { "lrb2", MAKE_GEN(0, 0) },
931 { "hsw", MAKE_GEN(7, 5) },
932 { "vlv", MAKE_GEN(7, 0) },
933 { "bdw", MAKE_GEN(8, 0) },
934 { "skl", MAKE_GEN(9, 0) },
935 { "chv", MAKE_GEN(8, 0) },
936 { "bxt", MAKE_GEN(9, 0) }
937 };
938
939 enum {
940 AUB_ITEM_DECODE_OK,
941 AUB_ITEM_DECODE_FAILED,
942 AUB_ITEM_DECODE_NEED_MORE_DATA,
943 };
944
945 static int
946 aub_file_decode_batch(struct aub_file *file, struct gen_spec *spec)
947 {
948 uint32_t *p, h, device, data_type, *new_cursor;
949 int header_length, payload_size, bias;
950
951 if (file->end - file->cursor < 1)
952 return AUB_ITEM_DECODE_NEED_MORE_DATA;
953
954 p = file->cursor;
955 h = *p;
956 header_length = h & 0xffff;
957
958 switch (OPCODE(h)) {
959 case OPCODE_AUB:
960 bias = 2;
961 break;
962 case OPCODE_NEW_AUB:
963 bias = 1;
964 break;
965 default:
966 printf("unknown opcode %d at %td/%td\n",
967 OPCODE(h), file->cursor - file->map,
968 file->end - file->map);
969 return AUB_ITEM_DECODE_FAILED;
970 }
971
972 payload_size = 0;
973 switch (h & 0xffff0000) {
974 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_HEADER):
975 if (file->end - file->cursor < 12)
976 return AUB_ITEM_DECODE_NEED_MORE_DATA;
977 payload_size = p[12];
978 break;
979 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BLOCK):
980 if (file->end - file->cursor < 4)
981 return AUB_ITEM_DECODE_NEED_MORE_DATA;
982 payload_size = p[4];
983 break;
984 default:
985 break;
986 }
987
988 new_cursor = p + header_length + bias + payload_size / 4;
989 if (new_cursor > file->end)
990 return AUB_ITEM_DECODE_NEED_MORE_DATA;
991
992 switch (h & 0xffff0000) {
993 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_HEADER):
994 break;
995 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BLOCK):
996 handle_trace_block(spec, p);
997 break;
998 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BMP):
999 break;
1000 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_VERSION):
1001 printf("version block: dw1 %08x\n", p[1]);
1002 device = (p[1] >> 8) & 0xff;
1003 printf(" device %s\n", device_map[device].name);
1004 break;
1005 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_REG_WRITE):
1006 printf("register write block: (dwords %d)\n", h & 0xffff);
1007 printf(" reg 0x%x, data 0x%x\n", p[1], p[5]);
1008 break;
1009 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_WRITE):
1010 printf("memory write block (dwords %d):\n", h & 0xffff);
1011 printf(" address 0x%"PRIx64"\n", *(uint64_t *) &p[1]);
1012 data_type = (p[3] >> 20) & 0xff;
1013 if (data_type != 0)
1014 printf(" data type 0x%x\n", data_type);
1015 printf(" address space 0x%x\n", (p[3] >> 28) & 0xf);
1016 break;
1017 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_POLL):
1018 printf("memory poll block (dwords %d):\n", h & 0xffff);
1019 break;
1020 default:
1021 printf("unknown block type=0x%x, opcode=0x%x, "
1022 "subopcode=0x%x (%08x)\n", TYPE(h), OPCODE(h), SUBOPCODE(h), h);
1023 break;
1024 }
1025 file->cursor = new_cursor;
1026
1027 return AUB_ITEM_DECODE_OK;
1028 }
1029
1030 static int
1031 aub_file_more_stuff(struct aub_file *file)
1032 {
1033 return file->cursor < file->end || (file->stream && !feof(file->stream));
1034 }
1035
1036 #define AUB_READ_BUFFER_SIZE (4096)
1037 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1038
1039 static void
1040 aub_file_data_grow(struct aub_file *file)
1041 {
1042 size_t old_size = (file->mem_end - file->map) * 4;
1043 size_t new_size = MAX(old_size * 2, AUB_READ_BUFFER_SIZE);
1044 uint32_t *new_start = realloc(file->map, new_size);
1045
1046 file->cursor = new_start + (file->cursor - file->map);
1047 file->end = new_start + (file->end - file->map);
1048 file->map = new_start;
1049 file->mem_end = file->map + (new_size / 4);
1050 }
1051
1052 static bool
1053 aub_file_data_load(struct aub_file *file)
1054 {
1055 size_t r;
1056
1057 if (file->stream == NULL)
1058 return false;
1059
1060 /* First remove any consumed data */
1061 if (file->cursor > file->map) {
1062 memmove(file->map, file->cursor,
1063 (file->end - file->cursor) * 4);
1064 file->end -= file->cursor - file->map;
1065 file->cursor = file->map;
1066 }
1067
1068 /* Then load some new data in */
1069 if ((file->mem_end - file->end) < (AUB_READ_BUFFER_SIZE / 4))
1070 aub_file_data_grow(file);
1071
1072 r = fread(file->end, 1, (file->mem_end - file->end) * 4, file->stream);
1073 file->end += r / 4;
1074
1075 return r != 0;
1076 }
1077
1078 static void
1079 setup_pager(void)
1080 {
1081 int fds[2];
1082 pid_t pid;
1083
1084 if (!isatty(1))
1085 return;
1086
1087 if (pipe(fds) == -1)
1088 return;
1089
1090 pid = fork();
1091 if (pid == -1)
1092 return;
1093
1094 if (pid == 0) {
1095 close(fds[1]);
1096 dup2(fds[0], 0);
1097 execlp("less", "less", "-FRSi", NULL);
1098 }
1099
1100 close(fds[0]);
1101 dup2(fds[1], 1);
1102 close(fds[1]);
1103 }
1104
1105 static void
1106 print_help(const char *progname, FILE *file)
1107 {
1108 fprintf(file,
1109 "Usage: %s [OPTION]... [FILE]\n"
1110 "Decode aub file contents from either FILE or the standard input.\n\n"
1111 "A valid --gen option must be provided.\n\n"
1112 " --help display this help and exit\n"
1113 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1114 " --headers decode only command headers\n"
1115 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1116 " if omitted), 'always', or 'never'\n"
1117 " --no-pager don't launch pager\n"
1118 " --no-offsets don't print instruction offsets\n"
1119 " --xml=DIR load hardware xml description from directory DIR\n",
1120 progname);
1121 }
1122
1123 int main(int argc, char *argv[])
1124 {
1125 struct gen_spec *spec;
1126 struct aub_file *file;
1127 int c, i;
1128 bool help = false, pager = true;
1129 char *input_file = NULL, *xml_path = NULL;
1130 char gen_val[24] = { 0, };
1131 const struct {
1132 const char *name;
1133 int pci_id;
1134 } gens[] = {
1135 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1136 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1137 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1138 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1139 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1140 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1141 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1142 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1143 }, *gen = NULL;
1144 const struct option aubinator_opts[] = {
1145 { "help", no_argument, (int *) &help, true },
1146 { "no-pager", no_argument, (int *) &pager, false },
1147 { "no-offsets", no_argument, (int *) &option_print_offsets, false },
1148 { "gen", required_argument, NULL, 'g' },
1149 { "headers", no_argument, (int *) &option_full_decode, false },
1150 { "color", required_argument, NULL, 'c' },
1151 { "xml", required_argument, NULL, 'x' },
1152 { NULL, 0, NULL, 0 }
1153 };
1154 struct gen_device_info devinfo;
1155
1156 i = 0;
1157 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
1158 switch (c) {
1159 case 'g':
1160 snprintf(gen_val, sizeof(gen_val), "%s", optarg);
1161 break;
1162 case 'c':
1163 if (optarg == NULL || strcmp(optarg, "always") == 0)
1164 option_color = COLOR_ALWAYS;
1165 else if (strcmp(optarg, "never") == 0)
1166 option_color = COLOR_NEVER;
1167 else if (strcmp(optarg, "auto") == 0)
1168 option_color = COLOR_AUTO;
1169 else {
1170 fprintf(stderr, "invalid value for --color: %s", optarg);
1171 exit(EXIT_FAILURE);
1172 }
1173 break;
1174 case 'x':
1175 xml_path = strdup(optarg);
1176 break;
1177 default:
1178 break;
1179 }
1180 }
1181
1182 if (help || argc == 1) {
1183 print_help(argv[0], stderr);
1184 exit(0);
1185 }
1186
1187 if (optind < argc)
1188 input_file = argv[optind];
1189
1190 for (i = 0; i < ARRAY_SIZE(gens); i++) {
1191 if (!strcmp(gen_val, gens[i].name)) {
1192 gen = &gens[i];
1193 break;
1194 }
1195 }
1196
1197 if (gen == NULL) {
1198 fprintf(stderr, "can't parse gen: '%s', expected ivb, byt, hsw, "
1199 "bdw, chv, skl, kbl or bxt\n", gen_val);
1200 exit(EXIT_FAILURE);
1201 }
1202
1203 if (!gen_get_device_info(gen->pci_id, &devinfo)) {
1204 fprintf(stderr, "can't find device information: pci_id=0x%x name=%s\n",
1205 gen->pci_id, gen->name);
1206 exit(EXIT_FAILURE);
1207 }
1208
1209
1210 /* Do this before we redirect stdout to pager. */
1211 if (option_color == COLOR_AUTO)
1212 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
1213
1214 if (isatty(1) && pager)
1215 setup_pager();
1216
1217 if (xml_path == NULL)
1218 spec = gen_spec_load(&devinfo);
1219 else
1220 spec = gen_spec_load_from_path(&devinfo, xml_path);
1221 disasm = gen_disasm_create(gen->pci_id);
1222
1223 if (spec == NULL || disasm == NULL)
1224 exit(EXIT_FAILURE);
1225
1226 if (input_file == NULL)
1227 file = aub_file_stdin();
1228 else
1229 file = aub_file_open(input_file);
1230
1231 /* mmap a terabyte for our gtt space. */
1232 gtt_size = 1ul << 40;
1233 gtt = mmap(NULL, gtt_size, PROT_READ | PROT_WRITE,
1234 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
1235 if (gtt == MAP_FAILED) {
1236 fprintf(stderr, "failed to alloc gtt space: %s\n", strerror(errno));
1237 exit(EXIT_FAILURE);
1238 }
1239
1240 while (aub_file_more_stuff(file)) {
1241 switch (aub_file_decode_batch(file, spec)) {
1242 case AUB_ITEM_DECODE_OK:
1243 break;
1244 case AUB_ITEM_DECODE_NEED_MORE_DATA:
1245 if (!file->stream) {
1246 file->cursor = file->end;
1247 break;
1248 }
1249 if (aub_file_more_stuff(file) && !aub_file_data_load(file)) {
1250 fprintf(stderr, "failed to load data from stdin\n");
1251 exit(EXIT_FAILURE);
1252 }
1253 break;
1254 default:
1255 fprintf(stderr, "failed to parse aubdump data\n");
1256 exit(EXIT_FAILURE);
1257 }
1258 }
1259
1260
1261 fflush(stdout);
1262 /* close the stdout which is opened to write the output */
1263 close(1);
1264 free(xml_path);
1265
1266 wait(NULL);
1267
1268 return EXIT_SUCCESS;
1269 }