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