2
* Copyright © 2018 Intel Corporation
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:
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
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
35
#include "util/list.h"
37
#include "aub_write.h"
38
#include "drm-uapi/i915_drm.h"
39
#include "intel_aub.h"
41
#define fail_if(cond, ...) _fail_if(cond, NULL, __VA_ARGS__)
43
#define fail(...) fail_if(true, __VA_ARGS__)
45
static int zlib_inflate(uint32_t **ptr, int len)
47
struct z_stream_s zstream;
49
const uint32_t out_size = 128*4096; /* approximate obj size */
51
memset(&zstream, 0, sizeof(zstream));
53
zstream.next_in = (unsigned char *)*ptr;
54
zstream.avail_in = 4*len;
56
if (inflateInit(&zstream) != Z_OK)
59
out = malloc(out_size);
60
zstream.next_out = out;
61
zstream.avail_out = out_size;
64
switch (inflate(&zstream, Z_SYNC_FLUSH)) {
74
if (zstream.avail_out)
77
out = realloc(out, 2*zstream.total_out);
83
zstream.next_out = (unsigned char *)out + zstream.total_out;
84
zstream.avail_out = zstream.total_out;
90
return zstream.total_out / 4;
93
static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
95
int len = 0, size = 1024;
97
*out = realloc(*out, sizeof(uint32_t)*size);
101
while (*in >= '!' && *in <= 'z') {
106
*out = realloc(*out, sizeof(uint32_t)*size);
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;
127
return zlib_inflate(out, len);
131
print_help(const char *progname, FILE *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",
160
enum drm_i915_gem_engine_class engine_class;
163
struct list_head link;
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,
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)
180
struct bo *new_bo = calloc(1, sizeof(*new_bo));
183
new_bo->engine_class = engine_class;
184
new_bo->engine_instance = engine_instance;
185
list_addtail(&new_bo->link, bo_list);
191
engine_from_name(const char *engine_name,
192
enum drm_i915_gem_engine_class *engine_class,
193
int *engine_instance)
197
enum drm_i915_gem_engine_class engine_class;
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 },
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);
218
*engine_instance = 0;
223
fail("Unknown engine %s\n", engine_name);
227
main(int argc, char *argv[])
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' },
240
while ((c = getopt_long(argc, argv, "ho:v", aubinator_opts, &i)) != -1) {
246
out_filename = strdup(optarg);
257
in_filename = argv[optind++];
259
if (help || argc == 1 || !in_filename) {
260
print_help(argv[0], stderr);
261
return in_filename ? EXIT_SUCCESS : EXIT_FAILURE;
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);
270
FILE *err_file = fopen(in_filename, "r");
271
fail_if(!err_file, "Failed to open error file \"%s\": %m\n", in_filename);
273
FILE *aub_file = fopen(out_filename, "w");
274
fail_if(!aub_file, "Failed to open aub file \"%s\": %m\n", in_filename);
276
struct aub_file aub = {};
278
enum drm_i915_gem_engine_class active_engine_class = I915_ENGINE_CLASS_INVALID;
279
int active_engine_instance = -1;
281
enum address_space active_gtt = PPGTT;
282
enum address_space default_gtt = PPGTT;
286
uint32_t ring_buffer_head;
287
uint32_t ring_buffer_tail;
289
} engines[I915_ENGINE_CLASS_VIDEO_ENHANCE + 1];
290
memset(engines, 0, sizeof(engines));
292
int num_ring_bos = 0;
294
struct list_head bo_list;
295
list_inithead(&bo_list);
297
struct bo *last_bo = NULL;
301
while (getline(&line, &line_size, err_file) > 0) {
302
const char *pci_id_start = strstr(line, "PCI ID");
305
int matched = sscanf(line, "PCI ID: 0x%04x\n", &pci_id);
306
fail_if(!matched, "Invalid error state file!\n");
308
aub_file_init(&aub, aub_file,
309
NULL, pci_id, "error_state");
311
aub.verbose_log_file = stdout;
312
default_gtt = active_gtt = aub_use_execlists(&aub) ? PPGTT : GGTT;
316
if (strstr(line, " command stream:")) {
317
engine_from_name(line, &active_engine_class, &active_engine_instance);
321
if (sscanf(line, " ring->head: 0x%x\n",
323
active_engine_class].instances[
324
active_engine_instance].ring_buffer_head) == 1) {
328
if (sscanf(line, " ring->tail: 0x%x\n",
330
active_engine_class].instances[
331
active_engine_instance].ring_buffer_tail) == 1) {
335
const char *active_start = "Active (";
336
if (strncmp(line, active_start, strlen(active_start)) == 0) {
337
char *ring = line + strlen(active_start);
339
engine_from_name(ring, &active_engine_class, &active_engine_instance);
340
active_gtt = default_gtt;
342
char *count = strchr(ring, '[');
343
fail_if(!count || sscanf(count, "[%d]:", &num_ring_bos) < 1,
344
"Failed to parse BO table header\n");
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;
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,
362
active_engine_instance);
363
bo_entry->size = size;
366
fail("Not enough BO entries in the active table\n");
371
if (line[0] == ':' || line[0] == '~') {
372
if (!last_bo || last_bo->type == BO_TYPE_UNKNOWN)
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;
381
char *dashes = strstr(line, " --- ");
385
engine_from_name(line, &active_engine_class, &active_engine_instance);
388
char *bo_address_str = strchr(dashes, '=');
389
if (!bo_address_str || sscanf(bo_address_str, "= 0x%08x %08x\n", &hi, &lo) != 2)
395
enum address_space gtt;
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 },
406
for (b = bo_types; b->type != BO_TYPE_UNKNOWN; b++) {
407
if (strncasecmp(dashes, b->match, strlen(b->match)) == 0)
411
last_bo = find_or_create(&bo_list, ((uint64_t) hi) << 32 | lo,
413
active_engine_class, active_engine_instance);
415
/* The batch buffer will appear twice as gtt_offset and user. Only
416
* keep the batch type.
418
if (last_bo->type == BO_TYPE_UNKNOWN) {
419
last_bo->type = b->type;
420
last_bo->name = b->match;
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);
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) {
443
fail_if(!batch_bo, "Failed to find batch buffer.\n");
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) {
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);
455
aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
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);
463
aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
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)) {
471
uint32_t *context = (uint32_t *) (bo_entry->data + 4096 /* GuC */ + 4096 /* HWSP */);
473
if (context[1] == 0) {
475
"Invalid context image data.\n"
476
"This is likely a kernel issue : https://bugs.freedesktop.org/show_bug.cgi?id=107691\n");
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]);
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.
488
context[49] = aub.pml4.phys_addr >> 32;
489
context[51] = aub.pml4.phys_addr & 0xffffffff;
491
fprintf(stdout, "context dump:\n");
492
for (int i = 0; i < 60; i++) {
494
fprintf(stdout, "\n 0x%08" PRIx64 ": ", bo_entry->addr + 8192 + i * 4);
495
fprintf(stdout, "0x%08x ", context[i]);
497
fprintf(stdout, "\n");
500
aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
502
case BO_TYPE_RINGBUFFER:
504
case BO_TYPE_CONTEXT_WA:
505
aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
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);
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);
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);
531
/* Use context id 0 -- if we are not using execlists it doesn't matter
534
aub_write_exec(&aub, 0, batch_bo->addr, 0, I915_ENGINE_CLASS_RENDER);
538
list_for_each_entry_safe(struct bo, bo_entry, &bo_list, link) {
539
list_del(&bo_entry->link);
540
free(bo_entry->data);
550
aub_file_finish(&aub);
551
} else if(aub_file) {
557
/* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/