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