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 while (gen_field_iterator_next(&iter
)) {
145 print_dword_val(&iter
, offset
, &dword_num
);
146 token
= print_iterator_values(&iter
, &idx
);
148 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, token
);
149 decode_structure(spec
, struct_val
, &p
[idx
]);
156 handle_struct_decode(struct gen_spec
*spec
, char *struct_name
, uint32_t *p
)
158 if (struct_name
== NULL
)
160 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, struct_name
);
161 decode_structure(spec
, struct_val
, p
);
165 dump_binding_table(struct gen_spec
*spec
, uint32_t offset
)
167 uint32_t *pointers
, i
;
169 struct gen_group
*surface_state
;
171 surface_state
= gen_spec_find_struct(spec
, "RENDER_SURFACE_STATE");
172 if (surface_state
== NULL
) {
173 printf("did not find RENDER_SURFACE_STATE info\n");
177 start
= surface_state_base
+ offset
;
178 pointers
= gtt
+ start
;
179 for (i
= 0; i
< 16; i
++) {
180 if (pointers
[i
] == 0)
182 start
= pointers
[i
] + surface_state_base
;
183 if (!valid_offset(start
)) {
184 printf("pointer %u: %08x <not valid>\n",
188 printf("pointer %u: %08x\n", i
, pointers
[i
]);
191 decode_structure(spec
, surface_state
, gtt
+ start
);
196 handle_3dstate_index_buffer(struct gen_spec
*spec
, uint32_t *p
)
199 uint32_t length
, i
, type
, size
;
202 type
= (p
[1] >> 8) & 3;
204 length
= p
[4] / size
;
210 for (i
= 0; i
< length
; i
++) {
213 printf("%3d ", ((uint8_t *)start
)[i
]);
216 printf("%3d ", ((uint16_t *)start
)[i
]);
219 printf("%3d ", ((uint32_t *)start
)[i
]);
223 if (length
< p
[4] / size
)
229 static inline uint64_t
230 get_qword(uint32_t *p
)
232 return ((uint64_t) p
[1] << 32) | p
[0];
236 handle_state_base_address(struct gen_spec
*spec
, uint32_t *p
)
238 uint64_t mask
= ~((1 << 12) - 1);
240 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
242 general_state_base
= get_qword(&p
[1]) & mask
;
244 surface_state_base
= get_qword(&p
[4]) & mask
;
246 dynamic_state_base
= get_qword(&p
[6]) & mask
;
248 instruction_base
= get_qword(&p
[10]) & mask
;
250 instruction_bound
= p
[15] & mask
;
253 surface_state_base
= p
[2] & mask
;
255 dynamic_state_base
= p
[3] & mask
;
257 instruction_base
= p
[5] & mask
;
259 instruction_bound
= p
[9] & mask
;
264 dump_samplers(struct gen_spec
*spec
, uint32_t offset
)
268 struct gen_group
*sampler_state
;
270 sampler_state
= gen_spec_find_struct(spec
, "SAMPLER_STATE");
272 start
= dynamic_state_base
+ offset
;
273 for (i
= 0; i
< 4; i
++) {
274 printf("sampler state %d\n", i
);
275 decode_structure(spec
, sampler_state
, gtt
+ start
+ i
* 16);
280 handle_media_interface_descriptor_load(struct gen_spec
*spec
, uint32_t *p
)
282 int i
, length
= p
[2] / 32;
283 struct gen_group
*descriptor_structure
;
284 uint32_t *descriptors
;
286 struct brw_instruction
*insns
;
288 descriptor_structure
=
289 gen_spec_find_struct(spec
, "INTERFACE_DESCRIPTOR_DATA");
290 if (descriptor_structure
== NULL
) {
291 printf("did not find INTERFACE_DESCRIPTOR_DATA info\n");
295 start
= dynamic_state_base
+ p
[3];
296 descriptors
= gtt
+ start
;
297 for (i
= 0; i
< length
; i
++, descriptors
+= 8) {
298 printf("descriptor %u: %08x\n", i
, *descriptors
);
299 decode_structure(spec
, descriptor_structure
, descriptors
);
301 start
= instruction_base
+ descriptors
[0];
302 if (!valid_offset(start
)) {
303 printf("kernel: %08"PRIx64
" <not valid>\n", start
);
306 printf("kernel: %08"PRIx64
"\n", start
);
309 insns
= (struct brw_instruction
*) (gtt
+ start
);
310 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
312 dump_samplers(spec
, descriptors
[3] & ~0x1f);
313 dump_binding_table(spec
, descriptors
[4] & ~0x1f);
317 /* Heuristic to determine whether a uint32_t is probably actually a float
318 * (http://stackoverflow.com/a/2953466)
322 probably_float(uint32_t bits
)
324 int exp
= ((bits
& 0x7f800000U
) >> 23) - 127;
325 uint32_t mant
= bits
& 0x007fffff;
328 if (exp
== -127 && mant
== 0)
331 /* +- 1 billionth to 1 billion */
332 if (-30 <= exp
&& exp
<= 30)
335 /* some value with only a few binary digits */
336 if ((mant
& 0x0000ffff) == 0)
343 handle_3dstate_vertex_buffers(struct gen_spec
*spec
, uint32_t *p
)
345 uint32_t *end
, *s
, *dw
, *dwend
;
347 int n
, i
, count
, stride
;
349 end
= (p
[0] & 0xff) + p
+ 2;
350 for (s
= &p
[1], n
= 0; s
< end
; s
+= 4, n
++) {
351 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
352 offset
= *(uint64_t *) &s
[1];
353 dwend
= gtt
+ offset
+ s
[3];
356 dwend
= gtt
+ s
[2] + 1;
359 stride
= field(s
[0], 0, 11);
361 printf("vertex buffer %d, size %d\n", n
, s
[3]);
362 for (dw
= gtt
+ offset
, i
= 0; dw
< dwend
&& i
< 256; dw
++) {
363 if (count
== 0 && count
% (8 * 4) == 0)
366 if (probably_float(*dw
))
367 printf(" %8.2f", *(float *) dw
);
369 printf(" 0x%08x", *dw
);
374 if (count
== stride
) {
377 } else if (count
% (8 * 4) == 0) {
383 if (count
> 0 && count
% (8 * 4) != 0)
389 handle_3dstate_vs(struct gen_spec
*spec
, uint32_t *p
)
392 struct brw_instruction
*insns
;
395 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
396 start
= get_qword(&p
[1]);
397 vs_enable
= p
[7] & 1;
400 vs_enable
= p
[5] & 1;
404 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
405 instruction_base
, start
);
407 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
408 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
413 handle_3dstate_hs(struct gen_spec
*spec
, uint32_t *p
)
416 struct brw_instruction
*insns
;
419 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
420 start
= get_qword(&p
[4]);
425 hs_enable
= p
[2] & 0x80000000;
428 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
429 instruction_base
, start
);
431 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
432 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
437 handle_3dstate_constant(struct gen_spec
*spec
, uint32_t *p
)
443 for (i
= 0; i
< 4; i
++) {
444 length
= (p
[1 + i
/ 2] >> (i
& 1) * 16) & 0xffff;
445 f
= (float *) (gtt
+ p
[3 + i
* 2] + dynamic_state_base
);
447 for (j
= 0; j
< length
* 8; j
++) {
448 if (probably_float(dw
[j
]))
449 printf(" %04.3f", f
[j
]);
451 printf(" 0x%08x", dw
[j
]);
460 handle_3dstate_ps(struct gen_spec
*spec
, uint32_t *p
)
462 uint32_t mask
= ~((1 << 6) - 1);
464 struct brw_instruction
*insns
;
465 static const char unused
[] = "unused";
466 static const char *pixel_type
[3] = {"8 pixel", "16 pixel", "32 pixel"};
467 const char *k0
, *k1
, *k2
;
468 uint32_t k_mask
, k1_offset
, k2_offset
;
470 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
481 #define DISPATCH_16 2
482 #define DISPATCH_32 4
495 case DISPATCH_8
| DISPATCH_16
:
505 case DISPATCH_16
| DISPATCH_32
:
510 case DISPATCH_8
| DISPATCH_16
| DISPATCH_32
:
522 start
= instruction_base
+ (p
[1] & mask
);
523 printf(" Kernel[0] %s\n", k0
);
525 insns
= (struct brw_instruction
*) (gtt
+ start
);
526 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
529 start
= instruction_base
+ (p
[k1_offset
] & mask
);
530 printf(" Kernel[1] %s\n", k1
);
532 insns
= (struct brw_instruction
*) (gtt
+ start
);
533 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
536 start
= instruction_base
+ (p
[k2_offset
] & mask
);
537 printf(" Kernel[2] %s\n", k2
);
539 insns
= (struct brw_instruction
*) (gtt
+ start
);
540 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
545 handle_3dstate_binding_table_pointers(struct gen_spec
*spec
, uint32_t *p
)
547 dump_binding_table(spec
, p
[1]);
551 handle_3dstate_sampler_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
553 dump_samplers(spec
, p
[1]);
557 handle_3dstate_viewport_state_pointers_cc(struct gen_spec
*spec
, uint32_t *p
)
560 struct gen_group
*cc_viewport
;
562 cc_viewport
= gen_spec_find_struct(spec
, "CC_VIEWPORT");
564 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
565 for (uint32_t i
= 0; i
< 4; i
++) {
566 printf("viewport %d\n", i
);
567 decode_structure(spec
, cc_viewport
, gtt
+ start
+ i
* 8);
572 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec
*spec
,
576 struct gen_group
*sf_clip_viewport
;
578 sf_clip_viewport
= gen_spec_find_struct(spec
, "SF_CLIP_VIEWPORT");
580 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
581 for (uint32_t i
= 0; i
< 4; i
++) {
582 printf("viewport %d\n", i
);
583 decode_structure(spec
, sf_clip_viewport
, gtt
+ start
+ i
* 64);
588 handle_3dstate_blend_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
591 struct gen_group
*blend_state
;
593 blend_state
= gen_spec_find_struct(spec
, "BLEND_STATE");
595 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
596 decode_structure(spec
, blend_state
, gtt
+ start
);
600 handle_3dstate_cc_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
603 struct gen_group
*cc_state
;
605 cc_state
= gen_spec_find_struct(spec
, "COLOR_CALC_STATE");
607 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
608 decode_structure(spec
, cc_state
, gtt
+ start
);
612 handle_3dstate_scissor_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
615 struct gen_group
*scissor_rect
;
617 scissor_rect
= gen_spec_find_struct(spec
, "SCISSOR_RECT");
619 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
620 decode_structure(spec
, scissor_rect
, gtt
+ start
);
624 handle_load_register_imm(struct gen_spec
*spec
, uint32_t *p
)
626 struct gen_group
*reg
= gen_spec_find_register(spec
, p
[1]);
629 printf("register %s (0x%x): 0x%x\n",
630 reg
->name
, reg
->register_offset
, p
[2]);
631 decode_structure(spec
, reg
, &p
[2]);
635 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
637 #define STATE_BASE_ADDRESS 0x61010000
639 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
641 #define _3DSTATE_INDEX_BUFFER 0x780a0000
642 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
644 #define _3DSTATE_VS 0x78100000
645 #define _3DSTATE_GS 0x78110000
646 #define _3DSTATE_HS 0x781b0000
647 #define _3DSTATE_DS 0x781d0000
649 #define _3DSTATE_CONSTANT_VS 0x78150000
650 #define _3DSTATE_CONSTANT_GS 0x78160000
651 #define _3DSTATE_CONSTANT_PS 0x78170000
652 #define _3DSTATE_CONSTANT_HS 0x78190000
653 #define _3DSTATE_CONSTANT_DS 0x781A0000
655 #define _3DSTATE_PS 0x78200000
657 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
658 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
659 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
660 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
661 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
663 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
664 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
665 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
667 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
668 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
669 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
670 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
671 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
673 #define _MI_LOAD_REGISTER_IMM 0x11000000
675 struct custom_handler
{
677 void (*handle
)(struct gen_spec
*spec
, uint32_t *p
);
678 } custom_handlers
[] = {
679 { STATE_BASE_ADDRESS
, handle_state_base_address
},
680 { MEDIA_INTERFACE_DESCRIPTOR_LOAD
, handle_media_interface_descriptor_load
},
681 { _3DSTATE_VERTEX_BUFFERS
, handle_3dstate_vertex_buffers
},
682 { _3DSTATE_INDEX_BUFFER
, handle_3dstate_index_buffer
},
683 { _3DSTATE_VS
, handle_3dstate_vs
},
684 { _3DSTATE_GS
, handle_3dstate_vs
},
685 { _3DSTATE_DS
, handle_3dstate_vs
},
686 { _3DSTATE_HS
, handle_3dstate_hs
},
687 { _3DSTATE_CONSTANT_VS
, handle_3dstate_constant
},
688 { _3DSTATE_CONSTANT_GS
, handle_3dstate_constant
},
689 { _3DSTATE_CONSTANT_PS
, handle_3dstate_constant
},
690 { _3DSTATE_CONSTANT_HS
, handle_3dstate_constant
},
691 { _3DSTATE_CONSTANT_DS
, handle_3dstate_constant
},
692 { _3DSTATE_PS
, handle_3dstate_ps
},
694 { _3DSTATE_BINDING_TABLE_POINTERS_VS
, handle_3dstate_binding_table_pointers
},
695 { _3DSTATE_BINDING_TABLE_POINTERS_HS
, handle_3dstate_binding_table_pointers
},
696 { _3DSTATE_BINDING_TABLE_POINTERS_DS
, handle_3dstate_binding_table_pointers
},
697 { _3DSTATE_BINDING_TABLE_POINTERS_GS
, handle_3dstate_binding_table_pointers
},
698 { _3DSTATE_BINDING_TABLE_POINTERS_PS
, handle_3dstate_binding_table_pointers
},
700 { _3DSTATE_SAMPLER_STATE_POINTERS_VS
, handle_3dstate_sampler_state_pointers
},
701 { _3DSTATE_SAMPLER_STATE_POINTERS_GS
, handle_3dstate_sampler_state_pointers
},
702 { _3DSTATE_SAMPLER_STATE_POINTERS_PS
, handle_3dstate_sampler_state_pointers
},
704 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC
, handle_3dstate_viewport_state_pointers_cc
},
705 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
, handle_3dstate_viewport_state_pointers_sf_clip
},
706 { _3DSTATE_BLEND_STATE_POINTERS
, handle_3dstate_blend_state_pointers
},
707 { _3DSTATE_CC_STATE_POINTERS
, handle_3dstate_cc_state_pointers
},
708 { _3DSTATE_SCISSOR_STATE_POINTERS
, handle_3dstate_scissor_state_pointers
},
709 { _MI_LOAD_REGISTER_IMM
, handle_load_register_imm
}
713 parse_commands(struct gen_spec
*spec
, uint32_t *cmds
, int size
, int engine
)
715 uint32_t *p
, *end
= cmds
+ size
/ 4;
716 unsigned int length
, i
;
717 struct gen_group
*inst
;
719 for (p
= cmds
; p
< end
; p
+= length
) {
720 inst
= gen_spec_find_instruction(spec
, p
);
722 printf("unknown instruction %08x\n", p
[0]);
723 length
= (p
[0] & 0xff) + 2;
726 length
= gen_group_get_length(inst
, p
);
728 const char *color
, *reset_color
= NORMAL
;
731 if (option_full_decode
) {
732 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
||
733 (p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
)
734 color
= GREEN_HEADER
;
740 if (option_color
== COLOR_NEVER
) {
745 if (option_print_offsets
)
746 offset
= (void *) p
- gtt
;
750 printf("%s0x%08"PRIx64
": 0x%08x: %-80s%s\n",
752 gen_group_get_name(inst
), reset_color
);
754 if (option_full_decode
) {
755 struct gen_field_iterator iter
;
757 int idx
= 0, dword_num
= 0;
758 gen_field_iterator_init(&iter
, inst
, p
);
759 while (gen_field_iterator_next(&iter
)) {
761 print_dword_val(&iter
, offset
, &dword_num
);
763 token
= print_iterator_values(&iter
, &idx
);
765 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
766 offset
+ 4 * idx
, p
[idx
], idx
);
767 handle_struct_decode(spec
,token
, &p
[idx
]);
772 for (i
= 0; i
< ARRAY_LENGTH(custom_handlers
); i
++) {
773 if (gen_group_get_opcode(inst
) ==
774 custom_handlers
[i
].opcode
)
775 custom_handlers
[i
].handle(spec
, p
);
779 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
) {
781 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
782 start
= get_qword(&p
[1]);
786 parse_commands(spec
, gtt
+ start
, 1 << 20, engine
);
787 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
793 #define GEN_ENGINE_RENDER 1
794 #define GEN_ENGINE_BLITTER 2
797 handle_trace_block(struct gen_spec
*spec
, uint32_t *p
)
799 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
800 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
801 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
802 uint64_t offset
= p
[3];
803 uint32_t size
= p
[4];
804 int header_length
= p
[0] & 0xffff;
805 uint32_t *data
= p
+ header_length
+ 2;
806 int engine
= GEN_ENGINE_RENDER
;
808 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
809 offset
+= (uint64_t) p
[5] << 32;
812 case AUB_TRACE_OP_DATA_WRITE
:
813 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
815 if (gtt_size
< offset
+ size
) {
816 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
819 memcpy((char *) gtt
+ offset
, data
, size
);
820 if (gtt_end
< offset
+ size
)
821 gtt_end
= offset
+ size
;
823 case AUB_TRACE_OP_COMMAND_WRITE
:
825 case AUB_TRACE_TYPE_RING_PRB0
:
826 engine
= GEN_ENGINE_RENDER
;
828 case AUB_TRACE_TYPE_RING_PRB2
:
829 engine
= GEN_ENGINE_BLITTER
;
832 printf("command write to unknown ring %d\n", type
);
836 parse_commands(spec
, data
, size
, engine
);
845 uint32_t *map
, *end
, *cursor
;
849 static struct aub_file
*
850 aub_file_open(const char *filename
)
852 struct aub_file
*file
;
856 file
= calloc(1, sizeof *file
);
857 fd
= open(filename
, O_RDONLY
);
859 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
863 if (fstat(fd
, &sb
) == -1) {
864 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
868 file
->map
= mmap(NULL
, sb
.st_size
,
869 PROT_READ
, MAP_SHARED
, fd
, 0);
870 if (file
->map
== MAP_FAILED
) {
871 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
875 file
->cursor
= file
->map
;
876 file
->end
= file
->map
+ sb
.st_size
/ 4;
881 static struct aub_file
*
884 struct aub_file
*file
;
886 file
= calloc(1, sizeof *file
);
887 file
->stream
= stdin
;
892 #define TYPE(dw) (((dw) >> 29) & 7)
893 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
894 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
896 #define MAKE_HEADER(type, opcode, subopcode) \
897 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
901 /* Classic AUB opcodes */
902 #define OPCODE_AUB 0x01
903 #define SUBOPCODE_HEADER 0x05
904 #define SUBOPCODE_BLOCK 0x41
905 #define SUBOPCODE_BMP 0x1e
907 /* Newer version AUB opcode */
908 #define OPCODE_NEW_AUB 0x2e
909 #define SUBOPCODE_VERSION 0x00
910 #define SUBOPCODE_REG_WRITE 0x03
911 #define SUBOPCODE_MEM_POLL 0x05
912 #define SUBOPCODE_MEM_WRITE 0x06
914 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
920 { "bwr", MAKE_GEN(4, 0) },
921 { "cln", MAKE_GEN(4, 0) },
922 { "blc", MAKE_GEN(4, 0) },
923 { "ctg", MAKE_GEN(4, 0) },
924 { "el", MAKE_GEN(4, 0) },
925 { "il", MAKE_GEN(4, 0) },
926 { "sbr", MAKE_GEN(6, 0) },
927 { "ivb", MAKE_GEN(7, 0) },
928 { "lrb2", MAKE_GEN(0, 0) },
929 { "hsw", MAKE_GEN(7, 5) },
930 { "vlv", MAKE_GEN(7, 0) },
931 { "bdw", MAKE_GEN(8, 0) },
932 { "skl", MAKE_GEN(9, 0) },
933 { "chv", MAKE_GEN(8, 0) },
934 { "bxt", MAKE_GEN(9, 0) }
939 AUB_ITEM_DECODE_FAILED
,
940 AUB_ITEM_DECODE_NEED_MORE_DATA
,
944 aub_file_decode_batch(struct aub_file
*file
, struct gen_spec
*spec
)
946 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
947 int header_length
, payload_size
, bias
;
949 if (file
->end
- file
->cursor
< 12)
950 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
954 header_length
= h
& 0xffff;
964 printf("unknown opcode %d at %td/%td\n",
965 OPCODE(h
), file
->cursor
- file
->map
,
966 file
->end
- file
->map
);
967 return AUB_ITEM_DECODE_FAILED
;
971 switch (h
& 0xffff0000) {
972 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
973 payload_size
= p
[12];
975 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
982 new_cursor
= p
+ header_length
+ bias
+ payload_size
/ 4;
983 if (new_cursor
> file
->end
)
984 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
986 switch (h
& 0xffff0000) {
987 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
989 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
990 handle_trace_block(spec
, p
);
992 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
994 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
995 printf("version block: dw1 %08x\n", p
[1]);
996 device
= (p
[1] >> 8) & 0xff;
997 printf(" device %s\n", device_map
[device
].name
);
999 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
1000 printf("register write block: (dwords %d)\n", h
& 0xffff);
1001 printf(" reg 0x%x, data 0x%x\n", p
[1], p
[5]);
1003 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1004 printf("memory write block (dwords %d):\n", h
& 0xffff);
1005 printf(" address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1006 data_type
= (p
[3] >> 20) & 0xff;
1008 printf(" data type 0x%x\n", data_type
);
1009 printf(" address space 0x%x\n", (p
[3] >> 28) & 0xf);
1011 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1012 printf("memory poll block (dwords %d):\n", h
& 0xffff);
1015 printf("unknown block type=0x%x, opcode=0x%x, "
1016 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1019 file
->cursor
= new_cursor
;
1021 return AUB_ITEM_DECODE_OK
;
1025 aub_file_more_stuff(struct aub_file
*file
)
1027 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1030 #define AUB_READ_BUFFER_SIZE (4096)
1031 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1034 aub_file_data_grow(struct aub_file
*file
)
1036 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1037 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1038 uint32_t *new_start
= realloc(file
->map
, new_size
);
1040 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1041 file
->end
= new_start
+ (file
->end
- file
->map
);
1042 file
->map
= new_start
;
1043 file
->mem_end
= file
->map
+ (new_size
/ 4);
1047 aub_file_data_load(struct aub_file
*file
)
1051 if (file
->stream
== NULL
)
1054 /* First remove any consumed data */
1055 if (file
->cursor
> file
->map
) {
1056 memmove(file
->map
, file
->cursor
,
1057 (file
->end
- file
->cursor
) * 4);
1058 file
->end
-= file
->cursor
- file
->map
;
1059 file
->cursor
= file
->map
;
1062 /* Then load some new data in */
1063 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1064 aub_file_data_grow(file
);
1066 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1081 if (pipe(fds
) == -1)
1091 execlp("less", "less", "-FRSi", NULL
);
1100 print_help(const char *progname
, FILE *file
)
1103 "Usage: %s [OPTION]... [FILE]\n"
1104 "Decode aub file contents from either FILE or the standard input.\n\n"
1105 "A valid --gen option must be provided.\n\n"
1106 " --help display this help and exit\n"
1107 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1108 " --headers decode only command headers\n"
1109 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1110 " if omitted), 'always', or 'never'\n"
1111 " --no-pager don't launch pager\n"
1112 " --no-offsets don't print instruction offsets\n"
1113 " --xml=DIR load hardware xml description from directory DIR\n",
1117 int main(int argc
, char *argv
[])
1119 struct gen_spec
*spec
;
1120 struct aub_file
*file
;
1122 bool help
= false, pager
= true;
1123 char *input_file
= NULL
, *xml_path
= NULL
;
1129 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1130 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1131 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1132 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1133 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1134 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1135 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1136 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1138 const struct option aubinator_opts
[] = {
1139 { "help", no_argument
, (int *) &help
, true },
1140 { "no-pager", no_argument
, (int *) &pager
, false },
1141 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1142 { "gen", required_argument
, NULL
, 'g' },
1143 { "headers", no_argument
, (int *) &option_full_decode
, false },
1144 { "color", required_argument
, NULL
, 'c' },
1145 { "xml", required_argument
, NULL
, 'x' },
1146 { NULL
, 0, NULL
, 0 }
1148 struct gen_device_info devinfo
;
1151 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1154 snprintf(gen_val
, sizeof(gen_val
), "%s", optarg
);
1157 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1158 option_color
= COLOR_ALWAYS
;
1159 else if (strcmp(optarg
, "never") == 0)
1160 option_color
= COLOR_NEVER
;
1161 else if (strcmp(optarg
, "auto") == 0)
1162 option_color
= COLOR_AUTO
;
1164 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1169 xml_path
= strdup(optarg
);
1176 if (help
|| argc
== 1) {
1177 print_help(argv
[0], stderr
);
1182 input_file
= argv
[optind
];
1184 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1185 if (!strcmp(gen_val
, gens
[i
].name
)) {
1192 fprintf(stderr
, "can't parse gen: %s, expected ivb, byt, hsw, "
1193 "bdw, chv, skl, kbl or bxt\n", gen_val
);
1197 if (!gen_get_device_info(gen
->pci_id
, &devinfo
)) {
1198 fprintf(stderr
, "can't find device information: pci_id=0x%x name=%s\n",
1199 gen
->pci_id
, gen
->name
);
1204 /* Do this before we redirect stdout to pager. */
1205 if (option_color
== COLOR_AUTO
)
1206 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1208 if (isatty(1) && pager
)
1211 if (xml_path
== NULL
)
1212 spec
= gen_spec_load(&devinfo
);
1214 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
1215 disasm
= gen_disasm_create(gen
->pci_id
);
1217 if (spec
== NULL
|| disasm
== NULL
)
1220 if (input_file
== NULL
)
1221 file
= aub_file_stdin();
1223 file
= aub_file_open(input_file
);
1225 /* mmap a terabyte for our gtt space. */
1226 gtt_size
= 1ul << 40;
1227 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1228 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1229 if (gtt
== MAP_FAILED
) {
1230 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1234 while (aub_file_more_stuff(file
)) {
1235 switch (aub_file_decode_batch(file
, spec
)) {
1236 case AUB_ITEM_DECODE_OK
:
1238 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1239 if (!file
->stream
) {
1240 file
->cursor
= file
->end
;
1243 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1244 fprintf(stderr
, "failed to load data from stdin\n");
1249 fprintf(stderr
, "failed to parse aubdump data\n");
1256 /* close the stdout which is opened to write the output */
1262 return EXIT_SUCCESS
;