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