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_val(struct gen_field_iterator
*iter
, uint64_t offset
,
104 f
= iter
->group
->fields
[iter
->i
- 1];
105 const int dword
= f
->start
/ 32;
107 if (*dword_num
!= dword
) {
108 fprintf(outfile
, "0x%08"PRIx64
": 0x%08x : Dword %d\n",
109 offset
+ 4 * dword
, iter
->p
[dword
], dword
);
115 print_iterator_values(struct gen_field_iterator
*iter
, int *idx
)
118 if (strstr(iter
->value
, "struct") == NULL
) {
119 if (strlen(iter
->description
) > 0) {
120 fprintf(outfile
, " %s: %s (%s)\n",
121 iter
->name
, iter
->value
, iter
->description
);
123 fprintf(outfile
, " %s: %s\n", iter
->name
, iter
->value
);
126 token
= strtok(iter
->value
, " ");
128 token
= strtok(NULL
, " ");
129 *idx
= atoi(strtok(NULL
, ">"));
133 fprintf(outfile
, " %s:<struct %s>\n", iter
->name
, token
);
139 decode_group(struct gen_spec
*spec
, struct gen_group
*strct
,
140 const uint32_t *p
, int starting_dword
,
141 bool print_dword_headers
)
143 struct gen_field_iterator iter
;
145 int idx
= 0, dword_num
= 0;
148 if (option_print_offsets
)
149 offset
= (void *) p
- gtt
;
153 gen_field_iterator_init(&iter
, strct
, p
,
154 option_color
== COLOR_ALWAYS
);
155 while (gen_field_iterator_next(&iter
)) {
157 print_dword_val(&iter
, offset
, &dword_num
);
158 if (dword_num
>= starting_dword
)
159 token
= print_iterator_values(&iter
, &idx
);
161 fprintf(outfile
, "0x%08"PRIx64
": 0x%08x : Dword %d\n",
162 offset
+ 4 * idx
, p
[idx
], idx
);
163 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, token
);
164 decode_group(spec
, struct_val
, &p
[idx
], 0, false);
171 decode_structure(struct gen_spec
*spec
, struct gen_group
*strct
,
174 decode_group(spec
, strct
, p
, 0, false);
178 dump_binding_table(struct gen_spec
*spec
, uint32_t offset
)
180 uint32_t *pointers
, i
;
182 struct gen_group
*surface_state
;
184 surface_state
= gen_spec_find_struct(spec
, "RENDER_SURFACE_STATE");
185 if (surface_state
== NULL
) {
186 fprintf(outfile
, "did not find RENDER_SURFACE_STATE info\n");
190 start
= surface_state_base
+ offset
;
191 pointers
= gtt
+ start
;
192 for (i
= 0; i
< 16; i
++) {
193 if (pointers
[i
] == 0)
195 start
= pointers
[i
] + surface_state_base
;
196 if (!valid_offset(start
)) {
197 fprintf(outfile
, "pointer %u: %08x <not valid>\n",
201 fprintf(outfile
, "pointer %u: %08x\n", i
, pointers
[i
]);
204 decode_structure(spec
, surface_state
, gtt
+ start
);
209 handle_3dstate_index_buffer(struct gen_spec
*spec
, uint32_t *p
)
212 uint32_t length
, i
, type
, size
;
215 type
= (p
[1] >> 8) & 3;
217 length
= p
[4] / size
;
221 fprintf(outfile
, "\t");
223 for (i
= 0; i
< length
; i
++) {
226 fprintf(outfile
, "%3d ", ((uint8_t *)start
)[i
]);
229 fprintf(outfile
, "%3d ", ((uint16_t *)start
)[i
]);
232 fprintf(outfile
, "%3d ", ((uint32_t *)start
)[i
]);
236 if (length
< p
[4] / size
)
237 fprintf(outfile
, "...\n");
239 fprintf(outfile
, "\n");
242 static inline uint64_t
243 get_address(struct gen_spec
*spec
, uint32_t *p
)
245 /* Addresses are always guaranteed to be page-aligned and sometimes
246 * hardware packets have extra stuff stuffed in the bottom 12 bits.
248 uint64_t addr
= p
[0] & ~0xfffu
;
250 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
251 /* On Broadwell and above, we have 48-bit addresses which consume two
252 * dwords. Some packets require that these get stored in a "canonical
253 * form" which means that bit 47 is sign-extended through the upper
254 * bits. In order to correctly handle those aub dumps, we need to mask
255 * off the top 16 bits.
257 addr
|= ((uint64_t)p
[1] & 0xffff) << 32;
263 static inline uint64_t
264 get_offset(uint32_t *p
, uint32_t start
, uint32_t end
)
266 assert(start
<= end
);
269 uint64_t mask
= (~0ull >> (64 - (end
- start
+ 1))) << start
;
271 uint64_t offset
= p
[0];
273 offset
|= (uint64_t) p
[1] << 32;
275 return offset
& mask
;
279 handle_state_base_address(struct gen_spec
*spec
, uint32_t *p
)
281 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
283 general_state_base
= get_address(spec
, &p
[1]);
285 surface_state_base
= get_address(spec
, &p
[4]);
287 dynamic_state_base
= get_address(spec
, &p
[6]);
289 instruction_base
= get_address(spec
, &p
[10]);
291 instruction_bound
= p
[15] & 0xfff;
294 surface_state_base
= get_address(spec
, &p
[2]);
296 dynamic_state_base
= get_address(spec
, &p
[3]);
298 instruction_base
= get_address(spec
, &p
[5]);
300 instruction_bound
= get_address(spec
, &p
[9]);
305 dump_samplers(struct gen_spec
*spec
, uint32_t offset
)
309 struct gen_group
*sampler_state
;
311 sampler_state
= gen_spec_find_struct(spec
, "SAMPLER_STATE");
313 start
= dynamic_state_base
+ offset
;
314 for (i
= 0; i
< 4; i
++) {
315 fprintf(outfile
, "sampler state %d\n", i
);
316 decode_structure(spec
, sampler_state
, gtt
+ start
+ i
* 16);
321 handle_media_interface_descriptor_load(struct gen_spec
*spec
, uint32_t *p
)
323 int i
, length
= p
[2] / 32;
324 struct gen_group
*descriptor_structure
;
325 uint32_t *descriptors
;
327 struct brw_instruction
*insns
;
329 descriptor_structure
=
330 gen_spec_find_struct(spec
, "INTERFACE_DESCRIPTOR_DATA");
331 if (descriptor_structure
== NULL
) {
332 fprintf(outfile
, "did not find INTERFACE_DESCRIPTOR_DATA info\n");
336 start
= dynamic_state_base
+ p
[3];
337 descriptors
= gtt
+ start
;
338 for (i
= 0; i
< length
; i
++, descriptors
+= 8) {
339 fprintf(outfile
, "descriptor %u: %08x\n", i
, *descriptors
);
340 decode_structure(spec
, descriptor_structure
, descriptors
);
342 start
= instruction_base
+ descriptors
[0];
343 if (!valid_offset(start
)) {
344 fprintf(outfile
, "kernel: %08"PRIx64
" <not valid>\n", start
);
347 fprintf(outfile
, "kernel: %08"PRIx64
"\n", start
);
350 insns
= (struct brw_instruction
*) (gtt
+ start
);
351 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
353 dump_samplers(spec
, descriptors
[3] & ~0x1f);
354 dump_binding_table(spec
, descriptors
[4] & ~0x1f);
358 /* Heuristic to determine whether a uint32_t is probably actually a float
359 * (http://stackoverflow.com/a/2953466)
363 probably_float(uint32_t bits
)
365 int exp
= ((bits
& 0x7f800000U
) >> 23) - 127;
366 uint32_t mant
= bits
& 0x007fffff;
369 if (exp
== -127 && mant
== 0)
372 /* +- 1 billionth to 1 billion */
373 if (-30 <= exp
&& exp
<= 30)
376 /* some value with only a few binary digits */
377 if ((mant
& 0x0000ffff) == 0)
384 handle_3dstate_vertex_buffers(struct gen_spec
*spec
, uint32_t *p
)
386 uint32_t *end
, *s
, *dw
, *dwend
;
388 int n
, i
, count
, stride
;
390 end
= (p
[0] & 0xff) + p
+ 2;
391 for (s
= &p
[1], n
= 0; s
< end
; s
+= 4, n
++) {
392 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
393 offset
= *(uint64_t *) &s
[1];
394 dwend
= gtt
+ offset
+ s
[3];
397 dwend
= gtt
+ s
[2] + 1;
400 stride
= field(s
[0], 0, 11);
402 fprintf(outfile
, "vertex buffer %d, size %d\n", n
, s
[3]);
403 for (dw
= gtt
+ offset
, i
= 0; dw
< dwend
&& i
< 256; dw
++) {
404 if (count
== 0 && count
% (8 * 4) == 0)
405 fprintf(outfile
, " ");
407 if (probably_float(*dw
))
408 fprintf(outfile
, " %8.2f", *(float *) dw
);
410 fprintf(outfile
, " 0x%08x", *dw
);
415 if (count
== stride
) {
416 fprintf(outfile
, "\n");
418 } else if (count
% (8 * 4) == 0) {
419 fprintf(outfile
, "\n");
421 fprintf(outfile
, " ");
424 if (count
> 0 && count
% (8 * 4) != 0)
425 fprintf(outfile
, "\n");
430 handle_3dstate_vs(struct gen_spec
*spec
, uint32_t *p
)
433 struct brw_instruction
*insns
;
436 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
437 start
= get_offset(&p
[1], 6, 63);
438 vs_enable
= p
[7] & 1;
440 start
= get_offset(&p
[1], 6, 31);
441 vs_enable
= p
[5] & 1;
445 fprintf(outfile
, "instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
446 instruction_base
, start
);
448 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
449 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
454 handle_3dstate_hs(struct gen_spec
*spec
, uint32_t *p
)
457 struct brw_instruction
*insns
;
460 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
461 start
= get_offset(&p
[3], 6, 63);
463 start
= get_offset(&p
[3], 6, 31);
466 hs_enable
= p
[2] & 0x80000000;
469 fprintf(outfile
, "instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
470 instruction_base
, start
);
472 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
473 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
478 handle_3dstate_constant(struct gen_spec
*spec
, uint32_t *p
)
484 for (i
= 0; i
< 4; i
++) {
485 length
= (p
[1 + i
/ 2] >> (i
& 1) * 16) & 0xffff;
486 f
= (float *) (gtt
+ p
[3 + i
* 2] + dynamic_state_base
);
488 for (j
= 0; j
< length
* 8; j
++) {
489 if (probably_float(dw
[j
]))
490 fprintf(outfile
, " %04.3f", f
[j
]);
492 fprintf(outfile
, " 0x%08x", dw
[j
]);
495 fprintf(outfile
, "\n");
501 handle_3dstate_ps(struct gen_spec
*spec
, uint32_t *p
)
503 uint32_t mask
= ~((1 << 6) - 1);
505 struct brw_instruction
*insns
;
506 static const char unused
[] = "unused";
507 static const char *pixel_type
[3] = {"8 pixel", "16 pixel", "32 pixel"};
508 const char *k0
, *k1
, *k2
;
509 uint32_t k_mask
, k1_offset
, k2_offset
;
511 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
522 #define DISPATCH_16 2
523 #define DISPATCH_32 4
536 case DISPATCH_8
| DISPATCH_16
:
546 case DISPATCH_16
| DISPATCH_32
:
551 case DISPATCH_8
| DISPATCH_16
| DISPATCH_32
:
563 start
= instruction_base
+ (p
[1] & mask
);
564 fprintf(outfile
, " Kernel[0] %s\n", k0
);
566 insns
= (struct brw_instruction
*) (gtt
+ start
);
567 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
570 start
= instruction_base
+ (p
[k1_offset
] & mask
);
571 fprintf(outfile
, " Kernel[1] %s\n", k1
);
573 insns
= (struct brw_instruction
*) (gtt
+ start
);
574 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
577 start
= instruction_base
+ (p
[k2_offset
] & mask
);
578 fprintf(outfile
, " Kernel[2] %s\n", k2
);
580 insns
= (struct brw_instruction
*) (gtt
+ start
);
581 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
586 handle_3dstate_binding_table_pointers(struct gen_spec
*spec
, uint32_t *p
)
588 dump_binding_table(spec
, p
[1]);
592 handle_3dstate_sampler_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
594 dump_samplers(spec
, p
[1]);
598 handle_3dstate_viewport_state_pointers_cc(struct gen_spec
*spec
, uint32_t *p
)
601 struct gen_group
*cc_viewport
;
603 cc_viewport
= gen_spec_find_struct(spec
, "CC_VIEWPORT");
605 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
606 for (uint32_t i
= 0; i
< 4; i
++) {
607 fprintf(outfile
, "viewport %d\n", i
);
608 decode_structure(spec
, cc_viewport
, gtt
+ start
+ i
* 8);
613 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec
*spec
,
617 struct gen_group
*sf_clip_viewport
;
619 sf_clip_viewport
= gen_spec_find_struct(spec
, "SF_CLIP_VIEWPORT");
621 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
622 for (uint32_t i
= 0; i
< 4; i
++) {
623 fprintf(outfile
, "viewport %d\n", i
);
624 decode_structure(spec
, sf_clip_viewport
, gtt
+ start
+ i
* 64);
629 handle_3dstate_blend_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
632 struct gen_group
*blend_state
;
634 blend_state
= gen_spec_find_struct(spec
, "BLEND_STATE");
636 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
637 decode_structure(spec
, blend_state
, gtt
+ start
);
641 handle_3dstate_cc_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
644 struct gen_group
*cc_state
;
646 cc_state
= gen_spec_find_struct(spec
, "COLOR_CALC_STATE");
648 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
649 decode_structure(spec
, cc_state
, gtt
+ start
);
653 handle_3dstate_scissor_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
656 struct gen_group
*scissor_rect
;
658 scissor_rect
= gen_spec_find_struct(spec
, "SCISSOR_RECT");
660 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
661 decode_structure(spec
, scissor_rect
, gtt
+ start
);
665 handle_load_register_imm(struct gen_spec
*spec
, uint32_t *p
)
667 struct gen_group
*reg
= gen_spec_find_register(spec
, p
[1]);
670 fprintf(outfile
, "register %s (0x%x): 0x%x\n",
671 reg
->name
, reg
->register_offset
, p
[2]);
672 decode_structure(spec
, reg
, &p
[2]);
676 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
678 #define STATE_BASE_ADDRESS 0x61010000
680 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
682 #define _3DSTATE_INDEX_BUFFER 0x780a0000
683 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
685 #define _3DSTATE_VS 0x78100000
686 #define _3DSTATE_GS 0x78110000
687 #define _3DSTATE_HS 0x781b0000
688 #define _3DSTATE_DS 0x781d0000
690 #define _3DSTATE_CONSTANT_VS 0x78150000
691 #define _3DSTATE_CONSTANT_GS 0x78160000
692 #define _3DSTATE_CONSTANT_PS 0x78170000
693 #define _3DSTATE_CONSTANT_HS 0x78190000
694 #define _3DSTATE_CONSTANT_DS 0x781A0000
696 #define _3DSTATE_PS 0x78200000
698 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
699 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
700 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
701 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
702 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
704 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
705 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
706 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
708 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
709 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
710 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
711 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
712 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
714 #define _MI_LOAD_REGISTER_IMM 0x11000000
716 struct custom_handler
{
718 void (*handle
)(struct gen_spec
*spec
, uint32_t *p
);
719 } custom_handlers
[] = {
720 { STATE_BASE_ADDRESS
, handle_state_base_address
},
721 { MEDIA_INTERFACE_DESCRIPTOR_LOAD
, handle_media_interface_descriptor_load
},
722 { _3DSTATE_VERTEX_BUFFERS
, handle_3dstate_vertex_buffers
},
723 { _3DSTATE_INDEX_BUFFER
, handle_3dstate_index_buffer
},
724 { _3DSTATE_VS
, handle_3dstate_vs
},
725 { _3DSTATE_GS
, handle_3dstate_vs
},
726 { _3DSTATE_DS
, handle_3dstate_vs
},
727 { _3DSTATE_HS
, handle_3dstate_hs
},
728 { _3DSTATE_CONSTANT_VS
, handle_3dstate_constant
},
729 { _3DSTATE_CONSTANT_GS
, handle_3dstate_constant
},
730 { _3DSTATE_CONSTANT_PS
, handle_3dstate_constant
},
731 { _3DSTATE_CONSTANT_HS
, handle_3dstate_constant
},
732 { _3DSTATE_CONSTANT_DS
, handle_3dstate_constant
},
733 { _3DSTATE_PS
, handle_3dstate_ps
},
735 { _3DSTATE_BINDING_TABLE_POINTERS_VS
, handle_3dstate_binding_table_pointers
},
736 { _3DSTATE_BINDING_TABLE_POINTERS_HS
, handle_3dstate_binding_table_pointers
},
737 { _3DSTATE_BINDING_TABLE_POINTERS_DS
, handle_3dstate_binding_table_pointers
},
738 { _3DSTATE_BINDING_TABLE_POINTERS_GS
, handle_3dstate_binding_table_pointers
},
739 { _3DSTATE_BINDING_TABLE_POINTERS_PS
, handle_3dstate_binding_table_pointers
},
741 { _3DSTATE_SAMPLER_STATE_POINTERS_VS
, handle_3dstate_sampler_state_pointers
},
742 { _3DSTATE_SAMPLER_STATE_POINTERS_GS
, handle_3dstate_sampler_state_pointers
},
743 { _3DSTATE_SAMPLER_STATE_POINTERS_PS
, handle_3dstate_sampler_state_pointers
},
745 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC
, handle_3dstate_viewport_state_pointers_cc
},
746 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
, handle_3dstate_viewport_state_pointers_sf_clip
},
747 { _3DSTATE_BLEND_STATE_POINTERS
, handle_3dstate_blend_state_pointers
},
748 { _3DSTATE_CC_STATE_POINTERS
, handle_3dstate_cc_state_pointers
},
749 { _3DSTATE_SCISSOR_STATE_POINTERS
, handle_3dstate_scissor_state_pointers
},
750 { _MI_LOAD_REGISTER_IMM
, handle_load_register_imm
}
754 parse_commands(struct gen_spec
*spec
, uint32_t *cmds
, int size
, int engine
)
756 uint32_t *p
, *end
= cmds
+ size
/ 4;
757 unsigned int length
, i
;
758 struct gen_group
*inst
;
760 for (p
= cmds
; p
< end
; p
+= length
) {
761 inst
= gen_spec_find_instruction(spec
, p
);
763 fprintf(outfile
, "unknown instruction %08x\n", p
[0]);
764 length
= (p
[0] & 0xff) + 2;
767 length
= gen_group_get_length(inst
, p
);
769 const char *color
, *reset_color
= NORMAL
;
772 if (option_full_decode
) {
773 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
||
774 (p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
)
775 color
= GREEN_HEADER
;
781 if (option_color
== COLOR_NEVER
) {
786 if (option_print_offsets
)
787 offset
= (void *) p
- gtt
;
791 fprintf(outfile
, "%s0x%08"PRIx64
": 0x%08x: %-80s%s\n",
793 gen_group_get_name(inst
), reset_color
);
795 if (option_full_decode
) {
796 decode_group(spec
, inst
, p
, 1, true);
798 for (i
= 0; i
< ARRAY_LENGTH(custom_handlers
); i
++) {
799 if (gen_group_get_opcode(inst
) == custom_handlers
[i
].opcode
)
800 custom_handlers
[i
].handle(spec
, p
);
804 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
) {
805 uint64_t start
= get_address(spec
, &p
[1]);
807 if (p
[0] & (1 << 22)) {
808 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
809 * like a subroutine call. Commands that come afterwards get
810 * processed once the 2nd level batch buffer returns with
811 * MI_BATCH_BUFFER_END.
813 parse_commands(spec
, gtt
+ start
, gtt_end
- start
, engine
);
815 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
816 * like a goto. Nothing after it will ever get processed. In
817 * order to prevent the recursion from growing, we just reset the
821 /* We don't know where secondaries end so use the GTT end */
826 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
832 #define GEN_ENGINE_RENDER 1
833 #define GEN_ENGINE_BLITTER 2
836 handle_trace_block(uint32_t *p
)
838 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
839 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
840 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
841 uint64_t offset
= p
[3];
842 uint32_t size
= p
[4];
843 int header_length
= p
[0] & 0xffff;
844 uint32_t *data
= p
+ header_length
+ 2;
845 int engine
= GEN_ENGINE_RENDER
;
847 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
848 offset
+= (uint64_t) p
[5] << 32;
851 case AUB_TRACE_OP_DATA_WRITE
:
852 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
854 if (gtt_size
< offset
+ size
) {
855 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
858 memcpy((char *) gtt
+ offset
, data
, size
);
859 if (gtt_end
< offset
+ size
)
860 gtt_end
= offset
+ size
;
862 case AUB_TRACE_OP_COMMAND_WRITE
:
864 case AUB_TRACE_TYPE_RING_PRB0
:
865 engine
= GEN_ENGINE_RENDER
;
867 case AUB_TRACE_TYPE_RING_PRB2
:
868 engine
= GEN_ENGINE_BLITTER
;
871 fprintf(outfile
, "command write to unknown ring %d\n", type
);
875 parse_commands(spec
, data
, size
, engine
);
882 handle_trace_header(uint32_t *p
)
884 /* The intel_aubdump tool from IGT is kind enough to put a PCI-ID= tag in
885 * the AUB header comment. If the user hasn't specified a hardware
886 * generation, try to use the one from the AUB file.
888 uint32_t *end
= p
+ (p
[0] & 0xffff) + 2;
890 if (end
> &p
[12] && p
[12] > 0)
891 sscanf((char *)&p
[13], "PCI-ID=%i", &aub_pci_id
);
896 struct gen_device_info devinfo
;
897 if (!gen_get_device_info(pci_id
, &devinfo
)) {
898 fprintf(stderr
, "can't find device information: pci_id=0x%x\n", pci_id
);
902 if (xml_path
== NULL
)
903 spec
= gen_spec_load(&devinfo
);
905 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
906 disasm
= gen_disasm_create(pci_id
);
908 if (spec
== NULL
|| disasm
== NULL
)
911 fprintf(outfile
, "%sAubinator: Intel AUB file decoder.%-80s%s\n",
912 GREEN_HEADER
, "", NORMAL
);
915 fprintf(outfile
, "File name: %s\n", input_file
);
918 fprintf(outfile
, "PCI ID: 0x%x\n", aub_pci_id
);
921 strncpy(app_name
, (char *)&p
[2], 32);
923 fprintf(outfile
, "Application name: %s\n", app_name
);
925 fprintf(outfile
, "Decoding as: %s\n", gen_get_device_name(pci_id
));
927 /* Throw in a new line before the first batch */
928 fprintf(outfile
, "\n");
934 uint32_t *map
, *end
, *cursor
;
938 static struct aub_file
*
939 aub_file_open(const char *filename
)
941 struct aub_file
*file
;
945 file
= calloc(1, sizeof *file
);
946 fd
= open(filename
, O_RDONLY
);
948 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
952 if (fstat(fd
, &sb
) == -1) {
953 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
957 file
->map
= mmap(NULL
, sb
.st_size
,
958 PROT_READ
, MAP_SHARED
, fd
, 0);
959 if (file
->map
== MAP_FAILED
) {
960 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
964 file
->cursor
= file
->map
;
965 file
->end
= file
->map
+ sb
.st_size
/ 4;
970 static struct aub_file
*
973 struct aub_file
*file
;
975 file
= calloc(1, sizeof *file
);
976 file
->stream
= stdin
;
981 #define TYPE(dw) (((dw) >> 29) & 7)
982 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
983 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
985 #define MAKE_HEADER(type, opcode, subopcode) \
986 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
990 /* Classic AUB opcodes */
991 #define OPCODE_AUB 0x01
992 #define SUBOPCODE_HEADER 0x05
993 #define SUBOPCODE_BLOCK 0x41
994 #define SUBOPCODE_BMP 0x1e
996 /* Newer version AUB opcode */
997 #define OPCODE_NEW_AUB 0x2e
998 #define SUBOPCODE_VERSION 0x00
999 #define SUBOPCODE_REG_WRITE 0x03
1000 #define SUBOPCODE_MEM_POLL 0x05
1001 #define SUBOPCODE_MEM_WRITE 0x06
1003 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
1009 { "bwr", MAKE_GEN(4, 0) },
1010 { "cln", MAKE_GEN(4, 0) },
1011 { "blc", MAKE_GEN(4, 0) },
1012 { "ctg", MAKE_GEN(4, 0) },
1013 { "el", MAKE_GEN(4, 0) },
1014 { "il", MAKE_GEN(4, 0) },
1015 { "sbr", MAKE_GEN(6, 0) },
1016 { "ivb", MAKE_GEN(7, 0) },
1017 { "lrb2", MAKE_GEN(0, 0) },
1018 { "hsw", MAKE_GEN(7, 5) },
1019 { "vlv", MAKE_GEN(7, 0) },
1020 { "bdw", MAKE_GEN(8, 0) },
1021 { "skl", MAKE_GEN(9, 0) },
1022 { "chv", MAKE_GEN(8, 0) },
1023 { "bxt", MAKE_GEN(9, 0) }
1028 AUB_ITEM_DECODE_FAILED
,
1029 AUB_ITEM_DECODE_NEED_MORE_DATA
,
1033 aub_file_decode_batch(struct aub_file
*file
)
1035 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
1036 int header_length
, bias
;
1038 if (file
->end
- file
->cursor
< 1)
1039 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1043 header_length
= h
& 0xffff;
1045 switch (OPCODE(h
)) {
1049 case OPCODE_NEW_AUB
:
1053 fprintf(outfile
, "unknown opcode %d at %td/%td\n",
1054 OPCODE(h
), file
->cursor
- file
->map
,
1055 file
->end
- file
->map
);
1056 return AUB_ITEM_DECODE_FAILED
;
1059 new_cursor
= p
+ header_length
+ bias
;
1060 if ((h
& 0xffff0000) == MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
)) {
1061 if (file
->end
- file
->cursor
< 4)
1062 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1063 new_cursor
+= p
[4] / 4;
1066 if (new_cursor
> file
->end
)
1067 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1069 switch (h
& 0xffff0000) {
1070 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
1071 handle_trace_header(p
);
1073 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
1074 handle_trace_block(p
);
1076 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
1078 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
1079 fprintf(outfile
, "version block: dw1 %08x\n", p
[1]);
1080 device
= (p
[1] >> 8) & 0xff;
1081 fprintf(outfile
, " device %s\n", device_map
[device
].name
);
1083 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
1084 fprintf(outfile
, "register write block: (dwords %d)\n", h
& 0xffff);
1085 fprintf(outfile
, " reg 0x%x, data 0x%x\n", p
[1], p
[5]);
1087 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1088 fprintf(outfile
, "memory write block (dwords %d):\n", h
& 0xffff);
1089 fprintf(outfile
, " address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1090 data_type
= (p
[3] >> 20) & 0xff;
1092 fprintf(outfile
, " data type 0x%x\n", data_type
);
1093 fprintf(outfile
, " address space 0x%x\n", (p
[3] >> 28) & 0xf);
1095 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1096 fprintf(outfile
, "memory poll block (dwords %d):\n", h
& 0xffff);
1099 fprintf(outfile
, "unknown block type=0x%x, opcode=0x%x, "
1100 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1103 file
->cursor
= new_cursor
;
1105 return AUB_ITEM_DECODE_OK
;
1109 aub_file_more_stuff(struct aub_file
*file
)
1111 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1114 #define AUB_READ_BUFFER_SIZE (4096)
1115 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1118 aub_file_data_grow(struct aub_file
*file
)
1120 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1121 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1122 uint32_t *new_start
= realloc(file
->map
, new_size
);
1124 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1125 file
->end
= new_start
+ (file
->end
- file
->map
);
1126 file
->map
= new_start
;
1127 file
->mem_end
= file
->map
+ (new_size
/ 4);
1131 aub_file_data_load(struct aub_file
*file
)
1135 if (file
->stream
== NULL
)
1138 /* First remove any consumed data */
1139 if (file
->cursor
> file
->map
) {
1140 memmove(file
->map
, file
->cursor
,
1141 (file
->end
- file
->cursor
) * 4);
1142 file
->end
-= file
->cursor
- file
->map
;
1143 file
->cursor
= file
->map
;
1146 /* Then load some new data in */
1147 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1148 aub_file_data_grow(file
);
1150 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1165 if (pipe(fds
) == -1)
1175 execlp("less", "less", "-FRSi", NULL
);
1184 print_help(const char *progname
, FILE *file
)
1187 "Usage: %s [OPTION]... [FILE]\n"
1188 "Decode aub file contents from either FILE or the standard input.\n\n"
1189 "A valid --gen option must be provided.\n\n"
1190 " --help display this help and exit\n"
1191 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1192 " --headers decode only command headers\n"
1193 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1194 " if omitted), 'always', or 'never'\n"
1195 " --no-pager don't launch pager\n"
1196 " --no-offsets don't print instruction offsets\n"
1197 " --xml=DIR load hardware xml description from directory DIR\n",
1201 int main(int argc
, char *argv
[])
1203 struct aub_file
*file
;
1205 bool help
= false, pager
= true;
1210 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1211 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1212 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1213 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1214 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1215 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1216 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1217 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1219 const struct option aubinator_opts
[] = {
1220 { "help", no_argument
, (int *) &help
, true },
1221 { "no-pager", no_argument
, (int *) &pager
, false },
1222 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1223 { "gen", required_argument
, NULL
, 'g' },
1224 { "headers", no_argument
, (int *) &option_full_decode
, false },
1225 { "color", required_argument
, NULL
, 'c' },
1226 { "xml", required_argument
, NULL
, 'x' },
1227 { NULL
, 0, NULL
, 0 }
1233 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1236 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1237 if (!strcmp(optarg
, gens
[i
].name
)) {
1238 pci_id
= gens
[i
].pci_id
;
1242 if (i
== ARRAY_SIZE(gens
)) {
1243 fprintf(stderr
, "can't parse gen: '%s', expected ivb, byt, hsw, "
1244 "bdw, chv, skl, kbl or bxt\n", optarg
);
1249 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1250 option_color
= COLOR_ALWAYS
;
1251 else if (strcmp(optarg
, "never") == 0)
1252 option_color
= COLOR_NEVER
;
1253 else if (strcmp(optarg
, "auto") == 0)
1254 option_color
= COLOR_AUTO
;
1256 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1261 xml_path
= strdup(optarg
);
1268 if (help
|| argc
== 1) {
1269 print_help(argv
[0], stderr
);
1274 input_file
= argv
[optind
];
1276 /* Do this before we redirect stdout to pager. */
1277 if (option_color
== COLOR_AUTO
)
1278 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1280 if (isatty(1) && pager
)
1283 if (input_file
== NULL
)
1284 file
= aub_file_stdin();
1286 file
= aub_file_open(input_file
);
1288 /* mmap a terabyte for our gtt space. */
1289 gtt_size
= 1ull << 40;
1290 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1291 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1292 if (gtt
== MAP_FAILED
) {
1293 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1297 while (aub_file_more_stuff(file
)) {
1298 switch (aub_file_decode_batch(file
)) {
1299 case AUB_ITEM_DECODE_OK
:
1301 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1302 if (!file
->stream
) {
1303 file
->cursor
= file
->end
;
1306 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1307 fprintf(stderr
, "failed to load data from stdin\n");
1312 fprintf(stderr
, "failed to parse aubdump data\n");
1319 /* close the stdout which is opened to write the output */
1325 return EXIT_SUCCESS
;