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 printf(" %s: %s\n", iter
->name
, iter
->value
);
116 token
= strtok(iter
->value
, " ");
118 token
= strtok(NULL
, " ");
119 *idx
= atoi(strtok(NULL
, ">"));
123 printf(" %s:<struct %s>\n", iter
->name
, token
);
129 decode_structure(struct gen_spec
*spec
, struct gen_group
*strct
,
132 struct gen_field_iterator iter
;
134 int idx
= 0, dword_num
= 0;
137 if (option_print_offsets
)
138 offset
= (void *) p
- gtt
;
142 gen_field_iterator_init(&iter
, strct
, p
,
143 option_color
== COLOR_ALWAYS
);
144 while (gen_field_iterator_next(&iter
)) {
146 print_dword_val(&iter
, offset
, &dword_num
);
147 token
= print_iterator_values(&iter
, &idx
);
149 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, token
);
150 decode_structure(spec
, struct_val
, &p
[idx
]);
157 handle_struct_decode(struct gen_spec
*spec
, char *struct_name
, uint32_t *p
)
159 if (struct_name
== NULL
)
161 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, struct_name
);
162 decode_structure(spec
, struct_val
, p
);
166 dump_binding_table(struct gen_spec
*spec
, uint32_t offset
)
168 uint32_t *pointers
, i
;
170 struct gen_group
*surface_state
;
172 surface_state
= gen_spec_find_struct(spec
, "RENDER_SURFACE_STATE");
173 if (surface_state
== NULL
) {
174 printf("did not find RENDER_SURFACE_STATE info\n");
178 start
= surface_state_base
+ offset
;
179 pointers
= gtt
+ start
;
180 for (i
= 0; i
< 16; i
++) {
181 if (pointers
[i
] == 0)
183 start
= pointers
[i
] + surface_state_base
;
184 if (!valid_offset(start
)) {
185 printf("pointer %u: %08x <not valid>\n",
189 printf("pointer %u: %08x\n", i
, pointers
[i
]);
192 decode_structure(spec
, surface_state
, gtt
+ start
);
197 handle_3dstate_index_buffer(struct gen_spec
*spec
, uint32_t *p
)
200 uint32_t length
, i
, type
, size
;
203 type
= (p
[1] >> 8) & 3;
205 length
= p
[4] / size
;
211 for (i
= 0; i
< length
; i
++) {
214 printf("%3d ", ((uint8_t *)start
)[i
]);
217 printf("%3d ", ((uint16_t *)start
)[i
]);
220 printf("%3d ", ((uint32_t *)start
)[i
]);
224 if (length
< p
[4] / size
)
230 static inline uint64_t
231 get_qword(uint32_t *p
)
233 return ((uint64_t) p
[1] << 32) | p
[0];
237 handle_state_base_address(struct gen_spec
*spec
, uint32_t *p
)
239 uint64_t mask
= ~((1 << 12) - 1);
241 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
243 general_state_base
= get_qword(&p
[1]) & mask
;
245 surface_state_base
= get_qword(&p
[4]) & mask
;
247 dynamic_state_base
= get_qword(&p
[6]) & mask
;
249 instruction_base
= get_qword(&p
[10]) & mask
;
251 instruction_bound
= p
[15] & mask
;
254 surface_state_base
= p
[2] & mask
;
256 dynamic_state_base
= p
[3] & mask
;
258 instruction_base
= p
[5] & mask
;
260 instruction_bound
= p
[9] & mask
;
265 dump_samplers(struct gen_spec
*spec
, uint32_t offset
)
269 struct gen_group
*sampler_state
;
271 sampler_state
= gen_spec_find_struct(spec
, "SAMPLER_STATE");
273 start
= dynamic_state_base
+ offset
;
274 for (i
= 0; i
< 4; i
++) {
275 printf("sampler state %d\n", i
);
276 decode_structure(spec
, sampler_state
, gtt
+ start
+ i
* 16);
281 handle_media_interface_descriptor_load(struct gen_spec
*spec
, uint32_t *p
)
283 int i
, length
= p
[2] / 32;
284 struct gen_group
*descriptor_structure
;
285 uint32_t *descriptors
;
287 struct brw_instruction
*insns
;
289 descriptor_structure
=
290 gen_spec_find_struct(spec
, "INTERFACE_DESCRIPTOR_DATA");
291 if (descriptor_structure
== NULL
) {
292 printf("did not find INTERFACE_DESCRIPTOR_DATA info\n");
296 start
= dynamic_state_base
+ p
[3];
297 descriptors
= gtt
+ start
;
298 for (i
= 0; i
< length
; i
++, descriptors
+= 8) {
299 printf("descriptor %u: %08x\n", i
, *descriptors
);
300 decode_structure(spec
, descriptor_structure
, descriptors
);
302 start
= instruction_base
+ descriptors
[0];
303 if (!valid_offset(start
)) {
304 printf("kernel: %08"PRIx64
" <not valid>\n", start
);
307 printf("kernel: %08"PRIx64
"\n", start
);
310 insns
= (struct brw_instruction
*) (gtt
+ start
);
311 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
313 dump_samplers(spec
, descriptors
[3] & ~0x1f);
314 dump_binding_table(spec
, descriptors
[4] & ~0x1f);
318 /* Heuristic to determine whether a uint32_t is probably actually a float
319 * (http://stackoverflow.com/a/2953466)
323 probably_float(uint32_t bits
)
325 int exp
= ((bits
& 0x7f800000U
) >> 23) - 127;
326 uint32_t mant
= bits
& 0x007fffff;
329 if (exp
== -127 && mant
== 0)
332 /* +- 1 billionth to 1 billion */
333 if (-30 <= exp
&& exp
<= 30)
336 /* some value with only a few binary digits */
337 if ((mant
& 0x0000ffff) == 0)
344 handle_3dstate_vertex_buffers(struct gen_spec
*spec
, uint32_t *p
)
346 uint32_t *end
, *s
, *dw
, *dwend
;
348 int n
, i
, count
, stride
;
350 end
= (p
[0] & 0xff) + p
+ 2;
351 for (s
= &p
[1], n
= 0; s
< end
; s
+= 4, n
++) {
352 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
353 offset
= *(uint64_t *) &s
[1];
354 dwend
= gtt
+ offset
+ s
[3];
357 dwend
= gtt
+ s
[2] + 1;
360 stride
= field(s
[0], 0, 11);
362 printf("vertex buffer %d, size %d\n", n
, s
[3]);
363 for (dw
= gtt
+ offset
, i
= 0; dw
< dwend
&& i
< 256; dw
++) {
364 if (count
== 0 && count
% (8 * 4) == 0)
367 if (probably_float(*dw
))
368 printf(" %8.2f", *(float *) dw
);
370 printf(" 0x%08x", *dw
);
375 if (count
== stride
) {
378 } else if (count
% (8 * 4) == 0) {
384 if (count
> 0 && count
% (8 * 4) != 0)
390 handle_3dstate_vs(struct gen_spec
*spec
, uint32_t *p
)
393 struct brw_instruction
*insns
;
396 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
397 start
= get_qword(&p
[1]);
398 vs_enable
= p
[7] & 1;
401 vs_enable
= p
[5] & 1;
405 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
406 instruction_base
, start
);
408 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
409 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
414 handle_3dstate_hs(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
[4]);
426 hs_enable
= p
[2] & 0x80000000;
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_constant(struct gen_spec
*spec
, uint32_t *p
)
444 for (i
= 0; i
< 4; i
++) {
445 length
= (p
[1 + i
/ 2] >> (i
& 1) * 16) & 0xffff;
446 f
= (float *) (gtt
+ p
[3 + i
* 2] + dynamic_state_base
);
448 for (j
= 0; j
< length
* 8; j
++) {
449 if (probably_float(dw
[j
]))
450 printf(" %04.3f", f
[j
]);
452 printf(" 0x%08x", dw
[j
]);
461 handle_3dstate_ps(struct gen_spec
*spec
, uint32_t *p
)
463 uint32_t mask
= ~((1 << 6) - 1);
465 struct brw_instruction
*insns
;
466 static const char unused
[] = "unused";
467 static const char *pixel_type
[3] = {"8 pixel", "16 pixel", "32 pixel"};
468 const char *k0
, *k1
, *k2
;
469 uint32_t k_mask
, k1_offset
, k2_offset
;
471 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
482 #define DISPATCH_16 2
483 #define DISPATCH_32 4
496 case DISPATCH_8
| DISPATCH_16
:
506 case DISPATCH_16
| DISPATCH_32
:
511 case DISPATCH_8
| DISPATCH_16
| DISPATCH_32
:
523 start
= instruction_base
+ (p
[1] & mask
);
524 printf(" Kernel[0] %s\n", k0
);
526 insns
= (struct brw_instruction
*) (gtt
+ start
);
527 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
530 start
= instruction_base
+ (p
[k1_offset
] & mask
);
531 printf(" Kernel[1] %s\n", k1
);
533 insns
= (struct brw_instruction
*) (gtt
+ start
);
534 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
537 start
= instruction_base
+ (p
[k2_offset
] & mask
);
538 printf(" Kernel[2] %s\n", k2
);
540 insns
= (struct brw_instruction
*) (gtt
+ start
);
541 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
546 handle_3dstate_binding_table_pointers(struct gen_spec
*spec
, uint32_t *p
)
548 dump_binding_table(spec
, p
[1]);
552 handle_3dstate_sampler_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
554 dump_samplers(spec
, p
[1]);
558 handle_3dstate_viewport_state_pointers_cc(struct gen_spec
*spec
, uint32_t *p
)
561 struct gen_group
*cc_viewport
;
563 cc_viewport
= gen_spec_find_struct(spec
, "CC_VIEWPORT");
565 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
566 for (uint32_t i
= 0; i
< 4; i
++) {
567 printf("viewport %d\n", i
);
568 decode_structure(spec
, cc_viewport
, gtt
+ start
+ i
* 8);
573 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec
*spec
,
577 struct gen_group
*sf_clip_viewport
;
579 sf_clip_viewport
= gen_spec_find_struct(spec
, "SF_CLIP_VIEWPORT");
581 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
582 for (uint32_t i
= 0; i
< 4; i
++) {
583 printf("viewport %d\n", i
);
584 decode_structure(spec
, sf_clip_viewport
, gtt
+ start
+ i
* 64);
589 handle_3dstate_blend_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
592 struct gen_group
*blend_state
;
594 blend_state
= gen_spec_find_struct(spec
, "BLEND_STATE");
596 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
597 decode_structure(spec
, blend_state
, gtt
+ start
);
601 handle_3dstate_cc_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
604 struct gen_group
*cc_state
;
606 cc_state
= gen_spec_find_struct(spec
, "COLOR_CALC_STATE");
608 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
609 decode_structure(spec
, cc_state
, gtt
+ start
);
613 handle_3dstate_scissor_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
616 struct gen_group
*scissor_rect
;
618 scissor_rect
= gen_spec_find_struct(spec
, "SCISSOR_RECT");
620 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
621 decode_structure(spec
, scissor_rect
, gtt
+ start
);
625 handle_load_register_imm(struct gen_spec
*spec
, uint32_t *p
)
627 struct gen_group
*reg
= gen_spec_find_register(spec
, p
[1]);
630 printf("register %s (0x%x): 0x%x\n",
631 reg
->name
, reg
->register_offset
, p
[2]);
632 decode_structure(spec
, reg
, &p
[2]);
636 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
638 #define STATE_BASE_ADDRESS 0x61010000
640 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
642 #define _3DSTATE_INDEX_BUFFER 0x780a0000
643 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
645 #define _3DSTATE_VS 0x78100000
646 #define _3DSTATE_GS 0x78110000
647 #define _3DSTATE_HS 0x781b0000
648 #define _3DSTATE_DS 0x781d0000
650 #define _3DSTATE_CONSTANT_VS 0x78150000
651 #define _3DSTATE_CONSTANT_GS 0x78160000
652 #define _3DSTATE_CONSTANT_PS 0x78170000
653 #define _3DSTATE_CONSTANT_HS 0x78190000
654 #define _3DSTATE_CONSTANT_DS 0x781A0000
656 #define _3DSTATE_PS 0x78200000
658 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
659 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
660 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
661 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
662 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
664 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
665 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
666 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
668 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
669 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
670 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
671 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
672 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
674 #define _MI_LOAD_REGISTER_IMM 0x11000000
676 struct custom_handler
{
678 void (*handle
)(struct gen_spec
*spec
, uint32_t *p
);
679 } custom_handlers
[] = {
680 { STATE_BASE_ADDRESS
, handle_state_base_address
},
681 { MEDIA_INTERFACE_DESCRIPTOR_LOAD
, handle_media_interface_descriptor_load
},
682 { _3DSTATE_VERTEX_BUFFERS
, handle_3dstate_vertex_buffers
},
683 { _3DSTATE_INDEX_BUFFER
, handle_3dstate_index_buffer
},
684 { _3DSTATE_VS
, handle_3dstate_vs
},
685 { _3DSTATE_GS
, handle_3dstate_vs
},
686 { _3DSTATE_DS
, handle_3dstate_vs
},
687 { _3DSTATE_HS
, handle_3dstate_hs
},
688 { _3DSTATE_CONSTANT_VS
, handle_3dstate_constant
},
689 { _3DSTATE_CONSTANT_GS
, handle_3dstate_constant
},
690 { _3DSTATE_CONSTANT_PS
, handle_3dstate_constant
},
691 { _3DSTATE_CONSTANT_HS
, handle_3dstate_constant
},
692 { _3DSTATE_CONSTANT_DS
, handle_3dstate_constant
},
693 { _3DSTATE_PS
, handle_3dstate_ps
},
695 { _3DSTATE_BINDING_TABLE_POINTERS_VS
, handle_3dstate_binding_table_pointers
},
696 { _3DSTATE_BINDING_TABLE_POINTERS_HS
, handle_3dstate_binding_table_pointers
},
697 { _3DSTATE_BINDING_TABLE_POINTERS_DS
, handle_3dstate_binding_table_pointers
},
698 { _3DSTATE_BINDING_TABLE_POINTERS_GS
, handle_3dstate_binding_table_pointers
},
699 { _3DSTATE_BINDING_TABLE_POINTERS_PS
, handle_3dstate_binding_table_pointers
},
701 { _3DSTATE_SAMPLER_STATE_POINTERS_VS
, handle_3dstate_sampler_state_pointers
},
702 { _3DSTATE_SAMPLER_STATE_POINTERS_GS
, handle_3dstate_sampler_state_pointers
},
703 { _3DSTATE_SAMPLER_STATE_POINTERS_PS
, handle_3dstate_sampler_state_pointers
},
705 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC
, handle_3dstate_viewport_state_pointers_cc
},
706 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
, handle_3dstate_viewport_state_pointers_sf_clip
},
707 { _3DSTATE_BLEND_STATE_POINTERS
, handle_3dstate_blend_state_pointers
},
708 { _3DSTATE_CC_STATE_POINTERS
, handle_3dstate_cc_state_pointers
},
709 { _3DSTATE_SCISSOR_STATE_POINTERS
, handle_3dstate_scissor_state_pointers
},
710 { _MI_LOAD_REGISTER_IMM
, handle_load_register_imm
}
714 parse_commands(struct gen_spec
*spec
, uint32_t *cmds
, int size
, int engine
)
716 uint32_t *p
, *end
= cmds
+ size
/ 4;
717 unsigned int length
, i
;
718 struct gen_group
*inst
;
720 for (p
= cmds
; p
< end
; p
+= length
) {
721 inst
= gen_spec_find_instruction(spec
, p
);
723 printf("unknown instruction %08x\n", p
[0]);
724 length
= (p
[0] & 0xff) + 2;
727 length
= gen_group_get_length(inst
, p
);
729 const char *color
, *reset_color
= NORMAL
;
732 if (option_full_decode
) {
733 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
||
734 (p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
)
735 color
= GREEN_HEADER
;
741 if (option_color
== COLOR_NEVER
) {
746 if (option_print_offsets
)
747 offset
= (void *) p
- gtt
;
751 printf("%s0x%08"PRIx64
": 0x%08x: %-80s%s\n",
753 gen_group_get_name(inst
), reset_color
);
755 if (option_full_decode
) {
756 struct gen_field_iterator iter
;
758 int idx
= 0, dword_num
= 0;
759 gen_field_iterator_init(&iter
, inst
, p
,
760 option_color
== COLOR_ALWAYS
);
761 while (gen_field_iterator_next(&iter
)) {
763 print_dword_val(&iter
, offset
, &dword_num
);
765 token
= print_iterator_values(&iter
, &idx
);
767 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
768 offset
+ 4 * idx
, p
[idx
], idx
);
769 handle_struct_decode(spec
,token
, &p
[idx
]);
774 for (i
= 0; i
< ARRAY_LENGTH(custom_handlers
); i
++) {
775 if (gen_group_get_opcode(inst
) ==
776 custom_handlers
[i
].opcode
)
777 custom_handlers
[i
].handle(spec
, p
);
781 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
) {
783 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
784 start
= get_qword(&p
[1]);
788 parse_commands(spec
, gtt
+ start
, 1 << 20, engine
);
789 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
795 #define GEN_ENGINE_RENDER 1
796 #define GEN_ENGINE_BLITTER 2
799 handle_trace_block(struct gen_spec
*spec
, uint32_t *p
)
801 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
802 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
803 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
804 uint64_t offset
= p
[3];
805 uint32_t size
= p
[4];
806 int header_length
= p
[0] & 0xffff;
807 uint32_t *data
= p
+ header_length
+ 2;
808 int engine
= GEN_ENGINE_RENDER
;
810 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
811 offset
+= (uint64_t) p
[5] << 32;
814 case AUB_TRACE_OP_DATA_WRITE
:
815 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
817 if (gtt_size
< offset
+ size
) {
818 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
821 memcpy((char *) gtt
+ offset
, data
, size
);
822 if (gtt_end
< offset
+ size
)
823 gtt_end
= offset
+ size
;
825 case AUB_TRACE_OP_COMMAND_WRITE
:
827 case AUB_TRACE_TYPE_RING_PRB0
:
828 engine
= GEN_ENGINE_RENDER
;
830 case AUB_TRACE_TYPE_RING_PRB2
:
831 engine
= GEN_ENGINE_BLITTER
;
834 printf("command write to unknown ring %d\n", type
);
838 parse_commands(spec
, data
, size
, engine
);
847 uint32_t *map
, *end
, *cursor
;
851 static struct aub_file
*
852 aub_file_open(const char *filename
)
854 struct aub_file
*file
;
858 file
= calloc(1, sizeof *file
);
859 fd
= open(filename
, O_RDONLY
);
861 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
865 if (fstat(fd
, &sb
) == -1) {
866 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
870 file
->map
= mmap(NULL
, sb
.st_size
,
871 PROT_READ
, MAP_SHARED
, fd
, 0);
872 if (file
->map
== MAP_FAILED
) {
873 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
877 file
->cursor
= file
->map
;
878 file
->end
= file
->map
+ sb
.st_size
/ 4;
883 static struct aub_file
*
886 struct aub_file
*file
;
888 file
= calloc(1, sizeof *file
);
889 file
->stream
= stdin
;
894 #define TYPE(dw) (((dw) >> 29) & 7)
895 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
896 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
898 #define MAKE_HEADER(type, opcode, subopcode) \
899 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
903 /* Classic AUB opcodes */
904 #define OPCODE_AUB 0x01
905 #define SUBOPCODE_HEADER 0x05
906 #define SUBOPCODE_BLOCK 0x41
907 #define SUBOPCODE_BMP 0x1e
909 /* Newer version AUB opcode */
910 #define OPCODE_NEW_AUB 0x2e
911 #define SUBOPCODE_VERSION 0x00
912 #define SUBOPCODE_REG_WRITE 0x03
913 #define SUBOPCODE_MEM_POLL 0x05
914 #define SUBOPCODE_MEM_WRITE 0x06
916 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
922 { "bwr", MAKE_GEN(4, 0) },
923 { "cln", MAKE_GEN(4, 0) },
924 { "blc", MAKE_GEN(4, 0) },
925 { "ctg", MAKE_GEN(4, 0) },
926 { "el", MAKE_GEN(4, 0) },
927 { "il", MAKE_GEN(4, 0) },
928 { "sbr", MAKE_GEN(6, 0) },
929 { "ivb", MAKE_GEN(7, 0) },
930 { "lrb2", MAKE_GEN(0, 0) },
931 { "hsw", MAKE_GEN(7, 5) },
932 { "vlv", MAKE_GEN(7, 0) },
933 { "bdw", MAKE_GEN(8, 0) },
934 { "skl", MAKE_GEN(9, 0) },
935 { "chv", MAKE_GEN(8, 0) },
936 { "bxt", MAKE_GEN(9, 0) }
941 AUB_ITEM_DECODE_FAILED
,
942 AUB_ITEM_DECODE_NEED_MORE_DATA
,
946 aub_file_decode_batch(struct aub_file
*file
, struct gen_spec
*spec
)
948 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
949 int header_length
, payload_size
, bias
;
951 if (file
->end
- file
->cursor
< 1)
952 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
956 header_length
= h
& 0xffff;
966 printf("unknown opcode %d at %td/%td\n",
967 OPCODE(h
), file
->cursor
- file
->map
,
968 file
->end
- file
->map
);
969 return AUB_ITEM_DECODE_FAILED
;
973 switch (h
& 0xffff0000) {
974 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
975 if (file
->end
- file
->cursor
< 12)
976 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
977 payload_size
= p
[12];
979 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
980 if (file
->end
- file
->cursor
< 4)
981 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
988 new_cursor
= p
+ header_length
+ bias
+ payload_size
/ 4;
989 if (new_cursor
> file
->end
)
990 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
992 switch (h
& 0xffff0000) {
993 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
995 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
996 handle_trace_block(spec
, p
);
998 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
1000 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
1001 printf("version block: dw1 %08x\n", p
[1]);
1002 device
= (p
[1] >> 8) & 0xff;
1003 printf(" device %s\n", device_map
[device
].name
);
1005 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
1006 printf("register write block: (dwords %d)\n", h
& 0xffff);
1007 printf(" reg 0x%x, data 0x%x\n", p
[1], p
[5]);
1009 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1010 printf("memory write block (dwords %d):\n", h
& 0xffff);
1011 printf(" address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1012 data_type
= (p
[3] >> 20) & 0xff;
1014 printf(" data type 0x%x\n", data_type
);
1015 printf(" address space 0x%x\n", (p
[3] >> 28) & 0xf);
1017 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1018 printf("memory poll block (dwords %d):\n", h
& 0xffff);
1021 printf("unknown block type=0x%x, opcode=0x%x, "
1022 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1025 file
->cursor
= new_cursor
;
1027 return AUB_ITEM_DECODE_OK
;
1031 aub_file_more_stuff(struct aub_file
*file
)
1033 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1036 #define AUB_READ_BUFFER_SIZE (4096)
1037 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1040 aub_file_data_grow(struct aub_file
*file
)
1042 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1043 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1044 uint32_t *new_start
= realloc(file
->map
, new_size
);
1046 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1047 file
->end
= new_start
+ (file
->end
- file
->map
);
1048 file
->map
= new_start
;
1049 file
->mem_end
= file
->map
+ (new_size
/ 4);
1053 aub_file_data_load(struct aub_file
*file
)
1057 if (file
->stream
== NULL
)
1060 /* First remove any consumed data */
1061 if (file
->cursor
> file
->map
) {
1062 memmove(file
->map
, file
->cursor
,
1063 (file
->end
- file
->cursor
) * 4);
1064 file
->end
-= file
->cursor
- file
->map
;
1065 file
->cursor
= file
->map
;
1068 /* Then load some new data in */
1069 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1070 aub_file_data_grow(file
);
1072 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1087 if (pipe(fds
) == -1)
1097 execlp("less", "less", "-FRSi", NULL
);
1106 print_help(const char *progname
, FILE *file
)
1109 "Usage: %s [OPTION]... [FILE]\n"
1110 "Decode aub file contents from either FILE or the standard input.\n\n"
1111 "A valid --gen option must be provided.\n\n"
1112 " --help display this help and exit\n"
1113 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1114 " --headers decode only command headers\n"
1115 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1116 " if omitted), 'always', or 'never'\n"
1117 " --no-pager don't launch pager\n"
1118 " --no-offsets don't print instruction offsets\n"
1119 " --xml=DIR load hardware xml description from directory DIR\n",
1123 int main(int argc
, char *argv
[])
1125 struct gen_spec
*spec
;
1126 struct aub_file
*file
;
1128 bool help
= false, pager
= true;
1129 char *input_file
= NULL
, *xml_path
= NULL
;
1130 char gen_val
[24] = { 0, };
1135 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1136 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1137 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1138 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1139 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1140 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1141 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1142 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1144 const struct option aubinator_opts
[] = {
1145 { "help", no_argument
, (int *) &help
, true },
1146 { "no-pager", no_argument
, (int *) &pager
, false },
1147 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1148 { "gen", required_argument
, NULL
, 'g' },
1149 { "headers", no_argument
, (int *) &option_full_decode
, false },
1150 { "color", required_argument
, NULL
, 'c' },
1151 { "xml", required_argument
, NULL
, 'x' },
1152 { NULL
, 0, NULL
, 0 }
1154 struct gen_device_info devinfo
;
1157 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1160 snprintf(gen_val
, sizeof(gen_val
), "%s", optarg
);
1163 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1164 option_color
= COLOR_ALWAYS
;
1165 else if (strcmp(optarg
, "never") == 0)
1166 option_color
= COLOR_NEVER
;
1167 else if (strcmp(optarg
, "auto") == 0)
1168 option_color
= COLOR_AUTO
;
1170 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1175 xml_path
= strdup(optarg
);
1182 if (help
|| argc
== 1) {
1183 print_help(argv
[0], stderr
);
1188 input_file
= argv
[optind
];
1190 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1191 if (!strcmp(gen_val
, gens
[i
].name
)) {
1198 fprintf(stderr
, "can't parse gen: '%s', expected ivb, byt, hsw, "
1199 "bdw, chv, skl, kbl or bxt\n", gen_val
);
1203 if (!gen_get_device_info(gen
->pci_id
, &devinfo
)) {
1204 fprintf(stderr
, "can't find device information: pci_id=0x%x name=%s\n",
1205 gen
->pci_id
, gen
->name
);
1210 /* Do this before we redirect stdout to pager. */
1211 if (option_color
== COLOR_AUTO
)
1212 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1214 if (isatty(1) && pager
)
1217 if (xml_path
== NULL
)
1218 spec
= gen_spec_load(&devinfo
);
1220 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
1221 disasm
= gen_disasm_create(gen
->pci_id
);
1223 if (spec
== NULL
|| disasm
== NULL
)
1226 if (input_file
== NULL
)
1227 file
= aub_file_stdin();
1229 file
= aub_file_open(input_file
);
1231 /* mmap a terabyte for our gtt space. */
1232 gtt_size
= 1ul << 40;
1233 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1234 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1235 if (gtt
== MAP_FAILED
) {
1236 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1240 while (aub_file_more_stuff(file
)) {
1241 switch (aub_file_decode_batch(file
, spec
)) {
1242 case AUB_ITEM_DECODE_OK
:
1244 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1245 if (!file
->stream
) {
1246 file
->cursor
= file
->end
;
1249 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1250 fprintf(stderr
, "failed to load data from stdin\n");
1255 fprintf(stderr
, "failed to parse aubdump data\n");
1262 /* close the stdout which is opened to write the output */
1268 return EXIT_SUCCESS
;