+{
+ uint32_t *p, *end = (data + *count);
+ unsigned int length;
+ struct gen_group *inst;
+
+ for (p = data; p < end; p += length) {
+ const char *color = option_full_decode ? BLUE_HEADER : NORMAL,
+ *reset_color = NORMAL;
+ uint64_t offset;
+
+ inst = gen_spec_find_instruction(spec, p);
+ if (inst == NULL) {
+ printf("unknown instruction %08x\n", p[0]);
+ length = (p[0] & 0xff) + 2;
+ continue;
+ }
+ if (option_color == COLOR_NEVER) {
+ color = "";
+ reset_color = "";
+ }
+
+ if (option_print_offsets)
+ offset = gtt_offset + ((uint8_t*) p - (uint8_t*) data);
+ else
+ offset = 0;
+
+ printf("%s0x%08"PRIx64": 0x%08x: %-80s%s\n",
+ color, offset, p[0], gen_group_get_name(inst), reset_color);
+
+ gen_print_group(stdout, inst, gtt_offset, data, 1,
+ option_color == COLOR_ALWAYS);
+ length = gen_group_get_length(inst, p);
+ }
+}
+
+static int zlib_inflate(uint32_t **ptr, int len)
+{
+ struct z_stream_s zstream;
+ void *out;
+
+ memset(&zstream, 0, sizeof(zstream));
+
+ zstream.next_in = (unsigned char *)*ptr;
+ zstream.avail_in = 4*len;
+
+ if (inflateInit(&zstream) != Z_OK)
+ return 0;
+
+ out = malloc(128*4096); /* approximate obj size */
+ zstream.next_out = out;
+ zstream.avail_out = 40*len;
+
+ do {
+ switch (inflate(&zstream, Z_SYNC_FLUSH)) {
+ case Z_STREAM_END:
+ goto end;
+ case Z_OK:
+ break;
+ default:
+ inflateEnd(&zstream);
+ return 0;
+ }
+
+ if (zstream.avail_out)
+ break;
+
+ out = realloc(out, 2*zstream.total_out);
+ if (out == NULL) {
+ inflateEnd(&zstream);
+ return 0;
+ }
+
+ zstream.next_out = (unsigned char *)out + zstream.total_out;
+ zstream.avail_out = zstream.total_out;
+ } while (1);
+ end:
+ inflateEnd(&zstream);
+ free(*ptr);
+ *ptr = out;
+ return zstream.total_out / 4;
+}
+
+static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
+{
+ int len = 0, size = 1024;
+
+ *out = realloc(*out, sizeof(uint32_t)*size);
+ if (*out == NULL)
+ return 0;
+
+ while (*in >= '!' && *in <= 'z') {
+ uint32_t v = 0;
+
+ if (len == size) {
+ size *= 2;
+ *out = realloc(*out, sizeof(uint32_t)*size);
+ if (*out == NULL)
+ return 0;
+ }
+
+ if (*in == 'z') {
+ in++;
+ } else {
+ v += in[0] - 33; v *= 85;
+ v += in[1] - 33; v *= 85;
+ v += in[2] - 33; v *= 85;
+ v += in[3] - 33; v *= 85;
+ v += in[4] - 33;
+ in += 5;
+ }
+ (*out)[len++] = v;
+ }
+
+ if (!inflate)
+ return len;
+
+ return zlib_inflate(out, len);
+}
+
+static void
+read_data_file(FILE *file)
+{
+ struct gen_spec *spec = NULL;
+ uint32_t *data = NULL;
+ uint32_t head[MAX_RINGS];
+ int head_idx = 0;
+ int num_rings = 0;
+ long long unsigned fence;
+ int data_size = 0, count = 0, line_number = 0, matched;
+ char *line = NULL;
+ size_t line_size;
+ uint32_t offset, value;
+ uint64_t gtt_offset = 0, new_gtt_offset;
+ uint32_t head_offset = -1;
+ const char *buffer_name = "batch buffer";
+ char *ring_name = NULL;
+ struct gen_device_info devinfo;
+
+ while (getline(&line, &line_size, file) > 0) {
+ char *new_ring_name = NULL;
+ char *dashes;
+ line_number++;
+
+ if (sscanf(line, "%m[^ ] command stream\n", &new_ring_name) > 0) {
+ free(ring_name);
+ ring_name = new_ring_name;
+ }
+
+ dashes = strstr(line, "---");
+ if (dashes) {
+ uint32_t lo, hi;
+ char *new_ring_name = malloc(dashes - line);
+ strncpy(new_ring_name, line, dashes - line);
+ new_ring_name[dashes - line - 1] = '\0';
+
+ printf("%s", line);
+
+ matched = sscanf(dashes, "--- gtt_offset = 0x%08x %08x\n",
+ &hi, &lo);
+ if (matched > 0) {
+ new_gtt_offset = hi;
+ if (matched == 2) {
+ new_gtt_offset <<= 32;
+ new_gtt_offset |= lo;
+ }
+
+ decode(spec,
+ buffer_name, ring_name,
+ gtt_offset, head_offset,
+ data, &count);
+ gtt_offset = new_gtt_offset;
+ head_offset = -1;
+ free(ring_name);
+ ring_name = new_ring_name;
+ buffer_name = "batch buffer";
+ continue;
+ }
+
+ matched = sscanf(dashes, "--- ringbuffer = 0x%08x %08x\n",
+ &hi, &lo);
+ if (matched > 0) {
+ new_gtt_offset = hi;
+ if (matched == 2) {
+ new_gtt_offset <<= 32;
+ new_gtt_offset |= lo;
+ }
+
+ decode(spec,
+ buffer_name, ring_name,
+ gtt_offset, head_offset,
+ data, &count);
+ gtt_offset = new_gtt_offset;
+ if (head_idx < num_rings)
+ head_offset = head[head_idx++];
+ else
+ head_offset = -1;
+ free(ring_name);
+ ring_name = new_ring_name;
+ buffer_name = "ring buffer";
+ continue;
+ }
+
+ matched = sscanf(dashes, "--- HW Context = 0x%08x %08x\n",
+ &hi, &lo);
+ if (matched > 0) {
+ new_gtt_offset = hi;
+ if (matched == 2) {
+ new_gtt_offset <<= 32;
+ new_gtt_offset |= lo;
+ }
+
+ decode(spec,
+ buffer_name, ring_name,
+ gtt_offset, head_offset,
+ data, &count);
+ gtt_offset = new_gtt_offset;
+ head_offset = -1;
+ free(ring_name);
+ ring_name = new_ring_name;
+ buffer_name = "HW Context";
+ continue;
+ }
+ }
+
+ if (line[0] == ':' || line[0] == '~') {
+ count = ascii85_decode(line+1, &data, line[0] == ':');
+ if (count == 0) {
+ fprintf(stderr, "ASCII85 decode failed.\n");
+ exit(1);
+ }
+ decode(spec,
+ buffer_name, ring_name,
+ gtt_offset, head_offset,
+ data, &count);
+ continue;
+ }
+
+ matched = sscanf(line, "%08x : %08x", &offset, &value);
+ if (matched != 2) {
+ uint32_t reg, reg2;
+
+ /* display reg section is after the ringbuffers, don't mix them */
+ decode(spec,
+ buffer_name, ring_name,
+ gtt_offset, head_offset,
+ data, &count);
+
+ printf("%s", line);
+
+ matched = sscanf(line, "PCI ID: 0x%04x\n", ®);
+ if (matched == 0)
+ matched = sscanf(line, " PCI ID: 0x%04x\n", ®);
+ if (matched == 0) {
+ const char *pci_id_start = strstr(line, "PCI ID");
+ if (pci_id_start)
+ matched = sscanf(pci_id_start, "PCI ID: 0x%04x\n", ®);
+ }
+ if (matched == 1) {
+ if (!gen_get_device_info(reg, &devinfo)) {
+ printf("Unable to identify devid=%x\n", reg);
+ return;
+ }
+
+ printf("Detected GEN%i chipset\n", devinfo.gen);
+
+ if (xml_path == NULL)
+ spec = gen_spec_load(&devinfo);
+ else
+ spec = gen_spec_load_from_path(&devinfo, xml_path);
+ }
+
+ matched = sscanf(line, " CTL: 0x%08x\n", ®);
+ if (matched == 1) {
+ print_register(spec,
+ register_name_from_ring(ctl_registers,
+ ARRAY_SIZE(ctl_registers),
+ ring_name), reg);
+ }
+
+ matched = sscanf(line, " HEAD: 0x%08x\n", ®);
+ if (matched == 1) {
+ head[num_rings++] = print_head(reg);
+ }
+
+ matched = sscanf(line, " ACTHD: 0x%08x\n", ®);
+ if (matched == 1) {
+ print_register(spec,
+ register_name_from_ring(acthd_registers,
+ ARRAY_SIZE(acthd_registers),
+ ring_name), reg);
+ }
+
+ matched = sscanf(line, " PGTBL_ER: 0x%08x\n", ®);
+ if (matched == 1 && reg)
+ print_pgtbl_err(reg, &devinfo);
+
+ matched = sscanf(line, " ERROR: 0x%08x\n", ®);
+ if (matched == 1 && reg) {
+ print_register(spec, "GFX_ARB_ERROR_RPT", reg);
+ }
+
+ matched = sscanf(line, " INSTDONE: 0x%08x\n", ®);
+ if (matched == 1) {
+ const char *reg_name =
+ instdone_register_for_ring(&devinfo, ring_name);
+ if (reg_name)
+ print_register(spec, reg_name, reg);
+ }
+
+ matched = sscanf(line, " INSTDONE1: 0x%08x\n", ®);
+ if (matched == 1)
+ print_register(spec, "INSTDONE_1", reg);
+
+ matched = sscanf(line, " fence[%i] = %Lx\n", ®, &fence);
+ if (matched == 2)
+ print_fence(&devinfo, fence);
+
+ matched = sscanf(line, " FAULT_REG: 0x%08x\n", ®);
+ if (matched == 1 && reg) {
+ const char *reg_name =
+ register_name_from_ring(fault_registers,
+ ARRAY_SIZE(fault_registers),
+ ring_name);
+ if (reg_name == NULL)
+ reg_name = "FAULT_REG";
+ print_register(spec, reg_name, reg);
+ }
+
+ matched = sscanf(line, " FAULT_TLB_DATA: 0x%08x 0x%08x\n", ®,
®2);
+ if (matched == 2)
+ print_fault_data(&devinfo, reg, reg2);
+
+ continue;
+ }
+
+ count++;
+
+ if (count > data_size) {
+ data_size = data_size ? data_size * 2 : 1024;
+ data = realloc(data, data_size * sizeof (uint32_t));
+ if (data == NULL) {
+ fprintf(stderr, "Out of memory.\n");
+ exit(1);
+ }
+ }
+
+ data[count-1] = value;
+ }
+
+ decode(spec,
+ buffer_name, ring_name,
+ gtt_offset, head_offset,
+ data, &count);
+
+ free(data);
+ free(line);
+ free(ring_name);
+}
+
+static void
+setup_pager(void)
+{
+ int fds[2];
+ pid_t pid;
+
+ if (!isatty(1))
+ return;
+
+ if (pipe(fds) == -1)
+ return;
+
+ pid = fork();
+ if (pid == -1)
+ return;
+
+ if (pid == 0) {
+ close(fds[1]);
+ dup2(fds[0], 0);
+ execlp("less", "less", "-FRSi", NULL);
+ }
+
+ close(fds[0]);
+ dup2(fds[1], 1);
+ close(fds[1]);
+}
+
+static void
+print_help(const char *progname, FILE *file)
+{
+ fprintf(file,
+ "Usage: %s [OPTION]... [FILE]\n"
+ "Parse an Intel GPU i915_error_state.\n"
+ "With no FILE, debugfs-dri-directory is probed for in /debug and \n"
+ "/sys/kernel/debug. Otherwise, it may be specified. If a file is
given,\n"
+ "it is parsed as an GPU dump in the format of
/debug/dri/0/i915_error_state.\n\n"
+ " --help display this help and exit\n"
+ " --headers decode only command headers\n"
+ " --color[=WHEN] colorize the output; WHEN can be 'auto'
(default\n"
+ " if omitted), 'always', or 'never'\n"
+ " --no-pager don't launch pager\n"
+ " --no-offsets don't print instruction offsets\n"
+ " --xml=DIR load hardware xml description from directory
DIR\n",
+ progname);
+}
+
+int
+main(int argc, char *argv[])
+{
+ FILE *file;
+ const char *path;
+ struct stat st;
+ int c, i, error;
+ bool help = false, pager = true;
+ const struct option aubinator_opts[] = {
+ { "help", no_argument, (int *) &help, true },
+ { "no-pager", no_argument, (int *) &pager, false
},
+ { "no-offsets", no_argument, (int *) &option_print_offsets, false
},
+ { "headers", no_argument, (int *) &option_full_decode, false
},
+ { "color", required_argument, NULL, 'c' },
+ { "xml", required_argument, NULL, 'x' },
+ { NULL, 0, NULL, 0 }
+ };
+
+ i = 0;
+ while ((c = getopt_long(argc, argv, "", aubinator_opts, &i)) != -1) {
+ switch (c) {
+ case 'c':
+ if (optarg == NULL || strcmp(optarg, "always") == 0)
+ option_color = COLOR_ALWAYS;
+ else if (strcmp(optarg, "never") == 0)
+ option_color = COLOR_NEVER;
+ else if (strcmp(optarg, "auto") == 0)
+ option_color = COLOR_AUTO;
+ else {
+ fprintf(stderr, "invalid value for --color: %s", optarg);
+ exit(EXIT_FAILURE);
+ }
+ break;
+ case 'x':
+ xml_path = strdup(optarg);
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (help || argc == 1) {
+ print_help(argv[0], stderr);
+ exit(0);
+ }
+
+ if (optind >= argc) {
+ if (isatty(0)) {
+ path = "/sys/class/drm/card0/error";
+ error = stat(path, &st);
+ if (error != 0) {
+ path = "/debug/dri";
+ error = stat(path, &st);
+ }
+ if (error != 0) {
+ path = "/sys/kernel/debug/dri";
+ error = stat(path, &st);
+ }
+ if (error != 0) {
+ errx(1,
+ "Couldn't find i915 debugfs directory.\n\n"
+ "Is debugfs mounted? You might try mounting it with a command such
as:\n\n"
+ "\tsudo mount -t debugfs debugfs /sys/kernel/debug\n");
+ }
+ } else {
+ read_data_file(stdin);
+ exit(0);
+ }
+ } else {
+ path = argv[optind];
+ error = stat(path, &st);
+ if (error != 0) {
+ fprintf(stderr, "Error opening %s: %s\n",
+ path, strerror(errno));
+ exit(1);
+ }
+ }
+
+ if (option_color == COLOR_AUTO)
+ option_color = isatty(1) ? COLOR_ALWAYS : COLOR_NEVER;
+
+ if (isatty(1) && pager)
+ setup_pager();
+
+ if (S_ISDIR(st.st_mode)) {
+ int ret;
+ char *filename;
+
+ ret = asprintf(&filename, "%s/i915_error_state", path);
+ assert(ret > 0);
+ file = fopen(filename, "r");
+ if (!file) {
+ int minor;
+ for (minor = 0; minor < 64; minor++) {
+ free(filename);
+ ret = asprintf(&filename, "%s/%d/i915_error_state", path, minor);
+ assert(ret > 0);
+
+ file = fopen(filename, "r");
+ if (file)
+ break;
+
+ free(filename);
+ }
+ }
+ if (!file) {
+ fprintf(stderr, "Failed to find i915_error_state beneath %s\n",
+ path);
+ return EXIT_FAILURE;
+ }
+ } else {
+ file = fopen(path, "r");
+ if (!file) {
+ fprintf(stderr, "Failed to open %s: %s\n",
+ path, strerror(errno));
+ return EXIT_FAILURE;
+ }
+ }
+
+ read_data_file(file);
+ fclose(file);
+
+ /* close the stdout which is opened to write the output */
+ fflush(stdout);
+ close(1);
+ wait(NULL);
+
+ if (xml_path)
+ free(xml_path);
+
+ return EXIT_SUCCESS;
+}
+
+/* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/
--