intel/tools: Add an error state to aub translator
[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 <zlib.h>
33
34 #include "aub_write.h"
35 #include "i915_drm.h"
36 #include "intel_aub.h"
37
38 static void __attribute__ ((format(__printf__, 2, 3)))
39 fail_if(int cond, const char *format, ...)
40 {
41 va_list args;
42
43 if (!cond)
44 return;
45
46 va_start(args, format);
47 vfprintf(stderr, format, args);
48 va_end(args);
49
50 raise(SIGTRAP);
51 }
52
53 #define fail(...) fail_if(true, __VA_ARGS__)
54
55 static int zlib_inflate(uint32_t **ptr, int len)
56 {
57 struct z_stream_s zstream;
58 void *out;
59 const uint32_t out_size = 128*4096; /* approximate obj size */
60
61 memset(&zstream, 0, sizeof(zstream));
62
63 zstream.next_in = (unsigned char *)*ptr;
64 zstream.avail_in = 4*len;
65
66 if (inflateInit(&zstream) != Z_OK)
67 return 0;
68
69 out = malloc(out_size);
70 zstream.next_out = out;
71 zstream.avail_out = out_size;
72
73 do {
74 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
75 case Z_STREAM_END:
76 goto end;
77 case Z_OK:
78 break;
79 default:
80 inflateEnd(&zstream);
81 return 0;
82 }
83
84 if (zstream.avail_out)
85 break;
86
87 out = realloc(out, 2*zstream.total_out);
88 if (out == NULL) {
89 inflateEnd(&zstream);
90 return 0;
91 }
92
93 zstream.next_out = (unsigned char *)out + zstream.total_out;
94 zstream.avail_out = zstream.total_out;
95 } while (1);
96 end:
97 inflateEnd(&zstream);
98 free(*ptr);
99 *ptr = out;
100 return zstream.total_out / 4;
101 }
102
103 static int ascii85_decode(const char *in, uint32_t **out, bool inflate)
104 {
105 int len = 0, size = 1024;
106
107 *out = realloc(*out, sizeof(uint32_t)*size);
108 if (*out == NULL)
109 return 0;
110
111 while (*in >= '!' && *in <= 'z') {
112 uint32_t v = 0;
113
114 if (len == size) {
115 size *= 2;
116 *out = realloc(*out, sizeof(uint32_t)*size);
117 if (*out == NULL)
118 return 0;
119 }
120
121 if (*in == 'z') {
122 in++;
123 } else {
124 v += in[0] - 33; v *= 85;
125 v += in[1] - 33; v *= 85;
126 v += in[2] - 33; v *= 85;
127 v += in[3] - 33; v *= 85;
128 v += in[4] - 33;
129 in += 5;
130 }
131 (*out)[len++] = v;
132 }
133
134 if (!inflate)
135 return len;
136
137 return zlib_inflate(out, len);
138 }
139
140 static void
141 print_help(const char *progname, FILE *file)
142 {
143 fprintf(file,
144 "Usage: %s [OPTION]... [FILE]\n"
145 "Convert an Intel GPU i915 error state to an aub file.\n"
146 " -h, --help display this help and exit\n"
147 " -o, --output=FILE the output aub file (default FILE.aub)\n",
148 progname);
149 }
150
151 int
152 main(int argc, char *argv[])
153 {
154 int i, c;
155 bool help = false;
156 char *out_filename = NULL, *in_filename = NULL;
157 const struct option aubinator_opts[] = {
158 { "help", no_argument, NULL, 'h' },
159 { "output", required_argument, NULL, 'o' },
160 { NULL, 0, NULL, 0 }
161 };
162
163 i = 0;
164 while ((c = getopt_long(argc, argv, "ho:", aubinator_opts, &i)) != -1) {
165 switch (c) {
166 case 'h':
167 help = true;
168 break;
169 case 'o':
170 out_filename = strdup(optarg);
171 break;
172 default:
173 break;
174 }
175 }
176
177 if (optind < argc)
178 in_filename = argv[optind++];
179
180 if (help || argc == 1 || !in_filename) {
181 print_help(argv[0], stderr);
182 return in_filename ? EXIT_SUCCESS : EXIT_FAILURE;
183 }
184
185 if (out_filename == NULL) {
186 int out_filename_size = strlen(in_filename) + 5;
187 out_filename = malloc(out_filename_size);
188 snprintf(out_filename, out_filename_size, "%s.aub", in_filename);
189 }
190
191 FILE *err_file = fopen(in_filename, "r");
192 fail_if(!err_file, "Failed to open error file \"%s\": %m\n", in_filename);
193
194 FILE *aub_file = fopen(out_filename, "w");
195 fail_if(!aub_file, "Failed to open aub file \"%s\": %m\n", in_filename);
196
197 struct aub_file aub = {};
198
199 uint32_t active_ring = 0;
200 int num_ring_bos = 0;
201
202 uint64_t batch_addr = 0;
203
204 enum bo_type {
205 BO_TYPE_UNKNOWN = 0,
206 BO_TYPE_BATCH,
207 BO_TYPE_USER,
208 } bo_type;
209 uint64_t bo_addr;
210
211 char *line = NULL;
212 size_t line_size;
213 while (getline(&line, &line_size, err_file) > 0) {
214 const char *pci_id_start = strstr(line, "PCI ID");
215 if (pci_id_start) {
216 int pci_id;
217 int matched = sscanf(line, "PCI ID: 0x%04x\n", &pci_id);
218 fail_if(!matched, "Invalid error state file!\n");
219
220 aub_file_init(&aub, aub_file, pci_id);
221 fail_if(!aub_use_execlists(&aub),
222 "%s currently only works on gen8+\n", argv[0]);
223
224 aub_write_header(&aub, "error state");
225 continue;
226 }
227
228 const char *active_start = "Active (";
229 if (strncmp(line, active_start, strlen(active_start)) == 0) {
230 fail_if(active_ring != 0, "TODO: Handle multiple active rings\n");
231
232 char *ring = line + strlen(active_start);
233
234 const struct {
235 const char *match;
236 uint32_t ring;
237 } rings[] = {
238 { "rcs", I915_EXEC_RENDER },
239 { "vcs", I915_EXEC_VEBOX },
240 { "bcs", I915_EXEC_BLT },
241 { NULL, BO_TYPE_UNKNOWN },
242 }, *r;
243
244 for (r = rings; r->match; r++) {
245 if (strncasecmp(ring, r->match, strlen(r->match)) == 0) {
246 active_ring = r->ring;
247 break;
248 }
249 }
250
251 char *count = strchr(ring, '[');
252 fail_if(!count || sscanf(count, "[%d]:", &num_ring_bos) < 1,
253 "Failed to parse BO table header\n");
254 continue;
255 }
256
257 if (num_ring_bos > 0) {
258 unsigned hi, lo, size;
259 if (sscanf(line, " %x_%x %d", &hi, &lo, &size) == 3) {
260 assert(aub_use_execlists(&aub));
261 aub_map_ppgtt(&aub, ((uint64_t)hi) << 32 | lo, size);
262 num_ring_bos--;
263 } else {
264 fail("Not enough BO entries in the active table\n");
265 }
266 continue;
267 }
268
269 if (line[0] == ':' || line[0] == '~') {
270 if (bo_type == BO_TYPE_UNKNOWN)
271 continue;
272
273 uint32_t *data = NULL;
274 int count = ascii85_decode(line+1, &data, line[0] == ':');
275 fail_if(count == 0, "ASCII85 decode failed.\n");
276 uint64_t bo_size = count * 4;
277
278 if (bo_type == BO_TYPE_BATCH) {
279 aub_write_trace_block(&aub, AUB_TRACE_TYPE_BATCH,
280 data, bo_size, bo_addr);
281 batch_addr = bo_addr;
282 } else {
283 assert(bo_type == BO_TYPE_USER);
284 aub_write_trace_block(&aub, AUB_TRACE_TYPE_NOTYPE,
285 data, bo_size, bo_addr);
286 }
287
288 continue;
289 }
290
291 char *dashes = strstr(line, "---");
292 if (dashes) {
293 dashes += 4;
294
295 const struct {
296 const char *match;
297 enum bo_type type;
298 } bo_types[] = {
299 { "gtt_offset", BO_TYPE_BATCH },
300 { "user", BO_TYPE_USER },
301 { NULL, BO_TYPE_UNKNOWN },
302 }, *b;
303
304 bo_type = BO_TYPE_UNKNOWN;
305 for (b = bo_types; b->match; b++) {
306 if (strncasecmp(dashes, b->match, strlen(b->match)) == 0) {
307 bo_type = b->type;
308 break;
309 }
310 }
311
312 if (bo_type != BO_TYPE_UNKNOWN) {
313 uint32_t hi, lo;
314 dashes = strchr(dashes, '=');
315 if (dashes && sscanf(dashes, "= 0x%08x %08x\n", &hi, &lo) == 2) {
316 bo_addr = ((uint64_t) hi) << 32 | lo;
317 } else {
318 fail("User BO does not have an address\n");
319 }
320 }
321 continue;
322 }
323 }
324
325 fail_if(!batch_addr, "Failed to find batch buffer.\n");
326
327 aub_write_exec(&aub, batch_addr, aub_gtt_size(&aub), I915_EXEC_RENDER);
328
329 return EXIT_SUCCESS;
330 }
331
332 /* vim: set ts=8 sw=8 tw=0 cino=:0,(0 noet :*/