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