OSDN Git Service

v3d: When not doing terminal pretty-printing, comment struct field names.
[android-x86/external-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, "f187") == 0)
320                 return (struct v3d_type) { .kind = V3D_TYPE_F187 };
321         else if (strcmp(s, "address") == 0)
322                 return (struct v3d_type) { .kind = V3D_TYPE_ADDRESS };
323         else if (strcmp(s, "offset") == 0)
324                 return (struct v3d_type) { .kind = V3D_TYPE_OFFSET };
325         else if (sscanf(s, "u%d.%d", &i, &f) == 2)
326                 return (struct v3d_type) { .kind = V3D_TYPE_UFIXED, .i = i, .f = f };
327         else if (sscanf(s, "s%d.%d", &i, &f) == 2)
328                 return (struct v3d_type) { .kind = V3D_TYPE_SFIXED, .i = i, .f = f };
329         else if (g = v3d_spec_find_struct(ctx->spec, s), g != NULL)
330                 return (struct v3d_type) { .kind = V3D_TYPE_STRUCT, .v3d_struct = g };
331         else if (e = v3d_spec_find_enum(ctx->spec, s), e != NULL)
332                 return (struct v3d_type) { .kind = V3D_TYPE_ENUM, .v3d_enum = e };
333         else if (strcmp(s, "mbo") == 0)
334                 return (struct v3d_type) { .kind = V3D_TYPE_MBO };
335         else
336                 fail(&ctx->loc, "invalid type: %s", s);
337 }
338
339 static struct v3d_field *
340 create_field(struct parser_context *ctx, const char **atts)
341 {
342         struct v3d_field *field;
343         char *p;
344         int i;
345         uint32_t size = 0;
346
347         field = xzalloc(sizeof(*field));
348
349         for (i = 0; atts[i]; i += 2) {
350                 if (strcmp(atts[i], "name") == 0)
351                         field->name = xstrdup(atts[i + 1]);
352                 else if (strcmp(atts[i], "start") == 0) {
353                         field->start = strtoul(atts[i + 1], &p, 0);
354                         if (is_byte_offset(atts[i + 1]))
355                                 field->start *= 8;
356                 } else if (strcmp(atts[i], "end") == 0) {
357                         field->end = strtoul(atts[i + 1], &p, 0) - 1;
358                         if (is_byte_offset(atts[i + 1]))
359                                 field->end *= 8;
360                 } else if (strcmp(atts[i], "size") == 0) {
361                         size = strtoul(atts[i + 1], &p, 0);
362                         if (is_byte_offset(atts[i + 1]))
363                                 size *= 8;
364                 } else if (strcmp(atts[i], "type") == 0)
365                         field->type = string_to_type(ctx, atts[i + 1]);
366                 else if (strcmp(atts[i], "default") == 0) {
367                         field->has_default = true;
368                         field->default_value = strtoul(atts[i + 1], &p, 0);
369                 } else if (strcmp(atts[i], "minus_one") == 0) {
370                         assert(strcmp(atts[i + 1], "true") == 0);
371                         field->minus_one = true;
372                 }
373         }
374
375         if (size)
376                 field->end = field->start + size - 1;
377
378         return field;
379 }
380
381 static struct v3d_value *
382 create_value(struct parser_context *ctx, const char **atts)
383 {
384         struct v3d_value *value = xzalloc(sizeof(*value));
385
386         for (int i = 0; atts[i]; i += 2) {
387                 if (strcmp(atts[i], "name") == 0)
388                         value->name = xstrdup(atts[i + 1]);
389                 else if (strcmp(atts[i], "value") == 0)
390                         value->value = strtoul(atts[i + 1], NULL, 0);
391         }
392
393         return value;
394 }
395
396 static void
397 create_and_append_field(struct parser_context *ctx,
398                         const char **atts)
399 {
400         if (ctx->group->nfields == ctx->group->fields_size) {
401                 ctx->group->fields_size = MAX2(ctx->group->fields_size * 2, 2);
402                 ctx->group->fields =
403                         (struct v3d_field **) realloc(ctx->group->fields,
404                                                       sizeof(ctx->group->fields[0]) *
405                                                       ctx->group->fields_size);
406         }
407
408         ctx->group->fields[ctx->group->nfields++] = create_field(ctx, atts);
409 }
410
411 static void
412 set_group_opcode(struct v3d_group *group, const char **atts)
413 {
414         char *p;
415         int i;
416
417         for (i = 0; atts[i]; i += 2) {
418                 if (strcmp(atts[i], "code") == 0)
419                         group->opcode = strtoul(atts[i + 1], &p, 0);
420         }
421         return;
422 }
423
424 static bool
425 ver_in_range(int ver, int min_ver, int max_ver)
426 {
427         return ((min_ver == 0 || ver >= min_ver) &&
428                 (max_ver == 0 || ver <= max_ver));
429 }
430
431 static bool
432 skip_if_ver_mismatch(struct parser_context *ctx, int min_ver, int max_ver)
433 {
434         if (!ctx->parse_skip_depth && !ver_in_range(ctx->devinfo->ver,
435                                                     min_ver, max_ver)) {
436                 assert(ctx->parse_depth != 0);
437                 ctx->parse_skip_depth = ctx->parse_depth;
438         }
439
440         return ctx->parse_skip_depth;
441 }
442
443 static void
444 start_element(void *data, const char *element_name, const char **atts)
445 {
446         struct parser_context *ctx = data;
447         int i;
448         const char *name = NULL;
449         const char *ver = NULL;
450         int min_ver = 0;
451         int max_ver = 0;
452
453         ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
454
455         for (i = 0; atts[i]; i += 2) {
456                 if (strcmp(atts[i], "name") == 0)
457                         name = atts[i + 1];
458                 else if (strcmp(atts[i], "gen") == 0)
459                         ver = atts[i + 1];
460                 else if (strcmp(atts[i], "min_ver") == 0)
461                         min_ver = strtoul(atts[i + 1], NULL, 0);
462                 else if (strcmp(atts[i], "max_ver") == 0)
463                         max_ver = strtoul(atts[i + 1], NULL, 0);
464         }
465
466         if (skip_if_ver_mismatch(ctx, min_ver, max_ver))
467                 goto skip;
468
469         if (strcmp(element_name, "vcxml") == 0) {
470                 if (ver == NULL)
471                         fail(&ctx->loc, "no ver given");
472
473                 /* Make sure that we picked an XML that matched our version.
474                  */
475                 assert(ver_in_range(ctx->devinfo->ver, min_ver, max_ver));
476
477                 int major, minor;
478                 int n = sscanf(ver, "%d.%d", &major, &minor);
479                 if (n == 0)
480                         fail(&ctx->loc, "invalid ver given: %s", ver);
481                 if (n == 1)
482                         minor = 0;
483
484                 ctx->spec->ver = major * 10 + minor;
485         } else if (strcmp(element_name, "packet") == 0 ||
486                    strcmp(element_name, "struct") == 0) {
487                 ctx->group = create_group(ctx, name, atts, NULL);
488
489                 if (strcmp(element_name, "packet") == 0)
490                         set_group_opcode(ctx->group, atts);
491         } else if (strcmp(element_name, "register") == 0) {
492                 ctx->group = create_group(ctx, name, atts, NULL);
493                 get_register_offset(atts, &ctx->group->register_offset);
494         } else if (strcmp(element_name, "group") == 0) {
495                 struct v3d_group *previous_group = ctx->group;
496                 while (previous_group->next)
497                         previous_group = previous_group->next;
498
499                 struct v3d_group *group = create_group(ctx, "", atts,
500                                                        ctx->group);
501                 previous_group->next = group;
502                 ctx->group = group;
503         } else if (strcmp(element_name, "field") == 0) {
504                 create_and_append_field(ctx, atts);
505         } else if (strcmp(element_name, "enum") == 0) {
506                 ctx->enoom = create_enum(ctx, name, atts);
507         } else if (strcmp(element_name, "value") == 0) {
508                 ctx->values[ctx->nvalues++] = create_value(ctx, atts);
509                 assert(ctx->nvalues < ARRAY_SIZE(ctx->values));
510         }
511
512 skip:
513         ctx->parse_depth++;
514 }
515
516 static int
517 field_offset_compare(const void *a, const void *b)
518 {
519         return ((*(const struct v3d_field **)a)->start -
520                 (*(const struct v3d_field **)b)->start);
521 }
522
523 static void
524 end_element(void *data, const char *name)
525 {
526         struct parser_context *ctx = data;
527         struct v3d_spec *spec = ctx->spec;
528
529         ctx->parse_depth--;
530
531         if (ctx->parse_skip_depth) {
532                 if (ctx->parse_skip_depth == ctx->parse_depth)
533                         ctx->parse_skip_depth = 0;
534                 return;
535         }
536
537         if (strcmp(name, "packet") == 0 ||
538             strcmp(name, "struct") == 0 ||
539             strcmp(name, "register") == 0) {
540                 struct v3d_group *group = ctx->group;
541
542                 ctx->group = ctx->group->parent;
543
544                 if (strcmp(name, "packet") == 0) {
545                         spec->commands[spec->ncommands++] = group;
546
547                         /* V3D packet XML has the packet contents with offsets
548                          * starting from the first bit after the opcode, to
549                          * match the spec.  Shift the fields up now.
550                          */
551                         for (int i = 0; i < group->nfields; i++) {
552                                 group->fields[i]->start += 8;
553                                 group->fields[i]->end += 8;
554                         }
555                 }
556                 else if (strcmp(name, "struct") == 0)
557                         spec->structs[spec->nstructs++] = group;
558                 else if (strcmp(name, "register") == 0)
559                         spec->registers[spec->nregisters++] = group;
560
561                 /* Sort the fields in increasing offset order.  The XML might
562                  * be specified in any order, but we'll want to iterate from
563                  * the bottom.
564                  */
565                 qsort(group->fields, group->nfields, sizeof(*group->fields),
566                       field_offset_compare);
567
568                 assert(spec->ncommands < ARRAY_SIZE(spec->commands));
569                 assert(spec->nstructs < ARRAY_SIZE(spec->structs));
570                 assert(spec->nregisters < ARRAY_SIZE(spec->registers));
571         } else if (strcmp(name, "group") == 0) {
572                 ctx->group = ctx->group->parent;
573         } else if (strcmp(name, "field") == 0) {
574                 assert(ctx->group->nfields > 0);
575                 struct v3d_field *field = ctx->group->fields[ctx->group->nfields - 1];
576                 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
577                 field->inline_enum.values = xzalloc(size);
578                 field->inline_enum.nvalues = ctx->nvalues;
579                 memcpy(field->inline_enum.values, ctx->values, size);
580                 ctx->nvalues = 0;
581         } else if (strcmp(name, "enum") == 0) {
582                 struct v3d_enum *e = ctx->enoom;
583                 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
584                 e->values = xzalloc(size);
585                 e->nvalues = ctx->nvalues;
586                 memcpy(e->values, ctx->values, size);
587                 ctx->nvalues = 0;
588                 ctx->enoom = NULL;
589                 spec->enums[spec->nenums++] = e;
590         }
591 }
592
593 static void
594 character_data(void *data, const XML_Char *s, int len)
595 {
596 }
597
598 static uint32_t zlib_inflate(const void *compressed_data,
599                              uint32_t compressed_len,
600                              void **out_ptr)
601 {
602         struct z_stream_s zstream;
603         void *out;
604
605         memset(&zstream, 0, sizeof(zstream));
606
607         zstream.next_in = (unsigned char *)compressed_data;
608         zstream.avail_in = compressed_len;
609
610         if (inflateInit(&zstream) != Z_OK)
611                 return 0;
612
613         out = malloc(4096);
614         zstream.next_out = out;
615         zstream.avail_out = 4096;
616
617         do {
618                 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
619                 case Z_STREAM_END:
620                         goto end;
621                 case Z_OK:
622                         break;
623                 default:
624                         inflateEnd(&zstream);
625                         return 0;
626                 }
627
628                 if (zstream.avail_out)
629                         break;
630
631                 out = realloc(out, 2*zstream.total_out);
632                 if (out == NULL) {
633                         inflateEnd(&zstream);
634                         return 0;
635                 }
636
637                 zstream.next_out = (unsigned char *)out + zstream.total_out;
638                 zstream.avail_out = zstream.total_out;
639         } while (1);
640  end:
641         inflateEnd(&zstream);
642         *out_ptr = out;
643         return zstream.total_out;
644 }
645
646 struct v3d_spec *
647 v3d_spec_load(const struct v3d_device_info *devinfo)
648 {
649         struct parser_context ctx;
650         void *buf;
651         uint8_t *text_data = NULL;
652         uint32_t text_offset = 0, text_length = 0, total_length;
653
654         for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
655                 if (i != 0) {
656                         assert(genxml_files_table[i - 1].gen_10 <
657                                genxml_files_table[i].gen_10);
658                 }
659
660                 if (genxml_files_table[i].gen_10 <= devinfo->ver) {
661                         text_offset = genxml_files_table[i].offset;
662                         text_length = genxml_files_table[i].length;
663                 }
664         }
665
666         if (text_length == 0) {
667                 fprintf(stderr, "unable to find gen (%u) data\n", devinfo->ver);
668                 return NULL;
669         }
670
671         memset(&ctx, 0, sizeof ctx);
672         ctx.parser = XML_ParserCreate(NULL);
673         ctx.devinfo = devinfo;
674         XML_SetUserData(ctx.parser, &ctx);
675         if (ctx.parser == NULL) {
676                 fprintf(stderr, "failed to create parser\n");
677                 return NULL;
678         }
679
680         XML_SetElementHandler(ctx.parser, start_element, end_element);
681         XML_SetCharacterDataHandler(ctx.parser, character_data);
682
683         ctx.spec = xzalloc(sizeof(*ctx.spec));
684
685         total_length = zlib_inflate(compress_genxmls,
686                                     sizeof(compress_genxmls),
687                                     (void **) &text_data);
688         assert(text_offset + text_length <= total_length);
689
690         buf = XML_GetBuffer(ctx.parser, text_length);
691         memcpy(buf, &text_data[text_offset], text_length);
692
693         if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
694                 fprintf(stderr,
695                         "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
696                         XML_GetCurrentLineNumber(ctx.parser),
697                         XML_GetCurrentColumnNumber(ctx.parser),
698                         XML_GetCurrentByteIndex(ctx.parser), text_length,
699                         XML_ErrorString(XML_GetErrorCode(ctx.parser)));
700                 XML_ParserFree(ctx.parser);
701                 free(text_data);
702                 return NULL;
703         }
704
705         XML_ParserFree(ctx.parser);
706         free(text_data);
707
708         return ctx.spec;
709 }
710
711 struct v3d_group *
712 v3d_spec_find_instruction(struct v3d_spec *spec, const uint8_t *p)
713 {
714         uint8_t opcode = *p;
715
716         for (int i = 0; i < spec->ncommands; i++) {
717                 struct v3d_group *group = spec->commands[i];
718
719                 if (opcode != group->opcode)
720                         continue;
721
722                 /* If there's a "sub-id" field, make sure that it matches the
723                  * instruction being decoded.
724                  */
725                 struct v3d_field *subid = NULL;
726                 for (int j = 0; j < group->nfields; j++) {
727                         struct v3d_field *field = group->fields[j];
728                         if (strcmp(field->name, "sub-id") == 0) {
729                                 subid = field;
730                                 break;
731                         }
732                 }
733
734                 if (subid && (__gen_unpack_uint(p, subid->start, subid->end) !=
735                               subid->default_value)) {
736                         continue;
737                 }
738
739                 return group;
740         }
741
742         return NULL;
743 }
744
745 /** Returns the size of a V3D packet. */
746 int
747 v3d_group_get_length(struct v3d_group *group)
748 {
749         int last_bit = 0;
750         for (int i = 0; i < group->nfields; i++) {
751                 struct v3d_field *field = group->fields[i];
752
753                 last_bit = MAX2(last_bit, field->end);
754         }
755         return last_bit / 8 + 1;
756 }
757
758 void
759 v3d_field_iterator_init(struct v3d_field_iterator *iter,
760                         struct v3d_group *group,
761                         const uint8_t *p)
762 {
763         memset(iter, 0, sizeof(*iter));
764
765         iter->group = group;
766         iter->p = p;
767 }
768
769 static const char *
770 v3d_get_enum_name(struct v3d_enum *e, uint64_t value)
771 {
772         for (int i = 0; i < e->nvalues; i++) {
773                 if (e->values[i]->value == value) {
774                         return e->values[i]->name;
775                 }
776         }
777         return NULL;
778 }
779
780 static bool
781 iter_more_fields(const struct v3d_field_iterator *iter)
782 {
783         return iter->field_iter < iter->group->nfields;
784 }
785
786 static uint32_t
787 iter_group_offset_bits(const struct v3d_field_iterator *iter,
788                        uint32_t group_iter)
789 {
790         return iter->group->group_offset + (group_iter *
791                                             iter->group->group_size);
792 }
793
794 static bool
795 iter_more_groups(const struct v3d_field_iterator *iter)
796 {
797         if (iter->group->variable) {
798                 return iter_group_offset_bits(iter, iter->group_iter + 1) <
799                         (v3d_group_get_length(iter->group) * 8);
800         } else {
801                 return (iter->group_iter + 1) < iter->group->group_count ||
802                         iter->group->next != NULL;
803         }
804 }
805
806 static void
807 iter_advance_group(struct v3d_field_iterator *iter)
808 {
809         if (iter->group->variable)
810                 iter->group_iter++;
811         else {
812                 if ((iter->group_iter + 1) < iter->group->group_count) {
813                         iter->group_iter++;
814                 } else {
815                         iter->group = iter->group->next;
816                         iter->group_iter = 0;
817                 }
818         }
819
820         iter->field_iter = 0;
821 }
822
823 static bool
824 iter_advance_field(struct v3d_field_iterator *iter)
825 {
826         while (!iter_more_fields(iter)) {
827                 if (!iter_more_groups(iter))
828                         return false;
829
830                 iter_advance_group(iter);
831         }
832
833         iter->field = iter->group->fields[iter->field_iter++];
834         if (iter->field->name)
835                 strncpy(iter->name, iter->field->name, sizeof(iter->name));
836         else
837                 memset(iter->name, 0, sizeof(iter->name));
838         iter->offset = iter_group_offset_bits(iter, iter->group_iter) / 8 +
839                 iter->field->start / 8;
840         iter->struct_desc = NULL;
841
842         return true;
843 }
844
845 bool
846 v3d_field_iterator_next(struct clif_dump *clif, struct v3d_field_iterator *iter)
847 {
848         if (!iter_advance_field(iter))
849                 return false;
850
851         const char *enum_name = NULL;
852
853         int group_member_offset =
854                 iter_group_offset_bits(iter, iter->group_iter);
855         int s = group_member_offset + iter->field->start;
856         int e = group_member_offset + iter->field->end;
857
858         assert(!iter->field->minus_one ||
859                iter->field->type.kind == V3D_TYPE_INT ||
860                iter->field->type.kind == V3D_TYPE_UINT);
861
862         switch (iter->field->type.kind) {
863         case V3D_TYPE_UNKNOWN:
864         case V3D_TYPE_INT: {
865                 uint32_t value = __gen_unpack_sint(iter->p, s, e);
866                 if (iter->field->minus_one)
867                         value++;
868                 snprintf(iter->value, sizeof(iter->value), "%d", value);
869                 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
870                 break;
871         }
872         case V3D_TYPE_UINT: {
873                 uint32_t value = __gen_unpack_uint(iter->p, s, e);
874                 if (iter->field->minus_one)
875                         value++;
876                 if (strcmp(iter->field->name, "Vec size") == 0 && value == 0)
877                         value = 1 << (e - s);
878                 snprintf(iter->value, sizeof(iter->value), "%u", value);
879                 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
880                 break;
881         }
882         case V3D_TYPE_BOOL:
883                 snprintf(iter->value, sizeof(iter->value), "%s",
884                          __gen_unpack_uint(iter->p, s, e) ?
885                          "1 /* true */" : "0 /* false */");
886                 break;
887         case V3D_TYPE_FLOAT:
888                 snprintf(iter->value, sizeof(iter->value), "%f",
889                          __gen_unpack_float(iter->p, s, e));
890                 break;
891
892         case V3D_TYPE_F187:
893                 snprintf(iter->value, sizeof(iter->value), "%f",
894                          __gen_unpack_f187(iter->p, s, e));
895                 break;
896
897         case V3D_TYPE_ADDRESS: {
898                 uint32_t addr =
899                         __gen_unpack_uint(iter->p, s, e) << (31 - (e - s));
900                 struct clif_bo *bo = clif_lookup_bo(clif, addr);
901                 if (bo) {
902                         snprintf(iter->value, sizeof(iter->value),
903                                  "[%s+0x%08x] /* 0x%08x */",
904                                  bo->name, addr - bo->offset, addr);
905                 } else if (addr) {
906                         snprintf(iter->value, sizeof(iter->value),
907                                  "/* XXX: BO unknown */ 0x%08x", addr);
908                 } else {
909                         snprintf(iter->value, sizeof(iter->value),
910                                  "[null]");
911                 }
912
913                 break;
914         }
915
916         case V3D_TYPE_OFFSET:
917                 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64,
918                          __gen_unpack_uint(iter->p, s, e) << (31 - (e - s)));
919                 break;
920         case V3D_TYPE_STRUCT:
921                 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
922                          iter->field->type.v3d_struct->name);
923                 iter->struct_desc =
924                         v3d_spec_find_struct(iter->group->spec,
925                                              iter->field->type.v3d_struct->name);
926                 break;
927         case V3D_TYPE_SFIXED:
928                 snprintf(iter->value, sizeof(iter->value), "%f",
929                          __gen_unpack_sfixed(iter->p, s, e,
930                                              iter->field->type.f));
931                 break;
932         case V3D_TYPE_UFIXED:
933                 snprintf(iter->value, sizeof(iter->value), "%f",
934                          __gen_unpack_ufixed(iter->p, s, e,
935                                              iter->field->type.f));
936                 break;
937         case V3D_TYPE_MBO:
938                 break;
939         case V3D_TYPE_ENUM: {
940                 uint32_t value = __gen_unpack_uint(iter->p, s, e);
941                 snprintf(iter->value, sizeof(iter->value), "%d", value);
942                 enum_name = v3d_get_enum_name(iter->field->type.v3d_enum, value);
943                 break;
944         }
945         }
946
947         if (strlen(iter->group->name) == 0) {
948                 int length = strlen(iter->name);
949                 snprintf(iter->name + length, sizeof(iter->name) - length,
950                          "[%i]", iter->group_iter);
951         }
952
953         if (enum_name) {
954                 int length = strlen(iter->value);
955                 snprintf(iter->value + length, sizeof(iter->value) - length,
956                          " /* %s */", enum_name);
957         }
958
959         return true;
960 }
961
962 void
963 v3d_print_group(struct clif_dump *clif, struct v3d_group *group,
964                 uint64_t offset, const uint8_t *p)
965 {
966         struct v3d_field_iterator iter;
967
968         v3d_field_iterator_init(&iter, group, p);
969         while (v3d_field_iterator_next(clif, &iter)) {
970                 /* Clif parsing uses the packet name, and expects no
971                  * sub-id.
972                  */
973                 if (strcmp(iter.field->name, "sub-id") == 0 ||
974                     strcmp(iter.field->name, "unused") == 0 ||
975                     strcmp(iter.field->name, "Pad") == 0)
976                         continue;
977
978                 if (clif->pretty) {
979                         fprintf(clif->out, "    %s: %s\n",
980                                 iter.name, iter.value);
981                 } else {
982                         fprintf(clif->out, "  /* %30s: */ %s\n",
983                                 iter.name, iter.value);
984                 }
985                 if (iter.struct_desc) {
986                         uint64_t struct_offset = offset + iter.offset;
987                         v3d_print_group(clif, iter.struct_desc,
988                                         struct_offset,
989                                         &p[iter.offset]);
990                 }
991         }
992 }