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
;
78 static inline uint32_t
79 field(uint32_t value
, int start
, int end
)
83 mask
= ~0U >> (31 - end
+ start
);
85 return (value
>> start
) & mask
;
88 struct brw_instruction
;
91 valid_offset(uint32_t offset
)
93 return offset
< gtt_end
;
97 print_dword_val(struct gen_field_iterator
*iter
, uint64_t offset
,
102 f
= iter
->group
->fields
[iter
->i
- 1];
103 const int dword
= f
->start
/ 32;
105 if (*dword_num
!= dword
) {
106 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
107 offset
+ 4 * dword
, iter
->p
[dword
], dword
);
113 print_iterator_values(struct gen_field_iterator
*iter
, int *idx
)
116 if (strstr(iter
->value
, "struct") == NULL
) {
117 if (strlen(iter
->description
) > 0) {
118 printf(" %s: %s (%s)\n",
119 iter
->name
, iter
->value
, iter
->description
);
121 printf(" %s: %s\n", iter
->name
, iter
->value
);
124 token
= strtok(iter
->value
, " ");
126 token
= strtok(NULL
, " ");
127 *idx
= atoi(strtok(NULL
, ">"));
131 printf(" %s:<struct %s>\n", iter
->name
, token
);
137 decode_structure(struct gen_spec
*spec
, struct gen_group
*strct
,
140 struct gen_field_iterator iter
;
142 int idx
= 0, dword_num
= 0;
145 if (option_print_offsets
)
146 offset
= (void *) p
- gtt
;
150 gen_field_iterator_init(&iter
, strct
, p
,
151 option_color
== COLOR_ALWAYS
);
152 while (gen_field_iterator_next(&iter
)) {
154 print_dword_val(&iter
, offset
, &dword_num
);
155 token
= print_iterator_values(&iter
, &idx
);
157 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, token
);
158 decode_structure(spec
, struct_val
, &p
[idx
]);
165 handle_struct_decode(struct gen_spec
*spec
, char *struct_name
, uint32_t *p
)
167 if (struct_name
== NULL
)
169 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, struct_name
);
170 decode_structure(spec
, struct_val
, p
);
174 dump_binding_table(struct gen_spec
*spec
, uint32_t offset
)
176 uint32_t *pointers
, i
;
178 struct gen_group
*surface_state
;
180 surface_state
= gen_spec_find_struct(spec
, "RENDER_SURFACE_STATE");
181 if (surface_state
== NULL
) {
182 printf("did not find RENDER_SURFACE_STATE info\n");
186 start
= surface_state_base
+ offset
;
187 pointers
= gtt
+ start
;
188 for (i
= 0; i
< 16; i
++) {
189 if (pointers
[i
] == 0)
191 start
= pointers
[i
] + surface_state_base
;
192 if (!valid_offset(start
)) {
193 printf("pointer %u: %08x <not valid>\n",
197 printf("pointer %u: %08x\n", i
, pointers
[i
]);
200 decode_structure(spec
, surface_state
, gtt
+ start
);
205 handle_3dstate_index_buffer(struct gen_spec
*spec
, uint32_t *p
)
208 uint32_t length
, i
, type
, size
;
211 type
= (p
[1] >> 8) & 3;
213 length
= p
[4] / size
;
219 for (i
= 0; i
< length
; i
++) {
222 printf("%3d ", ((uint8_t *)start
)[i
]);
225 printf("%3d ", ((uint16_t *)start
)[i
]);
228 printf("%3d ", ((uint32_t *)start
)[i
]);
232 if (length
< p
[4] / size
)
238 static inline uint64_t
239 get_address(struct gen_spec
*spec
, uint32_t *p
)
241 /* Addresses are always guaranteed to be page-aligned and sometimes
242 * hardware packets have extra stuff stuffed in the bottom 12 bits.
244 uint64_t addr
= p
[0] & ~0xfffu
;
246 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
247 /* On Broadwell and above, we have 48-bit addresses which consume two
248 * dwords. Some packets require that these get stored in a "canonical
249 * form" which means that bit 47 is sign-extended through the upper
250 * bits. In order to correctly handle those aub dumps, we need to mask
251 * off the top 16 bits.
253 addr
|= ((uint64_t)p
[1] & 0xffff) << 32;
259 static inline uint64_t
260 get_offset(uint32_t *p
, uint32_t start
, uint32_t end
)
262 assert(start
<= end
);
265 uint64_t mask
= (~0ull >> (64 - (end
- start
+ 1))) << start
;
267 uint64_t offset
= p
[0];
269 offset
|= (uint64_t) p
[1] << 32;
271 return offset
& mask
;
275 handle_state_base_address(struct gen_spec
*spec
, uint32_t *p
)
277 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
279 general_state_base
= get_address(spec
, &p
[1]);
281 surface_state_base
= get_address(spec
, &p
[4]);
283 dynamic_state_base
= get_address(spec
, &p
[6]);
285 instruction_base
= get_address(spec
, &p
[10]);
287 instruction_bound
= p
[15] & 0xfff;
290 surface_state_base
= get_address(spec
, &p
[2]);
292 dynamic_state_base
= get_address(spec
, &p
[3]);
294 instruction_base
= get_address(spec
, &p
[5]);
296 instruction_bound
= get_address(spec
, &p
[9]);
301 dump_samplers(struct gen_spec
*spec
, uint32_t offset
)
305 struct gen_group
*sampler_state
;
307 sampler_state
= gen_spec_find_struct(spec
, "SAMPLER_STATE");
309 start
= dynamic_state_base
+ offset
;
310 for (i
= 0; i
< 4; i
++) {
311 printf("sampler state %d\n", i
);
312 decode_structure(spec
, sampler_state
, gtt
+ start
+ i
* 16);
317 handle_media_interface_descriptor_load(struct gen_spec
*spec
, uint32_t *p
)
319 int i
, length
= p
[2] / 32;
320 struct gen_group
*descriptor_structure
;
321 uint32_t *descriptors
;
323 struct brw_instruction
*insns
;
325 descriptor_structure
=
326 gen_spec_find_struct(spec
, "INTERFACE_DESCRIPTOR_DATA");
327 if (descriptor_structure
== NULL
) {
328 printf("did not find INTERFACE_DESCRIPTOR_DATA info\n");
332 start
= dynamic_state_base
+ p
[3];
333 descriptors
= gtt
+ start
;
334 for (i
= 0; i
< length
; i
++, descriptors
+= 8) {
335 printf("descriptor %u: %08x\n", i
, *descriptors
);
336 decode_structure(spec
, descriptor_structure
, descriptors
);
338 start
= instruction_base
+ descriptors
[0];
339 if (!valid_offset(start
)) {
340 printf("kernel: %08"PRIx64
" <not valid>\n", start
);
343 printf("kernel: %08"PRIx64
"\n", start
);
346 insns
= (struct brw_instruction
*) (gtt
+ start
);
347 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
349 dump_samplers(spec
, descriptors
[3] & ~0x1f);
350 dump_binding_table(spec
, descriptors
[4] & ~0x1f);
354 /* Heuristic to determine whether a uint32_t is probably actually a float
355 * (http://stackoverflow.com/a/2953466)
359 probably_float(uint32_t bits
)
361 int exp
= ((bits
& 0x7f800000U
) >> 23) - 127;
362 uint32_t mant
= bits
& 0x007fffff;
365 if (exp
== -127 && mant
== 0)
368 /* +- 1 billionth to 1 billion */
369 if (-30 <= exp
&& exp
<= 30)
372 /* some value with only a few binary digits */
373 if ((mant
& 0x0000ffff) == 0)
380 handle_3dstate_vertex_buffers(struct gen_spec
*spec
, uint32_t *p
)
382 uint32_t *end
, *s
, *dw
, *dwend
;
384 int n
, i
, count
, stride
;
386 end
= (p
[0] & 0xff) + p
+ 2;
387 for (s
= &p
[1], n
= 0; s
< end
; s
+= 4, n
++) {
388 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
389 offset
= *(uint64_t *) &s
[1];
390 dwend
= gtt
+ offset
+ s
[3];
393 dwend
= gtt
+ s
[2] + 1;
396 stride
= field(s
[0], 0, 11);
398 printf("vertex buffer %d, size %d\n", n
, s
[3]);
399 for (dw
= gtt
+ offset
, i
= 0; dw
< dwend
&& i
< 256; dw
++) {
400 if (count
== 0 && count
% (8 * 4) == 0)
403 if (probably_float(*dw
))
404 printf(" %8.2f", *(float *) dw
);
406 printf(" 0x%08x", *dw
);
411 if (count
== stride
) {
414 } else if (count
% (8 * 4) == 0) {
420 if (count
> 0 && count
% (8 * 4) != 0)
426 handle_3dstate_vs(struct gen_spec
*spec
, uint32_t *p
)
429 struct brw_instruction
*insns
;
432 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
433 start
= get_offset(&p
[1], 6, 63);
434 vs_enable
= p
[7] & 1;
436 start
= get_offset(&p
[1], 6, 31);
437 vs_enable
= p
[5] & 1;
441 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
442 instruction_base
, start
);
444 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
445 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
450 handle_3dstate_hs(struct gen_spec
*spec
, uint32_t *p
)
453 struct brw_instruction
*insns
;
456 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
457 start
= get_offset(&p
[3], 6, 63);
459 start
= get_offset(&p
[3], 6, 31);
462 hs_enable
= p
[2] & 0x80000000;
465 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
466 instruction_base
, start
);
468 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
469 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
474 handle_3dstate_constant(struct gen_spec
*spec
, uint32_t *p
)
480 for (i
= 0; i
< 4; i
++) {
481 length
= (p
[1 + i
/ 2] >> (i
& 1) * 16) & 0xffff;
482 f
= (float *) (gtt
+ p
[3 + i
* 2] + dynamic_state_base
);
484 for (j
= 0; j
< length
* 8; j
++) {
485 if (probably_float(dw
[j
]))
486 printf(" %04.3f", f
[j
]);
488 printf(" 0x%08x", dw
[j
]);
497 handle_3dstate_ps(struct gen_spec
*spec
, uint32_t *p
)
499 uint32_t mask
= ~((1 << 6) - 1);
501 struct brw_instruction
*insns
;
502 static const char unused
[] = "unused";
503 static const char *pixel_type
[3] = {"8 pixel", "16 pixel", "32 pixel"};
504 const char *k0
, *k1
, *k2
;
505 uint32_t k_mask
, k1_offset
, k2_offset
;
507 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
518 #define DISPATCH_16 2
519 #define DISPATCH_32 4
532 case DISPATCH_8
| DISPATCH_16
:
542 case DISPATCH_16
| DISPATCH_32
:
547 case DISPATCH_8
| DISPATCH_16
| DISPATCH_32
:
559 start
= instruction_base
+ (p
[1] & mask
);
560 printf(" Kernel[0] %s\n", k0
);
562 insns
= (struct brw_instruction
*) (gtt
+ start
);
563 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
566 start
= instruction_base
+ (p
[k1_offset
] & mask
);
567 printf(" Kernel[1] %s\n", k1
);
569 insns
= (struct brw_instruction
*) (gtt
+ start
);
570 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
573 start
= instruction_base
+ (p
[k2_offset
] & mask
);
574 printf(" Kernel[2] %s\n", k2
);
576 insns
= (struct brw_instruction
*) (gtt
+ start
);
577 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
582 handle_3dstate_binding_table_pointers(struct gen_spec
*spec
, uint32_t *p
)
584 dump_binding_table(spec
, p
[1]);
588 handle_3dstate_sampler_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
590 dump_samplers(spec
, p
[1]);
594 handle_3dstate_viewport_state_pointers_cc(struct gen_spec
*spec
, uint32_t *p
)
597 struct gen_group
*cc_viewport
;
599 cc_viewport
= gen_spec_find_struct(spec
, "CC_VIEWPORT");
601 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
602 for (uint32_t i
= 0; i
< 4; i
++) {
603 printf("viewport %d\n", i
);
604 decode_structure(spec
, cc_viewport
, gtt
+ start
+ i
* 8);
609 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec
*spec
,
613 struct gen_group
*sf_clip_viewport
;
615 sf_clip_viewport
= gen_spec_find_struct(spec
, "SF_CLIP_VIEWPORT");
617 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
618 for (uint32_t i
= 0; i
< 4; i
++) {
619 printf("viewport %d\n", i
);
620 decode_structure(spec
, sf_clip_viewport
, gtt
+ start
+ i
* 64);
625 handle_3dstate_blend_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
628 struct gen_group
*blend_state
;
630 blend_state
= gen_spec_find_struct(spec
, "BLEND_STATE");
632 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
633 decode_structure(spec
, blend_state
, gtt
+ start
);
637 handle_3dstate_cc_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
640 struct gen_group
*cc_state
;
642 cc_state
= gen_spec_find_struct(spec
, "COLOR_CALC_STATE");
644 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
645 decode_structure(spec
, cc_state
, gtt
+ start
);
649 handle_3dstate_scissor_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
652 struct gen_group
*scissor_rect
;
654 scissor_rect
= gen_spec_find_struct(spec
, "SCISSOR_RECT");
656 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
657 decode_structure(spec
, scissor_rect
, gtt
+ start
);
661 handle_load_register_imm(struct gen_spec
*spec
, uint32_t *p
)
663 struct gen_group
*reg
= gen_spec_find_register(spec
, p
[1]);
666 printf("register %s (0x%x): 0x%x\n",
667 reg
->name
, reg
->register_offset
, p
[2]);
668 decode_structure(spec
, reg
, &p
[2]);
672 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
674 #define STATE_BASE_ADDRESS 0x61010000
676 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
678 #define _3DSTATE_INDEX_BUFFER 0x780a0000
679 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
681 #define _3DSTATE_VS 0x78100000
682 #define _3DSTATE_GS 0x78110000
683 #define _3DSTATE_HS 0x781b0000
684 #define _3DSTATE_DS 0x781d0000
686 #define _3DSTATE_CONSTANT_VS 0x78150000
687 #define _3DSTATE_CONSTANT_GS 0x78160000
688 #define _3DSTATE_CONSTANT_PS 0x78170000
689 #define _3DSTATE_CONSTANT_HS 0x78190000
690 #define _3DSTATE_CONSTANT_DS 0x781A0000
692 #define _3DSTATE_PS 0x78200000
694 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
695 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
696 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
697 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
698 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
700 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
701 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
702 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
704 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
705 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
706 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
707 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
708 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
710 #define _MI_LOAD_REGISTER_IMM 0x11000000
712 struct custom_handler
{
714 void (*handle
)(struct gen_spec
*spec
, uint32_t *p
);
715 } custom_handlers
[] = {
716 { STATE_BASE_ADDRESS
, handle_state_base_address
},
717 { MEDIA_INTERFACE_DESCRIPTOR_LOAD
, handle_media_interface_descriptor_load
},
718 { _3DSTATE_VERTEX_BUFFERS
, handle_3dstate_vertex_buffers
},
719 { _3DSTATE_INDEX_BUFFER
, handle_3dstate_index_buffer
},
720 { _3DSTATE_VS
, handle_3dstate_vs
},
721 { _3DSTATE_GS
, handle_3dstate_vs
},
722 { _3DSTATE_DS
, handle_3dstate_vs
},
723 { _3DSTATE_HS
, handle_3dstate_hs
},
724 { _3DSTATE_CONSTANT_VS
, handle_3dstate_constant
},
725 { _3DSTATE_CONSTANT_GS
, handle_3dstate_constant
},
726 { _3DSTATE_CONSTANT_PS
, handle_3dstate_constant
},
727 { _3DSTATE_CONSTANT_HS
, handle_3dstate_constant
},
728 { _3DSTATE_CONSTANT_DS
, handle_3dstate_constant
},
729 { _3DSTATE_PS
, handle_3dstate_ps
},
731 { _3DSTATE_BINDING_TABLE_POINTERS_VS
, handle_3dstate_binding_table_pointers
},
732 { _3DSTATE_BINDING_TABLE_POINTERS_HS
, handle_3dstate_binding_table_pointers
},
733 { _3DSTATE_BINDING_TABLE_POINTERS_DS
, handle_3dstate_binding_table_pointers
},
734 { _3DSTATE_BINDING_TABLE_POINTERS_GS
, handle_3dstate_binding_table_pointers
},
735 { _3DSTATE_BINDING_TABLE_POINTERS_PS
, handle_3dstate_binding_table_pointers
},
737 { _3DSTATE_SAMPLER_STATE_POINTERS_VS
, handle_3dstate_sampler_state_pointers
},
738 { _3DSTATE_SAMPLER_STATE_POINTERS_GS
, handle_3dstate_sampler_state_pointers
},
739 { _3DSTATE_SAMPLER_STATE_POINTERS_PS
, handle_3dstate_sampler_state_pointers
},
741 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC
, handle_3dstate_viewport_state_pointers_cc
},
742 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
, handle_3dstate_viewport_state_pointers_sf_clip
},
743 { _3DSTATE_BLEND_STATE_POINTERS
, handle_3dstate_blend_state_pointers
},
744 { _3DSTATE_CC_STATE_POINTERS
, handle_3dstate_cc_state_pointers
},
745 { _3DSTATE_SCISSOR_STATE_POINTERS
, handle_3dstate_scissor_state_pointers
},
746 { _MI_LOAD_REGISTER_IMM
, handle_load_register_imm
}
750 parse_commands(struct gen_spec
*spec
, uint32_t *cmds
, int size
, int engine
)
752 uint32_t *p
, *end
= cmds
+ size
/ 4;
753 unsigned int length
, i
;
754 struct gen_group
*inst
;
756 for (p
= cmds
; p
< end
; p
+= length
) {
757 inst
= gen_spec_find_instruction(spec
, p
);
759 printf("unknown instruction %08x\n", p
[0]);
760 length
= (p
[0] & 0xff) + 2;
763 length
= gen_group_get_length(inst
, p
);
765 const char *color
, *reset_color
= NORMAL
;
768 if (option_full_decode
) {
769 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
||
770 (p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
)
771 color
= GREEN_HEADER
;
777 if (option_color
== COLOR_NEVER
) {
782 if (option_print_offsets
)
783 offset
= (void *) p
- gtt
;
787 printf("%s0x%08"PRIx64
": 0x%08x: %-80s%s\n",
789 gen_group_get_name(inst
), reset_color
);
791 if (option_full_decode
) {
792 struct gen_field_iterator iter
;
794 int idx
= 0, dword_num
= 0;
795 gen_field_iterator_init(&iter
, inst
, p
,
796 option_color
== COLOR_ALWAYS
);
797 while (gen_field_iterator_next(&iter
)) {
799 print_dword_val(&iter
, offset
, &dword_num
);
801 token
= print_iterator_values(&iter
, &idx
);
803 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
804 offset
+ 4 * idx
, p
[idx
], idx
);
805 handle_struct_decode(spec
,token
, &p
[idx
]);
810 for (i
= 0; i
< ARRAY_LENGTH(custom_handlers
); i
++) {
811 if (gen_group_get_opcode(inst
) == custom_handlers
[i
].opcode
)
812 custom_handlers
[i
].handle(spec
, p
);
816 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
) {
817 uint64_t start
= get_address(spec
, &p
[1]);
819 if (p
[0] & (1 << 22)) {
820 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
821 * like a subroutine call. Commands that come afterwards get
822 * processed once the 2nd level batch buffer returns with
823 * MI_BATCH_BUFFER_END.
825 parse_commands(spec
, gtt
+ start
, gtt_end
- start
, engine
);
827 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
828 * like a goto. Nothing after it will ever get processed. In
829 * order to prevent the recursion from growing, we just reset the
833 /* We don't know where secondaries end so use the GTT end */
838 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
844 #define GEN_ENGINE_RENDER 1
845 #define GEN_ENGINE_BLITTER 2
848 handle_trace_block(uint32_t *p
)
850 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
851 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
852 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
853 uint64_t offset
= p
[3];
854 uint32_t size
= p
[4];
855 int header_length
= p
[0] & 0xffff;
856 uint32_t *data
= p
+ header_length
+ 2;
857 int engine
= GEN_ENGINE_RENDER
;
859 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
860 offset
+= (uint64_t) p
[5] << 32;
863 case AUB_TRACE_OP_DATA_WRITE
:
864 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
866 if (gtt_size
< offset
+ size
) {
867 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
870 memcpy((char *) gtt
+ offset
, data
, size
);
871 if (gtt_end
< offset
+ size
)
872 gtt_end
= offset
+ size
;
874 case AUB_TRACE_OP_COMMAND_WRITE
:
876 case AUB_TRACE_TYPE_RING_PRB0
:
877 engine
= GEN_ENGINE_RENDER
;
879 case AUB_TRACE_TYPE_RING_PRB2
:
880 engine
= GEN_ENGINE_BLITTER
;
883 printf("command write to unknown ring %d\n", type
);
887 parse_commands(spec
, data
, size
, engine
);
894 handle_trace_header(uint32_t *p
)
896 /* The intel_aubdump tool from IGT is kind enough to put a PCI-ID= tag in
897 * the AUB header comment. If the user hasn't specified a hardware
898 * generation, try to use the one from the AUB file.
900 uint32_t *end
= p
+ (p
[0] & 0xffff) + 2;
902 if (end
> &p
[12] && p
[12] > 0)
903 sscanf((char *)&p
[13], "PCI-ID=%i", &aub_pci_id
);
908 struct gen_device_info devinfo
;
909 if (!gen_get_device_info(pci_id
, &devinfo
)) {
910 fprintf(stderr
, "can't find device information: pci_id=0x%x\n", pci_id
);
914 if (xml_path
== NULL
)
915 spec
= gen_spec_load(&devinfo
);
917 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
918 disasm
= gen_disasm_create(pci_id
);
920 if (spec
== NULL
|| disasm
== NULL
)
923 printf("%sAubinator: Intel AUB file decoder.%-80s%s\n",
924 GREEN_HEADER
, "", NORMAL
);
927 printf("File name: %s\n", input_file
);
930 printf("PCI ID: 0x%x\n", aub_pci_id
);
933 strncpy(app_name
, (char *)&p
[2], 32);
935 printf("Application name: %s\n", app_name
);
937 printf("Decoding as: %s\n", gen_get_device_name(pci_id
));
939 /* Throw in a new line before the first batch */
946 uint32_t *map
, *end
, *cursor
;
950 static struct aub_file
*
951 aub_file_open(const char *filename
)
953 struct aub_file
*file
;
957 file
= calloc(1, sizeof *file
);
958 fd
= open(filename
, O_RDONLY
);
960 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
964 if (fstat(fd
, &sb
) == -1) {
965 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
969 file
->map
= mmap(NULL
, sb
.st_size
,
970 PROT_READ
, MAP_SHARED
, fd
, 0);
971 if (file
->map
== MAP_FAILED
) {
972 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
976 file
->cursor
= file
->map
;
977 file
->end
= file
->map
+ sb
.st_size
/ 4;
982 static struct aub_file
*
985 struct aub_file
*file
;
987 file
= calloc(1, sizeof *file
);
988 file
->stream
= stdin
;
993 #define TYPE(dw) (((dw) >> 29) & 7)
994 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
995 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
997 #define MAKE_HEADER(type, opcode, subopcode) \
998 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
1000 #define TYPE_AUB 0x7
1002 /* Classic AUB opcodes */
1003 #define OPCODE_AUB 0x01
1004 #define SUBOPCODE_HEADER 0x05
1005 #define SUBOPCODE_BLOCK 0x41
1006 #define SUBOPCODE_BMP 0x1e
1008 /* Newer version AUB opcode */
1009 #define OPCODE_NEW_AUB 0x2e
1010 #define SUBOPCODE_VERSION 0x00
1011 #define SUBOPCODE_REG_WRITE 0x03
1012 #define SUBOPCODE_MEM_POLL 0x05
1013 #define SUBOPCODE_MEM_WRITE 0x06
1015 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
1021 { "bwr", MAKE_GEN(4, 0) },
1022 { "cln", MAKE_GEN(4, 0) },
1023 { "blc", MAKE_GEN(4, 0) },
1024 { "ctg", MAKE_GEN(4, 0) },
1025 { "el", MAKE_GEN(4, 0) },
1026 { "il", MAKE_GEN(4, 0) },
1027 { "sbr", MAKE_GEN(6, 0) },
1028 { "ivb", MAKE_GEN(7, 0) },
1029 { "lrb2", MAKE_GEN(0, 0) },
1030 { "hsw", MAKE_GEN(7, 5) },
1031 { "vlv", MAKE_GEN(7, 0) },
1032 { "bdw", MAKE_GEN(8, 0) },
1033 { "skl", MAKE_GEN(9, 0) },
1034 { "chv", MAKE_GEN(8, 0) },
1035 { "bxt", MAKE_GEN(9, 0) }
1040 AUB_ITEM_DECODE_FAILED
,
1041 AUB_ITEM_DECODE_NEED_MORE_DATA
,
1045 aub_file_decode_batch(struct aub_file
*file
)
1047 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
1048 int header_length
, bias
;
1050 if (file
->end
- file
->cursor
< 1)
1051 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1055 header_length
= h
& 0xffff;
1057 switch (OPCODE(h
)) {
1061 case OPCODE_NEW_AUB
:
1065 printf("unknown opcode %d at %td/%td\n",
1066 OPCODE(h
), file
->cursor
- file
->map
,
1067 file
->end
- file
->map
);
1068 return AUB_ITEM_DECODE_FAILED
;
1071 new_cursor
= p
+ header_length
+ bias
;
1072 if ((h
& 0xffff0000) == MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
)) {
1073 if (file
->end
- file
->cursor
< 4)
1074 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1075 new_cursor
+= p
[4] / 4;
1078 if (new_cursor
> file
->end
)
1079 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1081 switch (h
& 0xffff0000) {
1082 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
1083 handle_trace_header(p
);
1085 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
1086 handle_trace_block(p
);
1088 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
1090 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
1091 printf("version block: dw1 %08x\n", p
[1]);
1092 device
= (p
[1] >> 8) & 0xff;
1093 printf(" device %s\n", device_map
[device
].name
);
1095 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
1096 printf("register write block: (dwords %d)\n", h
& 0xffff);
1097 printf(" reg 0x%x, data 0x%x\n", p
[1], p
[5]);
1099 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1100 printf("memory write block (dwords %d):\n", h
& 0xffff);
1101 printf(" address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1102 data_type
= (p
[3] >> 20) & 0xff;
1104 printf(" data type 0x%x\n", data_type
);
1105 printf(" address space 0x%x\n", (p
[3] >> 28) & 0xf);
1107 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1108 printf("memory poll block (dwords %d):\n", h
& 0xffff);
1111 printf("unknown block type=0x%x, opcode=0x%x, "
1112 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1115 file
->cursor
= new_cursor
;
1117 return AUB_ITEM_DECODE_OK
;
1121 aub_file_more_stuff(struct aub_file
*file
)
1123 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1126 #define AUB_READ_BUFFER_SIZE (4096)
1127 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1130 aub_file_data_grow(struct aub_file
*file
)
1132 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1133 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1134 uint32_t *new_start
= realloc(file
->map
, new_size
);
1136 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1137 file
->end
= new_start
+ (file
->end
- file
->map
);
1138 file
->map
= new_start
;
1139 file
->mem_end
= file
->map
+ (new_size
/ 4);
1143 aub_file_data_load(struct aub_file
*file
)
1147 if (file
->stream
== NULL
)
1150 /* First remove any consumed data */
1151 if (file
->cursor
> file
->map
) {
1152 memmove(file
->map
, file
->cursor
,
1153 (file
->end
- file
->cursor
) * 4);
1154 file
->end
-= file
->cursor
- file
->map
;
1155 file
->cursor
= file
->map
;
1158 /* Then load some new data in */
1159 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1160 aub_file_data_grow(file
);
1162 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1177 if (pipe(fds
) == -1)
1187 execlp("less", "less", "-FRSi", NULL
);
1196 print_help(const char *progname
, FILE *file
)
1199 "Usage: %s [OPTION]... [FILE]\n"
1200 "Decode aub file contents from either FILE or the standard input.\n\n"
1201 "A valid --gen option must be provided.\n\n"
1202 " --help display this help and exit\n"
1203 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1204 " --headers decode only command headers\n"
1205 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1206 " if omitted), 'always', or 'never'\n"
1207 " --no-pager don't launch pager\n"
1208 " --no-offsets don't print instruction offsets\n"
1209 " --xml=DIR load hardware xml description from directory DIR\n",
1213 int main(int argc
, char *argv
[])
1215 struct aub_file
*file
;
1217 bool help
= false, pager
= true;
1222 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1223 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1224 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1225 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1226 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1227 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1228 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1229 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1231 const struct option aubinator_opts
[] = {
1232 { "help", no_argument
, (int *) &help
, true },
1233 { "no-pager", no_argument
, (int *) &pager
, false },
1234 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1235 { "gen", required_argument
, NULL
, 'g' },
1236 { "headers", no_argument
, (int *) &option_full_decode
, false },
1237 { "color", required_argument
, NULL
, 'c' },
1238 { "xml", required_argument
, NULL
, 'x' },
1239 { NULL
, 0, NULL
, 0 }
1243 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1246 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1247 if (!strcmp(optarg
, gens
[i
].name
)) {
1248 pci_id
= gens
[i
].pci_id
;
1252 if (i
== ARRAY_SIZE(gens
)) {
1253 fprintf(stderr
, "can't parse gen: '%s', expected ivb, byt, hsw, "
1254 "bdw, chv, skl, kbl or bxt\n", optarg
);
1259 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1260 option_color
= COLOR_ALWAYS
;
1261 else if (strcmp(optarg
, "never") == 0)
1262 option_color
= COLOR_NEVER
;
1263 else if (strcmp(optarg
, "auto") == 0)
1264 option_color
= COLOR_AUTO
;
1266 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1271 xml_path
= strdup(optarg
);
1278 if (help
|| argc
== 1) {
1279 print_help(argv
[0], stderr
);
1284 input_file
= argv
[optind
];
1286 /* Do this before we redirect stdout to pager. */
1287 if (option_color
== COLOR_AUTO
)
1288 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1290 if (isatty(1) && pager
)
1293 if (input_file
== NULL
)
1294 file
= aub_file_stdin();
1296 file
= aub_file_open(input_file
);
1298 /* mmap a terabyte for our gtt space. */
1299 gtt_size
= 1ull << 40;
1300 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1301 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1302 if (gtt
== MAP_FAILED
) {
1303 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1307 while (aub_file_more_stuff(file
)) {
1308 switch (aub_file_decode_batch(file
)) {
1309 case AUB_ITEM_DECODE_OK
:
1311 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1312 if (!file
->stream
) {
1313 file
->cursor
= file
->end
;
1316 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1317 fprintf(stderr
, "failed to load data from stdin\n");
1322 fprintf(stderr
, "failed to parse aubdump data\n");
1329 /* close the stdout which is opened to write the output */
1335 return EXIT_SUCCESS
;