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