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