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 parse_commands(spec
, gtt
+ start
, 1 << 20, engine
);
794 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
800 #define GEN_ENGINE_RENDER 1
801 #define GEN_ENGINE_BLITTER 2
804 handle_trace_block(struct gen_spec
*spec
, uint32_t *p
)
806 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
807 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
808 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
809 uint64_t offset
= p
[3];
810 uint32_t size
= p
[4];
811 int header_length
= p
[0] & 0xffff;
812 uint32_t *data
= p
+ header_length
+ 2;
813 int engine
= GEN_ENGINE_RENDER
;
815 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
816 offset
+= (uint64_t) p
[5] << 32;
819 case AUB_TRACE_OP_DATA_WRITE
:
820 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
822 if (gtt_size
< offset
+ size
) {
823 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
826 memcpy((char *) gtt
+ offset
, data
, size
);
827 if (gtt_end
< offset
+ size
)
828 gtt_end
= offset
+ size
;
830 case AUB_TRACE_OP_COMMAND_WRITE
:
832 case AUB_TRACE_TYPE_RING_PRB0
:
833 engine
= GEN_ENGINE_RENDER
;
835 case AUB_TRACE_TYPE_RING_PRB2
:
836 engine
= GEN_ENGINE_BLITTER
;
839 printf("command write to unknown ring %d\n", type
);
843 parse_commands(spec
, data
, size
, engine
);
852 uint32_t *map
, *end
, *cursor
;
856 static struct aub_file
*
857 aub_file_open(const char *filename
)
859 struct aub_file
*file
;
863 file
= calloc(1, sizeof *file
);
864 fd
= open(filename
, O_RDONLY
);
866 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
870 if (fstat(fd
, &sb
) == -1) {
871 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
875 file
->map
= mmap(NULL
, sb
.st_size
,
876 PROT_READ
, MAP_SHARED
, fd
, 0);
877 if (file
->map
== MAP_FAILED
) {
878 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
882 file
->cursor
= file
->map
;
883 file
->end
= file
->map
+ sb
.st_size
/ 4;
888 static struct aub_file
*
891 struct aub_file
*file
;
893 file
= calloc(1, sizeof *file
);
894 file
->stream
= stdin
;
899 #define TYPE(dw) (((dw) >> 29) & 7)
900 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
901 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
903 #define MAKE_HEADER(type, opcode, subopcode) \
904 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
908 /* Classic AUB opcodes */
909 #define OPCODE_AUB 0x01
910 #define SUBOPCODE_HEADER 0x05
911 #define SUBOPCODE_BLOCK 0x41
912 #define SUBOPCODE_BMP 0x1e
914 /* Newer version AUB opcode */
915 #define OPCODE_NEW_AUB 0x2e
916 #define SUBOPCODE_VERSION 0x00
917 #define SUBOPCODE_REG_WRITE 0x03
918 #define SUBOPCODE_MEM_POLL 0x05
919 #define SUBOPCODE_MEM_WRITE 0x06
921 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
927 { "bwr", MAKE_GEN(4, 0) },
928 { "cln", MAKE_GEN(4, 0) },
929 { "blc", MAKE_GEN(4, 0) },
930 { "ctg", MAKE_GEN(4, 0) },
931 { "el", MAKE_GEN(4, 0) },
932 { "il", MAKE_GEN(4, 0) },
933 { "sbr", MAKE_GEN(6, 0) },
934 { "ivb", MAKE_GEN(7, 0) },
935 { "lrb2", MAKE_GEN(0, 0) },
936 { "hsw", MAKE_GEN(7, 5) },
937 { "vlv", MAKE_GEN(7, 0) },
938 { "bdw", MAKE_GEN(8, 0) },
939 { "skl", MAKE_GEN(9, 0) },
940 { "chv", MAKE_GEN(8, 0) },
941 { "bxt", MAKE_GEN(9, 0) }
946 AUB_ITEM_DECODE_FAILED
,
947 AUB_ITEM_DECODE_NEED_MORE_DATA
,
951 aub_file_decode_batch(struct aub_file
*file
, struct gen_spec
*spec
)
953 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
954 int header_length
, payload_size
, bias
;
956 if (file
->end
- file
->cursor
< 1)
957 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
961 header_length
= h
& 0xffff;
971 printf("unknown opcode %d at %td/%td\n",
972 OPCODE(h
), file
->cursor
- file
->map
,
973 file
->end
- file
->map
);
974 return AUB_ITEM_DECODE_FAILED
;
978 switch (h
& 0xffff0000) {
979 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
980 if (file
->end
- file
->cursor
< 12)
981 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
982 payload_size
= p
[12];
984 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
985 if (file
->end
- file
->cursor
< 4)
986 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
993 new_cursor
= p
+ header_length
+ bias
+ payload_size
/ 4;
994 if (new_cursor
> file
->end
)
995 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
997 switch (h
& 0xffff0000) {
998 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
1000 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
1001 handle_trace_block(spec
, p
);
1003 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
1005 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
1006 printf("version block: dw1 %08x\n", p
[1]);
1007 device
= (p
[1] >> 8) & 0xff;
1008 printf(" device %s\n", device_map
[device
].name
);
1010 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
1011 printf("register write block: (dwords %d)\n", h
& 0xffff);
1012 printf(" reg 0x%x, data 0x%x\n", p
[1], p
[5]);
1014 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1015 printf("memory write block (dwords %d):\n", h
& 0xffff);
1016 printf(" address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1017 data_type
= (p
[3] >> 20) & 0xff;
1019 printf(" data type 0x%x\n", data_type
);
1020 printf(" address space 0x%x\n", (p
[3] >> 28) & 0xf);
1022 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1023 printf("memory poll block (dwords %d):\n", h
& 0xffff);
1026 printf("unknown block type=0x%x, opcode=0x%x, "
1027 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1030 file
->cursor
= new_cursor
;
1032 return AUB_ITEM_DECODE_OK
;
1036 aub_file_more_stuff(struct aub_file
*file
)
1038 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1041 #define AUB_READ_BUFFER_SIZE (4096)
1042 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1045 aub_file_data_grow(struct aub_file
*file
)
1047 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1048 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1049 uint32_t *new_start
= realloc(file
->map
, new_size
);
1051 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1052 file
->end
= new_start
+ (file
->end
- file
->map
);
1053 file
->map
= new_start
;
1054 file
->mem_end
= file
->map
+ (new_size
/ 4);
1058 aub_file_data_load(struct aub_file
*file
)
1062 if (file
->stream
== NULL
)
1065 /* First remove any consumed data */
1066 if (file
->cursor
> file
->map
) {
1067 memmove(file
->map
, file
->cursor
,
1068 (file
->end
- file
->cursor
) * 4);
1069 file
->end
-= file
->cursor
- file
->map
;
1070 file
->cursor
= file
->map
;
1073 /* Then load some new data in */
1074 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1075 aub_file_data_grow(file
);
1077 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1092 if (pipe(fds
) == -1)
1102 execlp("less", "less", "-FRSi", NULL
);
1111 print_help(const char *progname
, FILE *file
)
1114 "Usage: %s [OPTION]... [FILE]\n"
1115 "Decode aub file contents from either FILE or the standard input.\n\n"
1116 "A valid --gen option must be provided.\n\n"
1117 " --help display this help and exit\n"
1118 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1119 " --headers decode only command headers\n"
1120 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1121 " if omitted), 'always', or 'never'\n"
1122 " --no-pager don't launch pager\n"
1123 " --no-offsets don't print instruction offsets\n"
1124 " --xml=DIR load hardware xml description from directory DIR\n",
1128 int main(int argc
, char *argv
[])
1130 struct gen_spec
*spec
;
1131 struct aub_file
*file
;
1133 bool help
= false, pager
= true;
1134 char *input_file
= NULL
, *xml_path
= NULL
;
1135 char gen_val
[24] = { 0, };
1140 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1141 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1142 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1143 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1144 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1145 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1146 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1147 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1149 const struct option aubinator_opts
[] = {
1150 { "help", no_argument
, (int *) &help
, true },
1151 { "no-pager", no_argument
, (int *) &pager
, false },
1152 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1153 { "gen", required_argument
, NULL
, 'g' },
1154 { "headers", no_argument
, (int *) &option_full_decode
, false },
1155 { "color", required_argument
, NULL
, 'c' },
1156 { "xml", required_argument
, NULL
, 'x' },
1157 { NULL
, 0, NULL
, 0 }
1159 struct gen_device_info devinfo
;
1162 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1165 snprintf(gen_val
, sizeof(gen_val
), "%s", optarg
);
1168 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1169 option_color
= COLOR_ALWAYS
;
1170 else if (strcmp(optarg
, "never") == 0)
1171 option_color
= COLOR_NEVER
;
1172 else if (strcmp(optarg
, "auto") == 0)
1173 option_color
= COLOR_AUTO
;
1175 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1180 xml_path
= strdup(optarg
);
1187 if (help
|| argc
== 1) {
1188 print_help(argv
[0], stderr
);
1193 input_file
= argv
[optind
];
1195 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1196 if (!strcmp(gen_val
, gens
[i
].name
)) {
1203 fprintf(stderr
, "can't parse gen: '%s', expected ivb, byt, hsw, "
1204 "bdw, chv, skl, kbl or bxt\n", gen_val
);
1208 if (!gen_get_device_info(gen
->pci_id
, &devinfo
)) {
1209 fprintf(stderr
, "can't find device information: pci_id=0x%x name=%s\n",
1210 gen
->pci_id
, gen
->name
);
1215 /* Do this before we redirect stdout to pager. */
1216 if (option_color
== COLOR_AUTO
)
1217 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1219 if (isatty(1) && pager
)
1222 if (xml_path
== NULL
)
1223 spec
= gen_spec_load(&devinfo
);
1225 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
1226 disasm
= gen_disasm_create(gen
->pci_id
);
1228 if (spec
== NULL
|| disasm
== NULL
)
1231 if (input_file
== NULL
)
1232 file
= aub_file_stdin();
1234 file
= aub_file_open(input_file
);
1236 /* mmap a terabyte for our gtt space. */
1237 gtt_size
= 1ul << 40;
1238 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1239 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1240 if (gtt
== MAP_FAILED
) {
1241 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1245 while (aub_file_more_stuff(file
)) {
1246 switch (aub_file_decode_batch(file
, spec
)) {
1247 case AUB_ITEM_DECODE_OK
:
1249 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1250 if (!file
->stream
) {
1251 file
->cursor
= file
->end
;
1254 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1255 fprintf(stderr
, "failed to load data from stdin\n");
1260 fprintf(stderr
, "failed to parse aubdump data\n");
1267 /* close the stdout which is opened to write the output */
1273 return EXIT_SUCCESS
;