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