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