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 HEADER CSI "37;44m"
54 #define NORMAL CSI "0m"
58 static bool option_full_decode
= true;
59 static bool option_print_offsets
= true;
60 static enum { COLOR_AUTO
, COLOR_ALWAYS
, COLOR_NEVER
} option_color
;
64 struct gen_disasm
*disasm
;
66 uint64_t gtt_size
, gtt_end
;
68 uint64_t general_state_base
;
69 uint64_t surface_state_base
;
70 uint64_t dynamic_state_base
;
71 uint64_t instruction_base
;
72 uint64_t instruction_bound
;
74 static inline uint32_t
75 field(uint32_t value
, int start
, int end
)
79 mask
= ~0U >> (31 - end
+ start
);
81 return (value
>> start
) & mask
;
84 struct brw_instruction
;
87 valid_offset(uint32_t offset
)
89 return offset
< gtt_end
;
93 print_dword_val(struct gen_field_iterator
*iter
, uint64_t offset
,
98 f
= iter
->group
->fields
[iter
->i
- 1];
99 const int dword
= f
->start
/ 32;
101 if (*dword_num
!= dword
) {
102 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
103 offset
+ 4 * dword
, iter
->p
[dword
], dword
);
109 print_iterator_values(struct gen_field_iterator
*iter
, int *idx
)
112 if (strstr(iter
->value
, "struct") == NULL
) {
113 printf(" %s: %s\n", iter
->name
, iter
->value
);
115 token
= strtok(iter
->value
, " ");
117 token
= strtok(NULL
, " ");
118 *idx
= atoi(strtok(NULL
, ">"));
122 printf(" %s:<struct %s>\n", iter
->name
, token
);
128 decode_structure(struct gen_spec
*spec
, struct gen_group
*strct
,
131 struct gen_field_iterator iter
;
133 int idx
= 0, dword_num
= 0;
136 if (option_print_offsets
)
137 offset
= (void *) p
- gtt
;
141 gen_field_iterator_init(&iter
, strct
, p
);
142 while (gen_field_iterator_next(&iter
)) {
144 print_dword_val(&iter
, offset
, &dword_num
);
145 token
= print_iterator_values(&iter
, &idx
);
147 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, token
);
148 decode_structure(spec
, struct_val
, &p
[idx
]);
155 handle_struct_decode(struct gen_spec
*spec
, char *struct_name
, uint32_t *p
)
157 if (struct_name
== NULL
)
159 struct gen_group
*struct_val
= gen_spec_find_struct(spec
, struct_name
);
160 decode_structure(spec
, struct_val
, p
);
164 dump_binding_table(struct gen_spec
*spec
, uint32_t offset
)
166 uint32_t *pointers
, i
;
168 struct gen_group
*surface_state
;
170 surface_state
= gen_spec_find_struct(spec
, "RENDER_SURFACE_STATE");
171 if (surface_state
== NULL
) {
172 printf("did not find RENDER_SURFACE_STATE info\n");
176 start
= surface_state_base
+ offset
;
177 pointers
= gtt
+ start
;
178 for (i
= 0; i
< 16; i
++) {
179 if (pointers
[i
] == 0)
181 start
= pointers
[i
] + surface_state_base
;
182 if (!valid_offset(start
)) {
183 printf("pointer %u: %08x <not valid>\n",
187 printf("pointer %u: %08x\n", i
, pointers
[i
]);
190 decode_structure(spec
, surface_state
, gtt
+ start
);
195 handle_3dstate_index_buffer(struct gen_spec
*spec
, uint32_t *p
)
198 uint32_t length
, i
, type
, size
;
201 type
= (p
[1] >> 8) & 3;
203 length
= p
[4] / size
;
209 for (i
= 0; i
< length
; i
++) {
212 printf("%3d ", ((uint8_t *)start
)[i
]);
215 printf("%3d ", ((uint16_t *)start
)[i
]);
218 printf("%3d ", ((uint32_t *)start
)[i
]);
222 if (length
< p
[4] / size
)
228 static inline uint64_t
229 get_qword(uint32_t *p
)
231 return ((uint64_t) p
[1] << 32) | p
[0];
235 handle_state_base_address(struct gen_spec
*spec
, uint32_t *p
)
237 uint64_t mask
= ~((1 << 12) - 1);
239 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0)) {
241 general_state_base
= get_qword(&p
[1]) & mask
;
243 surface_state_base
= get_qword(&p
[4]) & mask
;
245 dynamic_state_base
= get_qword(&p
[6]) & mask
;
247 instruction_base
= get_qword(&p
[10]) & mask
;
249 instruction_bound
= p
[15] & mask
;
252 surface_state_base
= p
[2] & mask
;
254 dynamic_state_base
= p
[3] & mask
;
256 instruction_base
= p
[5] & mask
;
258 instruction_bound
= p
[9] & mask
;
263 dump_samplers(struct gen_spec
*spec
, uint32_t offset
)
267 struct gen_group
*sampler_state
;
269 sampler_state
= gen_spec_find_struct(spec
, "SAMPLER_STATE");
271 start
= dynamic_state_base
+ offset
;
272 for (i
= 0; i
< 4; i
++) {
273 printf("sampler state %d\n", i
);
274 decode_structure(spec
, sampler_state
, gtt
+ start
+ i
* 16);
279 handle_media_interface_descriptor_load(struct gen_spec
*spec
, uint32_t *p
)
281 int i
, length
= p
[2] / 32;
282 struct gen_group
*descriptor_structure
;
283 uint32_t *descriptors
;
285 struct brw_instruction
*insns
;
287 descriptor_structure
=
288 gen_spec_find_struct(spec
, "INTERFACE_DESCRIPTOR_DATA");
289 if (descriptor_structure
== NULL
) {
290 printf("did not find INTERFACE_DESCRIPTOR_DATA info\n");
294 start
= dynamic_state_base
+ p
[3];
295 descriptors
= gtt
+ start
;
296 for (i
= 0; i
< length
; i
++, descriptors
+= 8) {
297 printf("descriptor %u: %08x\n", i
, *descriptors
);
298 decode_structure(spec
, descriptor_structure
, descriptors
);
300 start
= instruction_base
+ descriptors
[0];
301 if (!valid_offset(start
)) {
302 printf("kernel: %08"PRIx64
" <not valid>\n", start
);
305 printf("kernel: %08"PRIx64
"\n", start
);
308 insns
= (struct brw_instruction
*) (gtt
+ start
);
309 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
311 dump_samplers(spec
, descriptors
[3] & ~0x1f);
312 dump_binding_table(spec
, descriptors
[4] & ~0x1f);
316 /* Heuristic to determine whether a uint32_t is probably actually a float
317 * (http://stackoverflow.com/a/2953466)
321 probably_float(uint32_t bits
)
323 int exp
= ((bits
& 0x7f800000U
) >> 23) - 127;
324 uint32_t mant
= bits
& 0x007fffff;
327 if (exp
== -127 && mant
== 0)
330 /* +- 1 billionth to 1 billion */
331 if (-30 <= exp
&& exp
<= 30)
334 /* some value with only a few binary digits */
335 if ((mant
& 0x0000ffff) == 0)
342 handle_3dstate_vertex_buffers(struct gen_spec
*spec
, uint32_t *p
)
344 uint32_t *end
, *s
, *dw
, *dwend
;
346 int n
, i
, count
, stride
;
348 end
= (p
[0] & 0xff) + p
+ 2;
349 for (s
= &p
[1], n
= 0; s
< end
; s
+= 4, n
++) {
350 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
351 offset
= *(uint64_t *) &s
[1];
352 dwend
= gtt
+ offset
+ s
[3];
355 dwend
= gtt
+ s
[2] + 1;
358 stride
= field(s
[0], 0, 11);
360 printf("vertex buffer %d, size %d\n", n
, s
[3]);
361 for (dw
= gtt
+ offset
, i
= 0; dw
< dwend
&& i
< 256; dw
++) {
362 if (count
== 0 && count
% (8 * 4) == 0)
365 if (probably_float(*dw
))
366 printf(" %8.2f", *(float *) dw
);
368 printf(" 0x%08x", *dw
);
373 if (count
== stride
) {
376 } else if (count
% (8 * 4) == 0) {
382 if (count
> 0 && count
% (8 * 4) != 0)
388 handle_3dstate_vs(struct gen_spec
*spec
, uint32_t *p
)
391 struct brw_instruction
*insns
;
394 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
395 start
= get_qword(&p
[1]);
396 vs_enable
= p
[7] & 1;
399 vs_enable
= p
[5] & 1;
403 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
404 instruction_base
, start
);
406 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
407 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
412 handle_3dstate_hs(struct gen_spec
*spec
, uint32_t *p
)
415 struct brw_instruction
*insns
;
418 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
419 start
= get_qword(&p
[4]);
424 hs_enable
= p
[2] & 0x80000000;
427 printf("instruction_base %08"PRIx64
", start %08"PRIx64
"\n",
428 instruction_base
, start
);
430 insns
= (struct brw_instruction
*) (gtt
+ instruction_base
+ start
);
431 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
436 handle_3dstate_constant(struct gen_spec
*spec
, uint32_t *p
)
442 for (i
= 0; i
< 4; i
++) {
443 length
= (p
[1 + i
/ 2] >> (i
& 1) * 16) & 0xffff;
444 f
= (float *) (gtt
+ p
[3 + i
* 2] + dynamic_state_base
);
446 for (j
= 0; j
< length
* 8; j
++) {
447 if (probably_float(dw
[j
]))
448 printf(" %04.3f", f
[j
]);
450 printf(" 0x%08x", dw
[j
]);
459 handle_3dstate_ps(struct gen_spec
*spec
, uint32_t *p
)
461 uint32_t mask
= ~((1 << 6) - 1);
463 struct brw_instruction
*insns
;
464 static const char unused
[] = "unused";
465 static const char *pixel_type
[3] = {"8 pixel", "16 pixel", "32 pixel"};
466 const char *k0
, *k1
, *k2
;
467 uint32_t k_mask
, k1_offset
, k2_offset
;
469 if (gen_spec_get_gen(spec
) >= gen_make_gen(8, 0)) {
480 #define DISPATCH_16 2
481 #define DISPATCH_32 4
494 case DISPATCH_8
| DISPATCH_16
:
504 case DISPATCH_16
| DISPATCH_32
:
509 case DISPATCH_8
| DISPATCH_16
| DISPATCH_32
:
521 start
= instruction_base
+ (p
[1] & mask
);
522 printf(" Kernel[0] %s\n", k0
);
524 insns
= (struct brw_instruction
*) (gtt
+ start
);
525 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
528 start
= instruction_base
+ (p
[k1_offset
] & mask
);
529 printf(" Kernel[1] %s\n", k1
);
531 insns
= (struct brw_instruction
*) (gtt
+ start
);
532 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
535 start
= instruction_base
+ (p
[k2_offset
] & mask
);
536 printf(" Kernel[2] %s\n", k2
);
538 insns
= (struct brw_instruction
*) (gtt
+ start
);
539 gen_disasm_disassemble(disasm
, insns
, 0, stdout
);
544 handle_3dstate_binding_table_pointers(struct gen_spec
*spec
, uint32_t *p
)
546 dump_binding_table(spec
, p
[1]);
550 handle_3dstate_sampler_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
552 dump_samplers(spec
, p
[1]);
556 handle_3dstate_viewport_state_pointers_cc(struct gen_spec
*spec
, uint32_t *p
)
559 struct gen_group
*cc_viewport
;
561 cc_viewport
= gen_spec_find_struct(spec
, "CC_VIEWPORT");
563 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
564 for (uint32_t i
= 0; i
< 4; i
++) {
565 printf("viewport %d\n", i
);
566 decode_structure(spec
, cc_viewport
, gtt
+ start
+ i
* 8);
571 handle_3dstate_viewport_state_pointers_sf_clip(struct gen_spec
*spec
,
575 struct gen_group
*sf_clip_viewport
;
577 sf_clip_viewport
= gen_spec_find_struct(spec
, "SF_CLIP_VIEWPORT");
579 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
580 for (uint32_t i
= 0; i
< 4; i
++) {
581 printf("viewport %d\n", i
);
582 decode_structure(spec
, sf_clip_viewport
, gtt
+ start
+ i
* 64);
587 handle_3dstate_blend_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
590 struct gen_group
*blend_state
;
592 blend_state
= gen_spec_find_struct(spec
, "BLEND_STATE");
594 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
595 decode_structure(spec
, blend_state
, gtt
+ start
);
599 handle_3dstate_cc_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
602 struct gen_group
*cc_state
;
604 cc_state
= gen_spec_find_struct(spec
, "COLOR_CALC_STATE");
606 start
= dynamic_state_base
+ (p
[1] & ~0x3fu
);
607 decode_structure(spec
, cc_state
, gtt
+ start
);
611 handle_3dstate_scissor_state_pointers(struct gen_spec
*spec
, uint32_t *p
)
614 struct gen_group
*scissor_rect
;
616 scissor_rect
= gen_spec_find_struct(spec
, "SCISSOR_RECT");
618 start
= dynamic_state_base
+ (p
[1] & ~0x1fu
);
619 decode_structure(spec
, scissor_rect
, gtt
+ start
);
623 handle_load_register_imm(struct gen_spec
*spec
, uint32_t *p
)
625 struct gen_group
*reg
= gen_spec_find_register(spec
, p
[1]);
628 printf("register %s (0x%x): 0x%x\n",
629 reg
->name
, reg
->register_offset
, p
[2]);
630 decode_structure(spec
, reg
, &p
[2]);
634 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
636 #define STATE_BASE_ADDRESS 0x61010000
638 #define MEDIA_INTERFACE_DESCRIPTOR_LOAD 0x70020000
640 #define _3DSTATE_INDEX_BUFFER 0x780a0000
641 #define _3DSTATE_VERTEX_BUFFERS 0x78080000
643 #define _3DSTATE_VS 0x78100000
644 #define _3DSTATE_GS 0x78110000
645 #define _3DSTATE_HS 0x781b0000
646 #define _3DSTATE_DS 0x781d0000
648 #define _3DSTATE_CONSTANT_VS 0x78150000
649 #define _3DSTATE_CONSTANT_GS 0x78160000
650 #define _3DSTATE_CONSTANT_PS 0x78170000
651 #define _3DSTATE_CONSTANT_HS 0x78190000
652 #define _3DSTATE_CONSTANT_DS 0x781A0000
654 #define _3DSTATE_PS 0x78200000
656 #define _3DSTATE_BINDING_TABLE_POINTERS_VS 0x78260000
657 #define _3DSTATE_BINDING_TABLE_POINTERS_HS 0x78270000
658 #define _3DSTATE_BINDING_TABLE_POINTERS_DS 0x78280000
659 #define _3DSTATE_BINDING_TABLE_POINTERS_GS 0x78290000
660 #define _3DSTATE_BINDING_TABLE_POINTERS_PS 0x782a0000
662 #define _3DSTATE_SAMPLER_STATE_POINTERS_VS 0x782b0000
663 #define _3DSTATE_SAMPLER_STATE_POINTERS_GS 0x782e0000
664 #define _3DSTATE_SAMPLER_STATE_POINTERS_PS 0x782f0000
666 #define _3DSTATE_VIEWPORT_STATE_POINTERS_CC 0x78230000
667 #define _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP 0x78210000
668 #define _3DSTATE_BLEND_STATE_POINTERS 0x78240000
669 #define _3DSTATE_CC_STATE_POINTERS 0x780e0000
670 #define _3DSTATE_SCISSOR_STATE_POINTERS 0x780f0000
672 #define _MI_LOAD_REGISTER_IMM 0x11000000
674 struct custom_handler
{
676 void (*handle
)(struct gen_spec
*spec
, uint32_t *p
);
677 } custom_handlers
[] = {
678 { STATE_BASE_ADDRESS
, handle_state_base_address
},
679 { MEDIA_INTERFACE_DESCRIPTOR_LOAD
, handle_media_interface_descriptor_load
},
680 { _3DSTATE_VERTEX_BUFFERS
, handle_3dstate_vertex_buffers
},
681 { _3DSTATE_INDEX_BUFFER
, handle_3dstate_index_buffer
},
682 { _3DSTATE_VS
, handle_3dstate_vs
},
683 { _3DSTATE_GS
, handle_3dstate_vs
},
684 { _3DSTATE_DS
, handle_3dstate_vs
},
685 { _3DSTATE_HS
, handle_3dstate_hs
},
686 { _3DSTATE_CONSTANT_VS
, handle_3dstate_constant
},
687 { _3DSTATE_CONSTANT_GS
, handle_3dstate_constant
},
688 { _3DSTATE_CONSTANT_PS
, handle_3dstate_constant
},
689 { _3DSTATE_CONSTANT_HS
, handle_3dstate_constant
},
690 { _3DSTATE_CONSTANT_DS
, handle_3dstate_constant
},
691 { _3DSTATE_PS
, handle_3dstate_ps
},
693 { _3DSTATE_BINDING_TABLE_POINTERS_VS
, handle_3dstate_binding_table_pointers
},
694 { _3DSTATE_BINDING_TABLE_POINTERS_HS
, handle_3dstate_binding_table_pointers
},
695 { _3DSTATE_BINDING_TABLE_POINTERS_DS
, handle_3dstate_binding_table_pointers
},
696 { _3DSTATE_BINDING_TABLE_POINTERS_GS
, handle_3dstate_binding_table_pointers
},
697 { _3DSTATE_BINDING_TABLE_POINTERS_PS
, handle_3dstate_binding_table_pointers
},
699 { _3DSTATE_SAMPLER_STATE_POINTERS_VS
, handle_3dstate_sampler_state_pointers
},
700 { _3DSTATE_SAMPLER_STATE_POINTERS_GS
, handle_3dstate_sampler_state_pointers
},
701 { _3DSTATE_SAMPLER_STATE_POINTERS_PS
, handle_3dstate_sampler_state_pointers
},
703 { _3DSTATE_VIEWPORT_STATE_POINTERS_CC
, handle_3dstate_viewport_state_pointers_cc
},
704 { _3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP
, handle_3dstate_viewport_state_pointers_sf_clip
},
705 { _3DSTATE_BLEND_STATE_POINTERS
, handle_3dstate_blend_state_pointers
},
706 { _3DSTATE_CC_STATE_POINTERS
, handle_3dstate_cc_state_pointers
},
707 { _3DSTATE_SCISSOR_STATE_POINTERS
, handle_3dstate_scissor_state_pointers
},
708 { _MI_LOAD_REGISTER_IMM
, handle_load_register_imm
}
712 parse_commands(struct gen_spec
*spec
, uint32_t *cmds
, int size
, int engine
)
714 uint32_t *p
, *end
= cmds
+ size
/ 4;
715 unsigned int length
, i
;
716 struct gen_group
*inst
;
718 for (p
= cmds
; p
< end
; p
+= length
) {
719 inst
= gen_spec_find_instruction(spec
, p
);
721 printf("unknown instruction %08x\n", p
[0]);
722 length
= (p
[0] & 0xff) + 2;
725 length
= gen_group_get_length(inst
, p
);
727 const char *color
, *reset_color
= NORMAL
;
730 if (option_full_decode
)
735 if (option_color
== COLOR_NEVER
) {
740 if (option_print_offsets
)
741 offset
= (void *) p
- gtt
;
745 printf("%s0x%08"PRIx64
": 0x%08x: %-80s%s\n",
747 gen_group_get_name(inst
), reset_color
);
749 if (option_full_decode
) {
750 struct gen_field_iterator iter
;
752 int idx
= 0, dword_num
= 0;
753 gen_field_iterator_init(&iter
, inst
, p
);
754 while (gen_field_iterator_next(&iter
)) {
756 print_dword_val(&iter
, offset
, &dword_num
);
758 token
= print_iterator_values(&iter
, &idx
);
760 printf("0x%08"PRIx64
": 0x%08x : Dword %d\n",
761 offset
+ 4 * idx
, p
[idx
], idx
);
762 handle_struct_decode(spec
,token
, &p
[idx
]);
767 for (i
= 0; i
< ARRAY_LENGTH(custom_handlers
); i
++) {
768 if (gen_group_get_opcode(inst
) ==
769 custom_handlers
[i
].opcode
)
770 custom_handlers
[i
].handle(spec
, p
);
774 if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_START
) {
776 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
777 start
= get_qword(&p
[1]);
781 parse_commands(spec
, gtt
+ start
, 1 << 20, engine
);
782 } else if ((p
[0] & 0xffff0000) == AUB_MI_BATCH_BUFFER_END
) {
788 #define GEN_ENGINE_RENDER 1
789 #define GEN_ENGINE_BLITTER 2
792 handle_trace_block(struct gen_spec
*spec
, uint32_t *p
)
794 int operation
= p
[1] & AUB_TRACE_OPERATION_MASK
;
795 int type
= p
[1] & AUB_TRACE_TYPE_MASK
;
796 int address_space
= p
[1] & AUB_TRACE_ADDRESS_SPACE_MASK
;
797 uint64_t offset
= p
[3];
798 uint32_t size
= p
[4];
799 int header_length
= p
[0] & 0xffff;
800 uint32_t *data
= p
+ header_length
+ 2;
801 int engine
= GEN_ENGINE_RENDER
;
803 if (gen_spec_get_gen(spec
) >= gen_make_gen(8,0))
804 offset
+= (uint64_t) p
[5] << 32;
807 case AUB_TRACE_OP_DATA_WRITE
:
808 if (address_space
!= AUB_TRACE_MEMTYPE_GTT
)
810 if (gtt_size
< offset
+ size
) {
811 fprintf(stderr
, "overflow gtt space: %s\n", strerror(errno
));
814 memcpy((char *) gtt
+ offset
, data
, size
);
815 if (gtt_end
< offset
+ size
)
816 gtt_end
= offset
+ size
;
818 case AUB_TRACE_OP_COMMAND_WRITE
:
820 case AUB_TRACE_TYPE_RING_PRB0
:
821 engine
= GEN_ENGINE_RENDER
;
823 case AUB_TRACE_TYPE_RING_PRB2
:
824 engine
= GEN_ENGINE_BLITTER
;
827 printf("command write to unknown ring %d\n", type
);
831 parse_commands(spec
, data
, size
, engine
);
840 uint32_t *map
, *end
, *cursor
;
844 static struct aub_file
*
845 aub_file_open(const char *filename
)
847 struct aub_file
*file
;
851 file
= calloc(1, sizeof *file
);
852 fd
= open(filename
, O_RDONLY
);
854 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
858 if (fstat(fd
, &sb
) == -1) {
859 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
863 file
->map
= mmap(NULL
, sb
.st_size
,
864 PROT_READ
, MAP_SHARED
, fd
, 0);
865 if (file
->map
== MAP_FAILED
) {
866 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
870 file
->cursor
= file
->map
;
871 file
->end
= file
->map
+ sb
.st_size
/ 4;
876 static struct aub_file
*
879 struct aub_file
*file
;
881 file
= calloc(1, sizeof *file
);
882 file
->stream
= stdin
;
887 #define TYPE(dw) (((dw) >> 29) & 7)
888 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
889 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
891 #define MAKE_HEADER(type, opcode, subopcode) \
892 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
896 /* Classic AUB opcodes */
897 #define OPCODE_AUB 0x01
898 #define SUBOPCODE_HEADER 0x05
899 #define SUBOPCODE_BLOCK 0x41
900 #define SUBOPCODE_BMP 0x1e
902 /* Newer version AUB opcode */
903 #define OPCODE_NEW_AUB 0x2e
904 #define SUBOPCODE_VERSION 0x00
905 #define SUBOPCODE_REG_WRITE 0x03
906 #define SUBOPCODE_MEM_POLL 0x05
907 #define SUBOPCODE_MEM_WRITE 0x06
909 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
915 { "bwr", MAKE_GEN(4, 0) },
916 { "cln", MAKE_GEN(4, 0) },
917 { "blc", MAKE_GEN(4, 0) },
918 { "ctg", MAKE_GEN(4, 0) },
919 { "el", MAKE_GEN(4, 0) },
920 { "il", MAKE_GEN(4, 0) },
921 { "sbr", MAKE_GEN(6, 0) },
922 { "ivb", MAKE_GEN(7, 0) },
923 { "lrb2", MAKE_GEN(0, 0) },
924 { "hsw", MAKE_GEN(7, 5) },
925 { "vlv", MAKE_GEN(7, 0) },
926 { "bdw", MAKE_GEN(8, 0) },
927 { "skl", MAKE_GEN(9, 0) },
928 { "chv", MAKE_GEN(8, 0) },
929 { "bxt", MAKE_GEN(9, 0) }
934 AUB_ITEM_DECODE_FAILED
,
935 AUB_ITEM_DECODE_NEED_MORE_DATA
,
939 aub_file_decode_batch(struct aub_file
*file
, struct gen_spec
*spec
)
941 uint32_t *p
, h
, device
, data_type
, *new_cursor
;
942 int header_length
, payload_size
, bias
;
944 if (file
->end
- file
->cursor
< 12)
945 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
949 header_length
= h
& 0xffff;
959 printf("unknown opcode %d at %td/%td\n",
960 OPCODE(h
), file
->cursor
- file
->map
,
961 file
->end
- file
->map
);
962 return AUB_ITEM_DECODE_FAILED
;
966 switch (h
& 0xffff0000) {
967 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
968 payload_size
= p
[12];
970 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
972 handle_trace_block(spec
, p
);
978 new_cursor
= p
+ header_length
+ bias
+ payload_size
/ 4;
979 if (new_cursor
> file
->end
)
980 return AUB_ITEM_DECODE_NEED_MORE_DATA
;
982 switch (h
& 0xffff0000) {
983 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_HEADER
):
985 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BLOCK
):
986 handle_trace_block(spec
, p
);
988 case MAKE_HEADER(TYPE_AUB
, OPCODE_AUB
, SUBOPCODE_BMP
):
990 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_VERSION
):
991 printf("version block: dw1 %08x\n", p
[1]);
992 device
= (p
[1] >> 8) & 0xff;
993 printf(" device %s\n", device_map
[device
].name
);
995 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_REG_WRITE
):
996 printf("register write block: (dwords %d)\n", h
& 0xffff);
997 printf(" reg 0x%x, data 0x%x\n", p
[1], p
[5]);
999 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_WRITE
):
1000 printf("memory write block (dwords %d):\n", h
& 0xffff);
1001 printf(" address 0x%"PRIx64
"\n", *(uint64_t *) &p
[1]);
1002 data_type
= (p
[3] >> 20) & 0xff;
1004 printf(" data type 0x%x\n", data_type
);
1005 printf(" address space 0x%x\n", (p
[3] >> 28) & 0xf);
1007 case MAKE_HEADER(TYPE_AUB
, OPCODE_NEW_AUB
, SUBOPCODE_MEM_POLL
):
1008 printf("memory poll block (dwords %d):\n", h
& 0xffff);
1011 printf("unknown block type=0x%x, opcode=0x%x, "
1012 "subopcode=0x%x (%08x)\n", TYPE(h
), OPCODE(h
), SUBOPCODE(h
), h
);
1015 file
->cursor
= new_cursor
;
1017 return AUB_ITEM_DECODE_OK
;
1021 aub_file_more_stuff(struct aub_file
*file
)
1023 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
1026 #define AUB_READ_BUFFER_SIZE (4096)
1027 #define MAX(a, b) ((a) < (b) ? (b) : (a))
1030 aub_file_data_grow(struct aub_file
*file
)
1032 size_t old_size
= (file
->mem_end
- file
->map
) * 4;
1033 size_t new_size
= MAX(old_size
* 2, AUB_READ_BUFFER_SIZE
);
1034 uint32_t *new_start
= realloc(file
->map
, new_size
);
1036 file
->cursor
= new_start
+ (file
->cursor
- file
->map
);
1037 file
->end
= new_start
+ (file
->end
- file
->map
);
1038 file
->map
= new_start
;
1039 file
->mem_end
= file
->map
+ (new_size
/ 4);
1043 aub_file_data_load(struct aub_file
*file
)
1047 if (file
->stream
== NULL
)
1050 /* First remove any consumed data */
1051 if (file
->cursor
> file
->map
) {
1052 memmove(file
->map
, file
->cursor
,
1053 (file
->end
- file
->cursor
) * 4);
1054 file
->end
-= file
->cursor
- file
->map
;
1055 file
->cursor
= file
->map
;
1058 /* Then load some new data in */
1059 if ((file
->mem_end
- file
->end
) < (AUB_READ_BUFFER_SIZE
/ 4))
1060 aub_file_data_grow(file
);
1062 r
= fread(file
->end
, 1, (file
->mem_end
- file
->end
) * 4, file
->stream
);
1077 if (pipe(fds
) == -1)
1087 execlp("less", "less", "-FRSi", NULL
);
1096 print_help(const char *progname
, FILE *file
)
1099 "Usage: %s [OPTION]... [FILE]\n"
1100 "Decode aub file contents from either FILE or the standard input.\n\n"
1101 "A valid --gen option must be provided.\n\n"
1102 " --help display this help and exit\n"
1103 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl or bxt)\n"
1104 " --headers decode only command headers\n"
1105 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
1106 " if omitted), 'always', or 'never'\n"
1107 " --no-pager don't launch pager\n"
1108 " --no-offsets don't print instruction offsets\n"
1109 " --xml=DIR load hardware xml description from directory DIR\n",
1113 int main(int argc
, char *argv
[])
1115 struct gen_spec
*spec
;
1116 struct aub_file
*file
;
1118 bool help
= false, pager
= true;
1119 char *input_file
= NULL
, *xml_path
= NULL
;
1125 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
1126 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
1127 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
1128 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
1129 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
1130 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
1131 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
1132 { "bxt", 0x0A84 } /* Intel(R) HD Graphics (Broxton) */
1134 const struct option aubinator_opts
[] = {
1135 { "help", no_argument
, (int *) &help
, true },
1136 { "no-pager", no_argument
, (int *) &pager
, false },
1137 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
1138 { "gen", required_argument
, NULL
, 'g' },
1139 { "headers", no_argument
, (int *) &option_full_decode
, false },
1140 { "color", required_argument
, NULL
, 'c' },
1141 { "xml", required_argument
, NULL
, 'x' },
1142 { NULL
, 0, NULL
, 0 }
1144 struct gen_device_info devinfo
;
1147 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
1150 snprintf(gen_val
, sizeof(gen_val
), "%s", optarg
);
1153 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
1154 option_color
= COLOR_ALWAYS
;
1155 else if (strcmp(optarg
, "never") == 0)
1156 option_color
= COLOR_NEVER
;
1157 else if (strcmp(optarg
, "auto") == 0)
1158 option_color
= COLOR_AUTO
;
1160 fprintf(stderr
, "invalid value for --color: %s", optarg
);
1165 xml_path
= strdup(optarg
);
1172 if (help
|| argc
== 1) {
1173 print_help(argv
[0], stderr
);
1178 input_file
= argv
[optind
];
1180 for (i
= 0; i
< ARRAY_SIZE(gens
); i
++) {
1181 if (!strcmp(gen_val
, gens
[i
].name
)) {
1188 fprintf(stderr
, "can't parse gen: %s, expected ivb, byt, hsw, "
1189 "bdw, chv, skl, kbl or bxt\n", gen_val
);
1193 if (!gen_get_device_info(gen
->pci_id
, &devinfo
)) {
1194 fprintf(stderr
, "can't find device information: pci_id=0x%x name=%s\n",
1195 gen
->pci_id
, gen
->name
);
1200 /* Do this before we redirect stdout to pager. */
1201 if (option_color
== COLOR_AUTO
)
1202 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
1204 if (isatty(1) && pager
)
1207 if (xml_path
== NULL
)
1208 spec
= gen_spec_load(&devinfo
);
1210 spec
= gen_spec_load_from_path(&devinfo
, xml_path
);
1211 disasm
= gen_disasm_create(gen
->pci_id
);
1213 if (spec
== NULL
|| disasm
== NULL
)
1216 if (input_file
== NULL
)
1217 file
= aub_file_stdin();
1219 file
= aub_file_open(input_file
);
1221 /* mmap a terabyte for our gtt space. */
1222 gtt_size
= 1ul << 40;
1223 gtt
= mmap(NULL
, gtt_size
, PROT_READ
| PROT_WRITE
,
1224 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_NORESERVE
, -1, 0);
1225 if (gtt
== MAP_FAILED
) {
1226 fprintf(stderr
, "failed to alloc gtt space: %s\n", strerror(errno
));
1230 while (aub_file_more_stuff(file
)) {
1231 switch (aub_file_decode_batch(file
, spec
)) {
1232 case AUB_ITEM_DECODE_OK
:
1234 case AUB_ITEM_DECODE_NEED_MORE_DATA
:
1235 if (!file
->stream
) {
1236 file
->cursor
= file
->end
;
1239 if (aub_file_more_stuff(file
) && !aub_file_data_load(file
)) {
1240 fprintf(stderr
, "failed to load data from stdin\n");
1245 fprintf(stderr
, "failed to parse aubdump data\n");
1252 /* close the stdout which is opened to write the output */
1258 return EXIT_SUCCESS
;