Path: blob/21.2-virgl/src/intel/tools/aubinator.c
4547 views
/*1* Copyright © 2016 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include <stdio.h>24#include <stdlib.h>25#include <stdint.h>26#include <stdbool.h>27#include <getopt.h>2829#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>3940#include "util/macros.h"4142#include "aub_read.h"43#include "aub_mem.h"4445#define CSI "\e["46#define GREEN_HEADER CSI "1;42m"47#define NORMAL CSI "0m"4849/* options */5051static int option_full_decode = true;52static int option_print_offsets = true;53static int max_vbo_lines = -1;54static enum { COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER } option_color;5556/* state */5758uint16_t pci_id = 0;59char *input_file = NULL, *xml_path = NULL;60struct intel_device_info devinfo;61struct intel_batch_decode_ctx batch_ctx;62struct aub_mem mem;6364FILE *outfile;6566struct brw_instruction;6768static void69aubinator_error(void *user_data, const void *aub_data, const char *msg)70{71fprintf(stderr, "%s", msg);72}7374static void75aubinator_init(void *user_data, int aub_pci_id, const char *app_name)76{77pci_id = aub_pci_id;7879if (!intel_get_device_info_from_pci_id(pci_id, &devinfo)) {80fprintf(stderr, "can't find device information: pci_id=0x%x\n", pci_id);81exit(EXIT_FAILURE);82}8384enum intel_batch_decode_flags batch_flags = 0;85if (option_color == COLOR_ALWAYS)86batch_flags |= INTEL_BATCH_DECODE_IN_COLOR;87if (option_full_decode)88batch_flags |= INTEL_BATCH_DECODE_FULL;89if (option_print_offsets)90batch_flags |= INTEL_BATCH_DECODE_OFFSETS;91batch_flags |= INTEL_BATCH_DECODE_FLOATS;9293intel_batch_decode_ctx_init(&batch_ctx, &devinfo, outfile, batch_flags,94xml_path, NULL, NULL, NULL);9596/* Check for valid spec instance, if wrong xml_path is passed then spec97* instance is not initialized properly98*/99if (!batch_ctx.spec) {100fprintf(stderr, "Failed to initialize intel_batch_decode_ctx "101"spec instance\n");102free(xml_path);103intel_batch_decode_ctx_finish(&batch_ctx);104exit(EXIT_FAILURE);105}106107batch_ctx.max_vbo_decoded_lines = max_vbo_lines;108109char *color = GREEN_HEADER, *reset_color = NORMAL;110if (option_color == COLOR_NEVER)111color = reset_color = "";112113fprintf(outfile, "%sAubinator: Intel AUB file decoder.%-80s%s\n",114color, "", reset_color);115116if (input_file)117fprintf(outfile, "File name: %s\n", input_file);118119if (aub_pci_id)120fprintf(outfile, "PCI ID: 0x%x\n", aub_pci_id);121122fprintf(outfile, "Application name: %s\n", app_name);123124fprintf(outfile, "Decoding as: %s\n", intel_get_device_name(pci_id));125126/* Throw in a new line before the first batch */127fprintf(outfile, "\n");128}129130static struct intel_batch_decode_bo131get_bo(void *user_data, bool ppgtt, uint64_t addr)132{133if (ppgtt)134return aub_mem_get_ppgtt_bo(user_data, addr);135else136return aub_mem_get_ggtt_bo(user_data, addr);137}138139static void140handle_execlist_write(void *user_data, enum drm_i915_gem_engine_class engine, uint64_t context_descriptor)141{142const uint32_t pphwsp_size = 4096;143uint32_t pphwsp_addr = context_descriptor & 0xfffff000;144struct intel_batch_decode_bo pphwsp_bo = aub_mem_get_ggtt_bo(&mem, pphwsp_addr);145uint32_t *context = (uint32_t *)((uint8_t *)pphwsp_bo.map +146(pphwsp_addr - pphwsp_bo.addr) +147pphwsp_size);148149uint32_t ring_buffer_head = context[5];150uint32_t ring_buffer_tail = context[7];151uint32_t ring_buffer_start = context[9];152uint32_t ring_buffer_length = (context[11] & 0x1ff000) + 4096;153154mem.pml4 = (uint64_t)context[49] << 32 | context[51];155batch_ctx.user_data = &mem;156157struct intel_batch_decode_bo ring_bo = aub_mem_get_ggtt_bo(&mem,158ring_buffer_start);159assert(ring_bo.size > 0);160void *commands = (uint8_t *)ring_bo.map + (ring_buffer_start - ring_bo.addr) + ring_buffer_head;161162batch_ctx.get_bo = get_bo;163164batch_ctx.engine = engine;165intel_print_batch(&batch_ctx, commands,166MIN2(ring_buffer_tail - ring_buffer_head, ring_buffer_length),167ring_bo.addr + ring_buffer_head, true);168aub_mem_clear_bo_maps(&mem);169}170171static struct intel_batch_decode_bo172get_legacy_bo(void *user_data, bool ppgtt, uint64_t addr)173{174return aub_mem_get_ggtt_bo(user_data, addr);175}176177static void178handle_ring_write(void *user_data, enum drm_i915_gem_engine_class engine,179const void *data, uint32_t data_len)180{181batch_ctx.user_data = &mem;182batch_ctx.get_bo = get_legacy_bo;183184batch_ctx.engine = engine;185intel_print_batch(&batch_ctx, data, data_len, 0, false);186187aub_mem_clear_bo_maps(&mem);188}189190struct aub_file {191FILE *stream;192193void *map, *end, *cursor;194};195196static struct aub_file *197aub_file_open(const char *filename)198{199struct aub_file *file;200struct stat sb;201int fd;202203file = calloc(1, sizeof *file);204if (file == NULL)205return NULL;206207fd = open(filename, O_RDONLY);208if (fd == -1) {209fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno));210free(file);211exit(EXIT_FAILURE);212}213214if (fstat(fd, &sb) == -1) {215fprintf(stderr, "stat failed: %s\n", strerror(errno));216free(file);217exit(EXIT_FAILURE);218}219220file->map = mmap(NULL, sb.st_size,221PROT_READ, MAP_SHARED, fd, 0);222if (file->map == MAP_FAILED) {223fprintf(stderr, "mmap failed: %s\n", strerror(errno));224free(file);225exit(EXIT_FAILURE);226}227228close(fd);229230file->cursor = file->map;231file->end = file->map + sb.st_size;232233return file;234}235236static int237aub_file_more_stuff(struct aub_file *file)238{239return file->cursor < file->end || (file->stream && !feof(file->stream));240}241242static void243setup_pager(void)244{245int fds[2];246pid_t pid;247248if (!isatty(1))249return;250251if (pipe(fds) == -1)252return;253254pid = fork();255if (pid == -1)256return;257258if (pid == 0) {259close(fds[1]);260dup2(fds[0], 0);261execlp("less", "less", "-FRSi", NULL);262}263264close(fds[0]);265dup2(fds[1], 1);266close(fds[1]);267}268269static void270print_help(const char *progname, FILE *file)271{272fprintf(file,273"Usage: %s [OPTION]... FILE\n"274"Decode aub file contents from FILE.\n\n"275" --help display this help and exit\n"276" --gen=platform decode for given platform (3 letter platform name)\n"277" --headers decode only command headers\n"278" --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"279" if omitted), 'always', or 'never'\n"280" --max-vbo-lines=N limit the number of decoded VBO lines\n"281" --no-pager don't launch pager\n"282" --no-offsets don't print instruction offsets\n"283" --xml=DIR load hardware xml description from directory DIR\n",284progname);285}286287int main(int argc, char *argv[])288{289struct aub_file *file;290int c, i;291bool help = false, pager = true;292const struct option aubinator_opts[] = {293{ "help", no_argument, (int *) &help, true },294{ "no-pager", no_argument, (int *) &pager, false },295{ "no-offsets", no_argument, (int *) &option_print_offsets, false },296{ "gen", required_argument, NULL, 'g' },297{ "headers", no_argument, (int *) &option_full_decode, false },298{ "color", optional_argument, NULL, 'c' },299{ "xml", required_argument, NULL, 'x' },300{ "max-vbo-lines", required_argument, NULL, 'v' },301{ NULL, 0, NULL, 0 }302};303304outfile = stdout;305306i = 0;307while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {308switch (c) {309case 'g': {310const int id = intel_device_name_to_pci_device_id(optarg);311if (id < 0) {312fprintf(stderr, "can't parse gen: '%s', expected brw, g4x, ilk, "313"snb, ivb, hsw, byt, bdw, chv, skl, bxt, kbl, "314"aml, glk, cfl, whl, cnl, icl", optarg);315exit(EXIT_FAILURE);316} else {317pci_id = id;318}319break;320}321case 'c':322if (optarg == NULL || strcmp(optarg, "always") == 0)323option_color = COLOR_ALWAYS;324else if (strcmp(optarg, "never") == 0)325option_color = COLOR_NEVER;326else if (strcmp(optarg, "auto") == 0)327option_color = COLOR_AUTO;328else {329fprintf(stderr, "invalid value for --color: %s", optarg);330exit(EXIT_FAILURE);331}332break;333case 'x':334xml_path = strdup(optarg);335break;336case 'v':337max_vbo_lines = atoi(optarg);338break;339default:340break;341}342}343344if (optind < argc)345input_file = argv[optind];346347if (help || !input_file) {348print_help(argv[0], stderr);349exit(0);350}351352/* Do this before we redirect stdout to pager. */353if (option_color == COLOR_AUTO)354option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;355356if (isatty(1) && pager)357setup_pager();358359if (!aub_mem_init(&mem)) {360fprintf(stderr, "Unable to create GTT\n");361exit(EXIT_FAILURE);362}363364file = aub_file_open(input_file);365if (!file) {366fprintf(stderr, "Unable to allocate buffer to open aub file\n");367free(xml_path);368exit(EXIT_FAILURE);369}370371struct aub_read aub_read = {372.user_data = &mem,373.error = aubinator_error,374.info = aubinator_init,375376.local_write = aub_mem_local_write,377.phys_write = aub_mem_phys_write,378.ggtt_write = aub_mem_ggtt_write,379.ggtt_entry_write = aub_mem_ggtt_entry_write,380381.execlist_write = handle_execlist_write,382.ring_write = handle_ring_write,383};384int consumed;385while (aub_file_more_stuff(file) &&386(consumed = aub_read_command(&aub_read, file->cursor,387file->end - file->cursor)) > 0) {388file->cursor += consumed;389}390391aub_mem_fini(&mem);392393fflush(stdout);394/* close the stdout which is opened to write the output */395close(1);396free(file);397free(xml_path);398399wait(NULL);400intel_batch_decode_ctx_finish(&batch_ctx);401402return EXIT_SUCCESS;403}404405406