fcb46073f3086a7eaf7f10cf96868d623356f89f
[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 aubinator_init(uint16_t aub_pci_id, const char *app_name)
165 {
166 if (!gen_get_device_info(pci_id, &devinfo)) {
167 fprintf(stderr, "can't find device information: pci_id=0x%x\n", pci_id);
168 exit(EXIT_FAILURE);
169 }
170
171 enum gen_batch_decode_flags batch_flags = 0;
172 if (option_color == COLOR_ALWAYS)
173 batch_flags |= GEN_BATCH_DECODE_IN_COLOR;
174 if (option_full_decode)
175 batch_flags |= GEN_BATCH_DECODE_FULL;
176 if (option_print_offsets)
177 batch_flags |= GEN_BATCH_DECODE_OFFSETS;
178 batch_flags |= GEN_BATCH_DECODE_FLOATS;
179
180 gen_batch_decode_ctx_init(&batch_ctx, &devinfo, outfile, batch_flags,
181 xml_path, get_gen_batch_bo, NULL);
182
183 char *color = GREEN_HEADER, *reset_color = NORMAL;
184 if (option_color == COLOR_NEVER)
185 color = reset_color = "";
186
187 fprintf(outfile, "%sAubinator: Intel AUB file decoder.%-80s%s\n",
188 color, "", reset_color);
189
190 if (input_file)
191 fprintf(outfile, "File name: %s\n", input_file);
192
193 if (aub_pci_id)
194 fprintf(outfile, "PCI ID: 0x%x\n", aub_pci_id);
195
196 fprintf(outfile, "Application name: %s\n", app_name);
197
198 fprintf(outfile, "Decoding as: %s\n", gen_get_device_name(pci_id));
199
200 /* Throw in a new line before the first batch */
201 fprintf(outfile, "\n");
202 }
203
204 static void
205 handle_trace_header(uint32_t *p)
206 {
207 /* The intel_aubdump tool from IGT is kind enough to put a PCI-ID= tag in
208 * the AUB header comment. If the user hasn't specified a hardware
209 * generation, try to use the one from the AUB file.
210 */
211 uint32_t *end = p + (p[0] & 0xffff) + 2;
212 int aub_pci_id = 0;
213 if (end > &p[12] && p[12] > 0)
214 sscanf((char *)&p[13], "PCI-ID=%i", &aub_pci_id);
215
216 if (pci_id == 0)
217 pci_id = aub_pci_id;
218
219 char app_name[33];
220 strncpy(app_name, (char *)&p[2], 32);
221 app_name[32] = 0;
222
223 aubinator_init(aub_pci_id, app_name);
224 }
225
226 struct aub_file {
227 FILE *stream;
228
229 uint32_t *map, *end, *cursor;
230 uint32_t *mem_end;
231 };
232
233 static struct aub_file *
234 aub_file_open(const char *filename)
235 {
236 struct aub_file *file;
237 struct stat sb;
238 int fd;
239
240 file = calloc(1, sizeof *file);
241 fd = open(filename, O_RDONLY);
242 if (fd == -1) {
243 fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno));
244 exit(EXIT_FAILURE);
245 }
246
247 if (fstat(fd, &sb) == -1) {
248 fprintf(stderr, "stat failed: %s\n", strerror(errno));
249 exit(EXIT_FAILURE);
250 }
251
252 file->map = mmap(NULL, sb.st_size,
253 PROT_READ, MAP_SHARED, fd, 0);
254 if (file->map == MAP_FAILED) {
255 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
256 exit(EXIT_FAILURE);
257 }
258
259 close(fd);
260
261 file->cursor = file->map;
262 file->end = file->map + sb.st_size / 4;
263
264 return file;
265 }
266
267 static struct aub_file *
268 aub_file_stdin(void)
269 {
270 struct aub_file *file;
271
272 file = calloc(1, sizeof *file);
273 file->stream = stdin;
274
275 return file;
276 }
277
278 #define TYPE(dw) (((dw) >> 29) & 7)
279 #define OPCODE(dw) (((dw) >> 23) & 0x3f)
280 #define SUBOPCODE(dw) (((dw) >> 16) & 0x7f)
281
282 #define MAKE_HEADER(type, opcode, subopcode) \
283 (((type) << 29) | ((opcode) << 23) | ((subopcode) << 16))
284
285 #define TYPE_AUB 0x7
286
287 /* Classic AUB opcodes */
288 #define OPCODE_AUB 0x01
289 #define SUBOPCODE_HEADER 0x05
290 #define SUBOPCODE_BLOCK 0x41
291 #define SUBOPCODE_BMP 0x1e
292
293 /* Newer version AUB opcode */
294 #define OPCODE_NEW_AUB 0x2e
295 #define SUBOPCODE_VERSION 0x00
296 #define SUBOPCODE_REG_WRITE 0x03
297 #define SUBOPCODE_MEM_POLL 0x05
298 #define SUBOPCODE_MEM_WRITE 0x06
299
300 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
301
302 struct {
303 const char *name;
304 uint32_t gen;
305 } device_map[] = {
306 { "bwr", MAKE_GEN(4, 0) },
307 { "cln", MAKE_GEN(4, 0) },
308 { "blc", MAKE_GEN(4, 0) },
309 { "ctg", MAKE_GEN(4, 0) },
310 { "el", MAKE_GEN(4, 0) },
311 { "il", MAKE_GEN(4, 0) },
312 { "sbr", MAKE_GEN(6, 0) },
313 { "ivb", MAKE_GEN(7, 0) },
314 { "lrb2", MAKE_GEN(0, 0) },
315 { "hsw", MAKE_GEN(7, 5) },
316 { "vlv", MAKE_GEN(7, 0) },
317 { "bdw", MAKE_GEN(8, 0) },
318 { "skl", MAKE_GEN(9, 0) },
319 { "chv", MAKE_GEN(8, 0) },
320 { "bxt", MAKE_GEN(9, 0) },
321 { "cnl", MAKE_GEN(10, 0) },
322 };
323
324 enum {
325 AUB_ITEM_DECODE_OK,
326 AUB_ITEM_DECODE_FAILED,
327 AUB_ITEM_DECODE_NEED_MORE_DATA,
328 };
329
330 static int
331 aub_file_decode_batch(struct aub_file *file)
332 {
333 uint32_t *p, h, device, data_type, *new_cursor;
334 int header_length, bias;
335
336 if (file->end - file->cursor < 1)
337 return AUB_ITEM_DECODE_NEED_MORE_DATA;
338
339 p = file->cursor;
340 h = *p;
341 header_length = h & 0xffff;
342
343 switch (OPCODE(h)) {
344 case OPCODE_AUB:
345 bias = 2;
346 break;
347 case OPCODE_NEW_AUB:
348 bias = 1;
349 break;
350 default:
351 fprintf(outfile, "unknown opcode %d at %td/%td\n",
352 OPCODE(h), file->cursor - file->map,
353 file->end - file->map);
354 return AUB_ITEM_DECODE_FAILED;
355 }
356
357 new_cursor = p + header_length + bias;
358 if ((h & 0xffff0000) == MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BLOCK)) {
359 if (file->end - file->cursor < 4)
360 return AUB_ITEM_DECODE_NEED_MORE_DATA;
361 new_cursor += p[4] / 4;
362 }
363
364 if (new_cursor > file->end)
365 return AUB_ITEM_DECODE_NEED_MORE_DATA;
366
367 switch (h & 0xffff0000) {
368 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_HEADER):
369 handle_trace_header(p);
370 break;
371 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BLOCK):
372 handle_trace_block(p);
373 break;
374 case MAKE_HEADER(TYPE_AUB, OPCODE_AUB, SUBOPCODE_BMP):
375 break;
376 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_VERSION):
377 fprintf(outfile, "version block: dw1 %08x\n", p[1]);
378 device = (p[1] >> 8) & 0xff;
379 fprintf(outfile, " device %s\n", device_map[device].name);
380 break;
381 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_REG_WRITE):
382 fprintf(outfile, "register write block: (dwords %d)\n", h & 0xffff);
383 fprintf(outfile, " reg 0x%x, data 0x%x\n", p[1], p[5]);
384 break;
385 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_WRITE):
386 fprintf(outfile, "memory write block (dwords %d):\n", h & 0xffff);
387 fprintf(outfile, " address 0x%"PRIx64"\n", *(uint64_t *) &p[1]);
388 data_type = (p[3] >> 20) & 0xff;
389 if (data_type != 0)
390 fprintf(outfile, " data type 0x%x\n", data_type);
391 fprintf(outfile, " address space 0x%x\n", (p[3] >> 28) & 0xf);
392 break;
393 case MAKE_HEADER(TYPE_AUB, OPCODE_NEW_AUB, SUBOPCODE_MEM_POLL):
394 fprintf(outfile, "memory poll block (dwords %d):\n", h & 0xffff);
395 break;
396 default:
397 fprintf(outfile, "unknown block type=0x%x, opcode=0x%x, "
398 "subopcode=0x%x (%08x)\n", TYPE(h), OPCODE(h), SUBOPCODE(h), h);
399 break;
400 }
401 file->cursor = new_cursor;
402
403 return AUB_ITEM_DECODE_OK;
404 }
405
406 static int
407 aub_file_more_stuff(struct aub_file *file)
408 {
409 return file->cursor < file->end || (file->stream && !feof(file->stream));
410 }
411
412 #define AUB_READ_BUFFER_SIZE (4096)
413 #define MAX(a, b) ((a) < (b) ? (b) : (a))
414
415 static void
416 aub_file_data_grow(struct aub_file *file)
417 {
418 size_t old_size = (file->mem_end - file->map) * 4;
419 size_t new_size = MAX(old_size * 2, AUB_READ_BUFFER_SIZE);
420 uint32_t *new_start = realloc(file->map, new_size);
421
422 file->cursor = new_start + (file->cursor - file->map);
423 file->end = new_start + (file->end - file->map);
424 file->map = new_start;
425 file->mem_end = file->map + (new_size / 4);
426 }
427
428 static bool
429 aub_file_data_load(struct aub_file *file)
430 {
431 size_t r;
432
433 if (file->stream == NULL)
434 return false;
435
436 /* First remove any consumed data */
437 if (file->cursor > file->map) {
438 memmove(file->map, file->cursor,
439 (file->end - file->cursor) * 4);
440 file->end -= file->cursor - file->map;
441 file->cursor = file->map;
442 }
443
444 /* Then load some new data in */
445 if ((file->mem_end - file->end) < (AUB_READ_BUFFER_SIZE / 4))
446 aub_file_data_grow(file);
447
448 r = fread(file->end, 1, (file->mem_end - file->end) * 4, file->stream);
449 file->end += r / 4;
450
451 return r != 0;
452 }
453
454 static void
455 setup_pager(void)
456 {
457 int fds[2];
458 pid_t pid;
459
460 if (!isatty(1))
461 return;
462
463 if (pipe(fds) == -1)
464 return;
465
466 pid = fork();
467 if (pid == -1)
468 return;
469
470 if (pid == 0) {
471 close(fds[1]);
472 dup2(fds[0], 0);
473 execlp("less", "less", "-FRSi", NULL);
474 }
475
476 close(fds[0]);
477 dup2(fds[1], 1);
478 close(fds[1]);
479 }
480
481 static void
482 print_help(const char *progname, FILE *file)
483 {
484 fprintf(file,
485 "Usage: %s [OPTION]... [FILE]\n"
486 "Decode aub file contents from either FILE or the standard input.\n\n"
487 "A valid --gen option must be provided.\n\n"
488 " --help display this help and exit\n"
489 " --gen=platform decode for given platform (ivb, byt, hsw, bdw, chv, skl, kbl, bxt or cnl)\n"
490 " --headers decode only command headers\n"
491 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
492 " if omitted), 'always', or 'never'\n"
493 " --no-pager don't launch pager\n"
494 " --no-offsets don't print instruction offsets\n"
495 " --xml=DIR load hardware xml description from directory DIR\n",
496 progname);
497 }
498
499 int main(int argc, char *argv[])
500 {
501 struct aub_file *file;
502 int c, i;
503 bool help = false, pager = true;
504 const struct {
505 const char *name;
506 int pci_id;
507 } gens[] = {
508 { "ilk", 0x0046 }, /* Intel(R) Ironlake Mobile */
509 { "snb", 0x0126 }, /* Intel(R) Sandybridge Mobile GT2 */
510 { "ivb", 0x0166 }, /* Intel(R) Ivybridge Mobile GT2 */
511 { "hsw", 0x0416 }, /* Intel(R) Haswell Mobile GT2 */
512 { "byt", 0x0155 }, /* Intel(R) Bay Trail */
513 { "bdw", 0x1616 }, /* Intel(R) HD Graphics 5500 (Broadwell GT2) */
514 { "chv", 0x22B3 }, /* Intel(R) HD Graphics (Cherryview) */
515 { "skl", 0x1912 }, /* Intel(R) HD Graphics 530 (Skylake GT2) */
516 { "kbl", 0x591D }, /* Intel(R) Kabylake GT2 */
517 { "bxt", 0x0A84 }, /* Intel(R) HD Graphics (Broxton) */
518 { "cnl", 0x5A52 }, /* Intel(R) HD Graphics (Cannonlake) */
519 };
520 const struct option aubinator_opts[] = {
521 { "help", no_argument, (int *) &help, true },
522 { "no-pager", no_argument, (int *) &pager, false },
523 { "no-offsets", no_argument, (int *) &option_print_offsets, false },
524 { "gen", required_argument, NULL, 'g' },
525 { "headers", no_argument, (int *) &option_full_decode, false },
526 { "color", required_argument, NULL, 'c' },
527 { "xml", required_argument, NULL, 'x' },
528 { NULL, 0, NULL, 0 }
529 };
530
531 outfile = stdout;
532
533 i = 0;
534 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
535 switch (c) {
536 case 'g':
537 for (i = 0; i < ARRAY_SIZE(gens); i++) {
538 if (!strcmp(optarg, gens[i].name)) {
539 pci_id = gens[i].pci_id;
540 break;
541 }
542 }
543 if (i == ARRAY_SIZE(gens)) {
544 fprintf(stderr, "can't parse gen: '%s', expected ivb, byt, hsw, "
545 "bdw, chv, skl, kbl or bxt\n", optarg);
546 exit(EXIT_FAILURE);
547 }
548 break;
549 case 'c':
550 if (optarg == NULL || strcmp(optarg, "always") == 0)
551 option_color = COLOR_ALWAYS;
552 else if (strcmp(optarg, "never") == 0)
553 option_color = COLOR_NEVER;
554 else if (strcmp(optarg, "auto") == 0)
555 option_color = COLOR_AUTO;
556 else {
557 fprintf(stderr, "invalid value for --color: %s", optarg);
558 exit(EXIT_FAILURE);
559 }
560 break;
561 case 'x':
562 xml_path = strdup(optarg);
563 break;
564 default:
565 break;
566 }
567 }
568
569 if (help || argc == 1) {
570 print_help(argv[0], stderr);
571 exit(0);
572 }
573
574 if (optind < argc)
575 input_file = argv[optind];
576
577 /* Do this before we redirect stdout to pager. */
578 if (option_color == COLOR_AUTO)
579 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
580
581 if (isatty(1) && pager)
582 setup_pager();
583
584 if (input_file == NULL)
585 file = aub_file_stdin();
586 else
587 file = aub_file_open(input_file);
588
589 /* mmap a terabyte for our gtt space. */
590 gtt_size = 1ull << 40;
591 gtt = mmap(NULL, gtt_size, PROT_READ | PROT_WRITE,
592 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
593 if (gtt == MAP_FAILED) {
594 fprintf(stderr, "failed to alloc gtt space: %s\n", strerror(errno));
595 exit(EXIT_FAILURE);
596 }
597
598 while (aub_file_more_stuff(file)) {
599 switch (aub_file_decode_batch(file)) {
600 case AUB_ITEM_DECODE_OK:
601 break;
602 case AUB_ITEM_DECODE_NEED_MORE_DATA:
603 if (!file->stream) {
604 file->cursor = file->end;
605 break;
606 }
607 if (aub_file_more_stuff(file) && !aub_file_data_load(file)) {
608 fprintf(stderr, "failed to load data from stdin\n");
609 exit(EXIT_FAILURE);
610 }
611 break;
612 default:
613 fprintf(stderr, "failed to parse aubdump data\n");
614 exit(EXIT_FAILURE);
615 }
616 }
617
618
619 fflush(stdout);
620 /* close the stdout which is opened to write the output */
621 close(1);
622 free(xml_path);
623
624 wait(NULL);
625
626 return EXIT_SUCCESS;
627 }