intel: aubinator: print field values if available
[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 #include <inttypes.h>
31
32 #include <util/macros.h>
33
34 #include "decoder.h"
35
36 #include "genxml/gen6_xml.h"
37 #include "genxml/gen7_xml.h"
38 #include "genxml/gen75_xml.h"
39 #include "genxml/gen8_xml.h"
40 #include "genxml/gen9_xml.h"
41
42 #define XML_BUFFER_SIZE 4096
43
44 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
45
46 struct gen_spec {
47 uint32_t gen;
48
49 int ncommands;
50 struct gen_group *commands[256];
51 int nstructs;
52 struct gen_group *structs[256];
53 int nregisters;
54 struct gen_group *registers[256];
55 };
56
57 struct location {
58 const char *filename;
59 int line_number;
60 };
61
62 struct parser_context {
63 XML_Parser parser;
64 int foo;
65 struct location loc;
66 const char *platform;
67
68 struct gen_group *group;
69
70 int nfields;
71 struct gen_field *fields[128];
72
73 struct gen_spec *spec;
74 };
75
76 const char *
77 gen_group_get_name(struct gen_group *group)
78 {
79 return group->name;
80 }
81
82 uint32_t
83 gen_group_get_opcode(struct gen_group *group)
84 {
85 return group->opcode;
86 }
87
88 struct gen_group *
89 gen_spec_find_struct(struct gen_spec *spec, const char *name)
90 {
91 for (int i = 0; i < spec->nstructs; i++)
92 if (strcmp(spec->structs[i]->name, name) == 0)
93 return spec->structs[i];
94
95 return NULL;
96 }
97
98 struct gen_group *
99 gen_spec_find_register(struct gen_spec *spec, uint32_t offset)
100 {
101 for (int i = 0; i < spec->nregisters; i++)
102 if (spec->registers[i]->register_offset == offset)
103 return spec->registers[i];
104
105 return NULL;
106 }
107
108 uint32_t
109 gen_spec_get_gen(struct gen_spec *spec)
110 {
111 return spec->gen;
112 }
113
114 static void __attribute__((noreturn))
115 fail(struct location *loc, const char *msg, ...)
116 {
117 va_list ap;
118
119 va_start(ap, msg);
120 fprintf(stderr, "%s:%d: error: ",
121 loc->filename, loc->line_number);
122 vfprintf(stderr, msg, ap);
123 fprintf(stderr, "\n");
124 va_end(ap);
125 exit(EXIT_FAILURE);
126 }
127
128 static void *
129 fail_on_null(void *p)
130 {
131 if (p == NULL) {
132 fprintf(stderr, "aubinator: out of memory\n");
133 exit(EXIT_FAILURE);
134 }
135
136 return p;
137 }
138
139 static char *
140 xstrdup(const char *s)
141 {
142 return fail_on_null(strdup(s));
143 }
144
145 static void *
146 zalloc(size_t s)
147 {
148 return calloc(s, 1);
149 }
150
151 static void *
152 xzalloc(size_t s)
153 {
154 return fail_on_null(zalloc(s));
155 }
156
157 static struct gen_group *
158 create_group(struct parser_context *ctx, const char *name, const char **atts)
159 {
160 struct gen_group *group;
161
162 group = xzalloc(sizeof(*group));
163 if (name)
164 group->name = xstrdup(name);
165
166 group->group_offset = 0;
167 group->group_count = 0;
168
169 return group;
170 }
171
172 static void
173 get_group_offset_count(struct parser_context *ctx, const char *name,
174 const char **atts, uint32_t *offset, uint32_t *count)
175 {
176 char *p;
177 int i;
178
179 for (i = 0; atts[i]; i += 2) {
180 if (strcmp(atts[i], "count") == 0)
181 *count = strtoul(atts[i + 1], &p, 0);
182 else if (strcmp(atts[i], "start") == 0)
183 *offset = strtoul(atts[i + 1], &p, 0);
184 }
185 return;
186 }
187
188 static void
189 get_register_offset(const char **atts, uint32_t *offset)
190 {
191 char *p;
192 int i;
193
194 for (i = 0; atts[i]; i += 2) {
195 if (strcmp(atts[i], "num") == 0)
196 *offset = strtoul(atts[i + 1], &p, 0);
197 }
198 return;
199 }
200
201 static void
202 get_start_end_pos(int *start, int *end)
203 {
204 /* start value has to be mod with 32 as we need the relative
205 * start position in the first DWord. For the end position, add
206 * the length of the field to the start position to get the
207 * relative postion in the 64 bit address.
208 */
209 if (*end - *start > 32) {
210 int len = *end - *start;
211 *start = *start % 32;
212 *end = *start + len;
213 } else {
214 *start = *start % 32;
215 *end = *end % 32;
216 }
217
218 return;
219 }
220
221 static inline uint64_t
222 mask(int start, int end)
223 {
224 uint64_t v;
225
226 v = ~0ULL >> (63 - end + start);
227
228 return v << start;
229 }
230
231 static inline uint64_t
232 field(uint64_t value, int start, int end)
233 {
234 get_start_end_pos(&start, &end);
235 return (value & mask(start, end)) >> (start);
236 }
237
238 static inline uint64_t
239 field_address(uint64_t value, int start, int end)
240 {
241 /* no need to right shift for address/offset */
242 get_start_end_pos(&start, &end);
243 return (value & mask(start, end));
244 }
245
246 static struct gen_type
247 string_to_type(struct parser_context *ctx, const char *s)
248 {
249 int i, f;
250 struct gen_group *g;
251
252 if (strcmp(s, "int") == 0)
253 return (struct gen_type) { .kind = GEN_TYPE_INT };
254 else if (strcmp(s, "uint") == 0)
255 return (struct gen_type) { .kind = GEN_TYPE_UINT };
256 else if (strcmp(s, "bool") == 0)
257 return (struct gen_type) { .kind = GEN_TYPE_BOOL };
258 else if (strcmp(s, "float") == 0)
259 return (struct gen_type) { .kind = GEN_TYPE_FLOAT };
260 else if (strcmp(s, "address") == 0)
261 return (struct gen_type) { .kind = GEN_TYPE_ADDRESS };
262 else if (strcmp(s, "offset") == 0)
263 return (struct gen_type) { .kind = GEN_TYPE_OFFSET };
264 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
265 return (struct gen_type) { .kind = GEN_TYPE_UFIXED, .i = i, .f = f };
266 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
267 return (struct gen_type) { .kind = GEN_TYPE_SFIXED, .i = i, .f = f };
268 else if (g = gen_spec_find_struct(ctx->spec, s), g != NULL)
269 return (struct gen_type) { .kind = GEN_TYPE_STRUCT, .gen_struct = g };
270 else if (strcmp(s, "mbo") == 0)
271 return (struct gen_type) { .kind = GEN_TYPE_MBO };
272 else
273 fail(&ctx->loc, "invalid type: %s", s);
274 }
275
276 static struct gen_field *
277 create_field(struct parser_context *ctx, const char **atts)
278 {
279 struct gen_field *field;
280 char *p;
281 int i;
282
283 field = xzalloc(sizeof(*field));
284
285 for (i = 0; atts[i]; i += 2) {
286 if (strcmp(atts[i], "name") == 0)
287 field->name = xstrdup(atts[i + 1]);
288 else if (strcmp(atts[i], "start") == 0)
289 field->start = ctx->group->group_offset+strtoul(atts[i + 1], &p, 0);
290 else if (strcmp(atts[i], "end") == 0) {
291 field->end = ctx->group->group_offset+strtoul(atts[i + 1], &p, 0);
292 if (ctx->group->group_offset)
293 ctx->group->group_offset = field->end+1;
294 } else if (strcmp(atts[i], "type") == 0)
295 field->type = string_to_type(ctx, atts[i + 1]);
296 else if (strcmp(atts[i], "default") == 0 &&
297 field->start >= 16 && field->end <= 31) {
298 field->has_default = true;
299 field->default_value = strtoul(atts[i + 1], &p, 0);
300 }
301 }
302
303 return field;
304 }
305
306 static struct gen_value *
307 create_value(struct parser_context *ctx, const char **atts)
308 {
309 struct gen_value *value = xzalloc(sizeof(*value));
310
311 for (int i = 0; atts[i]; i += 2) {
312 if (strcmp(atts[i], "name") == 0)
313 value->name = xstrdup(atts[i + 1]);
314 else if (strcmp(atts[i], "value") == 0)
315 value->value = strtoul(atts[i + 1], NULL, 0);
316 }
317
318 return value;
319 }
320
321 static void
322 start_element(void *data, const char *element_name, const char **atts)
323 {
324 struct parser_context *ctx = data;
325 int i;
326 const char *name = NULL;
327 const char *gen = NULL;
328
329 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
330
331 for (i = 0; atts[i]; i += 2) {
332 if (strcmp(atts[i], "name") == 0)
333 name = atts[i + 1];
334 else if (strcmp(atts[i], "gen") == 0)
335 gen = atts[i + 1];
336 }
337
338 if (strcmp(element_name, "genxml") == 0) {
339 if (name == NULL)
340 fail(&ctx->loc, "no platform name given");
341 if (gen == NULL)
342 fail(&ctx->loc, "no gen given");
343
344 ctx->platform = xstrdup(name);
345 int major, minor;
346 int n = sscanf(gen, "%d.%d", &major, &minor);
347 if (n == 0)
348 fail(&ctx->loc, "invalid gen given: %s", gen);
349 if (n == 1)
350 minor = 0;
351
352 ctx->spec->gen = MAKE_GEN(major, minor);
353 } else if (strcmp(element_name, "instruction") == 0 ||
354 strcmp(element_name, "struct") == 0) {
355 ctx->group = create_group(ctx, name, atts);
356 } else if (strcmp(element_name, "register") == 0) {
357 ctx->group = create_group(ctx, name, atts);
358 get_register_offset(atts, &ctx->group->register_offset);
359 } else if (strcmp(element_name, "group") == 0) {
360 get_group_offset_count(ctx, name, atts, &ctx->group->group_offset,
361 &ctx->group->group_count);
362 } else if (strcmp(element_name, "field") == 0) {
363 do {
364 ctx->fields[ctx->nfields++] = create_field(ctx, atts);
365 if (ctx->group->group_count)
366 ctx->group->group_count--;
367 } while (ctx->group->group_count > 0);
368 } else if (strcmp(element_name, "enum") == 0) {
369 } else if (strcmp(element_name, "value") == 0) {
370 if (ctx->nfields > 0) {
371 struct gen_field *field = ctx->fields[ctx->nfields - 1];
372 if (field->n_allocated_values <= field->n_values) {
373 if (field->n_allocated_values == 0) {
374 field->n_allocated_values = 2;
375 field->values =
376 xzalloc(sizeof(field->values[0]) * field->n_allocated_values);
377 } else {
378 field->n_allocated_values *= 2;
379 field->values =
380 realloc(field->values,
381 sizeof(field->values[0]) * field->n_allocated_values);
382 }
383 }
384 field->values[field->n_values++] = create_value(ctx, atts);
385 }
386 }
387 }
388
389 static void
390 end_element(void *data, const char *name)
391 {
392 struct parser_context *ctx = data;
393
394 if (strcmp(name, "instruction") == 0 ||
395 strcmp(name, "struct") == 0 ||
396 strcmp(name, "register") == 0) {
397 size_t size = ctx->nfields * sizeof(ctx->fields[0]);
398 struct gen_group *group = ctx->group;
399
400 group->fields = xzalloc(size);
401 group->nfields = ctx->nfields;
402 memcpy(group->fields, ctx->fields, size);
403 ctx->nfields = 0;
404 ctx->group = NULL;
405
406 for (int i = 0; i < group->nfields; i++) {
407 if (group->fields[i]->start >= 16 &&
408 group->fields[i]->end <= 31 &&
409 group->fields[i]->has_default) {
410 group->opcode_mask |=
411 mask(group->fields[i]->start % 32, group->fields[i]->end % 32);
412 group->opcode |=
413 group->fields[i]->default_value << group->fields[i]->start;
414 }
415 }
416
417 struct gen_spec *spec = ctx->spec;
418 if (strcmp(name, "instruction") == 0)
419 spec->commands[spec->ncommands++] = group;
420 else if (strcmp(name, "struct") == 0)
421 spec->structs[spec->nstructs++] = group;
422 else if (strcmp(name, "register") == 0)
423 spec->registers[spec->nregisters++] = group;
424 } else if (strcmp(name, "group") == 0) {
425 ctx->group->group_offset = 0;
426 ctx->group->group_count = 0;
427 }
428 }
429
430 static void
431 character_data(void *data, const XML_Char *s, int len)
432 {
433 }
434
435 static int
436 devinfo_to_gen(const struct gen_device_info *devinfo)
437 {
438 int value = 10 * devinfo->gen;
439
440 if (devinfo->is_baytrail || devinfo->is_haswell)
441 value += 5;
442
443 return value;
444 }
445
446 static const struct {
447 int gen;
448 const uint8_t *data;
449 size_t data_length;
450 } gen_data[] = {
451 { .gen = 60, .data = gen6_xml, .data_length = sizeof(gen6_xml) },
452 { .gen = 70, .data = gen7_xml, .data_length = sizeof(gen7_xml) },
453 { .gen = 75, .data = gen75_xml, .data_length = sizeof(gen75_xml) },
454 { .gen = 80, .data = gen8_xml, .data_length = sizeof(gen8_xml) },
455 { .gen = 90, .data = gen9_xml, .data_length = sizeof(gen9_xml) }
456 };
457
458 static const uint8_t *
459 devinfo_to_xml_data(const struct gen_device_info *devinfo,
460 uint32_t *data_length)
461 {
462 int i, gen = devinfo_to_gen(devinfo);
463
464 for (i = 0; i < ARRAY_SIZE(gen_data); i++) {
465 if (gen_data[i].gen == gen) {
466 *data_length = gen_data[i].data_length;
467 return gen_data[i].data;
468 }
469 }
470
471 unreachable("Unknown generation");
472 return NULL;
473 }
474
475 struct gen_spec *
476 gen_spec_load(const struct gen_device_info *devinfo)
477 {
478 struct parser_context ctx;
479 void *buf;
480 const void *data;
481 uint32_t data_length = 0;
482
483 memset(&ctx, 0, sizeof ctx);
484 ctx.parser = XML_ParserCreate(NULL);
485 XML_SetUserData(ctx.parser, &ctx);
486 if (ctx.parser == NULL) {
487 fprintf(stderr, "failed to create parser\n");
488 return NULL;
489 }
490
491 XML_SetElementHandler(ctx.parser, start_element, end_element);
492 XML_SetCharacterDataHandler(ctx.parser, character_data);
493
494 ctx.spec = xzalloc(sizeof(*ctx.spec));
495
496 data = devinfo_to_xml_data(devinfo, &data_length);
497 buf = XML_GetBuffer(ctx.parser, data_length);
498
499 memcpy(buf, data, data_length);
500
501 if (XML_ParseBuffer(ctx.parser, data_length, true) == 0) {
502 fprintf(stderr,
503 "Error parsing XML at line %ld col %ld: %s\n",
504 XML_GetCurrentLineNumber(ctx.parser),
505 XML_GetCurrentColumnNumber(ctx.parser),
506 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
507 XML_ParserFree(ctx.parser);
508 return NULL;
509 }
510
511 XML_ParserFree(ctx.parser);
512
513 return ctx.spec;
514 }
515
516 struct gen_spec *
517 gen_spec_load_from_path(const struct gen_device_info *devinfo,
518 const char *path)
519 {
520 struct parser_context ctx;
521 size_t len, filename_len = strlen(path) + 20;
522 char *filename = malloc(filename_len);
523 void *buf;
524 FILE *input;
525
526 len = snprintf(filename, filename_len, "%s/gen%i.xml",
527 path, devinfo_to_gen(devinfo));
528 assert(len < filename_len);
529
530 input = fopen(filename, "r");
531 if (input == NULL) {
532 fprintf(stderr, "failed to open xml description\n");
533 free(filename);
534 return NULL;
535 }
536
537 memset(&ctx, 0, sizeof ctx);
538 ctx.parser = XML_ParserCreate(NULL);
539 XML_SetUserData(ctx.parser, &ctx);
540 if (ctx.parser == NULL) {
541 fprintf(stderr, "failed to create parser\n");
542 free(filename);
543 return NULL;
544 }
545
546 XML_SetElementHandler(ctx.parser, start_element, end_element);
547 XML_SetCharacterDataHandler(ctx.parser, character_data);
548 ctx.loc.filename = filename;
549 ctx.spec = xzalloc(sizeof(*ctx.spec));
550
551 do {
552 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
553 len = fread(buf, 1, XML_BUFFER_SIZE, input);
554 if (len < 0) {
555 fprintf(stderr, "fread: %m\n");
556 fclose(input);
557 free(filename);
558 return NULL;
559 }
560 if (XML_ParseBuffer(ctx.parser, len, len == 0) == 0) {
561 fprintf(stderr,
562 "Error parsing XML at line %ld col %ld: %s\n",
563 XML_GetCurrentLineNumber(ctx.parser),
564 XML_GetCurrentColumnNumber(ctx.parser),
565 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
566 fclose(input);
567 free(filename);
568 return NULL;
569 }
570 } while (len > 0);
571
572 XML_ParserFree(ctx.parser);
573
574 fclose(input);
575 free(filename);
576
577 return ctx.spec;
578 }
579
580 struct gen_group *
581 gen_spec_find_instruction(struct gen_spec *spec, const uint32_t *p)
582 {
583 for (int i = 0; i < spec->ncommands; i++) {
584 uint32_t opcode = *p & spec->commands[i]->opcode_mask;
585 if (opcode == spec->commands[i]->opcode)
586 return spec->commands[i];
587 }
588
589 return NULL;
590 }
591
592 int
593 gen_group_get_length(struct gen_group *group, const uint32_t *p)
594 {
595 uint32_t h = p[0];
596 uint32_t type = field(h, 29, 31);
597
598 switch (type) {
599 case 0: /* MI */ {
600 uint32_t opcode = field(h, 23, 28);
601 if (opcode < 16)
602 return 1;
603 else
604 return field(h, 0, 7) + 2;
605 break;
606 }
607
608 case 3: /* Render */ {
609 uint32_t subtype = field(h, 27, 28);
610 switch (subtype) {
611 case 0:
612 return field(h, 0, 7) + 2;
613 case 1:
614 return 1;
615 case 2:
616 return 2;
617 case 3:
618 return field(h, 0, 7) + 2;
619 }
620 }
621 }
622
623 unreachable("bad opcode");
624 }
625
626 void
627 gen_field_iterator_init(struct gen_field_iterator *iter,
628 struct gen_group *group,
629 const uint32_t *p,
630 bool print_colors)
631 {
632 iter->group = group;
633 iter->p = p;
634 iter->i = 0;
635 iter->print_colors = print_colors;
636 }
637
638 static void
639 gen_field_write_value(char *str, size_t max_length,
640 struct gen_field *field,
641 uint64_t value)
642 {
643 for (int i = 0; i < field->n_values; i++) {
644 if (field->values[i]->value == value) {
645 strncpy(str, field->values[i]->name, max_length);
646 return;
647 }
648 }
649 }
650
651 bool
652 gen_field_iterator_next(struct gen_field_iterator *iter)
653 {
654 struct gen_field *f;
655 union {
656 uint64_t qw;
657 float f;
658 } v;
659
660 if (iter->i == iter->group->nfields)
661 return false;
662
663 f = iter->group->fields[iter->i++];
664 iter->name = f->name;
665 int index = f->start / 32;
666
667 if ((f->end - f->start) > 32)
668 v.qw = ((uint64_t) iter->p[index+1] << 32) | iter->p[index];
669 else
670 v.qw = iter->p[index];
671
672 iter->description[0] = '\0';
673
674 switch (f->type.kind) {
675 case GEN_TYPE_UNKNOWN:
676 case GEN_TYPE_INT: {
677 uint64_t value = field(v.qw, f->start, f->end);
678 snprintf(iter->value, sizeof(iter->value),
679 "%"PRId64, value);
680 gen_field_write_value(iter->description, sizeof(iter->description),
681 f, value);
682 break;
683 }
684 case GEN_TYPE_UINT: {
685 uint64_t value = field(v.qw, f->start, f->end);
686 snprintf(iter->value, sizeof(iter->value),
687 "%"PRIu64, value);
688 gen_field_write_value(iter->description, sizeof(iter->description),
689 f, value);
690 break;
691 }
692 case GEN_TYPE_BOOL: {
693 const char *true_string =
694 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
695 snprintf(iter->value, sizeof(iter->value),
696 "%s", field(v.qw, f->start, f->end) ? true_string : "false");
697 break;
698 }
699 case GEN_TYPE_FLOAT:
700 snprintf(iter->value, sizeof(iter->value), "%f", v.f);
701 break;
702 case GEN_TYPE_ADDRESS:
703 case GEN_TYPE_OFFSET:
704 snprintf(iter->value, sizeof(iter->value),
705 "0x%08"PRIx64, field_address(v.qw, f->start, f->end));
706 break;
707 case GEN_TYPE_STRUCT:
708 snprintf(iter->value, sizeof(iter->value),
709 "<struct %s %d>", f->type.gen_struct->name, (f->start / 32));
710 break;
711 case GEN_TYPE_UFIXED:
712 snprintf(iter->value, sizeof(iter->value),
713 "%f", (float) field(v.qw, f->start, f->end) / (1 << f->type.f));
714 break;
715 case GEN_TYPE_SFIXED:
716 /* FIXME: Sign extend extracted field. */
717 snprintf(iter->value, sizeof(iter->value), "%s", "foo");
718 break;
719 case GEN_TYPE_MBO:
720 break;
721 }
722
723 return true;
724 }