anv: simplify push constant emissions
[mesa.git] / src / intel / tools / error2aub.c
1 /*
2 * Copyright © 2018 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
25 #include <assert.h>
26 #include <getopt.h>
27 #include <inttypes.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <zlib.h>
34
35 #include "util/list.h"
36
37 #include "aub_write.h"
38 #include "drm-uapi/i915_drm.h"
39 #include "intel_aub.h"
40
41 static void __attribute__ ((format(__printf__, 2, 3)))
42 fail_if(int cond, const char *format, ...)
43 {
44 va_list args;
45
46 if (!cond)
47 return;
48
49 va_start(args, format);
50 vfprintf(stderr, format, args);
51 va_end(args);
52
53 raise(SIGTRAP);
54 }
55
56 #define fail(...) fail_if(true, __VA_ARGS__)
57
58 static int zlib_inflate(uint32_t **ptr, int len)
59 {
60 struct z_stream_s zstream;
61 void *out;
62 const uint32_t out_size = 128*4096; /* approximate obj size */
63
64 memset(&zstream, 0, sizeof(zstream));
65
66 zstream.next_in = (unsigned char *)*ptr;
67 zstream.avail_in = 4*len;
68
69 if (inflateInit(&zstream) != Z_OK)
70 return 0;
71
72 out = malloc(out_size);
73 zstream.next_out = out;
74 zstream.avail_out = out_size;
75
76 do {
77 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
78 case Z_STREAM_END:
79 goto end;
80 case Z_OK:
81 break;
82 default:
83 inflateEnd(&zstream);
84 return 0;
85 }
86
87 if (zstream.avail_out)
88 break;
89
90 out = realloc(out, 2*zstream.total_out);
91 if (out == NULL) {
92 inflateEnd(&zstream);
93 return 0;
94 }
95
96 zstream.next_out = (unsigned char *)out + zstream.total_out;
97 zstream.avail_out = zstream.total_out;
98 } while (1);
99 end:
100 inflateEnd(&zstream);
101 free(*ptr);
102 *ptr = out;
103 return zstream.total_out / 4;
104 }
105
106 static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
107 {
108 int len = 0, size = 1024;
109
110 *out = realloc(*out, sizeof(uint32_t)*size);
111 if (*out == NULL)
112 return 0;
113
114 while (*in >= '!' && *in <= 'z') {
115 uint32_t v = 0;
116
117 if (len == size) {
118 size *= 2;
119 *out = realloc(*out, sizeof(uint32_t)*size);
120 if (*out == NULL)
121 return 0;
122 }
123
124 if (*in == 'z') {
125 in++;
126 } else {
127 v += in[0] - 33; v *= 85;
128 v += in[1] - 33; v *= 85;
129 v += in[2] - 33; v *= 85;
130 v += in[3] - 33; v *= 85;
131 v += in[4] - 33;
132 in += 5;
133 }
134 (*out)[len++] = v;
135 }
136
137 if (!inflate)
138 return len;
139
140 return zlib_inflate(out, len);
141 }
142
143 static void
144 print_help(const char *progname, FILE *file)
145 {
146 fprintf(file,
147 "Usage: %s [OPTION]... [FILE]\n"
148 "Convert an Intel GPU i915 error state to an aub file.\n"
149 " -h, --help display this help and exit\n"
150 " -o, --output=FILE the output aub file (default FILE.aub)\n",
151 progname);
152 }
153
154 struct bo {
155 enum address_space {
156 PPGTT,
157 GGTT,
158 } gtt;
159 enum bo_type {
160 BO_TYPE_UNKNOWN = 0,
161 BO_TYPE_BATCH,
162 BO_TYPE_USER,
163 BO_TYPE_CONTEXT,
164 BO_TYPE_RINGBUFFER,
165 BO_TYPE_STATUS,
166 BO_TYPE_CONTEXT_WA,
167 } type;
168 const char *name;
169 uint64_t addr;
170 uint8_t *data;
171 uint64_t size;
172
173 enum drm_i915_gem_engine_class engine_class;
174 int engine_instance;
175
176 struct list_head link;
177 };
178
179 static struct bo *
180 find_or_create(struct list_head *bo_list, uint64_t addr,
181 enum address_space gtt,
182 enum drm_i915_gem_engine_class engine_class,
183 int engine_instance)
184 {
185 list_for_each_entry(struct bo, bo_entry, bo_list, link) {
186 if (bo_entry->addr == addr &&
187 bo_entry->gtt == gtt &&
188 bo_entry->engine_class == engine_class &&
189 bo_entry->engine_instance == engine_instance)
190 return bo_entry;
191 }
192
193 struct bo *new_bo = calloc(1, sizeof(*new_bo));
194 new_bo->addr = addr;
195 new_bo->gtt = gtt;
196 new_bo->engine_class = engine_class;
197 new_bo->engine_instance = engine_instance;
198 list_addtail(&new_bo->link, bo_list);
199
200 return new_bo;
201 }
202
203 static void
204 engine_from_name(const char *engine_name,
205 enum drm_i915_gem_engine_class *engine_class,
206 int *engine_instance)
207 {
208 const struct {
209 const char *match;
210 enum drm_i915_gem_engine_class engine_class;
211 bool parse_instance;
212 } rings[] = {
213 { "rcs", I915_ENGINE_CLASS_RENDER, true },
214 { "vcs", I915_ENGINE_CLASS_VIDEO, true },
215 { "vecs", I915_ENGINE_CLASS_VIDEO_ENHANCE, true },
216 { "bcs", I915_ENGINE_CLASS_COPY, true },
217 { "global", I915_ENGINE_CLASS_INVALID, false },
218 { "render command stream", I915_ENGINE_CLASS_RENDER, false },
219 { "blt command stream", I915_ENGINE_CLASS_COPY, false },
220 { "bsd command stream", I915_ENGINE_CLASS_VIDEO, false },
221 { "vebox command stream", I915_ENGINE_CLASS_VIDEO_ENHANCE, false },
222 { NULL, I915_ENGINE_CLASS_INVALID },
223 }, *r;
224
225 for (r = rings; r->match; r++) {
226 if (strncasecmp(engine_name, r->match, strlen(r->match)) == 0) {
227 *engine_class = r->engine_class;
228 if (r->parse_instance)
229 *engine_instance = strtol(engine_name + strlen(r->match), NULL, 10);
230 else
231 *engine_instance = 0;
232 return;
233 }
234 }
235
236 fail("Unknown engine %s\n", engine_name);
237 }
238
239 int
240 main(int argc, char *argv[])
241 {
242 int i, c;
243 bool help = false, verbose;
244 char *out_filename = NULL, *in_filename = NULL;
245 const struct option aubinator_opts[] = {
246 { "help", no_argument, NULL, 'h' },
247 { "output", required_argument, NULL, 'o' },
248 { "verbose", no_argument, NULL, 'v' },
249 { NULL, 0, NULL, 0 }
250 };
251
252 i = 0;
253 while ((c = getopt_long(argc, argv, "ho:v", aubinator_opts, &i)) != -1) {
254 switch (c) {
255 case 'h':
256 help = true;
257 break;
258 case 'o':
259 out_filename = strdup(optarg);
260 break;
261 case 'v':
262 verbose = true;
263 break;
264 default:
265 break;
266 }
267 }
268
269 if (optind < argc)
270 in_filename = argv[optind++];
271
272 if (help || argc == 1 || !in_filename) {
273 print_help(argv[0], stderr);
274 return in_filename ? EXIT_SUCCESS : EXIT_FAILURE;
275 }
276
277 if (out_filename == NULL) {
278 int out_filename_size = strlen(in_filename) + 5;
279 out_filename = malloc(out_filename_size);
280 snprintf(out_filename, out_filename_size, "%s.aub", in_filename);
281 }
282
283 FILE *err_file = fopen(in_filename, "r");
284 fail_if(!err_file, "Failed to open error file \"%s\": %m\n", in_filename);
285
286 FILE *aub_file = fopen(out_filename, "w");
287 fail_if(!aub_file, "Failed to open aub file \"%s\": %m\n", in_filename);
288
289 struct aub_file aub = {};
290
291 enum drm_i915_gem_engine_class active_engine_class = I915_ENGINE_CLASS_INVALID;
292 int active_engine_instance = -1;
293
294 enum address_space active_gtt = PPGTT;
295 enum address_space default_gtt = PPGTT;
296
297 struct {
298 struct {
299 uint32_t ring_buffer_head;
300 uint32_t ring_buffer_tail;
301 } instances[3];
302 } engines[I915_ENGINE_CLASS_VIDEO_ENHANCE + 1];
303 memset(engines, 0, sizeof(engines));
304
305 int num_ring_bos = 0;
306
307 struct list_head bo_list;
308 list_inithead(&bo_list);
309
310 struct bo *last_bo = NULL;
311
312 char *line = NULL;
313 size_t line_size;
314 while (getline(&line, &line_size, err_file) > 0) {
315 const char *pci_id_start = strstr(line, "PCI ID");
316 if (pci_id_start) {
317 int pci_id;
318 int matched = sscanf(line, "PCI ID: 0x%04x\n", &pci_id);
319 fail_if(!matched, "Invalid error state file!\n");
320
321 aub_file_init(&aub, aub_file,
322 NULL, pci_id, "error_state");
323 if (verbose)
324 aub.verbose_log_file = stdout;
325 default_gtt = active_gtt = aub_use_execlists(&aub) ? PPGTT : GGTT;
326 continue;
327 }
328
329 if (strstr(line, " command stream:")) {
330 engine_from_name(line, &active_engine_class, &active_engine_instance);
331 continue;
332 }
333
334 if (sscanf(line, " ring->head: 0x%x\n",
335 &engines[
336 active_engine_class].instances[
337 active_engine_instance].ring_buffer_head) == 1) {
338 continue;
339 }
340
341 if (sscanf(line, " ring->tail: 0x%x\n",
342 &engines[
343 active_engine_class].instances[
344 active_engine_instance].ring_buffer_tail) == 1) {
345 continue;
346 }
347
348 const char *active_start = "Active (";
349 if (strncmp(line, active_start, strlen(active_start)) == 0) {
350 char *ring = line + strlen(active_start);
351
352 engine_from_name(ring, &active_engine_class, &active_engine_instance);
353 active_gtt = default_gtt;
354
355 char *count = strchr(ring, '[');
356 fail_if(!count || sscanf(count, "[%d]:", &num_ring_bos) < 1,
357 "Failed to parse BO table header\n");
358 continue;
359 }
360
361 const char *global_start = "Pinned (global) [";
362 if (strncmp(line, global_start, strlen(global_start)) == 0) {
363 active_engine_class = I915_ENGINE_CLASS_INVALID;
364 active_engine_instance = -1;
365 active_gtt = GGTT;
366 continue;
367 }
368
369 if (num_ring_bos > 0) {
370 unsigned hi, lo, size;
371 if (sscanf(line, " %x_%x %d", &hi, &lo, &size) == 3) {
372 struct bo *bo_entry = find_or_create(&bo_list, ((uint64_t)hi) << 32 | lo,
373 active_gtt,
374 active_engine_class,
375 active_engine_instance);
376 bo_entry->size = size;
377 num_ring_bos--;
378 } else {
379 fail("Not enough BO entries in the active table\n");
380 }
381 continue;
382 }
383
384 if (line[0] == ':' || line[0] == '~') {
385 if (!last_bo || last_bo->type == BO_TYPE_UNKNOWN)
386 continue;
387
388 int count = ascii85_decode(line+1, (uint32_t **) &last_bo->data, line[0] == ':');
389 fail_if(count == 0, "ASCII85 decode failed.\n");
390 last_bo->size = count * 4;
391 continue;
392 }
393
394 char *dashes = strstr(line, " --- ");
395 if (dashes) {
396 dashes += 5;
397
398 engine_from_name(line, &active_engine_class, &active_engine_instance);
399
400 uint32_t hi, lo;
401 char *bo_address_str = strchr(dashes, '=');
402 if (!bo_address_str || sscanf(bo_address_str, "= 0x%08x %08x\n", &hi, &lo) != 2)
403 continue;
404
405 const struct {
406 const char *match;
407 enum bo_type type;
408 enum address_space gtt;
409 } bo_types[] = {
410 { "gtt_offset", BO_TYPE_BATCH, default_gtt },
411 { "user", BO_TYPE_USER, default_gtt },
412 { "HW context", BO_TYPE_CONTEXT, GGTT },
413 { "ringbuffer", BO_TYPE_RINGBUFFER, GGTT },
414 { "HW Status", BO_TYPE_STATUS, GGTT },
415 { "WA context", BO_TYPE_CONTEXT_WA, GGTT },
416 { "unknown", BO_TYPE_UNKNOWN, GGTT },
417 }, *b;
418
419 for (b = bo_types; b->type != BO_TYPE_UNKNOWN; b++) {
420 if (strncasecmp(dashes, b->match, strlen(b->match)) == 0)
421 break;
422 }
423
424 last_bo = find_or_create(&bo_list, ((uint64_t) hi) << 32 | lo,
425 b->gtt,
426 active_engine_class, active_engine_instance);
427
428 /* The batch buffer will appear twice as gtt_offset and user. Only
429 * keep the batch type.
430 */
431 if (last_bo->type == BO_TYPE_UNKNOWN) {
432 last_bo->type = b->type;
433 last_bo->name = b->match;
434 }
435
436 continue;
437 }
438 }
439
440 if (verbose) {
441 fprintf(stdout, "BOs found:\n");
442 list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
443 fprintf(stdout, "\t type=%i addr=0x%016" PRIx64 " size=%" PRIu64 "\n",
444 bo_entry->type, bo_entry->addr, bo_entry->size);
445 }
446 }
447
448 /* Find the batch that trigger the hang */
449 struct bo *batch_bo = NULL;
450 list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
451 if (bo_entry->type == BO_TYPE_BATCH) {
452 batch_bo = bo_entry;
453 break;
454 }
455 }
456 fail_if(!batch_bo, "Failed to find batch buffer.\n");
457
458 /* Add all the BOs to the aub file */
459 struct bo *hwsp_bo = NULL;
460 list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
461 switch (bo_entry->type) {
462 case BO_TYPE_BATCH:
463 if (bo_entry->gtt == PPGTT) {
464 aub_map_ppgtt(&aub, bo_entry->addr, bo_entry->size);
465 aub_write_trace_block(&aub, AUB_TRACE_TYPE_BATCH,
466 bo_entry->data, bo_entry->size, bo_entry->addr);
467 } else
468 aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
469 break;
470 case BO_TYPE_USER:
471 if (bo_entry->gtt == PPGTT) {
472 aub_map_ppgtt(&aub, bo_entry->addr, bo_entry->size);
473 aub_write_trace_block(&aub, AUB_TRACE_TYPE_NOTYPE,
474 bo_entry->data, bo_entry->size, bo_entry->addr);
475 } else
476 aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
477 break;
478 case BO_TYPE_CONTEXT:
479 if (bo_entry->engine_class == batch_bo->engine_class &&
480 bo_entry->engine_instance == batch_bo->engine_instance &&
481 aub_use_execlists(&aub)) {
482 hwsp_bo = bo_entry;
483
484 uint32_t *context = (uint32_t *) (bo_entry->data + 4096 /* GuC */ + 4096 /* HWSP */);
485
486 if (context[1] == 0) {
487 fprintf(stderr,
488 "Invalid context image data.\n"
489 "This is likely a kernel issue : https://bugs.freedesktop.org/show_bug.cgi?id=107691\n");
490 }
491
492 /* Update the ring buffer at the last known location. */
493 context[5] = engines[bo_entry->engine_class].instances[bo_entry->engine_instance].ring_buffer_head;
494 context[7] = engines[bo_entry->engine_class].instances[bo_entry->engine_instance].ring_buffer_tail;
495 fprintf(stdout, "engine start=0x%x head/tail=0x%x/0x%x\n",
496 context[9], context[5], context[7]);
497
498 /* The error state doesn't provide a dump of the page tables, so
499 * we have to provide our own, that's easy enough.
500 */
501 context[49] = aub.pml4.phys_addr >> 32;
502 context[51] = aub.pml4.phys_addr & 0xffffffff;
503
504 fprintf(stdout, "context dump:\n");
505 for (int i = 0; i < 60; i++) {
506 if (i % 4 == 0)
507 fprintf(stdout, "\n 0x%08" PRIx64 ": ", bo_entry->addr + 8192 + i * 4);
508 fprintf(stdout, "0x%08x ", context[i]);
509 }
510 fprintf(stdout, "\n");
511
512 }
513 aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
514 break;
515 case BO_TYPE_RINGBUFFER:
516 case BO_TYPE_STATUS:
517 case BO_TYPE_CONTEXT_WA:
518 aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
519 break;
520 case BO_TYPE_UNKNOWN:
521 if (bo_entry->gtt == PPGTT) {
522 aub_map_ppgtt(&aub, bo_entry->addr, bo_entry->size);
523 if (bo_entry->data) {
524 aub_write_trace_block(&aub, AUB_TRACE_TYPE_NOTYPE,
525 bo_entry->data, bo_entry->size, bo_entry->addr);
526 }
527 } else {
528 if (bo_entry->size > 0) {
529 void *zero_data = calloc(1, bo_entry->size);
530 aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, zero_data);
531 free(zero_data);
532 }
533 }
534 break;
535 default:
536 break;
537 }
538 }
539
540 if (aub_use_execlists(&aub)) {
541 fail_if(!hwsp_bo, "Failed to find Context buffer.\n");
542 aub_write_context_execlists(&aub, hwsp_bo->addr + 4096 /* skip GuC page */, hwsp_bo->engine_class);
543 } else {
544 /* Use context id 0 -- if we are not using execlists it doesn't matter
545 * anyway
546 */
547 aub_write_exec(&aub, 0, batch_bo->addr, 0, I915_ENGINE_CLASS_RENDER);
548 }
549
550 /* Cleanup */
551 list_for_each_entry_safe(struct bo, bo_entry, &bo_list, link) {
552 list_del(&bo_entry->link);
553 free(bo_entry->data);
554 free(bo_entry);
555 }
556
557 free(out_filename);
558 free(line);
559 if(err_file) {
560 fclose(err_file);
561 }
562 if(aub.file) {
563 aub_file_finish(&aub);
564 } else if(aub_file) {
565 fclose(aub_file);
566 }
567 return EXIT_SUCCESS;
568 }
569
570 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/