intel/error2aub: parse other buffer types
[mesa.git] / src / intel / tools / error2aub.c
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
41 static void __attribute__ ((format(__printf__, 2, 3)))
42 fail_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
58 static 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
106 static 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
143 static void
144 print_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
154 struct bo {
155 enum bo_type {
156 BO_TYPE_UNKNOWN = 0,
157 BO_TYPE_BATCH,
158 BO_TYPE_USER,
159 BO_TYPE_CONTEXT,
160 BO_TYPE_RINGBUFFER,
161 BO_TYPE_STATUS,
162 BO_TYPE_CONTEXT_WA,
163 } type;
164 const char *name;
165 uint64_t addr;
166 uint8_t *data;
167 uint64_t size;
168
169 enum drm_i915_gem_engine_class engine_class;
170 int engine_instance;
171
172 struct list_head link;
173 };
174
175 static struct bo *
176 find_or_create(struct list_head *bo_list, uint64_t addr,
177 enum drm_i915_gem_engine_class engine_class,
178 int engine_instance)
179 {
180 list_for_each_entry(struct bo, bo_entry, bo_list, link) {
181 if (bo_entry->addr == addr &&
182 bo_entry->engine_class == engine_class &&
183 bo_entry->engine_instance == engine_instance)
184 return bo_entry;
185 }
186
187 struct bo *new_bo = calloc(1, sizeof(*new_bo));
188 new_bo->addr = addr;
189 new_bo->engine_class = engine_class;
190 new_bo->engine_instance = engine_instance;
191 list_addtail(&new_bo->link, bo_list);
192
193 return new_bo;
194 }
195
196 static void
197 engine_from_name(const char *engine_name,
198 enum drm_i915_gem_engine_class *engine_class,
199 int *engine_instance)
200 {
201 const struct {
202 const char *match;
203 enum drm_i915_gem_engine_class engine_class;
204 } rings[] = {
205 { "rcs", I915_ENGINE_CLASS_RENDER },
206 { "vcs", I915_ENGINE_CLASS_VIDEO },
207 { "vecs", I915_ENGINE_CLASS_VIDEO_ENHANCE },
208 { "bcs", I915_ENGINE_CLASS_COPY },
209 { NULL, I915_ENGINE_CLASS_INVALID },
210 }, *r;
211
212 for (r = rings; r->match; r++) {
213 if (strncasecmp(engine_name, r->match, strlen(r->match)) == 0) {
214 *engine_class = r->engine_class;
215 *engine_instance = strtol(engine_name + strlen(r->match), NULL, 10);
216 return;
217 }
218 }
219
220 fail("Unknown engine %s\n", engine_name);
221 }
222
223 int
224 main(int argc, char *argv[])
225 {
226 int i, c;
227 bool help = false;
228 char *out_filename = NULL, *in_filename = NULL;
229 const struct option aubinator_opts[] = {
230 { "help", no_argument, NULL, 'h' },
231 { "output", required_argument, NULL, 'o' },
232 { NULL, 0, NULL, 0 }
233 };
234
235 i = 0;
236 while ((c = getopt_long(argc, argv, "ho:", aubinator_opts, &i)) != -1) {
237 switch (c) {
238 case 'h':
239 help = true;
240 break;
241 case 'o':
242 out_filename = strdup(optarg);
243 break;
244 default:
245 break;
246 }
247 }
248
249 if (optind < argc)
250 in_filename = argv[optind++];
251
252 if (help || argc == 1 || !in_filename) {
253 print_help(argv[0], stderr);
254 return in_filename ? EXIT_SUCCESS : EXIT_FAILURE;
255 }
256
257 if (out_filename == NULL) {
258 int out_filename_size = strlen(in_filename) + 5;
259 out_filename = malloc(out_filename_size);
260 snprintf(out_filename, out_filename_size, "%s.aub", in_filename);
261 }
262
263 FILE *err_file = fopen(in_filename, "r");
264 fail_if(!err_file, "Failed to open error file \"%s\": %m\n", in_filename);
265
266 FILE *aub_file = fopen(out_filename, "w");
267 fail_if(!aub_file, "Failed to open aub file \"%s\": %m\n", in_filename);
268
269 struct aub_file aub = {};
270
271 enum drm_i915_gem_engine_class active_engine_class = I915_ENGINE_CLASS_INVALID;
272 int active_engine_instance = -1;
273
274 int num_ring_bos = 0;
275
276 struct list_head bo_list;
277 list_inithead(&bo_list);
278
279 struct bo *last_bo = NULL;
280
281 char *line = NULL;
282 size_t line_size;
283 while (getline(&line, &line_size, err_file) > 0) {
284 const char *pci_id_start = strstr(line, "PCI ID");
285 if (pci_id_start) {
286 int pci_id;
287 int matched = sscanf(line, "PCI ID: 0x%04x\n", &pci_id);
288 fail_if(!matched, "Invalid error state file!\n");
289
290 aub_file_init(&aub, aub_file,
291 NULL, pci_id, "error_state");
292 fail_if(!aub_use_execlists(&aub),
293 "%s currently only works on gen8+\n", argv[0]);
294
295 aub_write_default_setup(&aub);
296 continue;
297 }
298
299 if (strstr(line, " command stream:")) {
300 engine_from_name(line, &active_engine_class, &active_engine_instance);
301 continue;
302 }
303
304 const char *active_start = "Active (";
305 if (strncmp(line, active_start, strlen(active_start)) == 0) {
306 char *ring = line + strlen(active_start);
307
308 engine_from_name(ring, &active_engine_class, &active_engine_instance);
309
310 char *count = strchr(ring, '[');
311 fail_if(!count || sscanf(count, "[%d]:", &num_ring_bos) < 1,
312 "Failed to parse BO table header\n");
313 continue;
314 }
315
316 const char *global_start = "Pinned (global) [";
317 if (strncmp(line, global_start, strlen(global_start)) == 0) {
318 active_engine_class = I915_ENGINE_CLASS_INVALID;
319 active_engine_instance = -1;
320 continue;
321 }
322
323 if (num_ring_bos > 0) {
324 unsigned hi, lo, size;
325 if (sscanf(line, " %x_%x %d", &hi, &lo, &size) == 3) {
326 assert(aub_use_execlists(&aub));
327 struct bo *bo_entry = find_or_create(&bo_list, ((uint64_t)hi) << 32 | lo,
328 active_engine_class, active_engine_instance);
329 bo_entry->size = size;
330 num_ring_bos--;
331 } else {
332 fail("Not enough BO entries in the active table\n");
333 }
334 continue;
335 }
336
337 if (line[0] == ':' || line[0] == '~') {
338 if (!last_bo || last_bo->type == BO_TYPE_UNKNOWN)
339 continue;
340
341 int count = ascii85_decode(line+1, (uint32_t **) &last_bo->data, line[0] == ':');
342 fail_if(count == 0, "ASCII85 decode failed.\n");
343 last_bo->size = count * 4;
344 continue;
345 }
346
347 char *dashes = strstr(line, " --- ");
348 if (dashes) {
349 dashes += 5;
350
351 engine_from_name(line, &active_engine_class, &active_engine_instance);
352
353 uint32_t hi, lo;
354 char *bo_address_str = strchr(dashes, '=');
355 if (!bo_address_str || sscanf(bo_address_str, "= 0x%08x %08x\n", &hi, &lo) != 2)
356 continue;
357
358 last_bo = find_or_create(&bo_list, ((uint64_t) hi) << 32 | lo,
359 active_engine_class, active_engine_instance);
360
361 const struct {
362 const char *match;
363 enum bo_type type;
364 } bo_types[] = {
365 { "gtt_offset", BO_TYPE_BATCH },
366 { "user", BO_TYPE_USER },
367 { "HW context", BO_TYPE_CONTEXT },
368 { "ringbuffer", BO_TYPE_RINGBUFFER },
369 { "HW Status", BO_TYPE_STATUS },
370 { "WA context", BO_TYPE_CONTEXT_WA },
371 { "unknown", BO_TYPE_UNKNOWN },
372 }, *b;
373
374 for (b = bo_types; b->type != BO_TYPE_UNKNOWN; b++) {
375 if (strncasecmp(dashes, b->match, strlen(b->match)) == 0)
376 break;
377 }
378
379 /* The batch buffer will appear twice as gtt_offset and user. Only
380 * keep the batch type.
381 */
382 if (last_bo->type == BO_TYPE_UNKNOWN) {
383 last_bo->type = b->type;
384 last_bo->name = b->match;
385 }
386
387 continue;
388 }
389 }
390
391 /* Add all the BOs to the aub file */
392 list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
393 switch (bo_entry->type) {
394 case BO_TYPE_BATCH:
395 aub_map_ppgtt(&aub, bo_entry->addr, bo_entry->size);
396 aub_write_trace_block(&aub, AUB_TRACE_TYPE_BATCH,
397 bo_entry->data, bo_entry->size, bo_entry->addr);
398 break;
399 case BO_TYPE_USER:
400 aub_map_ppgtt(&aub, bo_entry->addr, bo_entry->size);
401 aub_write_trace_block(&aub, AUB_TRACE_TYPE_NOTYPE,
402 bo_entry->data, bo_entry->size, bo_entry->addr);
403 default:
404 break;
405 }
406 }
407
408 /* Finally exec the batch BO */
409 bool batch_found = false;
410 list_for_each_entry(struct bo, bo_entry, &bo_list, link) {
411 if (bo_entry->type == BO_TYPE_BATCH) {
412 aub_write_exec(&aub, bo_entry->addr, aub_gtt_size(&aub), bo_entry->engine_class);
413 batch_found = true;
414 break;
415 }
416 }
417 fail_if(!batch_found, "Failed to find batch buffer.\n");
418
419 /* Cleanup */
420 list_for_each_entry_safe(struct bo, bo_entry, &bo_list, link) {
421 list_del(&bo_entry->link);
422 free(bo_entry->data);
423 free(bo_entry);
424 }
425
426 free(out_filename);
427 free(line);
428 if(err_file) {
429 fclose(err_file);
430 }
431 if(aub.file) {
432 aub_file_finish(&aub);
433 } else if(aub_file) {
434 fclose(aub_file);
435 }
436 return EXIT_SUCCESS;
437 }
438
439 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/