1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2016 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include <stdio.h>
25bf215546Sopenharmony_ci#include <stdlib.h>
26bf215546Sopenharmony_ci#include <stdint.h>
27bf215546Sopenharmony_ci#include <stdbool.h>
28bf215546Sopenharmony_ci#include <getopt.h>
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <unistd.h>
31bf215546Sopenharmony_ci#include <fcntl.h>
32bf215546Sopenharmony_ci#include <string.h>
33bf215546Sopenharmony_ci#include <signal.h>
34bf215546Sopenharmony_ci#include <errno.h>
35bf215546Sopenharmony_ci#include <inttypes.h>
36bf215546Sopenharmony_ci#include <sys/types.h>
37bf215546Sopenharmony_ci#include <sys/stat.h>
38bf215546Sopenharmony_ci#include <sys/wait.h>
39bf215546Sopenharmony_ci#include <sys/mman.h>
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci#include "intel/compiler/brw_isa_info.h"
42bf215546Sopenharmony_ci#include "util/macros.h"
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci#include "aub_read.h"
45bf215546Sopenharmony_ci#include "aub_mem.h"
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci#define CSI "\e["
48bf215546Sopenharmony_ci#define GREEN_HEADER CSI "1;42m"
49bf215546Sopenharmony_ci#define NORMAL       CSI "0m"
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci/* options */
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic int option_full_decode = true;
54bf215546Sopenharmony_cistatic int option_print_offsets = true;
55bf215546Sopenharmony_cistatic int max_vbo_lines = -1;
56bf215546Sopenharmony_cistatic enum { COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER } option_color;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci/* state */
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ciuint16_t pci_id = 0;
61bf215546Sopenharmony_cichar *input_file = NULL, *xml_path = NULL;
62bf215546Sopenharmony_cistruct intel_device_info devinfo;
63bf215546Sopenharmony_cistruct brw_isa_info isa;
64bf215546Sopenharmony_cistruct intel_batch_decode_ctx batch_ctx;
65bf215546Sopenharmony_cistruct aub_mem mem;
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ciFILE *outfile;
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_cistruct brw_instruction;
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_cistatic void
72bf215546Sopenharmony_ciaubinator_error(void *user_data, const void *aub_data, const char *msg)
73bf215546Sopenharmony_ci{
74bf215546Sopenharmony_ci   fprintf(stderr, "%s", msg);
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cistatic void
78bf215546Sopenharmony_ciaubinator_comment(void *user_data, const char *str)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci   fprintf(outfile, "%s\n", str);
81bf215546Sopenharmony_ci}
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistatic void
84bf215546Sopenharmony_ciaubinator_init(void *user_data, int aub_pci_id, const char *app_name)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   pci_id = aub_pci_id;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   if (!intel_get_device_info_from_pci_id(pci_id, &devinfo)) {
89bf215546Sopenharmony_ci      fprintf(stderr, "can't find device information: pci_id=0x%x\n", pci_id);
90bf215546Sopenharmony_ci      exit(EXIT_FAILURE);
91bf215546Sopenharmony_ci   }
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci   brw_init_isa_info(&isa, &devinfo);
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   enum intel_batch_decode_flags batch_flags = 0;
96bf215546Sopenharmony_ci   if (option_color == COLOR_ALWAYS)
97bf215546Sopenharmony_ci      batch_flags |= INTEL_BATCH_DECODE_IN_COLOR;
98bf215546Sopenharmony_ci   if (option_full_decode)
99bf215546Sopenharmony_ci      batch_flags |= INTEL_BATCH_DECODE_FULL;
100bf215546Sopenharmony_ci   if (option_print_offsets)
101bf215546Sopenharmony_ci      batch_flags |= INTEL_BATCH_DECODE_OFFSETS;
102bf215546Sopenharmony_ci   batch_flags |= INTEL_BATCH_DECODE_FLOATS;
103bf215546Sopenharmony_ci
104bf215546Sopenharmony_ci   intel_batch_decode_ctx_init(&batch_ctx, &isa, &devinfo, outfile,
105bf215546Sopenharmony_ci                               batch_flags, xml_path, NULL, NULL, NULL);
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_ci   /* Check for valid spec instance, if wrong xml_path is passed then spec
108bf215546Sopenharmony_ci    * instance is not initialized properly
109bf215546Sopenharmony_ci    */
110bf215546Sopenharmony_ci   if (!batch_ctx.spec) {
111bf215546Sopenharmony_ci      fprintf(stderr, "Failed to initialize intel_batch_decode_ctx "
112bf215546Sopenharmony_ci                      "spec instance\n");
113bf215546Sopenharmony_ci      free(xml_path);
114bf215546Sopenharmony_ci      intel_batch_decode_ctx_finish(&batch_ctx);
115bf215546Sopenharmony_ci      exit(EXIT_FAILURE);
116bf215546Sopenharmony_ci   }
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci   batch_ctx.max_vbo_decoded_lines = max_vbo_lines;
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci   char *color = GREEN_HEADER, *reset_color = NORMAL;
121bf215546Sopenharmony_ci   if (option_color == COLOR_NEVER)
122bf215546Sopenharmony_ci      color = reset_color = "";
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_ci   fprintf(outfile, "%sAubinator: Intel AUB file decoder.%-80s%s\n",
125bf215546Sopenharmony_ci           color, "", reset_color);
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   if (input_file)
128bf215546Sopenharmony_ci      fprintf(outfile, "File name:        %s\n", input_file);
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   if (aub_pci_id)
131bf215546Sopenharmony_ci      fprintf(outfile, "PCI ID:           0x%x\n", aub_pci_id);
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   fprintf(outfile, "Application name: %s\n", app_name);
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   fprintf(outfile, "Decoding as:      %s\n", devinfo.name);
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   /* Throw in a new line before the first batch */
138bf215546Sopenharmony_ci   fprintf(outfile, "\n");
139bf215546Sopenharmony_ci}
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_cistatic struct intel_batch_decode_bo
142bf215546Sopenharmony_ciget_bo(void *user_data, bool ppgtt, uint64_t addr)
143bf215546Sopenharmony_ci{
144bf215546Sopenharmony_ci   if (ppgtt)
145bf215546Sopenharmony_ci      return aub_mem_get_ppgtt_bo(user_data, addr);
146bf215546Sopenharmony_ci   else
147bf215546Sopenharmony_ci      return aub_mem_get_ggtt_bo(user_data, addr);
148bf215546Sopenharmony_ci}
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_cistatic void
151bf215546Sopenharmony_cihandle_execlist_write(void *user_data, enum drm_i915_gem_engine_class engine, uint64_t context_descriptor)
152bf215546Sopenharmony_ci{
153bf215546Sopenharmony_ci   const uint32_t pphwsp_size = 4096;
154bf215546Sopenharmony_ci   uint32_t pphwsp_addr = context_descriptor & 0xfffff000;
155bf215546Sopenharmony_ci   struct intel_batch_decode_bo pphwsp_bo = aub_mem_get_ggtt_bo(&mem, pphwsp_addr);
156bf215546Sopenharmony_ci   uint32_t *context = (uint32_t *)((uint8_t *)pphwsp_bo.map +
157bf215546Sopenharmony_ci                                    (pphwsp_addr - pphwsp_bo.addr) +
158bf215546Sopenharmony_ci                                    pphwsp_size);
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci   uint32_t ring_buffer_head = context[5];
161bf215546Sopenharmony_ci   uint32_t ring_buffer_tail = context[7];
162bf215546Sopenharmony_ci   uint32_t ring_buffer_start = context[9];
163bf215546Sopenharmony_ci   uint32_t ring_buffer_length = (context[11] & 0x1ff000) + 4096;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   mem.pml4 = (uint64_t)context[49] << 32 | context[51];
166bf215546Sopenharmony_ci   batch_ctx.user_data = &mem;
167bf215546Sopenharmony_ci
168bf215546Sopenharmony_ci   struct intel_batch_decode_bo ring_bo = aub_mem_get_ggtt_bo(&mem,
169bf215546Sopenharmony_ci                                                              ring_buffer_start);
170bf215546Sopenharmony_ci   assert(ring_bo.size > 0);
171bf215546Sopenharmony_ci   void *commands = (uint8_t *)ring_bo.map + (ring_buffer_start - ring_bo.addr) + ring_buffer_head;
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci   batch_ctx.get_bo = get_bo;
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci   batch_ctx.engine = engine;
176bf215546Sopenharmony_ci   intel_print_batch(&batch_ctx, commands,
177bf215546Sopenharmony_ci                   MIN2(ring_buffer_tail - ring_buffer_head, ring_buffer_length),
178bf215546Sopenharmony_ci                   ring_bo.addr + ring_buffer_head, true);
179bf215546Sopenharmony_ci   aub_mem_clear_bo_maps(&mem);
180bf215546Sopenharmony_ci}
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_cistatic struct intel_batch_decode_bo
183bf215546Sopenharmony_ciget_legacy_bo(void *user_data, bool ppgtt, uint64_t addr)
184bf215546Sopenharmony_ci{
185bf215546Sopenharmony_ci   return aub_mem_get_ggtt_bo(user_data, addr);
186bf215546Sopenharmony_ci}
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_cistatic void
189bf215546Sopenharmony_cihandle_ring_write(void *user_data, enum drm_i915_gem_engine_class engine,
190bf215546Sopenharmony_ci                  const void *data, uint32_t data_len)
191bf215546Sopenharmony_ci{
192bf215546Sopenharmony_ci   batch_ctx.user_data = &mem;
193bf215546Sopenharmony_ci   batch_ctx.get_bo = get_legacy_bo;
194bf215546Sopenharmony_ci
195bf215546Sopenharmony_ci   batch_ctx.engine = engine;
196bf215546Sopenharmony_ci   intel_print_batch(&batch_ctx, data, data_len, 0, false);
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci   aub_mem_clear_bo_maps(&mem);
199bf215546Sopenharmony_ci}
200bf215546Sopenharmony_ci
201bf215546Sopenharmony_cistruct aub_file {
202bf215546Sopenharmony_ci   FILE *stream;
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci   void *map, *end, *cursor;
205bf215546Sopenharmony_ci};
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_cistatic struct aub_file *
208bf215546Sopenharmony_ciaub_file_open(const char *filename)
209bf215546Sopenharmony_ci{
210bf215546Sopenharmony_ci   struct aub_file *file;
211bf215546Sopenharmony_ci   struct stat sb;
212bf215546Sopenharmony_ci   int fd;
213bf215546Sopenharmony_ci
214bf215546Sopenharmony_ci   file = calloc(1, sizeof *file);
215bf215546Sopenharmony_ci   if (file == NULL)
216bf215546Sopenharmony_ci      return NULL;
217bf215546Sopenharmony_ci
218bf215546Sopenharmony_ci   fd = open(filename, O_RDONLY);
219bf215546Sopenharmony_ci   if (fd == -1) {
220bf215546Sopenharmony_ci      fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno));
221bf215546Sopenharmony_ci      free(file);
222bf215546Sopenharmony_ci      exit(EXIT_FAILURE);
223bf215546Sopenharmony_ci   }
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci   if (fstat(fd, &sb) == -1) {
226bf215546Sopenharmony_ci      fprintf(stderr, "stat failed: %s\n", strerror(errno));
227bf215546Sopenharmony_ci      free(file);
228bf215546Sopenharmony_ci      exit(EXIT_FAILURE);
229bf215546Sopenharmony_ci   }
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci   file->map = mmap(NULL, sb.st_size,
232bf215546Sopenharmony_ci                    PROT_READ, MAP_SHARED, fd, 0);
233bf215546Sopenharmony_ci   if (file->map == MAP_FAILED) {
234bf215546Sopenharmony_ci      fprintf(stderr, "mmap failed: %s\n", strerror(errno));
235bf215546Sopenharmony_ci      free(file);
236bf215546Sopenharmony_ci      exit(EXIT_FAILURE);
237bf215546Sopenharmony_ci   }
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci   close(fd);
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci   file->cursor = file->map;
242bf215546Sopenharmony_ci   file->end = file->map + sb.st_size;
243bf215546Sopenharmony_ci
244bf215546Sopenharmony_ci   return file;
245bf215546Sopenharmony_ci}
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_cistatic int
248bf215546Sopenharmony_ciaub_file_more_stuff(struct aub_file *file)
249bf215546Sopenharmony_ci{
250bf215546Sopenharmony_ci   return file->cursor < file->end || (file->stream && !feof(file->stream));
251bf215546Sopenharmony_ci}
252bf215546Sopenharmony_ci
253bf215546Sopenharmony_cistatic void
254bf215546Sopenharmony_cisetup_pager(void)
255bf215546Sopenharmony_ci{
256bf215546Sopenharmony_ci   int fds[2];
257bf215546Sopenharmony_ci   pid_t pid;
258bf215546Sopenharmony_ci
259bf215546Sopenharmony_ci   if (!isatty(1))
260bf215546Sopenharmony_ci      return;
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci   if (pipe(fds) == -1)
263bf215546Sopenharmony_ci      return;
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci   pid = fork();
266bf215546Sopenharmony_ci   if (pid == -1)
267bf215546Sopenharmony_ci      return;
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci   if (pid == 0) {
270bf215546Sopenharmony_ci      close(fds[1]);
271bf215546Sopenharmony_ci      dup2(fds[0], 0);
272bf215546Sopenharmony_ci      execlp("less", "less", "-FRSi", NULL);
273bf215546Sopenharmony_ci   }
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci   close(fds[0]);
276bf215546Sopenharmony_ci   dup2(fds[1], 1);
277bf215546Sopenharmony_ci   close(fds[1]);
278bf215546Sopenharmony_ci}
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_cistatic void
281bf215546Sopenharmony_ciprint_help(const char *progname, FILE *file)
282bf215546Sopenharmony_ci{
283bf215546Sopenharmony_ci   fprintf(file,
284bf215546Sopenharmony_ci           "Usage: %s [OPTION]... FILE\n"
285bf215546Sopenharmony_ci           "Decode aub file contents from FILE.\n\n"
286bf215546Sopenharmony_ci           "      --help             display this help and exit\n"
287bf215546Sopenharmony_ci           "      --gen=platform     decode for given platform (3 letter platform name)\n"
288bf215546Sopenharmony_ci           "      --headers          decode only command headers\n"
289bf215546Sopenharmony_ci           "      --color[=WHEN]     colorize the output; WHEN can be 'auto' (default\n"
290bf215546Sopenharmony_ci           "                         if omitted), 'always', or 'never'\n"
291bf215546Sopenharmony_ci           "      --max-vbo-lines=N  limit the number of decoded VBO lines\n"
292bf215546Sopenharmony_ci           "      --no-pager         don't launch pager\n"
293bf215546Sopenharmony_ci           "      --no-offsets       don't print instruction offsets\n"
294bf215546Sopenharmony_ci           "      --xml=DIR          load hardware xml description from directory DIR\n",
295bf215546Sopenharmony_ci           progname);
296bf215546Sopenharmony_ci}
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ciint main(int argc, char *argv[])
299bf215546Sopenharmony_ci{
300bf215546Sopenharmony_ci   struct aub_file *file;
301bf215546Sopenharmony_ci   int c, i;
302bf215546Sopenharmony_ci   bool help = false, pager = true;
303bf215546Sopenharmony_ci   const struct option aubinator_opts[] = {
304bf215546Sopenharmony_ci      { "help",          no_argument,       (int *) &help,                 true },
305bf215546Sopenharmony_ci      { "no-pager",      no_argument,       (int *) &pager,                false },
306bf215546Sopenharmony_ci      { "no-offsets",    no_argument,       (int *) &option_print_offsets, false },
307bf215546Sopenharmony_ci      { "gen",           required_argument, NULL,                          'g' },
308bf215546Sopenharmony_ci      { "headers",       no_argument,       (int *) &option_full_decode,   false },
309bf215546Sopenharmony_ci      { "color",         optional_argument, NULL,                          'c' },
310bf215546Sopenharmony_ci      { "xml",           required_argument, NULL,                          'x' },
311bf215546Sopenharmony_ci      { "max-vbo-lines", required_argument, NULL,                          'v' },
312bf215546Sopenharmony_ci      { NULL,            0,                 NULL,                          0 }
313bf215546Sopenharmony_ci   };
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci   outfile = stdout;
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci   i = 0;
318bf215546Sopenharmony_ci   while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
319bf215546Sopenharmony_ci      switch (c) {
320bf215546Sopenharmony_ci      case 'g': {
321bf215546Sopenharmony_ci         const int id = intel_device_name_to_pci_device_id(optarg);
322bf215546Sopenharmony_ci         if (id < 0) {
323bf215546Sopenharmony_ci            fprintf(stderr, "can't parse gen: '%s', expected lpt, brw, g4x, ilk, "
324bf215546Sopenharmony_ci                            "snb, ivb, hsw, byt, bdw, chv, skl, bxt, kbl, "
325bf215546Sopenharmony_ci                            "aml, glk, cfl, whl, cml, icl, ehl, jsl, tgl, "
326bf215546Sopenharmony_ci                            "rkl, dg1, adl, sg1, rpl, dg2\n", optarg);
327bf215546Sopenharmony_ci            exit(EXIT_FAILURE);
328bf215546Sopenharmony_ci         } else {
329bf215546Sopenharmony_ci            pci_id = id;
330bf215546Sopenharmony_ci         }
331bf215546Sopenharmony_ci         break;
332bf215546Sopenharmony_ci      }
333bf215546Sopenharmony_ci      case 'c':
334bf215546Sopenharmony_ci         if (optarg == NULL || strcmp(optarg, "always") == 0)
335bf215546Sopenharmony_ci            option_color = COLOR_ALWAYS;
336bf215546Sopenharmony_ci         else if (strcmp(optarg, "never") == 0)
337bf215546Sopenharmony_ci            option_color = COLOR_NEVER;
338bf215546Sopenharmony_ci         else if (strcmp(optarg, "auto") == 0)
339bf215546Sopenharmony_ci            option_color = COLOR_AUTO;
340bf215546Sopenharmony_ci         else {
341bf215546Sopenharmony_ci            fprintf(stderr, "invalid value for --color: %s", optarg);
342bf215546Sopenharmony_ci            exit(EXIT_FAILURE);
343bf215546Sopenharmony_ci         }
344bf215546Sopenharmony_ci         break;
345bf215546Sopenharmony_ci      case 'x':
346bf215546Sopenharmony_ci         xml_path = strdup(optarg);
347bf215546Sopenharmony_ci         break;
348bf215546Sopenharmony_ci      case 'v':
349bf215546Sopenharmony_ci         max_vbo_lines = atoi(optarg);
350bf215546Sopenharmony_ci         break;
351bf215546Sopenharmony_ci      default:
352bf215546Sopenharmony_ci         break;
353bf215546Sopenharmony_ci      }
354bf215546Sopenharmony_ci   }
355bf215546Sopenharmony_ci
356bf215546Sopenharmony_ci   if (optind < argc)
357bf215546Sopenharmony_ci      input_file = argv[optind];
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci   if (help || !input_file) {
360bf215546Sopenharmony_ci      print_help(argv[0], stderr);
361bf215546Sopenharmony_ci      exit(0);
362bf215546Sopenharmony_ci   }
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_ci   /* Do this before we redirect stdout to pager. */
365bf215546Sopenharmony_ci   if (option_color == COLOR_AUTO)
366bf215546Sopenharmony_ci      option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
367bf215546Sopenharmony_ci
368bf215546Sopenharmony_ci   if (isatty(1) && pager)
369bf215546Sopenharmony_ci      setup_pager();
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   if (!aub_mem_init(&mem)) {
372bf215546Sopenharmony_ci      fprintf(stderr, "Unable to create GTT\n");
373bf215546Sopenharmony_ci      exit(EXIT_FAILURE);
374bf215546Sopenharmony_ci   }
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci   file = aub_file_open(input_file);
377bf215546Sopenharmony_ci   if (!file) {
378bf215546Sopenharmony_ci      fprintf(stderr, "Unable to allocate buffer to open aub file\n");
379bf215546Sopenharmony_ci      free(xml_path);
380bf215546Sopenharmony_ci      exit(EXIT_FAILURE);
381bf215546Sopenharmony_ci   }
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci   struct aub_read aub_read = {
384bf215546Sopenharmony_ci      .user_data = &mem,
385bf215546Sopenharmony_ci      .error = aubinator_error,
386bf215546Sopenharmony_ci      .info = aubinator_init,
387bf215546Sopenharmony_ci      .comment = aubinator_comment,
388bf215546Sopenharmony_ci
389bf215546Sopenharmony_ci      .local_write = aub_mem_local_write,
390bf215546Sopenharmony_ci      .phys_write = aub_mem_phys_write,
391bf215546Sopenharmony_ci      .ggtt_write = aub_mem_ggtt_write,
392bf215546Sopenharmony_ci      .ggtt_entry_write = aub_mem_ggtt_entry_write,
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ci      .execlist_write = handle_execlist_write,
395bf215546Sopenharmony_ci      .ring_write = handle_ring_write,
396bf215546Sopenharmony_ci   };
397bf215546Sopenharmony_ci   int consumed;
398bf215546Sopenharmony_ci   while (aub_file_more_stuff(file) &&
399bf215546Sopenharmony_ci          (consumed = aub_read_command(&aub_read, file->cursor,
400bf215546Sopenharmony_ci                                       file->end - file->cursor)) > 0) {
401bf215546Sopenharmony_ci      file->cursor += consumed;
402bf215546Sopenharmony_ci   }
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci   aub_mem_fini(&mem);
405bf215546Sopenharmony_ci
406bf215546Sopenharmony_ci   fflush(stdout);
407bf215546Sopenharmony_ci   /* close the stdout which is opened to write the output */
408bf215546Sopenharmony_ci   close(1);
409bf215546Sopenharmony_ci   free(file);
410bf215546Sopenharmony_ci   free(xml_path);
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci   wait(NULL);
413bf215546Sopenharmony_ci   intel_batch_decode_ctx_finish(&batch_ctx);
414bf215546Sopenharmony_ci
415bf215546Sopenharmony_ci   return EXIT_SUCCESS;
416bf215546Sopenharmony_ci}
417