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