xref: /aosp_15_r20/external/mesa3d/src/intel/tools/error2aub.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2018 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 <assert.h>
26 #include <getopt.h>
27 #include <inttypes.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <zlib.h>
34 
35 #include "util/list.h"
36 
37 #include "aub_write.h"
38 #include "error_decode_lib.h"
39 #include "intel_aub.h"
40 
41 #define fail_if(cond, ...) _fail_if(cond, NULL, __VA_ARGS__)
42 
43 #define fail(...) fail_if(true, __VA_ARGS__)
44 
zlib_inflate(uint32_t ** ptr,int len)45 static int zlib_inflate(uint32_t **ptr, int len)
46 {
47    struct z_stream_s zstream;
48    void *out;
49    const uint32_t out_size = 128*4096;  /* approximate obj size */
50 
51    memset(&zstream, 0, sizeof(zstream));
52 
53    zstream.next_in = (unsigned char *)*ptr;
54    zstream.avail_in = 4*len;
55 
56    if (inflateInit(&zstream) != Z_OK)
57       return 0;
58 
59    out = malloc(out_size);
60    zstream.next_out = out;
61    zstream.avail_out = out_size;
62 
63    do {
64       switch (inflate(&zstream, Z_SYNC_FLUSH)) {
65       case Z_STREAM_END:
66          goto end;
67       case Z_OK:
68          break;
69       default:
70          inflateEnd(&zstream);
71          return 0;
72       }
73 
74       if (zstream.avail_out)
75          break;
76 
77       out = realloc(out, 2*zstream.total_out);
78       if (out == NULL) {
79          inflateEnd(&zstream);
80          return 0;
81       }
82 
83       zstream.next_out = (unsigned char *)out + zstream.total_out;
84       zstream.avail_out = zstream.total_out;
85    } while (1);
86  end:
87    inflateEnd(&zstream);
88    free(*ptr);
89    *ptr = out;
90    return zstream.total_out / 4;
91 }
92 
ascii85_decode(const char * in,uint32_t ** out,bool inflate)93 static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
94 {
95    int len = 0, size = 1024;
96 
97    *out = realloc(*out, sizeof(uint32_t)*size);
98    if (*out == NULL)
99       return 0;
100 
101    while (*in >= '!' && *in <= 'z') {
102       uint32_t v = 0;
103 
104       if (len == size) {
105          size *= 2;
106          *out = realloc(*out, sizeof(uint32_t)*size);
107          if (*out == NULL)
108             return 0;
109       }
110 
111       in = ascii85_decode_char(in, &v);
112       (*out)[len++] = v;
113    }
114 
115    if (!inflate)
116       return len;
117 
118    return zlib_inflate(out, len);
119 }
120 
121 static void
print_help(const char * progname,FILE * file)122 print_help(const char *progname, FILE *file)
123 {
124    fprintf(file,
125            "Usage: %s [OPTION]... [FILE]\n"
126            "Convert an Intel GPU i915 error state to an aub file.\n"
127            "  -h, --help          display this help and exit\n"
128            "  -o, --output=FILE   the output aub file (default FILE.aub)\n",
129            progname);
130 }
131 
132 struct bo {
133    enum address_space {
134       PPGTT,
135       GGTT,
136    } gtt;
137    enum bo_type {
138       BO_TYPE_UNKNOWN = 0,
139       BO_TYPE_BATCH,
140       BO_TYPE_USER,
141       BO_TYPE_CONTEXT,
142       BO_TYPE_RINGBUFFER,
143       BO_TYPE_STATUS,
144       BO_TYPE_CONTEXT_WA,
145    } type;
146    const char *name;
147    uint64_t addr;
148    uint8_t *data;
149    uint64_t size;
150 
151    enum intel_engine_class engine_class;
152    int engine_instance;
153 
154    struct list_head link;
155 };
156 
157 static struct bo *
find_or_create(struct list_head * bo_list,uint64_t addr,enum address_space gtt,enum intel_engine_class engine_class,int engine_instance)158 find_or_create(struct list_head *bo_list, uint64_t addr,
159                enum address_space gtt,
160                enum intel_engine_class engine_class,
161                int engine_instance)
162 {
163    list_for_each_entry(struct bo, bo_entry, bo_list, link) {
164       if (bo_entry->addr == addr &&
165           bo_entry->gtt == gtt &&
166           bo_entry->engine_class == engine_class &&
167           bo_entry->engine_instance == engine_instance)
168          return bo_entry;
169    }
170 
171    struct bo *new_bo = calloc(1, sizeof(*new_bo));
172    new_bo->addr = addr;
173    new_bo->gtt = gtt;
174    new_bo->engine_class = engine_class;
175    new_bo->engine_instance = engine_instance;
176    list_addtail(&new_bo->link, bo_list);
177 
178    return new_bo;
179 }
180 
181 static void
engine_from_name(const char * engine_name,enum intel_engine_class * engine_class,int * engine_instance)182 engine_from_name(const char *engine_name,
183                  enum intel_engine_class *engine_class,
184                  int *engine_instance)
185 {
186    const struct {
187       const char *match;
188       enum intel_engine_class engine_class;
189       bool parse_instance;
190    } rings[] = {
191       { "rcs", INTEL_ENGINE_CLASS_RENDER, true },
192       { "vcs", INTEL_ENGINE_CLASS_VIDEO, true },
193       { "vecs", INTEL_ENGINE_CLASS_VIDEO_ENHANCE, true },
194       { "bcs", INTEL_ENGINE_CLASS_COPY, true },
195       { "global", INTEL_ENGINE_CLASS_INVALID, false },
196       { "render command stream", INTEL_ENGINE_CLASS_RENDER, false },
197       { "blt command stream", INTEL_ENGINE_CLASS_COPY, false },
198       { "bsd command stream", INTEL_ENGINE_CLASS_VIDEO, false },
199       { "vebox command stream", INTEL_ENGINE_CLASS_VIDEO_ENHANCE, false },
200       { NULL, INTEL_ENGINE_CLASS_INVALID },
201    }, *r;
202 
203    for (r = rings; r->match; r++) {
204       if (strncasecmp(engine_name, r->match, strlen(r->match)) == 0) {
205          *engine_class = r->engine_class;
206          if (r->parse_instance)
207             *engine_instance = strtol(engine_name + strlen(r->match), NULL, 10);
208          else
209             *engine_instance = 0;
210          return;
211       }
212    }
213 
214    fail("Unknown engine %s\n", engine_name);
215 }
216 
217 int
main(int argc,char * argv[])218 main(int argc, char *argv[])
219 {
220    int i, c;
221    bool help = false, verbose = false;
222    char *out_filename = NULL, *in_filename = NULL;
223    const struct option aubinator_opts[] = {
224       { "help",       no_argument,       NULL,     'h' },
225       { "output",     required_argument, NULL,     'o' },
226       { "verbose",    no_argument,       NULL,     'v' },
227       { NULL,         0,                 NULL,     0 }
228    };
229 
230    i = 0;
231    while ((c = getopt_long(argc, argv, "ho:v", aubinator_opts, &i)) != -1) {
232       switch (c) {
233       case 'h':
234          help = true;
235          break;
236       case 'o':
237          out_filename = strdup(optarg);
238          break;
239       case 'v':
240          verbose = true;
241          break;
242       default:
243          break;
244       }
245    }
246 
247    if (optind < argc)
248       in_filename = argv[optind++];
249 
250    if (help || argc == 1 || !in_filename) {
251       print_help(argv[0], stderr);
252       return in_filename ? EXIT_SUCCESS : EXIT_FAILURE;
253    }
254 
255    if (out_filename == NULL) {
256       int out_filename_size = strlen(in_filename) + 5;
257       out_filename = malloc(out_filename_size);
258       snprintf(out_filename, out_filename_size, "%s.aub", in_filename);
259    }
260 
261    FILE *err_file = fopen(in_filename, "r");
262    fail_if(!err_file, "Failed to open error file \"%s\": %m\n", in_filename);
263 
264    FILE *aub_file = fopen(out_filename, "w");
265    fail_if(!aub_file, "Failed to open aub file \"%s\": %m\n", in_filename);
266 
267    struct aub_file aub = {};
268 
269    enum intel_engine_class active_engine_class = INTEL_ENGINE_CLASS_INVALID;
270    int active_engine_instance = -1;
271 
272    enum address_space active_gtt = PPGTT;
273    enum address_space default_gtt = PPGTT;
274 
275    struct {
276       struct {
277          uint32_t ring_buffer_head;
278          uint32_t ring_buffer_tail;
279       } instances[3];
280    } engines[INTEL_ENGINE_CLASS_INVALID + 1];
281    memset(engines, 0, sizeof(engines));
282 
283    int num_ring_bos = 0;
284 
285    struct list_head bo_list;
286    list_inithead(&bo_list);
287 
288    struct bo *last_bo = NULL;
289 
290    char *line = NULL;
291    size_t line_size;
292    while (getline(&line, &line_size, err_file) > 0) {
293       const char *pci_id_start = strstr(line, "PCI ID");
294       if (pci_id_start) {
295          int pci_id;
296          int matched = sscanf(line, "PCI ID: 0x%04x\n", &pci_id);
297          fail_if(!matched, "Invalid error state file!\n");
298 
299          aub_file_init(&aub, aub_file,
300                        NULL, pci_id, "error_state");
301          if (verbose)
302             aub.verbose_log_file = stdout;
303          default_gtt = active_gtt = aub_use_execlists(&aub) ? PPGTT : GGTT;
304          continue;
305       }
306 
307       if (strstr(line, " command stream:")) {
308          engine_from_name(line, &active_engine_class, &active_engine_instance);
309          continue;
310       }
311 
312       if (sscanf(line, "  ring->head: 0x%x\n",
313                  &engines[
314                     active_engine_class].instances[
315                        active_engine_instance].ring_buffer_head) == 1) {
316          continue;
317       }
318 
319       if (sscanf(line, "  ring->tail: 0x%x\n",
320                  &engines[
321                     active_engine_class].instances[
322                        active_engine_instance].ring_buffer_tail) == 1) {
323          continue;
324       }
325 
326       const char *active_start = "Active (";
327       if (strncmp(line, active_start, strlen(active_start)) == 0) {
328          char *ring = line + strlen(active_start);
329 
330          engine_from_name(ring, &active_engine_class, &active_engine_instance);
331          active_gtt = default_gtt;
332 
333          char *count = strchr(ring, '[');
334          fail_if(!count || sscanf(count, "[%d]:", &num_ring_bos) < 1,
335                  "Failed to parse BO table header\n");
336          continue;
337       }
338 
339       const char *global_start = "Pinned (global) [";
340       if (strncmp(line, global_start, strlen(global_start)) == 0) {
341          active_engine_class = INTEL_ENGINE_CLASS_INVALID;
342          active_engine_instance = -1;
343          active_gtt = GGTT;
344          continue;
345       }
346 
347       if (num_ring_bos > 0) {
348          unsigned hi, lo, size;
349          if (sscanf(line, " %x_%x %d", &hi, &lo, &size) == 3) {
350             struct bo *bo_entry = find_or_create(&bo_list, ((uint64_t)hi) << 32 | lo,
351                                                  active_gtt,
352                                                  active_engine_class,
353                                                  active_engine_instance);
354             bo_entry->size = size;
355             num_ring_bos--;
356          } else {
357             fail("Not enough BO entries in the active table\n");
358          }
359          continue;
360       }
361 
362       if (line[0] == ':' || line[0] == '~') {
363          if (!last_bo || last_bo->type == BO_TYPE_UNKNOWN)
364             continue;
365 
366          int count = ascii85_decode(line+1, (uint32_t **) &last_bo->data, line[0] == ':');
367          fail_if(count == 0, "ASCII85 decode failed.\n");
368          last_bo->size = count * 4;
369          continue;
370       }
371 
372       char *dashes = strstr(line, " --- ");
373       if (dashes) {
374          dashes += 5;
375 
376          engine_from_name(line, &active_engine_class, &active_engine_instance);
377 
378          uint32_t hi, lo;
379          char *bo_address_str = strchr(dashes, '=');
380          if (!bo_address_str || sscanf(bo_address_str, "= 0x%08x %08x\n", &hi, &lo) != 2)
381             continue;
382 
383          const struct {
384             const char *match;
385             enum bo_type type;
386             enum address_space gtt;
387          } bo_types[] = {
388             { "gtt_offset", BO_TYPE_BATCH,      default_gtt },
389             { "batch",      BO_TYPE_BATCH,      default_gtt },
390             { "user",       BO_TYPE_USER,       default_gtt },
391             { "HW context", BO_TYPE_CONTEXT,    GGTT },
392             { "ringbuffer", BO_TYPE_RINGBUFFER, GGTT },
393             { "HW Status",  BO_TYPE_STATUS,     GGTT },
394             { "WA context", BO_TYPE_CONTEXT_WA, GGTT },
395             { "unknown",    BO_TYPE_UNKNOWN,    GGTT },
396          }, *b;
397 
398          for (b = bo_types; b->type != BO_TYPE_UNKNOWN; b++) {
399             if (strncasecmp(dashes, b->match, strlen(b->match)) == 0)
400                break;
401          }
402 
403          last_bo = find_or_create(&bo_list, ((uint64_t) hi) << 32 | lo,
404                                   b->gtt,
405                                   active_engine_class, active_engine_instance);
406 
407          /* The batch buffer will appear twice as gtt_offset and user. Only
408           * keep the batch type.
409           */
410          if (last_bo->type == BO_TYPE_UNKNOWN) {
411             last_bo->type = b->type;
412             last_bo->name = b->match;
413          }
414 
415          continue;
416       }
417    }
418 
419    if (verbose) {
420       fprintf(stdout, "BOs found:\n");
421       list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
422          fprintf(stdout, "\t type=%i addr=0x%016" PRIx64 " size=%" PRIu64 "\n",
423                  bo_entry->type, bo_entry->addr, bo_entry->size);
424       }
425    }
426 
427    /* Find the batch that trigger the hang */
428    struct bo *batch_bo = NULL;
429    list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
430       if (bo_entry->type == BO_TYPE_BATCH) {
431          batch_bo = bo_entry;
432          break;
433       }
434    }
435    fail_if(!batch_bo, "Failed to find batch buffer.\n");
436 
437    /* Add all the BOs to the aub file */
438    struct bo *hwsp_bo = NULL;
439    list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
440       switch (bo_entry->type) {
441       case BO_TYPE_BATCH:
442          if (bo_entry->gtt == PPGTT) {
443             aub_map_ppgtt(&aub, bo_entry->addr, bo_entry->size);
444             aub_write_trace_block(&aub, AUB_TRACE_TYPE_BATCH,
445                                   bo_entry->data, bo_entry->size, bo_entry->addr);
446          } else
447             aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
448          break;
449       case BO_TYPE_USER:
450          if (bo_entry->gtt == PPGTT) {
451             aub_map_ppgtt(&aub, bo_entry->addr, bo_entry->size);
452             aub_write_trace_block(&aub, AUB_TRACE_TYPE_NOTYPE,
453                                   bo_entry->data, bo_entry->size, bo_entry->addr);
454          } else
455             aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
456          break;
457       case BO_TYPE_CONTEXT:
458          if (bo_entry->engine_class == batch_bo->engine_class &&
459              bo_entry->engine_instance == batch_bo->engine_instance &&
460              aub_use_execlists(&aub)) {
461             hwsp_bo = bo_entry;
462 
463             uint32_t *context = (uint32_t *) (bo_entry->data + 4096 /* GuC */ + 4096 /* HWSP */);
464 
465             if (context[1] == 0) {
466                fprintf(stderr,
467                        "Invalid context image data.\n"
468                        "This is likely a kernel issue : https://bugs.freedesktop.org/show_bug.cgi?id=107691\n");
469             }
470 
471             /* Update the ring buffer at the last known location. */
472             context[5] = engines[bo_entry->engine_class].instances[bo_entry->engine_instance].ring_buffer_head;
473             context[7] = engines[bo_entry->engine_class].instances[bo_entry->engine_instance].ring_buffer_tail;
474             fprintf(stdout, "engine start=0x%x head/tail=0x%x/0x%x\n",
475                     context[9], context[5], context[7]);
476 
477             /* The error state doesn't provide a dump of the page tables, so
478              * we have to provide our own, that's easy enough.
479              */
480             context[49] = aub.pml4.phys_addr >> 32;
481             context[51] = aub.pml4.phys_addr & 0xffffffff;
482 
483             fprintf(stdout, "context dump:\n");
484             for (int i = 0; i < 60; i++) {
485                if (i % 4 == 0)
486                   fprintf(stdout, "\n 0x%08" PRIx64 ": ", bo_entry->addr + 8192 + i * 4);
487                fprintf(stdout, "0x%08x ", context[i]);
488             }
489             fprintf(stdout, "\n");
490 
491          }
492          aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
493          break;
494       case BO_TYPE_RINGBUFFER:
495       case BO_TYPE_STATUS:
496       case BO_TYPE_CONTEXT_WA:
497          aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, bo_entry->data);
498          break;
499       case BO_TYPE_UNKNOWN:
500          if (bo_entry->gtt == PPGTT) {
501             aub_map_ppgtt(&aub, bo_entry->addr, bo_entry->size);
502             if (bo_entry->data) {
503                aub_write_trace_block(&aub, AUB_TRACE_TYPE_NOTYPE,
504                                      bo_entry->data, bo_entry->size, bo_entry->addr);
505             }
506          } else {
507             if (bo_entry->size > 0) {
508                void *zero_data = calloc(1, bo_entry->size);
509                aub_write_ggtt(&aub, bo_entry->addr, bo_entry->size, zero_data);
510                free(zero_data);
511             }
512          }
513          break;
514       default:
515          break;
516       }
517    }
518 
519    if (aub_use_execlists(&aub)) {
520       fail_if(!hwsp_bo, "Failed to find Context buffer.\n");
521       aub_write_context_execlists(&aub, hwsp_bo->addr + 4096 /* skip GuC page */, hwsp_bo->engine_class);
522    } else {
523       /* Use context id 0 -- if we are not using execlists it doesn't matter
524        * anyway
525        */
526       aub_write_exec(&aub, 0, batch_bo->addr, 0, INTEL_ENGINE_CLASS_RENDER);
527    }
528 
529    /* Cleanup */
530    list_for_each_entry_safe(struct bo, bo_entry, &bo_list, link) {
531       list_del(&bo_entry->link);
532       free(bo_entry->data);
533       free(bo_entry);
534    }
535 
536    free(out_filename);
537    free(line);
538    if(err_file) {
539       fclose(err_file);
540    }
541    if(aub.file) {
542       aub_file_finish(&aub);
543    } else if(aub_file) {
544       fclose(aub_file);
545    }
546    return EXIT_SUCCESS;
547 }
548 
549 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/
550