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 #define fail_if(cond, ...) _fail_if(cond, NULL, __VA_ARGS__)
42
43 #define fail(...) fail_if(true, __VA_ARGS__)
44
zlib_inflate(uint32_t ** ptr,int len)45 static int zlib_inflate(uint32_t **ptr, int len)
46 {
47 struct z_stream_s zstream;
48 void *out;
49 const uint32_t out_size = 128*4096; /* approximate obj size */
50
51 memset(&zstream, 0, sizeof(zstream));
52
53 zstream.next_in = (unsigned char *)*ptr;
54 zstream.avail_in = 4*len;
55
56 if (inflateInit(&zstream) != Z_OK)
57 return 0;
58
59 out = malloc(out_size);
60 zstream.next_out = out;
61 zstream.avail_out = out_size;
62
63 do {
64 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
65 case Z_STREAM_END:
66 goto end;
67 case Z_OK:
68 break;
69 default:
70 inflateEnd(&zstream);
71 return 0;
72 }
73
74 if (zstream.avail_out)
75 break;
76
77 out = realloc(out, 2*zstream.total_out);
78 if (out == NULL) {
79 inflateEnd(&zstream);
80 return 0;
81 }
82
83 zstream.next_out = (unsigned char *)out + zstream.total_out;
84 zstream.avail_out = zstream.total_out;
85 } while (1);
86 end:
87 inflateEnd(&zstream);
88 free(*ptr);
89 *ptr = out;
90 return zstream.total_out / 4;
91 }
92
ascii85_decode(const char * in,uint32_t ** out,bool inflate)93 static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
94 {
95 int len = 0, size = 1024;
96
97 *out = realloc(*out, sizeof(uint32_t)*size);
98 if (*out == NULL)
99 return 0;
100
101 while (*in >= '!' && *in <= 'z') {
102 uint32_t v = 0;
103
104 if (len == size) {
105 size *= 2;
106 *out = realloc(*out, sizeof(uint32_t)*size);
107 if (*out == NULL)
108 return 0;
109 }
110
111 if (*in == 'z') {
112 in++;
113 } else {
114 v += in[0] - 33; v *= 85;
115 v += in[1] - 33; v *= 85;
116 v += in[2] - 33; v *= 85;
117 v += in[3] - 33; v *= 85;
118 v += in[4] - 33;
119 in += 5;
120 }
121 (*out)[len++] = v;
122 }
123
124 if (!inflate)
125 return len;
126
127 return zlib_inflate(out, len);
128 }
129
130 static void
print_help(const char * progname,FILE * file)131 print_help(const char *progname, FILE *file)
132 {
133 fprintf(file,
134 "Usage: %s [OPTION]... [FILE]\n"
135 "Convert an Intel GPU i915 error state to an aub file.\n"
136 " -h, --help display this help and exit\n"
137 " -o, --output=FILE the output aub file (default FILE.aub)\n",
138 progname);
139 }
140
141 struct bo {
142 enum address_space {
143 PPGTT,
144 GGTT,
145 } gtt;
146 enum bo_type {
147 BO_TYPE_UNKNOWN = 0,
148 BO_TYPE_BATCH,
149 BO_TYPE_USER,
150 BO_TYPE_CONTEXT,
151 BO_TYPE_RINGBUFFER,
152 BO_TYPE_STATUS,
153 BO_TYPE_CONTEXT_WA,
154 } type;
155 const char *name;
156 uint64_t addr;
157 uint8_t *data;
158 uint64_t size;
159
160 enum drm_i915_gem_engine_class engine_class;
161 int engine_instance;
162
163 struct list_head link;
164 };
165
166 static struct bo *
find_or_create(struct list_head * bo_list,uint64_t addr,enum address_space gtt,enum drm_i915_gem_engine_class engine_class,int engine_instance)167 find_or_create(struct list_head *bo_list, uint64_t addr,
168 enum address_space gtt,
169 enum drm_i915_gem_engine_class engine_class,
170 int engine_instance)
171 {
172 list_for_each_entry(struct bo, bo_entry, bo_list, link) {
173 if (bo_entry->addr == addr &&
174 bo_entry->gtt == gtt &&
175 bo_entry->engine_class == engine_class &&
176 bo_entry->engine_instance == engine_instance)
177 return bo_entry;
178 }
179
180 struct bo *new_bo = calloc(1, sizeof(*new_bo));
181 new_bo->addr = addr;
182 new_bo->gtt = gtt;
183 new_bo->engine_class = engine_class;
184 new_bo->engine_instance = engine_instance;
185 list_addtail(&new_bo->link, bo_list);
186
187 return new_bo;
188 }
189
190 static void
engine_from_name(const char * engine_name,enum drm_i915_gem_engine_class * engine_class,int * engine_instance)191 engine_from_name(const char *engine_name,
192 enum drm_i915_gem_engine_class *engine_class,
193 int *engine_instance)
194 {
195 const struct {
196 const char *match;
197 enum drm_i915_gem_engine_class engine_class;
198 bool parse_instance;
199 } rings[] = {
200 { "rcs", I915_ENGINE_CLASS_RENDER, true },
201 { "vcs", I915_ENGINE_CLASS_VIDEO, true },
202 { "vecs", I915_ENGINE_CLASS_VIDEO_ENHANCE, true },
203 { "bcs", I915_ENGINE_CLASS_COPY, true },
204 { "global", I915_ENGINE_CLASS_INVALID, false },
205 { "render command stream", I915_ENGINE_CLASS_RENDER, false },
206 { "blt command stream", I915_ENGINE_CLASS_COPY, false },
207 { "bsd command stream", I915_ENGINE_CLASS_VIDEO, false },
208 { "vebox command stream", I915_ENGINE_CLASS_VIDEO_ENHANCE, false },
209 { NULL, I915_ENGINE_CLASS_INVALID },
210 }, *r;
211
212 for (r = rings; r->match; r++) {
213 if (strncasecmp(engine_name, r->match, strlen(r->match)) == 0) {
214 *engine_class = r->engine_class;
215 if (r->parse_instance)
216 *engine_instance = strtol(engine_name + strlen(r->match), NULL, 10);
217 else
218 *engine_instance = 0;
219 return;
220 }
221 }
222
223 fail("Unknown engine %s\n", engine_name);
224 }
225
226 int
main(int argc,char * argv[])227 main(int argc, char *argv[])
228 {
229 int i, c;
230 bool help = false, verbose = false;
231 char *out_filename = NULL, *in_filename = NULL;
232 const struct option aubinator_opts[] = {
233 { "help", no_argument, NULL, 'h' },
234 { "output", required_argument, NULL, 'o' },
235 { "verbose", no_argument, NULL, 'v' },
236 { NULL, 0, NULL, 0 }
237 };
238
239 i = 0;
240 while ((c = getopt_long(argc, argv, "ho:v", aubinator_opts, &i)) != -1) {
241 switch (c) {
242 case 'h':
243 help = true;
244 break;
245 case 'o':
246 out_filename = strdup(optarg);
247 break;
248 case 'v':
249 verbose = true;
250 break;
251 default:
252 break;
253 }
254 }
255
256 if (optind < argc)
257 in_filename = argv[optind++];
258
259 if (help || argc == 1 || !in_filename) {
260 print_help(argv[0], stderr);
261 return in_filename ? EXIT_SUCCESS : EXIT_FAILURE;
262 }
263
264 if (out_filename == NULL) {
265 int out_filename_size = strlen(in_filename) + 5;
266 out_filename = malloc(out_filename_size);
267 snprintf(out_filename, out_filename_size, "%s.aub", in_filename);
268 }
269
270 FILE *err_file = fopen(in_filename, "r");
271 fail_if(!err_file, "Failed to open error file \"%s\": %m\n", in_filename);
272
273 FILE *aub_file = fopen(out_filename, "w");
274 fail_if(!aub_file, "Failed to open aub file \"%s\": %m\n", in_filename);
275
276 struct aub_file aub = {};
277
278 enum drm_i915_gem_engine_class active_engine_class = I915_ENGINE_CLASS_INVALID;
279 int active_engine_instance = -1;
280
281 enum address_space active_gtt = PPGTT;
282 enum address_space default_gtt = PPGTT;
283
284 struct {
285 struct {
286 uint32_t ring_buffer_head;
287 uint32_t ring_buffer_tail;
288 } instances[3];
289 } engines[I915_ENGINE_CLASS_VIDEO_ENHANCE + 1];
290 memset(engines, 0, sizeof(engines));
291
292 int num_ring_bos = 0;
293
294 struct list_head bo_list;
295 list_inithead(&bo_list);
296
297 struct bo *last_bo = NULL;
298
299 char *line = NULL;
300 size_t line_size;
301 while (getline(&line, &line_size, err_file) > 0) {
302 const char *pci_id_start = strstr(line, "PCI ID");
303 if (pci_id_start) {
304 int pci_id;
305 int matched = sscanf(line, "PCI ID: 0x%04x\n", &pci_id);
306 fail_if(!matched, "Invalid error state file!\n");
307
308 aub_file_init(&aub, aub_file,
309 NULL, pci_id, "error_state");
310 if (verbose)
311 aub.verbose_log_file = stdout;
312 default_gtt = active_gtt = aub_use_execlists(&aub) ? PPGTT : GGTT;
313 continue;
314 }
315
316 if (strstr(line, " command stream:")) {
317 engine_from_name(line, &active_engine_class, &active_engine_instance);
318 continue;
319 }
320
321 if (sscanf(line, " ring->head: 0x%x\n",
322 &engines[
323 active_engine_class].instances[
324 active_engine_instance].ring_buffer_head) == 1) {
325 continue;
326 }
327
328 if (sscanf(line, " ring->tail: 0x%x\n",
329 &engines[
330 active_engine_class].instances[
331 active_engine_instance].ring_buffer_tail) == 1) {
332 continue;
333 }
334
335 const char *active_start = "Active (";
336 if (strncmp(line, active_start, strlen(active_start)) == 0) {
337 char *ring = line + strlen(active_start);
338
339 engine_from_name(ring, &active_engine_class, &active_engine_instance);
340 active_gtt = default_gtt;
341
342 char *count = strchr(ring, '[');
343 fail_if(!count || sscanf(count, "[%d]:", &num_ring_bos) < 1,
344 "Failed to parse BO table header\n");
345 continue;
346 }
347
348 const char *global_start = "Pinned (global) [";
349 if (strncmp(line, global_start, strlen(global_start)) == 0) {
350 active_engine_class = I915_ENGINE_CLASS_INVALID;
351 active_engine_instance = -1;
352 active_gtt = GGTT;
353 continue;
354 }
355
356 if (num_ring_bos > 0) {
357 unsigned hi, lo, size;
358 if (sscanf(line, " %x_%x %d", &hi, &lo, &size) == 3) {
359 struct bo *bo_entry = find_or_create(&bo_list, ((uint64_t)hi) << 32 | lo,
360 active_gtt,
361 active_engine_class,
362 active_engine_instance);
363 bo_entry->size = size;
364 num_ring_bos--;
365 } else {
366 fail("Not enough BO entries in the active table\n");
367 }
368 continue;
369 }
370
371 if (line[0] == ':' || line[0] == '~') {
372 if (!last_bo || last_bo->type == BO_TYPE_UNKNOWN)
373 continue;
374
375 int count = ascii85_decode(line+1, (uint32_t **) &last_bo->data, line[0] == ':');
376 fail_if(count == 0, "ASCII85 decode failed.\n");
377 last_bo->size = count * 4;
378 continue;
379 }
380
381 char *dashes = strstr(line, " --- ");
382 if (dashes) {
383 dashes += 5;
384
385 engine_from_name(line, &active_engine_class, &active_engine_instance);
386
387 uint32_t hi, lo;
388 char *bo_address_str = strchr(dashes, '=');
389 if (!bo_address_str || sscanf(bo_address_str, "= 0x%08x %08x\n", &hi, &lo) != 2)
390 continue;
391
392 const struct {
393 const char *match;
394 enum bo_type type;
395 enum address_space gtt;
396 } bo_types[] = {
397 { "gtt_offset", 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, I915_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