intel/decoder: add gen_spec_init method
[mesa.git] / src / intel / common / gen_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 #include <zlib.h>
32
33 #include <util/macros.h>
34 #include <util/ralloc.h>
35
36 #include "gen_decoder.h"
37
38 #include "isl/isl.h"
39 #include "genxml/genX_xml.h"
40
41 #define XML_BUFFER_SIZE 4096
42 #define MAX_VALUE_ITEMS 128
43
44 struct location {
45 const char *filename;
46 int line_number;
47 };
48
49 struct parser_context {
50 XML_Parser parser;
51 int foo;
52 struct location loc;
53
54 struct gen_group *group;
55 struct gen_enum *enoom;
56
57 int n_values, n_allocated_values;
58 struct gen_value **values;
59
60 struct gen_field *last_field;
61
62 struct gen_spec *spec;
63 };
64
65 const char *
66 gen_group_get_name(struct gen_group *group)
67 {
68 return group->name;
69 }
70
71 uint32_t
72 gen_group_get_opcode(struct gen_group *group)
73 {
74 return group->opcode;
75 }
76
77 struct gen_group *
78 gen_spec_find_struct(struct gen_spec *spec, const char *name)
79 {
80 struct hash_entry *entry = _mesa_hash_table_search(spec->structs,
81 name);
82 return entry ? entry->data : NULL;
83 }
84
85 struct gen_group *
86 gen_spec_find_register(struct gen_spec *spec, uint32_t offset)
87 {
88 struct hash_entry *entry =
89 _mesa_hash_table_search(spec->registers_by_offset,
90 (void *) (uintptr_t) offset);
91 return entry ? entry->data : NULL;
92 }
93
94 struct gen_group *
95 gen_spec_find_register_by_name(struct gen_spec *spec, const char *name)
96 {
97 struct hash_entry *entry =
98 _mesa_hash_table_search(spec->registers_by_name, name);
99 return entry ? entry->data : NULL;
100 }
101
102 struct gen_enum *
103 gen_spec_find_enum(struct gen_spec *spec, const char *name)
104 {
105 struct hash_entry *entry = _mesa_hash_table_search(spec->enums,
106 name);
107 return entry ? entry->data : NULL;
108 }
109
110 uint32_t
111 gen_spec_get_gen(struct gen_spec *spec)
112 {
113 return spec->gen;
114 }
115
116 static void __attribute__((noreturn))
117 fail(struct location *loc, const char *msg, ...)
118 {
119 va_list ap;
120
121 va_start(ap, msg);
122 fprintf(stderr, "%s:%d: error: ",
123 loc->filename, loc->line_number);
124 vfprintf(stderr, msg, ap);
125 fprintf(stderr, "\n");
126 va_end(ap);
127 exit(EXIT_FAILURE);
128 }
129
130 static void
131 get_group_offset_count(const char **atts, uint32_t *offset, uint32_t *count,
132 uint32_t *size, bool *variable)
133 {
134 for (int i = 0; atts[i]; i += 2) {
135 char *p;
136
137 if (strcmp(atts[i], "count") == 0) {
138 *count = strtoul(atts[i + 1], &p, 0);
139 if (*count == 0)
140 *variable = true;
141 } else if (strcmp(atts[i], "start") == 0) {
142 *offset = strtoul(atts[i + 1], &p, 0);
143 } else if (strcmp(atts[i], "size") == 0) {
144 *size = strtoul(atts[i + 1], &p, 0);
145 }
146 }
147 return;
148 }
149
150 static struct gen_group *
151 create_group(struct parser_context *ctx,
152 const char *name,
153 const char **atts,
154 struct gen_group *parent,
155 bool fixed_length)
156 {
157 struct gen_group *group;
158
159 group = rzalloc(ctx->spec, struct gen_group);
160 if (name)
161 group->name = ralloc_strdup(group, name);
162
163 group->spec = ctx->spec;
164 group->variable = false;
165 group->fixed_length = fixed_length;
166
167 for (int i = 0; atts[i]; i += 2) {
168 char *p;
169 if (strcmp(atts[i], "length") == 0) {
170 group->dw_length = strtoul(atts[i + 1], &p, 0);
171 }
172 }
173
174 if (parent) {
175 group->parent = parent;
176 get_group_offset_count(atts,
177 &group->group_offset,
178 &group->group_count,
179 &group->group_size,
180 &group->variable);
181 }
182
183 return group;
184 }
185
186 static struct gen_enum *
187 create_enum(struct parser_context *ctx, const char *name, const char **atts)
188 {
189 struct gen_enum *e;
190
191 e = rzalloc(ctx->spec, struct gen_enum);
192 if (name)
193 e->name = ralloc_strdup(e, name);
194
195 return e;
196 }
197
198 static void
199 get_register_offset(const char **atts, uint32_t *offset)
200 {
201 for (int i = 0; atts[i]; i += 2) {
202 char *p;
203
204 if (strcmp(atts[i], "num") == 0)
205 *offset = strtoul(atts[i + 1], &p, 0);
206 }
207 return;
208 }
209
210 static void
211 get_start_end_pos(int *start, int *end)
212 {
213 /* start value has to be mod with 32 as we need the relative
214 * start position in the first DWord. For the end position, add
215 * the length of the field to the start position to get the
216 * relative postion in the 64 bit address.
217 */
218 if (*end - *start > 32) {
219 int len = *end - *start;
220 *start = *start % 32;
221 *end = *start + len;
222 } else {
223 *start = *start % 32;
224 *end = *end % 32;
225 }
226
227 return;
228 }
229
230 static inline uint64_t
231 mask(int start, int end)
232 {
233 uint64_t v;
234
235 v = ~0ULL >> (63 - end + start);
236
237 return v << start;
238 }
239
240 static inline uint64_t
241 field_value(uint64_t value, int start, int end)
242 {
243 get_start_end_pos(&start, &end);
244 return (value & mask(start, end)) >> (start);
245 }
246
247 static struct gen_type
248 string_to_type(struct parser_context *ctx, const char *s)
249 {
250 int i, f;
251 struct gen_group *g;
252 struct gen_enum *e;
253
254 if (strcmp(s, "int") == 0)
255 return (struct gen_type) { .kind = GEN_TYPE_INT };
256 else if (strcmp(s, "uint") == 0)
257 return (struct gen_type) { .kind = GEN_TYPE_UINT };
258 else if (strcmp(s, "bool") == 0)
259 return (struct gen_type) { .kind = GEN_TYPE_BOOL };
260 else if (strcmp(s, "float") == 0)
261 return (struct gen_type) { .kind = GEN_TYPE_FLOAT };
262 else if (strcmp(s, "address") == 0)
263 return (struct gen_type) { .kind = GEN_TYPE_ADDRESS };
264 else if (strcmp(s, "offset") == 0)
265 return (struct gen_type) { .kind = GEN_TYPE_OFFSET };
266 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
267 return (struct gen_type) { .kind = GEN_TYPE_UFIXED, .i = i, .f = f };
268 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
269 return (struct gen_type) { .kind = GEN_TYPE_SFIXED, .i = i, .f = f };
270 else if (g = gen_spec_find_struct(ctx->spec, s), g != NULL)
271 return (struct gen_type) { .kind = GEN_TYPE_STRUCT, .gen_struct = g };
272 else if (e = gen_spec_find_enum(ctx->spec, s), e != NULL)
273 return (struct gen_type) { .kind = GEN_TYPE_ENUM, .gen_enum = e };
274 else if (strcmp(s, "mbo") == 0)
275 return (struct gen_type) { .kind = GEN_TYPE_MBO };
276 else
277 fail(&ctx->loc, "invalid type: %s", s);
278 }
279
280 static struct gen_field *
281 create_field(struct parser_context *ctx, const char **atts)
282 {
283 struct gen_field *field;
284
285 field = rzalloc(ctx->group, struct gen_field);
286 field->parent = ctx->group;
287
288 for (int i = 0; atts[i]; i += 2) {
289 char *p;
290
291 if (strcmp(atts[i], "name") == 0)
292 field->name = ralloc_strdup(field, atts[i + 1]);
293 else if (strcmp(atts[i], "start") == 0)
294 field->start = strtoul(atts[i + 1], &p, 0);
295 else if (strcmp(atts[i], "end") == 0) {
296 field->end = strtoul(atts[i + 1], &p, 0);
297 } else if (strcmp(atts[i], "type") == 0)
298 field->type = string_to_type(ctx, atts[i + 1]);
299 else if (strcmp(atts[i], "default") == 0 &&
300 field->start >= 16 && field->end <= 31) {
301 field->has_default = true;
302 field->default_value = strtoul(atts[i + 1], &p, 0);
303 }
304 }
305
306 return field;
307 }
308
309 static struct gen_value *
310 create_value(struct parser_context *ctx, const char **atts)
311 {
312 struct gen_value *value = rzalloc(ctx->values, struct gen_value);
313
314 for (int i = 0; atts[i]; i += 2) {
315 if (strcmp(atts[i], "name") == 0)
316 value->name = ralloc_strdup(value, atts[i + 1]);
317 else if (strcmp(atts[i], "value") == 0)
318 value->value = strtoul(atts[i + 1], NULL, 0);
319 }
320
321 return value;
322 }
323
324 static struct gen_field *
325 create_and_append_field(struct parser_context *ctx,
326 const char **atts)
327 {
328 struct gen_field *field = create_field(ctx, atts);
329 struct gen_field *prev = NULL, *list = ctx->group->fields;
330
331 while (list && field->start > list->start) {
332 prev = list;
333 list = list->next;
334 }
335
336 field->next = list;
337 if (prev == NULL)
338 ctx->group->fields = field;
339 else
340 prev->next = field;
341
342 return field;
343 }
344
345 static void
346 start_element(void *data, const char *element_name, const char **atts)
347 {
348 struct parser_context *ctx = data;
349 const char *name = NULL;
350 const char *gen = NULL;
351
352 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
353
354 for (int i = 0; atts[i]; i += 2) {
355 if (strcmp(atts[i], "name") == 0)
356 name = atts[i + 1];
357 else if (strcmp(atts[i], "gen") == 0)
358 gen = atts[i + 1];
359 }
360
361 if (strcmp(element_name, "genxml") == 0) {
362 if (name == NULL)
363 fail(&ctx->loc, "no platform name given");
364 if (gen == NULL)
365 fail(&ctx->loc, "no gen given");
366
367 int major, minor;
368 int n = sscanf(gen, "%d.%d", &major, &minor);
369 if (n == 0)
370 fail(&ctx->loc, "invalid gen given: %s", gen);
371 if (n == 1)
372 minor = 0;
373
374 ctx->spec->gen = gen_make_gen(major, minor);
375 } else if (strcmp(element_name, "instruction") == 0) {
376 ctx->group = create_group(ctx, name, atts, NULL, false);
377 } else if (strcmp(element_name, "struct") == 0) {
378 ctx->group = create_group(ctx, name, atts, NULL, true);
379 } else if (strcmp(element_name, "register") == 0) {
380 ctx->group = create_group(ctx, name, atts, NULL, true);
381 get_register_offset(atts, &ctx->group->register_offset);
382 } else if (strcmp(element_name, "group") == 0) {
383 struct gen_group *previous_group = ctx->group;
384 while (previous_group->next)
385 previous_group = previous_group->next;
386
387 struct gen_group *group = create_group(ctx, "", atts, ctx->group, false);
388 previous_group->next = group;
389 ctx->group = group;
390 } else if (strcmp(element_name, "field") == 0) {
391 ctx->last_field = create_and_append_field(ctx, atts);
392 } else if (strcmp(element_name, "enum") == 0) {
393 ctx->enoom = create_enum(ctx, name, atts);
394 } else if (strcmp(element_name, "value") == 0) {
395 if (ctx->n_values >= ctx->n_allocated_values) {
396 ctx->n_allocated_values = MAX2(2, ctx->n_allocated_values * 2);
397 ctx->values = reralloc_array_size(ctx->spec, ctx->values,
398 sizeof(struct gen_value *),
399 ctx->n_allocated_values);
400 }
401 assert(ctx->n_values < ctx->n_allocated_values);
402 ctx->values[ctx->n_values++] = create_value(ctx, atts);
403 }
404
405 }
406
407 static void
408 end_element(void *data, const char *name)
409 {
410 struct parser_context *ctx = data;
411 struct gen_spec *spec = ctx->spec;
412
413 if (strcmp(name, "instruction") == 0 ||
414 strcmp(name, "struct") == 0 ||
415 strcmp(name, "register") == 0) {
416 struct gen_group *group = ctx->group;
417 struct gen_field *list = group->fields;
418
419 ctx->group = ctx->group->parent;
420
421 while (list && list->end <= 31) {
422 if (list->start >= 16 && list->has_default) {
423 group->opcode_mask |=
424 mask(list->start % 32, list->end % 32);
425 group->opcode |= list->default_value << list->start;
426 }
427 list = list->next;
428 }
429
430 if (strcmp(name, "instruction") == 0)
431 _mesa_hash_table_insert(spec->commands, group->name, group);
432 else if (strcmp(name, "struct") == 0)
433 _mesa_hash_table_insert(spec->structs, group->name, group);
434 else if (strcmp(name, "register") == 0) {
435 _mesa_hash_table_insert(spec->registers_by_name, group->name, group);
436 _mesa_hash_table_insert(spec->registers_by_offset,
437 (void *) (uintptr_t) group->register_offset,
438 group);
439 }
440 } else if (strcmp(name, "group") == 0) {
441 ctx->group = ctx->group->parent;
442 } else if (strcmp(name, "field") == 0) {
443 struct gen_field *field = ctx->last_field;
444 ctx->last_field = NULL;
445 field->inline_enum.values = ctx->values;
446 field->inline_enum.nvalues = ctx->n_values;
447 ctx->values = ralloc_array(ctx->spec, struct gen_value*, ctx->n_allocated_values = 2);
448 ctx->n_values = 0;
449 } else if (strcmp(name, "enum") == 0) {
450 struct gen_enum *e = ctx->enoom;
451 e->values = ctx->values;
452 e->nvalues = ctx->n_values;
453 ctx->values = ralloc_array(ctx->spec, struct gen_value*, ctx->n_allocated_values = 2);
454 ctx->n_values = 0;
455 ctx->enoom = NULL;
456 _mesa_hash_table_insert(spec->enums, e->name, e);
457 }
458 }
459
460 static void
461 character_data(void *data, const XML_Char *s, int len)
462 {
463 }
464
465 static int
466 devinfo_to_gen(const struct gen_device_info *devinfo)
467 {
468 int value = 10 * devinfo->gen;
469
470 if (devinfo->is_baytrail || devinfo->is_haswell)
471 value += 5;
472
473 return value;
474 }
475
476 static uint32_t zlib_inflate(const void *compressed_data,
477 uint32_t compressed_len,
478 void **out_ptr)
479 {
480 struct z_stream_s zstream;
481 void *out;
482
483 memset(&zstream, 0, sizeof(zstream));
484
485 zstream.next_in = (unsigned char *)compressed_data;
486 zstream.avail_in = compressed_len;
487
488 if (inflateInit(&zstream) != Z_OK)
489 return 0;
490
491 out = malloc(4096);
492 zstream.next_out = out;
493 zstream.avail_out = 4096;
494
495 do {
496 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
497 case Z_STREAM_END:
498 goto end;
499 case Z_OK:
500 break;
501 default:
502 inflateEnd(&zstream);
503 return 0;
504 }
505
506 if (zstream.avail_out)
507 break;
508
509 out = realloc(out, 2*zstream.total_out);
510 if (out == NULL) {
511 inflateEnd(&zstream);
512 return 0;
513 }
514
515 zstream.next_out = (unsigned char *)out + zstream.total_out;
516 zstream.avail_out = zstream.total_out;
517 } while (1);
518 end:
519 inflateEnd(&zstream);
520 *out_ptr = out;
521 return zstream.total_out;
522 }
523
524 static uint32_t _hash_uint32(const void *key)
525 {
526 return (uint32_t) (uintptr_t) key;
527 }
528
529 static struct gen_spec *
530 gen_spec_init(void)
531 {
532 struct gen_spec *spec;
533 spec = rzalloc(NULL, struct gen_spec);
534 if (spec == NULL)
535 return NULL;
536
537 spec->commands =
538 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
539 spec->structs =
540 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
541 spec->registers_by_name =
542 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
543 spec->registers_by_offset =
544 _mesa_hash_table_create(spec, _hash_uint32, _mesa_key_pointer_equal);
545 spec->enums =
546 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
547 spec->access_cache =
548 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
549
550 return spec;
551 }
552
553 struct gen_spec *
554 gen_spec_load(const struct gen_device_info *devinfo)
555 {
556 struct parser_context ctx;
557 void *buf;
558 uint8_t *text_data = NULL;
559 uint32_t text_offset = 0, text_length = 0;
560 MAYBE_UNUSED uint32_t total_length;
561 uint32_t gen_10 = devinfo_to_gen(devinfo);
562
563 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
564 if (genxml_files_table[i].gen_10 == gen_10) {
565 text_offset = genxml_files_table[i].offset;
566 text_length = genxml_files_table[i].length;
567 break;
568 }
569 }
570
571 if (text_length == 0) {
572 fprintf(stderr, "unable to find gen (%u) data\n", gen_10);
573 return NULL;
574 }
575
576 memset(&ctx, 0, sizeof ctx);
577 ctx.parser = XML_ParserCreate(NULL);
578 XML_SetUserData(ctx.parser, &ctx);
579 if (ctx.parser == NULL) {
580 fprintf(stderr, "failed to create parser\n");
581 return NULL;
582 }
583
584 XML_SetElementHandler(ctx.parser, start_element, end_element);
585 XML_SetCharacterDataHandler(ctx.parser, character_data);
586
587 ctx.spec = gen_spec_init();
588 if (ctx.spec == NULL) {
589 fprintf(stderr, "Failed to create gen_spec\n");
590 return NULL;
591 }
592
593 total_length = zlib_inflate(compress_genxmls,
594 sizeof(compress_genxmls),
595 (void **) &text_data);
596 assert(text_offset + text_length <= total_length);
597
598 buf = XML_GetBuffer(ctx.parser, text_length);
599 memcpy(buf, &text_data[text_offset], text_length);
600
601 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
602 fprintf(stderr,
603 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
604 XML_GetCurrentLineNumber(ctx.parser),
605 XML_GetCurrentColumnNumber(ctx.parser),
606 XML_GetCurrentByteIndex(ctx.parser), text_length,
607 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
608 XML_ParserFree(ctx.parser);
609 free(text_data);
610 return NULL;
611 }
612
613 XML_ParserFree(ctx.parser);
614 free(text_data);
615
616 return ctx.spec;
617 }
618
619 struct gen_spec *
620 gen_spec_load_from_path(const struct gen_device_info *devinfo,
621 const char *path)
622 {
623 struct parser_context ctx;
624 size_t len, filename_len = strlen(path) + 20;
625 char *filename = malloc(filename_len);
626 void *buf;
627 FILE *input;
628
629 len = snprintf(filename, filename_len, "%s/gen%i.xml",
630 path, devinfo_to_gen(devinfo));
631 assert(len < filename_len);
632
633 input = fopen(filename, "r");
634 if (input == NULL) {
635 fprintf(stderr, "failed to open xml description\n");
636 free(filename);
637 return NULL;
638 }
639
640 memset(&ctx, 0, sizeof ctx);
641 ctx.parser = XML_ParserCreate(NULL);
642 XML_SetUserData(ctx.parser, &ctx);
643 if (ctx.parser == NULL) {
644 fprintf(stderr, "failed to create parser\n");
645 fclose(input);
646 free(filename);
647 return NULL;
648 }
649
650 XML_SetElementHandler(ctx.parser, start_element, end_element);
651 XML_SetCharacterDataHandler(ctx.parser, character_data);
652 ctx.loc.filename = filename;
653
654 ctx.spec = gen_spec_init();
655 if (ctx.spec == NULL) {
656 fprintf(stderr, "Failed to create gen_spec\n");
657 return NULL;
658 }
659
660 do {
661 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
662 len = fread(buf, 1, XML_BUFFER_SIZE, input);
663 if (len == 0) {
664 fprintf(stderr, "fread: %m\n");
665 free(ctx.spec);
666 ctx.spec = NULL;
667 goto end;
668 }
669 if (XML_ParseBuffer(ctx.parser, len, len == 0) == 0) {
670 fprintf(stderr,
671 "Error parsing XML at line %ld col %ld: %s\n",
672 XML_GetCurrentLineNumber(ctx.parser),
673 XML_GetCurrentColumnNumber(ctx.parser),
674 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
675 free(ctx.spec);
676 ctx.spec = NULL;
677 goto end;
678 }
679 } while (len > 0);
680
681 end:
682 XML_ParserFree(ctx.parser);
683
684 fclose(input);
685 free(filename);
686
687 return ctx.spec;
688 }
689
690 void gen_spec_destroy(struct gen_spec *spec)
691 {
692 ralloc_free(spec);
693 }
694
695 struct gen_group *
696 gen_spec_find_instruction(struct gen_spec *spec, const uint32_t *p)
697 {
698 struct hash_entry *entry;
699
700 hash_table_foreach(spec->commands, entry) {
701 struct gen_group *command = entry->data;
702 uint32_t opcode = *p & command->opcode_mask;
703 if (opcode == command->opcode)
704 return command;
705 }
706
707 return NULL;
708 }
709
710 struct gen_field *
711 gen_group_find_field(struct gen_group *group, const char *name)
712 {
713 char path[256];
714 snprintf(path, sizeof(path), "%s/%s", group->name, name);
715
716 struct gen_spec *spec = group->spec;
717 struct hash_entry *entry = _mesa_hash_table_search(spec->access_cache,
718 path);
719 if (entry)
720 return entry->data;
721
722 struct gen_field *field = group->fields;
723 while (field) {
724 if (strcmp(field->name, name) == 0) {
725 _mesa_hash_table_insert(spec->access_cache,
726 ralloc_strdup(spec, path),
727 field);
728 return field;
729 }
730 field = field->next;
731 }
732
733 return NULL;
734 }
735
736 int
737 gen_group_get_length(struct gen_group *group, const uint32_t *p)
738 {
739 if (group && group->fixed_length)
740 return group->dw_length;
741
742 uint32_t h = p[0];
743 uint32_t type = field_value(h, 29, 31);
744
745 switch (type) {
746 case 0: /* MI */ {
747 uint32_t opcode = field_value(h, 23, 28);
748 if (opcode < 16)
749 return 1;
750 else
751 return field_value(h, 0, 7) + 2;
752 break;
753 }
754
755 case 2: /* BLT */ {
756 return field_value(h, 0, 7) + 2;
757 }
758
759 case 3: /* Render */ {
760 uint32_t subtype = field_value(h, 27, 28);
761 uint32_t opcode = field_value(h, 24, 26);
762 uint16_t whole_opcode = field_value(h, 16, 31);
763 switch (subtype) {
764 case 0:
765 if (whole_opcode == 0x6104 /* PIPELINE_SELECT_965 */)
766 return 1;
767 else if (opcode < 2)
768 return field_value(h, 0, 7) + 2;
769 else
770 return -1;
771 case 1:
772 if (opcode < 2)
773 return 1;
774 else
775 return -1;
776 case 2: {
777 if (opcode == 0)
778 return field_value(h, 0, 7) + 2;
779 else if (opcode < 3)
780 return field_value(h, 0, 15) + 2;
781 else
782 return -1;
783 }
784 case 3:
785 if (whole_opcode == 0x780b)
786 return 1;
787 else if (opcode < 4)
788 return field_value(h, 0, 7) + 2;
789 else
790 return -1;
791 }
792 }
793 }
794
795 return -1;
796 }
797
798 static const char *
799 gen_get_enum_name(struct gen_enum *e, uint64_t value)
800 {
801 for (int i = 0; i < e->nvalues; i++) {
802 if (e->values[i]->value == value) {
803 return e->values[i]->name;
804 }
805 }
806 return NULL;
807 }
808
809 static bool
810 iter_more_fields(const struct gen_field_iterator *iter)
811 {
812 return iter->field != NULL && iter->field->next != NULL;
813 }
814
815 static uint32_t
816 iter_group_offset_bits(const struct gen_field_iterator *iter,
817 uint32_t group_iter)
818 {
819 return iter->group->group_offset + (group_iter * iter->group->group_size);
820 }
821
822 static bool
823 iter_more_groups(const struct gen_field_iterator *iter)
824 {
825 if (iter->group->variable) {
826 int length = gen_group_get_length(iter->group, iter->p);
827 assert(length >= 0 && "error the length is unknown!");
828 return iter_group_offset_bits(iter, iter->group_iter + 1) <
829 (length * 32);
830 } else {
831 return (iter->group_iter + 1) < iter->group->group_count ||
832 iter->group->next != NULL;
833 }
834 }
835
836 static void
837 iter_start_field(struct gen_field_iterator *iter, struct gen_field *field)
838 {
839 iter->field = field;
840
841 int group_member_offset = iter_group_offset_bits(iter, iter->group_iter);
842
843 iter->start_bit = group_member_offset + iter->field->start;
844 iter->end_bit = group_member_offset + iter->field->end;
845 iter->struct_desc = NULL;
846 }
847
848 static void
849 iter_advance_group(struct gen_field_iterator *iter)
850 {
851 if (iter->group->variable)
852 iter->group_iter++;
853 else {
854 if ((iter->group_iter + 1) < iter->group->group_count) {
855 iter->group_iter++;
856 } else {
857 iter->group = iter->group->next;
858 iter->group_iter = 0;
859 }
860 }
861
862 iter_start_field(iter, iter->group->fields);
863 }
864
865 static bool
866 iter_advance_field(struct gen_field_iterator *iter)
867 {
868 if (iter_more_fields(iter)) {
869 iter_start_field(iter, iter->field->next);
870 } else {
871 if (!iter_more_groups(iter))
872 return false;
873
874 iter_advance_group(iter);
875 }
876 return true;
877 }
878
879 static bool
880 iter_decode_field_raw(struct gen_field_iterator *iter, uint64_t *qw)
881 {
882 *qw = 0;
883
884 int field_start = iter->p_bit + iter->start_bit;
885 int field_end = iter->p_bit + iter->end_bit;
886
887 const uint32_t *p = iter->p + (iter->start_bit / 32);
888 if (iter->p_end && p >= iter->p_end)
889 return false;
890
891 if ((field_end - field_start) > 32) {
892 if (!iter->p_end || (p + 1) < iter->p_end)
893 *qw = ((uint64_t) p[1]) << 32;
894 *qw |= p[0];
895 } else
896 *qw = p[0];
897
898 *qw = field_value(*qw, field_start, field_end);
899
900 /* Address & offset types have to be aligned to dwords, their start bit is
901 * a reminder of the alignment requirement.
902 */
903 if (iter->field->type.kind == GEN_TYPE_ADDRESS ||
904 iter->field->type.kind == GEN_TYPE_OFFSET)
905 *qw <<= field_start % 32;
906
907 return true;
908 }
909
910 static bool
911 iter_decode_field(struct gen_field_iterator *iter)
912 {
913 union {
914 uint64_t qw;
915 float f;
916 } v;
917
918 if (iter->field->name)
919 snprintf(iter->name, sizeof(iter->name), "%s", iter->field->name);
920 else
921 memset(iter->name, 0, sizeof(iter->name));
922
923 memset(&v, 0, sizeof(v));
924
925 if (!iter_decode_field_raw(iter, &iter->raw_value))
926 return false;
927
928 const char *enum_name = NULL;
929
930 v.qw = iter->raw_value;
931 switch (iter->field->type.kind) {
932 case GEN_TYPE_UNKNOWN:
933 case GEN_TYPE_INT: {
934 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
935 enum_name = gen_get_enum_name(&iter->field->inline_enum, v.qw);
936 break;
937 }
938 case GEN_TYPE_UINT: {
939 snprintf(iter->value, sizeof(iter->value), "%"PRIu64, v.qw);
940 enum_name = gen_get_enum_name(&iter->field->inline_enum, v.qw);
941 break;
942 }
943 case GEN_TYPE_BOOL: {
944 const char *true_string =
945 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
946 snprintf(iter->value, sizeof(iter->value), "%s",
947 v.qw ? true_string : "false");
948 break;
949 }
950 case GEN_TYPE_FLOAT:
951 snprintf(iter->value, sizeof(iter->value), "%f", v.f);
952 break;
953 case GEN_TYPE_ADDRESS:
954 case GEN_TYPE_OFFSET:
955 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64, v.qw);
956 break;
957 case GEN_TYPE_STRUCT:
958 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
959 iter->field->type.gen_struct->name);
960 iter->struct_desc =
961 gen_spec_find_struct(iter->group->spec,
962 iter->field->type.gen_struct->name);
963 break;
964 case GEN_TYPE_UFIXED:
965 snprintf(iter->value, sizeof(iter->value), "%f",
966 (float) v.qw / (1 << iter->field->type.f));
967 break;
968 case GEN_TYPE_SFIXED: {
969 /* Sign extend before converting */
970 int bits = iter->field->type.i + iter->field->type.f + 1;
971 int64_t v_sign_extend = ((int64_t)(v.qw << (64 - bits))) >> (64 - bits);
972 snprintf(iter->value, sizeof(iter->value), "%f",
973 (float) v_sign_extend / (1 << iter->field->type.f));
974 break;
975 }
976 case GEN_TYPE_MBO:
977 break;
978 case GEN_TYPE_ENUM: {
979 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
980 enum_name = gen_get_enum_name(iter->field->type.gen_enum, v.qw);
981 break;
982 }
983 }
984
985 if (strlen(iter->group->name) == 0) {
986 int length = strlen(iter->name);
987 snprintf(iter->name + length, sizeof(iter->name) - length,
988 "[%i]", iter->group_iter);
989 }
990
991 if (enum_name) {
992 int length = strlen(iter->value);
993 snprintf(iter->value + length, sizeof(iter->value) - length,
994 " (%s)", enum_name);
995 } else if (strcmp(iter->name, "Surface Format") == 0 ||
996 strcmp(iter->name, "Source Element Format") == 0) {
997 if (isl_format_is_valid((enum isl_format)v.qw)) {
998 const char *fmt_name = isl_format_get_name((enum isl_format)v.qw);
999 int length = strlen(iter->value);
1000 snprintf(iter->value + length, sizeof(iter->value) - length,
1001 " (%s)", fmt_name);
1002 }
1003 }
1004
1005 return true;
1006 }
1007
1008 void
1009 gen_field_iterator_init(struct gen_field_iterator *iter,
1010 struct gen_group *group,
1011 const uint32_t *p, int p_bit,
1012 bool print_colors)
1013 {
1014 memset(iter, 0, sizeof(*iter));
1015
1016 iter->group = group;
1017 iter->p = p;
1018 iter->p_bit = p_bit;
1019
1020 int length = gen_group_get_length(iter->group, iter->p);
1021 assert(length >= 0 && "error the length is unknown!");
1022 iter->p_end = length >= 0 ? &p[length] : NULL;
1023 iter->print_colors = print_colors;
1024 }
1025
1026 bool
1027 gen_field_iterator_next(struct gen_field_iterator *iter)
1028 {
1029 /* Initial condition */
1030 if (!iter->field) {
1031 if (iter->group->fields)
1032 iter_start_field(iter, iter->group->fields);
1033 else
1034 iter_start_field(iter, iter->group->next->fields);
1035
1036 bool result = iter_decode_field(iter);
1037 if (!result && iter->p_end) {
1038 /* We're dealing with a non empty struct of length=0 (BLEND_STATE on
1039 * Gen 7.5)
1040 */
1041 assert(iter->group->dw_length == 0);
1042 }
1043
1044 return result;
1045 }
1046
1047 if (!iter_advance_field(iter))
1048 return false;
1049
1050 if (!iter_decode_field(iter))
1051 return false;
1052
1053 return true;
1054 }
1055
1056 static void
1057 print_dword_header(FILE *outfile,
1058 struct gen_field_iterator *iter,
1059 uint64_t offset, uint32_t dword)
1060 {
1061 fprintf(outfile, "0x%08"PRIx64": 0x%08x : Dword %d\n",
1062 offset + 4 * dword, iter->p[dword], dword);
1063 }
1064
1065 bool
1066 gen_field_is_header(struct gen_field *field)
1067 {
1068 uint32_t bits;
1069
1070 if (field->start >= 32)
1071 return false;
1072
1073 bits = (1U << (field->end - field->start + 1)) - 1;
1074 bits <<= field->start;
1075
1076 return (field->parent->opcode_mask & bits) != 0;
1077 }
1078
1079 void
1080 gen_print_group(FILE *outfile, struct gen_group *group, uint64_t offset,
1081 const uint32_t *p, int p_bit, bool color)
1082 {
1083 struct gen_field_iterator iter;
1084 int last_dword = -1;
1085
1086 gen_field_iterator_init(&iter, group, p, p_bit, color);
1087 while (gen_field_iterator_next(&iter)) {
1088 int iter_dword = iter.end_bit / 32;
1089 if (last_dword != iter_dword) {
1090 for (int i = last_dword + 1; i <= iter_dword; i++)
1091 print_dword_header(outfile, &iter, offset, i);
1092 last_dword = iter_dword;
1093 }
1094 if (!gen_field_is_header(iter.field)) {
1095 fprintf(outfile, " %s: %s\n", iter.name, iter.value);
1096 if (iter.struct_desc) {
1097 int struct_dword = iter.start_bit / 32;
1098 uint64_t struct_offset = offset + 4 * struct_dword;
1099 gen_print_group(outfile, iter.struct_desc, struct_offset,
1100 &p[struct_dword], iter.start_bit % 32, color);
1101 }
1102 }
1103 }
1104 }