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