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