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 <stdbool.h> 28#include <getopt.h> 29 30#include <unistd.h> 31#include <fcntl.h> 32#include <string.h> 33#include <signal.h> 34#include <errno.h> 35#include <inttypes.h> 36#include <sys/types.h> 37#include <sys/stat.h> 38#include <sys/wait.h> 39#include <sys/mman.h> 40 41#include "util/macros.h" 42 43#include "aub_read.h" 44#include "aub_mem.h" 45 46#define CSI "\e[" 47#define BLUE_HEADER CSI "0;44m" 48#define GREEN_HEADER CSI "1;42m" 49#define NORMAL CSI "0m" 50 51/* options */ 52 53static int option_full_decode = true; 54static int option_print_offsets = true; 55static int max_vbo_lines = -1; 56static enum { COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER } option_color; 57 58/* state */ 59 60uint16_t pci_id = 0; 61char *input_file = NULL, *xml_path = NULL; 62struct gen_device_info devinfo; 63struct gen_batch_decode_ctx batch_ctx; 64struct aub_mem mem; 65 66FILE *outfile; 67 68struct brw_instruction; 69 70static void 71aubinator_error(void *user_data, const void *aub_data, const char *msg) 72{ 73 fprintf(stderr, "%s", msg); 74} 75 76static void 77aubinator_init(void *user_data, int aub_pci_id, const char *app_name) 78{ 79 pci_id = aub_pci_id; 80 81 if (!gen_get_device_info(pci_id, &devinfo)) { 82 fprintf(stderr, "can't find device information: pci_id=0x%x\n", pci_id); 83 exit(EXIT_FAILURE); 84 } 85 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; 94 95 gen_batch_decode_ctx_init(&batch_ctx, &devinfo, outfile, batch_flags, 96 xml_path, NULL, NULL, NULL); 97 98 /* Check for valid spec instance, if wrong xml_path is passed then spec 99 * instance is not initialized properly 100 */ 101 if (!batch_ctx.spec) { 102 fprintf(stderr, "Failed to initialize gen_batch_decode_ctx " 103 "spec instance\n"); 104 free(xml_path); 105 gen_batch_decode_ctx_finish(&batch_ctx); 106 exit(EXIT_FAILURE); 107 } 108 109 batch_ctx.max_vbo_decoded_lines = max_vbo_lines; 110 111 char *color = GREEN_HEADER, *reset_color = NORMAL; 112 if (option_color == COLOR_NEVER) 113 color = reset_color = ""; 114 115 fprintf(outfile, "%sAubinator: Intel AUB file decoder.%-80s%s\n", 116 color, "", reset_color); 117 118 if (input_file) 119 fprintf(outfile, "File name: %s\n", input_file); 120 121 if (aub_pci_id) 122 fprintf(outfile, "PCI ID: 0x%x\n", aub_pci_id); 123 124 fprintf(outfile, "Application name: %s\n", app_name); 125 126 fprintf(outfile, "Decoding as: %s\n", gen_get_device_name(pci_id)); 127 128 /* Throw in a new line before the first batch */ 129 fprintf(outfile, "\n"); 130} 131 132static struct gen_batch_decode_bo 133get_bo(void *user_data, bool ppgtt, uint64_t addr) 134{ 135 if (ppgtt) 136 return aub_mem_get_ppgtt_bo(user_data, addr); 137 else 138 return aub_mem_get_ggtt_bo(user_data, addr); 139} 140 141static void 142handle_execlist_write(void *user_data, enum drm_i915_gem_engine_class engine, uint64_t context_descriptor) 143{ 144 const uint32_t pphwsp_size = 4096; 145 uint32_t pphwsp_addr = context_descriptor & 0xfffff000; 146 struct gen_batch_decode_bo pphwsp_bo = aub_mem_get_ggtt_bo(&mem, pphwsp_addr); 147 uint32_t *context = (uint32_t *)((uint8_t *)pphwsp_bo.map + 148 (pphwsp_addr - pphwsp_bo.addr) + 149 pphwsp_size); 150 151 uint32_t ring_buffer_head = context[5]; 152 uint32_t ring_buffer_tail = context[7]; 153 uint32_t ring_buffer_start = context[9]; 154 uint32_t ring_buffer_length = (context[11] & 0x1ff000) + 4096; 155 156 mem.pml4 = (uint64_t)context[49] << 32 | context[51]; 157 batch_ctx.user_data = &mem; 158 159 struct gen_batch_decode_bo ring_bo = aub_mem_get_ggtt_bo(&mem, 160 ring_buffer_start); 161 assert(ring_bo.size > 0); 162 void *commands = (uint8_t *)ring_bo.map + (ring_buffer_start - ring_bo.addr) + ring_buffer_head; 163 164 batch_ctx.get_bo = get_bo; 165 166 batch_ctx.engine = engine; 167 gen_print_batch(&batch_ctx, commands, 168 MIN2(ring_buffer_tail - ring_buffer_head, ring_buffer_length), 169 ring_bo.addr + ring_buffer_head, true); 170 aub_mem_clear_bo_maps(&mem); 171} 172 173static struct gen_batch_decode_bo 174get_legacy_bo(void *user_data, bool ppgtt, uint64_t addr) 175{ 176 return aub_mem_get_ggtt_bo(user_data, addr); 177} 178 179static void 180handle_ring_write(void *user_data, enum drm_i915_gem_engine_class engine, 181 const void *data, uint32_t data_len) 182{ 183 batch_ctx.user_data = &mem; 184 batch_ctx.get_bo = get_legacy_bo; 185 186 batch_ctx.engine = engine; 187 gen_print_batch(&batch_ctx, data, data_len, 0, false); 188 189 aub_mem_clear_bo_maps(&mem); 190} 191 192struct aub_file { 193 FILE *stream; 194 195 void *map, *end, *cursor; 196}; 197 198static struct aub_file * 199aub_file_open(const char *filename) 200{ 201 struct aub_file *file; 202 struct stat sb; 203 int fd; 204 205 file = calloc(1, sizeof *file); 206 if (file == NULL) 207 return NULL; 208 209 fd = open(filename, O_RDONLY); 210 if (fd == -1) { 211 fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno)); 212 free(file); 213 exit(EXIT_FAILURE); 214 } 215 216 if (fstat(fd, &sb) == -1) { 217 fprintf(stderr, "stat failed: %s\n", strerror(errno)); 218 free(file); 219 exit(EXIT_FAILURE); 220 } 221 222 file->map = mmap(NULL, sb.st_size, 223 PROT_READ, MAP_SHARED, fd, 0); 224 if (file->map == MAP_FAILED) { 225 fprintf(stderr, "mmap failed: %s\n", strerror(errno)); 226 free(file); 227 exit(EXIT_FAILURE); 228 } 229 230 close(fd); 231 232 file->cursor = file->map; 233 file->end = file->map + sb.st_size; 234 235 return file; 236} 237 238static int 239aub_file_more_stuff(struct aub_file *file) 240{ 241 return file->cursor < file->end || (file->stream && !feof(file->stream)); 242} 243 244static void 245setup_pager(void) 246{ 247 int fds[2]; 248 pid_t pid; 249 250 if (!isatty(1)) 251 return; 252 253 if (pipe(fds) == -1) 254 return; 255 256 pid = fork(); 257 if (pid == -1) 258 return; 259 260 if (pid == 0) { 261 close(fds[1]); 262 dup2(fds[0], 0); 263 execlp("less", "less", "-FRSi", NULL); 264 } 265 266 close(fds[0]); 267 dup2(fds[1], 1); 268 close(fds[1]); 269} 270 271static void 272print_help(const char *progname, FILE *file) 273{ 274 fprintf(file, 275 "Usage: %s [OPTION]... FILE\n" 276 "Decode aub file contents from FILE.\n\n" 277 " --help display this help and exit\n" 278 " --gen=platform decode for given platform (3 letter platform name)\n" 279 " --headers decode only command headers\n" 280 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n" 281 " if omitted), 'always', or 'never'\n" 282 " --max-vbo-lines=N limit the number of decoded VBO lines\n" 283 " --no-pager don't launch pager\n" 284 " --no-offsets don't print instruction offsets\n" 285 " --xml=DIR load hardware xml description from directory DIR\n", 286 progname); 287} 288 289int main(int argc, char *argv[]) 290{ 291 struct aub_file *file; 292 int c, i; 293 bool help = false, pager = true; 294 const struct option aubinator_opts[] = { 295 { "help", no_argument, (int *) &help, true }, 296 { "no-pager", no_argument, (int *) &pager, false }, 297 { "no-offsets", no_argument, (int *) &option_print_offsets, false }, 298 { "gen", required_argument, NULL, 'g' }, 299 { "headers", no_argument, (int *) &option_full_decode, false }, 300 { "color", required_argument, NULL, 'c' }, 301 { "xml", required_argument, NULL, 'x' }, 302 { "max-vbo-lines", required_argument, NULL, 'v' }, 303 { NULL, 0, NULL, 0 } 304 }; 305 306 outfile = stdout; 307 308 i = 0; 309 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) { 310 switch (c) { 311 case 'g': { 312 const int id = gen_device_name_to_pci_device_id(optarg); 313 if (id < 0) { 314 fprintf(stderr, "can't parse gen: '%s', expected brw, g4x, ilk, " 315 "snb, ivb, hsw, byt, bdw, chv, skl, bxt, kbl, " 316 "aml, glk, cfl, whl, cnl, icl", optarg); 317 exit(EXIT_FAILURE); 318 } else { 319 pci_id = id; 320 } 321 break; 322 } 323 case 'c': 324 if (optarg == NULL || strcmp(optarg, "always") == 0) 325 option_color = COLOR_ALWAYS; 326 else if (strcmp(optarg, "never") == 0) 327 option_color = COLOR_NEVER; 328 else if (strcmp(optarg, "auto") == 0) 329 option_color = COLOR_AUTO; 330 else { 331 fprintf(stderr, "invalid value for --color: %s", optarg); 332 exit(EXIT_FAILURE); 333 } 334 break; 335 case 'x': 336 xml_path = strdup(optarg); 337 break; 338 case 'v': 339 max_vbo_lines = atoi(optarg); 340 break; 341 default: 342 break; 343 } 344 } 345 346 if (optind < argc) 347 input_file = argv[optind]; 348 349 if (help || !input_file) { 350 print_help(argv[0], stderr); 351 exit(0); 352 } 353 354 /* Do this before we redirect stdout to pager. */ 355 if (option_color == COLOR_AUTO) 356 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER; 357 358 if (isatty(1) && pager) 359 setup_pager(); 360 361 if (!aub_mem_init(&mem)) { 362 fprintf(stderr, "Unable to create GTT\n"); 363 exit(EXIT_FAILURE); 364 } 365 366 file = aub_file_open(input_file); 367 if (!file) { 368 fprintf(stderr, "Unable to allocate buffer to open aub file\n"); 369 free(xml_path); 370 exit(EXIT_FAILURE); 371 } 372 373 struct aub_read aub_read = { 374 .user_data = &mem, 375 .error = aubinator_error, 376 .info = aubinator_init, 377 378 .local_write = aub_mem_local_write, 379 .phys_write = aub_mem_phys_write, 380 .ggtt_write = aub_mem_ggtt_write, 381 .ggtt_entry_write = aub_mem_ggtt_entry_write, 382 383 .execlist_write = handle_execlist_write, 384 .ring_write = handle_ring_write, 385 }; 386 int consumed; 387 while (aub_file_more_stuff(file) && 388 (consumed = aub_read_command(&aub_read, file->cursor, 389 file->end - file->cursor)) > 0) { 390 file->cursor += consumed; 391 } 392 393 aub_mem_fini(&mem); 394 395 fflush(stdout); 396 /* close the stdout which is opened to write the output */ 397 close(1); 398 free(file); 399 free(xml_path); 400 401 wait(NULL); 402 gen_batch_decode_ctx_finish(&batch_ctx); 403 404 return EXIT_SUCCESS; 405} 406