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