intel/tools/error: Refactor buffer matching, add more buffers.
[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;
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 const struct {
542 const char *match;
543 const char *name;
544 } buffers[] = {
545 { "ringbuffer", "ring buffer" },
546 { "gtt_offset", "batch buffer" },
547 { "hw context", "HW Context" },
548 { "hw status", "HW status" },
549 { "wa context", "WA context" },
550 { "wa batchbuffer", "WA batch" },
551 { "user", "user" },
552 { "semaphores", "semaphores", },
553 { "guc log buffer", "GuC log", },
554 { NULL, "unknown" },
555 }, *b;
556
557 printf("%s", line);
558
559 gtt_offset = 0;
560
561 free(ring_name);
562 ring_name = malloc(dashes - line);
563 strncpy(ring_name, line, dashes - line);
564
565 dashes += 4;
566 for (b = buffers; b->match; b++) {
567 if (strncasecmp(dashes, b->match, strlen(b->match)) == 0)
568 break;
569 }
570
571 buffer_name = b->name;
572
573 uint32_t hi, lo;
574 dashes = strchr(dashes, '=');
575 if (dashes && sscanf(dashes, "= 0x%08x %08x\n", &hi, &lo))
576 gtt_offset = ((uint64_t) hi) << 32 | lo;
577
578 continue;
579 }
580
581 matched = sscanf(line, "%08x : %08x", &offset, &value);
582 if (matched != 2) {
583 uint32_t reg, reg2;
584
585 /* display reg section is after the ringbuffers, don't mix them */
586 printf("%s", line);
587
588 matched = sscanf(line, "PCI ID: 0x%04x\n", &reg);
589 if (matched == 0)
590 matched = sscanf(line, " PCI ID: 0x%04x\n", &reg);
591 if (matched == 0) {
592 const char *pci_id_start = strstr(line, "PCI ID");
593 if (pci_id_start)
594 matched = sscanf(pci_id_start, "PCI ID: 0x%04x\n", &reg);
595 }
596 if (matched == 1) {
597 if (!gen_get_device_info(reg, &devinfo)) {
598 printf("Unable to identify devid=%x\n", reg);
599 exit(EXIT_FAILURE);
600 }
601
602 disasm = gen_disasm_create(reg);
603
604 printf("Detected GEN%i chipset\n", devinfo.gen);
605
606 if (xml_path == NULL)
607 spec = gen_spec_load(&devinfo);
608 else
609 spec = gen_spec_load_from_path(&devinfo, xml_path);
610 }
611
612 matched = sscanf(line, " CTL: 0x%08x\n", &reg);
613 if (matched == 1) {
614 print_register(spec,
615 register_name_from_ring(ctl_registers,
616 ARRAY_SIZE(ctl_registers),
617 ring_name), reg);
618 }
619
620 matched = sscanf(line, " HEAD: 0x%08x\n", &reg);
621 if (matched == 1)
622 print_head(reg);
623
624 matched = sscanf(line, " ACTHD: 0x%08x\n", &reg);
625 if (matched == 1) {
626 print_register(spec,
627 register_name_from_ring(acthd_registers,
628 ARRAY_SIZE(acthd_registers),
629 ring_name), reg);
630 }
631
632 matched = sscanf(line, " PGTBL_ER: 0x%08x\n", &reg);
633 if (matched == 1 && reg)
634 print_pgtbl_err(reg, &devinfo);
635
636 matched = sscanf(line, " ERROR: 0x%08x\n", &reg);
637 if (matched == 1 && reg) {
638 print_register(spec, "GFX_ARB_ERROR_RPT", reg);
639 }
640
641 matched = sscanf(line, " INSTDONE: 0x%08x\n", &reg);
642 if (matched == 1) {
643 const char *reg_name =
644 instdone_register_for_ring(&devinfo, ring_name);
645 if (reg_name)
646 print_register(spec, reg_name, reg);
647 }
648
649 matched = sscanf(line, " INSTDONE1: 0x%08x\n", &reg);
650 if (matched == 1)
651 print_register(spec, "INSTDONE_1", reg);
652
653 matched = sscanf(line, " fence[%i] = %Lx\n", &reg, &fence);
654 if (matched == 2)
655 print_fence(&devinfo, fence);
656
657 matched = sscanf(line, " FAULT_REG: 0x%08x\n", &reg);
658 if (matched == 1 && reg) {
659 const char *reg_name =
660 register_name_from_ring(fault_registers,
661 ARRAY_SIZE(fault_registers),
662 ring_name);
663 if (reg_name == NULL)
664 reg_name = "FAULT_REG";
665 print_register(spec, reg_name, reg);
666 }
667
668 matched = sscanf(line, " FAULT_TLB_DATA: 0x%08x 0x%08x\n", &reg, &reg2);
669 if (matched == 2)
670 print_fault_data(&devinfo, reg, reg2);
671
672 continue;
673 }
674 }
675
676 gen_disasm_destroy(disasm);
677 free(line);
678 free(ring_name);
679 }
680
681 static void
682 setup_pager(void)
683 {
684 int fds[2];
685 pid_t pid;
686
687 if (!isatty(1))
688 return;
689
690 if (pipe(fds) == -1)
691 return;
692
693 pid = fork();
694 if (pid == -1)
695 return;
696
697 if (pid == 0) {
698 close(fds[1]);
699 dup2(fds[0], 0);
700 execlp("less", "less", "-FRSi", NULL);
701 }
702
703 close(fds[0]);
704 dup2(fds[1], 1);
705 close(fds[1]);
706 }
707
708 static void
709 print_help(const char *progname, FILE *file)
710 {
711 fprintf(file,
712 "Usage: %s [OPTION]... [FILE]\n"
713 "Parse an Intel GPU i915_error_state.\n"
714 "With no FILE, debugfs-dri-directory is probed for in /debug and \n"
715 "/sys/kernel/debug. Otherwise, it may be specified. If a file is given,\n"
716 "it is parsed as an GPU dump in the format of /debug/dri/0/i915_error_state.\n\n"
717 " --help display this help and exit\n"
718 " --headers decode only command headers\n"
719 " --color[=WHEN] colorize the output; WHEN can be 'auto' (default\n"
720 " if omitted), 'always', or 'never'\n"
721 " --no-pager don't launch pager\n"
722 " --no-offsets don't print instruction offsets\n"
723 " --xml=DIR load hardware xml description from directory DIR\n",
724 progname);
725 }
726
727 int
728 main(int argc, char *argv[])
729 {
730 FILE *file;
731 const char *path;
732 struct stat st;
733 int c, i, error;
734 bool help = false, pager = true;
735 const struct option aubinator_opts[] = {
736 { "help", no_argument, (int *) &help, true },
737 { "no-pager", no_argument, (int *) &pager, false },
738 { "no-offsets", no_argument, (int *) &option_print_offsets, false },
739 { "headers", no_argument, (int *) &option_full_decode, false },
740 { "color", required_argument, NULL, 'c' },
741 { "xml", required_argument, NULL, 'x' },
742 { NULL, 0, NULL, 0 }
743 };
744
745 i = 0;
746 while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
747 switch (c) {
748 case 'c':
749 if (optarg == NULL || strcmp(optarg, "always") == 0)
750 option_color = COLOR_ALWAYS;
751 else if (strcmp(optarg, "never") == 0)
752 option_color = COLOR_NEVER;
753 else if (strcmp(optarg, "auto") == 0)
754 option_color = COLOR_AUTO;
755 else {
756 fprintf(stderr, "invalid value for --color: %s", optarg);
757 exit(EXIT_FAILURE);
758 }
759 break;
760 case 'x':
761 xml_path = strdup(optarg);
762 break;
763 default:
764 break;
765 }
766 }
767
768 if (help || argc == 1) {
769 print_help(argv[0], stderr);
770 exit(EXIT_SUCCESS);
771 }
772
773 if (optind >= argc) {
774 if (isatty(0)) {
775 path = "/sys/class/drm/card0/error";
776 error = stat(path, &st);
777 if (error != 0) {
778 path = "/debug/dri";
779 error = stat(path, &st);
780 }
781 if (error != 0) {
782 path = "/sys/kernel/debug/dri";
783 error = stat(path, &st);
784 }
785 if (error != 0) {
786 errx(1,
787 "Couldn't find i915 debugfs directory.\n\n"
788 "Is debugfs mounted? You might try mounting it with a command such as:\n\n"
789 "\tsudo mount -t debugfs debugfs /sys/kernel/debug\n");
790 }
791 } else {
792 read_data_file(stdin);
793 exit(EXIT_SUCCESS);
794 }
795 } else {
796 path = argv[optind];
797 error = stat(path, &st);
798 if (error != 0) {
799 fprintf(stderr, "Error opening %s: %s\n",
800 path, strerror(errno));
801 exit(EXIT_FAILURE);
802 }
803 }
804
805 if (option_color == COLOR_AUTO)
806 option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
807
808 if (isatty(1) && pager)
809 setup_pager();
810
811 if (S_ISDIR(st.st_mode)) {
812 int ret;
813 char *filename;
814
815 ret = asprintf(&filename, "%s/i915_error_state", path);
816 assert(ret > 0);
817 file = fopen(filename, "r");
818 if (!file) {
819 int minor;
820 free(filename);
821 for (minor = 0; minor < 64; minor++) {
822 ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor);
823 assert(ret > 0);
824
825 file = fopen(filename, "r");
826 if (file)
827 break;
828
829 free(filename);
830 }
831 }
832 if (!file) {
833 fprintf(stderr, "Failed to find i915_error_state beneath %s\n",
834 path);
835 return EXIT_FAILURE;
836 }
837 } else {
838 file = fopen(path, "r");
839 if (!file) {
840 fprintf(stderr, "Failed to open %s: %s\n",
841 path, strerror(errno));
842 return EXIT_FAILURE;
843 }
844 }
845
846 read_data_file(file);
847 fclose(file);
848
849 /* close the stdout which is opened to write the output */
850 fflush(stdout);
851 close(1);
852 wait(NULL);
853
854 if (xml_path)
855 free(xml_path);
856
857 return EXIT_SUCCESS;
858 }
859
860 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/