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