aubinator: honor --color option when printing the header
[mesa.git] / src / intel / tools / aubinator.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <getopt.h>
28
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <errno.h>
34 #include <inttypes.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <sys/mman.h>
39
40 #include "util/macros.h"
41
42 #include "common/gen_decoder.h"
43 #include "intel_aub.h"
44 #include "gen_disasm.h"
45
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
49 */
50 #define AUB_MI_BATCH_BUFFER_END (0x0500 << 16)
51
52 #define CSI "\e["
53 #define BLUE_HEADER CSI "0;44m"
54 #define GREEN_HEADER CSI "1;42m"
55 #define NORMAL CSI "0m"
56
57 /* options */
58
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;
62
63 /* state */
64
65 uint16_t pci_id = 0;
66 char *input_file = NULL, *xml_path = NULL;
67 struct gen_device_info devinfo;
68 struct gen_batch_decode_ctx batch_ctx;
69
70 uint64_t gtt_size, gtt_end;
71 void *gtt;
72 uint64_t general_state_base;
73 uint64_t surface_state_base;
74 uint64_t dynamic_state_base;
75 uint64_t instruction_base;
76 uint64_t instruction_bound;
77
78 FILE *outfile;
79
80 static inline uint32_t
81 field(uint32_t value, int start, int end)
82 {
83 uint32_t mask;
84
85 mask = ~0U >> (31 - end + start);
86
87 return (value >> start) & mask;
88 }
89
90 struct brw_instruction;
91
92 static inline int
93 valid_offset(uint32_t offset)
94 {
95 return offset < gtt_end;
96 }
97
98 #define GEN_ENGINE_RENDER 1
99 #define GEN_ENGINE_BLITTER 2
100
101 static void
102 handle_trace_block(uint32_t *p)
103 {
104 int operation = p[1] & AUB_TRACE_OPERATION_MASK;
105 int type = p[1] & AUB_TRACE_TYPE_MASK;
106 int address_space = p[1] & AUB_TRACE_ADDRESS_SPACE_MASK;
107 uint64_t offset = p[3];
108 uint32_t size = p[4];
109 int header_length = p[0] & 0xffff;
110 uint32_t *data = p + header_length + 2;
111 int engine = GEN_ENGINE_RENDER;
112
113 if (devinfo.gen >= 8)
114 offset += (uint64_t) p[5] << 32;
115
116 switch (operation) {
117 case AUB_TRACE_OP_DATA_WRITE:
118 if (address_space != AUB_TRACE_MEMTYPE_GTT)
119 break;
120 if (gtt_size < offset + size) {
121 fprintf(stderr, "overflow gtt space: %s\n", strerror(errno));
122 exit(EXIT_FAILURE);
123 }
124 memcpy((char *) gtt + offset, data, size);
125 if (gtt_end < offset + size)
126 gtt_end = offset + size;
127 break;
128 case AUB_TRACE_OP_COMMAND_WRITE:
129 switch (type) {
130 case AUB_TRACE_TYPE_RING_PRB0:
131 engine = GEN_ENGINE_RENDER;
132 break;
133 case AUB_TRACE_TYPE_RING_PRB2:
134 engine = GEN_ENGINE_BLITTER;
135 break;
136 default:
137 fprintf(outfile, "command write to unknown ring %d\n", type);
138 break;
139 }
140
141 (void)engine; /* TODO */
142 gen_print_batch(&batch_ctx, data, size, 0);
143
144 gtt_end = 0;
145 break;
146 }
147 }
148
149 static struct gen_batch_decode_bo
150 get_gen_batch_bo(void *user_data, uint64_t address)
151 {
152 if (address > gtt_end)
153 return (struct gen_batch_decode_bo) { .map = NULL };
154
155 /* We really only have one giant address range */
156 return (struct gen_batch_decode_bo) {
157 .addr = 0,
158 .map = gtt,
159 .size = gtt_size
160 };
161 }
162
163 static void
164 handle_trace_header(uint32_t *p)
165 {
166 /* The intel_aubdump tool from IGT is kind enough to put a PCI-ID= tag in
167 * the AUB header comment. If the user hasn't specified a hardware
168 * generation, try to use the one from the AUB file.
169 */
170 uint32_t *end = p + (p[0] & 0xffff) + 2;
171 int aub_pci_id = 0;
172 if (end > &p[12] && p[12] > 0)
173 sscanf((char *)&p[13], "PCI-ID=%i", &aub_pci_id);
174
175 if (pci_id == 0)
176 pci_id = aub_pci_id;
177
178 if (!gen_get_device_info(pci_id, &devinfo)) {
179 fprintf(stderr, "can't find device information: pci_id=0x%x\n", pci_id);
180 exit(EXIT_FAILURE);
181 }
182
183 enum gen_batch_decode_flags batch_flags = 0;
184 if (option_color == COLOR_ALWAYS)
185 batch_flags |= GEN_BATCH_DECODE_IN_COLOR;
186 if (option_full_decode)
187 batch_flags |= GEN_BATCH_DECODE_FULL;
188 if (option_print_offsets)
189 batch_flags |= GEN_BATCH_DECODE_OFFSETS;
190 batch_flags |= GEN_BATCH_DECODE_FLOATS;
191
192 gen_batch_decode_ctx_init(&batch_ctx, &devinfo, outfile, batch_flags,
193 xml_path, get_gen_batch_bo, NULL);
194
195 char *color = GREEN_HEADER, *reset_color = NORMAL;
196 if (option_color == COLOR_NEVER)
197 color = reset_color = "";
198
199 fprintf(outfile, "%sAubinator: Intel AUB file decoder.%-80s%s\n",
200 color, "", reset_color);
201
202 if (input_file)
203 fprintf(outfile, "File name: %s\n", input_file);
204
205 if (aub_pci_id)
206 fprintf(outfile, "PCI ID: 0x%x\n", aub_pci_id);
207
208 char app_name[33];
209 strncpy(app_name, (char *)&p[2], 32);
210 app_name[32] = 0;
211 fprintf(outfile, "Application name: %s\n", app_name);
212
213 fprintf(outfile, "Decoding as: %s\n", gen_get_device_name(pci_id));
214
215 /* Throw in a new line before the first batch */
216 fprintf(outfile, "\n");
217 }
218
219 struct aub_file {
220 FILE *stream;
221
222 uint32_t *map, *end, *cursor;
223 uint32_t *mem_end;
224 };
225
226 static struct aub_file *
227 aub_file_open(const char *filename)
228 {
229 struct aub_file *file;
230 struct stat sb;
231 int fd;
232
233 file = calloc(1, sizeof *file);
234 fd = open(filename, O_RDONLY);
235 if (fd == -1) {
236 fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno));
237 exit(EXIT_FAILURE);
238 }
239
240 if (fstat(fd, &sb) == -1) {
241 fprintf(stderr, "stat failed: %s\n", strerror(errno));
242 exit(EXIT_FAILURE);
243 }
244
245 file->map = mmap(NULL, sb.st_size,
246 PROT_READ, MAP_SHARED, fd, 0);
247 if (file->map == MAP_FAILED) {
248 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
249 exit(EXIT_FAILURE);
250 }
251
252 close(fd);
253
254 file->cursor = file->map;
255 file->end = file->map + sb.st_size / 4;
256
257 return file;
258 }
259
260 static struct aub_file *
261 aub_file_stdin(void)
262 {
263 struct aub_file *file;
264
265 file = calloc(1, sizeof *file);
266 file->stream = stdin;
267
268 return file;
269 }
270
271 #define TYPE(dw) (((dw) >> 29) & 7)
272 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
273 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
274
275 #define MAKE_HEADER(type, opcode, subopcode) \
276 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
277
278 #define TYPE_AUB 0x7
279
280 /* Classic AUB opcodes */
281 #define OPCODE_AUB 0x01
282 #define SUBOPCODE_HEADER 0x05
283 #define SUBOPCODE_BLOCK 0x41
284 #define SUBOPCODE_BMP 0x1e
285
286 /* Newer version AUB opcode */
287 #define OPCODE_NEW_AUB 0x2e
288 #define SUBOPCODE_VERSION 0x00
289 #define SUBOPCODE_REG_WRITE 0x03
290 #define SUBOPCODE_MEM_POLL 0x05
291 #define SUBOPCODE_MEM_WRITE 0x06
292
293 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
294
295 struct {
296 const char *name;
297 uint32_t gen;
298 } device_map[] = {
299 { "bwr", MAKE_GEN(4, 0) },
300 { "cln", MAKE_GEN(4, 0) },
301 { "blc", MAKE_GEN(4, 0) },
302 { "ctg", MAKE_GEN(4, 0) },
303 { "el", MAKE_GEN(4, 0) },
304 { "il", MAKE_GEN(4, 0) },
305 { "sbr", MAKE_GEN(6, 0) },
306 { "ivb", MAKE_GEN(7, 0) },
307 { "lrb2", MAKE_GEN(0, 0) },
308 { "hsw", MAKE_GEN(7, 5) },
309 { "vlv", MAKE_GEN(7, 0) },
310 { "bdw", MAKE_GEN(8, 0) },
311 { "skl", MAKE_GEN(9, 0) },
312 { "chv", MAKE_GEN(8, 0) },
313 { "bxt", MAKE_GEN(9, 0) },
314 { "cnl", MAKE_GEN(10, 0) },
315 };
316
317 enum {
318 AUB_ITEM_DECODE_OK,
319 AUB_ITEM_DECODE_FAILED,
320 AUB_ITEM_DECODE_NEED_MORE_DATA,
321 };
322
323 static int
324 aub_file_decode_batch(struct aub_file *file)
325 {
326 uint32_t *p, h, device, data_type, *new_cursor;
327 int header_length, bias;
328
329 if (file->end - file->cursor < 1)
330 return AUB_ITEM_DECODE_NEED_MORE_DATA;
331
332 p = file->cursor;
333 h = *p;
334 header_length = h & 0xffff;
335
336 switch (OPCODE(h)) {
337 case OPCODE_AUB:
338 bias = 2;
339 break;
340 case OPCODE_NEW_AUB:
341 bias = 1;
342 break;
343 default:
344 fprintf(outfile, "unknown opcode %d at %td/%td\n",
345 OPCODE(h), file->cursor - file->map,
346 file->end - file->map);
347 return AUB_ITEM_DECODE_FAILED;
348 }
349
350 new_cursor = p + header_length + bias;
351 if ((h & 0xffff0000) == MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BLOCK)) {
352 if (file->end - file->cursor < 4)
353 return AUB_ITEM_DECODE_NEED_MORE_DATA;
354 new_cursor += p[4] / 4;
355 }
356
357 if (new_cursor > file->end)
358 return AUB_ITEM_DECODE_NEED_MORE_DATA;
359
360 switch (h & 0xffff0000) {
361 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_HEADER):
362 handle_trace_header(p);
363 break;
364 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BLOCK):
365 handle_trace_block(p);
366 break;
367 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BMP):
368 break;
369 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_VERSION):
370 fprintf(outfile, "version block: dw1 %08x\n", p[1]);
371 device = (p[1] >> 8) & 0xff;
372 fprintf(outfile, " device %s\n", device_map[device].name);
373 break;
374 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_REG_WRITE):
375 fprintf(outfile, "register write block: (dwords %d)\n", h & 0xffff);
376 fprintf(outfile, " reg 0x%x, data 0x%x\n", p[1], p[5]);
377 break;
378 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_WRITE):
379 fprintf(outfile, "memory write block (dwords %d):\n", h & 0xffff);
380 fprintf(outfile, " address 0x%"PRIx64"\n", *(uint64_t *) &p[1]);
381 data_type = (p[3] >> 20) & 0xff;
382 if (data_type != 0)
383 fprintf(outfile, " data type 0x%x\n", data_type);
384 fprintf(outfile, " address space 0x%x\n", (p[3] >> 28) & 0xf);
385 break;
386 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_POLL):
387 fprintf(outfile, "memory poll block (dwords %d):\n", h & 0xffff);
388 break;
389 default:
390 fprintf(outfile, "unknown block type=0x%x, opcode=0x%x, "
391 "subopcode=0x%x (%08x)\n", TYPE(h), OPCODE(h), SUBOPCODE(h), h);
392 break;
393 }
394 file->cursor = new_cursor;
395
396 return AUB_ITEM_DECODE_OK;
397 }
398
399 static int
400 aub_file_more_stuff(struct aub_file *file)
401 {
402 return file->cursor < file->end || (file->stream && !feof(file->stream));
403 }
404
405 #define AUB_READ_BUFFER_SIZE (4096)
406 #define MAX(a, b) ((a) < (b) ? (b) : (a))
407
408 static void
409 aub_file_data_grow(struct aub_file *file)
410 {
411 size_t old_size = (file->mem_end - file->map) * 4;
412 size_t new_size = MAX(old_size * 2, AUB_READ_BUFFER_SIZE);
413 uint32_t *new_start = realloc(file->map, new_size);
414
415 file->cursor = new_start + (file->cursor - file->map);
416 file->end = new_start + (file->end - file->map);
417 file->map = new_start;
418 file->mem_end = file->map + (new_size / 4);
419 }
420
421 static bool
422 aub_file_data_load(struct aub_file *file)
423 {
424 size_t r;
425
426 if (file->stream == NULL)
427 return false;
428
429 /* First remove any consumed data */
430 if (file->cursor > file->map) {
431 memmove(file->map, file->cursor,
432 (file->end - file->cursor) * 4);
433 file->end -= file->cursor - file->map;
434 file->cursor = file->map;
435 }
436
437 /* Then load some new data in */
438 if ((file->mem_end - file->end) < (AUB_READ_BUFFER_SIZE / 4))
439 aub_file_data_grow(file);
440
441 r = fread(file->end, 1, (file->mem_end - file->end) * 4, file->stream);
442 file->end += r / 4;
443
444 return r != 0;
445 }
446
447 static void
448 setup_pager(void)
449 {
450 int fds[2];
451 pid_t pid;
452
453 if (!isatty(1))
454 return;
455
456 if (pipe(fds) == -1)
457 return;
458
459 pid = fork();
460 if (pid == -1)
461 return;
462
463 if (pid == 0) {
464 close(fds[1]);
465 dup2(fds[0], 0);
466 execlp("less", "less", "-FRSi", NULL);
467 }
468
469 close(fds[0]);
470 dup2(fds[1], 1);
471 close(fds[1]);
472 }
473
474 static void
475 print_help(const char *progname, FILE *file)
476 {
477 fprintf(file,
478 "Usage: %s [OPTION]... [FILE]\n"
479 "Decode aub file contents from either FILE or the standard input.\n\n"
480 "A valid --gen option must be provided.\n\n"
481 " --help display this help and exit\n"
482 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl, bxt or cnl)\n"
483 " --headers decode only command headers\n"
484 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
485 " if omitted), 'always', or 'never'\n"
486 " --no-pager don't launch pager\n"
487 " --no-offsets don't print instruction offsets\n"
488 " --xml=DIR load hardware xml description from directory DIR\n",
489 progname);
490 }
491
492 int main(int argc, char *argv[])
493 {
494 struct aub_file *file;
495 int c, i;
496 bool help = false, pager = true;
497 const struct {
498 const char *name;
499 int pci_id;
500 } gens[] = {
501 { "ilk", 0x0046 }, /* Intel(R) Ironlake Mobile */
502 { "snb", 0x0126 }, /* Intel(R) Sandybridge Mobile GT2 */
503 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
504 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
505 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
506 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
507 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
508 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
509 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
510 { "bxt", 0x0A84 }, /* Intel(R) HD Graphics (Broxton) */
511 { "cnl", 0x5A52 }, /* Intel(R) HD Graphics (Cannonlake) */
512 };
513 const struct option aubinator_opts[] = {
514 { "help", no_argument, (int *) &help, true },
515 { "no-pager", no_argument, (int *) &pager, false },
516 { "no-offsets", no_argument, (int *) &option_print_offsets, false },
517 { "gen", required_argument, NULL, 'g' },
518 { "headers", no_argument, (int *) &option_full_decode, false },
519 { "color", required_argument, NULL, 'c' },
520 { "xml", required_argument, NULL, 'x' },
521 { NULL, 0, NULL, 0 }
522 };
523
524 outfile = stdout;
525
526 i = 0;
527 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
528 switch (c) {
529 case 'g':
530 for (i = 0; i < ARRAY_SIZE(gens); i++) {
531 if (!strcmp(optarg, gens[i].name)) {
532 pci_id = gens[i].pci_id;
533 break;
534 }
535 }
536 if (i == ARRAY_SIZE(gens)) {
537 fprintf(stderr, "can't parse gen: '%s', expected ivb, byt, hsw, "
538 "bdw, chv, skl, kbl or bxt\n", optarg);
539 exit(EXIT_FAILURE);
540 }
541 break;
542 case 'c':
543 if (optarg == NULL || strcmp(optarg, "always") == 0)
544 option_color = COLOR_ALWAYS;
545 else if (strcmp(optarg, "never") == 0)
546 option_color = COLOR_NEVER;
547 else if (strcmp(optarg, "auto") == 0)
548 option_color = COLOR_AUTO;
549 else {
550 fprintf(stderr, "invalid value for --color: %s", optarg);
551 exit(EXIT_FAILURE);
552 }
553 break;
554 case 'x':
555 xml_path = strdup(optarg);
556 break;
557 default:
558 break;
559 }
560 }
561
562 if (help || argc == 1) {
563 print_help(argv[0], stderr);
564 exit(0);
565 }
566
567 if (optind < argc)
568 input_file = argv[optind];
569
570 /* Do this before we redirect stdout to pager. */
571 if (option_color == COLOR_AUTO)
572 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
573
574 if (isatty(1) && pager)
575 setup_pager();
576
577 if (input_file == NULL)
578 file = aub_file_stdin();
579 else
580 file = aub_file_open(input_file);
581
582 /* mmap a terabyte for our gtt space. */
583 gtt_size = 1ull << 40;
584 gtt = mmap(NULL, gtt_size, PROT_READ | PROT_WRITE,
585 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
586 if (gtt == MAP_FAILED) {
587 fprintf(stderr, "failed to alloc gtt space: %s\n", strerror(errno));
588 exit(EXIT_FAILURE);
589 }
590
591 while (aub_file_more_stuff(file)) {
592 switch (aub_file_decode_batch(file)) {
593 case AUB_ITEM_DECODE_OK:
594 break;
595 case AUB_ITEM_DECODE_NEED_MORE_DATA:
596 if (!file->stream) {
597 file->cursor = file->end;
598 break;
599 }
600 if (aub_file_more_stuff(file) && !aub_file_data_load(file)) {
601 fprintf(stderr, "failed to load data from stdin\n");
602 exit(EXIT_FAILURE);
603 }
604 break;
605 default:
606 fprintf(stderr, "failed to parse aubdump data\n");
607 exit(EXIT_FAILURE);
608 }
609 }
610
611
612 fflush(stdout);
613 /* close the stdout which is opened to write the output */
614 close(1);
615 free(xml_path);
616
617 wait(NULL);
618
619 return EXIT_SUCCESS;
620 }