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