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];
242 handle_state_base_address(struct gen_spec
*spec
, uint32_t *p
)
244 uint64_t mask
= ~((1 << 12) - 1);
246 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
248 general_state_base
= get_qword(&p
[1]) & mask
;
250 surface_state_base
= get_qword(&p
[4]) & mask
;
252 dynamic_state_base
= get_qword(&p
[6]) & mask
;
254 instruction_base
= get_qword(&p
[10]) & mask
;
256 instruction_bound
= p
[15] & mask
;
259 surface_state_base
= p
[2] & mask
;
261 dynamic_state_base
= p
[3] & mask
;
263 instruction_base
= p
[5] & mask
;
265 instruction_bound
= p
[9] & mask
;
270 dump_samplers(struct gen_spec
*spec
, uint32_t offset
)
274 struct gen_group
*sampler_state
;
276 sampler_state
= gen_spec_find_struct(spec
, "SAMPLER_STATE");
278 start
= dynamic_state_base
+ offset
;
279 for (i
= 0; i
< 4; i
++) {
280 printf("sampler state %d\n", i
);
281 decode_structure(spec
, sampler_state
, gtt
+ start
+ i
* 16);
286 handle_media_interface_descriptor_load(struct gen_spec
*spec
, uint32_t *p
)
288 int i
, length
= p
[2] / 32;
289 struct gen_group
*descriptor_structure
;
290 uint32_t *descriptors
;
292 struct brw_instruction
*insns
;
294 descriptor_structure
=
295 gen_spec_find_struct(spec
, "INTERFACE_DESCRIPTOR_DATA");
296 if (descriptor_structure
== NULL
) {
297 printf("did not find INTERFACE_DESCRIPTOR_DATA info\n");
301 start
= dynamic_state_base
+ p
[3];
302 descriptors
= gtt
+ start
;
303 for (i
= 0; i
< length
; i
++, descriptors
+= 8) {
304 printf("descriptor %u: %08x\n", i
, *descriptors
);
305 decode_structure(spec
, descriptor_structure
, descriptors
);
307 start
= instruction_base
+ descriptors
[0];
308 if (!valid_offset(start
)) {
309 printf("kernel: %08"PRIx64
" <not valid>\n", start
);
312 printf("kernel: %08"PRIx64
"\n", start
);
315 insns
= (struct brw_instruction
*) (gtt
+ start
);
316 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
318 dump_samplers(spec
, descriptors
[3] & ~0x1f);
319 dump_binding_table(spec
, descriptors
[4] & ~0x1f);
323 /* Heuristic to determine whether a uint32_t is probably actually a float
324 * (http://stackoverflow.com/a/2953466)
328 probably_float(uint32_t bits
)
330 int exp
= ((bits
& 0x7f800000U
) >> 23) - 127;
331 uint32_t mant
= bits
& 0x007fffff;
334 if (exp
== -127 && mant
== 0)
337 /* +- 1 billionth to 1 billion */
338 if (-30 <= exp
&& exp
<= 30)
341 /* some value with only a few binary digits */
342 if ((mant
& 0x0000ffff) == 0)
349 handle_3dstate_vertex_buffers(struct gen_spec
*spec
, uint32_t *p
)
351 uint32_t *end
, *s
, *dw
, *dwend
;
353 int n
, i
, count
, stride
;
355 end
= (p
[0] & 0xff) + p
+ 2;
356 for (s
= &p
[1], n
= 0; s
< end
; s
+= 4, n
++) {
357 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
358 offset
= *(uint64_t *) &s
[1];
359 dwend
= gtt
+ offset
+ s
[3];
362 dwend
= gtt
+ s
[2] + 1;
365 stride
= field(s
[0], 0, 11);
367 printf("vertex buffer %d, size %d\n", n
, s
[3]);
368 for (dw
= gtt
+ offset
, i
= 0; dw
< dwend
&& i
< 256; dw
++) {
369 if (count
== 0 && count
% (8 * 4) == 0)
372 if (probably_float(*dw
))
373 printf(" %8.2f", *(float *) dw
);
375 printf(" 0x%08x", *dw
);
380 if (count
== stride
) {
383 } else if (count
% (8 * 4) == 0) {
389 if (count
> 0 && count
% (8 * 4) != 0)
395 handle_3dstate_vs(struct gen_spec
*spec
, uint32_t *p
)
398 struct brw_instruction
*insns
;
401 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
402 start
= get_qword(&p
[1]);
403 vs_enable
= p
[7] & 1;
406 vs_enable
= p
[5] & 1;
410 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
411 instruction_base
, start
);
413 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
414 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
419 handle_3dstate_hs(struct gen_spec
*spec
, uint32_t *p
)
422 struct brw_instruction
*insns
;
425 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
426 start
= get_qword(&p
[4]);
431 hs_enable
= p
[2] & 0x80000000;
434 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
435 instruction_base
, start
);
437 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
438 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
443 handle_3dstate_constant(struct gen_spec
*spec
, uint32_t *p
)
449 for (i
= 0; i
< 4; i
++) {
450 length
= (p
[1 + i
/ 2] >> (i
& 1) * 16) & 0xffff;
451 f
= (float *) (gtt
+ p
[3 + i
* 2] + dynamic_state_base
);
453 for (j
= 0; j
< length
* 8; j
++) {
454 if (probably_float(dw
[j
]))
455 printf(" %04.3f", f
[j
]);
457 printf(" 0x%08x", dw
[j
]);
466 handle_3dstate_ps(struct gen_spec
*spec
, uint32_t *p
)
468 uint32_t mask
= ~((1 << 6) - 1);
470 struct brw_instruction
*insns
;
471 static const char unused
[] = "unused";
472 static const char *pixel_type
[3] = {"8 pixel", "16 pixel", "32 pixel"};
473 const char *k0
, *k1
, *k2
;
474 uint32_t k_mask
, k1_offset
, k2_offset
;
476 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
487 #define DISPATCH_16 2
488 #define DISPATCH_32 4
501 case DISPATCH_8
| DISPATCH_16
:
511 case DISPATCH_16
| DISPATCH_32
:
516 case DISPATCH_8
| DISPATCH_16
| DISPATCH_32
:
528 start
= instruction_base
+ (p
[1] & mask
);
529 printf(" Kernel[0] %s\n", k0
);
531 insns
= (struct brw_instruction
*) (gtt
+ start
);
532 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
535 start
= instruction_base
+ (p
[k1_offset
] & mask
);
536 printf(" Kernel[1] %s\n", k1
);
538 insns
= (struct brw_instruction
*) (gtt
+ start
);
539 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
542 start
= instruction_base
+ (p
[k2_offset
] & mask
);
543 printf(" Kernel[2] %s\n", k2
);
545 insns
= (struct brw_instruction
*) (gtt
+ start
);
546 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
551 handle_3dstate_binding_table_pointers(struct gen_spec
*spec
, uint32_t *p
)
553 dump_binding_table(spec
, p
[1]);
557 handle_3dstate_sampler_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
559 dump_samplers(spec
, p
[1]);
563 handle_3dstate_viewport_state_pointers_cc(struct gen_spec
*spec
, uint32_t *p
)
566 struct gen_group
*cc_viewport
;
568 cc_viewport
= gen_spec_find_struct(spec
, "CC_VIEWPORT");
570 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
571 for (uint32_t i
= 0; i
< 4; i
++) {
572 printf("viewport %d\n", i
);
573 decode_structure(spec
, cc_viewport
, gtt
+ start
+ i
* 8);
578 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec
*spec
,
582 struct gen_group
*sf_clip_viewport
;
584 sf_clip_viewport
= gen_spec_find_struct(spec
, "SF_CLIP_VIEWPORT");
586 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
587 for (uint32_t i
= 0; i
< 4; i
++) {
588 printf("viewport %d\n", i
);
589 decode_structure(spec
, sf_clip_viewport
, gtt
+ start
+ i
* 64);
594 handle_3dstate_blend_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
597 struct gen_group
*blend_state
;
599 blend_state
= gen_spec_find_struct(spec
, "BLEND_STATE");
601 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
602 decode_structure(spec
, blend_state
, gtt
+ start
);
606 handle_3dstate_cc_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
609 struct gen_group
*cc_state
;
611 cc_state
= gen_spec_find_struct(spec
, "COLOR_CALC_STATE");
613 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
614 decode_structure(spec
, cc_state
, gtt
+ start
);
618 handle_3dstate_scissor_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
621 struct gen_group
*scissor_rect
;
623 scissor_rect
= gen_spec_find_struct(spec
, "SCISSOR_RECT");
625 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
626 decode_structure(spec
, scissor_rect
, gtt
+ start
);
630 handle_load_register_imm(struct gen_spec
*spec
, uint32_t *p
)
632 struct gen_group
*reg
= gen_spec_find_register(spec
, p
[1]);
635 printf("register %s (0x%x): 0x%x\n",
636 reg
->name
, reg
->register_offset
, p
[2]);
637 decode_structure(spec
, reg
, &p
[2]);
641 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
643 #define STATE_BASE_ADDRESS 0x61010000
645 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
647 #define _3DSTATE_INDEX_BUFFER 0x780a0000
648 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
650 #define _3DSTATE_VS 0x78100000
651 #define _3DSTATE_GS 0x78110000
652 #define _3DSTATE_HS 0x781b0000
653 #define _3DSTATE_DS 0x781d0000
655 #define _3DSTATE_CONSTANT_VS 0x78150000
656 #define _3DSTATE_CONSTANT_GS 0x78160000
657 #define _3DSTATE_CONSTANT_PS 0x78170000
658 #define _3DSTATE_CONSTANT_HS 0x78190000
659 #define _3DSTATE_CONSTANT_DS 0x781A0000
661 #define _3DSTATE_PS 0x78200000
663 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
664 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
665 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
666 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
667 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
669 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
670 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
671 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
673 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
674 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
675 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
676 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
677 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
679 #define _MI_LOAD_REGISTER_IMM 0x11000000
681 struct custom_handler
{
683 void (*handle
)(struct gen_spec
*spec
, uint32_t *p
);
684 } custom_handlers
[] = {
685 { STATE_BASE_ADDRESS
, handle_state_base_address
},
686 { MEDIA_INTERFACE_DESCRIPTOR_LOAD
, handle_media_interface_descriptor_load
},
687 { _3DSTATE_VERTEX_BUFFERS
, handle_3dstate_vertex_buffers
},
688 { _3DSTATE_INDEX_BUFFER
, handle_3dstate_index_buffer
},
689 { _3DSTATE_VS
, handle_3dstate_vs
},
690 { _3DSTATE_GS
, handle_3dstate_vs
},
691 { _3DSTATE_DS
, handle_3dstate_vs
},
692 { _3DSTATE_HS
, handle_3dstate_hs
},
693 { _3DSTATE_CONSTANT_VS
, handle_3dstate_constant
},
694 { _3DSTATE_CONSTANT_GS
, handle_3dstate_constant
},
695 { _3DSTATE_CONSTANT_PS
, handle_3dstate_constant
},
696 { _3DSTATE_CONSTANT_HS
, handle_3dstate_constant
},
697 { _3DSTATE_CONSTANT_DS
, handle_3dstate_constant
},
698 { _3DSTATE_PS
, handle_3dstate_ps
},
700 { _3DSTATE_BINDING_TABLE_POINTERS_VS
, handle_3dstate_binding_table_pointers
},
701 { _3DSTATE_BINDING_TABLE_POINTERS_HS
, handle_3dstate_binding_table_pointers
},
702 { _3DSTATE_BINDING_TABLE_POINTERS_DS
, handle_3dstate_binding_table_pointers
},
703 { _3DSTATE_BINDING_TABLE_POINTERS_GS
, handle_3dstate_binding_table_pointers
},
704 { _3DSTATE_BINDING_TABLE_POINTERS_PS
, handle_3dstate_binding_table_pointers
},
706 { _3DSTATE_SAMPLER_STATE_POINTERS_VS
, handle_3dstate_sampler_state_pointers
},
707 { _3DSTATE_SAMPLER_STATE_POINTERS_GS
, handle_3dstate_sampler_state_pointers
},
708 { _3DSTATE_SAMPLER_STATE_POINTERS_PS
, handle_3dstate_sampler_state_pointers
},
710 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC
, handle_3dstate_viewport_state_pointers_cc
},
711 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
, handle_3dstate_viewport_state_pointers_sf_clip
},
712 { _3DSTATE_BLEND_STATE_POINTERS
, handle_3dstate_blend_state_pointers
},
713 { _3DSTATE_CC_STATE_POINTERS
, handle_3dstate_cc_state_pointers
},
714 { _3DSTATE_SCISSOR_STATE_POINTERS
, handle_3dstate_scissor_state_pointers
},
715 { _MI_LOAD_REGISTER_IMM
, handle_load_register_imm
}
719 parse_commands(struct gen_spec
*spec
, uint32_t *cmds
, int size
, int engine
)
721 uint32_t *p
, *end
= cmds
+ size
/ 4;
722 unsigned int length
, i
;
723 struct gen_group
*inst
;
725 for (p
= cmds
; p
< end
; p
+= length
) {
726 inst
= gen_spec_find_instruction(spec
, p
);
728 printf("unknown instruction %08x\n", p
[0]);
729 length
= (p
[0] & 0xff) + 2;
732 length
= gen_group_get_length(inst
, p
);
734 const char *color
, *reset_color
= NORMAL
;
737 if (option_full_decode
) {
738 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
||
739 (p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
)
740 color
= GREEN_HEADER
;
746 if (option_color
== COLOR_NEVER
) {
751 if (option_print_offsets
)
752 offset
= (void *) p
- gtt
;
756 printf("%s0x%08"PRIx64
": 0x%08x: %-80s%s\n",
758 gen_group_get_name(inst
), reset_color
);
760 if (option_full_decode
) {
761 struct gen_field_iterator iter
;
763 int idx
= 0, dword_num
= 0;
764 gen_field_iterator_init(&iter
, inst
, p
,
765 option_color
== COLOR_ALWAYS
);
766 while (gen_field_iterator_next(&iter
)) {
768 print_dword_val(&iter
, offset
, &dword_num
);
770 token
= print_iterator_values(&iter
, &idx
);
772 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
773 offset
+ 4 * idx
, p
[idx
], idx
);
774 handle_struct_decode(spec
,token
, &p
[idx
]);
779 for (i
= 0; i
< ARRAY_LENGTH(custom_handlers
); i
++) {
780 if (gen_group_get_opcode(inst
) ==
781 custom_handlers
[i
].opcode
)
782 custom_handlers
[i
].handle(spec
, p
);
786 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
) {
788 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
789 start
= get_qword(&p
[1]);
793 if (p
[0] & (1 << 22)) {
794 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" set acts
795 * like a subroutine call. Commands that come afterwards get
796 * processed once the 2nd level batch buffer returns with
797 * MI_BATCH_BUFFER_END.
799 parse_commands(spec
, gtt
+ start
, gtt_end
- start
, engine
);
801 /* MI_BATCH_BUFFER_START with "2nd Level Batch Buffer" unset acts
802 * like a goto. Nothing after it will ever get processed. In
803 * order to prevent the recursion from growing, we just reset the
807 /* We don't know where secondaries end so use the GTT end */
812 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
818 #define GEN_ENGINE_RENDER 1
819 #define GEN_ENGINE_BLITTER 2
822 handle_trace_block(struct gen_spec
*spec
, uint32_t *p
)
824 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
825 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
826 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
827 uint64_t offset
= p
[3];
828 uint32_t size
= p
[4];
829 int header_length
= p
[0] & 0xffff;
830 uint32_t *data
= p
+ header_length
+ 2;
831 int engine
= GEN_ENGINE_RENDER
;
833 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
834 offset
+= (uint64_t) p
[5] << 32;
837 case AUB_TRACE_OP_DATA_WRITE
:
838 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
840 if (gtt_size
< offset
+ size
) {
841 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
844 memcpy((char *) gtt
+ offset
, data
, size
);
845 if (gtt_end
< offset
+ size
)
846 gtt_end
= offset
+ size
;
848 case AUB_TRACE_OP_COMMAND_WRITE
:
850 case AUB_TRACE_TYPE_RING_PRB0
:
851 engine
= GEN_ENGINE_RENDER
;
853 case AUB_TRACE_TYPE_RING_PRB2
:
854 engine
= GEN_ENGINE_BLITTER
;
857 printf("command write to unknown ring %d\n", type
);
861 parse_commands(spec
, data
, size
, engine
);
870 uint32_t *map
, *end
, *cursor
;
874 static struct aub_file
*
875 aub_file_open(const char *filename
)
877 struct aub_file
*file
;
881 file
= calloc(1, sizeof *file
);
882 fd
= open(filename
, O_RDONLY
);
884 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
888 if (fstat(fd
, &sb
) == -1) {
889 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
893 file
->map
= mmap(NULL
, sb
.st_size
,
894 PROT_READ
, MAP_SHARED
, fd
, 0);
895 if (file
->map
== MAP_FAILED
) {
896 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
900 file
->cursor
= file
->map
;
901 file
->end
= file
->map
+ sb
.st_size
/ 4;
906 static struct aub_file
*
909 struct aub_file
*file
;
911 file
= calloc(1, sizeof *file
);
912 file
->stream
= stdin
;
917 #define TYPE(dw) (((dw) >> 29) & 7)
918 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
919 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
921 #define MAKE_HEADER(type, opcode, subopcode) \
922 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
926 /* Classic AUB opcodes */
927 #define OPCODE_AUB 0x01
928 #define SUBOPCODE_HEADER 0x05
929 #define SUBOPCODE_BLOCK 0x41
930 #define SUBOPCODE_BMP 0x1e
932 /* Newer version AUB opcode */
933 #define OPCODE_NEW_AUB 0x2e
934 #define SUBOPCODE_VERSION 0x00
935 #define SUBOPCODE_REG_WRITE 0x03
936 #define SUBOPCODE_MEM_POLL 0x05
937 #define SUBOPCODE_MEM_WRITE 0x06
939 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
945 { "bwr", MAKE_GEN(4, 0) },
946 { "cln", MAKE_GEN(4, 0) },
947 { "blc", MAKE_GEN(4, 0) },
948 { "ctg", MAKE_GEN(4, 0) },
949 { "el", MAKE_GEN(4, 0) },
950 { "il", MAKE_GEN(4, 0) },
951 { "sbr", MAKE_GEN(6, 0) },
952 { "ivb", MAKE_GEN(7, 0) },
953 { "lrb2", MAKE_GEN(0, 0) },
954 { "hsw", MAKE_GEN(7, 5) },
955 { "vlv", MAKE_GEN(7, 0) },
956 { "bdw", MAKE_GEN(8, 0) },
957 { "skl", MAKE_GEN(9, 0) },
958 { "chv", MAKE_GEN(8, 0) },
959 { "bxt", MAKE_GEN(9, 0) }
964 AUB_ITEM_DECODE_FAILED
,
965 AUB_ITEM_DECODE_NEED_MORE_DATA
,
969 aub_file_decode_batch(struct aub_file
*file
, struct gen_spec
*spec
)
971 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
972 int header_length
, payload_size
, bias
;
974 if (file
->end
- file
->cursor
< 1)
975 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
979 header_length
= h
& 0xffff;
989 printf("unknown opcode %d at %td/%td\n",
990 OPCODE(h
), file
->cursor
- file
->map
,
991 file
->end
- file
->map
);
992 return AUB_ITEM_DECODE_FAILED
;
996 switch (h
& 0xffff0000) {
997 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
998 if (file
->end
- file
->cursor
< 12)
999 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1000 payload_size
= p
[12];
1002 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
1003 if (file
->end
- file
->cursor
< 4)
1004 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1005 payload_size
= p
[4];
1011 new_cursor
= p
+ header_length
+ bias
+ payload_size
/ 4;
1012 if (new_cursor
> file
->end
)
1013 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
1015 switch (h
& 0xffff0000) {
1016 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
1018 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
1019 handle_trace_block(spec
, p
);
1021 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
1023 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
1024 printf("version block: dw1 %08x\n", p
[1]);
1025 device
= (p
[1] >> 8) & 0xff;
1026 printf(" device %s\n", device_map
[device
].name
);
1028 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
1029 printf("register write block: (dwords %d)\n", h
& 0xffff);
1030 printf(" reg 0x%x, data 0x%x\n", p
[1], p
[5]);
1032 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1033 printf("memory write block (dwords %d):\n", h
& 0xffff);
1034 printf(" address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1035 data_type
= (p
[3] >> 20) & 0xff;
1037 printf(" data type 0x%x\n", data_type
);
1038 printf(" address space 0x%x\n", (p
[3] >> 28) & 0xf);
1040 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1041 printf("memory poll block (dwords %d):\n", h
& 0xffff);
1044 printf("unknown block type=0x%x, opcode=0x%x, "
1045 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1048 file
->cursor
= new_cursor
;
1050 return AUB_ITEM_DECODE_OK
;
1054 aub_file_more_stuff(struct aub_file
*file
)
1056 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1059 #define AUB_READ_BUFFER_SIZE (4096)
1060 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1063 aub_file_data_grow(struct aub_file
*file
)
1065 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1066 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1067 uint32_t *new_start
= realloc(file
->map
, new_size
);
1069 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1070 file
->end
= new_start
+ (file
->end
- file
->map
);
1071 file
->map
= new_start
;
1072 file
->mem_end
= file
->map
+ (new_size
/ 4);
1076 aub_file_data_load(struct aub_file
*file
)
1080 if (file
->stream
== NULL
)
1083 /* First remove any consumed data */
1084 if (file
->cursor
> file
->map
) {
1085 memmove(file
->map
, file
->cursor
,
1086 (file
->end
- file
->cursor
) * 4);
1087 file
->end
-= file
->cursor
- file
->map
;
1088 file
->cursor
= file
->map
;
1091 /* Then load some new data in */
1092 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1093 aub_file_data_grow(file
);
1095 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1110 if (pipe(fds
) == -1)
1120 execlp("less", "less", "-FRSi", NULL
);
1129 print_help(const char *progname
, FILE *file
)
1132 "Usage: %s [OPTION]... [FILE]\n"
1133 "Decode aub file contents from either FILE or the standard input.\n\n"
1134 "A valid --gen option must be provided.\n\n"
1135 " --help display this help and exit\n"
1136 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1137 " --headers decode only command headers\n"
1138 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1139 " if omitted), 'always', or 'never'\n"
1140 " --no-pager don't launch pager\n"
1141 " --no-offsets don't print instruction offsets\n"
1142 " --xml=DIR load hardware xml description from directory DIR\n",
1146 int main(int argc
, char *argv
[])
1148 struct gen_spec
*spec
;
1149 struct aub_file
*file
;
1151 bool help
= false, pager
= true;
1152 char *input_file
= NULL
, *xml_path
= NULL
;
1153 char gen_val
[24] = { 0, };
1158 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1159 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1160 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1161 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1162 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1163 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1164 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1165 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1167 const struct option aubinator_opts
[] = {
1168 { "help", no_argument
, (int *) &help
, true },
1169 { "no-pager", no_argument
, (int *) &pager
, false },
1170 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1171 { "gen", required_argument
, NULL
, 'g' },
1172 { "headers", no_argument
, (int *) &option_full_decode
, false },
1173 { "color", required_argument
, NULL
, 'c' },
1174 { "xml", required_argument
, NULL
, 'x' },
1175 { NULL
, 0, NULL
, 0 }
1177 struct gen_device_info devinfo
;
1180 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1183 snprintf(gen_val
, sizeof(gen_val
), "%s", optarg
);
1186 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1187 option_color
= COLOR_ALWAYS
;
1188 else if (strcmp(optarg
, "never") == 0)
1189 option_color
= COLOR_NEVER
;
1190 else if (strcmp(optarg
, "auto") == 0)
1191 option_color
= COLOR_AUTO
;
1193 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1198 xml_path
= strdup(optarg
);
1205 if (help
|| argc
== 1) {
1206 print_help(argv
[0], stderr
);
1211 input_file
= argv
[optind
];
1213 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1214 if (!strcmp(gen_val
, gens
[i
].name
)) {
1221 fprintf(stderr
, "can't parse gen: '%s', expected ivb, byt, hsw, "
1222 "bdw, chv, skl, kbl or bxt\n", gen_val
);
1226 if (!gen_get_device_info(gen
->pci_id
, &devinfo
)) {
1227 fprintf(stderr
, "can't find device information: pci_id=0x%x name=%s\n",
1228 gen
->pci_id
, gen
->name
);
1233 /* Do this before we redirect stdout to pager. */
1234 if (option_color
== COLOR_AUTO
)
1235 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1237 if (isatty(1) && pager
)
1240 if (xml_path
== NULL
)
1241 spec
= gen_spec_load(&devinfo
);
1243 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
1244 disasm
= gen_disasm_create(gen
->pci_id
);
1246 if (spec
== NULL
|| disasm
== NULL
)
1249 if (input_file
== NULL
)
1250 file
= aub_file_stdin();
1252 file
= aub_file_open(input_file
);
1254 /* mmap a terabyte for our gtt space. */
1255 gtt_size
= 1ul << 40;
1256 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1257 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1258 if (gtt
== MAP_FAILED
) {
1259 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1263 while (aub_file_more_stuff(file
)) {
1264 switch (aub_file_decode_batch(file
, spec
)) {
1265 case AUB_ITEM_DECODE_OK
:
1267 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1268 if (!file
->stream
) {
1269 file
->cursor
= file
->end
;
1272 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1273 fprintf(stderr
, "failed to load data from stdin\n");
1278 fprintf(stderr
, "failed to parse aubdump data\n");
1285 /* close the stdout which is opened to write the output */
1291 return EXIT_SUCCESS
;