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_address(struct gen_spec
*spec
, uint32_t *p
)
238 /* Addresses are always guaranteed to be page-aligned and sometimes
239 * hardware packets have extra stuff stuffed in the bottom 12 bits.
241 uint64_t addr
= p
[0] & ~0xfffu
;
243 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
244 /* On Broadwell and above, we have 48-bit addresses which consume two
245 * dwords. Some packets require that these get stored in a "canonical
246 * form" which means that bit 47 is sign-extended through the upper
247 * bits. In order to correctly handle those aub dumps, we need to mask
248 * off the top 16 bits.
250 addr
|= ((uint64_t)p
[1] & 0xffff) << 32;
256 static inline uint64_t
257 get_offset(uint32_t *p
, uint32_t start
, uint32_t end
)
259 assert(start
<= end
);
262 uint64_t mask
= (~0ull >> (64 - (end
- start
+ 1))) << start
;
264 uint64_t offset
= p
[0];
266 offset
|= (uint64_t) p
[1] << 32;
268 return offset
& mask
;
272 handle_state_base_address(struct gen_spec
*spec
, uint32_t *p
)
274 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
276 general_state_base
= get_address(spec
, &p
[1]);
278 surface_state_base
= get_address(spec
, &p
[4]);
280 dynamic_state_base
= get_address(spec
, &p
[6]);
282 instruction_base
= get_address(spec
, &p
[10]);
284 instruction_bound
= p
[15] & 0xfff;
287 surface_state_base
= get_address(spec
, &p
[2]);
289 dynamic_state_base
= get_address(spec
, &p
[3]);
291 instruction_base
= get_address(spec
, &p
[5]);
293 instruction_bound
= get_address(spec
, &p
[9]);
298 dump_samplers(struct gen_spec
*spec
, uint32_t offset
)
302 struct gen_group
*sampler_state
;
304 sampler_state
= gen_spec_find_struct(spec
, "SAMPLER_STATE");
306 start
= dynamic_state_base
+ offset
;
307 for (i
= 0; i
< 4; i
++) {
308 printf("sampler state %d\n", i
);
309 decode_structure(spec
, sampler_state
, gtt
+ start
+ i
* 16);
314 handle_media_interface_descriptor_load(struct gen_spec
*spec
, uint32_t *p
)
316 int i
, length
= p
[2] / 32;
317 struct gen_group
*descriptor_structure
;
318 uint32_t *descriptors
;
320 struct brw_instruction
*insns
;
322 descriptor_structure
=
323 gen_spec_find_struct(spec
, "INTERFACE_DESCRIPTOR_DATA");
324 if (descriptor_structure
== NULL
) {
325 printf("did not find INTERFACE_DESCRIPTOR_DATA info\n");
329 start
= dynamic_state_base
+ p
[3];
330 descriptors
= gtt
+ start
;
331 for (i
= 0; i
< length
; i
++, descriptors
+= 8) {
332 printf("descriptor %u: %08x\n", i
, *descriptors
);
333 decode_structure(spec
, descriptor_structure
, descriptors
);
335 start
= instruction_base
+ descriptors
[0];
336 if (!valid_offset(start
)) {
337 printf("kernel: %08"PRIx64
" <not valid>\n", start
);
340 printf("kernel: %08"PRIx64
"\n", start
);
343 insns
= (struct brw_instruction
*) (gtt
+ start
);
344 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
346 dump_samplers(spec
, descriptors
[3] & ~0x1f);
347 dump_binding_table(spec
, descriptors
[4] & ~0x1f);
351 /* Heuristic to determine whether a uint32_t is probably actually a float
352 * (http://stackoverflow.com/a/2953466)
356 probably_float(uint32_t bits
)
358 int exp
= ((bits
& 0x7f800000U
) >> 23) - 127;
359 uint32_t mant
= bits
& 0x007fffff;
362 if (exp
== -127 && mant
== 0)
365 /* +- 1 billionth to 1 billion */
366 if (-30 <= exp
&& exp
<= 30)
369 /* some value with only a few binary digits */
370 if ((mant
& 0x0000ffff) == 0)
377 handle_3dstate_vertex_buffers(struct gen_spec
*spec
, uint32_t *p
)
379 uint32_t *end
, *s
, *dw
, *dwend
;
381 int n
, i
, count
, stride
;
383 end
= (p
[0] & 0xff) + p
+ 2;
384 for (s
= &p
[1], n
= 0; s
< end
; s
+= 4, n
++) {
385 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
386 offset
= *(uint64_t *) &s
[1];
387 dwend
= gtt
+ offset
+ s
[3];
390 dwend
= gtt
+ s
[2] + 1;
393 stride
= field(s
[0], 0, 11);
395 printf("vertex buffer %d, size %d\n", n
, s
[3]);
396 for (dw
= gtt
+ offset
, i
= 0; dw
< dwend
&& i
< 256; dw
++) {
397 if (count
== 0 && count
% (8 * 4) == 0)
400 if (probably_float(*dw
))
401 printf(" %8.2f", *(float *) dw
);
403 printf(" 0x%08x", *dw
);
408 if (count
== stride
) {
411 } else if (count
% (8 * 4) == 0) {
417 if (count
> 0 && count
% (8 * 4) != 0)
423 handle_3dstate_vs(struct gen_spec
*spec
, uint32_t *p
)
426 struct brw_instruction
*insns
;
429 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
430 start
= get_offset(&p
[1], 6, 63);
431 vs_enable
= p
[7] & 1;
433 start
= get_offset(&p
[1], 6, 31);
434 vs_enable
= p
[5] & 1;
438 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
439 instruction_base
, start
);
441 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
442 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
447 handle_3dstate_hs(struct gen_spec
*spec
, uint32_t *p
)
450 struct brw_instruction
*insns
;
453 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
454 start
= get_offset(&p
[3], 6, 63);
456 start
= get_offset(&p
[3], 6, 31);
459 hs_enable
= p
[2] & 0x80000000;
462 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
463 instruction_base
, start
);
465 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
466 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
471 handle_3dstate_constant(struct gen_spec
*spec
, uint32_t *p
)
477 for (i
= 0; i
< 4; i
++) {
478 length
= (p
[1 + i
/ 2] >> (i
& 1) * 16) & 0xffff;
479 f
= (float *) (gtt
+ p
[3 + i
* 2] + dynamic_state_base
);
481 for (j
= 0; j
< length
* 8; j
++) {
482 if (probably_float(dw
[j
]))
483 printf(" %04.3f", f
[j
]);
485 printf(" 0x%08x", dw
[j
]);
494 handle_3dstate_ps(struct gen_spec
*spec
, uint32_t *p
)
496 uint32_t mask
= ~((1 << 6) - 1);
498 struct brw_instruction
*insns
;
499 static const char unused
[] = "unused";
500 static const char *pixel_type
[3] = {"8 pixel", "16 pixel", "32 pixel"};
501 const char *k0
, *k1
, *k2
;
502 uint32_t k_mask
, k1_offset
, k2_offset
;
504 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
515 #define DISPATCH_16 2
516 #define DISPATCH_32 4
529 case DISPATCH_8
| DISPATCH_16
:
539 case DISPATCH_16
| DISPATCH_32
:
544 case DISPATCH_8
| DISPATCH_16
| DISPATCH_32
:
556 start
= instruction_base
+ (p
[1] & mask
);
557 printf(" Kernel[0] %s\n", k0
);
559 insns
= (struct brw_instruction
*) (gtt
+ start
);
560 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
563 start
= instruction_base
+ (p
[k1_offset
] & mask
);
564 printf(" Kernel[1] %s\n", k1
);
566 insns
= (struct brw_instruction
*) (gtt
+ start
);
567 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
570 start
= instruction_base
+ (p
[k2_offset
] & mask
);
571 printf(" Kernel[2] %s\n", k2
);
573 insns
= (struct brw_instruction
*) (gtt
+ start
);
574 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
579 handle_3dstate_binding_table_pointers(struct gen_spec
*spec
, uint32_t *p
)
581 dump_binding_table(spec
, p
[1]);
585 handle_3dstate_sampler_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
587 dump_samplers(spec
, p
[1]);
591 handle_3dstate_viewport_state_pointers_cc(struct gen_spec
*spec
, uint32_t *p
)
594 struct gen_group
*cc_viewport
;
596 cc_viewport
= gen_spec_find_struct(spec
, "CC_VIEWPORT");
598 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
599 for (uint32_t i
= 0; i
< 4; i
++) {
600 printf("viewport %d\n", i
);
601 decode_structure(spec
, cc_viewport
, gtt
+ start
+ i
* 8);
606 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec
*spec
,
610 struct gen_group
*sf_clip_viewport
;
612 sf_clip_viewport
= gen_spec_find_struct(spec
, "SF_CLIP_VIEWPORT");
614 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
615 for (uint32_t i
= 0; i
< 4; i
++) {
616 printf("viewport %d\n", i
);
617 decode_structure(spec
, sf_clip_viewport
, gtt
+ start
+ i
* 64);
622 handle_3dstate_blend_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
625 struct gen_group
*blend_state
;
627 blend_state
= gen_spec_find_struct(spec
, "BLEND_STATE");
629 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
630 decode_structure(spec
, blend_state
, gtt
+ start
);
634 handle_3dstate_cc_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
637 struct gen_group
*cc_state
;
639 cc_state
= gen_spec_find_struct(spec
, "COLOR_CALC_STATE");
641 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
642 decode_structure(spec
, cc_state
, gtt
+ start
);
646 handle_3dstate_scissor_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
649 struct gen_group
*scissor_rect
;
651 scissor_rect
= gen_spec_find_struct(spec
, "SCISSOR_RECT");
653 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
654 decode_structure(spec
, scissor_rect
, gtt
+ start
);
658 handle_load_register_imm(struct gen_spec
*spec
, uint32_t *p
)
660 struct gen_group
*reg
= gen_spec_find_register(spec
, p
[1]);
663 printf("register %s (0x%x): 0x%x\n",
664 reg
->name
, reg
->register_offset
, p
[2]);
665 decode_structure(spec
, reg
, &p
[2]);
669 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
671 #define STATE_BASE_ADDRESS 0x61010000
673 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
675 #define _3DSTATE_INDEX_BUFFER 0x780a0000
676 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
678 #define _3DSTATE_VS 0x78100000
679 #define _3DSTATE_GS 0x78110000
680 #define _3DSTATE_HS 0x781b0000
681 #define _3DSTATE_DS 0x781d0000
683 #define _3DSTATE_CONSTANT_VS 0x78150000
684 #define _3DSTATE_CONSTANT_GS 0x78160000
685 #define _3DSTATE_CONSTANT_PS 0x78170000
686 #define _3DSTATE_CONSTANT_HS 0x78190000
687 #define _3DSTATE_CONSTANT_DS 0x781A0000
689 #define _3DSTATE_PS 0x78200000
691 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
692 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
693 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
694 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
695 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
697 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
698 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
699 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
701 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
702 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
703 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
704 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
705 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
707 #define _MI_LOAD_REGISTER_IMM 0x11000000
709 struct custom_handler
{
711 void (*handle
)(struct gen_spec
*spec
, uint32_t *p
);
712 } custom_handlers
[] = {
713 { STATE_BASE_ADDRESS
, handle_state_base_address
},
714 { MEDIA_INTERFACE_DESCRIPTOR_LOAD
, handle_media_interface_descriptor_load
},
715 { _3DSTATE_VERTEX_BUFFERS
, handle_3dstate_vertex_buffers
},
716 { _3DSTATE_INDEX_BUFFER
, handle_3dstate_index_buffer
},
717 { _3DSTATE_VS
, handle_3dstate_vs
},
718 { _3DSTATE_GS
, handle_3dstate_vs
},
719 { _3DSTATE_DS
, handle_3dstate_vs
},
720 { _3DSTATE_HS
, handle_3dstate_hs
},
721 { _3DSTATE_CONSTANT_VS
, handle_3dstate_constant
},
722 { _3DSTATE_CONSTANT_GS
, handle_3dstate_constant
},
723 { _3DSTATE_CONSTANT_PS
, handle_3dstate_constant
},
724 { _3DSTATE_CONSTANT_HS
, handle_3dstate_constant
},
725 { _3DSTATE_CONSTANT_DS
, handle_3dstate_constant
},
726 { _3DSTATE_PS
, handle_3dstate_ps
},
728 { _3DSTATE_BINDING_TABLE_POINTERS_VS
, handle_3dstate_binding_table_pointers
},
729 { _3DSTATE_BINDING_TABLE_POINTERS_HS
, handle_3dstate_binding_table_pointers
},
730 { _3DSTATE_BINDING_TABLE_POINTERS_DS
, handle_3dstate_binding_table_pointers
},
731 { _3DSTATE_BINDING_TABLE_POINTERS_GS
, handle_3dstate_binding_table_pointers
},
732 { _3DSTATE_BINDING_TABLE_POINTERS_PS
, handle_3dstate_binding_table_pointers
},
734 { _3DSTATE_SAMPLER_STATE_POINTERS_VS
, handle_3dstate_sampler_state_pointers
},
735 { _3DSTATE_SAMPLER_STATE_POINTERS_GS
, handle_3dstate_sampler_state_pointers
},
736 { _3DSTATE_SAMPLER_STATE_POINTERS_PS
, handle_3dstate_sampler_state_pointers
},
738 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC
, handle_3dstate_viewport_state_pointers_cc
},
739 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
, handle_3dstate_viewport_state_pointers_sf_clip
},
740 { _3DSTATE_BLEND_STATE_POINTERS
, handle_3dstate_blend_state_pointers
},
741 { _3DSTATE_CC_STATE_POINTERS
, handle_3dstate_cc_state_pointers
},
742 { _3DSTATE_SCISSOR_STATE_POINTERS
, handle_3dstate_scissor_state_pointers
},
743 { _MI_LOAD_REGISTER_IMM
, handle_load_register_imm
}
747 parse_commands(struct gen_spec
*spec
, uint32_t *cmds
, int size
, int engine
)
749 uint32_t *p
, *end
= cmds
+ size
/ 4;
750 unsigned int length
, i
;
751 struct gen_group
*inst
;
753 for (p
= cmds
; p
< end
; p
+= length
) {
754 inst
= gen_spec_find_instruction(spec
, p
);
756 printf("unknown instruction %08x\n", p
[0]);
757 length
= (p
[0] & 0xff) + 2;
760 length
= gen_group_get_length(inst
, p
);
762 const char *color
, *reset_color
= NORMAL
;
765 if (option_full_decode
) {
766 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
||
767 (p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
)
768 color
= GREEN_HEADER
;
774 if (option_color
== COLOR_NEVER
) {
779 if (option_print_offsets
)
780 offset
= (void *) p
- gtt
;
784 printf("%s0x%08"PRIx64
": 0x%08x: %-80s%s\n",
786 gen_group_get_name(inst
), reset_color
);
788 if (option_full_decode
) {
789 struct gen_field_iterator iter
;
791 int idx
= 0, dword_num
= 0;
792 gen_field_iterator_init(&iter
, inst
, p
,
793 option_color
== COLOR_ALWAYS
);
794 while (gen_field_iterator_next(&iter
)) {
796 print_dword_val(&iter
, offset
, &dword_num
);
798 token
= print_iterator_values(&iter
, &idx
);
800 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
801 offset
+ 4 * idx
, p
[idx
], idx
);
802 handle_struct_decode(spec
,token
, &p
[idx
]);
807 for (i
= 0; i
< ARRAY_LENGTH(custom_handlers
); i
++) {
808 if (gen_group_get_opcode(inst
) ==
809 custom_handlers
[i
].opcode
)
810 custom_handlers
[i
].handle(spec
, p
);
814 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
) {
815 uint64_t start
= get_address(spec
, &p
[1]);
817 if (p
[0] & (1 << 22)) {
818 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
819 * like a subroutine call. Commands that come afterwards get
820 * processed once the 2nd level batch buffer returns with
821 * MI_BATCH_BUFFER_END.
823 parse_commands(spec
, gtt
+ start
, gtt_end
- start
, engine
);
825 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
826 * like a goto. Nothing after it will ever get processed. In
827 * order to prevent the recursion from growing, we just reset the
831 /* We don't know where secondaries end so use the GTT end */
836 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
842 #define GEN_ENGINE_RENDER 1
843 #define GEN_ENGINE_BLITTER 2
846 handle_trace_block(struct gen_spec
*spec
, uint32_t *p
)
848 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
849 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
850 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
851 uint64_t offset
= p
[3];
852 uint32_t size
= p
[4];
853 int header_length
= p
[0] & 0xffff;
854 uint32_t *data
= p
+ header_length
+ 2;
855 int engine
= GEN_ENGINE_RENDER
;
857 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
858 offset
+= (uint64_t) p
[5] << 32;
861 case AUB_TRACE_OP_DATA_WRITE
:
862 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
864 if (gtt_size
< offset
+ size
) {
865 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
868 memcpy((char *) gtt
+ offset
, data
, size
);
869 if (gtt_end
< offset
+ size
)
870 gtt_end
= offset
+ size
;
872 case AUB_TRACE_OP_COMMAND_WRITE
:
874 case AUB_TRACE_TYPE_RING_PRB0
:
875 engine
= GEN_ENGINE_RENDER
;
877 case AUB_TRACE_TYPE_RING_PRB2
:
878 engine
= GEN_ENGINE_BLITTER
;
881 printf("command write to unknown ring %d\n", type
);
885 parse_commands(spec
, data
, size
, engine
);
894 uint32_t *map
, *end
, *cursor
;
898 static struct aub_file
*
899 aub_file_open(const char *filename
)
901 struct aub_file
*file
;
905 file
= calloc(1, sizeof *file
);
906 fd
= open(filename
, O_RDONLY
);
908 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
912 if (fstat(fd
, &sb
) == -1) {
913 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
917 file
->map
= mmap(NULL
, sb
.st_size
,
918 PROT_READ
, MAP_SHARED
, fd
, 0);
919 if (file
->map
== MAP_FAILED
) {
920 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
924 file
->cursor
= file
->map
;
925 file
->end
= file
->map
+ sb
.st_size
/ 4;
930 static struct aub_file
*
933 struct aub_file
*file
;
935 file
= calloc(1, sizeof *file
);
936 file
->stream
= stdin
;
941 #define TYPE(dw) (((dw) >> 29) & 7)
942 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
943 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
945 #define MAKE_HEADER(type, opcode, subopcode) \
946 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
950 /* Classic AUB opcodes */
951 #define OPCODE_AUB 0x01
952 #define SUBOPCODE_HEADER 0x05
953 #define SUBOPCODE_BLOCK 0x41
954 #define SUBOPCODE_BMP 0x1e
956 /* Newer version AUB opcode */
957 #define OPCODE_NEW_AUB 0x2e
958 #define SUBOPCODE_VERSION 0x00
959 #define SUBOPCODE_REG_WRITE 0x03
960 #define SUBOPCODE_MEM_POLL 0x05
961 #define SUBOPCODE_MEM_WRITE 0x06
963 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
969 { "bwr", MAKE_GEN(4, 0) },
970 { "cln", MAKE_GEN(4, 0) },
971 { "blc", MAKE_GEN(4, 0) },
972 { "ctg", MAKE_GEN(4, 0) },
973 { "el", MAKE_GEN(4, 0) },
974 { "il", MAKE_GEN(4, 0) },
975 { "sbr", MAKE_GEN(6, 0) },
976 { "ivb", MAKE_GEN(7, 0) },
977 { "lrb2", MAKE_GEN(0, 0) },
978 { "hsw", MAKE_GEN(7, 5) },
979 { "vlv", MAKE_GEN(7, 0) },
980 { "bdw", MAKE_GEN(8, 0) },
981 { "skl", MAKE_GEN(9, 0) },
982 { "chv", MAKE_GEN(8, 0) },
983 { "bxt", MAKE_GEN(9, 0) }
988 AUB_ITEM_DECODE_FAILED
,
989 AUB_ITEM_DECODE_NEED_MORE_DATA
,
993 aub_file_decode_batch(struct aub_file
*file
, struct gen_spec
*spec
)
995 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
996 int header_length
, bias
;
998 if (file
->end
- file
->cursor
< 1)
999 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1003 header_length
= h
& 0xffff;
1005 switch (OPCODE(h
)) {
1009 case OPCODE_NEW_AUB
:
1013 printf("unknown opcode %d at %td/%td\n",
1014 OPCODE(h
), file
->cursor
- file
->map
,
1015 file
->end
- file
->map
);
1016 return AUB_ITEM_DECODE_FAILED
;
1019 new_cursor
= p
+ header_length
+ bias
;
1020 if ((h
& 0xffff0000) == MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
)) {
1021 if (file
->end
- file
->cursor
< 4)
1022 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1023 new_cursor
+= p
[4] / 4;
1026 if (new_cursor
> file
->end
)
1027 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1029 switch (h
& 0xffff0000) {
1030 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
1032 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
1033 handle_trace_block(spec
, p
);
1035 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
1037 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
1038 printf("version block: dw1 %08x\n", p
[1]);
1039 device
= (p
[1] >> 8) & 0xff;
1040 printf(" device %s\n", device_map
[device
].name
);
1042 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
1043 printf("register write block: (dwords %d)\n", h
& 0xffff);
1044 printf(" reg 0x%x, data 0x%x\n", p
[1], p
[5]);
1046 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1047 printf("memory write block (dwords %d):\n", h
& 0xffff);
1048 printf(" address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1049 data_type
= (p
[3] >> 20) & 0xff;
1051 printf(" data type 0x%x\n", data_type
);
1052 printf(" address space 0x%x\n", (p
[3] >> 28) & 0xf);
1054 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1055 printf("memory poll block (dwords %d):\n", h
& 0xffff);
1058 printf("unknown block type=0x%x, opcode=0x%x, "
1059 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1062 file
->cursor
= new_cursor
;
1064 return AUB_ITEM_DECODE_OK
;
1068 aub_file_more_stuff(struct aub_file
*file
)
1070 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1073 #define AUB_READ_BUFFER_SIZE (4096)
1074 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1077 aub_file_data_grow(struct aub_file
*file
)
1079 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1080 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1081 uint32_t *new_start
= realloc(file
->map
, new_size
);
1083 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1084 file
->end
= new_start
+ (file
->end
- file
->map
);
1085 file
->map
= new_start
;
1086 file
->mem_end
= file
->map
+ (new_size
/ 4);
1090 aub_file_data_load(struct aub_file
*file
)
1094 if (file
->stream
== NULL
)
1097 /* First remove any consumed data */
1098 if (file
->cursor
> file
->map
) {
1099 memmove(file
->map
, file
->cursor
,
1100 (file
->end
- file
->cursor
) * 4);
1101 file
->end
-= file
->cursor
- file
->map
;
1102 file
->cursor
= file
->map
;
1105 /* Then load some new data in */
1106 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1107 aub_file_data_grow(file
);
1109 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1124 if (pipe(fds
) == -1)
1134 execlp("less", "less", "-FRSi", NULL
);
1143 print_help(const char *progname
, FILE *file
)
1146 "Usage: %s [OPTION]... [FILE]\n"
1147 "Decode aub file contents from either FILE or the standard input.\n\n"
1148 "A valid --gen option must be provided.\n\n"
1149 " --help display this help and exit\n"
1150 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1151 " --headers decode only command headers\n"
1152 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1153 " if omitted), 'always', or 'never'\n"
1154 " --no-pager don't launch pager\n"
1155 " --no-offsets don't print instruction offsets\n"
1156 " --xml=DIR load hardware xml description from directory DIR\n",
1160 int main(int argc
, char *argv
[])
1162 struct gen_spec
*spec
;
1163 struct aub_file
*file
;
1165 bool help
= false, pager
= true;
1166 char *input_file
= NULL
, *xml_path
= NULL
;
1167 char gen_val
[24] = { 0, };
1172 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1173 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1174 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1175 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1176 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1177 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1178 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1179 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1181 const struct option aubinator_opts
[] = {
1182 { "help", no_argument
, (int *) &help
, true },
1183 { "no-pager", no_argument
, (int *) &pager
, false },
1184 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1185 { "gen", required_argument
, NULL
, 'g' },
1186 { "headers", no_argument
, (int *) &option_full_decode
, false },
1187 { "color", required_argument
, NULL
, 'c' },
1188 { "xml", required_argument
, NULL
, 'x' },
1189 { NULL
, 0, NULL
, 0 }
1191 struct gen_device_info devinfo
;
1194 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1197 snprintf(gen_val
, sizeof(gen_val
), "%s", optarg
);
1200 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1201 option_color
= COLOR_ALWAYS
;
1202 else if (strcmp(optarg
, "never") == 0)
1203 option_color
= COLOR_NEVER
;
1204 else if (strcmp(optarg
, "auto") == 0)
1205 option_color
= COLOR_AUTO
;
1207 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1212 xml_path
= strdup(optarg
);
1219 if (help
|| argc
== 1) {
1220 print_help(argv
[0], stderr
);
1225 input_file
= argv
[optind
];
1227 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1228 if (!strcmp(gen_val
, gens
[i
].name
)) {
1235 fprintf(stderr
, "can't parse gen: '%s', expected ivb, byt, hsw, "
1236 "bdw, chv, skl, kbl or bxt\n", gen_val
);
1240 if (!gen_get_device_info(gen
->pci_id
, &devinfo
)) {
1241 fprintf(stderr
, "can't find device information: pci_id=0x%x name=%s\n",
1242 gen
->pci_id
, gen
->name
);
1247 /* Do this before we redirect stdout to pager. */
1248 if (option_color
== COLOR_AUTO
)
1249 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1251 if (isatty(1) && pager
)
1254 if (xml_path
== NULL
)
1255 spec
= gen_spec_load(&devinfo
);
1257 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
1258 disasm
= gen_disasm_create(gen
->pci_id
);
1260 if (spec
== NULL
|| disasm
== NULL
)
1263 if (input_file
== NULL
)
1264 file
= aub_file_stdin();
1266 file
= aub_file_open(input_file
);
1268 /* mmap a terabyte for our gtt space. */
1269 gtt_size
= 1ul << 40;
1270 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1271 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1272 if (gtt
== MAP_FAILED
) {
1273 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1277 while (aub_file_more_stuff(file
)) {
1278 switch (aub_file_decode_batch(file
, spec
)) {
1279 case AUB_ITEM_DECODE_OK
:
1281 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1282 if (!file
->stream
) {
1283 file
->cursor
= file
->end
;
1286 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1287 fprintf(stderr
, "failed to load data from stdin\n");
1292 fprintf(stderr
, "failed to parse aubdump data\n");
1299 /* close the stdout which is opened to write the output */
1305 return EXIT_SUCCESS
;