intel: decoder: fix starting dword of struct fields
[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 struct gen_spec *
530 gen_spec_load(const struct gen_device_info *devinfo)
531 {
532 struct parser_context ctx;
533 void *buf;
534 uint8_t *text_data = NULL;
535 uint32_t text_offset = 0, text_length = 0, total_length;
536 uint32_t gen_10 = devinfo_to_gen(devinfo);
537
538 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
539 if (genxml_files_table[i].gen_10 == gen_10) {
540 text_offset = genxml_files_table[i].offset;
541 text_length = genxml_files_table[i].length;
542 break;
543 }
544 }
545
546 if (text_length == 0) {
547 fprintf(stderr, "unable to find gen (%u) data\n", gen_10);
548 return NULL;
549 }
550
551 memset(&ctx, 0, sizeof ctx);
552 ctx.parser = XML_ParserCreate(NULL);
553 XML_SetUserData(ctx.parser, &ctx);
554 if (ctx.parser == NULL) {
555 fprintf(stderr, "failed to create parser\n");
556 return NULL;
557 }
558
559 XML_SetElementHandler(ctx.parser, start_element, end_element);
560 XML_SetCharacterDataHandler(ctx.parser, character_data);
561
562 ctx.spec = rzalloc(NULL, struct gen_spec);
563
564 ctx.spec->commands =
565 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
566 ctx.spec->structs =
567 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
568 ctx.spec->registers_by_name =
569 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
570 ctx.spec->registers_by_offset =
571 _mesa_hash_table_create(ctx.spec, _hash_uint32, _mesa_key_pointer_equal);
572 ctx.spec->enums =
573 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
574
575 ctx.spec->access_cache =
576 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
577
578 total_length = zlib_inflate(compress_genxmls,
579 sizeof(compress_genxmls),
580 (void **) &text_data);
581 assert(text_offset + text_length <= total_length);
582
583 buf = XML_GetBuffer(ctx.parser, text_length);
584 memcpy(buf, &text_data[text_offset], text_length);
585
586 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
587 fprintf(stderr,
588 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
589 XML_GetCurrentLineNumber(ctx.parser),
590 XML_GetCurrentColumnNumber(ctx.parser),
591 XML_GetCurrentByteIndex(ctx.parser), text_length,
592 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
593 XML_ParserFree(ctx.parser);
594 free(text_data);
595 return NULL;
596 }
597
598 XML_ParserFree(ctx.parser);
599 free(text_data);
600
601 return ctx.spec;
602 }
603
604 struct gen_spec *
605 gen_spec_load_from_path(const struct gen_device_info *devinfo,
606 const char *path)
607 {
608 struct parser_context ctx;
609 size_t len, filename_len = strlen(path) + 20;
610 char *filename = malloc(filename_len);
611 void *buf;
612 FILE *input;
613
614 len = snprintf(filename, filename_len, "%s/gen%i.xml",
615 path, devinfo_to_gen(devinfo));
616 assert(len < filename_len);
617
618 input = fopen(filename, "r");
619 if (input == NULL) {
620 fprintf(stderr, "failed to open xml description\n");
621 free(filename);
622 return NULL;
623 }
624
625 memset(&ctx, 0, sizeof ctx);
626 ctx.parser = XML_ParserCreate(NULL);
627 XML_SetUserData(ctx.parser, &ctx);
628 if (ctx.parser == NULL) {
629 fprintf(stderr, "failed to create parser\n");
630 fclose(input);
631 free(filename);
632 return NULL;
633 }
634
635 XML_SetElementHandler(ctx.parser, start_element, end_element);
636 XML_SetCharacterDataHandler(ctx.parser, character_data);
637 ctx.loc.filename = filename;
638 ctx.spec = rzalloc(NULL, struct gen_spec);
639
640 do {
641 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
642 len = fread(buf, 1, XML_BUFFER_SIZE, input);
643 if (len == 0) {
644 fprintf(stderr, "fread: %m\n");
645 free(ctx.spec);
646 ctx.spec = NULL;
647 goto end;
648 }
649 if (XML_ParseBuffer(ctx.parser, len, len == 0) == 0) {
650 fprintf(stderr,
651 "Error parsing XML at line %ld col %ld: %s\n",
652 XML_GetCurrentLineNumber(ctx.parser),
653 XML_GetCurrentColumnNumber(ctx.parser),
654 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
655 free(ctx.spec);
656 ctx.spec = NULL;
657 goto end;
658 }
659 } while (len > 0);
660
661 end:
662 XML_ParserFree(ctx.parser);
663
664 fclose(input);
665 free(filename);
666
667 return ctx.spec;
668 }
669
670 void gen_spec_destroy(struct gen_spec *spec)
671 {
672 ralloc_free(spec);
673 }
674
675 struct gen_group *
676 gen_spec_find_instruction(struct gen_spec *spec, const uint32_t *p)
677 {
678 struct hash_entry *entry;
679
680 hash_table_foreach(spec->commands, entry) {
681 struct gen_group *command = entry->data;
682 uint32_t opcode = *p & command->opcode_mask;
683 if (opcode == command->opcode)
684 return command;
685 }
686
687 return NULL;
688 }
689
690 struct gen_field *
691 gen_group_find_field(struct gen_group *group, const char *name)
692 {
693 char path[256];
694 snprintf(path, sizeof(path), "%s/%s", group->name, name);
695
696 struct gen_spec *spec = group->spec;
697 struct hash_entry *entry = _mesa_hash_table_search(spec->access_cache,
698 path);
699 if (entry)
700 return entry->data;
701
702 struct gen_field *field = group->fields;
703 while (field) {
704 if (strcmp(field->name, name) == 0) {
705 _mesa_hash_table_insert(spec->access_cache,
706 ralloc_strdup(spec, path),
707 field);
708 return field;
709 }
710 field = field->next;
711 }
712
713 return NULL;
714 }
715
716 int
717 gen_group_get_length(struct gen_group *group, const uint32_t *p)
718 {
719 if (group && group->fixed_length)
720 return group->dw_length;
721
722 uint32_t h = p[0];
723 uint32_t type = field_value(h, 29, 31);
724
725 switch (type) {
726 case 0: /* MI */ {
727 uint32_t opcode = field_value(h, 23, 28);
728 if (opcode < 16)
729 return 1;
730 else
731 return field_value(h, 0, 7) + 2;
732 break;
733 }
734
735 case 2: /* BLT */ {
736 return field_value(h, 0, 7) + 2;
737 }
738
739 case 3: /* Render */ {
740 uint32_t subtype = field_value(h, 27, 28);
741 uint32_t opcode = field_value(h, 24, 26);
742 uint16_t whole_opcode = field_value(h, 16, 31);
743 switch (subtype) {
744 case 0:
745 if (whole_opcode == 0x6104 /* PIPELINE_SELECT_965 */)
746 return 1;
747 else if (opcode < 2)
748 return field_value(h, 0, 7) + 2;
749 else
750 return -1;
751 case 1:
752 if (opcode < 2)
753 return 1;
754 else
755 return -1;
756 case 2: {
757 if (opcode == 0)
758 return field_value(h, 0, 7) + 2;
759 else if (opcode < 3)
760 return field_value(h, 0, 15) + 2;
761 else
762 return -1;
763 }
764 case 3:
765 if (whole_opcode == 0x780b)
766 return 1;
767 else if (opcode < 4)
768 return field_value(h, 0, 7) + 2;
769 else
770 return -1;
771 }
772 }
773 }
774
775 return -1;
776 }
777
778 static const char *
779 gen_get_enum_name(struct gen_enum *e, uint64_t value)
780 {
781 for (int i = 0; i < e->nvalues; i++) {
782 if (e->values[i]->value == value) {
783 return e->values[i]->name;
784 }
785 }
786 return NULL;
787 }
788
789 static bool
790 iter_more_fields(const struct gen_field_iterator *iter)
791 {
792 return iter->field != NULL && iter->field->next != NULL;
793 }
794
795 static uint32_t
796 iter_group_offset_bits(const struct gen_field_iterator *iter,
797 uint32_t group_iter)
798 {
799 return iter->group->group_offset + (group_iter * iter->group->group_size);
800 }
801
802 static bool
803 iter_more_groups(const struct gen_field_iterator *iter)
804 {
805 if (iter->group->variable) {
806 return iter_group_offset_bits(iter, iter->group_iter + 1) <
807 (gen_group_get_length(iter->group, iter->p) * 32);
808 } else {
809 return (iter->group_iter + 1) < iter->group->group_count ||
810 iter->group->next != NULL;
811 }
812 }
813
814 static void
815 iter_advance_group(struct gen_field_iterator *iter)
816 {
817 if (iter->group->variable)
818 iter->group_iter++;
819 else {
820 if ((iter->group_iter + 1) < iter->group->group_count) {
821 iter->group_iter++;
822 } else {
823 iter->group = iter->group->next;
824 iter->group_iter = 0;
825 }
826 }
827
828 iter->field = iter->group->fields;
829 }
830
831 static bool
832 iter_advance_field(struct gen_field_iterator *iter)
833 {
834 if (iter_more_fields(iter)) {
835 iter->field = iter->field->next;
836 } else {
837 if (!iter_more_groups(iter))
838 return false;
839
840 iter_advance_group(iter);
841 }
842
843 if (iter->field->name)
844 strncpy(iter->name, iter->field->name, sizeof(iter->name));
845 else
846 memset(iter->name, 0, sizeof(iter->name));
847
848 int group_member_offset = iter_group_offset_bits(iter, iter->group_iter);
849
850 iter->start_bit = group_member_offset + iter->field->start;
851 iter->end_bit = group_member_offset + iter->field->end;
852 iter->struct_desc = NULL;
853
854 return true;
855 }
856
857 static bool
858 iter_decode_field_raw(struct gen_field_iterator *iter, uint64_t *qw)
859 {
860 *qw = 0;
861
862 int field_start = iter->p_bit + iter->start_bit;
863 int field_end = iter->p_bit + iter->end_bit;
864
865 const uint32_t *p = iter->p + (iter->start_bit / 32);
866 if (iter->p_end && p >= iter->p_end)
867 return false;
868
869 if ((field_end - field_start) > 32) {
870 if (!iter->p_end || (p + 1) < iter->p_end)
871 *qw = ((uint64_t) p[1]) << 32;
872 *qw |= p[0];
873 } else
874 *qw = p[0];
875
876 *qw = field_value(*qw, field_start, field_end);
877
878 /* Address & offset types have to be aligned to dwords, their start bit is
879 * a reminder of the alignment requirement.
880 */
881 if (iter->field->type.kind == GEN_TYPE_ADDRESS ||
882 iter->field->type.kind == GEN_TYPE_OFFSET)
883 *qw <<= field_start % 32;
884
885 return true;
886 }
887
888 static bool
889 iter_decode_field(struct gen_field_iterator *iter)
890 {
891 union {
892 uint64_t qw;
893 float f;
894 } v;
895
896 if (iter->field->name)
897 strncpy(iter->name, iter->field->name, sizeof(iter->name));
898 else
899 memset(iter->name, 0, sizeof(iter->name));
900
901 memset(&v, 0, sizeof(v));
902
903 if (!iter_decode_field_raw(iter, &iter->raw_value))
904 return false;
905
906 const char *enum_name = NULL;
907
908 v.qw = iter->raw_value;
909 switch (iter->field->type.kind) {
910 case GEN_TYPE_UNKNOWN:
911 case GEN_TYPE_INT: {
912 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
913 enum_name = gen_get_enum_name(&iter->field->inline_enum, v.qw);
914 break;
915 }
916 case GEN_TYPE_UINT: {
917 snprintf(iter->value, sizeof(iter->value), "%"PRIu64, v.qw);
918 enum_name = gen_get_enum_name(&iter->field->inline_enum, v.qw);
919 break;
920 }
921 case GEN_TYPE_BOOL: {
922 const char *true_string =
923 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
924 snprintf(iter->value, sizeof(iter->value), "%s",
925 v.qw ? true_string : "false");
926 break;
927 }
928 case GEN_TYPE_FLOAT:
929 snprintf(iter->value, sizeof(iter->value), "%f", v.f);
930 break;
931 case GEN_TYPE_ADDRESS:
932 case GEN_TYPE_OFFSET:
933 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64, v.qw);
934 break;
935 case GEN_TYPE_STRUCT:
936 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
937 iter->field->type.gen_struct->name);
938 iter->struct_desc =
939 gen_spec_find_struct(iter->group->spec,
940 iter->field->type.gen_struct->name);
941 break;
942 case GEN_TYPE_UFIXED:
943 snprintf(iter->value, sizeof(iter->value), "%f",
944 (float) v.qw / (1 << iter->field->type.f));
945 break;
946 case GEN_TYPE_SFIXED:
947 /* FIXME: Sign extend extracted field. */
948 snprintf(iter->value, sizeof(iter->value), "%s", "foo");
949 break;
950 case GEN_TYPE_MBO:
951 break;
952 case GEN_TYPE_ENUM: {
953 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
954 enum_name = gen_get_enum_name(iter->field->type.gen_enum, v.qw);
955 break;
956 }
957 }
958
959 if (strlen(iter->group->name) == 0) {
960 int length = strlen(iter->name);
961 snprintf(iter->name + length, sizeof(iter->name) - length,
962 "[%i]", iter->group_iter);
963 }
964
965 if (enum_name) {
966 int length = strlen(iter->value);
967 snprintf(iter->value + length, sizeof(iter->value) - length,
968 " (%s)", enum_name);
969 } else if (strcmp(iter->name, "Surface Format") == 0) {
970 if (isl_format_is_valid((enum isl_format)v.qw)) {
971 const char *fmt_name = isl_format_get_name((enum isl_format)v.qw);
972 int length = strlen(iter->value);
973 snprintf(iter->value + length, sizeof(iter->value) - length,
974 " (%s)", fmt_name);
975 }
976 }
977
978 return true;
979 }
980
981 void
982 gen_field_iterator_init(struct gen_field_iterator *iter,
983 struct gen_group *group,
984 const uint32_t *p, int p_bit,
985 bool print_colors)
986 {
987 memset(iter, 0, sizeof(*iter));
988
989 iter->group = group;
990 iter->p = p;
991 iter->p_bit = p_bit;
992
993 int length = gen_group_get_length(iter->group, iter->p);
994 iter->p_end = length > 0 ? &p[length] : NULL;
995 iter->print_colors = print_colors;
996 }
997
998 bool
999 gen_field_iterator_next(struct gen_field_iterator *iter)
1000 {
1001 /* Initial condition */
1002 if (!iter->field) {
1003 if (iter->group->fields)
1004 iter->field = iter->group->fields;
1005 else
1006 iter->field = iter->group->next->fields;
1007
1008 bool result = iter_decode_field(iter);
1009 if (iter->p_end)
1010 assert(result);
1011
1012 return true;
1013 }
1014
1015 if (!iter_advance_field(iter))
1016 return false;
1017
1018 if (!iter_decode_field(iter))
1019 return false;
1020
1021 return true;
1022 }
1023
1024 static void
1025 print_dword_header(FILE *outfile,
1026 struct gen_field_iterator *iter,
1027 uint64_t offset, uint32_t dword)
1028 {
1029 fprintf(outfile, "0x%08"PRIx64": 0x%08x : Dword %d\n",
1030 offset + 4 * dword, iter->p[dword], dword);
1031 }
1032
1033 bool
1034 gen_field_is_header(struct gen_field *field)
1035 {
1036 uint32_t bits;
1037
1038 if (field->start >= 32)
1039 return false;
1040
1041 bits = (1U << (field->end - field->start + 1)) - 1;
1042 bits <<= field->start;
1043
1044 return (field->parent->opcode_mask & bits) != 0;
1045 }
1046
1047 void
1048 gen_print_group(FILE *outfile, struct gen_group *group, uint64_t offset,
1049 const uint32_t *p, int p_bit, bool color)
1050 {
1051 struct gen_field_iterator iter;
1052 int last_dword = -1;
1053
1054 gen_field_iterator_init(&iter, group, p, p_bit, color);
1055 while (gen_field_iterator_next(&iter)) {
1056 int iter_dword = iter.end_bit / 32;
1057 if (last_dword != iter_dword) {
1058 for (int i = last_dword + 1; i <= iter_dword; i++)
1059 print_dword_header(outfile, &iter, offset, i);
1060 last_dword = iter_dword;
1061 }
1062 if (!gen_field_is_header(iter.field)) {
1063 fprintf(outfile, " %s: %s\n", iter.name, iter.value);
1064 if (iter.struct_desc) {
1065 int struct_dword = iter.start_bit / 32;
1066 uint64_t struct_offset = offset + 4 * struct_dword;
1067 gen_print_group(outfile, iter.struct_desc, struct_offset,
1068 &p[struct_dword], iter.start_bit % 32, color);
1069 }
1070 }
1071 }
1072 }