intel/decoder: Use 'DWord Length' and 'bias' fields for packet length.
[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 group->dword_length_field = NULL;
167 group->dw_length = 0;
168 group->bias = 1;
169
170 for (int i = 0; atts[i]; i += 2) {
171 char *p;
172 if (strcmp(atts[i], "length") == 0) {
173 group->dw_length = strtoul(atts[i + 1], &p, 0);
174 } else if (strcmp(atts[i], "bias") == 0) {
175 group->bias = strtoul(atts[i + 1], &p, 0);
176 }
177 }
178
179 if (parent) {
180 group->parent = parent;
181 get_group_offset_count(atts,
182 &group->group_offset,
183 &group->group_count,
184 &group->group_size,
185 &group->variable);
186 }
187
188 return group;
189 }
190
191 static struct gen_enum *
192 create_enum(struct parser_context *ctx, const char *name, const char **atts)
193 {
194 struct gen_enum *e;
195
196 e = rzalloc(ctx->spec, struct gen_enum);
197 if (name)
198 e->name = ralloc_strdup(e, name);
199
200 return e;
201 }
202
203 static void
204 get_register_offset(const char **atts, uint32_t *offset)
205 {
206 for (int i = 0; atts[i]; i += 2) {
207 char *p;
208
209 if (strcmp(atts[i], "num") == 0)
210 *offset = strtoul(atts[i + 1], &p, 0);
211 }
212 return;
213 }
214
215 static void
216 get_start_end_pos(int *start, int *end)
217 {
218 /* start value has to be mod with 32 as we need the relative
219 * start position in the first DWord. For the end position, add
220 * the length of the field to the start position to get the
221 * relative postion in the 64 bit address.
222 */
223 if (*end - *start > 32) {
224 int len = *end - *start;
225 *start = *start % 32;
226 *end = *start + len;
227 } else {
228 *start = *start % 32;
229 *end = *end % 32;
230 }
231
232 return;
233 }
234
235 static inline uint64_t
236 mask(int start, int end)
237 {
238 uint64_t v;
239
240 v = ~0ULL >> (63 - end + start);
241
242 return v << start;
243 }
244
245 static inline uint64_t
246 field_value(uint64_t value, int start, int end)
247 {
248 get_start_end_pos(&start, &end);
249 return (value & mask(start, end)) >> (start);
250 }
251
252 static struct gen_type
253 string_to_type(struct parser_context *ctx, const char *s)
254 {
255 int i, f;
256 struct gen_group *g;
257 struct gen_enum *e;
258
259 if (strcmp(s, "int") == 0)
260 return (struct gen_type) { .kind = GEN_TYPE_INT };
261 else if (strcmp(s, "uint") == 0)
262 return (struct gen_type) { .kind = GEN_TYPE_UINT };
263 else if (strcmp(s, "bool") == 0)
264 return (struct gen_type) { .kind = GEN_TYPE_BOOL };
265 else if (strcmp(s, "float") == 0)
266 return (struct gen_type) { .kind = GEN_TYPE_FLOAT };
267 else if (strcmp(s, "address") == 0)
268 return (struct gen_type) { .kind = GEN_TYPE_ADDRESS };
269 else if (strcmp(s, "offset") == 0)
270 return (struct gen_type) { .kind = GEN_TYPE_OFFSET };
271 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
272 return (struct gen_type) { .kind = GEN_TYPE_UFIXED, .i = i, .f = f };
273 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
274 return (struct gen_type) { .kind = GEN_TYPE_SFIXED, .i = i, .f = f };
275 else if (g = gen_spec_find_struct(ctx->spec, s), g != NULL)
276 return (struct gen_type) { .kind = GEN_TYPE_STRUCT, .gen_struct = g };
277 else if (e = gen_spec_find_enum(ctx->spec, s), e != NULL)
278 return (struct gen_type) { .kind = GEN_TYPE_ENUM, .gen_enum = e };
279 else if (strcmp(s, "mbo") == 0)
280 return (struct gen_type) { .kind = GEN_TYPE_MBO };
281 else
282 fail(&ctx->loc, "invalid type: %s", s);
283 }
284
285 static struct gen_field *
286 create_field(struct parser_context *ctx, const char **atts)
287 {
288 struct gen_field *field;
289
290 field = rzalloc(ctx->group, struct gen_field);
291 field->parent = ctx->group;
292
293 for (int i = 0; atts[i]; i += 2) {
294 char *p;
295
296 if (strcmp(atts[i], "name") == 0) {
297 field->name = ralloc_strdup(field, atts[i + 1]);
298 if (strcmp(field->name, "DWord Length") == 0) {
299 field->parent->dword_length_field = field;
300 }
301 } else if (strcmp(atts[i], "start") == 0) {
302 field->start = strtoul(atts[i + 1], &p, 0);
303 } else if (strcmp(atts[i], "end") == 0) {
304 field->end = strtoul(atts[i + 1], &p, 0);
305 } else if (strcmp(atts[i], "type") == 0) {
306 field->type = string_to_type(ctx, atts[i + 1]);
307 } else if (strcmp(atts[i], "default") == 0 &&
308 field->start >= 16 && field->end <= 31) {
309 field->has_default = true;
310 field->default_value = strtoul(atts[i + 1], &p, 0);
311 }
312 }
313
314 return field;
315 }
316
317 static struct gen_value *
318 create_value(struct parser_context *ctx, const char **atts)
319 {
320 struct gen_value *value = rzalloc(ctx->values, struct gen_value);
321
322 for (int i = 0; atts[i]; i += 2) {
323 if (strcmp(atts[i], "name") == 0)
324 value->name = ralloc_strdup(value, atts[i + 1]);
325 else if (strcmp(atts[i], "value") == 0)
326 value->value = strtoul(atts[i + 1], NULL, 0);
327 }
328
329 return value;
330 }
331
332 static struct gen_field *
333 create_and_append_field(struct parser_context *ctx,
334 const char **atts)
335 {
336 struct gen_field *field = create_field(ctx, atts);
337 struct gen_field *prev = NULL, *list = ctx->group->fields;
338
339 while (list && field->start > list->start) {
340 prev = list;
341 list = list->next;
342 }
343
344 field->next = list;
345 if (prev == NULL)
346 ctx->group->fields = field;
347 else
348 prev->next = field;
349
350 return field;
351 }
352
353 static void
354 start_element(void *data, const char *element_name, const char **atts)
355 {
356 struct parser_context *ctx = data;
357 const char *name = NULL;
358 const char *gen = NULL;
359
360 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
361
362 for (int i = 0; atts[i]; i += 2) {
363 if (strcmp(atts[i], "name") == 0)
364 name = atts[i + 1];
365 else if (strcmp(atts[i], "gen") == 0)
366 gen = atts[i + 1];
367 }
368
369 if (strcmp(element_name, "genxml") == 0) {
370 if (name == NULL)
371 fail(&ctx->loc, "no platform name given");
372 if (gen == NULL)
373 fail(&ctx->loc, "no gen given");
374
375 int major, minor;
376 int n = sscanf(gen, "%d.%d", &major, &minor);
377 if (n == 0)
378 fail(&ctx->loc, "invalid gen given: %s", gen);
379 if (n == 1)
380 minor = 0;
381
382 ctx->spec->gen = gen_make_gen(major, minor);
383 } else if (strcmp(element_name, "instruction") == 0) {
384 ctx->group = create_group(ctx, name, atts, NULL, false);
385 } else if (strcmp(element_name, "struct") == 0) {
386 ctx->group = create_group(ctx, name, atts, NULL, true);
387 } else if (strcmp(element_name, "register") == 0) {
388 ctx->group = create_group(ctx, name, atts, NULL, true);
389 get_register_offset(atts, &ctx->group->register_offset);
390 } else if (strcmp(element_name, "group") == 0) {
391 struct gen_group *previous_group = ctx->group;
392 while (previous_group->next)
393 previous_group = previous_group->next;
394
395 struct gen_group *group = create_group(ctx, "", atts, ctx->group, false);
396 previous_group->next = group;
397 ctx->group = group;
398 } else if (strcmp(element_name, "field") == 0) {
399 ctx->last_field = create_and_append_field(ctx, atts);
400 } else if (strcmp(element_name, "enum") == 0) {
401 ctx->enoom = create_enum(ctx, name, atts);
402 } else if (strcmp(element_name, "value") == 0) {
403 if (ctx->n_values >= ctx->n_allocated_values) {
404 ctx->n_allocated_values = MAX2(2, ctx->n_allocated_values * 2);
405 ctx->values = reralloc_array_size(ctx->spec, ctx->values,
406 sizeof(struct gen_value *),
407 ctx->n_allocated_values);
408 }
409 assert(ctx->n_values < ctx->n_allocated_values);
410 ctx->values[ctx->n_values++] = create_value(ctx, atts);
411 }
412
413 }
414
415 static void
416 end_element(void *data, const char *name)
417 {
418 struct parser_context *ctx = data;
419 struct gen_spec *spec = ctx->spec;
420
421 if (strcmp(name, "instruction") == 0 ||
422 strcmp(name, "struct") == 0 ||
423 strcmp(name, "register") == 0) {
424 struct gen_group *group = ctx->group;
425 struct gen_field *list = group->fields;
426
427 ctx->group = ctx->group->parent;
428
429 while (list && list->end <= 31) {
430 if (list->start >= 16 && list->has_default) {
431 group->opcode_mask |=
432 mask(list->start % 32, list->end % 32);
433 group->opcode |= list->default_value << list->start;
434 }
435 list = list->next;
436 }
437
438 if (strcmp(name, "instruction") == 0)
439 _mesa_hash_table_insert(spec->commands, group->name, group);
440 else if (strcmp(name, "struct") == 0)
441 _mesa_hash_table_insert(spec->structs, group->name, group);
442 else if (strcmp(name, "register") == 0) {
443 _mesa_hash_table_insert(spec->registers_by_name, group->name, group);
444 _mesa_hash_table_insert(spec->registers_by_offset,
445 (void *) (uintptr_t) group->register_offset,
446 group);
447 }
448 } else if (strcmp(name, "group") == 0) {
449 ctx->group = ctx->group->parent;
450 } else if (strcmp(name, "field") == 0) {
451 struct gen_field *field = ctx->last_field;
452 ctx->last_field = NULL;
453 field->inline_enum.values = ctx->values;
454 field->inline_enum.nvalues = ctx->n_values;
455 ctx->values = ralloc_array(ctx->spec, struct gen_value*, ctx->n_allocated_values = 2);
456 ctx->n_values = 0;
457 } else if (strcmp(name, "enum") == 0) {
458 struct gen_enum *e = ctx->enoom;
459 e->values = ctx->values;
460 e->nvalues = ctx->n_values;
461 ctx->values = ralloc_array(ctx->spec, struct gen_value*, ctx->n_allocated_values = 2);
462 ctx->n_values = 0;
463 ctx->enoom = NULL;
464 _mesa_hash_table_insert(spec->enums, e->name, e);
465 }
466 }
467
468 static void
469 character_data(void *data, const XML_Char *s, int len)
470 {
471 }
472
473 static int
474 devinfo_to_gen(const struct gen_device_info *devinfo, bool x10)
475 {
476 if (devinfo->is_baytrail || devinfo->is_haswell) {
477 return devinfo->gen * 10 + 5;
478 }
479
480 return x10 ? devinfo->gen * 10 : devinfo->gen;
481 }
482
483 static uint32_t zlib_inflate(const void *compressed_data,
484 uint32_t compressed_len,
485 void **out_ptr)
486 {
487 struct z_stream_s zstream;
488 void *out;
489
490 memset(&zstream, 0, sizeof(zstream));
491
492 zstream.next_in = (unsigned char *)compressed_data;
493 zstream.avail_in = compressed_len;
494
495 if (inflateInit(&zstream) != Z_OK)
496 return 0;
497
498 out = malloc(4096);
499 zstream.next_out = out;
500 zstream.avail_out = 4096;
501
502 do {
503 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
504 case Z_STREAM_END:
505 goto end;
506 case Z_OK:
507 break;
508 default:
509 inflateEnd(&zstream);
510 return 0;
511 }
512
513 if (zstream.avail_out)
514 break;
515
516 out = realloc(out, 2*zstream.total_out);
517 if (out == NULL) {
518 inflateEnd(&zstream);
519 return 0;
520 }
521
522 zstream.next_out = (unsigned char *)out + zstream.total_out;
523 zstream.avail_out = zstream.total_out;
524 } while (1);
525 end:
526 inflateEnd(&zstream);
527 *out_ptr = out;
528 return zstream.total_out;
529 }
530
531 static uint32_t _hash_uint32(const void *key)
532 {
533 return (uint32_t) (uintptr_t) key;
534 }
535
536 static struct gen_spec *
537 gen_spec_init(void)
538 {
539 struct gen_spec *spec;
540 spec = rzalloc(NULL, struct gen_spec);
541 if (spec == NULL)
542 return NULL;
543
544 spec->commands =
545 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
546 spec->structs =
547 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
548 spec->registers_by_name =
549 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
550 spec->registers_by_offset =
551 _mesa_hash_table_create(spec, _hash_uint32, _mesa_key_pointer_equal);
552 spec->enums =
553 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
554 spec->access_cache =
555 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
556
557 return spec;
558 }
559
560 struct gen_spec *
561 gen_spec_load(const struct gen_device_info *devinfo)
562 {
563 struct parser_context ctx;
564 void *buf;
565 uint8_t *text_data = NULL;
566 uint32_t text_offset = 0, text_length = 0;
567 MAYBE_UNUSED uint32_t total_length;
568 uint32_t gen_10 = devinfo_to_gen(devinfo, true);
569
570 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
571 if (genxml_files_table[i].gen_10 == gen_10) {
572 text_offset = genxml_files_table[i].offset;
573 text_length = genxml_files_table[i].length;
574 break;
575 }
576 }
577
578 if (text_length == 0) {
579 fprintf(stderr, "unable to find gen (%u) data\n", gen_10);
580 return NULL;
581 }
582
583 memset(&ctx, 0, sizeof ctx);
584 ctx.parser = XML_ParserCreate(NULL);
585 XML_SetUserData(ctx.parser, &ctx);
586 if (ctx.parser == NULL) {
587 fprintf(stderr, "failed to create parser\n");
588 return NULL;
589 }
590
591 XML_SetElementHandler(ctx.parser, start_element, end_element);
592 XML_SetCharacterDataHandler(ctx.parser, character_data);
593
594 ctx.spec = gen_spec_init();
595 if (ctx.spec == NULL) {
596 fprintf(stderr, "Failed to create gen_spec\n");
597 return NULL;
598 }
599
600 total_length = zlib_inflate(compress_genxmls,
601 sizeof(compress_genxmls),
602 (void **) &text_data);
603 assert(text_offset + text_length <= total_length);
604
605 buf = XML_GetBuffer(ctx.parser, text_length);
606 memcpy(buf, &text_data[text_offset], text_length);
607
608 if (XML_ParseBuffer(ctx.parser, text_length, true) == 0) {
609 fprintf(stderr,
610 "Error parsing XML at line %ld col %ld byte %ld/%u: %s\n",
611 XML_GetCurrentLineNumber(ctx.parser),
612 XML_GetCurrentColumnNumber(ctx.parser),
613 XML_GetCurrentByteIndex(ctx.parser), text_length,
614 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
615 XML_ParserFree(ctx.parser);
616 free(text_data);
617 return NULL;
618 }
619
620 XML_ParserFree(ctx.parser);
621 free(text_data);
622
623 return ctx.spec;
624 }
625
626 struct gen_spec *
627 gen_spec_load_from_path(const struct gen_device_info *devinfo,
628 const char *path)
629 {
630 struct parser_context ctx;
631 size_t len, filename_len = strlen(path) + 20;
632 char *filename = malloc(filename_len);
633 void *buf;
634 FILE *input;
635
636 len = snprintf(filename, filename_len, "%s/gen%i.xml",
637 path, devinfo_to_gen(devinfo, false));
638 assert(len < filename_len);
639
640 input = fopen(filename, "r");
641 if (input == NULL) {
642 fprintf(stderr, "failed to open xml description\n");
643 free(filename);
644 return NULL;
645 }
646
647 memset(&ctx, 0, sizeof ctx);
648 ctx.parser = XML_ParserCreate(NULL);
649 XML_SetUserData(ctx.parser, &ctx);
650 if (ctx.parser == NULL) {
651 fprintf(stderr, "failed to create parser\n");
652 fclose(input);
653 free(filename);
654 return NULL;
655 }
656
657 XML_SetElementHandler(ctx.parser, start_element, end_element);
658 XML_SetCharacterDataHandler(ctx.parser, character_data);
659 ctx.loc.filename = filename;
660
661 ctx.spec = gen_spec_init();
662 if (ctx.spec == NULL) {
663 fprintf(stderr, "Failed to create gen_spec\n");
664 goto end;
665 }
666
667 do {
668 buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
669 len = fread(buf, 1, XML_BUFFER_SIZE, input);
670 if (ferror(input)) {
671 fprintf(stderr, "fread: %m\n");
672 gen_spec_destroy(ctx.spec);
673 ctx.spec = NULL;
674 goto end;
675 } else if (feof(input))
676 goto end;
677
678 if (XML_ParseBuffer(ctx.parser, len, len == 0) == 0) {
679 fprintf(stderr,
680 "Error parsing XML at line %ld col %ld: %s\n",
681 XML_GetCurrentLineNumber(ctx.parser),
682 XML_GetCurrentColumnNumber(ctx.parser),
683 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
684 gen_spec_destroy(ctx.spec);
685 ctx.spec = NULL;
686 goto end;
687 }
688 } while (len > 0);
689
690 end:
691 XML_ParserFree(ctx.parser);
692
693 fclose(input);
694 free(filename);
695
696 /* free ctx.spec if genxml is empty */
697 if (ctx.spec && _mesa_hash_table_num_entries(ctx.spec->commands) == 0) {
698 gen_spec_destroy(ctx.spec);
699 return NULL;
700 }
701
702 return ctx.spec;
703 }
704
705 void gen_spec_destroy(struct gen_spec *spec)
706 {
707 ralloc_free(spec);
708 }
709
710 struct gen_group *
711 gen_spec_find_instruction(struct gen_spec *spec, const uint32_t *p)
712 {
713 hash_table_foreach(spec->commands, entry) {
714 struct gen_group *command = entry->data;
715 uint32_t opcode = *p & command->opcode_mask;
716 if (opcode == command->opcode)
717 return command;
718 }
719
720 return NULL;
721 }
722
723 struct gen_field *
724 gen_group_find_field(struct gen_group *group, const char *name)
725 {
726 char path[256];
727 snprintf(path, sizeof(path), "%s/%s", group->name, name);
728
729 struct gen_spec *spec = group->spec;
730 struct hash_entry *entry = _mesa_hash_table_search(spec->access_cache,
731 path);
732 if (entry)
733 return entry->data;
734
735 struct gen_field *field = group->fields;
736 while (field) {
737 if (strcmp(field->name, name) == 0) {
738 _mesa_hash_table_insert(spec->access_cache,
739 ralloc_strdup(spec, path),
740 field);
741 return field;
742 }
743 field = field->next;
744 }
745
746 return NULL;
747 }
748
749 int
750 gen_group_get_length(struct gen_group *group, const uint32_t *p)
751 {
752 if (group) {
753 if (group->fixed_length)
754 return group->dw_length;
755 else {
756 struct gen_field *field = group->dword_length_field;
757 if (field) {
758 return field_value(p[0], field->start, field->end) + group->bias;
759 }
760 }
761 }
762
763 uint32_t h = p[0];
764 uint32_t type = field_value(h, 29, 31);
765
766 switch (type) {
767 case 0: /* MI */ {
768 uint32_t opcode = field_value(h, 23, 28);
769 if (opcode < 16)
770 return 1;
771 else
772 return field_value(h, 0, 7) + 2;
773 break;
774 }
775
776 case 2: /* BLT */ {
777 return field_value(h, 0, 7) + 2;
778 }
779
780 case 3: /* Render */ {
781 uint32_t subtype = field_value(h, 27, 28);
782 uint32_t opcode = field_value(h, 24, 26);
783 uint16_t whole_opcode = field_value(h, 16, 31);
784 switch (subtype) {
785 case 0:
786 if (whole_opcode == 0x6104 /* PIPELINE_SELECT_965 */)
787 return 1;
788 else if (opcode < 2)
789 return field_value(h, 0, 7) + 2;
790 else
791 return -1;
792 case 1:
793 if (opcode < 2)
794 return 1;
795 else
796 return -1;
797 case 2: {
798 if (opcode == 0)
799 return field_value(h, 0, 7) + 2;
800 else if (opcode < 3)
801 return field_value(h, 0, 15) + 2;
802 else
803 return -1;
804 }
805 case 3:
806 if (whole_opcode == 0x780b)
807 return 1;
808 else if (opcode < 4)
809 return field_value(h, 0, 7) + 2;
810 else
811 return -1;
812 }
813 }
814 }
815
816 return -1;
817 }
818
819 static const char *
820 gen_get_enum_name(struct gen_enum *e, uint64_t value)
821 {
822 for (int i = 0; i < e->nvalues; i++) {
823 if (e->values[i]->value == value) {
824 return e->values[i]->name;
825 }
826 }
827 return NULL;
828 }
829
830 static bool
831 iter_more_fields(const struct gen_field_iterator *iter)
832 {
833 return iter->field != NULL && iter->field->next != NULL;
834 }
835
836 static uint32_t
837 iter_group_offset_bits(const struct gen_field_iterator *iter,
838 uint32_t group_iter)
839 {
840 return iter->group->group_offset + (group_iter * iter->group->group_size);
841 }
842
843 static bool
844 iter_more_groups(const struct gen_field_iterator *iter)
845 {
846 if (iter->group->variable) {
847 int length = gen_group_get_length(iter->group, iter->p);
848 assert(length >= 0 && "error the length is unknown!");
849 return iter_group_offset_bits(iter, iter->group_iter + 1) <
850 (length * 32);
851 } else {
852 return (iter->group_iter + 1) < iter->group->group_count ||
853 iter->group->next != NULL;
854 }
855 }
856
857 static void
858 iter_start_field(struct gen_field_iterator *iter, struct gen_field *field)
859 {
860 iter->field = field;
861
862 int group_member_offset = iter_group_offset_bits(iter, iter->group_iter);
863
864 iter->start_bit = group_member_offset + iter->field->start;
865 iter->end_bit = group_member_offset + iter->field->end;
866 iter->struct_desc = NULL;
867 }
868
869 static void
870 iter_advance_group(struct gen_field_iterator *iter)
871 {
872 if (iter->group->variable)
873 iter->group_iter++;
874 else {
875 if ((iter->group_iter + 1) < iter->group->group_count) {
876 iter->group_iter++;
877 } else {
878 iter->group = iter->group->next;
879 iter->group_iter = 0;
880 }
881 }
882
883 iter_start_field(iter, iter->group->fields);
884 }
885
886 static bool
887 iter_advance_field(struct gen_field_iterator *iter)
888 {
889 if (iter_more_fields(iter)) {
890 iter_start_field(iter, iter->field->next);
891 } else {
892 if (!iter_more_groups(iter))
893 return false;
894
895 iter_advance_group(iter);
896 }
897 return true;
898 }
899
900 static bool
901 iter_decode_field_raw(struct gen_field_iterator *iter, uint64_t *qw)
902 {
903 *qw = 0;
904
905 int field_start = iter->p_bit + iter->start_bit;
906 int field_end = iter->p_bit + iter->end_bit;
907
908 const uint32_t *p = iter->p + (iter->start_bit / 32);
909 if (iter->p_end && p >= iter->p_end)
910 return false;
911
912 if ((field_end - field_start) > 32) {
913 if (!iter->p_end || (p + 1) < iter->p_end)
914 *qw = ((uint64_t) p[1]) << 32;
915 *qw |= p[0];
916 } else
917 *qw = p[0];
918
919 *qw = field_value(*qw, field_start, field_end);
920
921 /* Address & offset types have to be aligned to dwords, their start bit is
922 * a reminder of the alignment requirement.
923 */
924 if (iter->field->type.kind == GEN_TYPE_ADDRESS ||
925 iter->field->type.kind == GEN_TYPE_OFFSET)
926 *qw <<= field_start % 32;
927
928 return true;
929 }
930
931 static bool
932 iter_decode_field(struct gen_field_iterator *iter)
933 {
934 union {
935 uint64_t qw;
936 float f;
937 } v;
938
939 if (iter->field->name)
940 snprintf(iter->name, sizeof(iter->name), "%s", iter->field->name);
941 else
942 memset(iter->name, 0, sizeof(iter->name));
943
944 memset(&v, 0, sizeof(v));
945
946 if (!iter_decode_field_raw(iter, &iter->raw_value))
947 return false;
948
949 const char *enum_name = NULL;
950
951 v.qw = iter->raw_value;
952 switch (iter->field->type.kind) {
953 case GEN_TYPE_UNKNOWN:
954 case GEN_TYPE_INT: {
955 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
956 enum_name = gen_get_enum_name(&iter->field->inline_enum, v.qw);
957 break;
958 }
959 case GEN_TYPE_UINT: {
960 snprintf(iter->value, sizeof(iter->value), "%"PRIu64, v.qw);
961 enum_name = gen_get_enum_name(&iter->field->inline_enum, v.qw);
962 break;
963 }
964 case GEN_TYPE_BOOL: {
965 const char *true_string =
966 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
967 snprintf(iter->value, sizeof(iter->value), "%s",
968 v.qw ? true_string : "false");
969 break;
970 }
971 case GEN_TYPE_FLOAT:
972 snprintf(iter->value, sizeof(iter->value), "%f", v.f);
973 break;
974 case GEN_TYPE_ADDRESS:
975 case GEN_TYPE_OFFSET:
976 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64, v.qw);
977 break;
978 case GEN_TYPE_STRUCT:
979 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
980 iter->field->type.gen_struct->name);
981 iter->struct_desc =
982 gen_spec_find_struct(iter->group->spec,
983 iter->field->type.gen_struct->name);
984 break;
985 case GEN_TYPE_UFIXED:
986 snprintf(iter->value, sizeof(iter->value), "%f",
987 (float) v.qw / (1 << iter->field->type.f));
988 break;
989 case GEN_TYPE_SFIXED: {
990 /* Sign extend before converting */
991 int bits = iter->field->type.i + iter->field->type.f + 1;
992 int64_t v_sign_extend = ((int64_t)(v.qw << (64 - bits))) >> (64 - bits);
993 snprintf(iter->value, sizeof(iter->value), "%f",
994 (float) v_sign_extend / (1 << iter->field->type.f));
995 break;
996 }
997 case GEN_TYPE_MBO:
998 break;
999 case GEN_TYPE_ENUM: {
1000 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
1001 enum_name = gen_get_enum_name(iter->field->type.gen_enum, v.qw);
1002 break;
1003 }
1004 }
1005
1006 if (strlen(iter->group->name) == 0) {
1007 int length = strlen(iter->name);
1008 snprintf(iter->name + length, sizeof(iter->name) - length,
1009 "[%i]", iter->group_iter);
1010 }
1011
1012 if (enum_name) {
1013 int length = strlen(iter->value);
1014 snprintf(iter->value + length, sizeof(iter->value) - length,
1015 " (%s)", enum_name);
1016 } else if (strcmp(iter->name, "Surface Format") == 0 ||
1017 strcmp(iter->name, "Source Element Format") == 0) {
1018 if (isl_format_is_valid((enum isl_format)v.qw)) {
1019 const char *fmt_name = isl_format_get_name((enum isl_format)v.qw);
1020 int length = strlen(iter->value);
1021 snprintf(iter->value + length, sizeof(iter->value) - length,
1022 " (%s)", fmt_name);
1023 }
1024 }
1025
1026 return true;
1027 }
1028
1029 void
1030 gen_field_iterator_init(struct gen_field_iterator *iter,
1031 struct gen_group *group,
1032 const uint32_t *p, int p_bit,
1033 bool print_colors)
1034 {
1035 memset(iter, 0, sizeof(*iter));
1036
1037 iter->group = group;
1038 iter->p = p;
1039 iter->p_bit = p_bit;
1040
1041 int length = gen_group_get_length(iter->group, iter->p);
1042 assert(length >= 0 && "error the length is unknown!");
1043 iter->p_end = length >= 0 ? &p[length] : NULL;
1044 iter->print_colors = print_colors;
1045 }
1046
1047 bool
1048 gen_field_iterator_next(struct gen_field_iterator *iter)
1049 {
1050 /* Initial condition */
1051 if (!iter->field) {
1052 if (iter->group->fields)
1053 iter_start_field(iter, iter->group->fields);
1054 else
1055 iter_start_field(iter, iter->group->next->fields);
1056
1057 bool result = iter_decode_field(iter);
1058 if (!result && iter->p_end) {
1059 /* We're dealing with a non empty struct of length=0 (BLEND_STATE on
1060 * Gen 7.5)
1061 */
1062 assert(iter->group->dw_length == 0);
1063 }
1064
1065 return result;
1066 }
1067
1068 if (!iter_advance_field(iter))
1069 return false;
1070
1071 if (!iter_decode_field(iter))
1072 return false;
1073
1074 return true;
1075 }
1076
1077 static void
1078 print_dword_header(FILE *outfile,
1079 struct gen_field_iterator *iter,
1080 uint64_t offset, uint32_t dword)
1081 {
1082 fprintf(outfile, "0x%08"PRIx64": 0x%08x : Dword %d\n",
1083 offset + 4 * dword, iter->p[dword], dword);
1084 }
1085
1086 bool
1087 gen_field_is_header(struct gen_field *field)
1088 {
1089 uint32_t bits;
1090
1091 if (field->start >= 32)
1092 return false;
1093
1094 bits = (1U << (field->end - field->start + 1)) - 1;
1095 bits <<= field->start;
1096
1097 return (field->parent->opcode_mask & bits) != 0;
1098 }
1099
1100 void
1101 gen_print_group(FILE *outfile, struct gen_group *group, uint64_t offset,
1102 const uint32_t *p, int p_bit, bool color)
1103 {
1104 struct gen_field_iterator iter;
1105 int last_dword = -1;
1106
1107 gen_field_iterator_init(&iter, group, p, p_bit, color);
1108 while (gen_field_iterator_next(&iter)) {
1109 int iter_dword = iter.end_bit / 32;
1110 if (last_dword != iter_dword) {
1111 for (int i = last_dword + 1; i <= iter_dword; i++)
1112 print_dword_header(outfile, &iter, offset, i);
1113 last_dword = iter_dword;
1114 }
1115 if (!gen_field_is_header(iter.field)) {
1116 fprintf(outfile, " %s: %s\n", iter.name, iter.value);
1117 if (iter.struct_desc) {
1118 int struct_dword = iter.start_bit / 32;
1119 uint64_t struct_offset = offset + 4 * struct_dword;
1120 gen_print_group(outfile, iter.struct_desc, struct_offset,
1121 &p[struct_dword], iter.start_bit % 32, color);
1122 }
1123 }
1124 }
1125 }