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