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