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