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
36 #include <sys/types.h>
41 #include "util/macros.h"
47 #define BLUE_HEADER CSI "0;44m"
48 #define GREEN_HEADER CSI "1;42m"
49 #define NORMAL CSI "0m"
53 static int option_full_decode
= true;
54 static int option_print_offsets
= true;
55 static int max_vbo_lines
= -1;
56 static enum { COLOR_AUTO
, COLOR_ALWAYS
, COLOR_NEVER
} option_color
;
61 char *input_file
= NULL
, *xml_path
= NULL
;
62 struct gen_device_info devinfo
;
63 struct gen_batch_decode_ctx batch_ctx
;
68 struct brw_instruction
;
71 aubinator_error(void *user_data
, const void *aub_data
, const char *msg
)
73 fprintf(stderr
, "%s", msg
);
77 aubinator_init(void *user_data
, int aub_pci_id
, const char *app_name
)
81 if (!gen_get_device_info(pci_id
, &devinfo
)) {
82 fprintf(stderr
, "can't find device information: pci_id=0x%x\n", pci_id
);
86 enum gen_batch_decode_flags batch_flags
= 0;
87 if (option_color
== COLOR_ALWAYS
)
88 batch_flags
|= GEN_BATCH_DECODE_IN_COLOR
;
89 if (option_full_decode
)
90 batch_flags
|= GEN_BATCH_DECODE_FULL
;
91 if (option_print_offsets
)
92 batch_flags
|= GEN_BATCH_DECODE_OFFSETS
;
93 batch_flags
|= GEN_BATCH_DECODE_FLOATS
;
95 gen_batch_decode_ctx_init(&batch_ctx
, &devinfo
, outfile
, batch_flags
,
96 xml_path
, NULL
, NULL
, NULL
);
98 /* Check for valid spec instance, if wrong xml_path is passed then spec
99 * instance is not initialized properly
101 if (!batch_ctx
.spec
) {
102 fprintf(stderr
, "Failed to initialize gen_batch_decode_ctx "
105 gen_batch_decode_ctx_finish(&batch_ctx
);
109 batch_ctx
.max_vbo_decoded_lines
= max_vbo_lines
;
111 char *color
= GREEN_HEADER
, *reset_color
= NORMAL
;
112 if (option_color
== COLOR_NEVER
)
113 color
= reset_color
= "";
115 fprintf(outfile
, "%sAubinator: Intel AUB file decoder.%-80s%s\n",
116 color
, "", reset_color
);
119 fprintf(outfile
, "File name: %s\n", input_file
);
122 fprintf(outfile
, "PCI ID: 0x%x\n", aub_pci_id
);
124 fprintf(outfile
, "Application name: %s\n", app_name
);
126 fprintf(outfile
, "Decoding as: %s\n", gen_get_device_name(pci_id
));
128 /* Throw in a new line before the first batch */
129 fprintf(outfile
, "\n");
133 handle_execlist_write(void *user_data
, enum drm_i915_gem_engine_class engine
, uint64_t context_descriptor
)
135 const uint32_t pphwsp_size
= 4096;
136 uint32_t pphwsp_addr
= context_descriptor
& 0xfffff000;
137 struct gen_batch_decode_bo pphwsp_bo
= aub_mem_get_ggtt_bo(&mem
, pphwsp_addr
);
138 uint32_t *context
= (uint32_t *)((uint8_t *)pphwsp_bo
.map
+
139 (pphwsp_addr
- pphwsp_bo
.addr
) +
142 uint32_t ring_buffer_head
= context
[5];
143 uint32_t ring_buffer_tail
= context
[7];
144 uint32_t ring_buffer_start
= context
[9];
145 uint32_t ring_buffer_length
= (context
[11] & 0x1ff000) + 4096;
147 mem
.pml4
= (uint64_t)context
[49] << 32 | context
[51];
148 batch_ctx
.user_data
= &mem
;
150 struct gen_batch_decode_bo ring_bo
= aub_mem_get_ggtt_bo(&mem
,
152 assert(ring_bo
.size
> 0);
153 void *commands
= (uint8_t *)ring_bo
.map
+ (ring_buffer_start
- ring_bo
.addr
);
155 if (context_descriptor
& 0x100 /* ppgtt */) {
156 batch_ctx
.get_bo
= aub_mem_get_ppgtt_bo
;
158 batch_ctx
.get_bo
= aub_mem_get_ggtt_bo
;
161 batch_ctx
.engine
= engine
;
162 gen_print_batch(&batch_ctx
, commands
,
163 MIN2(ring_buffer_tail
- ring_buffer_head
, ring_buffer_length
),
165 aub_mem_clear_bo_maps(&mem
);
169 handle_ring_write(void *user_data
, enum drm_i915_gem_engine_class engine
,
170 const void *data
, uint32_t data_len
)
172 batch_ctx
.user_data
= &mem
;
173 batch_ctx
.get_bo
= aub_mem_get_ggtt_bo
;
175 batch_ctx
.engine
= engine
;
176 gen_print_batch(&batch_ctx
, data
, data_len
, 0);
178 aub_mem_clear_bo_maps(&mem
);
184 void *map
, *end
, *cursor
;
187 static struct aub_file
*
188 aub_file_open(const char *filename
)
190 struct aub_file
*file
;
194 file
= calloc(1, sizeof *file
);
198 fd
= open(filename
, O_RDONLY
);
200 fprintf(stderr
, "open %s failed: %s\n", filename
, strerror(errno
));
205 if (fstat(fd
, &sb
) == -1) {
206 fprintf(stderr
, "stat failed: %s\n", strerror(errno
));
211 file
->map
= mmap(NULL
, sb
.st_size
,
212 PROT_READ
, MAP_SHARED
, fd
, 0);
213 if (file
->map
== MAP_FAILED
) {
214 fprintf(stderr
, "mmap failed: %s\n", strerror(errno
));
221 file
->cursor
= file
->map
;
222 file
->end
= file
->map
+ sb
.st_size
;
228 aub_file_more_stuff(struct aub_file
*file
)
230 return file
->cursor
< file
->end
|| (file
->stream
&& !feof(file
->stream
));
252 execlp("less", "less", "-FRSi", NULL
);
261 print_help(const char *progname
, FILE *file
)
264 "Usage: %s [OPTION]... FILE\n"
265 "Decode aub file contents from FILE.\n\n"
266 " --help display this help and exit\n"
267 " --gen=platform decode for given platform (3 letter platform name)\n"
268 " --headers decode only command headers\n"
269 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
270 " if omitted), 'always', or 'never'\n"
271 " --max-vbo-lines=N limit the number of decoded VBO lines\n"
272 " --no-pager don't launch pager\n"
273 " --no-offsets don't print instruction offsets\n"
274 " --xml=DIR load hardware xml description from directory DIR\n",
278 int main(int argc
, char *argv
[])
280 struct aub_file
*file
;
282 bool help
= false, pager
= true;
283 const struct option aubinator_opts
[] = {
284 { "help", no_argument
, (int *) &help
, true },
285 { "no-pager", no_argument
, (int *) &pager
, false },
286 { "no-offsets", no_argument
, (int *) &option_print_offsets
, false },
287 { "gen", required_argument
, NULL
, 'g' },
288 { "headers", no_argument
, (int *) &option_full_decode
, false },
289 { "color", required_argument
, NULL
, 'c' },
290 { "xml", required_argument
, NULL
, 'x' },
291 { "max-vbo-lines", required_argument
, NULL
, 'v' },
298 while ((c
= getopt_long(argc
, argv
, "", aubinator_opts
, &i
)) != -1) {
301 const int id
= gen_device_name_to_pci_device_id(optarg
);
303 fprintf(stderr
, "can't parse gen: '%s', expected brw, g4x, ilk, "
304 "snb, ivb, hsw, byt, bdw, chv, skl, bxt, kbl, "
305 "aml, glk, cfl, whl, cnl, icl", optarg
);
313 if (optarg
== NULL
|| strcmp(optarg
, "always") == 0)
314 option_color
= COLOR_ALWAYS
;
315 else if (strcmp(optarg
, "never") == 0)
316 option_color
= COLOR_NEVER
;
317 else if (strcmp(optarg
, "auto") == 0)
318 option_color
= COLOR_AUTO
;
320 fprintf(stderr
, "invalid value for --color: %s", optarg
);
325 xml_path
= strdup(optarg
);
328 max_vbo_lines
= atoi(optarg
);
336 input_file
= argv
[optind
];
338 if (help
|| !input_file
) {
339 print_help(argv
[0], stderr
);
343 /* Do this before we redirect stdout to pager. */
344 if (option_color
== COLOR_AUTO
)
345 option_color
= isatty(1) ? COLOR_ALWAYS
: COLOR_NEVER
;
347 if (isatty(1) && pager
)
350 if (!aub_mem_init(&mem
)) {
351 fprintf(stderr
, "Unable to create GTT\n");
355 file
= aub_file_open(input_file
);
357 fprintf(stderr
, "Unable to allocate buffer to open aub file\n");
362 struct aub_read aub_read
= {
364 .error
= aubinator_error
,
365 .info
= aubinator_init
,
367 .local_write
= aub_mem_local_write
,
368 .phys_write
= aub_mem_phys_write
,
369 .ggtt_write
= aub_mem_ggtt_write
,
370 .ggtt_entry_write
= aub_mem_ggtt_entry_write
,
372 .execlist_write
= handle_execlist_write
,
373 .ring_write
= handle_ring_write
,
376 while (aub_file_more_stuff(file
) &&
377 (consumed
= aub_read_command(&aub_read
, file
->cursor
,
378 file
->end
- file
->cursor
)) > 0) {
379 file
->cursor
+= consumed
;
385 /* close the stdout which is opened to write the output */
391 gen_batch_decode_ctx_finish(&batch_ctx
);