f97aa6604cb1a5218b8d93959e273332f7707e6e
[mesa.git] / src / broadcom / cle / v3d_decoder.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 * Copyright © 2017 Broadcom
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <expat.h>
31 #include <inttypes.h>
32 #include <zlib.h>
33
34 #include <util/macros.h>
35 #include <util/ralloc.h>
36
37 #include "v3d_decoder.h"
38 #include "v3d_packet_helpers.h"
39 #include "v3d_xml.h"
40 #include "broadcom/clif/clif_private.h"
41
42 struct v3d_spec {
43 uint32_t ver;
44
45 int ncommands;
46 struct v3d_group *commands[256];
47 int nstructs;
48 struct v3d_group *structs[256];
49 int nregisters;
50 struct v3d_group *registers[256];
51 int nenums;
52 struct v3d_enum *enums[256];
53 };
54
55 struct location {
56 const char *filename;
57 int line_number;
58 };
59
60 struct parser_context {
61 XML_Parser parser;
62 const struct v3d_device_info *devinfo;
63 int foo;
64 struct location loc;
65
66 struct v3d_group *group;
67 struct v3d_enum *enoom;
68
69 int nvalues;
70 struct v3d_value *values[256];
71
72 struct v3d_spec *spec;
73
74 int parse_depth;
75 int parse_skip_depth;
76 };
77
78 const char *
79 v3d_group_get_name(struct v3d_group *group)
80 {
81 return group->name;
82 }
83
84 uint8_t
85 v3d_group_get_opcode(struct v3d_group *group)
86 {
87 return group->opcode;
88 }
89
90 struct v3d_group *
91 v3d_spec_find_struct(struct v3d_spec *spec, const char *name)
92 {
93 for (int i = 0; i < spec->nstructs; i++)
94 if (strcmp(spec->structs[i]->name, name) == 0)
95 return spec->structs[i];
96
97 return NULL;
98 }
99
100 struct v3d_group *
101 v3d_spec_find_register(struct v3d_spec *spec, uint32_t offset)
102 {
103 for (int i = 0; i < spec->nregisters; i++)
104 if (spec->registers[i]->register_offset == offset)
105 return spec->registers[i];
106
107 return NULL;
108 }
109
110 struct v3d_group *
111 v3d_spec_find_register_by_name(struct v3d_spec *spec, const char *name)
112 {
113 for (int i = 0; i < spec->nregisters; i++) {
114 if (strcmp(spec->registers[i]->name, name) == 0)
115 return spec->registers[i];
116 }
117
118 return NULL;
119 }
120
121 struct v3d_enum *
122 v3d_spec_find_enum(struct v3d_spec *spec, const char *name)
123 {
124 for (int i = 0; i < spec->nenums; i++)
125 if (strcmp(spec->enums[i]->name, name) == 0)
126 return spec->enums[i];
127
128 return NULL;
129 }
130
131 static void __attribute__((noreturn))
132 fail(struct location *loc, const char *msg, ...)
133 {
134 va_list ap;
135
136 va_start(ap, msg);
137 fprintf(stderr, "%s:%d: error: ",
138 loc->filename, loc->line_number);
139 vfprintf(stderr, msg, ap);
140 fprintf(stderr, "\n");
141 va_end(ap);
142 exit(EXIT_FAILURE);
143 }
144
145 static void *
146 fail_on_null(void *p)
147 {
148 if (p == NULL) {
149 fprintf(stderr, "aubinator: out of memory\n");
150 exit(EXIT_FAILURE);
151 }
152
153 return p;
154 }
155
156 static char *
157 xstrdup(const char *s)
158 {
159 return fail_on_null(strdup(s));
160 }
161
162 static void *
163 zalloc(size_t s)
164 {
165 return calloc(s, 1);
166 }
167
168 static void *
169 xzalloc(size_t s)
170 {
171 return fail_on_null(zalloc(s));
172 }
173
174 /* We allow fields to have either a bit index, or append "b" for a byte index.
175 */
176 static bool
177 is_byte_offset(const char *value)
178 {
179 return value[strlen(value) - 1] == 'b';
180 }
181
182 static void
183 get_group_offset_count(const char **atts, uint32_t *offset, uint32_t *count,
184 uint32_t *size, bool *variable)
185 {
186 char *p;
187 int i;
188
189 for (i = 0; atts[i]; i += 2) {
190 if (strcmp(atts[i], "count") == 0) {
191 *count = strtoul(atts[i + 1], &p, 0);
192 if (*count == 0)
193 *variable = true;
194 } else if (strcmp(atts[i], "start") == 0) {
195 *offset = strtoul(atts[i + 1], &p, 0);
196 } else if (strcmp(atts[i], "size") == 0) {
197 *size = strtoul(atts[i + 1], &p, 0);
198 }
199 }
200 return;
201 }
202
203 static struct v3d_group *
204 create_group(struct parser_context *ctx,
205 const char *name,
206 const char **atts,
207 struct v3d_group *parent)
208 {
209 struct v3d_group *group;
210
211 group = xzalloc(sizeof(*group));
212 if (name)
213 group->name = xstrdup(name);
214
215 group->spec = ctx->spec;
216 group->group_offset = 0;
217 group->group_count = 0;
218 group->variable = false;
219
220 if (parent) {
221 group->parent = parent;
222 get_group_offset_count(atts,
223 &group->group_offset,
224 &group->group_count,
225 &group->group_size,
226 &group->variable);
227 }
228
229 return group;
230 }
231
232 static struct v3d_enum *
233 create_enum(struct parser_context *ctx, const char *name, const char **atts)
234 {
235 struct v3d_enum *e;
236
237 e = xzalloc(sizeof(*e));
238 if (name)
239 e->name = xstrdup(name);
240
241 e->nvalues = 0;
242
243 return e;
244 }
245
246 static void
247 get_register_offset(const char **atts, uint32_t *offset)
248 {
249 char *p;
250 int i;
251
252 for (i = 0; atts[i]; i += 2) {
253 if (strcmp(atts[i], "num") == 0)
254 *offset = strtoul(atts[i + 1], &p, 0);
255 }
256 return;
257 }
258
259 static void
260 get_start_end_pos(int *start, int *end)
261 {
262 /* start value has to be mod with 32 as we need the relative
263 * start position in the first DWord. For the end position, add
264 * the length of the field to the start position to get the
265 * relative postion in the 64 bit address.
266 */
267 if (*end - *start > 32) {
268 int len = *end - *start;
269 *start = *start % 32;
270 *end = *start + len;
271 } else {
272 *start = *start % 32;
273 *end = *end % 32;
274 }
275
276 return;
277 }
278
279 static inline uint64_t
280 mask(int start, int end)
281 {
282 uint64_t v;
283
284 v = ~0ULL >> (63 - end + start);
285
286 return v << start;
287 }
288
289 static inline uint64_t
290 field(uint64_t value, int start, int end)
291 {
292 get_start_end_pos(&start, &end);
293 return (value & mask(start, end)) >> (start);
294 }
295
296 static inline uint64_t
297 field_address(uint64_t value, int start, int end)
298 {
299 /* no need to right shift for address/offset */
300 get_start_end_pos(&start, &end);
301 return (value & mask(start, end));
302 }
303
304 static struct v3d_type
305 string_to_type(struct parser_context *ctx, const char *s)
306 {
307 int i, f;
308 struct v3d_group *g;
309 struct v3d_enum *e;
310
311 if (strcmp(s, "int") == 0)
312 return (struct v3d_type) { .kind = V3D_TYPE_INT };
313 else if (strcmp(s, "uint") == 0)
314 return (struct v3d_type) { .kind = V3D_TYPE_UINT };
315 else if (strcmp(s, "bool") == 0)
316 return (struct v3d_type) { .kind = V3D_TYPE_BOOL };
317 else if (strcmp(s, "float") == 0)
318 return (struct v3d_type) { .kind = V3D_TYPE_FLOAT };
319 else if (strcmp(s, "address") == 0)
320 return (struct v3d_type) { .kind = V3D_TYPE_ADDRESS };
321 else if (strcmp(s, "offset") == 0)
322 return (struct v3d_type) { .kind = V3D_TYPE_OFFSET };
323 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
324 return (struct v3d_type) { .kind = V3D_TYPE_UFIXED, .i = i, .f = f };
325 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
326 return (struct v3d_type) { .kind = V3D_TYPE_SFIXED, .i = i, .f = f };
327 else if (g = v3d_spec_find_struct(ctx->spec, s), g != NULL)
328 return (struct v3d_type) { .kind = V3D_TYPE_STRUCT, .v3d_struct = g };
329 else if (e = v3d_spec_find_enum(ctx->spec, s), e != NULL)
330 return (struct v3d_type) { .kind = V3D_TYPE_ENUM, .v3d_enum = e };
331 else if (strcmp(s, "mbo") == 0)
332 return (struct v3d_type) { .kind = V3D_TYPE_MBO };
333 else
334 fail(&ctx->loc, "invalid type: %s", s);
335 }
336
337 static struct v3d_field *
338 create_field(struct parser_context *ctx, const char **atts)
339 {
340 struct v3d_field *field;
341 char *p;
342 int i;
343 uint32_t size = 0;
344
345 field = xzalloc(sizeof(*field));
346
347 for (i = 0; atts[i]; i += 2) {
348 if (strcmp(atts[i], "name") == 0)
349 field->name = xstrdup(atts[i + 1]);
350 else if (strcmp(atts[i], "start") == 0) {
351 field->start = strtoul(atts[i + 1], &p, 0);
352 if (is_byte_offset(atts[i + 1]))
353 field->start *= 8;
354 } else if (strcmp(atts[i], "end") == 0) {
355 field->end = strtoul(atts[i + 1], &p, 0) - 1;
356 if (is_byte_offset(atts[i + 1]))
357 field->end *= 8;
358 } else if (strcmp(atts[i], "size") == 0) {
359 size = strtoul(atts[i + 1], &p, 0);
360 if (is_byte_offset(atts[i + 1]))
361 size *= 8;
362 } else if (strcmp(atts[i], "type") == 0)
363 field->type = string_to_type(ctx, atts[i + 1]);
364 else if (strcmp(atts[i], "default") == 0) {
365 field->has_default = true;
366 field->default_value = strtoul(atts[i + 1], &p, 0);
367 } else if (strcmp(atts[i], "minus_one") == 0) {
368 assert(strcmp(atts[i + 1], "true") == 0);
369 field->minus_one = true;
370 }
371 }
372
373 if (size)
374 field->end = field->start + size - 1;
375
376 return field;
377 }
378
379 static struct v3d_value *
380 create_value(struct parser_context *ctx, const char **atts)
381 {
382 struct v3d_value *value = xzalloc(sizeof(*value));
383
384 for (int i = 0; atts[i]; i += 2) {
385 if (strcmp(atts[i], "name") == 0)
386 value->name = xstrdup(atts[i + 1]);
387 else if (strcmp(atts[i], "value") == 0)
388 value->value = strtoul(atts[i + 1], NULL, 0);
389 }
390
391 return value;
392 }
393
394 static void
395 create_and_append_field(struct parser_context *ctx,
396 const char **atts)
397 {
398 if (ctx->group->nfields == ctx->group->fields_size) {
399 ctx->group->fields_size = MAX2(ctx->group->fields_size * 2, 2);
400 ctx->group->fields =
401 (struct v3d_field **) realloc(ctx->group->fields,
402 sizeof(ctx->group->fields[0]) *
403 ctx->group->fields_size);
404 }
405
406 ctx->group->fields[ctx->group->nfields++] = create_field(ctx, atts);
407 }
408
409 static void
410 set_group_opcode(struct v3d_group *group, const char **atts)
411 {
412 char *p;
413 int i;
414
415 for (i = 0; atts[i]; i += 2) {
416 if (strcmp(atts[i], "code") == 0)
417 group->opcode = strtoul(atts[i + 1], &p, 0);
418 }
419 return;
420 }
421
422 static bool
423 ver_in_range(int ver, int min_ver, int max_ver)
424 {
425 return ((min_ver == 0 || ver >= min_ver) &&
426 (max_ver == 0 || ver <= max_ver));
427 }
428
429 static bool
430 skip_if_ver_mismatch(struct parser_context *ctx, int min_ver, int max_ver)
431 {
432 if (!ctx->parse_skip_depth && !ver_in_range(ctx->devinfo->ver,
433 min_ver, max_ver)) {
434 assert(ctx->parse_depth != 0);
435 ctx->parse_skip_depth = ctx->parse_depth;
436 }
437
438 return ctx->parse_skip_depth;
439 }
440
441 static void
442 start_element(void *data, const char *element_name, const char **atts)
443 {
444 struct parser_context *ctx = data;
445 int i;
446 const char *name = NULL;
447 const char *ver = NULL;
448 int min_ver = 0;
449 int max_ver = 0;
450
451 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
452
453 for (i = 0; atts[i]; i += 2) {
454 if (strcmp(atts[i], "name") == 0)
455 name = atts[i + 1];
456 else if (strcmp(atts[i], "gen") == 0)
457 ver = atts[i + 1];
458 else if (strcmp(atts[i], "min_ver") == 0)
459 min_ver = strtoul(atts[i + 1], NULL, 0);
460 else if (strcmp(atts[i], "max_ver") == 0)
461 max_ver = strtoul(atts[i + 1], NULL, 0);
462 }
463
464 if (skip_if_ver_mismatch(ctx, min_ver, max_ver))
465 goto skip;
466
467 if (strcmp(element_name, "vcxml") == 0) {
468 if (ver == NULL)
469 fail(&ctx->loc, "no ver given");
470
471 /* Make sure that we picked an XML that matched our version.
472 */
473 assert(ver_in_range(ctx->devinfo->ver, min_ver, max_ver));
474
475 int major, minor;
476 int n = sscanf(ver, "%d.%d", &major, &minor);
477 if (n == 0)
478 fail(&ctx->loc, "invalid ver given: %s", ver);
479 if (n == 1)
480 minor = 0;
481
482 ctx->spec->ver = major * 10 + minor;
483 } else if (strcmp(element_name, "packet") == 0 ||
484 strcmp(element_name, "struct") == 0) {
485 ctx->group = create_group(ctx, name, atts, NULL);
486
487 if (strcmp(element_name, "packet") == 0)
488 set_group_opcode(ctx->group, atts);
489 } else if (strcmp(element_name, "register") == 0) {
490 ctx->group = create_group(ctx, name, atts, NULL);
491 get_register_offset(atts, &ctx->group->register_offset);
492 } else if (strcmp(element_name, "group") == 0) {
493 struct v3d_group *previous_group = ctx->group;
494 while (previous_group->next)
495 previous_group = previous_group->next;
496
497 struct v3d_group *group = create_group(ctx, "", atts,
498 ctx->group);
499 previous_group->next = group;
500 ctx->group = group;
501 } else if (strcmp(element_name, "field") == 0) {
502 create_and_append_field(ctx, atts);
503 } else if (strcmp(element_name, "enum") == 0) {
504 ctx->enoom = create_enum(ctx, name, atts);
505 } else if (strcmp(element_name, "value") == 0) {
506 ctx->values[ctx->nvalues++] = create_value(ctx, atts);
507 assert(ctx->nvalues < ARRAY_SIZE(ctx->values));
508 }
509
510 skip:
511 ctx->parse_depth++;
512 }
513
514 static void
515 end_element(void *data, const char *name)
516 {
517 struct parser_context *ctx = data;
518 struct v3d_spec *spec = ctx->spec;
519
520 ctx->parse_depth--;
521
522 if (ctx->parse_skip_depth) {
523 if (ctx->parse_skip_depth == ctx->parse_depth)
524 ctx->parse_skip_depth = 0;
525 return;
526 }
527
528 if (strcmp(name, "packet") == 0 ||
529 strcmp(name, "struct") == 0 ||
530 strcmp(name, "register") == 0) {
531 struct v3d_group *group = ctx->group;
532
533 ctx->group = ctx->group->parent;
534
535 if (strcmp(name, "packet") == 0) {
536 spec->commands[spec->ncommands++] = group;
537
538 /* V3D packet XML has the packet contents with offsets
539 * starting from the first bit after the opcode, to
540 * match the spec. Shift the fields up now.
541 */
542 for (int i = 0; i < group->nfields; i++) {
543 group->fields[i]->start += 8;
544 group->fields[i]->end += 8;
545 }
546 }
547 else if (strcmp(name, "struct") == 0)
548 spec->structs[spec->nstructs++] = group;
549 else if (strcmp(name, "register") == 0)
550 spec->registers[spec->nregisters++] = group;
551
552 assert(spec->ncommands < ARRAY_SIZE(spec->commands));
553 assert(spec->nstructs < ARRAY_SIZE(spec->structs));
554 assert(spec->nregisters < ARRAY_SIZE(spec->registers));
555 } else if (strcmp(name, "group") == 0) {
556 ctx->group = ctx->group->parent;
557 } else if (strcmp(name, "field") == 0) {
558 assert(ctx->group->nfields > 0);
559 struct v3d_field *field = ctx->group->fields[ctx->group->nfields - 1];
560 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
561 field->inline_enum.values = xzalloc(size);
562 field->inline_enum.nvalues = ctx->nvalues;
563 memcpy(field->inline_enum.values, ctx->values, size);
564 ctx->nvalues = 0;
565 } else if (strcmp(name, "enum") == 0) {
566 struct v3d_enum *e = ctx->enoom;
567 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
568 e->values = xzalloc(size);
569 e->nvalues = ctx->nvalues;
570 memcpy(e->values, ctx->values, size);
571 ctx->nvalues = 0;
572 ctx->enoom = NULL;
573 spec->enums[spec->nenums++] = e;
574 }
575 }
576
577 static void
578 character_data(void *data, const XML_Char *s, int len)
579 {
580 }
581
582 static uint32_t zlib_inflate(const void *compressed_data,
583 uint32_t compressed_len,
584 void **out_ptr)
585 {
586 struct z_stream_s zstream;
587 void *out;
588
589 memset(&zstream, 0, sizeof(zstream));
590
591 zstream.next_in = (unsigned char *)compressed_data;
592 zstream.avail_in = compressed_len;
593
594 if (inflateInit(&zstream) != Z_OK)
595 return 0;
596
597 out = malloc(4096);
598 zstream.next_out = out;
599 zstream.avail_out = 4096;
600
601 do {
602 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
603 case Z_STREAM_END:
604 goto end;
605 case Z_OK:
606 break;
607 default:
608 inflateEnd(&zstream);
609 return 0;
610 }
611
612 if (zstream.avail_out)
613 break;
614
615 out = realloc(out, 2*zstream.total_out);
616 if (out == NULL) {
617 inflateEnd(&zstream);
618 return 0;
619 }
620
621 zstream.next_out = (unsigned char *)out + zstream.total_out;
622 zstream.avail_out = zstream.total_out;
623 } while (1);
624 end:
625 inflateEnd(&zstream);
626 *out_ptr = out;
627 return zstream.total_out;
628 }
629
630 struct v3d_spec *
631 v3d_spec_load(const struct v3d_device_info *devinfo)
632 {
633 struct parser_context ctx;
634 void *buf;
635 uint8_t *text_data = NULL;
636 uint32_t text_offset = 0, text_length = 0, total_length;
637
638 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
639 if (i != 0) {
640 assert(genxml_files_table[i - 1].gen_10 <
641 genxml_files_table[i].gen_10);
642 }
643
644 if (genxml_files_table[i].gen_10 <= devinfo->ver) {
645 text_offset = genxml_files_table[i].offset;
646 text_length = genxml_files_table[i].length;
647 }
648 }
649
650 if (text_length == 0) {
651 fprintf(stderr, "unable to find gen (%u) data\n", devinfo->ver);
652 return NULL;
653 }
654
655 memset(&ctx, 0, sizeof ctx);
656 ctx.parser = XML_ParserCreate(NULL);
657 ctx.devinfo = devinfo;
658 XML_SetUserData(ctx.parser, &ctx);
659 if (ctx.parser == NULL) {
660 fprintf(stderr, "failed to create parser\n");
661 return NULL;
662 }
663
664 XML_SetElementHandler(ctx.parser, start_element, end_element);
665 XML_SetCharacterDataHandler(ctx.parser, character_data);
666
667 ctx.spec = xzalloc(sizeof(*ctx.spec));
668
669 total_length = zlib_inflate(compress_genxmls,
670 sizeof(compress_genxmls),
671 (void **) &text_data);
672 assert(text_offset + text_length <= total_length);
673
674 buf = XML_GetBuffer(ctx.parser, text_length);
675 memcpy(buf, &text_data[text_offset], text_length);
676
677 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
678 fprintf(stderr,
679 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
680 XML_GetCurrentLineNumber(ctx.parser),
681 XML_GetCurrentColumnNumber(ctx.parser),
682 XML_GetCurrentByteIndex(ctx.parser), text_length,
683 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
684 XML_ParserFree(ctx.parser);
685 free(text_data);
686 return NULL;
687 }
688
689 XML_ParserFree(ctx.parser);
690 free(text_data);
691
692 return ctx.spec;
693 }
694
695 struct v3d_group *
696 v3d_spec_find_instruction(struct v3d_spec *spec, const uint8_t *p)
697 {
698 uint8_t opcode = *p;
699
700 for (int i = 0; i < spec->ncommands; i++) {
701 struct v3d_group *group = spec->commands[i];
702
703 if (opcode != group->opcode)
704 continue;
705
706 /* If there's a "sub-id" field, make sure that it matches the
707 * instruction being decoded.
708 */
709 struct v3d_field *subid = NULL;
710 for (int j = 0; j < group->nfields; j++) {
711 struct v3d_field *field = group->fields[j];
712 if (strcmp(field->name, "sub-id") == 0) {
713 subid = field;
714 break;
715 }
716 }
717
718 if (subid && (__gen_unpack_uint(p, subid->start, subid->end) !=
719 subid->default_value)) {
720 continue;
721 }
722
723 return group;
724 }
725
726 return NULL;
727 }
728
729 /** Returns the size of a V3D packet. */
730 int
731 v3d_group_get_length(struct v3d_group *group)
732 {
733 int last_bit = 0;
734 for (int i = 0; i < group->nfields; i++) {
735 struct v3d_field *field = group->fields[i];
736
737 last_bit = MAX2(last_bit, field->end);
738 }
739 return last_bit / 8 + 1;
740 }
741
742 void
743 v3d_field_iterator_init(struct v3d_field_iterator *iter,
744 struct v3d_group *group,
745 const uint8_t *p)
746 {
747 memset(iter, 0, sizeof(*iter));
748
749 iter->group = group;
750 iter->p = p;
751 }
752
753 static const char *
754 v3d_get_enum_name(struct v3d_enum *e, uint64_t value)
755 {
756 for (int i = 0; i < e->nvalues; i++) {
757 if (e->values[i]->value == value) {
758 return e->values[i]->name;
759 }
760 }
761 return NULL;
762 }
763
764 static bool
765 iter_more_fields(const struct v3d_field_iterator *iter)
766 {
767 return iter->field_iter < iter->group->nfields;
768 }
769
770 static uint32_t
771 iter_group_offset_bits(const struct v3d_field_iterator *iter,
772 uint32_t group_iter)
773 {
774 return iter->group->group_offset + (group_iter *
775 iter->group->group_size);
776 }
777
778 static bool
779 iter_more_groups(const struct v3d_field_iterator *iter)
780 {
781 if (iter->group->variable) {
782 return iter_group_offset_bits(iter, iter->group_iter + 1) <
783 (v3d_group_get_length(iter->group) * 8);
784 } else {
785 return (iter->group_iter + 1) < iter->group->group_count ||
786 iter->group->next != NULL;
787 }
788 }
789
790 static void
791 iter_advance_group(struct v3d_field_iterator *iter)
792 {
793 if (iter->group->variable)
794 iter->group_iter++;
795 else {
796 if ((iter->group_iter + 1) < iter->group->group_count) {
797 iter->group_iter++;
798 } else {
799 iter->group = iter->group->next;
800 iter->group_iter = 0;
801 }
802 }
803
804 iter->field_iter = 0;
805 }
806
807 static bool
808 iter_advance_field(struct v3d_field_iterator *iter)
809 {
810 while (!iter_more_fields(iter)) {
811 if (!iter_more_groups(iter))
812 return false;
813
814 iter_advance_group(iter);
815 }
816
817 iter->field = iter->group->fields[iter->field_iter++];
818 if (iter->field->name)
819 strncpy(iter->name, iter->field->name, sizeof(iter->name));
820 else
821 memset(iter->name, 0, sizeof(iter->name));
822 iter->offset = iter_group_offset_bits(iter, iter->group_iter) / 8 +
823 iter->field->start / 8;
824 iter->struct_desc = NULL;
825
826 return true;
827 }
828
829 bool
830 v3d_field_iterator_next(struct v3d_field_iterator *iter)
831 {
832 if (!iter_advance_field(iter))
833 return false;
834
835 const char *enum_name = NULL;
836
837 int group_member_offset =
838 iter_group_offset_bits(iter, iter->group_iter);
839 int s = group_member_offset + iter->field->start;
840 int e = group_member_offset + iter->field->end;
841
842 assert(!iter->field->minus_one ||
843 iter->field->type.kind == V3D_TYPE_INT ||
844 iter->field->type.kind == V3D_TYPE_UINT);
845
846 switch (iter->field->type.kind) {
847 case V3D_TYPE_UNKNOWN:
848 case V3D_TYPE_INT: {
849 uint32_t value = __gen_unpack_sint(iter->p, s, e);
850 if (iter->field->minus_one)
851 value++;
852 snprintf(iter->value, sizeof(iter->value), "%d", value);
853 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
854 break;
855 }
856 case V3D_TYPE_UINT: {
857 uint32_t value = __gen_unpack_uint(iter->p, s, e);
858 if (iter->field->minus_one)
859 value++;
860 if (strcmp(iter->field->name, "Vec size") == 0 && value == 0)
861 value = 1 << (e - s);
862 snprintf(iter->value, sizeof(iter->value), "%u", value);
863 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
864 break;
865 }
866 case V3D_TYPE_BOOL:
867 snprintf(iter->value, sizeof(iter->value), "%s",
868 __gen_unpack_uint(iter->p, s, e) ?
869 "1 /* true */" : "0 /* false */");
870 break;
871 case V3D_TYPE_FLOAT:
872 snprintf(iter->value, sizeof(iter->value), "%f",
873 __gen_unpack_float(iter->p, s, e));
874 break;
875 case V3D_TYPE_ADDRESS:
876 case V3D_TYPE_OFFSET:
877 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64,
878 __gen_unpack_uint(iter->p, s, e) << (31 - (e - s)));
879 break;
880 case V3D_TYPE_STRUCT:
881 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
882 iter->field->type.v3d_struct->name);
883 iter->struct_desc =
884 v3d_spec_find_struct(iter->group->spec,
885 iter->field->type.v3d_struct->name);
886 break;
887 case V3D_TYPE_SFIXED:
888 snprintf(iter->value, sizeof(iter->value), "%f",
889 __gen_unpack_sfixed(iter->p, s, e,
890 iter->field->type.f));
891 break;
892 case V3D_TYPE_UFIXED:
893 snprintf(iter->value, sizeof(iter->value), "%f",
894 __gen_unpack_ufixed(iter->p, s, e,
895 iter->field->type.f));
896 break;
897 case V3D_TYPE_MBO:
898 break;
899 case V3D_TYPE_ENUM: {
900 uint32_t value = __gen_unpack_uint(iter->p, s, e);
901 snprintf(iter->value, sizeof(iter->value), "%d", value);
902 enum_name = v3d_get_enum_name(iter->field->type.v3d_enum, value);
903 break;
904 }
905 }
906
907 if (strlen(iter->group->name) == 0) {
908 int length = strlen(iter->name);
909 snprintf(iter->name + length, sizeof(iter->name) - length,
910 "[%i]", iter->group_iter);
911 }
912
913 if (enum_name) {
914 int length = strlen(iter->value);
915 snprintf(iter->value + length, sizeof(iter->value) - length,
916 " /* %s */", enum_name);
917 }
918
919 return true;
920 }
921
922 void
923 v3d_print_group(struct clif_dump *clif, struct v3d_group *group,
924 uint64_t offset, const uint8_t *p)
925 {
926 struct v3d_field_iterator iter;
927
928 v3d_field_iterator_init(&iter, group, p);
929 while (v3d_field_iterator_next(&iter)) {
930 fprintf(clif->out, " %s: %s\n", iter.name, iter.value);
931 if (iter.struct_desc) {
932 uint64_t struct_offset = offset + iter.offset;
933 v3d_print_group(clif, iter.struct_desc,
934 struct_offset,
935 &p[iter.offset]);
936 }
937 }
938 }