2 * Copyright © 2016 Intel Corporation
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:
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
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
33 #include <util/macros.h>
34 #include <util/ralloc.h>
36 #include "gen_decoder.h"
39 #include "genxml/genX_xml.h"
41 #define XML_BUFFER_SIZE 4096
42 #define MAX_VALUE_ITEMS 128
49 struct parser_context
{
54 struct gen_group
*group
;
55 struct gen_enum
*enoom
;
57 int n_values
, n_allocated_values
;
58 struct gen_value
**values
;
60 struct gen_field
*last_field
;
62 struct gen_spec
*spec
;
66 gen_group_get_name(struct gen_group
*group
)
72 gen_group_get_opcode(struct gen_group
*group
)
78 gen_spec_find_struct(struct gen_spec
*spec
, const char *name
)
80 struct hash_entry
*entry
= _mesa_hash_table_search(spec
->structs
,
82 return entry
? entry
->data
: NULL
;
86 gen_spec_find_register(struct gen_spec
*spec
, uint32_t offset
)
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
;
95 gen_spec_find_register_by_name(struct gen_spec
*spec
, const char *name
)
97 struct hash_entry
*entry
=
98 _mesa_hash_table_search(spec
->registers_by_name
, name
);
99 return entry
? entry
->data
: NULL
;
103 gen_spec_find_enum(struct gen_spec
*spec
, const char *name
)
105 struct hash_entry
*entry
= _mesa_hash_table_search(spec
->enums
,
107 return entry
? entry
->data
: NULL
;
111 gen_spec_get_gen(struct gen_spec
*spec
)
116 static void __attribute__((noreturn
))
117 fail(struct location
*loc
, const char *msg
, ...)
122 fprintf(stderr
, "%s:%d: error: ",
123 loc
->filename
, loc
->line_number
);
124 vfprintf(stderr
, msg
, ap
);
125 fprintf(stderr
, "\n");
131 get_group_offset_count(const char **atts
, uint32_t *offset
, uint32_t *count
,
132 uint32_t *size
, bool *variable
)
134 for (int i
= 0; atts
[i
]; i
+= 2) {
137 if (strcmp(atts
[i
], "count") == 0) {
138 *count
= strtoul(atts
[i
+ 1], &p
, 0);
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);
150 static struct gen_group
*
151 create_group(struct parser_context
*ctx
,
154 struct gen_group
*parent
,
157 struct gen_group
*group
;
159 group
= rzalloc(ctx
->spec
, struct gen_group
);
161 group
->name
= ralloc_strdup(group
, name
);
163 group
->spec
= ctx
->spec
;
164 group
->variable
= false;
165 group
->fixed_length
= fixed_length
;
167 for (int i
= 0; atts
[i
]; i
+= 2) {
169 if (strcmp(atts
[i
], "length") == 0) {
170 group
->dw_length
= strtoul(atts
[i
+ 1], &p
, 0);
175 group
->parent
= parent
;
176 get_group_offset_count(atts
,
177 &group
->group_offset
,
186 static struct gen_enum
*
187 create_enum(struct parser_context
*ctx
, const char *name
, const char **atts
)
191 e
= rzalloc(ctx
->spec
, struct gen_enum
);
193 e
->name
= ralloc_strdup(e
, name
);
199 get_register_offset(const char **atts
, uint32_t *offset
)
201 for (int i
= 0; atts
[i
]; i
+= 2) {
204 if (strcmp(atts
[i
], "num") == 0)
205 *offset
= strtoul(atts
[i
+ 1], &p
, 0);
211 get_start_end_pos(int *start
, int *end
)
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.
218 if (*end
- *start
> 32) {
219 int len
= *end
- *start
;
220 *start
= *start
% 32;
223 *start
= *start
% 32;
230 static inline uint64_t
231 mask(int start
, int end
)
235 v
= ~0ULL >> (63 - end
+ start
);
240 static inline uint64_t
241 field_value(uint64_t value
, int start
, int end
)
243 get_start_end_pos(&start
, &end
);
244 return (value
& mask(start
, end
)) >> (start
);
247 static struct gen_type
248 string_to_type(struct parser_context
*ctx
, const char *s
)
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
};
277 fail(&ctx
->loc
, "invalid type: %s", s
);
280 static struct gen_field
*
281 create_field(struct parser_context
*ctx
, const char **atts
)
283 struct gen_field
*field
;
285 field
= rzalloc(ctx
->group
, struct gen_field
);
286 field
->parent
= ctx
->group
;
288 for (int i
= 0; atts
[i
]; i
+= 2) {
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);
309 static struct gen_value
*
310 create_value(struct parser_context
*ctx
, const char **atts
)
312 struct gen_value
*value
= rzalloc(ctx
->values
, struct gen_value
);
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);
324 static struct gen_field
*
325 create_and_append_field(struct parser_context
*ctx
,
328 struct gen_field
*field
= create_field(ctx
, atts
);
329 struct gen_field
*prev
= NULL
, *list
= ctx
->group
->fields
;
331 while (list
&& field
->start
> list
->start
) {
338 ctx
->group
->fields
= field
;
346 start_element(void *data
, const char *element_name
, const char **atts
)
348 struct parser_context
*ctx
= data
;
349 const char *name
= NULL
;
350 const char *gen
= NULL
;
352 ctx
->loc
.line_number
= XML_GetCurrentLineNumber(ctx
->parser
);
354 for (int i
= 0; atts
[i
]; i
+= 2) {
355 if (strcmp(atts
[i
], "name") == 0)
357 else if (strcmp(atts
[i
], "gen") == 0)
361 if (strcmp(element_name
, "genxml") == 0) {
363 fail(&ctx
->loc
, "no platform name given");
365 fail(&ctx
->loc
, "no gen given");
368 int n
= sscanf(gen
, "%d.%d", &major
, &minor
);
370 fail(&ctx
->loc
, "invalid gen given: %s", gen
);
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
;
387 struct gen_group
*group
= create_group(ctx
, "", atts
, ctx
->group
, false);
388 previous_group
->next
= 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
);
401 assert(ctx
->n_values
< ctx
->n_allocated_values
);
402 ctx
->values
[ctx
->n_values
++] = create_value(ctx
, atts
);
408 end_element(void *data
, const char *name
)
410 struct parser_context
*ctx
= data
;
411 struct gen_spec
*spec
= ctx
->spec
;
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
;
419 ctx
->group
= ctx
->group
->parent
;
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
;
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
,
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);
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);
456 _mesa_hash_table_insert(spec
->enums
, e
->name
, e
);
461 character_data(void *data
, const XML_Char
*s
, int len
)
466 devinfo_to_gen(const struct gen_device_info
*devinfo
)
468 int value
= 10 * devinfo
->gen
;
470 if (devinfo
->is_baytrail
|| devinfo
->is_haswell
)
476 static uint32_t zlib_inflate(const void *compressed_data
,
477 uint32_t compressed_len
,
480 struct z_stream_s zstream
;
483 memset(&zstream
, 0, sizeof(zstream
));
485 zstream
.next_in
= (unsigned char *)compressed_data
;
486 zstream
.avail_in
= compressed_len
;
488 if (inflateInit(&zstream
) != Z_OK
)
492 zstream
.next_out
= out
;
493 zstream
.avail_out
= 4096;
496 switch (inflate(&zstream
, Z_SYNC_FLUSH
)) {
502 inflateEnd(&zstream
);
506 if (zstream
.avail_out
)
509 out
= realloc(out
, 2*zstream
.total_out
);
511 inflateEnd(&zstream
);
515 zstream
.next_out
= (unsigned char *)out
+ zstream
.total_out
;
516 zstream
.avail_out
= zstream
.total_out
;
519 inflateEnd(&zstream
);
521 return zstream
.total_out
;
524 static uint32_t _hash_uint32(const void *key
)
526 return (uint32_t) (uintptr_t) key
;
530 gen_spec_load(const struct gen_device_info
*devinfo
)
532 struct parser_context ctx
;
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
);
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
;
547 if (text_length
== 0) {
548 fprintf(stderr
, "unable to find gen (%u) data\n", gen_10
);
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");
560 XML_SetElementHandler(ctx
.parser
, start_element
, end_element
);
561 XML_SetCharacterDataHandler(ctx
.parser
, character_data
);
563 ctx
.spec
= rzalloc(NULL
, struct gen_spec
);
566 _mesa_hash_table_create(ctx
.spec
, _mesa_hash_string
, _mesa_key_string_equal
);
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
);
574 _mesa_hash_table_create(ctx
.spec
, _mesa_hash_string
, _mesa_key_string_equal
);
576 ctx
.spec
->access_cache
=
577 _mesa_hash_table_create(ctx
.spec
, _mesa_hash_string
, _mesa_key_string_equal
);
579 total_length
= zlib_inflate(compress_genxmls
,
580 sizeof(compress_genxmls
),
581 (void **) &text_data
);
582 assert(text_offset
+ text_length
<= total_length
);
584 buf
= XML_GetBuffer(ctx
.parser
, text_length
);
585 memcpy(buf
, &text_data
[text_offset
], text_length
);
587 if (XML_ParseBuffer(ctx
.parser
, text_length
, true) == 0) {
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
);
599 XML_ParserFree(ctx
.parser
);
606 gen_spec_load_from_path(const struct gen_device_info
*devinfo
,
609 struct parser_context ctx
;
610 size_t len
, filename_len
= strlen(path
) + 20;
611 char *filename
= malloc(filename_len
);
615 len
= snprintf(filename
, filename_len
, "%s/gen%i.xml",
616 path
, devinfo_to_gen(devinfo
));
617 assert(len
< filename_len
);
619 input
= fopen(filename
, "r");
621 fprintf(stderr
, "failed to open xml description\n");
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");
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
);
642 buf
= XML_GetBuffer(ctx
.parser
, XML_BUFFER_SIZE
);
643 len
= fread(buf
, 1, XML_BUFFER_SIZE
, input
);
645 fprintf(stderr
, "fread: %m\n");
650 if (XML_ParseBuffer(ctx
.parser
, len
, len
== 0) == 0) {
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
)));
663 XML_ParserFree(ctx
.parser
);
671 void gen_spec_destroy(struct gen_spec
*spec
)
677 gen_spec_find_instruction(struct gen_spec
*spec
, const uint32_t *p
)
679 struct hash_entry
*entry
;
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
)
692 gen_group_find_field(struct gen_group
*group
, const char *name
)
695 snprintf(path
, sizeof(path
), "%s/%s", group
->name
, name
);
697 struct gen_spec
*spec
= group
->spec
;
698 struct hash_entry
*entry
= _mesa_hash_table_search(spec
->access_cache
,
703 struct gen_field
*field
= group
->fields
;
705 if (strcmp(field
->name
, name
) == 0) {
706 _mesa_hash_table_insert(spec
->access_cache
,
707 ralloc_strdup(spec
, path
),
718 gen_group_get_length(struct gen_group
*group
, const uint32_t *p
)
720 if (group
&& group
->fixed_length
)
721 return group
->dw_length
;
724 uint32_t type
= field_value(h
, 29, 31);
728 uint32_t opcode
= field_value(h
, 23, 28);
732 return field_value(h
, 0, 7) + 2;
737 return field_value(h
, 0, 7) + 2;
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);
746 if (whole_opcode
== 0x6104 /* PIPELINE_SELECT_965 */)
749 return field_value(h
, 0, 7) + 2;
759 return field_value(h
, 0, 7) + 2;
761 return field_value(h
, 0, 15) + 2;
766 if (whole_opcode
== 0x780b)
769 return field_value(h
, 0, 7) + 2;
780 gen_get_enum_name(struct gen_enum
*e
, uint64_t value
)
782 for (int i
= 0; i
< e
->nvalues
; i
++) {
783 if (e
->values
[i
]->value
== value
) {
784 return e
->values
[i
]->name
;
791 iter_more_fields(const struct gen_field_iterator
*iter
)
793 return iter
->field
!= NULL
&& iter
->field
->next
!= NULL
;
797 iter_group_offset_bits(const struct gen_field_iterator
*iter
,
800 return iter
->group
->group_offset
+ (group_iter
* iter
->group
->group_size
);
804 iter_more_groups(const struct gen_field_iterator
*iter
)
806 if (iter
->group
->variable
) {
807 return iter_group_offset_bits(iter
, iter
->group_iter
+ 1) <
808 (gen_group_get_length(iter
->group
, iter
->p
) * 32);
810 return (iter
->group_iter
+ 1) < iter
->group
->group_count
||
811 iter
->group
->next
!= NULL
;
816 iter_start_field(struct gen_field_iterator
*iter
, struct gen_field
*field
)
820 int group_member_offset
= iter_group_offset_bits(iter
, iter
->group_iter
);
822 iter
->start_bit
= group_member_offset
+ iter
->field
->start
;
823 iter
->end_bit
= group_member_offset
+ iter
->field
->end
;
824 iter
->struct_desc
= NULL
;
828 iter_advance_group(struct gen_field_iterator
*iter
)
830 if (iter
->group
->variable
)
833 if ((iter
->group_iter
+ 1) < iter
->group
->group_count
) {
836 iter
->group
= iter
->group
->next
;
837 iter
->group_iter
= 0;
841 iter_start_field(iter
, iter
->group
->fields
);
845 iter_advance_field(struct gen_field_iterator
*iter
)
847 if (iter_more_fields(iter
)) {
848 iter_start_field(iter
, iter
->field
->next
);
850 if (!iter_more_groups(iter
))
853 iter_advance_group(iter
);
859 iter_decode_field_raw(struct gen_field_iterator
*iter
, uint64_t *qw
)
863 int field_start
= iter
->p_bit
+ iter
->start_bit
;
864 int field_end
= iter
->p_bit
+ iter
->end_bit
;
866 const uint32_t *p
= iter
->p
+ (iter
->start_bit
/ 32);
867 if (iter
->p_end
&& p
>= iter
->p_end
)
870 if ((field_end
- field_start
) > 32) {
871 if (!iter
->p_end
|| (p
+ 1) < iter
->p_end
)
872 *qw
= ((uint64_t) p
[1]) << 32;
877 *qw
= field_value(*qw
, field_start
, field_end
);
879 /* Address & offset types have to be aligned to dwords, their start bit is
880 * a reminder of the alignment requirement.
882 if (iter
->field
->type
.kind
== GEN_TYPE_ADDRESS
||
883 iter
->field
->type
.kind
== GEN_TYPE_OFFSET
)
884 *qw
<<= field_start
% 32;
890 iter_decode_field(struct gen_field_iterator
*iter
)
897 if (iter
->field
->name
)
898 snprintf(iter
->name
, sizeof(iter
->name
), "%s", iter
->field
->name
);
900 memset(iter
->name
, 0, sizeof(iter
->name
));
902 memset(&v
, 0, sizeof(v
));
904 if (!iter_decode_field_raw(iter
, &iter
->raw_value
))
907 const char *enum_name
= NULL
;
909 v
.qw
= iter
->raw_value
;
910 switch (iter
->field
->type
.kind
) {
911 case GEN_TYPE_UNKNOWN
:
913 snprintf(iter
->value
, sizeof(iter
->value
), "%"PRId64
, v
.qw
);
914 enum_name
= gen_get_enum_name(&iter
->field
->inline_enum
, v
.qw
);
917 case GEN_TYPE_UINT
: {
918 snprintf(iter
->value
, sizeof(iter
->value
), "%"PRIu64
, v
.qw
);
919 enum_name
= gen_get_enum_name(&iter
->field
->inline_enum
, v
.qw
);
922 case GEN_TYPE_BOOL
: {
923 const char *true_string
=
924 iter
->print_colors
? "\e[0;35mtrue\e[0m" : "true";
925 snprintf(iter
->value
, sizeof(iter
->value
), "%s",
926 v
.qw
? true_string
: "false");
930 snprintf(iter
->value
, sizeof(iter
->value
), "%f", v
.f
);
932 case GEN_TYPE_ADDRESS
:
933 case GEN_TYPE_OFFSET
:
934 snprintf(iter
->value
, sizeof(iter
->value
), "0x%08"PRIx64
, v
.qw
);
936 case GEN_TYPE_STRUCT
:
937 snprintf(iter
->value
, sizeof(iter
->value
), "<struct %s>",
938 iter
->field
->type
.gen_struct
->name
);
940 gen_spec_find_struct(iter
->group
->spec
,
941 iter
->field
->type
.gen_struct
->name
);
943 case GEN_TYPE_UFIXED
:
944 snprintf(iter
->value
, sizeof(iter
->value
), "%f",
945 (float) v
.qw
/ (1 << iter
->field
->type
.f
));
947 case GEN_TYPE_SFIXED
: {
948 /* Sign extend before converting */
949 int bits
= iter
->field
->type
.i
+ iter
->field
->type
.f
+ 1;
950 int64_t v_sign_extend
= ((int64_t)(v
.qw
<< (64 - bits
))) >> (64 - bits
);
951 snprintf(iter
->value
, sizeof(iter
->value
), "%f",
952 (float) v_sign_extend
/ (1 << iter
->field
->type
.f
));
957 case GEN_TYPE_ENUM
: {
958 snprintf(iter
->value
, sizeof(iter
->value
), "%"PRId64
, v
.qw
);
959 enum_name
= gen_get_enum_name(iter
->field
->type
.gen_enum
, v
.qw
);
964 if (strlen(iter
->group
->name
) == 0) {
965 int length
= strlen(iter
->name
);
966 snprintf(iter
->name
+ length
, sizeof(iter
->name
) - length
,
967 "[%i]", iter
->group_iter
);
971 int length
= strlen(iter
->value
);
972 snprintf(iter
->value
+ length
, sizeof(iter
->value
) - length
,
974 } else if (strcmp(iter
->name
, "Surface Format") == 0 ||
975 strcmp(iter
->name
, "Source Element Format") == 0) {
976 if (isl_format_is_valid((enum isl_format
)v
.qw
)) {
977 const char *fmt_name
= isl_format_get_name((enum isl_format
)v
.qw
);
978 int length
= strlen(iter
->value
);
979 snprintf(iter
->value
+ length
, sizeof(iter
->value
) - length
,
988 gen_field_iterator_init(struct gen_field_iterator
*iter
,
989 struct gen_group
*group
,
990 const uint32_t *p
, int p_bit
,
993 memset(iter
, 0, sizeof(*iter
));
999 int length
= gen_group_get_length(iter
->group
, iter
->p
);
1000 iter
->p_end
= length
> 0 ? &p
[length
] : NULL
;
1001 iter
->print_colors
= print_colors
;
1005 gen_field_iterator_next(struct gen_field_iterator
*iter
)
1007 /* Initial condition */
1009 if (iter
->group
->fields
)
1010 iter_start_field(iter
, iter
->group
->fields
);
1012 iter_start_field(iter
, iter
->group
->next
->fields
);
1014 bool result
= iter_decode_field(iter
);
1021 if (!iter_advance_field(iter
))
1024 if (!iter_decode_field(iter
))
1031 print_dword_header(FILE *outfile
,
1032 struct gen_field_iterator
*iter
,
1033 uint64_t offset
, uint32_t dword
)
1035 fprintf(outfile
, "0x%08"PRIx64
": 0x%08x : Dword %d\n",
1036 offset
+ 4 * dword
, iter
->p
[dword
], dword
);
1040 gen_field_is_header(struct gen_field
*field
)
1044 if (field
->start
>= 32)
1047 bits
= (1U << (field
->end
- field
->start
+ 1)) - 1;
1048 bits
<<= field
->start
;
1050 return (field
->parent
->opcode_mask
& bits
) != 0;
1054 gen_print_group(FILE *outfile
, struct gen_group
*group
, uint64_t offset
,
1055 const uint32_t *p
, int p_bit
, bool color
)
1057 struct gen_field_iterator iter
;
1058 int last_dword
= -1;
1060 gen_field_iterator_init(&iter
, group
, p
, p_bit
, color
);
1061 while (gen_field_iterator_next(&iter
)) {
1062 int iter_dword
= iter
.end_bit
/ 32;
1063 if (last_dword
!= iter_dword
) {
1064 for (int i
= last_dword
+ 1; i
<= iter_dword
; i
++)
1065 print_dword_header(outfile
, &iter
, offset
, i
);
1066 last_dword
= iter_dword
;
1068 if (!gen_field_is_header(iter
.field
)) {
1069 fprintf(outfile
, " %s: %s\n", iter
.name
, iter
.value
);
1070 if (iter
.struct_desc
) {
1071 int struct_dword
= iter
.start_bit
/ 32;
1072 uint64_t struct_offset
= offset
+ 4 * struct_dword
;
1073 gen_print_group(outfile
, iter
.struct_desc
, struct_offset
,
1074 &p
[struct_dword
], iter
.start_bit
% 32, color
);