2 * Copyright © 2016 Intel Corporation
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:
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
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
35 #include <sys/types.h>
40 #include "util/macros.h"
43 #include "intel_aub.h"
44 #include "gen_disasm.h"
46 /* Below is the only command missing from intel_aub.h in libdrm
47 * So, reuse intel_aub.h from libdrm and #define the
48 * AUB_MI_BATCH_BUFFER_END as below
50 #define AUB_MI_BATCH_BUFFER_END (0x0500 << 16)
53 #define BLUE_HEADER CSI "0;44m"
54 #define GREEN_HEADER CSI "1;42m"
55 #define NORMAL CSI "0m"
59 static bool option_full_decode
= true;
60 static bool option_print_offsets
= true;
61 static enum { COLOR_AUTO
, COLOR_ALWAYS
, COLOR_NEVER
} option_color
;
66 char *input_file
= NULL
, *xml_path
= NULL
;
67 struct gen_spec
*spec
;
68 struct gen_disasm
*disasm
;
70 uint64_t gtt_size
, gtt_end
;
72 uint64_t general_state_base
;
73 uint64_t surface_state_base
;
74 uint64_t dynamic_state_base
;
75 uint64_t instruction_base
;
76 uint64_t instruction_bound
;
80 static inline uint32_t
81 field(uint32_t value
, int start
, int end
)
85 mask
= ~0U >> (31 - end
+ start
);
87 return (value
>> start
) & mask
;
90 struct brw_instruction
;
93 valid_offset(uint32_t offset
)
95 return offset
< gtt_end
;
99 print_dword_header(struct gen_field_iterator
*iter
, uint64_t offset
)
101 fprintf(outfile
, "0x%08"PRIx64
": 0x%08x : Dword %d\n",
102 offset
+ 4 * iter
->dword
, iter
->p
[iter
->dword
], iter
->dword
);
106 decode_group(struct gen_group
*strct
, const uint32_t *p
, int starting_dword
)
108 struct gen_field_iterator iter
;
112 if (option_print_offsets
)
113 offset
= (void *) p
- gtt
;
117 gen_field_iterator_init(&iter
, strct
, p
,
118 option_color
== COLOR_ALWAYS
);
119 while (gen_field_iterator_next(&iter
)) {
120 if (last_dword
!= iter
.dword
) {
121 print_dword_header(&iter
, offset
);
122 last_dword
= iter
.dword
;
124 if (iter
.dword
>= starting_dword
) {
125 fprintf(outfile
, " %s: %s\n", iter
.name
, iter
.value
);
126 if (iter
.struct_desc
) {
127 print_dword_header(&iter
, offset
+ 4 * iter
.dword
);
128 decode_group(iter
.struct_desc
, &p
[iter
.dword
], 0);
135 dump_binding_table(struct gen_spec
*spec
, uint32_t offset
)
137 uint32_t *pointers
, i
;
139 struct gen_group
*surface_state
;
141 surface_state
= gen_spec_find_struct(spec
, "RENDER_SURFACE_STATE");
142 if (surface_state
== NULL
) {
143 fprintf(outfile
, "did not find RENDER_SURFACE_STATE info\n");
147 start
= surface_state_base
+ offset
;
148 pointers
= gtt
+ start
;
149 for (i
= 0; i
< 16; i
++) {
150 if (pointers
[i
] == 0)
152 start
= pointers
[i
] + surface_state_base
;
153 if (!valid_offset(start
)) {
154 fprintf(outfile
, "pointer %u: %08x <not valid>\n",
158 fprintf(outfile
, "pointer %u: %08x\n", i
, pointers
[i
]);
161 decode_group(surface_state
, gtt
+ start
, 0);
166 handle_3dstate_index_buffer(struct gen_spec
*spec
, uint32_t *p
)
169 uint32_t length
, i
, type
, size
;
172 type
= (p
[1] >> 8) & 3;
174 length
= p
[4] / size
;
178 fprintf(outfile
, "\t");
180 for (i
= 0; i
< length
; i
++) {
183 fprintf(outfile
, "%3d ", ((uint8_t *)start
)[i
]);
186 fprintf(outfile
, "%3d ", ((uint16_t *)start
)[i
]);
189 fprintf(outfile
, "%3d ", ((uint32_t *)start
)[i
]);
193 if (length
< p
[4] / size
)
194 fprintf(outfile
, "...\n");
196 fprintf(outfile
, "\n");
199 static inline uint64_t
200 get_address(struct gen_spec
*spec
, uint32_t *p
)
202 /* Addresses are always guaranteed to be page-aligned and sometimes
203 * hardware packets have extra stuff stuffed in the bottom 12 bits.
205 uint64_t addr
= p
[0] & ~0xfffu
;
207 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
208 /* On Broadwell and above, we have 48-bit addresses which consume two
209 * dwords. Some packets require that these get stored in a "canonical
210 * form" which means that bit 47 is sign-extended through the upper
211 * bits. In order to correctly handle those aub dumps, we need to mask
212 * off the top 16 bits.
214 addr
|= ((uint64_t)p
[1] & 0xffff) << 32;
220 static inline uint64_t
221 get_offset(uint32_t *p
, uint32_t start
, uint32_t end
)
223 assert(start
<= end
);
226 uint64_t mask
= (~0ull >> (64 - (end
- start
+ 1))) << start
;
228 uint64_t offset
= p
[0];
230 offset
|= (uint64_t) p
[1] << 32;
232 return offset
& mask
;
236 handle_state_base_address(struct gen_spec
*spec
, uint32_t *p
)
238 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
240 general_state_base
= get_address(spec
, &p
[1]);
242 surface_state_base
= get_address(spec
, &p
[4]);
244 dynamic_state_base
= get_address(spec
, &p
[6]);
246 instruction_base
= get_address(spec
, &p
[10]);
248 instruction_bound
= p
[15] & 0xfff;
251 surface_state_base
= get_address(spec
, &p
[2]);
253 dynamic_state_base
= get_address(spec
, &p
[3]);
255 instruction_base
= get_address(spec
, &p
[5]);
257 instruction_bound
= get_address(spec
, &p
[9]);
262 dump_samplers(struct gen_spec
*spec
, uint32_t offset
)
266 struct gen_group
*sampler_state
;
268 sampler_state
= gen_spec_find_struct(spec
, "SAMPLER_STATE");
270 start
= dynamic_state_base
+ offset
;
271 for (i
= 0; i
< 4; i
++) {
272 fprintf(outfile
, "sampler state %d\n", i
);
273 decode_group(sampler_state
, gtt
+ start
+ i
* 16, 0);
278 handle_media_interface_descriptor_load(struct gen_spec
*spec
, uint32_t *p
)
280 int i
, length
= p
[2] / 32;
281 struct gen_group
*descriptor_structure
;
282 uint32_t *descriptors
;
284 struct brw_instruction
*insns
;
286 descriptor_structure
=
287 gen_spec_find_struct(spec
, "INTERFACE_DESCRIPTOR_DATA");
288 if (descriptor_structure
== NULL
) {
289 fprintf(outfile
, "did not find INTERFACE_DESCRIPTOR_DATA info\n");
293 start
= dynamic_state_base
+ p
[3];
294 descriptors
= gtt
+ start
;
295 for (i
= 0; i
< length
; i
++, descriptors
+= 8) {
296 fprintf(outfile
, "descriptor %u: %08x\n", i
, *descriptors
);
297 decode_group(descriptor_structure
, descriptors
, 0);
299 start
= instruction_base
+ descriptors
[0];
300 if (!valid_offset(start
)) {
301 fprintf(outfile
, "kernel: %08"PRIx64
" <not valid>\n", start
);
304 fprintf(outfile
, "kernel: %08"PRIx64
"\n", start
);
307 insns
= (struct brw_instruction
*) (gtt
+ start
);
308 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
310 dump_samplers(spec
, descriptors
[3] & ~0x1f);
311 dump_binding_table(spec
, descriptors
[4] & ~0x1f);
315 /* Heuristic to determine whether a uint32_t is probably actually a float
316 * (http://stackoverflow.com/a/2953466)
320 probably_float(uint32_t bits
)
322 int exp
= ((bits
& 0x7f800000U
) >> 23) - 127;
323 uint32_t mant
= bits
& 0x007fffff;
326 if (exp
== -127 && mant
== 0)
329 /* +- 1 billionth to 1 billion */
330 if (-30 <= exp
&& exp
<= 30)
333 /* some value with only a few binary digits */
334 if ((mant
& 0x0000ffff) == 0)
341 handle_3dstate_vertex_buffers(struct gen_spec
*spec
, uint32_t *p
)
343 uint32_t *end
, *s
, *dw
, *dwend
;
345 int n
, i
, count
, stride
;
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];
354 dwend
= gtt
+ s
[2] + 1;
357 stride
= field(s
[0], 0, 11);
359 fprintf(outfile
, "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 fprintf(outfile
, " ");
364 if (probably_float(*dw
))
365 fprintf(outfile
, " %8.2f", *(float *) dw
);
367 fprintf(outfile
, " 0x%08x", *dw
);
372 if (count
== stride
) {
373 fprintf(outfile
, "\n");
375 } else if (count
% (8 * 4) == 0) {
376 fprintf(outfile
, "\n");
378 fprintf(outfile
, " ");
381 if (count
> 0 && count
% (8 * 4) != 0)
382 fprintf(outfile
, "\n");
387 handle_3dstate_vs(struct gen_spec
*spec
, uint32_t *p
)
390 struct brw_instruction
*insns
;
393 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
394 start
= get_offset(&p
[1], 6, 63);
395 vs_enable
= p
[7] & 1;
397 start
= get_offset(&p
[1], 6, 31);
398 vs_enable
= p
[5] & 1;
402 fprintf(outfile
, "instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
403 instruction_base
, start
);
405 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
406 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
411 handle_3dstate_hs(struct gen_spec
*spec
, uint32_t *p
)
414 struct brw_instruction
*insns
;
417 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
418 start
= get_offset(&p
[3], 6, 63);
420 start
= get_offset(&p
[3], 6, 31);
423 hs_enable
= p
[2] & 0x80000000;
426 fprintf(outfile
, "instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
427 instruction_base
, start
);
429 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
430 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
435 handle_3dstate_constant(struct gen_spec
*spec
, uint32_t *p
)
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
);
445 for (j
= 0; j
< length
* 8; j
++) {
446 if (probably_float(dw
[j
]))
447 fprintf(outfile
, " %04.3f", f
[j
]);
449 fprintf(outfile
, " 0x%08x", dw
[j
]);
452 fprintf(outfile
, "\n");
458 handle_3dstate_ps(struct gen_spec
*spec
, uint32_t *p
)
460 uint32_t mask
= ~((1 << 6) - 1);
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
;
468 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
479 #define DISPATCH_16 2
480 #define DISPATCH_32 4
493 case DISPATCH_8
| DISPATCH_16
:
503 case DISPATCH_16
| DISPATCH_32
:
508 case DISPATCH_8
| DISPATCH_16
| DISPATCH_32
:
520 start
= instruction_base
+ (p
[1] & mask
);
521 fprintf(outfile
, " Kernel[0] %s\n", k0
);
523 insns
= (struct brw_instruction
*) (gtt
+ start
);
524 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
527 start
= instruction_base
+ (p
[k1_offset
] & mask
);
528 fprintf(outfile
, " Kernel[1] %s\n", k1
);
530 insns
= (struct brw_instruction
*) (gtt
+ start
);
531 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
534 start
= instruction_base
+ (p
[k2_offset
] & mask
);
535 fprintf(outfile
, " Kernel[2] %s\n", k2
);
537 insns
= (struct brw_instruction
*) (gtt
+ start
);
538 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
543 handle_3dstate_binding_table_pointers(struct gen_spec
*spec
, uint32_t *p
)
545 dump_binding_table(spec
, p
[1]);
549 handle_3dstate_sampler_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
551 dump_samplers(spec
, p
[1]);
555 handle_3dstate_viewport_state_pointers_cc(struct gen_spec
*spec
, uint32_t *p
)
558 struct gen_group
*cc_viewport
;
560 cc_viewport
= gen_spec_find_struct(spec
, "CC_VIEWPORT");
562 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
563 for (uint32_t i
= 0; i
< 4; i
++) {
564 fprintf(outfile
, "viewport %d\n", i
);
565 decode_group(cc_viewport
, gtt
+ start
+ i
* 8, 0);
570 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec
*spec
,
574 struct gen_group
*sf_clip_viewport
;
576 sf_clip_viewport
= gen_spec_find_struct(spec
, "SF_CLIP_VIEWPORT");
578 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
579 for (uint32_t i
= 0; i
< 4; i
++) {
580 fprintf(outfile
, "viewport %d\n", i
);
581 decode_group(sf_clip_viewport
, gtt
+ start
+ i
* 64, 0);
586 handle_3dstate_blend_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
589 struct gen_group
*blend_state
;
591 blend_state
= gen_spec_find_struct(spec
, "BLEND_STATE");
593 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
594 decode_group(blend_state
, gtt
+ start
, 0);
598 handle_3dstate_cc_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
601 struct gen_group
*cc_state
;
603 cc_state
= gen_spec_find_struct(spec
, "COLOR_CALC_STATE");
605 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
606 decode_group(cc_state
, gtt
+ start
, 0);
610 handle_3dstate_scissor_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
613 struct gen_group
*scissor_rect
;
615 scissor_rect
= gen_spec_find_struct(spec
, "SCISSOR_RECT");
617 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
618 decode_group(scissor_rect
, gtt
+ start
, 0);
622 handle_load_register_imm(struct gen_spec
*spec
, uint32_t *p
)
624 struct gen_group
*reg
= gen_spec_find_register(spec
, p
[1]);
627 fprintf(outfile
, "register %s (0x%x): 0x%x\n",
628 reg
->name
, reg
->register_offset
, p
[2]);
629 decode_group(reg
, &p
[2], 0);
633 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
635 #define STATE_BASE_ADDRESS 0x61010000
637 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
639 #define _3DSTATE_INDEX_BUFFER 0x780a0000
640 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
642 #define _3DSTATE_VS 0x78100000
643 #define _3DSTATE_GS 0x78110000
644 #define _3DSTATE_HS 0x781b0000
645 #define _3DSTATE_DS 0x781d0000
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
653 #define _3DSTATE_PS 0x78200000
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
661 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
662 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
663 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
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
671 #define _MI_LOAD_REGISTER_IMM 0x11000000
673 struct custom_handler
{
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
},
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
},
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
},
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
}
711 parse_commands(struct gen_spec
*spec
, uint32_t *cmds
, int size
, int engine
)
713 uint32_t *p
, *end
= cmds
+ size
/ 4;
714 unsigned int length
, i
;
715 struct gen_group
*inst
;
717 for (p
= cmds
; p
< end
; p
+= length
) {
718 inst
= gen_spec_find_instruction(spec
, p
);
720 fprintf(outfile
, "unknown instruction %08x\n", p
[0]);
721 length
= (p
[0] & 0xff) + 2;
724 length
= gen_group_get_length(inst
, p
);
726 const char *color
, *reset_color
= NORMAL
;
729 if (option_full_decode
) {
730 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
||
731 (p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
)
732 color
= GREEN_HEADER
;
738 if (option_color
== COLOR_NEVER
) {
743 if (option_print_offsets
)
744 offset
= (void *) p
- gtt
;
748 fprintf(outfile
, "%s0x%08"PRIx64
": 0x%08x: %-80s%s\n",
750 gen_group_get_name(inst
), reset_color
);
752 if (option_full_decode
) {
753 decode_group(inst
, p
, 1);
755 for (i
= 0; i
< ARRAY_LENGTH(custom_handlers
); i
++) {
756 if (gen_group_get_opcode(inst
) == custom_handlers
[i
].opcode
)
757 custom_handlers
[i
].handle(spec
, p
);
761 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
) {
762 uint64_t start
= get_address(spec
, &p
[1]);
764 if (p
[0] & (1 << 22)) {
765 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
766 * like a subroutine call. Commands that come afterwards get
767 * processed once the 2nd level batch buffer returns with
768 * MI_BATCH_BUFFER_END.
770 parse_commands(spec
, gtt
+ start
, gtt_end
- start
, engine
);
772 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
773 * like a goto. Nothing after it will ever get processed. In
774 * order to prevent the recursion from growing, we just reset the
778 /* We don't know where secondaries end so use the GTT end */
783 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
789 #define GEN_ENGINE_RENDER 1
790 #define GEN_ENGINE_BLITTER 2
793 handle_trace_block(uint32_t *p
)
795 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
796 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
797 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
798 uint64_t offset
= p
[3];
799 uint32_t size
= p
[4];
800 int header_length
= p
[0] & 0xffff;
801 uint32_t *data
= p
+ header_length
+ 2;
802 int engine
= GEN_ENGINE_RENDER
;
804 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
805 offset
+= (uint64_t) p
[5] << 32;
808 case AUB_TRACE_OP_DATA_WRITE
:
809 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
811 if (gtt_size
< offset
+ size
) {
812 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
815 memcpy((char *) gtt
+ offset
, data
, size
);
816 if (gtt_end
< offset
+ size
)
817 gtt_end
= offset
+ size
;
819 case AUB_TRACE_OP_COMMAND_WRITE
:
821 case AUB_TRACE_TYPE_RING_PRB0
:
822 engine
= GEN_ENGINE_RENDER
;
824 case AUB_TRACE_TYPE_RING_PRB2
:
825 engine
= GEN_ENGINE_BLITTER
;
828 fprintf(outfile
, "command write to unknown ring %d\n", type
);
832 parse_commands(spec
, data
, size
, engine
);
839 handle_trace_header(uint32_t *p
)
841 /* The intel_aubdump tool from IGT is kind enough to put a PCI-ID= tag in
842 * the AUB header comment. If the user hasn't specified a hardware
843 * generation, try to use the one from the AUB file.
845 uint32_t *end
= p
+ (p
[0] & 0xffff) + 2;
847 if (end
> &p
[12] && p
[12] > 0)
848 sscanf((char *)&p
[13], "PCI-ID=%i", &aub_pci_id
);
853 struct gen_device_info devinfo
;
854 if (!gen_get_device_info(pci_id
, &devinfo
)) {
855 fprintf(stderr
, "can't find device information: pci_id=0x%x\n", pci_id
);
859 if (xml_path
== NULL
)
860 spec
= gen_spec_load(&devinfo
);
862 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
863 disasm
= gen_disasm_create(pci_id
);
865 if (spec
== NULL
|| disasm
== NULL
)
868 fprintf(outfile
, "%sAubinator: Intel AUB file decoder.%-80s%s\n",
869 GREEN_HEADER
, "", NORMAL
);
872 fprintf(outfile
, "File name: %s\n", input_file
);
875 fprintf(outfile
, "PCI ID: 0x%x\n", aub_pci_id
);
878 strncpy(app_name
, (char *)&p
[2], 32);
880 fprintf(outfile
, "Application name: %s\n", app_name
);
882 fprintf(outfile
, "Decoding as: %s\n", gen_get_device_name(pci_id
));
884 /* Throw in a new line before the first batch */
885 fprintf(outfile
, "\n");
891 uint32_t *map
, *end
, *cursor
;
895 static struct aub_file
*
896 aub_file_open(const char *filename
)
898 struct aub_file
*file
;
902 file
= calloc(1, sizeof *file
);
903 fd
= open(filename
, O_RDONLY
);
905 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
909 if (fstat(fd
, &sb
) == -1) {
910 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
914 file
->map
= mmap(NULL
, sb
.st_size
,
915 PROT_READ
, MAP_SHARED
, fd
, 0);
916 if (file
->map
== MAP_FAILED
) {
917 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
921 file
->cursor
= file
->map
;
922 file
->end
= file
->map
+ sb
.st_size
/ 4;
927 static struct aub_file
*
930 struct aub_file
*file
;
932 file
= calloc(1, sizeof *file
);
933 file
->stream
= stdin
;
938 #define TYPE(dw) (((dw) >> 29) & 7)
939 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
940 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
942 #define MAKE_HEADER(type, opcode, subopcode) \
943 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
947 /* Classic AUB opcodes */
948 #define OPCODE_AUB 0x01
949 #define SUBOPCODE_HEADER 0x05
950 #define SUBOPCODE_BLOCK 0x41
951 #define SUBOPCODE_BMP 0x1e
953 /* Newer version AUB opcode */
954 #define OPCODE_NEW_AUB 0x2e
955 #define SUBOPCODE_VERSION 0x00
956 #define SUBOPCODE_REG_WRITE 0x03
957 #define SUBOPCODE_MEM_POLL 0x05
958 #define SUBOPCODE_MEM_WRITE 0x06
960 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
966 { "bwr", MAKE_GEN(4, 0) },
967 { "cln", MAKE_GEN(4, 0) },
968 { "blc", MAKE_GEN(4, 0) },
969 { "ctg", MAKE_GEN(4, 0) },
970 { "el", MAKE_GEN(4, 0) },
971 { "il", MAKE_GEN(4, 0) },
972 { "sbr", MAKE_GEN(6, 0) },
973 { "ivb", MAKE_GEN(7, 0) },
974 { "lrb2", MAKE_GEN(0, 0) },
975 { "hsw", MAKE_GEN(7, 5) },
976 { "vlv", MAKE_GEN(7, 0) },
977 { "bdw", MAKE_GEN(8, 0) },
978 { "skl", MAKE_GEN(9, 0) },
979 { "chv", MAKE_GEN(8, 0) },
980 { "bxt", MAKE_GEN(9, 0) }
985 AUB_ITEM_DECODE_FAILED
,
986 AUB_ITEM_DECODE_NEED_MORE_DATA
,
990 aub_file_decode_batch(struct aub_file
*file
)
992 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
993 int header_length
, bias
;
995 if (file
->end
- file
->cursor
< 1)
996 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1000 header_length
= h
& 0xffff;
1002 switch (OPCODE(h
)) {
1006 case OPCODE_NEW_AUB
:
1010 fprintf(outfile
, "unknown opcode %d at %td/%td\n",
1011 OPCODE(h
), file
->cursor
- file
->map
,
1012 file
->end
- file
->map
);
1013 return AUB_ITEM_DECODE_FAILED
;
1016 new_cursor
= p
+ header_length
+ bias
;
1017 if ((h
& 0xffff0000) == MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
)) {
1018 if (file
->end
- file
->cursor
< 4)
1019 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1020 new_cursor
+= p
[4] / 4;
1023 if (new_cursor
> file
->end
)
1024 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1026 switch (h
& 0xffff0000) {
1027 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
1028 handle_trace_header(p
);
1030 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
1031 handle_trace_block(p
);
1033 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
1035 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
1036 fprintf(outfile
, "version block: dw1 %08x\n", p
[1]);
1037 device
= (p
[1] >> 8) & 0xff;
1038 fprintf(outfile
, " device %s\n", device_map
[device
].name
);
1040 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
1041 fprintf(outfile
, "register write block: (dwords %d)\n", h
& 0xffff);
1042 fprintf(outfile
, " reg 0x%x, data 0x%x\n", p
[1], p
[5]);
1044 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1045 fprintf(outfile
, "memory write block (dwords %d):\n", h
& 0xffff);
1046 fprintf(outfile
, " address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1047 data_type
= (p
[3] >> 20) & 0xff;
1049 fprintf(outfile
, " data type 0x%x\n", data_type
);
1050 fprintf(outfile
, " address space 0x%x\n", (p
[3] >> 28) & 0xf);
1052 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1053 fprintf(outfile
, "memory poll block (dwords %d):\n", h
& 0xffff);
1056 fprintf(outfile
, "unknown block type=0x%x, opcode=0x%x, "
1057 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1060 file
->cursor
= new_cursor
;
1062 return AUB_ITEM_DECODE_OK
;
1066 aub_file_more_stuff(struct aub_file
*file
)
1068 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1071 #define AUB_READ_BUFFER_SIZE (4096)
1072 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1075 aub_file_data_grow(struct aub_file
*file
)
1077 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1078 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1079 uint32_t *new_start
= realloc(file
->map
, new_size
);
1081 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1082 file
->end
= new_start
+ (file
->end
- file
->map
);
1083 file
->map
= new_start
;
1084 file
->mem_end
= file
->map
+ (new_size
/ 4);
1088 aub_file_data_load(struct aub_file
*file
)
1092 if (file
->stream
== NULL
)
1095 /* First remove any consumed data */
1096 if (file
->cursor
> file
->map
) {
1097 memmove(file
->map
, file
->cursor
,
1098 (file
->end
- file
->cursor
) * 4);
1099 file
->end
-= file
->cursor
- file
->map
;
1100 file
->cursor
= file
->map
;
1103 /* Then load some new data in */
1104 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1105 aub_file_data_grow(file
);
1107 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1122 if (pipe(fds
) == -1)
1132 execlp("less", "less", "-FRSi", NULL
);
1141 print_help(const char *progname
, FILE *file
)
1144 "Usage: %s [OPTION]... [FILE]\n"
1145 "Decode aub file contents from either FILE or the standard input.\n\n"
1146 "A valid --gen option must be provided.\n\n"
1147 " --help display this help and exit\n"
1148 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1149 " --headers decode only command headers\n"
1150 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1151 " if omitted), 'always', or 'never'\n"
1152 " --no-pager don't launch pager\n"
1153 " --no-offsets don't print instruction offsets\n"
1154 " --xml=DIR load hardware xml description from directory DIR\n",
1158 int main(int argc
, char *argv
[])
1160 struct aub_file
*file
;
1162 bool help
= false, pager
= true;
1167 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1168 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1169 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1170 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1171 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1172 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1173 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1174 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1176 const struct option aubinator_opts
[] = {
1177 { "help", no_argument
, (int *) &help
, true },
1178 { "no-pager", no_argument
, (int *) &pager
, false },
1179 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1180 { "gen", required_argument
, NULL
, 'g' },
1181 { "headers", no_argument
, (int *) &option_full_decode
, false },
1182 { "color", required_argument
, NULL
, 'c' },
1183 { "xml", required_argument
, NULL
, 'x' },
1184 { NULL
, 0, NULL
, 0 }
1190 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1193 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1194 if (!strcmp(optarg
, gens
[i
].name
)) {
1195 pci_id
= gens
[i
].pci_id
;
1199 if (i
== ARRAY_SIZE(gens
)) {
1200 fprintf(stderr
, "can't parse gen: '%s', expected ivb, byt, hsw, "
1201 "bdw, chv, skl, kbl or bxt\n", optarg
);
1206 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1207 option_color
= COLOR_ALWAYS
;
1208 else if (strcmp(optarg
, "never") == 0)
1209 option_color
= COLOR_NEVER
;
1210 else if (strcmp(optarg
, "auto") == 0)
1211 option_color
= COLOR_AUTO
;
1213 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1218 xml_path
= strdup(optarg
);
1225 if (help
|| argc
== 1) {
1226 print_help(argv
[0], stderr
);
1231 input_file
= argv
[optind
];
1233 /* Do this before we redirect stdout to pager. */
1234 if (option_color
== COLOR_AUTO
)
1235 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1237 if (isatty(1) && pager
)
1240 if (input_file
== NULL
)
1241 file
= aub_file_stdin();
1243 file
= aub_file_open(input_file
);
1245 /* mmap a terabyte for our gtt space. */
1246 gtt_size
= 1ull << 40;
1247 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1248 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1249 if (gtt
== MAP_FAILED
) {
1250 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1254 while (aub_file_more_stuff(file
)) {
1255 switch (aub_file_decode_batch(file
)) {
1256 case AUB_ITEM_DECODE_OK
:
1258 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1259 if (!file
->stream
) {
1260 file
->cursor
= file
->end
;
1263 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1264 fprintf(stderr
, "failed to load data from stdin\n");
1269 fprintf(stderr
, "failed to parse aubdump data\n");
1276 /* close the stdout which is opened to write the output */
1282 return EXIT_SUCCESS
;