intel/aubinator: fix ring buffer pointer
[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 uint32_t ring_buffer_length = (context[11] & 0x1ff000) + 4096;
146
147 mem.pml4 = (uint64_t)context[49] << 32 | context[51];
148 batch_ctx.user_data = &mem;
149
150 struct gen_batch_decode_bo ring_bo = aub_mem_get_ggtt_bo(&mem,
151 ring_buffer_start);
152 assert(ring_bo.size > 0);
153 void *commands = (uint8_t *)ring_bo.map + (ring_buffer_start - ring_bo.addr) + ring_buffer_head;
154
155 if (context_descriptor & 0x100 /* ppgtt */) {
156 batch_ctx.get_bo = aub_mem_get_ppgtt_bo;
157 } else {
158 batch_ctx.get_bo = aub_mem_get_ggtt_bo;
159 }
160
161 batch_ctx.engine = engine;
162 gen_print_batch(&batch_ctx, commands,
163 MIN2(ring_buffer_tail - ring_buffer_head, ring_buffer_length),
164 ring_bo.addr + ring_buffer_head);
165 aub_mem_clear_bo_maps(&mem);
166 }
167
168 static void
169 handle_ring_write(void *user_data, enum drm_i915_gem_engine_class engine,
170 const void *data, uint32_t data_len)
171 {
172 batch_ctx.user_data = &mem;
173 batch_ctx.get_bo = aub_mem_get_ggtt_bo;
174
175 batch_ctx.engine = engine;
176 gen_print_batch(&batch_ctx, data, data_len, 0);
177
178 aub_mem_clear_bo_maps(&mem);
179 }
180
181 struct aub_file {
182 FILE *stream;
183
184 void *map, *end, *cursor;
185 };
186
187 static struct aub_file *
188 aub_file_open(const char *filename)
189 {
190 struct aub_file *file;
191 struct stat sb;
192 int fd;
193
194 file = calloc(1, sizeof *file);
195 if (file == NULL)
196 return NULL;
197
198 fd = open(filename, O_RDONLY);
199 if (fd == -1) {
200 fprintf(stderr, "open %s failed: %s\n", filename, strerror(errno));
201 free(file);
202 exit(EXIT_FAILURE);
203 }
204
205 if (fstat(fd, &sb) == -1) {
206 fprintf(stderr, "stat failed: %s\n", strerror(errno));
207 free(file);
208 exit(EXIT_FAILURE);
209 }
210
211 file->map = mmap(NULL, sb.st_size,
212 PROT_READ, MAP_SHARED, fd, 0);
213 if (file->map == MAP_FAILED) {
214 fprintf(stderr, "mmap failed: %s\n", strerror(errno));
215 free(file);
216 exit(EXIT_FAILURE);
217 }
218
219 close(fd);
220
221 file->cursor = file->map;
222 file->end = file->map + sb.st_size;
223
224 return file;
225 }
226
227 static int
228 aub_file_more_stuff(struct aub_file *file)
229 {
230 return file->cursor < file->end || (file->stream && !feof(file->stream));
231 }
232
233 static void
234 setup_pager(void)
235 {
236 int fds[2];
237 pid_t pid;
238
239 if (!isatty(1))
240 return;
241
242 if (pipe(fds) == -1)
243 return;
244
245 pid = fork();
246 if (pid == -1)
247 return;
248
249 if (pid == 0) {
250 close(fds[1]);
251 dup2(fds[0], 0);
252 execlp("less", "less", "-FRSi", NULL);
253 }
254
255 close(fds[0]);
256 dup2(fds[1], 1);
257 close(fds[1]);
258 }
259
260 static void
261 print_help(const char *progname, FILE *file)
262 {
263 fprintf(file,
264 "Usage: %s [OPTION]... FILE\n"
265 "Decode aub file contents from FILE.\n\n"
266 " --help display this help and exit\n"
267 " --gen=platform decode for given platform (3 letter platform name)\n"
268 " --headers decode only command headers\n"
269 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
270 " if omitted), 'always', or 'never'\n"
271 " --max-vbo-lines=N limit the number of decoded VBO lines\n"
272 " --no-pager don't launch pager\n"
273 " --no-offsets don't print instruction offsets\n"
274 " --xml=DIR load hardware xml description from directory DIR\n",
275 progname);
276 }
277
278 int main(int argc, char *argv[])
279 {
280 struct aub_file *file;
281 int c, i;
282 bool help = false, pager = true;
283 const struct option aubinator_opts[] = {
284 { "help", no_argument, (int *) &help, true },
285 { "no-pager", no_argument, (int *) &pager, false },
286 { "no-offsets", no_argument, (int *) &option_print_offsets, false },
287 { "gen", required_argument, NULL, 'g' },
288 { "headers", no_argument, (int *) &option_full_decode, false },
289 { "color", required_argument, NULL, 'c' },
290 { "xml", required_argument, NULL, 'x' },
291 { "max-vbo-lines", required_argument, NULL, 'v' },
292 { NULL, 0, NULL, 0 }
293 };
294
295 outfile = stdout;
296
297 i = 0;
298 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
299 switch (c) {
300 case 'g': {
301 const int id = gen_device_name_to_pci_device_id(optarg);
302 if (id < 0) {
303 fprintf(stderr, "can't parse gen: '%s', expected brw, g4x, ilk, "
304 "snb, ivb, hsw, byt, bdw, chv, skl, bxt, kbl, "
305 "aml, glk, cfl, whl, cnl, icl", optarg);
306 exit(EXIT_FAILURE);
307 } else {
308 pci_id = id;
309 }
310 break;
311 }
312 case 'c':
313 if (optarg == NULL || strcmp(optarg, "always") == 0)
314 option_color = COLOR_ALWAYS;
315 else if (strcmp(optarg, "never") == 0)
316 option_color = COLOR_NEVER;
317 else if (strcmp(optarg, "auto") == 0)
318 option_color = COLOR_AUTO;
319 else {
320 fprintf(stderr, "invalid value for --color: %s", optarg);
321 exit(EXIT_FAILURE);
322 }
323 break;
324 case 'x':
325 xml_path = strdup(optarg);
326 break;
327 case 'v':
328 max_vbo_lines = atoi(optarg);
329 break;
330 default:
331 break;
332 }
333 }
334
335 if (optind < argc)
336 input_file = argv[optind];
337
338 if (help || !input_file) {
339 print_help(argv[0], stderr);
340 exit(0);
341 }
342
343 /* Do this before we redirect stdout to pager. */
344 if (option_color == COLOR_AUTO)
345 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
346
347 if (isatty(1) && pager)
348 setup_pager();
349
350 if (!aub_mem_init(&mem)) {
351 fprintf(stderr, "Unable to create GTT\n");
352 exit(EXIT_FAILURE);
353 }
354
355 file = aub_file_open(input_file);
356 if (!file) {
357 fprintf(stderr, "Unable to allocate buffer to open aub file\n");
358 free(xml_path);
359 exit(EXIT_FAILURE);
360 }
361
362 struct aub_read aub_read = {
363 .user_data = &mem,
364 .error = aubinator_error,
365 .info = aubinator_init,
366
367 .local_write = aub_mem_local_write,
368 .phys_write = aub_mem_phys_write,
369 .ggtt_write = aub_mem_ggtt_write,
370 .ggtt_entry_write = aub_mem_ggtt_entry_write,
371
372 .execlist_write = handle_execlist_write,
373 .ring_write = handle_ring_write,
374 };
375 int consumed;
376 while (aub_file_more_stuff(file) &&
377 (consumed = aub_read_command(&aub_read, file->cursor,
378 file->end - file->cursor)) > 0) {
379 file->cursor += consumed;
380 }
381
382 aub_mem_fini(&mem);
383
384 fflush(stdout);
385 /* close the stdout which is opened to write the output */
386 close(1);
387 free(file);
388 free(xml_path);
389
390 wait(NULL);
391 gen_batch_decode_ctx_finish(&batch_ctx);
392
393 return EXIT_SUCCESS;
394 }