aubinator: Fix the tool to correctly decode the DWords
[mesa.git] / src / intel / tools / decoder.c
1 /*
2 * Copyright © 2016 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 #include <stdio.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <expat.h>
30
31 #include <util/macros.h>
32
33 #include "decoder.h"
34
35 #define XML_BUFFER_SIZE 4096
36
37 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
38
39 struct gen_spec {
40 uint32_t gen;
41
42 int ncommands;
43 struct gen_group *commands[256];
44 int nstructs;
45 struct gen_group *structs[256];
46 int nregisters;
47 struct gen_group *registers[256];
48 };
49
50 struct location {
51 const char *filename;
52 int line_number;
53 };
54
55 struct parser_context {
56 XML_Parser parser;
57 int foo;
58 struct location loc;
59 const char *platform;
60
61 struct gen_group *group;
62
63 int nfields;
64 struct gen_field *fields[128];
65
66 struct gen_spec *spec;
67 };
68
69 const char *
70 gen_group_get_name(struct gen_group *group)
71 {
72 return group->name;
73 }
74
75 uint32_t
76 gen_group_get_opcode(struct gen_group *group)
77 {
78 return group->opcode;
79 }
80
81 struct gen_group *
82 gen_spec_find_struct(struct gen_spec *spec, const char *name)
83 {
84 for (int i = 0; i < spec->nstructs; i++)
85 if (strcmp(spec->structs[i]->name, name) == 0)
86 return spec->structs[i];
87
88 return NULL;
89 }
90
91 uint32_t
92 gen_spec_get_gen(struct gen_spec *spec)
93 {
94 return spec->gen;
95 }
96
97 static void __attribute__((noreturn))
98 fail(struct location *loc, const char *msg, ...)
99 {
100 va_list ap;
101
102 va_start(ap, msg);
103 fprintf(stderr, "%s:%d: error: ",
104 loc->filename, loc->line_number);
105 vfprintf(stderr, msg, ap);
106 fprintf(stderr, "\n");
107 va_end(ap);
108 exit(EXIT_FAILURE);
109 }
110
111 static void *
112 fail_on_null(void *p)
113 {
114 if (p == NULL) {
115 fprintf(stderr, "aubinator: out of memory\n");
116 exit(EXIT_FAILURE);
117 }
118
119 return p;
120 }
121
122 static char *
123 xstrdup(const char *s)
124 {
125 return fail_on_null(strdup(s));
126 }
127
128 static void *
129 zalloc(size_t s)
130 {
131 return calloc(s, 1);
132 }
133
134 static void *
135 xzalloc(size_t s)
136 {
137 return fail_on_null(zalloc(s));
138 }
139
140 static struct gen_group *
141 create_group(struct parser_context *ctx, const char *name, const char **atts)
142 {
143 struct gen_group *group;
144
145 group = xzalloc(sizeof(*group));
146 if (name)
147 group->name = xstrdup(name);
148
149 group->group_offset = 0;
150 group->group_count = 0;
151
152 return group;
153 }
154
155 static void
156 get_group_offset_count(struct parser_context *ctx, const char *name, const char **atts, uint32_t *offset, uint32_t *count)
157 {
158 char *p;
159 int i;
160
161 for (i = 0; atts[i]; i += 2) {
162 if (strcmp(atts[i], "count") == 0)
163 *count = strtoul(atts[i + 1], &p, 0);
164 else if (strcmp(atts[i], "start") == 0)
165 *offset = strtoul(atts[i + 1], &p, 0);
166 }
167 return;
168 }
169
170 static inline uint64_t
171 mask(int start, int end)
172 {
173 uint64_t v;
174
175 v = ~0ULL >> (63 - end + start);
176
177 return v << start;
178 }
179
180 static inline uint64_t
181 field(uint64_t value, int start, int end)
182 {
183 /* The field values are obtained from the DWord,
184 * Hence, get the relative start and end positions
185 * by doing a %32 on the start and end positions
186 */
187 return (value & mask(start % 32, end % 32)) >> (start % 32);
188 }
189
190 static inline uint64_t
191 field_address(uint64_t value, int start, int end)
192 {
193 /* no need to right shift for address/offset */
194 return (value & mask(start % 32, end % 32));
195 }
196
197 static struct gen_type
198 string_to_type(struct parser_context *ctx, const char *s)
199 {
200 int i, f;
201 struct gen_group *g;
202
203 if (strcmp(s, "int") == 0)
204 return (struct gen_type) { .kind = GEN_TYPE_INT };
205 else if (strcmp(s, "uint") == 0)
206 return (struct gen_type) { .kind = GEN_TYPE_UINT };
207 else if (strcmp(s, "bool") == 0)
208 return (struct gen_type) { .kind = GEN_TYPE_BOOL };
209 else if (strcmp(s, "float") == 0)
210 return (struct gen_type) { .kind = GEN_TYPE_FLOAT };
211 else if (strcmp(s, "address") == 0)
212 return (struct gen_type) { .kind = GEN_TYPE_ADDRESS };
213 else if (strcmp(s, "offset") == 0)
214 return (struct gen_type) { .kind = GEN_TYPE_OFFSET };
215 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
216 return (struct gen_type) { .kind = GEN_TYPE_UFIXED, .i = i, .f = f };
217 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
218 return (struct gen_type) { .kind = GEN_TYPE_SFIXED, .i = i, .f = f };
219 else if (g = gen_spec_find_struct(ctx->spec, s), g != NULL)
220 return (struct gen_type) { .kind = GEN_TYPE_STRUCT, .gen_struct = g };
221 else if (strcmp(s, "mbo") == 0)
222 return (struct gen_type) { .kind = GEN_TYPE_MBO };
223 else
224 fail(&ctx->loc, "invalid type: %s", s);
225 }
226
227 static struct gen_field *
228 create_field(struct parser_context *ctx, const char **atts)
229 {
230 struct gen_field *field;
231 char *p;
232 int i;
233
234 field = xzalloc(sizeof(*field));
235
236 for (i = 0; atts[i]; i += 2) {
237 if (strcmp(atts[i], "name") == 0)
238 field->name = xstrdup(atts[i + 1]);
239 else if (strcmp(atts[i], "start") == 0)
240 field->start = ctx->group->group_offset+strtoul(atts[i + 1], &p, 0);
241 else if (strcmp(atts[i], "end") == 0) {
242 field->end = ctx->group->group_offset+strtoul(atts[i + 1], &p, 0);
243 if(ctx->group->group_offset)
244 ctx->group->group_offset = field->end+1;
245 } else if (strcmp(atts[i], "type") == 0)
246 field->type = string_to_type(ctx, atts[i + 1]);
247 else if (strcmp(atts[i], "default") == 0 &&
248 field->start >= 16 && field->end <= 31) {
249 field->has_default = true;
250 field->default_value = strtoul(atts[i + 1], &p, 0);
251 }
252 }
253
254 return field;
255 }
256
257 static void
258 start_element(void *data, const char *element_name, const char **atts)
259 {
260 struct parser_context *ctx = data;
261 int i;
262 const char *name = NULL;
263 const char *gen = NULL;
264
265 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
266
267 for (i = 0; atts[i]; i += 2) {
268 if (strcmp(atts[i], "name") == 0)
269 name = atts[i + 1];
270 else if (strcmp(atts[i], "gen") == 0)
271 gen = atts[i + 1];
272 }
273
274 if (strcmp(element_name, "genxml") == 0) {
275 if (name == NULL)
276 fail(&ctx->loc, "no platform name given");
277 if (gen == NULL)
278 fail(&ctx->loc, "no gen given");
279
280 ctx->platform = xstrdup(name);
281 int major, minor;
282 int n = sscanf(gen, "%d.%d", &major, &minor);
283 if (n == 0)
284 fail(&ctx->loc, "invalid gen given: %s", gen);
285 if (n == 1)
286 minor = 0;
287
288 ctx->spec->gen = MAKE_GEN(major, minor);
289 } else if (strcmp(element_name, "instruction") == 0 ||
290 strcmp(element_name, "struct") == 0 ||
291 strcmp(element_name, "register") == 0) {
292 ctx->group = create_group(ctx, name, atts);
293 } else if (strcmp(element_name, "group") == 0) {
294 get_group_offset_count(ctx, name, atts,&ctx->group->group_offset,&ctx->group->group_count);
295 } else if (strcmp(element_name, "field") == 0) {
296 do {
297 ctx->fields[ctx->nfields++] = create_field(ctx, atts);
298 if(ctx->group->group_count)
299 ctx->group->group_count--;
300 } while (ctx->group->group_count > 0);
301 } else if (strcmp(element_name, "enum") == 0) {
302 } else if (strcmp(element_name, "value") == 0) {
303 }
304 }
305
306 static void
307 end_element(void *data, const char *name)
308 {
309 struct parser_context *ctx = data;
310
311 if (strcmp(name, "instruction") == 0 ||
312 strcmp(name, "struct") == 0 ||
313 strcmp(name, "register") == 0) {
314 size_t size = ctx->nfields * sizeof(ctx->fields[0]);
315 struct gen_group *group = ctx->group;
316
317 group->fields = xzalloc(size);
318 group->nfields = ctx->nfields;
319 memcpy(group->fields, ctx->fields, size);
320 ctx->nfields = 0;
321 ctx->group = NULL;
322
323 for (int i = 0; i < group->nfields; i++) {
324 if (group->fields[i]->start >= 16 &&
325 group->fields[i]->end <= 31 &&
326 group->fields[i]->has_default) {
327 group->opcode_mask |=
328 mask(group->fields[i]->start % 32, group->fields[i]->end % 32);
329 group->opcode |=
330 group->fields[i]->default_value << group->fields[i]->start;
331 }
332 }
333
334 struct gen_spec *spec = ctx->spec;
335 if (strcmp(name, "instruction") == 0)
336 spec->commands[spec->ncommands++] = group;
337 else if (strcmp(name, "struct") == 0)
338 spec->structs[spec->nstructs++] = group;
339 else if (strcmp(name, "register") == 0)
340 spec->registers[spec->nregisters++] = group;
341 } else if (strcmp(name, "group") == 0) {
342 ctx->group->group_offset = 0;
343 ctx->group->group_count = 0;
344 }
345 }
346
347 static void
348 character_data(void *data, const XML_Char *s, int len)
349 {
350 }
351
352 struct gen_spec *
353 gen_spec_load(const char *filename)
354 {
355 struct parser_context ctx;
356 void *buf;
357 int len;
358 FILE *input;
359
360 input = fopen(filename, "r");
361 printf("xml filename = %s\n", filename);
362 if (input == NULL) {
363 fprintf(stderr, "failed to open xml description\n");
364 exit(EXIT_FAILURE);
365 }
366
367 memset(&ctx, 0, sizeof ctx);
368 ctx.parser = XML_ParserCreate(NULL);
369 XML_SetUserData(ctx.parser, &ctx);
370 if (ctx.parser == NULL) {
371 fprintf(stderr, "failed to create parser\n");
372 fclose(input);
373 return NULL;
374 }
375
376 XML_SetElementHandler(ctx.parser, start_element, end_element);
377 XML_SetCharacterDataHandler(ctx.parser, character_data);
378 ctx.loc.filename = filename;
379
380 ctx.spec = xzalloc(sizeof(*ctx.spec));
381
382 do {
383 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
384 len = fread(buf, 1, XML_BUFFER_SIZE, input);
385 if (len < 0) {
386 fprintf(stderr, "fread: %m\n");
387 fclose(input);
388 return NULL;
389 }
390 if (XML_ParseBuffer(ctx.parser, len, len == 0) == 0) {
391 fprintf(stderr,
392 "Error parsing XML at line %ld col %ld: %s\n",
393 XML_GetCurrentLineNumber(ctx.parser),
394 XML_GetCurrentColumnNumber(ctx.parser),
395 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
396 fclose(input);
397 return NULL;
398 }
399 } while (len > 0);
400
401 XML_ParserFree(ctx.parser);
402 fclose(input);
403
404 return ctx.spec;
405 }
406
407 struct gen_group *
408 gen_spec_find_instruction(struct gen_spec *spec, const uint32_t *p)
409 {
410 for (int i = 0; i < spec->ncommands; i++) {
411 uint32_t opcode = *p & spec->commands[i]->opcode_mask;
412 if (opcode == spec->commands[i]->opcode)
413 return spec->commands[i];
414 }
415
416 return NULL;
417 }
418
419 int
420 gen_group_get_length(struct gen_group *group, const uint32_t *p)
421 {
422 uint32_t h = p[0];
423 uint32_t type = field(h, 29, 31);
424
425 switch (type) {
426 case 0: /* MI */ {
427 uint32_t opcode = field(h, 23, 28);
428 if (opcode < 16)
429 return 1;
430 else
431 return field(h, 0, 7) + 2;
432 break;
433 }
434
435 case 3: /* Render */ {
436 uint32_t subtype = field(h, 27, 28);
437 switch (subtype) {
438 case 0:
439 return field(h, 0, 7) + 2;
440 case 1:
441 return 1;
442 case 2:
443 return 2;
444 case 3:
445 return field(h, 0, 7) + 2;
446 }
447 }
448 }
449
450 unreachable("bad opcode");
451 }
452
453 void
454 gen_field_iterator_init(struct gen_field_iterator *iter,
455 struct gen_group *group, const uint32_t *p)
456 {
457 iter->group = group;
458 iter->p = p;
459 iter->i = 0;
460 }
461
462 bool
463 gen_field_iterator_next(struct gen_field_iterator *iter)
464 {
465 struct gen_field *f;
466 union {
467 uint32_t dw;
468 float f;
469 } v;
470
471 if (iter->i == iter->group->nfields)
472 return false;
473
474 f = iter->group->fields[iter->i++];
475 iter->name = f->name;
476 v.dw = iter->p[f->start / 32];
477 switch (f->type.kind) {
478 case GEN_TYPE_UNKNOWN:
479 case GEN_TYPE_INT:
480 snprintf(iter->value, sizeof(iter->value),
481 "%ld", field(v.dw, f->start, f->end));
482 break;
483 case GEN_TYPE_UINT:
484 snprintf(iter->value, sizeof(iter->value),
485 "%lu", field(v.dw, f->start, f->end));
486 break;
487 case GEN_TYPE_BOOL:
488 snprintf(iter->value, sizeof(iter->value),
489 "%s", field(v.dw, f->start, f->end) ? "true" : "false");
490 break;
491 case GEN_TYPE_FLOAT:
492 snprintf(iter->value, sizeof(iter->value), "%f", v.f);
493 break;
494 case GEN_TYPE_ADDRESS:
495 case GEN_TYPE_OFFSET:
496 snprintf(iter->value, sizeof(iter->value),
497 "0x%08lx", field_address(v.dw, f->start, f->end));
498 break;
499 case GEN_TYPE_STRUCT:
500 snprintf(iter->value, sizeof(iter->value),
501 "<struct %s %d>", f->type.gen_struct->name, (f->start / 32));
502 break;
503 case GEN_TYPE_UFIXED:
504 snprintf(iter->value, sizeof(iter->value),
505 "%f", (float) field(v.dw, f->start, f->end) / (1 << f->type.f));
506 break;
507 case GEN_TYPE_SFIXED:
508 /* FIXME: Sign extend extracted field. */
509 snprintf(iter->value, sizeof(iter->value), "%s", "foo");
510 break;
511 case GEN_TYPE_MBO:
512 break;
513 }
514
515 return true;
516 }