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