intel: aubinator: add missing return characters
[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 <inttypes.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #include <sys/mman.h>
38
39 #include "util/macros.h"
40
41 #include "decoder.h"
42 #include "intel_aub.h"
43 #include "gen_disasm.h"
44
45 /* Below is the only command missing from intel_aub.h in libdrm
46 * So, reuse intel_aub.h from libdrm and #define the
47 * AUB_MI_BATCH_BUFFER_END as below
48 */
49 #define AUB_MI_BATCH_BUFFER_END (0x0500 << 16)
50
51 #define CSI "\e["
52 #define HEADER CSI "37;44m"
53 #define NORMAL CSI "0m"
54
55 /* options */
56
57 static bool option_full_decode = true;
58 static bool option_print_offsets = true;
59 static enum { COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER } option_color;
60
61 /* state */
62
63 struct gen_disasm *disasm;
64
65 uint64_t gtt_size, gtt_end;
66 void *gtt;
67 uint64_t general_state_base;
68 uint64_t surface_state_base;
69 uint64_t dynamic_state_base;
70 uint64_t instruction_base;
71 uint64_t instruction_bound;
72
73 static inline uint32_t
74 field(uint32_t value, int start, int end)
75 {
76 uint32_t mask;
77
78 mask = ~0U >> (31 - end + start);
79
80 return (value >> start) & mask;
81 }
82
83 struct brw_instruction;
84
85 static inline int
86 valid_offset(uint32_t offset)
87 {
88 return offset < gtt_end;
89 }
90
91 static void
92 print_dword_val(struct gen_field_iterator *iter, uint64_t offset,
93 int *dword_num)
94 {
95 struct gen_field *f;
96
97 f = iter->group->fields[iter->i - 1];
98 const int dword = f->start / 32;
99
100 if (*dword_num != dword) {
101 printf("0x%08"PRIx64": 0x%08x : Dword %d\n",
102 offset + 4 * dword, iter->p[dword], dword);
103 *dword_num = dword;
104 }
105 }
106
107 static char *
108 print_iterator_values(struct gen_field_iterator *iter, int *idx)
109 {
110 char *token = NULL;
111 if (strstr(iter->value, "struct") == NULL) {
112 printf(" %s: %s\n", iter->name, iter->value);
113 } else {
114 token = strtok(iter->value, " ");
115 if (token != NULL) {
116 token = strtok(NULL, " ");
117 *idx = atoi(strtok(NULL, ">"));
118 } else {
119 token = NULL;
120 }
121 printf(" %s:<struct %s>\n", iter->name, token);
122 }
123 return token;
124 }
125
126 static void
127 decode_structure(struct gen_spec *spec, struct gen_group *strct,
128 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: %08"PRIx64" <not valid>\n", start);
302 continue;
303 } else {
304 printf("kernel: %08"PRIx64"\n", start);
305 }
306
307 insns = (struct brw_instruction *) (gtt + start);
308 gen_disasm_disassemble(disasm, insns, 0, 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 %08"PRIx64", start %08"PRIx64"\n",
403 instruction_base, start);
404
405 insns = (struct brw_instruction *) (gtt + instruction_base + start);
406 gen_disasm_disassemble(disasm, insns, 0, 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 %08"PRIx64", start %08"PRIx64"\n",
427 instruction_base, start);
428
429 insns = (struct brw_instruction *) (gtt + instruction_base + start);
430 gen_disasm_disassemble(disasm, insns, 0, 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, 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, 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, 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,
571 uint32_t *p)
572 {
573 uint64_t start;
574 struct gen_group *sf_clip_viewport;
575
576 sf_clip_viewport = gen_spec_find_struct(spec, "SF_CLIP_VIEWPORT");
577
578 start = dynamic_state_base + (p[1] & ~0x3fu);
579 for (uint32_t i = 0; i < 4; i++) {
580 printf("viewport %d\n", i);
581 decode_structure(spec, sf_clip_viewport, gtt + start + i * 64);
582 }
583 }
584
585 static void
586 handle_3dstate_blend_state_pointers(struct gen_spec *spec, uint32_t *p)
587 {
588 uint64_t start;
589 struct gen_group *blend_state;
590
591 blend_state = gen_spec_find_struct(spec, "BLEND_STATE");
592
593 start = dynamic_state_base + (p[1] & ~0x3fu);
594 decode_structure(spec, blend_state, gtt + start);
595 }
596
597 static void
598 handle_3dstate_cc_state_pointers(struct gen_spec *spec, uint32_t *p)
599 {
600 uint64_t start;
601 struct gen_group *cc_state;
602
603 cc_state = gen_spec_find_struct(spec, "COLOR_CALC_STATE");
604
605 start = dynamic_state_base + (p[1] & ~0x3fu);
606 decode_structure(spec, cc_state, gtt + start);
607 }
608
609 static void
610 handle_3dstate_scissor_state_pointers(struct gen_spec *spec, uint32_t *p)
611 {
612 uint64_t start;
613 struct gen_group *scissor_rect;
614
615 scissor_rect = gen_spec_find_struct(spec, "SCISSOR_RECT");
616
617 start = dynamic_state_base + (p[1] & ~0x1fu);
618 decode_structure(spec, scissor_rect, gtt + start);
619 }
620
621 static void
622 handle_load_register_imm(struct gen_spec *spec, uint32_t *p)
623 {
624 struct gen_group *reg = gen_spec_find_register(spec, p[1]);
625
626 if (reg != NULL) {
627 printf("register %s (0x%x): 0x%x\n",
628 reg->name, reg->register_offset, p[2]);
629 decode_structure(spec, reg, &p[2]);
630 }
631 }
632
633 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
634
635 #define STATE_BASE_ADDRESS 0x61010000
636
637 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
638
639 #define _3DSTATE_INDEX_BUFFER 0x780a0000
640 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
641
642 #define _3DSTATE_VS 0x78100000
643 #define _3DSTATE_GS 0x78110000
644 #define _3DSTATE_HS 0x781b0000
645 #define _3DSTATE_DS 0x781d0000
646
647 #define _3DSTATE_CONSTANT_VS 0x78150000
648 #define _3DSTATE_CONSTANT_GS 0x78160000
649 #define _3DSTATE_CONSTANT_PS 0x78170000
650 #define _3DSTATE_CONSTANT_HS 0x78190000
651 #define _3DSTATE_CONSTANT_DS 0x781A0000
652
653 #define _3DSTATE_PS 0x78200000
654
655 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
656 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
657 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
658 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
659 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
660
661 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
662 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
663 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
664
665 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
666 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
667 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
668 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
669 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
670
671 #define _MI_LOAD_REGISTER_IMM 0x11000000
672
673 struct custom_handler {
674 uint32_t opcode;
675 void (*handle)(struct gen_spec *spec, uint32_t *p);
676 } custom_handlers[] = {
677 { STATE_BASE_ADDRESS, handle_state_base_address },
678 { MEDIA_INTERFACE_DESCRIPTOR_LOAD, handle_media_interface_descriptor_load },
679 { _3DSTATE_VERTEX_BUFFERS, handle_3dstate_vertex_buffers },
680 { _3DSTATE_INDEX_BUFFER, handle_3dstate_index_buffer },
681 { _3DSTATE_VS, handle_3dstate_vs },
682 { _3DSTATE_GS, handle_3dstate_vs },
683 { _3DSTATE_DS, handle_3dstate_vs },
684 { _3DSTATE_HS, handle_3dstate_hs },
685 { _3DSTATE_CONSTANT_VS, handle_3dstate_constant },
686 { _3DSTATE_CONSTANT_GS, handle_3dstate_constant },
687 { _3DSTATE_CONSTANT_PS, handle_3dstate_constant },
688 { _3DSTATE_CONSTANT_HS, handle_3dstate_constant },
689 { _3DSTATE_CONSTANT_DS, handle_3dstate_constant },
690 { _3DSTATE_PS, handle_3dstate_ps },
691
692 { _3DSTATE_BINDING_TABLE_POINTERS_VS, handle_3dstate_binding_table_pointers },
693 { _3DSTATE_BINDING_TABLE_POINTERS_HS, handle_3dstate_binding_table_pointers },
694 { _3DSTATE_BINDING_TABLE_POINTERS_DS, handle_3dstate_binding_table_pointers },
695 { _3DSTATE_BINDING_TABLE_POINTERS_GS, handle_3dstate_binding_table_pointers },
696 { _3DSTATE_BINDING_TABLE_POINTERS_PS, handle_3dstate_binding_table_pointers },
697
698 { _3DSTATE_SAMPLER_STATE_POINTERS_VS, handle_3dstate_sampler_state_pointers },
699 { _3DSTATE_SAMPLER_STATE_POINTERS_GS, handle_3dstate_sampler_state_pointers },
700 { _3DSTATE_SAMPLER_STATE_POINTERS_PS, handle_3dstate_sampler_state_pointers },
701
702 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC, handle_3dstate_viewport_state_pointers_cc },
703 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP, handle_3dstate_viewport_state_pointers_sf_clip },
704 { _3DSTATE_BLEND_STATE_POINTERS, handle_3dstate_blend_state_pointers },
705 { _3DSTATE_CC_STATE_POINTERS, handle_3dstate_cc_state_pointers },
706 { _3DSTATE_SCISSOR_STATE_POINTERS, handle_3dstate_scissor_state_pointers },
707 { _MI_LOAD_REGISTER_IMM, handle_load_register_imm }
708 };
709
710 static void
711 parse_commands(struct gen_spec *spec, uint32_t *cmds, int size, int engine)
712 {
713 uint32_t *p, *end = cmds + size / 4;
714 unsigned int length, i;
715 struct gen_group *inst;
716
717 for (p = cmds; p < end; p += length) {
718 inst = gen_spec_find_instruction(spec, p);
719 if (inst == NULL) {
720 printf("unknown instruction %08x\n", p[0]);
721 length = (p[0] & 0xff) + 2;
722 continue;
723 }
724 length = gen_group_get_length(inst, p);
725
726 const char *color, *reset_color = NORMAL;
727 uint64_t offset;
728
729 if (option_full_decode)
730 color = HEADER;
731 else
732 color = NORMAL;
733
734 if (option_color == COLOR_NEVER) {
735 color = "";
736 reset_color = "";
737 }
738
739 if (option_print_offsets)
740 offset = (void *) p - gtt;
741 else
742 offset = 0;
743
744 printf("%s0x%08"PRIx64": 0x%08x: %-80s%s\n",
745 color, offset, p[0],
746 gen_group_get_name(inst), reset_color);
747
748 if (option_full_decode) {
749 struct gen_field_iterator iter;
750 char *token = NULL;
751 int idx = 0, dword_num = 0;
752 gen_field_iterator_init(&iter, inst, p);
753 while (gen_field_iterator_next(&iter)) {
754 idx = 0;
755 print_dword_val(&iter, offset, &dword_num);
756 if (dword_num > 0)
757 token = print_iterator_values(&iter, &idx);
758 if (token != NULL) {
759 printf("0x%08"PRIx64": 0x%08x : Dword %d\n",
760 offset + 4 * idx, p[idx], idx);
761 handle_struct_decode(spec,token, &p[idx]);
762 token = NULL;
763 }
764 }
765
766 for (i = 0; i < ARRAY_LENGTH(custom_handlers); i++) {
767 if (gen_group_get_opcode(inst) ==
768 custom_handlers[i].opcode)
769 custom_handlers[i].handle(spec, p);
770 }
771 }
772
773 if ((p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START) {
774 uint64_t start;
775 if (gen_spec_get_gen(spec) >= gen_make_gen(8,0))
776 start = get_qword(&p[1]);
777 else
778 start = p[1];
779
780 parse_commands(spec, gtt + start, 1 << 20, engine);
781 } else if ((p[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END) {
782 break;
783 }
784 }
785 }
786
787 #define GEN_ENGINE_RENDER 1
788 #define GEN_ENGINE_BLITTER 2
789
790 static void
791 handle_trace_block(struct gen_spec *spec, uint32_t *p)
792 {
793 int operation = p[1] & AUB_TRACE_OPERATION_MASK;
794 int type = p[1] & AUB_TRACE_TYPE_MASK;
795 int address_space = p[1] & AUB_TRACE_ADDRESS_SPACE_MASK;
796 uint64_t offset = p[3];
797 uint32_t size = p[4];
798 int header_length = p[0] & 0xffff;
799 uint32_t *data = p + header_length + 2;
800 int engine = GEN_ENGINE_RENDER;
801
802 if (gen_spec_get_gen(spec) >= gen_make_gen(8,0))
803 offset += (uint64_t) p[5] << 32;
804
805 switch (operation) {
806 case AUB_TRACE_OP_DATA_WRITE:
807 if (address_space != AUB_TRACE_MEMTYPE_GTT)
808 break;
809 if (gtt_size < offset + size) {
810 fprintf(stderr, "overflow gtt space: %s\n", strerror(errno));
811 exit(EXIT_FAILURE);
812 }
813 memcpy((char *) gtt + offset, data, size);
814 if (gtt_end < offset + size)
815 gtt_end = offset + size;
816 break;
817 case AUB_TRACE_OP_COMMAND_WRITE:
818 switch (type) {
819 case AUB_TRACE_TYPE_RING_PRB0:
820 engine = GEN_ENGINE_RENDER;
821 break;
822 case AUB_TRACE_TYPE_RING_PRB2:
823 engine = GEN_ENGINE_BLITTER;
824 break;
825 default:
826 printf("command write to unknown ring %d\n", type);
827 break;
828 }
829
830 parse_commands(spec, data, size, engine);
831 gtt_end = 0;
832 break;
833 }
834 }
835
836 struct aub_file {
837 char *filename;
838 int fd;
839 struct stat sb;
840 uint32_t *map, *end, *cursor;
841 };
842
843 static struct aub_file *
844 aub_file_open(const char *filename)
845 {
846 struct aub_file *file;
847
848 file = malloc(sizeof *file);
849 file->filename = strdup(filename);
850 file->fd = open(file->filename, O_RDONLY);
851 if (file->fd == -1) {
852 fprintf(stderr, "open %s failed: %s\n", file->filename, strerror(errno));
853 exit(EXIT_FAILURE);
854 }
855
856 if (fstat(file->fd, &file->sb) == -1) {
857 fprintf(stderr, "stat failed: %s\n", strerror(errno));
858 exit(EXIT_FAILURE);
859 }
860
861 file->map = mmap(NULL, file->sb.st_size,
862 PROT_READ, MAP_SHARED, file->fd, 0);
863 if (file->map == MAP_FAILED) {
864 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
865 exit(EXIT_FAILURE);
866 }
867
868 file->cursor = file->map;
869 file->end = file->map + file->sb.st_size / 4;
870
871 /* mmap a terabyte for our gtt space. */
872 gtt_size = 1ul << 40;
873 gtt = mmap(NULL, gtt_size, PROT_READ | PROT_WRITE,
874 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
875 if (gtt == MAP_FAILED) {
876 fprintf(stderr, "failed to alloc gtt space: %s\n", strerror(errno));
877 exit(1);
878 }
879
880 return file;
881 }
882
883 #define TYPE(dw) (((dw) >> 29) & 7)
884 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
885 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
886
887 #define MAKE_HEADER(type, opcode, subopcode) \
888 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
889
890 #define TYPE_AUB 0x7
891
892 /* Classic AUB opcodes */
893 #define OPCODE_AUB 0x01
894 #define SUBOPCODE_HEADER 0x05
895 #define SUBOPCODE_BLOCK 0x41
896 #define SUBOPCODE_BMP 0x1e
897
898 /* Newer version AUB opcode */
899 #define OPCODE_NEW_AUB 0x2e
900 #define SUBOPCODE_VERSION 0x00
901 #define SUBOPCODE_REG_WRITE 0x03
902 #define SUBOPCODE_MEM_POLL 0x05
903 #define SUBOPCODE_MEM_WRITE 0x06
904
905 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
906
907 struct {
908 const char *name;
909 uint32_t gen;
910 } device_map[] = {
911 { "bwr", MAKE_GEN(4, 0) },
912 { "cln", MAKE_GEN(4, 0) },
913 { "blc", MAKE_GEN(4, 0) },
914 { "ctg", MAKE_GEN(4, 0) },
915 { "el", MAKE_GEN(4, 0) },
916 { "il", MAKE_GEN(4, 0) },
917 { "sbr", MAKE_GEN(6, 0) },
918 { "ivb", MAKE_GEN(7, 0) },
919 { "lrb2", MAKE_GEN(0, 0) },
920 { "hsw", MAKE_GEN(7, 5) },
921 { "vlv", MAKE_GEN(7, 0) },
922 { "bdw", MAKE_GEN(8, 0) },
923 { "skl", MAKE_GEN(9, 0) },
924 { "chv", MAKE_GEN(8, 0) },
925 { "bxt", MAKE_GEN(9, 0) }
926 };
927
928 static void
929 aub_file_decode_batch(struct aub_file *file, struct gen_spec *spec)
930 {
931 uint32_t *p, h, device, data_type;
932 int header_length, payload_size, bias;
933
934 p = file->cursor;
935 h = *p;
936 header_length = h & 0xffff;
937
938 switch (OPCODE(h)) {
939 case OPCODE_AUB:
940 bias = 2;
941 break;
942 case OPCODE_NEW_AUB:
943 bias = 1;
944 break;
945 default:
946 printf("unknown opcode %d at %td/%td\n",
947 OPCODE(h), file->cursor - file->map,
948 file->end - file->map);
949 file->cursor = file->end;
950 return;
951 }
952
953 payload_size = 0;
954 switch (h & 0xffff0000) {
955 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_HEADER):
956 payload_size = p[12];
957 break;
958 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BLOCK):
959 payload_size = p[4];
960 handle_trace_block(spec, p);
961 break;
962 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BMP):
963 break;
964
965 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_VERSION):
966 printf("version block: dw1 %08x\n", p[1]);
967 device = (p[1] >> 8) & 0xff;
968 printf(" device %s\n", device_map[device].name);
969 break;
970 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_REG_WRITE):
971 printf("register write block: (dwords %d)\n", h & 0xffff);
972 printf(" reg 0x%x, data 0x%x\n", p[1], p[5]);
973 break;
974 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_WRITE):
975 printf("memory write block (dwords %d):\n", h & 0xffff);
976 printf(" address 0x%"PRIx64"\n", *(uint64_t *) &p[1]);
977 data_type = (p[3] >> 20) & 0xff;
978 if (data_type != 0)
979 printf(" data type 0x%x\n", data_type);
980 printf(" address space 0x%x\n", (p[3] >> 28) & 0xf);
981 break;
982 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_POLL):
983 printf("memory poll block (dwords %d):\n", h & 0xffff);
984 break;
985 default:
986 printf("unknown block type=0x%x, opcode=0x%x, "
987 "subopcode=0x%x (%08x)\n", TYPE(h), OPCODE(h), SUBOPCODE(h), h);
988 break;
989 }
990 file->cursor = p + header_length + bias + payload_size / 4;
991 }
992
993 static int
994 aub_file_more_stuff(struct aub_file *file)
995 {
996 return file->cursor < file->end;
997 }
998
999 static void
1000 setup_pager(void)
1001 {
1002 int fds[2];
1003 pid_t pid;
1004
1005 if (!isatty(1))
1006 return;
1007
1008 if (pipe(fds) == -1)
1009 return;
1010
1011 pid = fork();
1012 if (pid == -1)
1013 return;
1014
1015 if (pid == 0) {
1016 close(fds[1]);
1017 dup2(fds[0], 0);
1018 execlp("less", "less", "-FRSi", NULL);
1019 }
1020
1021 close(fds[0]);
1022 dup2(fds[1], 1);
1023 close(fds[1]);
1024 }
1025
1026 static void
1027 print_help(const char *progname, FILE *file)
1028 {
1029 fprintf(file,
1030 "Usage: %s [OPTION]... FILE\n"
1031 "Decode aub file contents.\n\n"
1032 "A valid --gen option must be provided.\n\n"
1033 " --help display this help and exit\n"
1034 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1035 " --headers decode only command headers\n"
1036 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1037 " if omitted), 'always', or 'never'\n"
1038 " --no-pager don't launch pager\n"
1039 " --no-offsets don't print instruction offsets\n",
1040 progname);
1041 }
1042
1043 static bool
1044 is_prefix(const char *arg, const char *prefix, const char **value)
1045 {
1046 int l = strlen(prefix);
1047
1048 if (strncmp(arg, prefix, l) == 0 && (arg[l] == '\0' || arg[l] == '=')) {
1049 if (arg[l] == '=')
1050 *value = arg + l + 1;
1051 else
1052 *value = NULL;
1053
1054 return true;
1055 }
1056
1057 return false;
1058 }
1059
1060 int main(int argc, char *argv[])
1061 {
1062 struct gen_spec *spec;
1063 struct aub_file *file;
1064 int i;
1065 bool found_arg_gen = false, pager = true;
1066 const char *value, *input_file;
1067 char gen_file[256], gen_val[24];
1068 const struct {
1069 const char *name;
1070 int pci_id;
1071 int major;
1072 int minor;
1073 } gens[] = {
1074 { "ivb", 0x0166, 7, 0 }, /* Intel(R) Ivybridge Mobile GT2 */
1075 { "hsw", 0x0416, 7, 5 }, /* Intel(R) Haswell Mobile GT2 */
1076 { "byt", 0x0155, 7, 5 }, /* Intel(R) Bay Trail */
1077 { "bdw", 0x1616, 8, 0 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1078 { "chv", 0x22B3, 8, 0 }, /* Intel(R) HD Graphics (Cherryview) */
1079 { "skl", 0x1912, 9, 0 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1080 { "kbl", 0x591D, 9, 0 }, /* Intel(R) Kabylake GT2 */
1081 { "bxt", 0x0A84, 9, 0 } /* Intel(R) HD Graphics (Broxton) */
1082 }, *gen = NULL;
1083
1084 if (argc == 1) {
1085 print_help(argv[0], stderr);
1086 exit(EXIT_FAILURE);
1087 }
1088
1089 for (i = 1; i < argc; ++i) {
1090 if (strcmp(argv[i], "--no-pager") == 0) {
1091 pager = false;
1092 } else if (strcmp(argv[i], "--no-offsets") == 0) {
1093 option_print_offsets = false;
1094 } else if (is_prefix(argv[i], "--gen", &value)) {
1095 if (value == NULL) {
1096 fprintf(stderr, "option '--gen' requires an argument\n");
1097 exit(EXIT_FAILURE);
1098 }
1099 found_arg_gen = true;
1100 snprintf(gen_val, sizeof(gen_val), "%s", value);
1101 } else if (strcmp(argv[i], "--headers") == 0) {
1102 option_full_decode = false;
1103 } else if (is_prefix(argv[i], "--color", &value)) {
1104 if (value == NULL || strcmp(value, "always") == 0)
1105 option_color = COLOR_ALWAYS;
1106 else if (strcmp(value, "never") == 0)
1107 option_color = COLOR_NEVER;
1108 else if (strcmp(value, "auto") == 0)
1109 option_color = COLOR_AUTO;
1110 else {
1111 fprintf(stderr, "invalid value for --color: %s", value);
1112 exit(EXIT_FAILURE);
1113 }
1114 } else if (strcmp(argv[i], "--help") == 0) {
1115 print_help(argv[0], stdout);
1116 exit(EXIT_SUCCESS);
1117 } else {
1118 if (argv[i][0] == '-') {
1119 fprintf(stderr, "unknown option %s\n", argv[i]);
1120 exit(EXIT_FAILURE);
1121 }
1122 input_file = argv[i];
1123 break;
1124 }
1125 }
1126
1127 if (!found_arg_gen) {
1128 fprintf(stderr, "argument --gen is required\n");
1129 exit(EXIT_FAILURE);
1130 }
1131
1132 for (i = 0; i < ARRAY_SIZE(gens); i++) {
1133 if (!strcmp(gen_val, gens[i].name)) {
1134 gen = &gens[i];
1135 break;
1136 }
1137 }
1138
1139 if (gen == NULL) {
1140 fprintf(stderr, "can't parse gen: %s, expected ivb, byt, hsw, "
1141 "bdw, chv, skl, kbl or bxt\n", gen_val);
1142 exit(EXIT_FAILURE);
1143 }
1144
1145 /* Do this before we redirect stdout to pager. */
1146 if (option_color == COLOR_AUTO)
1147 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
1148
1149 if (isatty(1) && pager)
1150 setup_pager();
1151
1152 if (gen->minor > 0) {
1153 snprintf(gen_file, sizeof(gen_file), "../genxml/gen%d%d.xml",
1154 gen->major, gen->minor);
1155 } else {
1156 snprintf(gen_file, sizeof(gen_file), "../genxml/gen%d.xml", gen->major);
1157 }
1158
1159 spec = gen_spec_load(gen_file);
1160 disasm = gen_disasm_create(gen->pci_id);
1161
1162 if (input_file == NULL) {
1163 print_help(input_file, stderr);
1164 exit(EXIT_FAILURE);
1165 } else {
1166 file = aub_file_open(input_file);
1167 }
1168
1169 while (aub_file_more_stuff(file))
1170 aub_file_decode_batch(file, spec);
1171
1172 fflush(stdout);
1173 /* close the stdout which is opened to write the output */
1174 close(1);
1175
1176 wait(NULL);
1177
1178 return EXIT_SUCCESS;
1179 }