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