v3d: Dump fields in CLIF output in increasing offset order.
[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 int
515 field_offset_compare(const void *a, const void *b)
516 {
517 return ((*(const struct v3d_field **)a)->start -
518 (*(const struct v3d_field **)b)->start);
519 }
520
521 static void
522 end_element(void *data, const char *name)
523 {
524 struct parser_context *ctx = data;
525 struct v3d_spec *spec = ctx->spec;
526
527 ctx->parse_depth--;
528
529 if (ctx->parse_skip_depth) {
530 if (ctx->parse_skip_depth == ctx->parse_depth)
531 ctx->parse_skip_depth = 0;
532 return;
533 }
534
535 if (strcmp(name, "packet") == 0 ||
536 strcmp(name, "struct") == 0 ||
537 strcmp(name, "register") == 0) {
538 struct v3d_group *group = ctx->group;
539
540 ctx->group = ctx->group->parent;
541
542 if (strcmp(name, "packet") == 0) {
543 spec->commands[spec->ncommands++] = group;
544
545 /* V3D packet XML has the packet contents with offsets
546 * starting from the first bit after the opcode, to
547 * match the spec. Shift the fields up now.
548 */
549 for (int i = 0; i < group->nfields; i++) {
550 group->fields[i]->start += 8;
551 group->fields[i]->end += 8;
552 }
553 }
554 else if (strcmp(name, "struct") == 0)
555 spec->structs[spec->nstructs++] = group;
556 else if (strcmp(name, "register") == 0)
557 spec->registers[spec->nregisters++] = group;
558
559 /* Sort the fields in increasing offset order. The XML might
560 * be specified in any order, but we'll want to iterate from
561 * the bottom.
562 */
563 qsort(group->fields, group->nfields, sizeof(*group->fields),
564 field_offset_compare);
565
566 assert(spec->ncommands < ARRAY_SIZE(spec->commands));
567 assert(spec->nstructs < ARRAY_SIZE(spec->structs));
568 assert(spec->nregisters < ARRAY_SIZE(spec->registers));
569 } else if (strcmp(name, "group") == 0) {
570 ctx->group = ctx->group->parent;
571 } else if (strcmp(name, "field") == 0) {
572 assert(ctx->group->nfields > 0);
573 struct v3d_field *field = ctx->group->fields[ctx->group->nfields - 1];
574 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
575 field->inline_enum.values = xzalloc(size);
576 field->inline_enum.nvalues = ctx->nvalues;
577 memcpy(field->inline_enum.values, ctx->values, size);
578 ctx->nvalues = 0;
579 } else if (strcmp(name, "enum") == 0) {
580 struct v3d_enum *e = ctx->enoom;
581 size_t size = ctx->nvalues * sizeof(ctx->values[0]);
582 e->values = xzalloc(size);
583 e->nvalues = ctx->nvalues;
584 memcpy(e->values, ctx->values, size);
585 ctx->nvalues = 0;
586 ctx->enoom = NULL;
587 spec->enums[spec->nenums++] = e;
588 }
589 }
590
591 static void
592 character_data(void *data, const XML_Char *s, int len)
593 {
594 }
595
596 static uint32_t zlib_inflate(const void *compressed_data,
597 uint32_t compressed_len,
598 void **out_ptr)
599 {
600 struct z_stream_s zstream;
601 void *out;
602
603 memset(&zstream, 0, sizeof(zstream));
604
605 zstream.next_in = (unsigned char *)compressed_data;
606 zstream.avail_in = compressed_len;
607
608 if (inflateInit(&zstream) != Z_OK)
609 return 0;
610
611 out = malloc(4096);
612 zstream.next_out = out;
613 zstream.avail_out = 4096;
614
615 do {
616 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
617 case Z_STREAM_END:
618 goto end;
619 case Z_OK:
620 break;
621 default:
622 inflateEnd(&zstream);
623 return 0;
624 }
625
626 if (zstream.avail_out)
627 break;
628
629 out = realloc(out, 2*zstream.total_out);
630 if (out == NULL) {
631 inflateEnd(&zstream);
632 return 0;
633 }
634
635 zstream.next_out = (unsigned char *)out + zstream.total_out;
636 zstream.avail_out = zstream.total_out;
637 } while (1);
638 end:
639 inflateEnd(&zstream);
640 *out_ptr = out;
641 return zstream.total_out;
642 }
643
644 struct v3d_spec *
645 v3d_spec_load(const struct v3d_device_info *devinfo)
646 {
647 struct parser_context ctx;
648 void *buf;
649 uint8_t *text_data = NULL;
650 uint32_t text_offset = 0, text_length = 0, total_length;
651
652 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
653 if (i != 0) {
654 assert(genxml_files_table[i - 1].gen_10 <
655 genxml_files_table[i].gen_10);
656 }
657
658 if (genxml_files_table[i].gen_10 <= devinfo->ver) {
659 text_offset = genxml_files_table[i].offset;
660 text_length = genxml_files_table[i].length;
661 }
662 }
663
664 if (text_length == 0) {
665 fprintf(stderr, "unable to find gen (%u) data\n", devinfo->ver);
666 return NULL;
667 }
668
669 memset(&ctx, 0, sizeof ctx);
670 ctx.parser = XML_ParserCreate(NULL);
671 ctx.devinfo = devinfo;
672 XML_SetUserData(ctx.parser, &ctx);
673 if (ctx.parser == NULL) {
674 fprintf(stderr, "failed to create parser\n");
675 return NULL;
676 }
677
678 XML_SetElementHandler(ctx.parser, start_element, end_element);
679 XML_SetCharacterDataHandler(ctx.parser, character_data);
680
681 ctx.spec = xzalloc(sizeof(*ctx.spec));
682
683 total_length = zlib_inflate(compress_genxmls,
684 sizeof(compress_genxmls),
685 (void **) &text_data);
686 assert(text_offset + text_length <= total_length);
687
688 buf = XML_GetBuffer(ctx.parser, text_length);
689 memcpy(buf, &text_data[text_offset], text_length);
690
691 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
692 fprintf(stderr,
693 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
694 XML_GetCurrentLineNumber(ctx.parser),
695 XML_GetCurrentColumnNumber(ctx.parser),
696 XML_GetCurrentByteIndex(ctx.parser), text_length,
697 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
698 XML_ParserFree(ctx.parser);
699 free(text_data);
700 return NULL;
701 }
702
703 XML_ParserFree(ctx.parser);
704 free(text_data);
705
706 return ctx.spec;
707 }
708
709 struct v3d_group *
710 v3d_spec_find_instruction(struct v3d_spec *spec, const uint8_t *p)
711 {
712 uint8_t opcode = *p;
713
714 for (int i = 0; i < spec->ncommands; i++) {
715 struct v3d_group *group = spec->commands[i];
716
717 if (opcode != group->opcode)
718 continue;
719
720 /* If there's a "sub-id" field, make sure that it matches the
721 * instruction being decoded.
722 */
723 struct v3d_field *subid = NULL;
724 for (int j = 0; j < group->nfields; j++) {
725 struct v3d_field *field = group->fields[j];
726 if (strcmp(field->name, "sub-id") == 0) {
727 subid = field;
728 break;
729 }
730 }
731
732 if (subid && (__gen_unpack_uint(p, subid->start, subid->end) !=
733 subid->default_value)) {
734 continue;
735 }
736
737 return group;
738 }
739
740 return NULL;
741 }
742
743 /** Returns the size of a V3D packet. */
744 int
745 v3d_group_get_length(struct v3d_group *group)
746 {
747 int last_bit = 0;
748 for (int i = 0; i < group->nfields; i++) {
749 struct v3d_field *field = group->fields[i];
750
751 last_bit = MAX2(last_bit, field->end);
752 }
753 return last_bit / 8 + 1;
754 }
755
756 void
757 v3d_field_iterator_init(struct v3d_field_iterator *iter,
758 struct v3d_group *group,
759 const uint8_t *p)
760 {
761 memset(iter, 0, sizeof(*iter));
762
763 iter->group = group;
764 iter->p = p;
765 }
766
767 static const char *
768 v3d_get_enum_name(struct v3d_enum *e, uint64_t value)
769 {
770 for (int i = 0; i < e->nvalues; i++) {
771 if (e->values[i]->value == value) {
772 return e->values[i]->name;
773 }
774 }
775 return NULL;
776 }
777
778 static bool
779 iter_more_fields(const struct v3d_field_iterator *iter)
780 {
781 return iter->field_iter < iter->group->nfields;
782 }
783
784 static uint32_t
785 iter_group_offset_bits(const struct v3d_field_iterator *iter,
786 uint32_t group_iter)
787 {
788 return iter->group->group_offset + (group_iter *
789 iter->group->group_size);
790 }
791
792 static bool
793 iter_more_groups(const struct v3d_field_iterator *iter)
794 {
795 if (iter->group->variable) {
796 return iter_group_offset_bits(iter, iter->group_iter + 1) <
797 (v3d_group_get_length(iter->group) * 8);
798 } else {
799 return (iter->group_iter + 1) < iter->group->group_count ||
800 iter->group->next != NULL;
801 }
802 }
803
804 static void
805 iter_advance_group(struct v3d_field_iterator *iter)
806 {
807 if (iter->group->variable)
808 iter->group_iter++;
809 else {
810 if ((iter->group_iter + 1) < iter->group->group_count) {
811 iter->group_iter++;
812 } else {
813 iter->group = iter->group->next;
814 iter->group_iter = 0;
815 }
816 }
817
818 iter->field_iter = 0;
819 }
820
821 static bool
822 iter_advance_field(struct v3d_field_iterator *iter)
823 {
824 while (!iter_more_fields(iter)) {
825 if (!iter_more_groups(iter))
826 return false;
827
828 iter_advance_group(iter);
829 }
830
831 iter->field = iter->group->fields[iter->field_iter++];
832 if (iter->field->name)
833 strncpy(iter->name, iter->field->name, sizeof(iter->name));
834 else
835 memset(iter->name, 0, sizeof(iter->name));
836 iter->offset = iter_group_offset_bits(iter, iter->group_iter) / 8 +
837 iter->field->start / 8;
838 iter->struct_desc = NULL;
839
840 return true;
841 }
842
843 bool
844 v3d_field_iterator_next(struct clif_dump *clif, struct v3d_field_iterator *iter)
845 {
846 if (!iter_advance_field(iter))
847 return false;
848
849 const char *enum_name = NULL;
850
851 int group_member_offset =
852 iter_group_offset_bits(iter, iter->group_iter);
853 int s = group_member_offset + iter->field->start;
854 int e = group_member_offset + iter->field->end;
855
856 assert(!iter->field->minus_one ||
857 iter->field->type.kind == V3D_TYPE_INT ||
858 iter->field->type.kind == V3D_TYPE_UINT);
859
860 switch (iter->field->type.kind) {
861 case V3D_TYPE_UNKNOWN:
862 case V3D_TYPE_INT: {
863 uint32_t value = __gen_unpack_sint(iter->p, s, e);
864 if (iter->field->minus_one)
865 value++;
866 snprintf(iter->value, sizeof(iter->value), "%d", value);
867 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
868 break;
869 }
870 case V3D_TYPE_UINT: {
871 uint32_t value = __gen_unpack_uint(iter->p, s, e);
872 if (iter->field->minus_one)
873 value++;
874 if (strcmp(iter->field->name, "Vec size") == 0 && value == 0)
875 value = 1 << (e - s);
876 snprintf(iter->value, sizeof(iter->value), "%u", value);
877 enum_name = v3d_get_enum_name(&iter->field->inline_enum, value);
878 break;
879 }
880 case V3D_TYPE_BOOL:
881 snprintf(iter->value, sizeof(iter->value), "%s",
882 __gen_unpack_uint(iter->p, s, e) ?
883 "1 /* true */" : "0 /* false */");
884 break;
885 case V3D_TYPE_FLOAT:
886 snprintf(iter->value, sizeof(iter->value), "%f",
887 __gen_unpack_float(iter->p, s, e));
888 break;
889
890 case V3D_TYPE_ADDRESS: {
891 uint32_t addr =
892 __gen_unpack_uint(iter->p, s, e) << (31 - (e - s));
893 struct clif_bo *bo = clif_lookup_bo(clif, addr);
894 if (bo) {
895 snprintf(iter->value, sizeof(iter->value),
896 "[%s+0x%08x] /* 0x%08x */",
897 bo->name, addr - bo->offset, addr);
898 } else if (addr) {
899 snprintf(iter->value, sizeof(iter->value),
900 "/* XXX: BO unknown */ 0x%08x", addr);
901 } else {
902 snprintf(iter->value, sizeof(iter->value),
903 "[null]");
904 }
905
906 break;
907 }
908
909 case V3D_TYPE_OFFSET:
910 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64,
911 __gen_unpack_uint(iter->p, s, e) << (31 - (e - s)));
912 break;
913 case V3D_TYPE_STRUCT:
914 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
915 iter->field->type.v3d_struct->name);
916 iter->struct_desc =
917 v3d_spec_find_struct(iter->group->spec,
918 iter->field->type.v3d_struct->name);
919 break;
920 case V3D_TYPE_SFIXED:
921 snprintf(iter->value, sizeof(iter->value), "%f",
922 __gen_unpack_sfixed(iter->p, s, e,
923 iter->field->type.f));
924 break;
925 case V3D_TYPE_UFIXED:
926 snprintf(iter->value, sizeof(iter->value), "%f",
927 __gen_unpack_ufixed(iter->p, s, e,
928 iter->field->type.f));
929 break;
930 case V3D_TYPE_MBO:
931 break;
932 case V3D_TYPE_ENUM: {
933 uint32_t value = __gen_unpack_uint(iter->p, s, e);
934 snprintf(iter->value, sizeof(iter->value), "%d", value);
935 enum_name = v3d_get_enum_name(iter->field->type.v3d_enum, value);
936 break;
937 }
938 }
939
940 if (strlen(iter->group->name) == 0) {
941 int length = strlen(iter->name);
942 snprintf(iter->name + length, sizeof(iter->name) - length,
943 "[%i]", iter->group_iter);
944 }
945
946 if (enum_name) {
947 int length = strlen(iter->value);
948 snprintf(iter->value + length, sizeof(iter->value) - length,
949 " /* %s */", enum_name);
950 }
951
952 return true;
953 }
954
955 void
956 v3d_print_group(struct clif_dump *clif, struct v3d_group *group,
957 uint64_t offset, const uint8_t *p)
958 {
959 struct v3d_field_iterator iter;
960
961 v3d_field_iterator_init(&iter, group, p);
962 while (v3d_field_iterator_next(clif, &iter)) {
963 fprintf(clif->out, " %s: %s\n", iter.name, iter.value);
964 if (iter.struct_desc) {
965 uint64_t struct_offset = offset + iter.offset;
966 v3d_print_group(clif, iter.struct_desc,
967 struct_offset,
968 &p[iter.offset]);
969 }
970 }
971 }