1 /*
2 * Copyright © 2022 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 "error_decode_lib.h"
38 #include "error2hangdump_lib.h"
39 #include "error2hangdump_xe.h"
40 #include "intel/dev/intel_device_info.h"
41
42 #define XE_KMD_ERROR_DUMP_IDENTIFIER "**** Xe Device Coredump ****"
43
zlib_inflate(uint32_t ** ptr,int len)44 static int zlib_inflate(uint32_t **ptr, int len)
45 {
46 struct z_stream_s zstream;
47 void *out;
48 const uint32_t out_size = 128*4096; /* approximate obj size */
49
50 memset(&zstream, 0, sizeof(zstream));
51
52 zstream.next_in = (unsigned char *)*ptr;
53 zstream.avail_in = 4*len;
54
55 if (inflateInit(&zstream) != Z_OK)
56 return 0;
57
58 out = malloc(out_size);
59 zstream.next_out = out;
60 zstream.avail_out = out_size;
61
62 do {
63 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
64 case Z_STREAM_END:
65 goto end;
66 case Z_OK:
67 break;
68 default:
69 inflateEnd(&zstream);
70 return 0;
71 }
72
73 if (zstream.avail_out)
74 break;
75
76 out = realloc(out, 2*zstream.total_out);
77 if (out == NULL) {
78 inflateEnd(&zstream);
79 return 0;
80 }
81
82 zstream.next_out = (unsigned char *)out + zstream.total_out;
83 zstream.avail_out = zstream.total_out;
84 } while (1);
85 end:
86 inflateEnd(&zstream);
87 free(*ptr);
88 *ptr = out;
89 return zstream.total_out / 4;
90 }
91
ascii85_decode(const char * in,uint32_t ** out,bool inflate)92 static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
93 {
94 int len = 0, size = 1024;
95
96 *out = realloc(*out, sizeof(uint32_t)*size);
97 if (*out == NULL)
98 return 0;
99
100 while (*in >= '!' && *in <= 'z') {
101 uint32_t v = 0;
102
103 if (len == size) {
104 size *= 2;
105 *out = realloc(*out, sizeof(uint32_t)*size);
106 if (*out == NULL)
107 return 0;
108 }
109
110 in = ascii85_decode_char(in, &v);
111 (*out)[len++] = v;
112 }
113
114 if (!inflate)
115 return len;
116
117 return zlib_inflate(out, len);
118 }
119
120 static void
print_help(const char * progname,FILE * file)121 print_help(const char *progname, FILE *file)
122 {
123 fprintf(file,
124 "Usage: %s [OPTION]... [FILE]\n"
125 "Convert an Intel GPU i915 error state to a hang dump file, replayable with intel_hang_replay.\n"
126 " -h, --help display this help and exit\n"
127 " -o, --output=FILE the output dump file (default FILE.dmp)\n",
128 progname);
129 }
130
131 struct bo {
132 enum address_space {
133 PPGTT,
134 GGTT,
135 } gtt;
136 enum bo_type {
137 BO_TYPE_UNKNOWN = 0,
138 BO_TYPE_BATCH,
139 BO_TYPE_USER,
140 BO_TYPE_CONTEXT,
141 BO_TYPE_RINGBUFFER,
142 BO_TYPE_STATUS,
143 BO_TYPE_CONTEXT_WA,
144 } type;
145 const char *name;
146 uint64_t addr;
147 uint8_t *data;
148 uint64_t size;
149
150 enum intel_engine_class engine_class;
151 int engine_instance;
152
153 struct list_head link;
154 };
155
156 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)157 find_or_create(struct list_head *bo_list, uint64_t addr,
158 enum address_space gtt,
159 enum intel_engine_class engine_class,
160 int engine_instance)
161 {
162 list_for_each_entry(struct bo, bo_entry, bo_list, link) {
163 if (bo_entry->addr == addr &&
164 bo_entry->gtt == gtt &&
165 bo_entry->engine_class == engine_class &&
166 bo_entry->engine_instance == engine_instance)
167 return bo_entry;
168 }
169
170 struct bo *new_bo = calloc(1, sizeof(*new_bo));
171 new_bo->addr = addr;
172 new_bo->gtt = gtt;
173 new_bo->engine_class = engine_class;
174 new_bo->engine_instance = engine_instance;
175 list_addtail(&new_bo->link, bo_list);
176
177 return new_bo;
178 }
179
180 static void
engine_from_name(const char * engine_name,enum intel_engine_class * engine_class,int * engine_instance)181 engine_from_name(const char *engine_name,
182 enum intel_engine_class *engine_class,
183 int *engine_instance)
184 {
185 const struct {
186 const char *match;
187 enum intel_engine_class engine_class;
188 bool parse_instance;
189 } rings[] = {
190 { "rcs", INTEL_ENGINE_CLASS_RENDER, true },
191 { "vcs", INTEL_ENGINE_CLASS_VIDEO, true },
192 { "vecs", INTEL_ENGINE_CLASS_VIDEO_ENHANCE, true },
193 { "bcs", INTEL_ENGINE_CLASS_COPY, true },
194 { "global", INTEL_ENGINE_CLASS_INVALID, false },
195 { "render command stream", INTEL_ENGINE_CLASS_RENDER, false },
196 { "blt command stream", INTEL_ENGINE_CLASS_COPY, false },
197 { "bsd command stream", INTEL_ENGINE_CLASS_VIDEO, false },
198 { "vebox command stream", INTEL_ENGINE_CLASS_VIDEO_ENHANCE, false },
199 { NULL, INTEL_ENGINE_CLASS_INVALID },
200 }, *r;
201
202 for (r = rings; r->match; r++) {
203 if (strncasecmp(engine_name, r->match, strlen(r->match)) == 0) {
204 *engine_class = r->engine_class;
205 if (r->parse_instance)
206 *engine_instance = strtol(engine_name + strlen(r->match), NULL, 10);
207 else
208 *engine_instance = 0;
209 return;
210 }
211 }
212
213 fail("Unknown engine %s\n", engine_name);
214 }
215
216 static void
read_i915_data_file(FILE * err_file,FILE * hang_file,bool verbose,enum intel_engine_class capture_engine)217 read_i915_data_file(FILE *err_file, FILE *hang_file, bool verbose, enum intel_engine_class capture_engine)
218 {
219 enum address_space active_gtt = PPGTT;
220 enum address_space default_gtt = PPGTT;
221
222 int num_ring_bos = 0;
223
224 struct list_head bo_list;
225 list_inithead(&bo_list);
226
227 struct bo *last_bo = NULL;
228
229 enum intel_engine_class active_engine_class = INTEL_ENGINE_CLASS_INVALID;
230 int active_engine_instance = -1;
231
232 char *line = NULL;
233 size_t line_size;
234 while (getline(&line, &line_size, err_file) > 0) {
235 if (strstr(line, " command stream:")) {
236 engine_from_name(line, &active_engine_class, &active_engine_instance);
237 continue;
238 }
239
240 if (num_ring_bos > 0) {
241 unsigned hi, lo, size;
242 if (sscanf(line, " %x_%x %d", &hi, &lo, &size) == 3) {
243 struct bo *bo_entry = find_or_create(&bo_list, ((uint64_t)hi) << 32 | lo,
244 active_gtt,
245 active_engine_class,
246 active_engine_instance);
247 bo_entry->size = size;
248 num_ring_bos--;
249 } else {
250 fail("Not enough BO entries in the active table\n");
251 }
252 continue;
253 }
254
255 if (line[0] == ':' || line[0] == '~') {
256 if (!last_bo || last_bo->type == BO_TYPE_UNKNOWN)
257 continue;
258
259 int count = ascii85_decode(line+1, (uint32_t **) &last_bo->data, line[0] == ':');
260 fail_if(count == 0, "ASCII85 decode failed.\n");
261 last_bo->size = count * 4;
262 continue;
263 }
264
265 char *dashes = strstr(line, " --- ");
266 if (dashes) {
267 dashes += 5;
268
269 engine_from_name(line, &active_engine_class, &active_engine_instance);
270
271 uint32_t hi, lo;
272 char *bo_address_str = strchr(dashes, '=');
273 if (!bo_address_str || sscanf(bo_address_str, "= 0x%08x %08x\n", &hi, &lo) != 2)
274 continue;
275
276 const struct {
277 const char *match;
278 enum bo_type type;
279 enum address_space gtt;
280 } bo_types[] = {
281 { "gtt_offset", BO_TYPE_BATCH, default_gtt },
282 { "batch", BO_TYPE_BATCH, default_gtt },
283 { "user", BO_TYPE_USER, default_gtt },
284 { "HW context", BO_TYPE_CONTEXT, GGTT },
285 { "ringbuffer", BO_TYPE_RINGBUFFER, GGTT },
286 { "HW Status", BO_TYPE_STATUS, GGTT },
287 { "WA context", BO_TYPE_CONTEXT_WA, GGTT },
288 { "unknown", BO_TYPE_UNKNOWN, GGTT },
289 }, *b;
290
291 for (b = bo_types; b->type != BO_TYPE_UNKNOWN; b++) {
292 if (strncasecmp(dashes, b->match, strlen(b->match)) == 0)
293 break;
294 }
295
296 last_bo = find_or_create(&bo_list, ((uint64_t) hi) << 32 | lo,
297 b->gtt,
298 active_engine_class, active_engine_instance);
299
300 /* The batch buffer will appear twice as gtt_offset and user. Only
301 * keep the batch type.
302 */
303 if (last_bo->type == BO_TYPE_UNKNOWN) {
304 last_bo->type = b->type;
305 last_bo->name = b->match;
306 }
307
308 continue;
309 }
310 }
311
312 if (verbose) {
313 fprintf(stdout, "BOs found:\n");
314 list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
315 fprintf(stdout, "\t type=%i addr=0x%016" PRIx64 " size=%" PRIu64 "\n",
316 bo_entry->type, bo_entry->addr, bo_entry->size);
317 }
318 }
319
320 /* Find the batch that trigger the hang */
321 struct bo *batch_bo = NULL, *hw_image_bo = NULL;
322 list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
323 if (batch_bo != NULL && hw_image_bo != NULL)
324 break;
325
326 if (bo_entry->engine_class != capture_engine)
327 continue;
328
329 switch (bo_entry->type) {
330 case BO_TYPE_BATCH:
331 batch_bo = bo_entry;
332 break;
333 case BO_TYPE_CONTEXT:
334 hw_image_bo = bo_entry;
335 break;
336 default:
337 break;
338 }
339 }
340 fail_if(!batch_bo, "Failed to find batch buffer.\n");
341 fail_if(!hw_image_bo, "Failed to find HW image buffer.\n");
342
343 /* Add all the user BOs to the aub file */
344 list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
345 if (bo_entry->type == BO_TYPE_USER && bo_entry->gtt == PPGTT)
346 write_buffer(hang_file, bo_entry->addr, bo_entry->data, bo_entry->size, "user");
347 }
348
349 write_buffer(hang_file, batch_bo->addr, batch_bo->data, batch_bo->size, "batch");
350 fprintf(stderr, "writing image buffer 0x%016"PRIx64" size=0x%016"PRIx64"\n",
351 hw_image_bo->addr, hw_image_bo->size);
352 write_hw_image_buffer(hang_file, hw_image_bo->data, hw_image_bo->size);
353 write_exec(hang_file, batch_bo->addr);
354
355 /* Cleanup */
356 list_for_each_entry_safe(struct bo, bo_entry, &bo_list, link) {
357 list_del(&bo_entry->link);
358 free(bo_entry->data);
359 free(bo_entry);
360 }
361
362 free(line);
363 }
364
365 int
main(int argc,char * argv[])366 main(int argc, char *argv[])
367 {
368 int i, c;
369 bool help = false, verbose = false;
370 char *out_filename = NULL, *in_filename = NULL, *capture_engine_name = "rcs";
371 const struct option aubinator_opts[] = {
372 { "help", no_argument, NULL, 'h' },
373 { "output", required_argument, NULL, 'o' },
374 { "verbose", no_argument, NULL, 'v' },
375 { "engine", required_argument, NULL, 'e' },
376 { NULL, 0, NULL, 0 }
377 };
378 char *line = NULL;
379 size_t line_size;
380
381 i = 0;
382 while ((c = getopt_long(argc, argv, "ho:v", aubinator_opts, &i)) != -1) {
383 switch (c) {
384 case 'h':
385 help = true;
386 break;
387 case 'o':
388 out_filename = strdup(optarg);
389 break;
390 case 'v':
391 verbose = true;
392 break;
393 case 'e':
394 capture_engine_name = optarg;
395 break;
396 default:
397 break;
398 }
399 }
400
401 if (optind < argc)
402 in_filename = argv[optind++];
403
404 if (help || argc == 1 || !in_filename) {
405 print_help(argv[0], stderr);
406 return in_filename ? EXIT_SUCCESS : EXIT_FAILURE;
407 }
408
409 enum intel_engine_class capture_engine;
410 engine_from_name(capture_engine_name, &capture_engine, &c);
411
412 if (out_filename == NULL) {
413 int out_filename_size = strlen(in_filename) + 5;
414 out_filename = malloc(out_filename_size);
415 snprintf(out_filename, out_filename_size, "%s.dmp", in_filename);
416 }
417
418 FILE *err_file = fopen(in_filename, "r");
419 fail_if(!err_file, "Failed to open error file \"%s\": %m\n", in_filename);
420
421 FILE *hang_file = fopen(out_filename, "w");
422 fail_if(!hang_file, "Failed to open aub file \"%s\": %m\n", out_filename);
423
424 getline(&line, &line_size, err_file);
425 rewind(err_file);
426 if (strncmp(line, XE_KMD_ERROR_DUMP_IDENTIFIER, strlen(XE_KMD_ERROR_DUMP_IDENTIFIER)) == 0)
427 read_xe_data_file(err_file, hang_file, verbose);
428 else
429 read_i915_data_file(err_file, hang_file, verbose, capture_engine);
430
431 free(line);
432 free(out_filename);
433 if (err_file)
434 fclose(err_file);
435 fclose(hang_file);
436
437 return EXIT_SUCCESS;
438 }
439
440 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/
441