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