intel/tools/error: Only decode a few sections of error states.
[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 MAX_RINGS 10 /* I really hope this never... */
211
212 #define CSI "\e["
213 #define NORMAL CSI "0m"
214
215 struct program {
216 const char *type;
217 const char *command;
218 uint64_t command_offset;
219 uint64_t instruction_base_address;
220 uint64_t ksp;
221 };
222
223 #define MAX_NUM_PROGRAMS 4096
224 static struct program programs[MAX_NUM_PROGRAMS];
225 static int idx_program = 0, num_programs = 0;
226
227 static int next_program(void)
228 {
229 int ret = idx_program;
230 idx_program = (idx_program + 1) % MAX_NUM_PROGRAMS;
231 num_programs = MIN(num_programs + 1, MAX_NUM_PROGRAMS);
232 return ret;
233 }
234
235 static void decode(struct gen_spec *spec,
236 uint64_t gtt_offset,
237 uint32_t *data,
238 int count)
239 {
240 uint32_t *p, *end = (data + count);
241 int length;
242 struct gen_group *inst;
243 uint64_t current_instruction_base_address = 0;
244
245 for (p = data; p < end; p += length) {
246 const char *color = option_full_decode ? BLUE_HEADER : NORMAL,
247 *reset_color = NORMAL;
248 uint64_t offset = gtt_offset + 4 * (p - data);
249
250 inst = gen_spec_find_instruction(spec, p);
251 length = gen_group_get_length(inst, p);
252 assert(inst == NULL || length > 0);
253 length = MAX2(1, length);
254 if (inst == NULL) {
255 printf("unknown instruction %08x\n", p[0]);
256 continue;
257 }
258 if (option_color == COLOR_NEVER) {
259 color = "";
260 reset_color = "";
261 }
262
263 printf("%s0x%08"PRIx64": 0x%08x: %-80s%s\n",
264 color, offset, p[0], gen_group_get_name(inst), reset_color);
265
266 gen_print_group(stdout, inst, offset, p,
267 option_color == COLOR_ALWAYS);
268
269 if (strcmp(inst->name, "MI_BATCH_BUFFER_END") == 0)
270 break;
271
272 if (strcmp(inst->name, "STATE_BASE_ADDRESS") == 0) {
273 struct gen_field_iterator iter;
274 gen_field_iterator_init(&iter, inst, p, false);
275
276 while (gen_field_iterator_next(&iter)) {
277 if (strcmp(iter.name, "Instruction Base Address") == 0) {
278 current_instruction_base_address = strtol(iter.value, NULL, 16);
279 }
280 }
281 } else if (strcmp(inst->name, "WM_STATE") == 0 ||
282 strcmp(inst->name, "3DSTATE_PS") == 0 ||
283 strcmp(inst->name, "3DSTATE_WM") == 0) {
284 struct gen_field_iterator iter;
285 gen_field_iterator_init(&iter, inst, p, false);
286 uint64_t ksp[3] = {0, 0, 0};
287 bool enabled[3] = {false, false, false};
288
289 while (gen_field_iterator_next(&iter)) {
290 if (strncmp(iter.name, "Kernel Start Pointer ",
291 strlen("Kernel Start Pointer ")) == 0) {
292 int idx = iter.name[strlen("Kernel Start Pointer ")] - '0';
293 ksp[idx] = strtol(iter.value, NULL, 16);
294 } else if (strcmp(iter.name, "8 Pixel Dispatch Enable") == 0) {
295 enabled[0] = strcmp(iter.value, "true") == 0;
296 } else if (strcmp(iter.name, "16 Pixel Dispatch Enable") == 0) {
297 enabled[1] = strcmp(iter.value, "true") == 0;
298 } else if (strcmp(iter.name, "32 Pixel Dispatch Enable") == 0) {
299 enabled[2] = strcmp(iter.value, "true") == 0;
300 }
301 }
302
303 /* FINISHME: Broken for multi-program WM_STATE,
304 * which Mesa does not use
305 */
306 if (enabled[0] + enabled[1] + enabled[2] == 1) {
307 const char *type = enabled[0] ? "SIMD8 fragment shader" :
308 enabled[1] ? "SIMD16 fragment shader" :
309 enabled[2] ? "SIMD32 fragment shader" : NULL;
310
311 programs[next_program()] = (struct program) {
312 .type = type,
313 .command = inst->name,
314 .command_offset = offset,
315 .instruction_base_address = current_instruction_base_address,
316 .ksp = ksp[0],
317 };
318 } else {
319 if (enabled[0]) /* SIMD8 */ {
320 programs[next_program()] = (struct program) {
321 .type = "SIMD8 fragment shader",
322 .command = inst->name,
323 .command_offset = offset,
324 .instruction_base_address = current_instruction_base_address,
325 .ksp = ksp[0], /* SIMD8 shader is specified by ksp[0] */
326 };
327 }
328 if (enabled[1]) /* SIMD16 */ {
329 programs[next_program()] = (struct program) {
330 .type = "SIMD16 fragment shader",
331 .command = inst->name,
332 .command_offset = offset,
333 .instruction_base_address = current_instruction_base_address,
334 .ksp = ksp[2], /* SIMD16 shader is specified by ksp[2] */
335 };
336 }
337 if (enabled[2]) /* SIMD32 */ {
338 programs[next_program()] = (struct program) {
339 .type = "SIMD32 fragment shader",
340 .command = inst->name,
341 .command_offset = offset,
342 .instruction_base_address = current_instruction_base_address,
343 .ksp = ksp[1], /* SIMD32 shader is specified by ksp[1] */
344 };
345 }
346 }
347 } else if (strcmp(inst->name, "VS_STATE") == 0 ||
348 strcmp(inst->name, "GS_STATE") == 0 ||
349 strcmp(inst->name, "SF_STATE") == 0 ||
350 strcmp(inst->name, "CLIP_STATE") == 0 ||
351 strcmp(inst->name, "3DSTATE_DS") == 0 ||
352 strcmp(inst->name, "3DSTATE_HS") == 0 ||
353 strcmp(inst->name, "3DSTATE_GS") == 0 ||
354 strcmp(inst->name, "3DSTATE_VS") == 0) {
355 struct gen_field_iterator iter;
356 gen_field_iterator_init(&iter, inst, p, false);
357 uint64_t ksp = 0;
358 bool is_simd8 = false; /* vertex shaders on Gen8+ only */
359 bool is_enabled = true;
360
361 while (gen_field_iterator_next(&iter)) {
362 if (strcmp(iter.name, "Kernel Start Pointer") == 0) {
363 ksp = strtol(iter.value, NULL, 16);
364 } else if (strcmp(iter.name, "SIMD8 Dispatch Enable") == 0) {
365 is_simd8 = strcmp(iter.value, "true") == 0;
366 } else if (strcmp(iter.name, "Dispatch Enable") == 0) {
367 is_simd8 = strcmp(iter.value, "SIMD8") == 0;
368 } else if (strcmp(iter.name, "Enable") == 0) {
369 is_enabled = strcmp(iter.value, "true") == 0;
370 }
371 }
372
373 const char *type =
374 strcmp(inst->name, "VS_STATE") == 0 ? "vertex shader" :
375 strcmp(inst->name, "GS_STATE") == 0 ? "geometry shader" :
376 strcmp(inst->name, "SF_STATE") == 0 ? "strips and fans shader" :
377 strcmp(inst->name, "CLIP_STATE") == 0 ? "clip shader" :
378 strcmp(inst->name, "3DSTATE_DS") == 0 ? "tessellation control shader" :
379 strcmp(inst->name, "3DSTATE_HS") == 0 ? "tessellation evaluation shader" :
380 strcmp(inst->name, "3DSTATE_VS") == 0 ? (is_simd8 ? "SIMD8 vertex shader" : "vec4 vertex shader") :
381 strcmp(inst->name, "3DSTATE_GS") == 0 ? (is_simd8 ? "SIMD8 geometry shader" : "vec4 geometry shader") :
382 NULL;
383
384 if (is_enabled) {
385 programs[next_program()] = (struct program) {
386 .type = type,
387 .command = inst->name,
388 .command_offset = offset,
389 .instruction_base_address = current_instruction_base_address,
390 .ksp = ksp,
391 };
392 }
393 }
394 }
395 }
396
397 static int zlib_inflate(uint32_t **ptr, int len)
398 {
399 struct z_stream_s zstream;
400 void *out;
401 const uint32_t out_size = 128*4096; /* approximate obj size */
402
403 memset(&zstream, 0, sizeof(zstream));
404
405 zstream.next_in = (unsigned char *)*ptr;
406 zstream.avail_in = 4*len;
407
408 if (inflateInit(&zstream) != Z_OK)
409 return 0;
410
411 out = malloc(out_size);
412 zstream.next_out = out;
413 zstream.avail_out = out_size;
414
415 do {
416 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
417 case Z_STREAM_END:
418 goto end;
419 case Z_OK:
420 break;
421 default:
422 inflateEnd(&zstream);
423 return 0;
424 }
425
426 if (zstream.avail_out)
427 break;
428
429 out = realloc(out, 2*zstream.total_out);
430 if (out == NULL) {
431 inflateEnd(&zstream);
432 return 0;
433 }
434
435 zstream.next_out = (unsigned char *)out + zstream.total_out;
436 zstream.avail_out = zstream.total_out;
437 } while (1);
438 end:
439 inflateEnd(&zstream);
440 free(*ptr);
441 *ptr = out;
442 return zstream.total_out / 4;
443 }
444
445 static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
446 {
447 int len = 0, size = 1024;
448
449 *out = realloc(*out, sizeof(uint32_t)*size);
450 if (*out == NULL)
451 return 0;
452
453 while (*in >= '!' && *in <= 'z') {
454 uint32_t v = 0;
455
456 if (len == size) {
457 size *= 2;
458 *out = realloc(*out, sizeof(uint32_t)*size);
459 if (*out == NULL)
460 return 0;
461 }
462
463 if (*in == 'z') {
464 in++;
465 } else {
466 v += in[0] - 33; v *= 85;
467 v += in[1] - 33; v *= 85;
468 v += in[2] - 33; v *= 85;
469 v += in[3] - 33; v *= 85;
470 v += in[4] - 33;
471 in += 5;
472 }
473 (*out)[len++] = v;
474 }
475
476 if (!inflate)
477 return len;
478
479 return zlib_inflate(out, len);
480 }
481
482 static void
483 read_data_file(FILE *file)
484 {
485 struct gen_spec *spec = NULL;
486 long long unsigned fence;
487 int matched;
488 char *line = NULL;
489 size_t line_size;
490 uint32_t offset, value;
491 uint64_t gtt_offset = 0, new_gtt_offset;
492 const char *buffer_name = "batch buffer";
493 char *ring_name = NULL;
494 struct gen_device_info devinfo;
495 struct gen_disasm *disasm = NULL;
496
497 while (getline(&line, &line_size, file) > 0) {
498 char *new_ring_name = NULL;
499 char *dashes;
500
501 if (sscanf(line, "%m[^ ] command stream\n", &new_ring_name) > 0) {
502 free(ring_name);
503 ring_name = new_ring_name;
504 }
505
506 if (line[0] == ':' || line[0] == '~') {
507 uint32_t *data = NULL;
508 int count = ascii85_decode(line+1, &data, line[0] == ':');
509 if (count == 0) {
510 fprintf(stderr, "ASCII85 decode failed.\n");
511 exit(EXIT_FAILURE);
512 }
513
514 if (strcmp(buffer_name, "user") == 0) {
515 printf("Disassembly of programs in instruction buffer at "
516 "0x%08"PRIx64":\n", gtt_offset);
517 for (int i = 0; i < num_programs; i++) {
518 int idx = (idx_program + i) % MAX_NUM_PROGRAMS;
519 if (programs[idx].instruction_base_address == gtt_offset) {
520 printf("\n%s (specified by %s at batch offset "
521 "0x%08"PRIx64") at offset 0x%08"PRIx64"\n",
522 programs[idx].type,
523 programs[idx].command,
524 programs[idx].command_offset,
525 programs[idx].ksp);
526 gen_disasm_disassemble(disasm, data, programs[idx].ksp,
527 stdout);
528 }
529 }
530 } else if (strcmp(buffer_name, "batch buffer") == 0 ||
531 strcmp(buffer_name, "ring buffer") == 0 ||
532 strcmp(buffer_name, "HW Context") == 0) {
533 decode(spec, gtt_offset, data, count);
534 }
535 free(data);
536 continue;
537 }
538
539 dashes = strstr(line, "---");
540 if (dashes) {
541 uint32_t lo, hi;
542 char *new_ring_name = malloc(dashes - line);
543 strncpy(new_ring_name, line, dashes - line);
544 new_ring_name[dashes - line - 1] = '\0';
545
546 printf("%s", line);
547
548 matched = sscanf(dashes, "--- gtt_offset = 0x%08x %08x\n",
549 &hi, &lo);
550 if (matched > 0) {
551 new_gtt_offset = hi;
552 if (matched == 2) {
553 new_gtt_offset <<= 32;
554 new_gtt_offset |= lo;
555 }
556
557 gtt_offset = new_gtt_offset;
558 free(ring_name);
559 ring_name = new_ring_name;
560 buffer_name = "batch buffer";
561 continue;
562 }
563
564 matched = sscanf(dashes, "--- ringbuffer = 0x%08x %08x\n",
565 &hi, &lo);
566 if (matched > 0) {
567 new_gtt_offset = hi;
568 if (matched == 2) {
569 new_gtt_offset <<= 32;
570 new_gtt_offset |= lo;
571 }
572
573 gtt_offset = new_gtt_offset;
574 free(ring_name);
575 ring_name = new_ring_name;
576 buffer_name = "ring buffer";
577 continue;
578 }
579
580 matched = sscanf(dashes, "--- HW Context = 0x%08x %08x\n",
581 &hi, &lo);
582 if (matched > 0) {
583 new_gtt_offset = hi;
584 if (matched == 2) {
585 new_gtt_offset <<= 32;
586 new_gtt_offset |= lo;
587 }
588
589 gtt_offset = new_gtt_offset;
590 free(ring_name);
591 ring_name = new_ring_name;
592 buffer_name = "HW Context";
593 continue;
594 }
595
596 matched = sscanf(dashes, "--- user = 0x%08x %08x\n",
597 &hi, &lo);
598 if (matched > 0) {
599 new_gtt_offset = hi;
600 if (matched == 2) {
601 new_gtt_offset <<= 32;
602 new_gtt_offset |= lo;
603 }
604
605 gtt_offset = new_gtt_offset;
606 free(ring_name);
607 ring_name = new_ring_name;
608 buffer_name = "user";
609 continue;
610 }
611 }
612
613 matched = sscanf(line, "%08x : %08x", &offset, &value);
614 if (matched != 2) {
615 uint32_t reg, reg2;
616
617 /* display reg section is after the ringbuffers, don't mix them */
618 printf("%s", line);
619
620 matched = sscanf(line, "PCI ID: 0x%04x\n", &reg);
621 if (matched == 0)
622 matched = sscanf(line, " PCI ID: 0x%04x\n", &reg);
623 if (matched == 0) {
624 const char *pci_id_start = strstr(line, "PCI ID");
625 if (pci_id_start)
626 matched = sscanf(pci_id_start, "PCI ID: 0x%04x\n", &reg);
627 }
628 if (matched == 1) {
629 if (!gen_get_device_info(reg, &devinfo)) {
630 printf("Unable to identify devid=%x\n", reg);
631 exit(EXIT_FAILURE);
632 }
633
634 disasm = gen_disasm_create(reg);
635
636 printf("Detected GEN%i chipset\n", devinfo.gen);
637
638 if (xml_path == NULL)
639 spec = gen_spec_load(&devinfo);
640 else
641 spec = gen_spec_load_from_path(&devinfo, xml_path);
642 }
643
644 matched = sscanf(line, " CTL: 0x%08x\n", &reg);
645 if (matched == 1) {
646 print_register(spec,
647 register_name_from_ring(ctl_registers,
648 ARRAY_SIZE(ctl_registers),
649 ring_name), reg);
650 }
651
652 matched = sscanf(line, " HEAD: 0x%08x\n", &reg);
653 if (matched == 1)
654 print_head(reg);
655
656 matched = sscanf(line, " ACTHD: 0x%08x\n", &reg);
657 if (matched == 1) {
658 print_register(spec,
659 register_name_from_ring(acthd_registers,
660 ARRAY_SIZE(acthd_registers),
661 ring_name), reg);
662 }
663
664 matched = sscanf(line, " PGTBL_ER: 0x%08x\n", &reg);
665 if (matched == 1 && reg)
666 print_pgtbl_err(reg, &devinfo);
667
668 matched = sscanf(line, " ERROR: 0x%08x\n", &reg);
669 if (matched == 1 && reg) {
670 print_register(spec, "GFX_ARB_ERROR_RPT", reg);
671 }
672
673 matched = sscanf(line, " INSTDONE: 0x%08x\n", &reg);
674 if (matched == 1) {
675 const char *reg_name =
676 instdone_register_for_ring(&devinfo, ring_name);
677 if (reg_name)
678 print_register(spec, reg_name, reg);
679 }
680
681 matched = sscanf(line, " INSTDONE1: 0x%08x\n", &reg);
682 if (matched == 1)
683 print_register(spec, "INSTDONE_1", reg);
684
685 matched = sscanf(line, " fence[%i] = %Lx\n", &reg, &fence);
686 if (matched == 2)
687 print_fence(&devinfo, fence);
688
689 matched = sscanf(line, " FAULT_REG: 0x%08x\n", &reg);
690 if (matched == 1 && reg) {
691 const char *reg_name =
692 register_name_from_ring(fault_registers,
693 ARRAY_SIZE(fault_registers),
694 ring_name);
695 if (reg_name == NULL)
696 reg_name = "FAULT_REG";
697 print_register(spec, reg_name, reg);
698 }
699
700 matched = sscanf(line, " FAULT_TLB_DATA: 0x%08x 0x%08x\n", &reg, &reg2);
701 if (matched == 2)
702 print_fault_data(&devinfo, reg, reg2);
703
704 continue;
705 }
706 }
707
708 gen_disasm_destroy(disasm);
709 free(line);
710 free(ring_name);
711 }
712
713 static void
714 setup_pager(void)
715 {
716 int fds[2];
717 pid_t pid;
718
719 if (!isatty(1))
720 return;
721
722 if (pipe(fds) == -1)
723 return;
724
725 pid = fork();
726 if (pid == -1)
727 return;
728
729 if (pid == 0) {
730 close(fds[1]);
731 dup2(fds[0], 0);
732 execlp("less", "less", "-FRSi", NULL);
733 }
734
735 close(fds[0]);
736 dup2(fds[1], 1);
737 close(fds[1]);
738 }
739
740 static void
741 print_help(const char *progname, FILE *file)
742 {
743 fprintf(file,
744 "Usage: %s [OPTION]... [FILE]\n"
745 "Parse an Intel GPU i915_error_state.\n"
746 "With no FILE, debugfs-dri-directory is probed for in /debug and \n"
747 "/sys/kernel/debug. Otherwise, it may be specified. If a file is given,\n"
748 "it is parsed as an GPU dump in the format of /debug/dri/0/i915_error_state.\n\n"
749 " --help display this help and exit\n"
750 " --headers decode only command headers\n"
751 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
752 " if omitted), 'always', or 'never'\n"
753 " --no-pager don't launch pager\n"
754 " --no-offsets don't print instruction offsets\n"
755 " --xml=DIR load hardware xml description from directory DIR\n",
756 progname);
757 }
758
759 int
760 main(int argc, char *argv[])
761 {
762 FILE *file;
763 const char *path;
764 struct stat st;
765 int c, i, error;
766 bool help = false, pager = true;
767 const struct option aubinator_opts[] = {
768 { "help", no_argument, (int *) &help, true },
769 { "no-pager", no_argument, (int *) &pager, false },
770 { "no-offsets", no_argument, (int *) &option_print_offsets, false },
771 { "headers", no_argument, (int *) &option_full_decode, false },
772 { "color", required_argument, NULL, 'c' },
773 { "xml", required_argument, NULL, 'x' },
774 { NULL, 0, NULL, 0 }
775 };
776
777 i = 0;
778 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
779 switch (c) {
780 case 'c':
781 if (optarg == NULL || strcmp(optarg, "always") == 0)
782 option_color = COLOR_ALWAYS;
783 else if (strcmp(optarg, "never") == 0)
784 option_color = COLOR_NEVER;
785 else if (strcmp(optarg, "auto") == 0)
786 option_color = COLOR_AUTO;
787 else {
788 fprintf(stderr, "invalid value for --color: %s", optarg);
789 exit(EXIT_FAILURE);
790 }
791 break;
792 case 'x':
793 xml_path = strdup(optarg);
794 break;
795 default:
796 break;
797 }
798 }
799
800 if (help || argc == 1) {
801 print_help(argv[0], stderr);
802 exit(EXIT_SUCCESS);
803 }
804
805 if (optind >= argc) {
806 if (isatty(0)) {
807 path = "/sys/class/drm/card0/error";
808 error = stat(path, &st);
809 if (error != 0) {
810 path = "/debug/dri";
811 error = stat(path, &st);
812 }
813 if (error != 0) {
814 path = "/sys/kernel/debug/dri";
815 error = stat(path, &st);
816 }
817 if (error != 0) {
818 errx(1,
819 "Couldn't find i915 debugfs directory.\n\n"
820 "Is debugfs mounted? You might try mounting it with a command such as:\n\n"
821 "\tsudo mount -t debugfs debugfs /sys/kernel/debug\n");
822 }
823 } else {
824 read_data_file(stdin);
825 exit(EXIT_SUCCESS);
826 }
827 } else {
828 path = argv[optind];
829 error = stat(path, &st);
830 if (error != 0) {
831 fprintf(stderr, "Error opening %s: %s\n",
832 path, strerror(errno));
833 exit(EXIT_FAILURE);
834 }
835 }
836
837 if (option_color == COLOR_AUTO)
838 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
839
840 if (isatty(1) && pager)
841 setup_pager();
842
843 if (S_ISDIR(st.st_mode)) {
844 int ret;
845 char *filename;
846
847 ret = asprintf(&filename, "%s/i915_error_state", path);
848 assert(ret > 0);
849 file = fopen(filename, "r");
850 if (!file) {
851 int minor;
852 free(filename);
853 for (minor = 0; minor < 64; minor++) {
854 ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor);
855 assert(ret > 0);
856
857 file = fopen(filename, "r");
858 if (file)
859 break;
860
861 free(filename);
862 }
863 }
864 if (!file) {
865 fprintf(stderr, "Failed to find i915_error_state beneath %s\n",
866 path);
867 return EXIT_FAILURE;
868 }
869 } else {
870 file = fopen(path, "r");
871 if (!file) {
872 fprintf(stderr, "Failed to open %s: %s\n",
873 path, strerror(errno));
874 return EXIT_FAILURE;
875 }
876 }
877
878 read_data_file(file);
879 fclose(file);
880
881 /* close the stdout which is opened to write the output */
882 fflush(stdout);
883 close(1);
884 wait(NULL);
885
886 if (xml_path)
887 free(xml_path);
888
889 return EXIT_SUCCESS;
890 }
891
892 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/