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