intel/tools/error: Save error state sections and decode them later.
[mesa.git] / src / intel / tools / aubinator_error_decode.c
1 /*
2 * Copyright © 2007-2017 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 <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <inttypes.h>
32 #include <errno.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <err.h>
37 #include <assert.h>
38 #include <getopt.h>
39 #include <zlib.h>
40
41 #include "common/gen_decoder.h"
42 #include "util/macros.h"
43 #include "gen_disasm.h"
44
45 #define CSI "\e["
46 #define BLUE_HEADER CSI "0;44m"
47 #define GREEN_HEADER CSI "1;42m"
48 #define NORMAL CSI "0m"
49
50 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51
52 /* options */
53
54 static bool option_full_decode = true;
55 static bool option_print_offsets = true;
56 static enum { COLOR_AUTO, COLOR_ALWAYS, COLOR_NEVER } option_color;
57 static char *xml_path = NULL;
58
59 static uint32_t
60 print_head(unsigned int reg)
61 {
62 printf(" head = 0x%08x, wraps = %d\n", reg & (0x7ffff<<2), reg >> 21);
63 return reg & (0x7ffff<<2);
64 }
65
66 static void
67 print_register(struct gen_spec *spec, const char *name, uint32_t reg)
68 {
69 struct gen_group *reg_spec = gen_spec_find_register_by_name(spec, name);
70
71 if (reg_spec)
72 gen_print_group(stdout, reg_spec, 0, &reg, option_color == COLOR_ALWAYS);
73 }
74
75 struct ring_register_mapping {
76 const char *ring_name;
77 const char *register_name;
78 };
79
80 static const struct ring_register_mapping acthd_registers[] = {
81 { "blt", "BCS_ACTHD_UDW" },
82 { "bsd", "VCS_ACTHD_UDW" },
83 { "bsd2", "VCS2_ACTHD_UDW" },
84 { "render", "ACTHD_UDW" },
85 { "vebox", "VECS_ACTHD_UDW" },
86 };
87
88 static const struct ring_register_mapping ctl_registers[] = {
89 { "blt", "BCS_RING_BUFFER_CTL" },
90 { "bsd", "VCS_RING_BUFFER_CTL" },
91 { "bsd2", "VCS2_RING_BUFFER_CTL" },
92 { "render", "RCS_RING_BUFFER_CTL" },
93 { "vebox", "VECS_RING_BUFFER_CTL" },
94 };
95
96 static const struct ring_register_mapping fault_registers[] = {
97 { "blt", "BCS_FAULT_REG" },
98 { "bsd", "VCS_FAULT_REG" },
99 { "render", "RCS_FAULT_REG" },
100 { "vebox", "VECS_FAULT_REG" },
101 };
102
103 static const char *
104 register_name_from_ring(const struct ring_register_mapping *mapping,
105 unsigned nb_mapping,
106 const char *ring_name)
107 {
108 for (unsigned i = 0; i < nb_mapping; i++) {
109 if (strcmp(mapping[i].ring_name, ring_name) == 0)
110 return mapping[i].register_name;
111 }
112 return NULL;
113 }
114
115 static const char *
116 instdone_register_for_ring(const struct gen_device_info *devinfo,
117 const char *ring_name)
118 {
119 if (strcmp(ring_name, "blt") == 0)
120 return "BCS_INSTDONE";
121 else if (strcmp(ring_name, "vebox") == 0)
122 return "VECS_INSTDONE";
123 else if (strcmp(ring_name, "bsd") == 0)
124 return "VCS_INSTDONE";
125 else if (strcmp(ring_name, "render") == 0) {
126 if (devinfo->gen == 6)
127 return "INSTDONE_2";
128 return "INSTDONE_1";
129 }
130
131 return NULL;
132 }
133
134 static void
135 print_pgtbl_err(unsigned int reg, struct gen_device_info *devinfo)
136 {
137 if (reg & (1 << 26))
138 printf(" Invalid Sampler Cache GTT entry\n");
139 if (reg & (1 << 24))
140 printf(" Invalid Render Cache GTT entry\n");
141 if (reg & (1 << 23))
142 printf(" Invalid Instruction/State Cache GTT entry\n");
143 if (reg & (1 << 22))
144 printf(" There is no ROC, this cannot occur!\n");
145 if (reg & (1 << 21))
146 printf(" Invalid GTT entry during Vertex Fetch\n");
147 if (reg & (1 << 20))
148 printf(" Invalid GTT entry during Command Fetch\n");
149 if (reg & (1 << 19))
150 printf(" Invalid GTT entry during CS\n");
151 if (reg & (1 << 18))
152 printf(" Invalid GTT entry during Cursor Fetch\n");
153 if (reg & (1 << 17))
154 printf(" Invalid GTT entry during Overlay Fetch\n");
155 if (reg & (1 << 8))
156 printf(" Invalid GTT entry during Display B Fetch\n");
157 if (reg & (1 << 4))
158 printf(" Invalid GTT entry during Display A Fetch\n");
159 if (reg & (1 << 1))
160 printf(" Valid PTE references illegal memory\n");
161 if (reg & (1 << 0))
162 printf(" Invalid GTT entry during fetch for host\n");
163 }
164
165 static void
166 print_snb_fence(struct gen_device_info *devinfo, uint64_t fence)
167 {
168 printf(" %svalid, %c-tiled, pitch: %i, start: 0x%08x, size: %u\n",
169 fence & 1 ? "" : "in",
170 fence & (1<<1) ? 'y' : 'x',
171 (int)(((fence>>32)&0xfff)+1)*128,
172 (uint32_t)fence & 0xfffff000,
173 (uint32_t)(((fence>>32)&0xfffff000) - (fence&0xfffff000) + 4096));
174 }
175
176 static void
177 print_i965_fence(struct gen_device_info *devinfo, uint64_t fence)
178 {
179 printf(" %svalid, %c-tiled, pitch: %i, start: 0x%08x, size: %u\n",
180 fence & 1 ? "" : "in",
181 fence & (1<<1) ? 'y' : 'x',
182 (int)(((fence>>2)&0x1ff)+1)*128,
183 (uint32_t)fence & 0xfffff000,
184 (uint32_t)(((fence>>32)&0xfffff000) - (fence&0xfffff000) + 4096));
185 }
186
187 static void
188 print_fence(struct gen_device_info *devinfo, uint64_t fence)
189 {
190 if (devinfo->gen == 6 || devinfo->gen == 7) {
191 return print_snb_fence(devinfo, fence);
192 } else if (devinfo->gen == 4 || devinfo->gen == 5) {
193 return print_i965_fence(devinfo, fence);
194 }
195 }
196
197 static void
198 print_fault_data(struct gen_device_info *devinfo, uint32_t data1, uint32_t data0)
199 {
200 uint64_t address;
201
202 if (devinfo->gen < 8)
203 return;
204
205 address = ((uint64_t)(data0) << 12) | ((uint64_t)data1 & 0xf) << 44;
206 printf(" Address 0x%016" PRIx64 " %s\n", address,
207 data1 & (1 << 4) ? "GGTT" : "PPGTT");
208 }
209
210 #define CSI "\e["
211 #define NORMAL CSI "0m"
212
213 struct section {
214 uint64_t gtt_offset;
215 char *ring_name;
216 const char *buffer_name;
217 uint32_t *data;
218 int count;
219 };
220
221 #define MAX_SECTIONS 30
222 static struct section sections[MAX_SECTIONS];
223
224 struct program {
225 const char *type;
226 const char *command;
227 uint64_t command_offset;
228 uint64_t instruction_base_address;
229 uint64_t ksp;
230 };
231
232 #define MAX_NUM_PROGRAMS 4096
233 static struct program programs[MAX_NUM_PROGRAMS];
234 static int idx_program = 0, num_programs = 0;
235
236 static int next_program(void)
237 {
238 int ret = idx_program;
239 idx_program = (idx_program + 1) % MAX_NUM_PROGRAMS;
240 num_programs = MIN(num_programs + 1, MAX_NUM_PROGRAMS);
241 return ret;
242 }
243
244 static void
245 decode(struct gen_spec *spec, const struct section *section)
246 {
247 uint64_t gtt_offset = section->gtt_offset;
248 uint32_t *data = section->data;
249 uint32_t *p, *end = (data + section->count);
250 int length;
251 struct gen_group *inst;
252 uint64_t current_instruction_base_address = 0;
253
254 for (p = data; p < end; p += length) {
255 const char *color = option_full_decode ? BLUE_HEADER : NORMAL,
256 *reset_color = NORMAL;
257 uint64_t offset = gtt_offset + 4 * (p - data);
258
259 inst = gen_spec_find_instruction(spec, p);
260 length = gen_group_get_length(inst, p);
261 assert(inst == NULL || length > 0);
262 length = MAX2(1, length);
263 if (inst == NULL) {
264 printf("unknown instruction %08x\n", p[0]);
265 continue;
266 }
267 if (option_color == COLOR_NEVER) {
268 color = "";
269 reset_color = "";
270 }
271
272 printf("%s0x%08"PRIx64": 0x%08x: %-80s%s\n",
273 color, offset, p[0], gen_group_get_name(inst), reset_color);
274
275 gen_print_group(stdout, inst, offset, p,
276 option_color == COLOR_ALWAYS);
277
278 if (strcmp(inst->name, "MI_BATCH_BUFFER_END") == 0)
279 break;
280
281 if (strcmp(inst->name, "STATE_BASE_ADDRESS") == 0) {
282 struct gen_field_iterator iter;
283 gen_field_iterator_init(&iter, inst, p, false);
284
285 while (gen_field_iterator_next(&iter)) {
286 if (strcmp(iter.name, "Instruction Base Address") == 0) {
287 current_instruction_base_address = strtol(iter.value, NULL, 16);
288 }
289 }
290 } else if (strcmp(inst->name, "WM_STATE") == 0 ||
291 strcmp(inst->name, "3DSTATE_PS") == 0 ||
292 strcmp(inst->name, "3DSTATE_WM") == 0) {
293 struct gen_field_iterator iter;
294 gen_field_iterator_init(&iter, inst, p, false);
295 uint64_t ksp[3] = {0, 0, 0};
296 bool enabled[3] = {false, false, false};
297
298 while (gen_field_iterator_next(&iter)) {
299 if (strncmp(iter.name, "Kernel Start Pointer ",
300 strlen("Kernel Start Pointer ")) == 0) {
301 int idx = iter.name[strlen("Kernel Start Pointer ")] - '0';
302 ksp[idx] = strtol(iter.value, NULL, 16);
303 } else if (strcmp(iter.name, "8 Pixel Dispatch Enable") == 0) {
304 enabled[0] = strcmp(iter.value, "true") == 0;
305 } else if (strcmp(iter.name, "16 Pixel Dispatch Enable") == 0) {
306 enabled[1] = strcmp(iter.value, "true") == 0;
307 } else if (strcmp(iter.name, "32 Pixel Dispatch Enable") == 0) {
308 enabled[2] = strcmp(iter.value, "true") == 0;
309 }
310 }
311
312 /* FINISHME: Broken for multi-program WM_STATE,
313 * which Mesa does not use
314 */
315 if (enabled[0] + enabled[1] + enabled[2] == 1) {
316 const char *type = enabled[0] ? "SIMD8 fragment shader" :
317 enabled[1] ? "SIMD16 fragment shader" :
318 enabled[2] ? "SIMD32 fragment shader" : NULL;
319
320 programs[next_program()] = (struct program) {
321 .type = type,
322 .command = inst->name,
323 .command_offset = offset,
324 .instruction_base_address = current_instruction_base_address,
325 .ksp = ksp[0],
326 };
327 } else {
328 if (enabled[0]) /* SIMD8 */ {
329 programs[next_program()] = (struct program) {
330 .type = "SIMD8 fragment shader",
331 .command = inst->name,
332 .command_offset = offset,
333 .instruction_base_address = current_instruction_base_address,
334 .ksp = ksp[0], /* SIMD8 shader is specified by ksp[0] */
335 };
336 }
337 if (enabled[1]) /* SIMD16 */ {
338 programs[next_program()] = (struct program) {
339 .type = "SIMD16 fragment shader",
340 .command = inst->name,
341 .command_offset = offset,
342 .instruction_base_address = current_instruction_base_address,
343 .ksp = ksp[2], /* SIMD16 shader is specified by ksp[2] */
344 };
345 }
346 if (enabled[2]) /* SIMD32 */ {
347 programs[next_program()] = (struct program) {
348 .type = "SIMD32 fragment shader",
349 .command = inst->name,
350 .command_offset = offset,
351 .instruction_base_address = current_instruction_base_address,
352 .ksp = ksp[1], /* SIMD32 shader is specified by ksp[1] */
353 };
354 }
355 }
356 } else if (strcmp(inst->name, "VS_STATE") == 0 ||
357 strcmp(inst->name, "GS_STATE") == 0 ||
358 strcmp(inst->name, "SF_STATE") == 0 ||
359 strcmp(inst->name, "CLIP_STATE") == 0 ||
360 strcmp(inst->name, "3DSTATE_DS") == 0 ||
361 strcmp(inst->name, "3DSTATE_HS") == 0 ||
362 strcmp(inst->name, "3DSTATE_GS") == 0 ||
363 strcmp(inst->name, "3DSTATE_VS") == 0) {
364 struct gen_field_iterator iter;
365 gen_field_iterator_init(&iter, inst, p, false);
366 uint64_t ksp = 0;
367 bool is_simd8 = false; /* vertex shaders on Gen8+ only */
368 bool is_enabled = true;
369
370 while (gen_field_iterator_next(&iter)) {
371 if (strcmp(iter.name, "Kernel Start Pointer") == 0) {
372 ksp = strtol(iter.value, NULL, 16);
373 } else if (strcmp(iter.name, "SIMD8 Dispatch Enable") == 0) {
374 is_simd8 = strcmp(iter.value, "true") == 0;
375 } else if (strcmp(iter.name, "Dispatch Enable") == 0) {
376 is_simd8 = strcmp(iter.value, "SIMD8") == 0;
377 } else if (strcmp(iter.name, "Enable") == 0) {
378 is_enabled = strcmp(iter.value, "true") == 0;
379 }
380 }
381
382 const char *type =
383 strcmp(inst->name, "VS_STATE") == 0 ? "vertex shader" :
384 strcmp(inst->name, "GS_STATE") == 0 ? "geometry shader" :
385 strcmp(inst->name, "SF_STATE") == 0 ? "strips and fans shader" :
386 strcmp(inst->name, "CLIP_STATE") == 0 ? "clip shader" :
387 strcmp(inst->name, "3DSTATE_DS") == 0 ? "tessellation control shader" :
388 strcmp(inst->name, "3DSTATE_HS") == 0 ? "tessellation evaluation shader" :
389 strcmp(inst->name, "3DSTATE_VS") == 0 ? (is_simd8 ? "SIMD8 vertex shader" : "vec4 vertex shader") :
390 strcmp(inst->name, "3DSTATE_GS") == 0 ? (is_simd8 ? "SIMD8 geometry shader" : "vec4 geometry shader") :
391 NULL;
392
393 if (is_enabled) {
394 programs[next_program()] = (struct program) {
395 .type = type,
396 .command = inst->name,
397 .command_offset = offset,
398 .instruction_base_address = current_instruction_base_address,
399 .ksp = ksp,
400 };
401 }
402 }
403 }
404 }
405
406 static int zlib_inflate(uint32_t **ptr, int len)
407 {
408 struct z_stream_s zstream;
409 void *out;
410 const uint32_t out_size = 128*4096; /* approximate obj size */
411
412 memset(&zstream, 0, sizeof(zstream));
413
414 zstream.next_in = (unsigned char *)*ptr;
415 zstream.avail_in = 4*len;
416
417 if (inflateInit(&zstream) != Z_OK)
418 return 0;
419
420 out = malloc(out_size);
421 zstream.next_out = out;
422 zstream.avail_out = out_size;
423
424 do {
425 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
426 case Z_STREAM_END:
427 goto end;
428 case Z_OK:
429 break;
430 default:
431 inflateEnd(&zstream);
432 return 0;
433 }
434
435 if (zstream.avail_out)
436 break;
437
438 out = realloc(out, 2*zstream.total_out);
439 if (out == NULL) {
440 inflateEnd(&zstream);
441 return 0;
442 }
443
444 zstream.next_out = (unsigned char *)out + zstream.total_out;
445 zstream.avail_out = zstream.total_out;
446 } while (1);
447 end:
448 inflateEnd(&zstream);
449 free(*ptr);
450 *ptr = out;
451 return zstream.total_out / 4;
452 }
453
454 static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
455 {
456 int len = 0, size = 1024;
457
458 *out = realloc(*out, sizeof(uint32_t)*size);
459 if (*out == NULL)
460 return 0;
461
462 while (*in >= '!' && *in <= 'z') {
463 uint32_t v = 0;
464
465 if (len == size) {
466 size *= 2;
467 *out = realloc(*out, sizeof(uint32_t)*size);
468 if (*out == NULL)
469 return 0;
470 }
471
472 if (*in == 'z') {
473 in++;
474 } else {
475 v += in[0] - 33; v *= 85;
476 v += in[1] - 33; v *= 85;
477 v += in[2] - 33; v *= 85;
478 v += in[3] - 33; v *= 85;
479 v += in[4] - 33;
480 in += 5;
481 }
482 (*out)[len++] = v;
483 }
484
485 if (!inflate)
486 return len;
487
488 return zlib_inflate(out, len);
489 }
490
491 static void
492 read_data_file(FILE *file)
493 {
494 struct gen_spec *spec = NULL;
495 long long unsigned fence;
496 int matched;
497 char *line = NULL;
498 size_t line_size;
499 uint32_t offset, value;
500 char *ring_name = NULL;
501 struct gen_device_info devinfo;
502 struct gen_disasm *disasm = NULL;
503 int sect_num = 0;
504
505 while (getline(&line, &line_size, file) > 0) {
506 char *new_ring_name = NULL;
507 char *dashes;
508
509 if (sscanf(line, "%m[^ ] command stream\n", &new_ring_name) > 0) {
510 free(ring_name);
511 ring_name = new_ring_name;
512 }
513
514 if (line[0] == ':' || line[0] == '~') {
515 uint32_t *data = NULL;
516 int count = ascii85_decode(line+1, &data, line[0] == ':');
517 if (count == 0) {
518 fprintf(stderr, "ASCII85 decode failed.\n");
519 exit(EXIT_FAILURE);
520 }
521 sections[sect_num].data = data;
522 sections[sect_num].count = count;
523 sect_num++;
524 continue;
525 }
526
527 dashes = strstr(line, "---");
528 if (dashes) {
529 const struct {
530 const char *match;
531 const char *name;
532 } buffers[] = {
533 { "ringbuffer", "ring buffer" },
534 { "gtt_offset", "batch buffer" },
535 { "hw context", "HW Context" },
536 { "hw status", "HW status" },
537 { "wa context", "WA context" },
538 { "wa batchbuffer", "WA batch" },
539 { "user", "user" },
540 { "semaphores", "semaphores", },
541 { "guc log buffer", "GuC log", },
542 { NULL, "unknown" },
543 }, *b;
544
545 free(ring_name);
546 ring_name = malloc(dashes - line);
547 strncpy(ring_name, line, dashes - line);
548 ring_name[dashes - line - 1] = '\0';
549
550 dashes += 4;
551 for (b = buffers; b->match; b++) {
552 if (strncasecmp(dashes, b->match, strlen(b->match)) == 0)
553 break;
554 }
555
556 sections[sect_num].buffer_name = b->name;
557 sections[sect_num].ring_name = strdup(ring_name);
558
559 uint32_t hi, lo;
560 dashes = strchr(dashes, '=');
561 if (dashes && sscanf(dashes, "= 0x%08x %08x\n", &hi, &lo))
562 sections[sect_num].gtt_offset = ((uint64_t) hi) << 32 | lo;
563
564 continue;
565 }
566
567 matched = sscanf(line, "%08x : %08x", &offset, &value);
568 if (matched != 2) {
569 uint32_t reg, reg2;
570
571 /* display reg section is after the ringbuffers, don't mix them */
572 printf("%s", line);
573
574 matched = sscanf(line, "PCI ID: 0x%04x\n", &reg);
575 if (matched == 0)
576 matched = sscanf(line, " PCI ID: 0x%04x\n", &reg);
577 if (matched == 0) {
578 const char *pci_id_start = strstr(line, "PCI ID");
579 if (pci_id_start)
580 matched = sscanf(pci_id_start, "PCI ID: 0x%04x\n", &reg);
581 }
582 if (matched == 1) {
583 if (!gen_get_device_info(reg, &devinfo)) {
584 printf("Unable to identify devid=%x\n", reg);
585 exit(EXIT_FAILURE);
586 }
587
588 disasm = gen_disasm_create(reg);
589
590 printf("Detected GEN%i chipset\n", devinfo.gen);
591
592 if (xml_path == NULL)
593 spec = gen_spec_load(&devinfo);
594 else
595 spec = gen_spec_load_from_path(&devinfo, xml_path);
596 }
597
598 matched = sscanf(line, " CTL: 0x%08x\n", &reg);
599 if (matched == 1) {
600 print_register(spec,
601 register_name_from_ring(ctl_registers,
602 ARRAY_SIZE(ctl_registers),
603 ring_name), reg);
604 }
605
606 matched = sscanf(line, " HEAD: 0x%08x\n", &reg);
607 if (matched == 1)
608 print_head(reg);
609
610 matched = sscanf(line, " ACTHD: 0x%08x\n", &reg);
611 if (matched == 1) {
612 print_register(spec,
613 register_name_from_ring(acthd_registers,
614 ARRAY_SIZE(acthd_registers),
615 ring_name), reg);
616 }
617
618 matched = sscanf(line, " PGTBL_ER: 0x%08x\n", &reg);
619 if (matched == 1 && reg)
620 print_pgtbl_err(reg, &devinfo);
621
622 matched = sscanf(line, " ERROR: 0x%08x\n", &reg);
623 if (matched == 1 && reg) {
624 print_register(spec, "GFX_ARB_ERROR_RPT", reg);
625 }
626
627 matched = sscanf(line, " INSTDONE: 0x%08x\n", &reg);
628 if (matched == 1) {
629 const char *reg_name =
630 instdone_register_for_ring(&devinfo, ring_name);
631 if (reg_name)
632 print_register(spec, reg_name, reg);
633 }
634
635 matched = sscanf(line, " INSTDONE1: 0x%08x\n", &reg);
636 if (matched == 1)
637 print_register(spec, "INSTDONE_1", reg);
638
639 matched = sscanf(line, " fence[%i] = %Lx\n", &reg, &fence);
640 if (matched == 2)
641 print_fence(&devinfo, fence);
642
643 matched = sscanf(line, " FAULT_REG: 0x%08x\n", &reg);
644 if (matched == 1 && reg) {
645 const char *reg_name =
646 register_name_from_ring(fault_registers,
647 ARRAY_SIZE(fault_registers),
648 ring_name);
649 if (reg_name == NULL)
650 reg_name = "FAULT_REG";
651 print_register(spec, reg_name, reg);
652 }
653
654 matched = sscanf(line, " FAULT_TLB_DATA: 0x%08x 0x%08x\n", &reg, &reg2);
655 if (matched == 2)
656 print_fault_data(&devinfo, reg, reg2);
657
658 continue;
659 }
660 }
661
662 free(line);
663 free(ring_name);
664
665 for (int s = 0; s < sect_num; s++) {
666 printf("--- %s (%s) at 0x%08x %08x\n",
667 sections[s].buffer_name, sections[s].ring_name,
668 (unsigned) (sections[s].gtt_offset >> 32),
669 (unsigned) sections[s].gtt_offset);
670
671 if (strcmp(sections[s].buffer_name, "batch buffer") == 0 ||
672 strcmp(sections[s].buffer_name, "ring buffer") == 0 ||
673 strcmp(sections[s].buffer_name, "HW Context") == 0) {
674 decode(spec, &sections[s]);
675 } else if (strcmp(sections[s].buffer_name, "user") == 0) {
676 printf("Disassembly of programs in instruction buffer at "
677 "0x%08"PRIx64":\n", sections[s].gtt_offset);
678 for (int i = 0; i < num_programs; i++) {
679 int idx = (idx_program + i) % MAX_NUM_PROGRAMS;
680 if (programs[idx].instruction_base_address ==
681 sections[s].gtt_offset) {
682 printf("\n%s (specified by %s at batch offset "
683 "0x%08"PRIx64") at offset 0x%08"PRIx64"\n",
684 programs[idx].type,
685 programs[idx].command,
686 programs[idx].command_offset,
687 programs[idx].ksp);
688 gen_disasm_disassemble(disasm, sections[s].data,
689 programs[idx].ksp, stdout);
690 }
691 }
692 }
693
694 free(sections[s].ring_name);
695 free(sections[s].data);
696 }
697
698 gen_disasm_destroy(disasm);
699 }
700
701 static void
702 setup_pager(void)
703 {
704 int fds[2];
705 pid_t pid;
706
707 if (!isatty(1))
708 return;
709
710 if (pipe(fds) == -1)
711 return;
712
713 pid = fork();
714 if (pid == -1)
715 return;
716
717 if (pid == 0) {
718 close(fds[1]);
719 dup2(fds[0], 0);
720 execlp("less", "less", "-FRSi", NULL);
721 }
722
723 close(fds[0]);
724 dup2(fds[1], 1);
725 close(fds[1]);
726 }
727
728 static void
729 print_help(const char *progname, FILE *file)
730 {
731 fprintf(file,
732 "Usage: %s [OPTION]... [FILE]\n"
733 "Parse an Intel GPU i915_error_state.\n"
734 "With no FILE, debugfs-dri-directory is probed for in /debug and \n"
735 "/sys/kernel/debug. Otherwise, it may be specified. If a file is given,\n"
736 "it is parsed as an GPU dump in the format of /debug/dri/0/i915_error_state.\n\n"
737 " --help display this help and exit\n"
738 " --headers decode only command headers\n"
739 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
740 " if omitted), 'always', or 'never'\n"
741 " --no-pager don't launch pager\n"
742 " --no-offsets don't print instruction offsets\n"
743 " --xml=DIR load hardware xml description from directory DIR\n",
744 progname);
745 }
746
747 int
748 main(int argc, char *argv[])
749 {
750 FILE *file;
751 const char *path;
752 struct stat st;
753 int c, i, error;
754 bool help = false, pager = true;
755 const struct option aubinator_opts[] = {
756 { "help", no_argument, (int *) &help, true },
757 { "no-pager", no_argument, (int *) &pager, false },
758 { "no-offsets", no_argument, (int *) &option_print_offsets, false },
759 { "headers", no_argument, (int *) &option_full_decode, false },
760 { "color", required_argument, NULL, 'c' },
761 { "xml", required_argument, NULL, 'x' },
762 { NULL, 0, NULL, 0 }
763 };
764
765 i = 0;
766 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
767 switch (c) {
768 case 'c':
769 if (optarg == NULL || strcmp(optarg, "always") == 0)
770 option_color = COLOR_ALWAYS;
771 else if (strcmp(optarg, "never") == 0)
772 option_color = COLOR_NEVER;
773 else if (strcmp(optarg, "auto") == 0)
774 option_color = COLOR_AUTO;
775 else {
776 fprintf(stderr, "invalid value for --color: %s", optarg);
777 exit(EXIT_FAILURE);
778 }
779 break;
780 case 'x':
781 xml_path = strdup(optarg);
782 break;
783 default:
784 break;
785 }
786 }
787
788 if (help || argc == 1) {
789 print_help(argv[0], stderr);
790 exit(EXIT_SUCCESS);
791 }
792
793 if (optind >= argc) {
794 if (isatty(0)) {
795 path = "/sys/class/drm/card0/error";
796 error = stat(path, &st);
797 if (error != 0) {
798 path = "/debug/dri";
799 error = stat(path, &st);
800 }
801 if (error != 0) {
802 path = "/sys/kernel/debug/dri";
803 error = stat(path, &st);
804 }
805 if (error != 0) {
806 errx(1,
807 "Couldn't find i915 debugfs directory.\n\n"
808 "Is debugfs mounted? You might try mounting it with a command such as:\n\n"
809 "\tsudo mount -t debugfs debugfs /sys/kernel/debug\n");
810 }
811 } else {
812 read_data_file(stdin);
813 exit(EXIT_SUCCESS);
814 }
815 } else {
816 path = argv[optind];
817 error = stat(path, &st);
818 if (error != 0) {
819 fprintf(stderr, "Error opening %s: %s\n",
820 path, strerror(errno));
821 exit(EXIT_FAILURE);
822 }
823 }
824
825 if (option_color == COLOR_AUTO)
826 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
827
828 if (isatty(1) && pager)
829 setup_pager();
830
831 if (S_ISDIR(st.st_mode)) {
832 int ret;
833 char *filename;
834
835 ret = asprintf(&filename, "%s/i915_error_state", path);
836 assert(ret > 0);
837 file = fopen(filename, "r");
838 if (!file) {
839 int minor;
840 free(filename);
841 for (minor = 0; minor < 64; minor++) {
842 ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor);
843 assert(ret > 0);
844
845 file = fopen(filename, "r");
846 if (file)
847 break;
848
849 free(filename);
850 }
851 }
852 if (!file) {
853 fprintf(stderr, "Failed to find i915_error_state beneath %s\n",
854 path);
855 return EXIT_FAILURE;
856 }
857 } else {
858 file = fopen(path, "r");
859 if (!file) {
860 fprintf(stderr, "Failed to open %s: %s\n",
861 path, strerror(errno));
862 return EXIT_FAILURE;
863 }
864 }
865
866 read_data_file(file);
867 fclose(file);
868
869 /* close the stdout which is opened to write the output */
870 fflush(stdout);
871 close(1);
872 wait(NULL);
873
874 if (xml_path)
875 free(xml_path);
876
877 return EXIT_SUCCESS;
878 }
879
880 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/