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