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