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