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