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
;
65 struct gen_disasm
*disasm
;
67 uint64_t gtt_size
, gtt_end
;
69 uint64_t general_state_base
;
70 uint64_t surface_state_base
;
71 uint64_t dynamic_state_base
;
72 uint64_t instruction_base
;
73 uint64_t instruction_bound
;
75 static inline uint32_t
76 field(uint32_t value
, int start
, int end
)
80 mask
= ~0U >> (31 - end
+ start
);
82 return (value
>> start
) & mask
;
85 struct brw_instruction
;
88 valid_offset(uint32_t offset
)
90 return offset
< gtt_end
;
94 print_dword_val(struct gen_field_iterator
*iter
, uint64_t offset
,
99 f
= iter
->group
->fields
[iter
->i
- 1];
100 const int dword
= f
->start
/ 32;
102 if (*dword_num
!= dword
) {
103 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
104 offset
+ 4 * dword
, iter
->p
[dword
], dword
);
110 print_iterator_values(struct gen_field_iterator
*iter
, int *idx
)
113 if (strstr(iter
->value
, "struct") == NULL
) {
114 if (strlen(iter
->description
) > 0) {
115 printf(" %s: %s (%s)\n",
116 iter
->name
, iter
->value
, iter
->description
);
118 printf(" %s: %s\n", iter
->name
, iter
->value
);
121 token
= strtok(iter
->value
, " ");
123 token
= strtok(NULL
, " ");
124 *idx
= atoi(strtok(NULL
, ">"));
128 printf(" %s:<struct %s>\n", iter
->name
, token
);
134 decode_structure(struct gen_spec
*spec
, struct gen_group
*strct
,
137 struct gen_field_iterator iter
;
139 int idx
= 0, dword_num
= 0;
142 if (option_print_offsets
)
143 offset
= (void *) p
- gtt
;
147 gen_field_iterator_init(&iter
, strct
, p
,
148 option_color
== COLOR_ALWAYS
);
149 while (gen_field_iterator_next(&iter
)) {
151 print_dword_val(&iter
, offset
, &dword_num
);
152 token
= print_iterator_values(&iter
, &idx
);
154 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, token
);
155 decode_structure(spec
, struct_val
, &p
[idx
]);
162 handle_struct_decode(struct gen_spec
*spec
, char *struct_name
, uint32_t *p
)
164 if (struct_name
== NULL
)
166 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, struct_name
);
167 decode_structure(spec
, struct_val
, p
);
171 dump_binding_table(struct gen_spec
*spec
, uint32_t offset
)
173 uint32_t *pointers
, i
;
175 struct gen_group
*surface_state
;
177 surface_state
= gen_spec_find_struct(spec
, "RENDER_SURFACE_STATE");
178 if (surface_state
== NULL
) {
179 printf("did not find RENDER_SURFACE_STATE info\n");
183 start
= surface_state_base
+ offset
;
184 pointers
= gtt
+ start
;
185 for (i
= 0; i
< 16; i
++) {
186 if (pointers
[i
] == 0)
188 start
= pointers
[i
] + surface_state_base
;
189 if (!valid_offset(start
)) {
190 printf("pointer %u: %08x <not valid>\n",
194 printf("pointer %u: %08x\n", i
, pointers
[i
]);
197 decode_structure(spec
, surface_state
, gtt
+ start
);
202 handle_3dstate_index_buffer(struct gen_spec
*spec
, uint32_t *p
)
205 uint32_t length
, i
, type
, size
;
208 type
= (p
[1] >> 8) & 3;
210 length
= p
[4] / size
;
216 for (i
= 0; i
< length
; i
++) {
219 printf("%3d ", ((uint8_t *)start
)[i
]);
222 printf("%3d ", ((uint16_t *)start
)[i
]);
225 printf("%3d ", ((uint32_t *)start
)[i
]);
229 if (length
< p
[4] / size
)
235 static inline uint64_t
236 get_qword(uint32_t *p
)
238 return ((uint64_t) p
[1] << 32) | p
[0];
241 static inline uint64_t
242 get_address(struct gen_spec
*spec
, uint32_t *p
)
244 /* Addresses are always guaranteed to be page-aligned and sometimes
245 * hardware packets have extra stuff stuffed in the bottom 12 bits.
247 uint64_t addr
= p
[0] & ~0xfffu
;
249 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
250 /* On Broadwell and above, we have 48-bit addresses which consume two
251 * dwords. Some packets require that these get stored in a "canonical
252 * form" which means that bit 47 is sign-extended through the upper
253 * bits. In order to correctly handle those aub dumps, we need to mask
254 * off the top 16 bits.
256 addr
|= ((uint64_t)p
[1] & 0xffff) << 32;
263 handle_state_base_address(struct gen_spec
*spec
, uint32_t *p
)
265 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
267 general_state_base
= get_address(spec
, &p
[1]);
269 surface_state_base
= get_address(spec
, &p
[4]);
271 dynamic_state_base
= get_address(spec
, &p
[6]);
273 instruction_base
= get_address(spec
, &p
[10]);
275 instruction_bound
= p
[15] & 0xfff;
278 surface_state_base
= get_address(spec
, &p
[2]);
280 dynamic_state_base
= get_address(spec
, &p
[3]);
282 instruction_base
= get_address(spec
, &p
[5]);
284 instruction_bound
= get_address(spec
, &p
[9]);
289 dump_samplers(struct gen_spec
*spec
, uint32_t offset
)
293 struct gen_group
*sampler_state
;
295 sampler_state
= gen_spec_find_struct(spec
, "SAMPLER_STATE");
297 start
= dynamic_state_base
+ offset
;
298 for (i
= 0; i
< 4; i
++) {
299 printf("sampler state %d\n", i
);
300 decode_structure(spec
, sampler_state
, gtt
+ start
+ i
* 16);
305 handle_media_interface_descriptor_load(struct gen_spec
*spec
, uint32_t *p
)
307 int i
, length
= p
[2] / 32;
308 struct gen_group
*descriptor_structure
;
309 uint32_t *descriptors
;
311 struct brw_instruction
*insns
;
313 descriptor_structure
=
314 gen_spec_find_struct(spec
, "INTERFACE_DESCRIPTOR_DATA");
315 if (descriptor_structure
== NULL
) {
316 printf("did not find INTERFACE_DESCRIPTOR_DATA info\n");
320 start
= dynamic_state_base
+ p
[3];
321 descriptors
= gtt
+ start
;
322 for (i
= 0; i
< length
; i
++, descriptors
+= 8) {
323 printf("descriptor %u: %08x\n", i
, *descriptors
);
324 decode_structure(spec
, descriptor_structure
, descriptors
);
326 start
= instruction_base
+ descriptors
[0];
327 if (!valid_offset(start
)) {
328 printf("kernel: %08"PRIx64
" <not valid>\n", start
);
331 printf("kernel: %08"PRIx64
"\n", start
);
334 insns
= (struct brw_instruction
*) (gtt
+ start
);
335 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
337 dump_samplers(spec
, descriptors
[3] & ~0x1f);
338 dump_binding_table(spec
, descriptors
[4] & ~0x1f);
342 /* Heuristic to determine whether a uint32_t is probably actually a float
343 * (http://stackoverflow.com/a/2953466)
347 probably_float(uint32_t bits
)
349 int exp
= ((bits
& 0x7f800000U
) >> 23) - 127;
350 uint32_t mant
= bits
& 0x007fffff;
353 if (exp
== -127 && mant
== 0)
356 /* +- 1 billionth to 1 billion */
357 if (-30 <= exp
&& exp
<= 30)
360 /* some value with only a few binary digits */
361 if ((mant
& 0x0000ffff) == 0)
368 handle_3dstate_vertex_buffers(struct gen_spec
*spec
, uint32_t *p
)
370 uint32_t *end
, *s
, *dw
, *dwend
;
372 int n
, i
, count
, stride
;
374 end
= (p
[0] & 0xff) + p
+ 2;
375 for (s
= &p
[1], n
= 0; s
< end
; s
+= 4, n
++) {
376 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
377 offset
= *(uint64_t *) &s
[1];
378 dwend
= gtt
+ offset
+ s
[3];
381 dwend
= gtt
+ s
[2] + 1;
384 stride
= field(s
[0], 0, 11);
386 printf("vertex buffer %d, size %d\n", n
, s
[3]);
387 for (dw
= gtt
+ offset
, i
= 0; dw
< dwend
&& i
< 256; dw
++) {
388 if (count
== 0 && count
% (8 * 4) == 0)
391 if (probably_float(*dw
))
392 printf(" %8.2f", *(float *) dw
);
394 printf(" 0x%08x", *dw
);
399 if (count
== stride
) {
402 } else if (count
% (8 * 4) == 0) {
408 if (count
> 0 && count
% (8 * 4) != 0)
414 handle_3dstate_vs(struct gen_spec
*spec
, uint32_t *p
)
417 struct brw_instruction
*insns
;
420 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
421 start
= get_qword(&p
[1]);
422 vs_enable
= p
[7] & 1;
425 vs_enable
= p
[5] & 1;
429 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
430 instruction_base
, start
);
432 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
433 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
438 handle_3dstate_hs(struct gen_spec
*spec
, uint32_t *p
)
441 struct brw_instruction
*insns
;
444 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
445 start
= get_qword(&p
[4]);
450 hs_enable
= p
[2] & 0x80000000;
453 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
454 instruction_base
, start
);
456 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
457 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
462 handle_3dstate_constant(struct gen_spec
*spec
, uint32_t *p
)
468 for (i
= 0; i
< 4; i
++) {
469 length
= (p
[1 + i
/ 2] >> (i
& 1) * 16) & 0xffff;
470 f
= (float *) (gtt
+ p
[3 + i
* 2] + dynamic_state_base
);
472 for (j
= 0; j
< length
* 8; j
++) {
473 if (probably_float(dw
[j
]))
474 printf(" %04.3f", f
[j
]);
476 printf(" 0x%08x", dw
[j
]);
485 handle_3dstate_ps(struct gen_spec
*spec
, uint32_t *p
)
487 uint32_t mask
= ~((1 << 6) - 1);
489 struct brw_instruction
*insns
;
490 static const char unused
[] = "unused";
491 static const char *pixel_type
[3] = {"8 pixel", "16 pixel", "32 pixel"};
492 const char *k0
, *k1
, *k2
;
493 uint32_t k_mask
, k1_offset
, k2_offset
;
495 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
506 #define DISPATCH_16 2
507 #define DISPATCH_32 4
520 case DISPATCH_8
| DISPATCH_16
:
530 case DISPATCH_16
| DISPATCH_32
:
535 case DISPATCH_8
| DISPATCH_16
| DISPATCH_32
:
547 start
= instruction_base
+ (p
[1] & mask
);
548 printf(" Kernel[0] %s\n", k0
);
550 insns
= (struct brw_instruction
*) (gtt
+ start
);
551 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
554 start
= instruction_base
+ (p
[k1_offset
] & mask
);
555 printf(" Kernel[1] %s\n", k1
);
557 insns
= (struct brw_instruction
*) (gtt
+ start
);
558 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
561 start
= instruction_base
+ (p
[k2_offset
] & mask
);
562 printf(" Kernel[2] %s\n", k2
);
564 insns
= (struct brw_instruction
*) (gtt
+ start
);
565 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
570 handle_3dstate_binding_table_pointers(struct gen_spec
*spec
, uint32_t *p
)
572 dump_binding_table(spec
, p
[1]);
576 handle_3dstate_sampler_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
578 dump_samplers(spec
, p
[1]);
582 handle_3dstate_viewport_state_pointers_cc(struct gen_spec
*spec
, uint32_t *p
)
585 struct gen_group
*cc_viewport
;
587 cc_viewport
= gen_spec_find_struct(spec
, "CC_VIEWPORT");
589 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
590 for (uint32_t i
= 0; i
< 4; i
++) {
591 printf("viewport %d\n", i
);
592 decode_structure(spec
, cc_viewport
, gtt
+ start
+ i
* 8);
597 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec
*spec
,
601 struct gen_group
*sf_clip_viewport
;
603 sf_clip_viewport
= gen_spec_find_struct(spec
, "SF_CLIP_VIEWPORT");
605 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
606 for (uint32_t i
= 0; i
< 4; i
++) {
607 printf("viewport %d\n", i
);
608 decode_structure(spec
, sf_clip_viewport
, gtt
+ start
+ i
* 64);
613 handle_3dstate_blend_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
616 struct gen_group
*blend_state
;
618 blend_state
= gen_spec_find_struct(spec
, "BLEND_STATE");
620 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
621 decode_structure(spec
, blend_state
, gtt
+ start
);
625 handle_3dstate_cc_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
628 struct gen_group
*cc_state
;
630 cc_state
= gen_spec_find_struct(spec
, "COLOR_CALC_STATE");
632 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
633 decode_structure(spec
, cc_state
, gtt
+ start
);
637 handle_3dstate_scissor_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
640 struct gen_group
*scissor_rect
;
642 scissor_rect
= gen_spec_find_struct(spec
, "SCISSOR_RECT");
644 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
645 decode_structure(spec
, scissor_rect
, gtt
+ start
);
649 handle_load_register_imm(struct gen_spec
*spec
, uint32_t *p
)
651 struct gen_group
*reg
= gen_spec_find_register(spec
, p
[1]);
654 printf("register %s (0x%x): 0x%x\n",
655 reg
->name
, reg
->register_offset
, p
[2]);
656 decode_structure(spec
, reg
, &p
[2]);
660 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
662 #define STATE_BASE_ADDRESS 0x61010000
664 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
666 #define _3DSTATE_INDEX_BUFFER 0x780a0000
667 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
669 #define _3DSTATE_VS 0x78100000
670 #define _3DSTATE_GS 0x78110000
671 #define _3DSTATE_HS 0x781b0000
672 #define _3DSTATE_DS 0x781d0000
674 #define _3DSTATE_CONSTANT_VS 0x78150000
675 #define _3DSTATE_CONSTANT_GS 0x78160000
676 #define _3DSTATE_CONSTANT_PS 0x78170000
677 #define _3DSTATE_CONSTANT_HS 0x78190000
678 #define _3DSTATE_CONSTANT_DS 0x781A0000
680 #define _3DSTATE_PS 0x78200000
682 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
683 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
684 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
685 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
686 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
688 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
689 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
690 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
692 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
693 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
694 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
695 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
696 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
698 #define _MI_LOAD_REGISTER_IMM 0x11000000
700 struct custom_handler
{
702 void (*handle
)(struct gen_spec
*spec
, uint32_t *p
);
703 } custom_handlers
[] = {
704 { STATE_BASE_ADDRESS
, handle_state_base_address
},
705 { MEDIA_INTERFACE_DESCRIPTOR_LOAD
, handle_media_interface_descriptor_load
},
706 { _3DSTATE_VERTEX_BUFFERS
, handle_3dstate_vertex_buffers
},
707 { _3DSTATE_INDEX_BUFFER
, handle_3dstate_index_buffer
},
708 { _3DSTATE_VS
, handle_3dstate_vs
},
709 { _3DSTATE_GS
, handle_3dstate_vs
},
710 { _3DSTATE_DS
, handle_3dstate_vs
},
711 { _3DSTATE_HS
, handle_3dstate_hs
},
712 { _3DSTATE_CONSTANT_VS
, handle_3dstate_constant
},
713 { _3DSTATE_CONSTANT_GS
, handle_3dstate_constant
},
714 { _3DSTATE_CONSTANT_PS
, handle_3dstate_constant
},
715 { _3DSTATE_CONSTANT_HS
, handle_3dstate_constant
},
716 { _3DSTATE_CONSTANT_DS
, handle_3dstate_constant
},
717 { _3DSTATE_PS
, handle_3dstate_ps
},
719 { _3DSTATE_BINDING_TABLE_POINTERS_VS
, handle_3dstate_binding_table_pointers
},
720 { _3DSTATE_BINDING_TABLE_POINTERS_HS
, handle_3dstate_binding_table_pointers
},
721 { _3DSTATE_BINDING_TABLE_POINTERS_DS
, handle_3dstate_binding_table_pointers
},
722 { _3DSTATE_BINDING_TABLE_POINTERS_GS
, handle_3dstate_binding_table_pointers
},
723 { _3DSTATE_BINDING_TABLE_POINTERS_PS
, handle_3dstate_binding_table_pointers
},
725 { _3DSTATE_SAMPLER_STATE_POINTERS_VS
, handle_3dstate_sampler_state_pointers
},
726 { _3DSTATE_SAMPLER_STATE_POINTERS_GS
, handle_3dstate_sampler_state_pointers
},
727 { _3DSTATE_SAMPLER_STATE_POINTERS_PS
, handle_3dstate_sampler_state_pointers
},
729 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC
, handle_3dstate_viewport_state_pointers_cc
},
730 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
, handle_3dstate_viewport_state_pointers_sf_clip
},
731 { _3DSTATE_BLEND_STATE_POINTERS
, handle_3dstate_blend_state_pointers
},
732 { _3DSTATE_CC_STATE_POINTERS
, handle_3dstate_cc_state_pointers
},
733 { _3DSTATE_SCISSOR_STATE_POINTERS
, handle_3dstate_scissor_state_pointers
},
734 { _MI_LOAD_REGISTER_IMM
, handle_load_register_imm
}
738 parse_commands(struct gen_spec
*spec
, uint32_t *cmds
, int size
, int engine
)
740 uint32_t *p
, *end
= cmds
+ size
/ 4;
741 unsigned int length
, i
;
742 struct gen_group
*inst
;
744 for (p
= cmds
; p
< end
; p
+= length
) {
745 inst
= gen_spec_find_instruction(spec
, p
);
747 printf("unknown instruction %08x\n", p
[0]);
748 length
= (p
[0] & 0xff) + 2;
751 length
= gen_group_get_length(inst
, p
);
753 const char *color
, *reset_color
= NORMAL
;
756 if (option_full_decode
) {
757 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
||
758 (p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
)
759 color
= GREEN_HEADER
;
765 if (option_color
== COLOR_NEVER
) {
770 if (option_print_offsets
)
771 offset
= (void *) p
- gtt
;
775 printf("%s0x%08"PRIx64
": 0x%08x: %-80s%s\n",
777 gen_group_get_name(inst
), reset_color
);
779 if (option_full_decode
) {
780 struct gen_field_iterator iter
;
782 int idx
= 0, dword_num
= 0;
783 gen_field_iterator_init(&iter
, inst
, p
,
784 option_color
== COLOR_ALWAYS
);
785 while (gen_field_iterator_next(&iter
)) {
787 print_dword_val(&iter
, offset
, &dword_num
);
789 token
= print_iterator_values(&iter
, &idx
);
791 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
792 offset
+ 4 * idx
, p
[idx
], idx
);
793 handle_struct_decode(spec
,token
, &p
[idx
]);
798 for (i
= 0; i
< ARRAY_LENGTH(custom_handlers
); i
++) {
799 if (gen_group_get_opcode(inst
) ==
800 custom_handlers
[i
].opcode
)
801 custom_handlers
[i
].handle(spec
, p
);
805 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
) {
806 uint64_t start
= get_address(spec
, &p
[1]);
808 if (p
[0] & (1 << 22)) {
809 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
810 * like a subroutine call. Commands that come afterwards get
811 * processed once the 2nd level batch buffer returns with
812 * MI_BATCH_BUFFER_END.
814 parse_commands(spec
, gtt
+ start
, gtt_end
- start
, engine
);
816 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
817 * like a goto. Nothing after it will ever get processed. In
818 * order to prevent the recursion from growing, we just reset the
822 /* We don't know where secondaries end so use the GTT end */
827 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
833 #define GEN_ENGINE_RENDER 1
834 #define GEN_ENGINE_BLITTER 2
837 handle_trace_block(struct gen_spec
*spec
, uint32_t *p
)
839 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
840 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
841 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
842 uint64_t offset
= p
[3];
843 uint32_t size
= p
[4];
844 int header_length
= p
[0] & 0xffff;
845 uint32_t *data
= p
+ header_length
+ 2;
846 int engine
= GEN_ENGINE_RENDER
;
848 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
849 offset
+= (uint64_t) p
[5] << 32;
852 case AUB_TRACE_OP_DATA_WRITE
:
853 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
855 if (gtt_size
< offset
+ size
) {
856 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
859 memcpy((char *) gtt
+ offset
, data
, size
);
860 if (gtt_end
< offset
+ size
)
861 gtt_end
= offset
+ size
;
863 case AUB_TRACE_OP_COMMAND_WRITE
:
865 case AUB_TRACE_TYPE_RING_PRB0
:
866 engine
= GEN_ENGINE_RENDER
;
868 case AUB_TRACE_TYPE_RING_PRB2
:
869 engine
= GEN_ENGINE_BLITTER
;
872 printf("command write to unknown ring %d\n", type
);
876 parse_commands(spec
, data
, size
, engine
);
885 uint32_t *map
, *end
, *cursor
;
889 static struct aub_file
*
890 aub_file_open(const char *filename
)
892 struct aub_file
*file
;
896 file
= calloc(1, sizeof *file
);
897 fd
= open(filename
, O_RDONLY
);
899 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
903 if (fstat(fd
, &sb
) == -1) {
904 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
908 file
->map
= mmap(NULL
, sb
.st_size
,
909 PROT_READ
, MAP_SHARED
, fd
, 0);
910 if (file
->map
== MAP_FAILED
) {
911 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
915 file
->cursor
= file
->map
;
916 file
->end
= file
->map
+ sb
.st_size
/ 4;
921 static struct aub_file
*
924 struct aub_file
*file
;
926 file
= calloc(1, sizeof *file
);
927 file
->stream
= stdin
;
932 #define TYPE(dw) (((dw) >> 29) & 7)
933 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
934 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
936 #define MAKE_HEADER(type, opcode, subopcode) \
937 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
941 /* Classic AUB opcodes */
942 #define OPCODE_AUB 0x01
943 #define SUBOPCODE_HEADER 0x05
944 #define SUBOPCODE_BLOCK 0x41
945 #define SUBOPCODE_BMP 0x1e
947 /* Newer version AUB opcode */
948 #define OPCODE_NEW_AUB 0x2e
949 #define SUBOPCODE_VERSION 0x00
950 #define SUBOPCODE_REG_WRITE 0x03
951 #define SUBOPCODE_MEM_POLL 0x05
952 #define SUBOPCODE_MEM_WRITE 0x06
954 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
960 { "bwr", MAKE_GEN(4, 0) },
961 { "cln", MAKE_GEN(4, 0) },
962 { "blc", MAKE_GEN(4, 0) },
963 { "ctg", MAKE_GEN(4, 0) },
964 { "el", MAKE_GEN(4, 0) },
965 { "il", MAKE_GEN(4, 0) },
966 { "sbr", MAKE_GEN(6, 0) },
967 { "ivb", MAKE_GEN(7, 0) },
968 { "lrb2", MAKE_GEN(0, 0) },
969 { "hsw", MAKE_GEN(7, 5) },
970 { "vlv", MAKE_GEN(7, 0) },
971 { "bdw", MAKE_GEN(8, 0) },
972 { "skl", MAKE_GEN(9, 0) },
973 { "chv", MAKE_GEN(8, 0) },
974 { "bxt", MAKE_GEN(9, 0) }
979 AUB_ITEM_DECODE_FAILED
,
980 AUB_ITEM_DECODE_NEED_MORE_DATA
,
984 aub_file_decode_batch(struct aub_file
*file
, struct gen_spec
*spec
)
986 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
987 int header_length
, payload_size
, bias
;
989 if (file
->end
- file
->cursor
< 1)
990 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
994 header_length
= h
& 0xffff;
1000 case OPCODE_NEW_AUB
:
1004 printf("unknown opcode %d at %td/%td\n",
1005 OPCODE(h
), file
->cursor
- file
->map
,
1006 file
->end
- file
->map
);
1007 return AUB_ITEM_DECODE_FAILED
;
1011 switch (h
& 0xffff0000) {
1012 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
1013 if (file
->end
- file
->cursor
< 12)
1014 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1015 payload_size
= p
[12];
1017 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
1018 if (file
->end
- file
->cursor
< 4)
1019 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1020 payload_size
= p
[4];
1026 new_cursor
= p
+ header_length
+ bias
+ payload_size
/ 4;
1027 if (new_cursor
> file
->end
)
1028 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1030 switch (h
& 0xffff0000) {
1031 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
1033 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
1034 handle_trace_block(spec
, p
);
1036 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
1038 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
1039 printf("version block: dw1 %08x\n", p
[1]);
1040 device
= (p
[1] >> 8) & 0xff;
1041 printf(" device %s\n", device_map
[device
].name
);
1043 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
1044 printf("register write block: (dwords %d)\n", h
& 0xffff);
1045 printf(" reg 0x%x, data 0x%x\n", p
[1], p
[5]);
1047 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1048 printf("memory write block (dwords %d):\n", h
& 0xffff);
1049 printf(" address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1050 data_type
= (p
[3] >> 20) & 0xff;
1052 printf(" data type 0x%x\n", data_type
);
1053 printf(" address space 0x%x\n", (p
[3] >> 28) & 0xf);
1055 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1056 printf("memory poll block (dwords %d):\n", h
& 0xffff);
1059 printf("unknown block type=0x%x, opcode=0x%x, "
1060 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1063 file
->cursor
= new_cursor
;
1065 return AUB_ITEM_DECODE_OK
;
1069 aub_file_more_stuff(struct aub_file
*file
)
1071 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1074 #define AUB_READ_BUFFER_SIZE (4096)
1075 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1078 aub_file_data_grow(struct aub_file
*file
)
1080 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1081 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1082 uint32_t *new_start
= realloc(file
->map
, new_size
);
1084 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1085 file
->end
= new_start
+ (file
->end
- file
->map
);
1086 file
->map
= new_start
;
1087 file
->mem_end
= file
->map
+ (new_size
/ 4);
1091 aub_file_data_load(struct aub_file
*file
)
1095 if (file
->stream
== NULL
)
1098 /* First remove any consumed data */
1099 if (file
->cursor
> file
->map
) {
1100 memmove(file
->map
, file
->cursor
,
1101 (file
->end
- file
->cursor
) * 4);
1102 file
->end
-= file
->cursor
- file
->map
;
1103 file
->cursor
= file
->map
;
1106 /* Then load some new data in */
1107 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1108 aub_file_data_grow(file
);
1110 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1125 if (pipe(fds
) == -1)
1135 execlp("less", "less", "-FRSi", NULL
);
1144 print_help(const char *progname
, FILE *file
)
1147 "Usage: %s [OPTION]... [FILE]\n"
1148 "Decode aub file contents from either FILE or the standard input.\n\n"
1149 "A valid --gen option must be provided.\n\n"
1150 " --help display this help and exit\n"
1151 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1152 " --headers decode only command headers\n"
1153 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1154 " if omitted), 'always', or 'never'\n"
1155 " --no-pager don't launch pager\n"
1156 " --no-offsets don't print instruction offsets\n"
1157 " --xml=DIR load hardware xml description from directory DIR\n",
1161 int main(int argc
, char *argv
[])
1163 struct gen_spec
*spec
;
1164 struct aub_file
*file
;
1166 bool help
= false, pager
= true;
1167 char *input_file
= NULL
, *xml_path
= NULL
;
1168 char gen_val
[24] = { 0, };
1173 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1174 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1175 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1176 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1177 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1178 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1179 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1180 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1182 const struct option aubinator_opts
[] = {
1183 { "help", no_argument
, (int *) &help
, true },
1184 { "no-pager", no_argument
, (int *) &pager
, false },
1185 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1186 { "gen", required_argument
, NULL
, 'g' },
1187 { "headers", no_argument
, (int *) &option_full_decode
, false },
1188 { "color", required_argument
, NULL
, 'c' },
1189 { "xml", required_argument
, NULL
, 'x' },
1190 { NULL
, 0, NULL
, 0 }
1192 struct gen_device_info devinfo
;
1195 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1198 snprintf(gen_val
, sizeof(gen_val
), "%s", optarg
);
1201 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1202 option_color
= COLOR_ALWAYS
;
1203 else if (strcmp(optarg
, "never") == 0)
1204 option_color
= COLOR_NEVER
;
1205 else if (strcmp(optarg
, "auto") == 0)
1206 option_color
= COLOR_AUTO
;
1208 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1213 xml_path
= strdup(optarg
);
1220 if (help
|| argc
== 1) {
1221 print_help(argv
[0], stderr
);
1226 input_file
= argv
[optind
];
1228 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1229 if (!strcmp(gen_val
, gens
[i
].name
)) {
1236 fprintf(stderr
, "can't parse gen: '%s', expected ivb, byt, hsw, "
1237 "bdw, chv, skl, kbl or bxt\n", gen_val
);
1241 if (!gen_get_device_info(gen
->pci_id
, &devinfo
)) {
1242 fprintf(stderr
, "can't find device information: pci_id=0x%x name=%s\n",
1243 gen
->pci_id
, gen
->name
);
1248 /* Do this before we redirect stdout to pager. */
1249 if (option_color
== COLOR_AUTO
)
1250 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1252 if (isatty(1) && pager
)
1255 if (xml_path
== NULL
)
1256 spec
= gen_spec_load(&devinfo
);
1258 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
1259 disasm
= gen_disasm_create(gen
->pci_id
);
1261 if (spec
== NULL
|| disasm
== NULL
)
1264 if (input_file
== NULL
)
1265 file
= aub_file_stdin();
1267 file
= aub_file_open(input_file
);
1269 /* mmap a terabyte for our gtt space. */
1270 gtt_size
= 1ul << 40;
1271 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1272 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1273 if (gtt
== MAP_FAILED
) {
1274 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1278 while (aub_file_more_stuff(file
)) {
1279 switch (aub_file_decode_batch(file
, spec
)) {
1280 case AUB_ITEM_DECODE_OK
:
1282 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1283 if (!file
->stream
) {
1284 file
->cursor
= file
->end
;
1287 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1288 fprintf(stderr
, "failed to load data from stdin\n");
1293 fprintf(stderr
, "failed to parse aubdump data\n");
1300 /* close the stdout which is opened to write the output */
1306 return EXIT_SUCCESS
;