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