intel/decoder: fix the possible out of bounds group_iter
[mesa.git] / src / intel / common / gen_decoder.c
1 /*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <stdbool.h>
26 #include <stdint.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <expat.h>
30 #include <inttypes.h>
31 #include <zlib.h>
32
33 #include <util/macros.h>
34 #include <util/ralloc.h>
35
36 #include "gen_decoder.h"
37
38 #include "isl/isl.h"
39 #include "genxml/genX_xml.h"
40
41 #define XML_BUFFER_SIZE 4096
42 #define MAX_VALUE_ITEMS 128
43
44 struct location {
45 const char *filename;
46 int line_number;
47 };
48
49 struct parser_context {
50 XML_Parser parser;
51 int foo;
52 struct location loc;
53
54 struct gen_group *group;
55 struct gen_enum *enoom;
56
57 int n_values, n_allocated_values;
58 struct gen_value **values;
59
60 struct gen_field *last_field;
61
62 struct gen_spec *spec;
63 };
64
65 const char *
66 gen_group_get_name(struct gen_group *group)
67 {
68 return group->name;
69 }
70
71 uint32_t
72 gen_group_get_opcode(struct gen_group *group)
73 {
74 return group->opcode;
75 }
76
77 struct gen_group *
78 gen_spec_find_struct(struct gen_spec *spec, const char *name)
79 {
80 struct hash_entry *entry = _mesa_hash_table_search(spec->structs,
81 name);
82 return entry ? entry->data : NULL;
83 }
84
85 struct gen_group *
86 gen_spec_find_register(struct gen_spec *spec, uint32_t offset)
87 {
88 struct hash_entry *entry =
89 _mesa_hash_table_search(spec->registers_by_offset,
90 (void *) (uintptr_t) offset);
91 return entry ? entry->data : NULL;
92 }
93
94 struct gen_group *
95 gen_spec_find_register_by_name(struct gen_spec *spec, const char *name)
96 {
97 struct hash_entry *entry =
98 _mesa_hash_table_search(spec->registers_by_name, name);
99 return entry ? entry->data : NULL;
100 }
101
102 struct gen_enum *
103 gen_spec_find_enum(struct gen_spec *spec, const char *name)
104 {
105 struct hash_entry *entry = _mesa_hash_table_search(spec->enums,
106 name);
107 return entry ? entry->data : NULL;
108 }
109
110 uint32_t
111 gen_spec_get_gen(struct gen_spec *spec)
112 {
113 return spec->gen;
114 }
115
116 static void __attribute__((noreturn))
117 fail(struct location *loc, const char *msg, ...)
118 {
119 va_list ap;
120
121 va_start(ap, msg);
122 fprintf(stderr, "%s:%d: error: ",
123 loc->filename, loc->line_number);
124 vfprintf(stderr, msg, ap);
125 fprintf(stderr, "\n");
126 va_end(ap);
127 exit(EXIT_FAILURE);
128 }
129
130 static void
131 get_group_offset_count(const char **atts, uint32_t *offset, uint32_t *count,
132 uint32_t *size, bool *variable)
133 {
134 for (int i = 0; atts[i]; i += 2) {
135 char *p;
136
137 if (strcmp(atts[i], "count") == 0) {
138 *count = strtoul(atts[i + 1], &p, 0);
139 if (*count == 0)
140 *variable = true;
141 } else if (strcmp(atts[i], "start") == 0) {
142 *offset = strtoul(atts[i + 1], &p, 0);
143 } else if (strcmp(atts[i], "size") == 0) {
144 *size = strtoul(atts[i + 1], &p, 0);
145 }
146 }
147 return;
148 }
149
150 static struct gen_group *
151 create_group(struct parser_context *ctx,
152 const char *name,
153 const char **atts,
154 struct gen_group *parent,
155 bool fixed_length)
156 {
157 struct gen_group *group;
158
159 group = rzalloc(ctx->spec, struct gen_group);
160 if (name)
161 group->name = ralloc_strdup(group, name);
162
163 group->spec = ctx->spec;
164 group->variable = false;
165 group->fixed_length = fixed_length;
166
167 for (int i = 0; atts[i]; i += 2) {
168 char *p;
169 if (strcmp(atts[i], "length") == 0) {
170 group->dw_length = strtoul(atts[i + 1], &p, 0);
171 }
172 }
173
174 if (parent) {
175 group->parent = parent;
176 get_group_offset_count(atts,
177 &group->group_offset,
178 &group->group_count,
179 &group->group_size,
180 &group->variable);
181 }
182
183 return group;
184 }
185
186 static struct gen_enum *
187 create_enum(struct parser_context *ctx, const char *name, const char **atts)
188 {
189 struct gen_enum *e;
190
191 e = rzalloc(ctx->spec, struct gen_enum);
192 if (name)
193 e->name = ralloc_strdup(e, name);
194
195 return e;
196 }
197
198 static void
199 get_register_offset(const char **atts, uint32_t *offset)
200 {
201 for (int i = 0; atts[i]; i += 2) {
202 char *p;
203
204 if (strcmp(atts[i], "num") == 0)
205 *offset = strtoul(atts[i + 1], &p, 0);
206 }
207 return;
208 }
209
210 static void
211 get_start_end_pos(int *start, int *end)
212 {
213 /* start value has to be mod with 32 as we need the relative
214 * start position in the first DWord. For the end position, add
215 * the length of the field to the start position to get the
216 * relative postion in the 64 bit address.
217 */
218 if (*end - *start > 32) {
219 int len = *end - *start;
220 *start = *start % 32;
221 *end = *start + len;
222 } else {
223 *start = *start % 32;
224 *end = *end % 32;
225 }
226
227 return;
228 }
229
230 static inline uint64_t
231 mask(int start, int end)
232 {
233 uint64_t v;
234
235 v = ~0ULL >> (63 - end + start);
236
237 return v << start;
238 }
239
240 static inline uint64_t
241 field_value(uint64_t value, int start, int end)
242 {
243 get_start_end_pos(&start, &end);
244 return (value & mask(start, end)) >> (start);
245 }
246
247 static struct gen_type
248 string_to_type(struct parser_context *ctx, const char *s)
249 {
250 int i, f;
251 struct gen_group *g;
252 struct gen_enum *e;
253
254 if (strcmp(s, "int") == 0)
255 return (struct gen_type) { .kind = GEN_TYPE_INT };
256 else if (strcmp(s, "uint") == 0)
257 return (struct gen_type) { .kind = GEN_TYPE_UINT };
258 else if (strcmp(s, "bool") == 0)
259 return (struct gen_type) { .kind = GEN_TYPE_BOOL };
260 else if (strcmp(s, "float") == 0)
261 return (struct gen_type) { .kind = GEN_TYPE_FLOAT };
262 else if (strcmp(s, "address") == 0)
263 return (struct gen_type) { .kind = GEN_TYPE_ADDRESS };
264 else if (strcmp(s, "offset") == 0)
265 return (struct gen_type) { .kind = GEN_TYPE_OFFSET };
266 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
267 return (struct gen_type) { .kind = GEN_TYPE_UFIXED, .i = i, .f = f };
268 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
269 return (struct gen_type) { .kind = GEN_TYPE_SFIXED, .i = i, .f = f };
270 else if (g = gen_spec_find_struct(ctx->spec, s), g != NULL)
271 return (struct gen_type) { .kind = GEN_TYPE_STRUCT, .gen_struct = g };
272 else if (e = gen_spec_find_enum(ctx->spec, s), e != NULL)
273 return (struct gen_type) { .kind = GEN_TYPE_ENUM, .gen_enum = e };
274 else if (strcmp(s, "mbo") == 0)
275 return (struct gen_type) { .kind = GEN_TYPE_MBO };
276 else
277 fail(&ctx->loc, "invalid type: %s", s);
278 }
279
280 static struct gen_field *
281 create_field(struct parser_context *ctx, const char **atts)
282 {
283 struct gen_field *field;
284
285 field = rzalloc(ctx->group, struct gen_field);
286 field->parent = ctx->group;
287
288 for (int i = 0; atts[i]; i += 2) {
289 char *p;
290
291 if (strcmp(atts[i], "name") == 0)
292 field->name = ralloc_strdup(field, atts[i + 1]);
293 else if (strcmp(atts[i], "start") == 0)
294 field->start = strtoul(atts[i + 1], &p, 0);
295 else if (strcmp(atts[i], "end") == 0) {
296 field->end = strtoul(atts[i + 1], &p, 0);
297 } else if (strcmp(atts[i], "type") == 0)
298 field->type = string_to_type(ctx, atts[i + 1]);
299 else if (strcmp(atts[i], "default") == 0 &&
300 field->start >= 16 && field->end <= 31) {
301 field->has_default = true;
302 field->default_value = strtoul(atts[i + 1], &p, 0);
303 }
304 }
305
306 return field;
307 }
308
309 static struct gen_value *
310 create_value(struct parser_context *ctx, const char **atts)
311 {
312 struct gen_value *value = rzalloc(ctx->values, struct gen_value);
313
314 for (int i = 0; atts[i]; i += 2) {
315 if (strcmp(atts[i], "name") == 0)
316 value->name = ralloc_strdup(value, atts[i + 1]);
317 else if (strcmp(atts[i], "value") == 0)
318 value->value = strtoul(atts[i + 1], NULL, 0);
319 }
320
321 return value;
322 }
323
324 static struct gen_field *
325 create_and_append_field(struct parser_context *ctx,
326 const char **atts)
327 {
328 struct gen_field *field = create_field(ctx, atts);
329 struct gen_field *prev = NULL, *list = ctx->group->fields;
330
331 while (list && field->start > list->start) {
332 prev = list;
333 list = list->next;
334 }
335
336 field->next = list;
337 if (prev == NULL)
338 ctx->group->fields = field;
339 else
340 prev->next = field;
341
342 return field;
343 }
344
345 static void
346 start_element(void *data, const char *element_name, const char **atts)
347 {
348 struct parser_context *ctx = data;
349 const char *name = NULL;
350 const char *gen = NULL;
351
352 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
353
354 for (int i = 0; atts[i]; i += 2) {
355 if (strcmp(atts[i], "name") == 0)
356 name = atts[i + 1];
357 else if (strcmp(atts[i], "gen") == 0)
358 gen = atts[i + 1];
359 }
360
361 if (strcmp(element_name, "genxml") == 0) {
362 if (name == NULL)
363 fail(&ctx->loc, "no platform name given");
364 if (gen == NULL)
365 fail(&ctx->loc, "no gen given");
366
367 int major, minor;
368 int n = sscanf(gen, "%d.%d", &major, &minor);
369 if (n == 0)
370 fail(&ctx->loc, "invalid gen given: %s", gen);
371 if (n == 1)
372 minor = 0;
373
374 ctx->spec->gen = gen_make_gen(major, minor);
375 } else if (strcmp(element_name, "instruction") == 0) {
376 ctx->group = create_group(ctx, name, atts, NULL, false);
377 } else if (strcmp(element_name, "struct") == 0) {
378 ctx->group = create_group(ctx, name, atts, NULL, true);
379 } else if (strcmp(element_name, "register") == 0) {
380 ctx->group = create_group(ctx, name, atts, NULL, true);
381 get_register_offset(atts, &ctx->group->register_offset);
382 } else if (strcmp(element_name, "group") == 0) {
383 struct gen_group *previous_group = ctx->group;
384 while (previous_group->next)
385 previous_group = previous_group->next;
386
387 struct gen_group *group = create_group(ctx, "", atts, ctx->group, false);
388 previous_group->next = group;
389 ctx->group = group;
390 } else if (strcmp(element_name, "field") == 0) {
391 ctx->last_field = create_and_append_field(ctx, atts);
392 } else if (strcmp(element_name, "enum") == 0) {
393 ctx->enoom = create_enum(ctx, name, atts);
394 } else if (strcmp(element_name, "value") == 0) {
395 if (ctx->n_values >= ctx->n_allocated_values) {
396 ctx->n_allocated_values = MAX2(2, ctx->n_allocated_values * 2);
397 ctx->values = reralloc_array_size(ctx->spec, ctx->values,
398 sizeof(struct gen_value *),
399 ctx->n_allocated_values);
400 }
401 assert(ctx->n_values < ctx->n_allocated_values);
402 ctx->values[ctx->n_values++] = create_value(ctx, atts);
403 }
404
405 }
406
407 static void
408 end_element(void *data, const char *name)
409 {
410 struct parser_context *ctx = data;
411 struct gen_spec *spec = ctx->spec;
412
413 if (strcmp(name, "instruction") == 0 ||
414 strcmp(name, "struct") == 0 ||
415 strcmp(name, "register") == 0) {
416 struct gen_group *group = ctx->group;
417 struct gen_field *list = group->fields;
418
419 ctx->group = ctx->group->parent;
420
421 while (list && list->end <= 31) {
422 if (list->start >= 16 && list->has_default) {
423 group->opcode_mask |=
424 mask(list->start % 32, list->end % 32);
425 group->opcode |= list->default_value << list->start;
426 }
427 list = list->next;
428 }
429
430 if (strcmp(name, "instruction") == 0)
431 _mesa_hash_table_insert(spec->commands, group->name, group);
432 else if (strcmp(name, "struct") == 0)
433 _mesa_hash_table_insert(spec->structs, group->name, group);
434 else if (strcmp(name, "register") == 0) {
435 _mesa_hash_table_insert(spec->registers_by_name, group->name, group);
436 _mesa_hash_table_insert(spec->registers_by_offset,
437 (void *) (uintptr_t) group->register_offset,
438 group);
439 }
440 } else if (strcmp(name, "group") == 0) {
441 ctx->group = ctx->group->parent;
442 } else if (strcmp(name, "field") == 0) {
443 struct gen_field *field = ctx->last_field;
444 ctx->last_field = NULL;
445 field->inline_enum.values = ctx->values;
446 field->inline_enum.nvalues = ctx->n_values;
447 ctx->values = ralloc_array(ctx->spec, struct gen_value*, ctx->n_allocated_values = 2);
448 ctx->n_values = 0;
449 } else if (strcmp(name, "enum") == 0) {
450 struct gen_enum *e = ctx->enoom;
451 e->values = ctx->values;
452 e->nvalues = ctx->n_values;
453 ctx->values = ralloc_array(ctx->spec, struct gen_value*, ctx->n_allocated_values = 2);
454 ctx->n_values = 0;
455 ctx->enoom = NULL;
456 _mesa_hash_table_insert(spec->enums, e->name, e);
457 }
458 }
459
460 static void
461 character_data(void *data, const XML_Char *s, int len)
462 {
463 }
464
465 static int
466 devinfo_to_gen(const struct gen_device_info *devinfo)
467 {
468 int value = 10 * devinfo->gen;
469
470 if (devinfo->is_baytrail || devinfo->is_haswell)
471 value += 5;
472
473 return value;
474 }
475
476 static uint32_t zlib_inflate(const void *compressed_data,
477 uint32_t compressed_len,
478 void **out_ptr)
479 {
480 struct z_stream_s zstream;
481 void *out;
482
483 memset(&zstream, 0, sizeof(zstream));
484
485 zstream.next_in = (unsigned char *)compressed_data;
486 zstream.avail_in = compressed_len;
487
488 if (inflateInit(&zstream) != Z_OK)
489 return 0;
490
491 out = malloc(4096);
492 zstream.next_out = out;
493 zstream.avail_out = 4096;
494
495 do {
496 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
497 case Z_STREAM_END:
498 goto end;
499 case Z_OK:
500 break;
501 default:
502 inflateEnd(&zstream);
503 return 0;
504 }
505
506 if (zstream.avail_out)
507 break;
508
509 out = realloc(out, 2*zstream.total_out);
510 if (out == NULL) {
511 inflateEnd(&zstream);
512 return 0;
513 }
514
515 zstream.next_out = (unsigned char *)out + zstream.total_out;
516 zstream.avail_out = zstream.total_out;
517 } while (1);
518 end:
519 inflateEnd(&zstream);
520 *out_ptr = out;
521 return zstream.total_out;
522 }
523
524 static uint32_t _hash_uint32(const void *key)
525 {
526 return (uint32_t) (uintptr_t) key;
527 }
528
529 struct gen_spec *
530 gen_spec_load(const struct gen_device_info *devinfo)
531 {
532 struct parser_context ctx;
533 void *buf;
534 uint8_t *text_data = NULL;
535 uint32_t text_offset = 0, text_length = 0;
536 MAYBE_UNUSED uint32_t total_length;
537 uint32_t gen_10 = devinfo_to_gen(devinfo);
538
539 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
540 if (genxml_files_table[i].gen_10 == gen_10) {
541 text_offset = genxml_files_table[i].offset;
542 text_length = genxml_files_table[i].length;
543 break;
544 }
545 }
546
547 if (text_length == 0) {
548 fprintf(stderr, "unable to find gen (%u) data\n", gen_10);
549 return NULL;
550 }
551
552 memset(&ctx, 0, sizeof ctx);
553 ctx.parser = XML_ParserCreate(NULL);
554 XML_SetUserData(ctx.parser, &ctx);
555 if (ctx.parser == NULL) {
556 fprintf(stderr, "failed to create parser\n");
557 return NULL;
558 }
559
560 XML_SetElementHandler(ctx.parser, start_element, end_element);
561 XML_SetCharacterDataHandler(ctx.parser, character_data);
562
563 ctx.spec = rzalloc(NULL, struct gen_spec);
564
565 ctx.spec->commands =
566 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
567 ctx.spec->structs =
568 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
569 ctx.spec->registers_by_name =
570 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
571 ctx.spec->registers_by_offset =
572 _mesa_hash_table_create(ctx.spec, _hash_uint32, _mesa_key_pointer_equal);
573 ctx.spec->enums =
574 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
575
576 ctx.spec->access_cache =
577 _mesa_hash_table_create(ctx.spec, _mesa_hash_string, _mesa_key_string_equal);
578
579 total_length = zlib_inflate(compress_genxmls,
580 sizeof(compress_genxmls),
581 (void **) &text_data);
582 assert(text_offset + text_length <= total_length);
583
584 buf = XML_GetBuffer(ctx.parser, text_length);
585 memcpy(buf, &text_data[text_offset], text_length);
586
587 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
588 fprintf(stderr,
589 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
590 XML_GetCurrentLineNumber(ctx.parser),
591 XML_GetCurrentColumnNumber(ctx.parser),
592 XML_GetCurrentByteIndex(ctx.parser), text_length,
593 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
594 XML_ParserFree(ctx.parser);
595 free(text_data);
596 return NULL;
597 }
598
599 XML_ParserFree(ctx.parser);
600 free(text_data);
601
602 return ctx.spec;
603 }
604
605 struct gen_spec *
606 gen_spec_load_from_path(const struct gen_device_info *devinfo,
607 const char *path)
608 {
609 struct parser_context ctx;
610 size_t len, filename_len = strlen(path) + 20;
611 char *filename = malloc(filename_len);
612 void *buf;
613 FILE *input;
614
615 len = snprintf(filename, filename_len, "%s/gen%i.xml",
616 path, devinfo_to_gen(devinfo));
617 assert(len < filename_len);
618
619 input = fopen(filename, "r");
620 if (input == NULL) {
621 fprintf(stderr, "failed to open xml description\n");
622 free(filename);
623 return NULL;
624 }
625
626 memset(&ctx, 0, sizeof ctx);
627 ctx.parser = XML_ParserCreate(NULL);
628 XML_SetUserData(ctx.parser, &ctx);
629 if (ctx.parser == NULL) {
630 fprintf(stderr, "failed to create parser\n");
631 fclose(input);
632 free(filename);
633 return NULL;
634 }
635
636 XML_SetElementHandler(ctx.parser, start_element, end_element);
637 XML_SetCharacterDataHandler(ctx.parser, character_data);
638 ctx.loc.filename = filename;
639 ctx.spec = rzalloc(NULL, struct gen_spec);
640
641 do {
642 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
643 len = fread(buf, 1, XML_BUFFER_SIZE, input);
644 if (len == 0) {
645 fprintf(stderr, "fread: %m\n");
646 free(ctx.spec);
647 ctx.spec = NULL;
648 goto end;
649 }
650 if (XML_ParseBuffer(ctx.parser, len, len == 0) == 0) {
651 fprintf(stderr,
652 "Error parsing XML at line %ld col %ld: %s\n",
653 XML_GetCurrentLineNumber(ctx.parser),
654 XML_GetCurrentColumnNumber(ctx.parser),
655 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
656 free(ctx.spec);
657 ctx.spec = NULL;
658 goto end;
659 }
660 } while (len > 0);
661
662 end:
663 XML_ParserFree(ctx.parser);
664
665 fclose(input);
666 free(filename);
667
668 return ctx.spec;
669 }
670
671 void gen_spec_destroy(struct gen_spec *spec)
672 {
673 ralloc_free(spec);
674 }
675
676 struct gen_group *
677 gen_spec_find_instruction(struct gen_spec *spec, const uint32_t *p)
678 {
679 struct hash_entry *entry;
680
681 hash_table_foreach(spec->commands, entry) {
682 struct gen_group *command = entry->data;
683 uint32_t opcode = *p & command->opcode_mask;
684 if (opcode == command->opcode)
685 return command;
686 }
687
688 return NULL;
689 }
690
691 struct gen_field *
692 gen_group_find_field(struct gen_group *group, const char *name)
693 {
694 char path[256];
695 snprintf(path, sizeof(path), "%s/%s", group->name, name);
696
697 struct gen_spec *spec = group->spec;
698 struct hash_entry *entry = _mesa_hash_table_search(spec->access_cache,
699 path);
700 if (entry)
701 return entry->data;
702
703 struct gen_field *field = group->fields;
704 while (field) {
705 if (strcmp(field->name, name) == 0) {
706 _mesa_hash_table_insert(spec->access_cache,
707 ralloc_strdup(spec, path),
708 field);
709 return field;
710 }
711 field = field->next;
712 }
713
714 return NULL;
715 }
716
717 int
718 gen_group_get_length(struct gen_group *group, const uint32_t *p)
719 {
720 if (group && group->fixed_length)
721 return group->dw_length;
722
723 uint32_t h = p[0];
724 uint32_t type = field_value(h, 29, 31);
725
726 switch (type) {
727 case 0: /* MI */ {
728 uint32_t opcode = field_value(h, 23, 28);
729 if (opcode < 16)
730 return 1;
731 else
732 return field_value(h, 0, 7) + 2;
733 break;
734 }
735
736 case 2: /* BLT */ {
737 return field_value(h, 0, 7) + 2;
738 }
739
740 case 3: /* Render */ {
741 uint32_t subtype = field_value(h, 27, 28);
742 uint32_t opcode = field_value(h, 24, 26);
743 uint16_t whole_opcode = field_value(h, 16, 31);
744 switch (subtype) {
745 case 0:
746 if (whole_opcode == 0x6104 /* PIPELINE_SELECT_965 */)
747 return 1;
748 else if (opcode < 2)
749 return field_value(h, 0, 7) + 2;
750 else
751 return -1;
752 case 1:
753 if (opcode < 2)
754 return 1;
755 else
756 return -1;
757 case 2: {
758 if (opcode == 0)
759 return field_value(h, 0, 7) + 2;
760 else if (opcode < 3)
761 return field_value(h, 0, 15) + 2;
762 else
763 return -1;
764 }
765 case 3:
766 if (whole_opcode == 0x780b)
767 return 1;
768 else if (opcode < 4)
769 return field_value(h, 0, 7) + 2;
770 else
771 return -1;
772 }
773 }
774 }
775
776 return -1;
777 }
778
779 static const char *
780 gen_get_enum_name(struct gen_enum *e, uint64_t value)
781 {
782 for (int i = 0; i < e->nvalues; i++) {
783 if (e->values[i]->value == value) {
784 return e->values[i]->name;
785 }
786 }
787 return NULL;
788 }
789
790 static bool
791 iter_more_fields(const struct gen_field_iterator *iter)
792 {
793 return iter->field != NULL && iter->field->next != NULL;
794 }
795
796 static uint32_t
797 iter_group_offset_bits(const struct gen_field_iterator *iter,
798 uint32_t group_iter)
799 {
800 return iter->group->group_offset + (group_iter * iter->group->group_size);
801 }
802
803 static bool
804 iter_more_groups(const struct gen_field_iterator *iter)
805 {
806 if (iter->group->variable) {
807 int length = gen_group_get_length(iter->group, iter->p);
808 assert(length >= 0 && "error the length is unknown!");
809 return iter_group_offset_bits(iter, iter->group_iter + 1) <
810 (length * 32);
811 } else {
812 return (iter->group_iter + 1) < iter->group->group_count ||
813 iter->group->next != NULL;
814 }
815 }
816
817 static void
818 iter_start_field(struct gen_field_iterator *iter, struct gen_field *field)
819 {
820 iter->field = field;
821
822 int group_member_offset = iter_group_offset_bits(iter, iter->group_iter);
823
824 iter->start_bit = group_member_offset + iter->field->start;
825 iter->end_bit = group_member_offset + iter->field->end;
826 iter->struct_desc = NULL;
827 }
828
829 static void
830 iter_advance_group(struct gen_field_iterator *iter)
831 {
832 if (iter->group->variable)
833 iter->group_iter++;
834 else {
835 if ((iter->group_iter + 1) < iter->group->group_count) {
836 iter->group_iter++;
837 } else {
838 iter->group = iter->group->next;
839 iter->group_iter = 0;
840 }
841 }
842
843 iter_start_field(iter, iter->group->fields);
844 }
845
846 static bool
847 iter_advance_field(struct gen_field_iterator *iter)
848 {
849 if (iter_more_fields(iter)) {
850 iter_start_field(iter, iter->field->next);
851 } else {
852 if (!iter_more_groups(iter))
853 return false;
854
855 iter_advance_group(iter);
856 }
857 return true;
858 }
859
860 static bool
861 iter_decode_field_raw(struct gen_field_iterator *iter, uint64_t *qw)
862 {
863 *qw = 0;
864
865 int field_start = iter->p_bit + iter->start_bit;
866 int field_end = iter->p_bit + iter->end_bit;
867
868 const uint32_t *p = iter->p + (iter->start_bit / 32);
869 if (iter->p_end && p >= iter->p_end)
870 return false;
871
872 if ((field_end - field_start) > 32) {
873 if (!iter->p_end || (p + 1) < iter->p_end)
874 *qw = ((uint64_t) p[1]) << 32;
875 *qw |= p[0];
876 } else
877 *qw = p[0];
878
879 *qw = field_value(*qw, field_start, field_end);
880
881 /* Address & offset types have to be aligned to dwords, their start bit is
882 * a reminder of the alignment requirement.
883 */
884 if (iter->field->type.kind == GEN_TYPE_ADDRESS ||
885 iter->field->type.kind == GEN_TYPE_OFFSET)
886 *qw <<= field_start % 32;
887
888 return true;
889 }
890
891 static bool
892 iter_decode_field(struct gen_field_iterator *iter)
893 {
894 union {
895 uint64_t qw;
896 float f;
897 } v;
898
899 if (iter->field->name)
900 snprintf(iter->name, sizeof(iter->name), "%s", iter->field->name);
901 else
902 memset(iter->name, 0, sizeof(iter->name));
903
904 memset(&v, 0, sizeof(v));
905
906 if (!iter_decode_field_raw(iter, &iter->raw_value))
907 return false;
908
909 const char *enum_name = NULL;
910
911 v.qw = iter->raw_value;
912 switch (iter->field->type.kind) {
913 case GEN_TYPE_UNKNOWN:
914 case GEN_TYPE_INT: {
915 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
916 enum_name = gen_get_enum_name(&iter->field->inline_enum, v.qw);
917 break;
918 }
919 case GEN_TYPE_UINT: {
920 snprintf(iter->value, sizeof(iter->value), "%"PRIu64, v.qw);
921 enum_name = gen_get_enum_name(&iter->field->inline_enum, v.qw);
922 break;
923 }
924 case GEN_TYPE_BOOL: {
925 const char *true_string =
926 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
927 snprintf(iter->value, sizeof(iter->value), "%s",
928 v.qw ? true_string : "false");
929 break;
930 }
931 case GEN_TYPE_FLOAT:
932 snprintf(iter->value, sizeof(iter->value), "%f", v.f);
933 break;
934 case GEN_TYPE_ADDRESS:
935 case GEN_TYPE_OFFSET:
936 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64, v.qw);
937 break;
938 case GEN_TYPE_STRUCT:
939 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
940 iter->field->type.gen_struct->name);
941 iter->struct_desc =
942 gen_spec_find_struct(iter->group->spec,
943 iter->field->type.gen_struct->name);
944 break;
945 case GEN_TYPE_UFIXED:
946 snprintf(iter->value, sizeof(iter->value), "%f",
947 (float) v.qw / (1 << iter->field->type.f));
948 break;
949 case GEN_TYPE_SFIXED: {
950 /* Sign extend before converting */
951 int bits = iter->field->type.i + iter->field->type.f + 1;
952 int64_t v_sign_extend = ((int64_t)(v.qw << (64 - bits))) >> (64 - bits);
953 snprintf(iter->value, sizeof(iter->value), "%f",
954 (float) v_sign_extend / (1 << iter->field->type.f));
955 break;
956 }
957 case GEN_TYPE_MBO:
958 break;
959 case GEN_TYPE_ENUM: {
960 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
961 enum_name = gen_get_enum_name(iter->field->type.gen_enum, v.qw);
962 break;
963 }
964 }
965
966 if (strlen(iter->group->name) == 0) {
967 int length = strlen(iter->name);
968 snprintf(iter->name + length, sizeof(iter->name) - length,
969 "[%i]", iter->group_iter);
970 }
971
972 if (enum_name) {
973 int length = strlen(iter->value);
974 snprintf(iter->value + length, sizeof(iter->value) - length,
975 " (%s)", enum_name);
976 } else if (strcmp(iter->name, "Surface Format") == 0 ||
977 strcmp(iter->name, "Source Element Format") == 0) {
978 if (isl_format_is_valid((enum isl_format)v.qw)) {
979 const char *fmt_name = isl_format_get_name((enum isl_format)v.qw);
980 int length = strlen(iter->value);
981 snprintf(iter->value + length, sizeof(iter->value) - length,
982 " (%s)", fmt_name);
983 }
984 }
985
986 return true;
987 }
988
989 void
990 gen_field_iterator_init(struct gen_field_iterator *iter,
991 struct gen_group *group,
992 const uint32_t *p, int p_bit,
993 bool print_colors)
994 {
995 memset(iter, 0, sizeof(*iter));
996
997 iter->group = group;
998 iter->p = p;
999 iter->p_bit = p_bit;
1000
1001 int length = gen_group_get_length(iter->group, iter->p);
1002 assert(length >= 0 && "error the length is unknown!");
1003 iter->p_end = length >= 0 ? &p[length] : NULL;
1004 iter->print_colors = print_colors;
1005 }
1006
1007 bool
1008 gen_field_iterator_next(struct gen_field_iterator *iter)
1009 {
1010 /* Initial condition */
1011 if (!iter->field) {
1012 if (iter->group->fields)
1013 iter_start_field(iter, iter->group->fields);
1014 else
1015 iter_start_field(iter, iter->group->next->fields);
1016
1017 bool result = iter_decode_field(iter);
1018 if (!result && iter->p_end) {
1019 /* We're dealing with a non empty struct of length=0 (BLEND_STATE on
1020 * Gen 7.5)
1021 */
1022 assert(iter->group->dw_length == 0);
1023 }
1024
1025 return result;
1026 }
1027
1028 if (!iter_advance_field(iter))
1029 return false;
1030
1031 if (!iter_decode_field(iter))
1032 return false;
1033
1034 return true;
1035 }
1036
1037 static void
1038 print_dword_header(FILE *outfile,
1039 struct gen_field_iterator *iter,
1040 uint64_t offset, uint32_t dword)
1041 {
1042 fprintf(outfile, "0x%08"PRIx64": 0x%08x : Dword %d\n",
1043 offset + 4 * dword, iter->p[dword], dword);
1044 }
1045
1046 bool
1047 gen_field_is_header(struct gen_field *field)
1048 {
1049 uint32_t bits;
1050
1051 if (field->start >= 32)
1052 return false;
1053
1054 bits = (1U << (field->end - field->start + 1)) - 1;
1055 bits <<= field->start;
1056
1057 return (field->parent->opcode_mask & bits) != 0;
1058 }
1059
1060 void
1061 gen_print_group(FILE *outfile, struct gen_group *group, uint64_t offset,
1062 const uint32_t *p, int p_bit, bool color)
1063 {
1064 struct gen_field_iterator iter;
1065 int last_dword = -1;
1066
1067 gen_field_iterator_init(&iter, group, p, p_bit, color);
1068 while (gen_field_iterator_next(&iter)) {
1069 int iter_dword = iter.end_bit / 32;
1070 if (last_dword != iter_dword) {
1071 for (int i = last_dword + 1; i <= iter_dword; i++)
1072 print_dword_header(outfile, &iter, offset, i);
1073 last_dword = iter_dword;
1074 }
1075 if (!gen_field_is_header(iter.field)) {
1076 fprintf(outfile, " %s: %s\n", iter.name, iter.value);
1077 if (iter.struct_desc) {
1078 int struct_dword = iter.start_bit / 32;
1079 uint64_t struct_offset = offset + 4 * struct_dword;
1080 gen_print_group(outfile, iter.struct_desc, struct_offset,
1081 &p[struct_dword], iter.start_bit % 32, color);
1082 }
1083 }
1084 }
1085 }