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