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/list.h>
34 #include <util/macros.h>
35 #include <util/os_file.h>
36 #include <util/ralloc.h>
37 #include <util/u_math.h>
38
39 #include "intel_decoder.h"
40
41 #include "isl/isl.h"
42 #include "genxml/genX_xml.h"
43
44 #define XML_BUFFER_SIZE 4096
45 #define MAX_VALUE_ITEMS 128
46
47 struct location {
48 const char *filename;
49 int line_number;
50 };
51
52 struct genxml_import_exclusion {
53 struct list_head link;
54 char *name;
55 };
56
57 struct genxml_import {
58 struct list_head link;
59 struct list_head exclusions;
60 char *name;
61 };
62
63 struct parser_context {
64 XML_Parser parser;
65 int foo;
66 struct location loc;
67
68 struct intel_group *group;
69 struct intel_enum *enoom;
70 const char *dirname;
71 struct genxml_import import;
72
73 int n_values, n_allocated_values;
74 struct intel_value **values;
75
76 struct intel_field *last_field;
77
78 struct intel_spec *spec;
79 };
80
81 const char *
intel_group_get_name(const struct intel_group * group)82 intel_group_get_name(const struct intel_group *group)
83 {
84 return group->name;
85 }
86
87 uint32_t
intel_group_get_opcode(const struct intel_group * group)88 intel_group_get_opcode(const struct intel_group *group)
89 {
90 return group->opcode;
91 }
92
93 struct intel_group *
intel_spec_find_struct(struct intel_spec * spec,const char * name)94 intel_spec_find_struct(struct intel_spec *spec, const char *name)
95 {
96 struct hash_entry *entry = _mesa_hash_table_search(spec->structs,
97 name);
98 return entry ? entry->data : NULL;
99 }
100
101 struct intel_group *
intel_spec_find_register(struct intel_spec * spec,uint32_t offset)102 intel_spec_find_register(struct intel_spec *spec, uint32_t offset)
103 {
104 struct hash_entry *entry =
105 _mesa_hash_table_search(spec->registers_by_offset,
106 (void *) (uintptr_t) offset);
107 return entry ? entry->data : NULL;
108 }
109
110 struct intel_group *
intel_spec_find_register_by_name(struct intel_spec * spec,const char * name)111 intel_spec_find_register_by_name(struct intel_spec *spec, const char *name)
112 {
113 struct hash_entry *entry =
114 _mesa_hash_table_search(spec->registers_by_name, name);
115 return entry ? entry->data : NULL;
116 }
117
118 struct intel_enum *
intel_spec_find_enum(struct intel_spec * spec,const char * name)119 intel_spec_find_enum(struct intel_spec *spec, const char *name)
120 {
121 struct hash_entry *entry = _mesa_hash_table_search(spec->enums,
122 name);
123 return entry ? entry->data : NULL;
124 }
125
126 uint32_t
intel_spec_get_gen(struct intel_spec * spec)127 intel_spec_get_gen(struct intel_spec *spec)
128 {
129 return spec->gen;
130 }
131
132 static void __attribute__((noreturn))
fail(struct location * loc,const char * msg,...)133 fail(struct location *loc, const char *msg, ...)
134 {
135 va_list ap;
136
137 va_start(ap, msg);
138 fprintf(stderr, "%s:%d: error: ",
139 loc->filename, loc->line_number);
140 vfprintf(stderr, msg, ap);
141 fprintf(stderr, "\n");
142 va_end(ap);
143 exit(EXIT_FAILURE);
144 }
145
146 static void
get_array_offset_count(const char ** atts,uint32_t * offset,uint32_t * count,uint32_t * size,bool * variable)147 get_array_offset_count(const char **atts, uint32_t *offset, uint32_t *count,
148 uint32_t *size, bool *variable)
149 {
150 for (int i = 0; atts[i]; i += 2) {
151 char *p;
152
153 if (strcmp(atts[i], "count") == 0) {
154 *count = strtoul(atts[i + 1], &p, 0);
155 if (*count == 0)
156 *variable = true;
157 } else if (strcmp(atts[i], "start") == 0) {
158 *offset = strtoul(atts[i + 1], &p, 0);
159 } else if (strcmp(atts[i], "size") == 0) {
160 *size = strtoul(atts[i + 1], &p, 0);
161 }
162 }
163 return;
164 }
165
166 static struct intel_group *
create_group(struct parser_context * ctx,const char * name,const char ** atts,struct intel_group * parent,bool fixed_length)167 create_group(struct parser_context *ctx,
168 const char *name,
169 const char **atts,
170 struct intel_group *parent,
171 bool fixed_length)
172 {
173 struct intel_group *group;
174
175 group = rzalloc(ctx->spec, struct intel_group);
176 if (name)
177 group->name = ralloc_strdup(group, name);
178
179 group->spec = ctx->spec;
180 group->variable = false;
181 group->fixed_length = fixed_length;
182 group->dword_length_field = NULL;
183 group->dw_length = 0;
184 group->engine_mask = INTEL_ENGINE_CLASS_TO_MASK(INTEL_ENGINE_CLASS_RENDER) |
185 INTEL_ENGINE_CLASS_TO_MASK(INTEL_ENGINE_CLASS_COMPUTE) |
186 INTEL_ENGINE_CLASS_TO_MASK(INTEL_ENGINE_CLASS_VIDEO) |
187 INTEL_ENGINE_CLASS_TO_MASK(INTEL_ENGINE_CLASS_COPY);
188 group->bias = 1;
189
190 for (int i = 0; atts[i]; i += 2) {
191 char *p;
192 if (strcmp(atts[i], "length") == 0) {
193 group->dw_length = strtoul(atts[i + 1], &p, 0);
194 } else if (strcmp(atts[i], "bias") == 0) {
195 group->bias = strtoul(atts[i + 1], &p, 0);
196 } else if (strcmp(atts[i], "engine") == 0) {
197 void *mem_ctx = ralloc_context(NULL);
198 char *tmp = ralloc_strdup(mem_ctx, atts[i + 1]);
199 char *save_ptr;
200 char *tok = strtok_r(tmp, "|", &save_ptr);
201
202 group->engine_mask = 0;
203 while (tok != NULL) {
204 if (strcmp(tok, "render") == 0) {
205 group->engine_mask |= INTEL_ENGINE_CLASS_TO_MASK(INTEL_ENGINE_CLASS_RENDER);
206 } else if (strcmp(tok, "compute") == 0) {
207 group->engine_mask |= INTEL_ENGINE_CLASS_TO_MASK(INTEL_ENGINE_CLASS_COMPUTE);
208 } else if (strcmp(tok, "video") == 0) {
209 group->engine_mask |= INTEL_ENGINE_CLASS_TO_MASK(INTEL_ENGINE_CLASS_VIDEO);
210 } else if (strcmp(tok, "blitter") == 0) {
211 group->engine_mask |= INTEL_ENGINE_CLASS_TO_MASK(INTEL_ENGINE_CLASS_COPY);
212 } else {
213 fprintf(stderr, "unknown engine class defined for instruction \"%s\": %s\n", name, atts[i + 1]);
214 }
215
216 tok = strtok_r(NULL, "|", &save_ptr);
217 }
218
219 ralloc_free(mem_ctx);
220 }
221 }
222
223 if (parent) {
224 group->parent = parent;
225 get_array_offset_count(atts,
226 &group->array_offset,
227 &group->array_count,
228 &group->array_item_size,
229 &group->variable);
230 }
231
232 return group;
233 }
234
235 static struct intel_enum *
create_enum(struct parser_context * ctx,const char * name,const char ** atts)236 create_enum(struct parser_context *ctx, const char *name, const char **atts)
237 {
238 struct intel_enum *e;
239
240 e = rzalloc(ctx->spec, struct intel_enum);
241 if (name)
242 e->name = ralloc_strdup(e, name);
243
244 return e;
245 }
246
247 static void
get_register_offset(const char ** atts,uint32_t * offset)248 get_register_offset(const char **atts, uint32_t *offset)
249 {
250 for (int i = 0; atts[i]; i += 2) {
251 char *p;
252
253 if (strcmp(atts[i], "num") == 0)
254 *offset = strtoul(atts[i + 1], &p, 0);
255 }
256 return;
257 }
258
259 static void
get_start_end_pos(int * start,int * end)260 get_start_end_pos(int *start, int *end)
261 {
262 /* start value has to be mod with 32 as we need the relative
263 * start position in the first DWord. For the end position, add
264 * the length of the field to the start position to get the
265 * relative position in the 64 bit address.
266 */
267 if (*end - *start > 32) {
268 int len = *end - *start;
269 *start = *start % 32;
270 *end = *start + len;
271 } else {
272 *start = *start % 32;
273 *end = *end % 32;
274 }
275
276 return;
277 }
278
279 static inline uint64_t
mask(int start,int end)280 mask(int start, int end)
281 {
282 uint64_t v;
283
284 v = ~0ULL >> (63 - end + start);
285
286 return v << start;
287 }
288
289 static inline uint64_t
field_value(uint64_t value,int start,int end)290 field_value(uint64_t value, int start, int end)
291 {
292 get_start_end_pos(&start, &end);
293 return (value & mask(start, end)) >> (start);
294 }
295
296 static struct intel_type
string_to_type(struct parser_context * ctx,const char * s)297 string_to_type(struct parser_context *ctx, const char *s)
298 {
299 int i, f;
300 struct intel_group *g;
301 struct intel_enum *e;
302
303 if (strcmp(s, "int") == 0)
304 return (struct intel_type) { .kind = INTEL_TYPE_INT };
305 else if (strcmp(s, "uint") == 0)
306 return (struct intel_type) { .kind = INTEL_TYPE_UINT };
307 else if (strcmp(s, "bool") == 0)
308 return (struct intel_type) { .kind = INTEL_TYPE_BOOL };
309 else if (strcmp(s, "float") == 0)
310 return (struct intel_type) { .kind = INTEL_TYPE_FLOAT };
311 else if (strcmp(s, "address") == 0)
312 return (struct intel_type) { .kind = INTEL_TYPE_ADDRESS };
313 else if (strcmp(s, "offset") == 0)
314 return (struct intel_type) { .kind = INTEL_TYPE_OFFSET };
315 else if (sscanf(s, "u%d.%d", &i, &f) == 2)
316 return (struct intel_type) { .kind = INTEL_TYPE_UFIXED, .i = i, .f = f };
317 else if (sscanf(s, "s%d.%d", &i, &f) == 2)
318 return (struct intel_type) { .kind = INTEL_TYPE_SFIXED, .i = i, .f = f };
319 else if (g = intel_spec_find_struct(ctx->spec, s), g != NULL)
320 return (struct intel_type) { .kind = INTEL_TYPE_STRUCT, .intel_struct = g };
321 else if (e = intel_spec_find_enum(ctx->spec, s), e != NULL)
322 return (struct intel_type) { .kind = INTEL_TYPE_ENUM, .intel_enum = e };
323 else if (strcmp(s, "mbo") == 0)
324 return (struct intel_type) { .kind = INTEL_TYPE_MBO };
325 else if (strcmp(s, "mbz") == 0)
326 return (struct intel_type) { .kind = INTEL_TYPE_MBZ };
327 else
328 fail(&ctx->loc, "invalid type: %s", s);
329 }
330
331 static struct intel_field *
create_field(struct parser_context * ctx,const char ** atts)332 create_field(struct parser_context *ctx, const char **atts)
333 {
334 struct intel_field *field;
335
336 field = rzalloc(ctx->group, struct intel_field);
337 field->parent = ctx->group;
338
339 for (int i = 0; atts[i]; i += 2) {
340 char *p;
341
342 if (strcmp(atts[i], "name") == 0) {
343 field->name = ralloc_strdup(field, atts[i + 1]);
344 if (strcmp(field->name, "DWord Length") == 0) {
345 field->parent->dword_length_field = field;
346 }
347 } else if (strcmp(atts[i], "start") == 0) {
348 field->start = strtoul(atts[i + 1], &p, 0);
349 } else if (strcmp(atts[i], "end") == 0) {
350 field->end = strtoul(atts[i + 1], &p, 0);
351 } else if (strcmp(atts[i], "type") == 0) {
352 field->type = string_to_type(ctx, atts[i + 1]);
353 } else if (strcmp(atts[i], "default") == 0 &&
354 field->start >= 16 && field->end <= 31) {
355 field->has_default = true;
356 field->default_value = strtoul(atts[i + 1], &p, 0);
357 }
358 }
359
360 return field;
361 }
362
363 static struct intel_field *
create_array_field(struct parser_context * ctx,struct intel_group * array)364 create_array_field(struct parser_context *ctx, struct intel_group *array)
365 {
366 struct intel_field *field;
367
368 field = rzalloc(ctx->group, struct intel_field);
369 field->parent = ctx->group;
370
371 field->array = array;
372 field->start = field->array->array_offset;
373
374 return field;
375 }
376
377 static struct intel_value *
create_value(struct parser_context * ctx,const char ** atts)378 create_value(struct parser_context *ctx, const char **atts)
379 {
380 struct intel_value *value = rzalloc(ctx->values, struct intel_value);
381
382 for (int i = 0; atts[i]; i += 2) {
383 if (strcmp(atts[i], "name") == 0)
384 value->name = ralloc_strdup(value, atts[i + 1]);
385 else if (strcmp(atts[i], "value") == 0)
386 value->value = strtoul(atts[i + 1], NULL, 0);
387 }
388
389 return value;
390 }
391
392 static struct intel_field *
create_and_append_field(struct parser_context * ctx,const char ** atts,struct intel_group * array)393 create_and_append_field(struct parser_context *ctx,
394 const char **atts,
395 struct intel_group *array)
396 {
397 struct intel_field *field = array ?
398 create_array_field(ctx, array) : create_field(ctx, atts);
399 struct intel_field *prev = NULL, *list = ctx->group->fields;
400
401 while (list && field->start > list->start) {
402 prev = list;
403 list = list->next;
404 }
405
406 field->next = list;
407 if (prev == NULL)
408 ctx->group->fields = field;
409 else
410 prev->next = field;
411
412 return field;
413 }
414
415 static bool
start_genxml_import(struct parser_context * ctx,const char ** atts)416 start_genxml_import(struct parser_context *ctx, const char **atts)
417 {
418 assert(ctx->import.name == NULL);
419 assert(list_is_empty(&ctx->import.exclusions));
420 list_inithead(&ctx->import.exclusions);
421
422 for (int i = 0; atts[i]; i += 2) {
423 if (strcmp(atts[i], "name") == 0) {
424 ctx->import.name = ralloc_strdup(ctx->spec, atts[i + 1]);
425 }
426 }
427
428 if (ctx->import.name == NULL)
429 fail(&ctx->loc, "import without name");
430
431 return ctx->import.name != NULL;
432 }
433
434 static struct genxml_import_exclusion *
add_genxml_import_exclusion(struct parser_context * ctx,const char ** atts)435 add_genxml_import_exclusion(struct parser_context *ctx, const char **atts)
436 {
437 struct genxml_import_exclusion *exclusion;
438
439 if (ctx->import.name == NULL) {
440 fail(&ctx->loc, "exclude found without a named import");
441 return NULL;
442 }
443
444 exclusion = rzalloc(ctx->import.name, struct genxml_import_exclusion);
445
446 for (int i = 0; atts[i]; i += 2) {
447 if (strcmp(atts[i], "name") == 0) {
448 exclusion->name = ralloc_strdup(exclusion, atts[i + 1]);
449 }
450 }
451
452 if (exclusion->name != NULL) {
453 list_addtail(&exclusion->link, &ctx->import.exclusions);
454 } else {
455 ralloc_free(exclusion);
456 exclusion = NULL;
457 }
458
459 return exclusion;
460 }
461
462 static void
463 move_group_to_spec(struct intel_spec *new_spec, struct intel_spec *old_spec,
464 struct intel_group *group);
465
466 static void
move_field_to_spec(struct intel_spec * new_spec,struct intel_spec * old_spec,struct intel_field * field)467 move_field_to_spec(struct intel_spec *new_spec, struct intel_spec *old_spec,
468 struct intel_field *field)
469 {
470 while (field != NULL) {
471 if (field->array != NULL && field->array->spec == old_spec)
472 move_group_to_spec(new_spec, old_spec, field->array);
473 if (field->type.kind == INTEL_TYPE_STRUCT &&
474 field->type.intel_struct->spec == old_spec)
475 move_group_to_spec(new_spec, old_spec, field->type.intel_struct);
476 if (field->type.kind == INTEL_TYPE_ENUM)
477 ralloc_steal(new_spec, field->type.intel_enum);
478 field = field->next;
479 }
480 }
481
482 static void
move_group_to_spec(struct intel_spec * new_spec,struct intel_spec * old_spec,struct intel_group * group)483 move_group_to_spec(struct intel_spec *new_spec, struct intel_spec *old_spec,
484 struct intel_group *group)
485 {
486 struct intel_group *g = group;
487 while (g != NULL) {
488 if (g->spec == old_spec) {
489 if (ralloc_parent(g) == old_spec)
490 ralloc_steal(new_spec, g);
491 g->spec = new_spec;
492 }
493 g = g->next;
494 }
495 move_field_to_spec(new_spec, old_spec, group->fields);
496 move_field_to_spec(new_spec, old_spec, group->dword_length_field);
497 }
498
499 static bool
finish_genxml_import(struct parser_context * ctx)500 finish_genxml_import(struct parser_context *ctx)
501 {
502 struct intel_spec *spec = ctx->spec;
503 struct genxml_import *import = &ctx->import;
504
505 if (import->name == NULL) {
506 fail(&ctx->loc, "import without name");
507 return false;
508 }
509
510 struct intel_spec *imported_spec =
511 intel_spec_load_filename(ctx->dirname, import->name);
512 if (import->name == NULL) {
513 fail(&ctx->loc, "failed to load %s for importing", import->name);
514 return false;
515 }
516
517 assert(_mesa_hash_table_num_entries(imported_spec->access_cache) == 0);
518
519 list_for_each_entry(struct genxml_import_exclusion, exclusion,
520 &import->exclusions, link) {
521 struct hash_entry *entry;
522 entry = _mesa_hash_table_search(imported_spec->commands,
523 exclusion->name);
524 if (entry != NULL) {
525 _mesa_hash_table_remove(imported_spec->commands, entry);
526 }
527 entry = _mesa_hash_table_search(imported_spec->structs,
528 exclusion->name);
529 if (entry != NULL) {
530 _mesa_hash_table_remove(imported_spec->structs, entry);
531 }
532 entry = _mesa_hash_table_search(imported_spec->registers_by_name,
533 exclusion->name);
534 if (entry != NULL) {
535 struct intel_group *group = entry->data;
536 _mesa_hash_table_remove(imported_spec->registers_by_name, entry);
537 entry = _mesa_hash_table_search(imported_spec->registers_by_offset,
538 (void *) (uintptr_t) group->register_offset);
539 if (entry != NULL)
540 _mesa_hash_table_remove(imported_spec->registers_by_offset, entry);
541 }
542 entry = _mesa_hash_table_search(imported_spec->enums,
543 exclusion->name);
544 if (entry != NULL) {
545 _mesa_hash_table_remove(imported_spec->enums, entry);
546 }
547 }
548
549 hash_table_foreach(imported_spec->commands, entry) {
550 struct intel_group *group = entry->data;
551 move_group_to_spec(spec, imported_spec, group);
552 _mesa_hash_table_insert(spec->commands, group->name, group);
553 }
554 hash_table_foreach(imported_spec->structs, entry) {
555 struct intel_group *group = entry->data;
556 move_group_to_spec(spec, imported_spec, group);
557 _mesa_hash_table_insert(spec->structs, group->name, group);
558 }
559 hash_table_foreach(imported_spec->registers_by_name, entry) {
560 struct intel_group *group = entry->data;
561 move_group_to_spec(spec, imported_spec, group);
562 _mesa_hash_table_insert(spec->registers_by_name, group->name, group);
563 _mesa_hash_table_insert(spec->registers_by_offset,
564 (void *) (uintptr_t) group->register_offset,
565 group);
566 }
567 hash_table_foreach(imported_spec->enums, entry) {
568 struct intel_enum *enoom = entry->data;
569 ralloc_steal(spec, enoom);
570 _mesa_hash_table_insert(spec->enums, enoom->name, enoom);
571 }
572
573 intel_spec_destroy(imported_spec);
574 ralloc_free(ctx->import.name); /* also frees exclusions */
575 ctx->import.name = NULL;
576 list_inithead(&ctx->import.exclusions);
577
578 return true;
579 }
580
581 static void
start_element(void * data,const char * element_name,const char ** atts)582 start_element(void *data, const char *element_name, const char **atts)
583 {
584 struct parser_context *ctx = data;
585 const char *name = NULL;
586 const char *gen = NULL;
587
588 ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
589
590 for (int i = 0; atts[i]; i += 2) {
591 if (strcmp(atts[i], "name") == 0)
592 name = atts[i + 1];
593 else if (strcmp(atts[i], "gen") == 0)
594 gen = atts[i + 1];
595 }
596
597 if (strcmp(element_name, "genxml") == 0) {
598 if (name == NULL)
599 fail(&ctx->loc, "no platform name given");
600 if (gen == NULL)
601 fail(&ctx->loc, "no gen given");
602
603 int major, minor;
604 int n = sscanf(gen, "%d.%d", &major, &minor);
605 if (n == 0)
606 fail(&ctx->loc, "invalid gen given: %s", gen);
607 if (n == 1)
608 minor = 0;
609
610 ctx->spec->gen = intel_make_gen(major, minor);
611 } else if (strcmp(element_name, "instruction") == 0) {
612 ctx->group = create_group(ctx, name, atts, NULL, false);
613 } else if (strcmp(element_name, "struct") == 0) {
614 ctx->group = create_group(ctx, name, atts, NULL, true);
615 } else if (strcmp(element_name, "register") == 0) {
616 ctx->group = create_group(ctx, name, atts, NULL, true);
617 get_register_offset(atts, &ctx->group->register_offset);
618 } else if (strcmp(element_name, "group") == 0) {
619 struct intel_group *group = create_group(ctx, "", atts, ctx->group, false);
620 ctx->last_field = create_and_append_field(ctx, NULL, group);
621 ctx->group = group;
622 } else if (strcmp(element_name, "field") == 0) {
623 ctx->last_field = create_and_append_field(ctx, atts, NULL);
624 } else if (strcmp(element_name, "enum") == 0) {
625 ctx->enoom = create_enum(ctx, name, atts);
626 } else if (strcmp(element_name, "value") == 0) {
627 if (ctx->n_values >= ctx->n_allocated_values) {
628 ctx->n_allocated_values = MAX2(2, ctx->n_allocated_values * 2);
629 ctx->values = reralloc_array_size(ctx->spec, ctx->values,
630 sizeof(struct intel_value *),
631 ctx->n_allocated_values);
632 }
633 assert(ctx->n_values < ctx->n_allocated_values);
634 ctx->values[ctx->n_values++] = create_value(ctx, atts);
635 } else if (strcmp(element_name, "import") == 0) {
636 start_genxml_import(ctx, atts);
637 } else if (strcmp(element_name, "exclude") == 0) {
638 add_genxml_import_exclusion(ctx, atts);
639 }
640
641 }
642
643 static void
end_element(void * data,const char * name)644 end_element(void *data, const char *name)
645 {
646 struct parser_context *ctx = data;
647 struct intel_spec *spec = ctx->spec;
648
649 if (strcmp(name, "instruction") == 0 ||
650 strcmp(name, "struct") == 0 ||
651 strcmp(name, "register") == 0) {
652 struct intel_group *group = ctx->group;
653 struct intel_field *list = group->fields;
654
655 ctx->group = ctx->group->parent;
656
657 if (strcmp(name, "instruction") == 0) {
658 while (list && list->end <= 31) {
659 if (list->start >= 16 && list->has_default) {
660 group->opcode_mask |=
661 mask(list->start % 32, list->end % 32);
662 group->opcode |= list->default_value << list->start;
663 }
664 list = list->next;
665 }
666 }
667
668 if (strcmp(name, "instruction") == 0)
669 _mesa_hash_table_insert(spec->commands, group->name, group);
670 else if (strcmp(name, "struct") == 0)
671 _mesa_hash_table_insert(spec->structs, group->name, group);
672 else if (strcmp(name, "register") == 0) {
673 _mesa_hash_table_insert(spec->registers_by_name, group->name, group);
674 _mesa_hash_table_insert(spec->registers_by_offset,
675 (void *) (uintptr_t) group->register_offset,
676 group);
677 }
678 } else if (strcmp(name, "group") == 0) {
679 ctx->group = ctx->group->parent;
680 } else if (strcmp(name, "field") == 0) {
681 struct intel_field *field = ctx->last_field;
682 ctx->last_field = NULL;
683 field->inline_enum.values = ctx->values;
684 ralloc_steal(field, ctx->values);
685 field->inline_enum.nvalues = ctx->n_values;
686 ctx->values = ralloc_array(ctx->spec, struct intel_value*, ctx->n_allocated_values = 2);
687 ctx->n_values = 0;
688 } else if (strcmp(name, "enum") == 0) {
689 struct intel_enum *e = ctx->enoom;
690 e->values = ctx->values;
691 ralloc_steal(e, ctx->values);
692 e->nvalues = ctx->n_values;
693 ctx->values = ralloc_array(ctx->spec, struct intel_value*, ctx->n_allocated_values = 2);
694 ctx->n_values = 0;
695 ctx->enoom = NULL;
696 _mesa_hash_table_insert(spec->enums, e->name, e);
697 } else if (strcmp(name, "import") == 0) {
698 finish_genxml_import(ctx);
699 }
700 }
701
702 static void
character_data(void * data,const XML_Char * s,int len)703 character_data(void *data, const XML_Char *s, int len)
704 {
705 }
706
zlib_inflate(const void * compressed_data,uint32_t compressed_len,void ** out_ptr)707 static uint32_t zlib_inflate(const void *compressed_data,
708 uint32_t compressed_len,
709 void **out_ptr)
710 {
711 struct z_stream_s zstream;
712 void *out;
713
714 memset(&zstream, 0, sizeof(zstream));
715
716 zstream.next_in = (unsigned char *)compressed_data;
717 zstream.avail_in = compressed_len;
718
719 if (inflateInit(&zstream) != Z_OK)
720 return 0;
721
722 out = malloc(4096);
723 zstream.next_out = out;
724 zstream.avail_out = 4096;
725
726 do {
727 switch (inflate(&zstream, Z_SYNC_FLUSH)) {
728 case Z_STREAM_END:
729 goto end;
730 case Z_OK:
731 break;
732 default:
733 inflateEnd(&zstream);
734 return 0;
735 }
736
737 if (zstream.avail_out)
738 break;
739
740 out = realloc(out, 2*zstream.total_out);
741 if (out == NULL) {
742 inflateEnd(&zstream);
743 return 0;
744 }
745
746 zstream.next_out = (unsigned char *)out + zstream.total_out;
747 zstream.avail_out = zstream.total_out;
748 } while (1);
749 end:
750 inflateEnd(&zstream);
751 *out_ptr = out;
752 return zstream.total_out;
753 }
754
_hash_uint32(const void * key)755 static uint32_t _hash_uint32(const void *key)
756 {
757 return (uint32_t) (uintptr_t) key;
758 }
759
760 static struct intel_spec *
intel_spec_init(void)761 intel_spec_init(void)
762 {
763 struct intel_spec *spec;
764 spec = rzalloc(NULL, struct intel_spec);
765 if (spec == NULL)
766 return NULL;
767
768 spec->commands =
769 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
770 spec->structs =
771 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
772 spec->registers_by_name =
773 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
774 spec->registers_by_offset =
775 _mesa_hash_table_create(spec, _hash_uint32, _mesa_key_pointer_equal);
776 spec->enums =
777 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
778 spec->access_cache =
779 _mesa_hash_table_create(spec, _mesa_hash_string, _mesa_key_string_equal);
780
781 return spec;
782 }
783
784 static bool
get_xml_data_dir(const char * dirname,const char * filename,void ** data,size_t * data_len)785 get_xml_data_dir(const char *dirname, const char *filename,
786 void **data, size_t *data_len)
787 {
788 size_t fullname_len = strlen(dirname) + strlen(filename) + 2;
789 char *fullname = malloc(fullname_len);
790
791 if (fullname == NULL)
792 return NULL;
793
794 ASSERTED size_t len = snprintf(fullname, fullname_len, "%s/%s",
795 dirname, filename);
796 assert(len < fullname_len);
797
798 *data = (void*)os_read_file(fullname, data_len);
799 free(fullname);
800 return *data != NULL;
801 }
802
803 static bool
get_embedded_xml_data(int verx10,void ** data,size_t * data_len)804 get_embedded_xml_data(int verx10, void **data, size_t *data_len)
805 {
806 uint8_t *text_data = NULL;
807 uint32_t text_offset = 0, text_length = 0;
808 ASSERTED uint32_t total_length;
809
810 for (int i = 0; i < ARRAY_SIZE(genxml_files_table); i++) {
811 if (genxml_files_table[i].ver_10 == verx10) {
812 text_offset = genxml_files_table[i].offset;
813 text_length = genxml_files_table[i].length;
814 break;
815 }
816 }
817
818 if (text_length == 0) {
819 fprintf(stderr, "unable to find gen (%u) data\n", verx10);
820 return false;
821 }
822
823 total_length = zlib_inflate(compress_genxmls,
824 sizeof(compress_genxmls),
825 (void **) &text_data);
826 assert(text_offset + text_length <= total_length);
827
828 *data = malloc(text_length);
829 if (*data == NULL) {
830 free(text_data);
831 return false;
832 }
833
834 memcpy(*data, &text_data[text_offset], text_length);
835 free(text_data);
836 *data_len = text_length;
837 return true;
838 }
839
840 static bool
get_embedded_xml_data_by_name(const char * filename,void ** data,size_t * data_len)841 get_embedded_xml_data_by_name(const char *filename,
842 void **data, size_t *data_len)
843 {
844 int filename_len = strlen(filename);
845 if (filename_len < 8 || filename_len > 10)
846 return false;
847
848 if (strncmp(filename, "gen", 3) != 0 ||
849 strcmp(filename + filename_len - 4, ".xml") != 0)
850 return false;
851
852 char *numstr = strndup(filename + 3, filename_len - 7);
853 char *endptr;
854 long num = strtol(numstr, &endptr, 10);
855 if (*endptr != '\0') {
856 free(numstr);
857 return false;
858 }
859
860 assert(num >= 40);
861
862 free(numstr);
863 return get_embedded_xml_data(num, data, data_len);
864 }
865
866 static bool
get_xml_data(int verx10,const char * dirname,const char * filename,void ** data,size_t * data_len)867 get_xml_data(int verx10, const char *dirname, const char *filename,
868 void **data, size_t *data_len)
869 {
870 if (dirname != NULL)
871 return get_xml_data_dir(dirname, filename, data, data_len);
872 else if (filename != NULL)
873 return get_embedded_xml_data_by_name(filename, data, data_len);
874 else
875 return get_embedded_xml_data(verx10, data, data_len);
876 }
877
878 static struct intel_spec *
intel_spec_load_common(int verx10,const char * dirname,const char * filename)879 intel_spec_load_common(int verx10, const char *dirname, const char *filename)
880 {
881 struct parser_context ctx;
882 void *xmlbuf, *data;
883 size_t data_len;
884
885 if (!get_xml_data(verx10, dirname, filename, &data, &data_len))
886 return NULL;
887
888 memset(&ctx, 0, sizeof ctx);
889 ctx.dirname = dirname;
890 list_inithead(&ctx.import.exclusions);
891 ctx.parser = XML_ParserCreate(NULL);
892 XML_SetUserData(ctx.parser, &ctx);
893 if (ctx.parser == NULL) {
894 free(data);
895 fprintf(stderr, "failed to create parser\n");
896 return NULL;
897 }
898
899 XML_SetElementHandler(ctx.parser, start_element, end_element);
900 XML_SetCharacterDataHandler(ctx.parser, character_data);
901
902 ctx.spec = intel_spec_init();
903 if (ctx.spec == NULL) {
904 free(data);
905 fprintf(stderr, "Failed to create intel_spec\n");
906 return NULL;
907 }
908
909 xmlbuf = XML_GetBuffer(ctx.parser, data_len);
910 memcpy(xmlbuf, data, data_len);
911 free(data);
912 data = NULL;
913
914 if (XML_ParseBuffer(ctx.parser, data_len, true) == 0) {
915 fprintf(stderr,
916 "Error parsing XML at line %ld col %ld byte %ld/%zu: %s\n",
917 XML_GetCurrentLineNumber(ctx.parser),
918 XML_GetCurrentColumnNumber(ctx.parser),
919 XML_GetCurrentByteIndex(ctx.parser), data_len,
920 XML_ErrorString(XML_GetErrorCode(ctx.parser)));
921 XML_ParserFree(ctx.parser);
922 return NULL;
923 }
924
925 XML_ParserFree(ctx.parser);
926 assert(ctx.import.name == NULL);
927
928 return ctx.spec;
929 }
930
931 struct intel_spec *
intel_spec_load(const struct intel_device_info * devinfo)932 intel_spec_load(const struct intel_device_info *devinfo)
933 {
934 return intel_spec_load_common(devinfo->verx10, NULL, NULL);
935 }
936
937 struct intel_spec *
intel_spec_load_filename(const char * dir,const char * name)938 intel_spec_load_filename(const char *dir, const char *name)
939 {
940 return intel_spec_load_common(0, dir, name);
941 }
942
943 struct intel_spec *
intel_spec_load_from_path(const struct intel_device_info * devinfo,const char * path)944 intel_spec_load_from_path(const struct intel_device_info *devinfo,
945 const char *path)
946 {
947 char filename[20];
948 int xml_file_num = devinfo->verx10 % 10 ? devinfo->verx10 : devinfo->ver;
949
950 ASSERTED size_t len = snprintf(filename, ARRAY_SIZE(filename), "gen%i.xml",
951 xml_file_num);
952 assert(len < ARRAY_SIZE(filename));
953
954 return intel_spec_load_common(devinfo->verx10, path, filename);
955 }
956
intel_spec_destroy(struct intel_spec * spec)957 void intel_spec_destroy(struct intel_spec *spec)
958 {
959 ralloc_free(spec);
960 }
961
962 struct intel_group *
intel_spec_find_instruction(struct intel_spec * spec,enum intel_engine_class engine,const uint32_t * p)963 intel_spec_find_instruction(struct intel_spec *spec,
964 enum intel_engine_class engine,
965 const uint32_t *p)
966 {
967 hash_table_foreach(spec->commands, entry) {
968 struct intel_group *command = entry->data;
969 uint32_t opcode = *p & command->opcode_mask;
970 if ((command->engine_mask & INTEL_ENGINE_CLASS_TO_MASK(engine)) &&
971 opcode == command->opcode)
972 return command;
973 }
974
975 return NULL;
976 }
977
978 struct intel_field *
intel_group_find_field(struct intel_group * group,const char * name)979 intel_group_find_field(struct intel_group *group, const char *name)
980 {
981 char path[256];
982 snprintf(path, sizeof(path), "%s/%s", group->name, name);
983
984 struct intel_spec *spec = group->spec;
985 struct hash_entry *entry = _mesa_hash_table_search(spec->access_cache,
986 path);
987 if (entry)
988 return entry->data;
989
990 struct intel_field *field = group->fields;
991 while (field) {
992 if (strcmp(field->name, name) == 0) {
993 _mesa_hash_table_insert(spec->access_cache,
994 ralloc_strdup(spec, path),
995 field);
996 return field;
997 }
998 field = field->next;
999 }
1000
1001 return NULL;
1002 }
1003
1004 int
intel_group_get_length(const struct intel_group * group,const uint32_t * p)1005 intel_group_get_length(const struct intel_group *group, const uint32_t *p)
1006 {
1007 if (group) {
1008 if (group->fixed_length)
1009 return group->dw_length;
1010 else {
1011 struct intel_field *field = group->dword_length_field;
1012 if (field) {
1013 return field_value(p[0], field->start, field->end) + group->bias;
1014 }
1015 }
1016 }
1017
1018 uint32_t h = p[0];
1019 uint32_t type = field_value(h, 29, 31);
1020
1021 switch (type) {
1022 case 0: /* MI */ {
1023 uint32_t opcode = field_value(h, 23, 28);
1024 if (opcode < 16)
1025 return 1;
1026 else
1027 return field_value(h, 0, 7) + 2;
1028 break;
1029 }
1030
1031 case 2: /* BLT */ {
1032 return field_value(h, 0, 7) + 2;
1033 }
1034
1035 case 3: /* Render */ {
1036 uint32_t subtype = field_value(h, 27, 28);
1037 uint32_t opcode = field_value(h, 24, 26);
1038 uint16_t whole_opcode = field_value(h, 16, 31);
1039 switch (subtype) {
1040 case 0:
1041 if (whole_opcode == 0x6104 /* PIPELINE_SELECT_965 */)
1042 return 1;
1043 else if (opcode < 2)
1044 return field_value(h, 0, 7) + 2;
1045 else
1046 return -1;
1047 case 1:
1048 if (opcode < 2)
1049 return 1;
1050 else
1051 return -1;
1052 case 2: {
1053 if (whole_opcode == 0x73A2 /* HCP_PAK_INSERT_OBJECT */)
1054 return field_value(h, 0, 11) + 2;
1055 else if (opcode == 0)
1056 return field_value(h, 0, 7) + 2;
1057 else if (opcode < 3)
1058 return field_value(h, 0, 15) + 2;
1059 else
1060 return -1;
1061 }
1062 case 3:
1063 if (whole_opcode == 0x780b)
1064 return 1;
1065 else if (opcode < 4)
1066 return field_value(h, 0, 7) + 2;
1067 else
1068 return -1;
1069 }
1070 }
1071 }
1072
1073 return -1;
1074 }
1075
1076 static const char *
intel_get_enum_name(const struct intel_enum * e,uint64_t value)1077 intel_get_enum_name(const struct intel_enum *e, uint64_t value)
1078 {
1079 for (int i = 0; i < e->nvalues; i++) {
1080 if (e->values[i]->value == value) {
1081 return e->values[i]->name;
1082 }
1083 }
1084 return NULL;
1085 }
1086
1087 static bool
iter_more_fields(const struct intel_field_iterator * iter)1088 iter_more_fields(const struct intel_field_iterator *iter)
1089 {
1090 return iter->field != NULL && iter->field->next != NULL;
1091 }
1092
1093 static uint32_t
iter_array_offset_bits(const struct intel_field_iterator * iter)1094 iter_array_offset_bits(const struct intel_field_iterator *iter)
1095 {
1096 if (iter->level == 0)
1097 return 0;
1098
1099 uint32_t offset = 0;
1100 const struct intel_group *group = iter->groups[1];
1101 for (int level = 1; level <= iter->level; level++, group = iter->groups[level]) {
1102 uint32_t array_idx = iter->array_iter[level];
1103 offset += group->array_offset + array_idx * group->array_item_size;
1104 }
1105
1106 return offset;
1107 }
1108
1109 /* Checks whether we have more items in the array to iterate, or more arrays to
1110 * iterate through.
1111 */
1112 /* descend into a non-array field */
1113 static void
iter_push_array(struct intel_field_iterator * iter)1114 iter_push_array(struct intel_field_iterator *iter)
1115 {
1116 assert(iter->level >= 0);
1117
1118 iter->group = iter->field->array;
1119 iter->level++;
1120 assert(iter->level < DECODE_MAX_ARRAY_DEPTH);
1121 iter->groups[iter->level] = iter->group;
1122 iter->array_iter[iter->level] = 0;
1123
1124 assert(iter->group->fields != NULL); /* an empty <group> makes no sense */
1125 iter->field = iter->group->fields;
1126 iter->fields[iter->level] = iter->field;
1127 }
1128
1129 static void
iter_pop_array(struct intel_field_iterator * iter)1130 iter_pop_array(struct intel_field_iterator *iter)
1131 {
1132 assert(iter->level > 0);
1133
1134 iter->level--;
1135 iter->field = iter->fields[iter->level];
1136 iter->group = iter->groups[iter->level];
1137 }
1138
1139 static void
iter_start_field(struct intel_field_iterator * iter,struct intel_field * field)1140 iter_start_field(struct intel_field_iterator *iter, struct intel_field *field)
1141 {
1142 iter->field = field;
1143 iter->fields[iter->level] = field;
1144
1145 while (iter->field->array)
1146 iter_push_array(iter);
1147
1148 int array_member_offset = iter_array_offset_bits(iter);
1149
1150 iter->start_bit = array_member_offset + iter->field->start;
1151 iter->end_bit = array_member_offset + iter->field->end;
1152 iter->struct_desc = NULL;
1153 }
1154
1155 static void
iter_advance_array(struct intel_field_iterator * iter)1156 iter_advance_array(struct intel_field_iterator *iter)
1157 {
1158 assert(iter->level > 0);
1159 int lvl = iter->level;
1160
1161 if (iter->group->variable)
1162 iter->array_iter[lvl]++;
1163 else {
1164 if ((iter->array_iter[lvl] + 1) < iter->group->array_count) {
1165 iter->array_iter[lvl]++;
1166 }
1167 }
1168
1169 iter_start_field(iter, iter->group->fields);
1170 }
1171
1172 static bool
iter_more_array_elems(const struct intel_field_iterator * iter)1173 iter_more_array_elems(const struct intel_field_iterator *iter)
1174 {
1175 int lvl = iter->level;
1176 assert(lvl >= 0);
1177
1178 if (iter->group->variable) {
1179 int length = intel_group_get_length(iter->group, iter->p);
1180 assert(length >= 0 && "error the length is unknown!");
1181 return iter_array_offset_bits(iter) + iter->group->array_item_size <
1182 (length * 32);
1183 } else {
1184 return (iter->array_iter[lvl] + 1) < iter->group->array_count;
1185 }
1186 }
1187
1188 static bool
iter_advance_field(struct intel_field_iterator * iter)1189 iter_advance_field(struct intel_field_iterator *iter)
1190 {
1191 /* Keep looping while we either have more fields to look at, or we are
1192 * inside a <group> and can go up a level.
1193 */
1194 while (iter_more_fields(iter) || iter->level > 0) {
1195 if (iter_more_fields(iter)) {
1196 iter_start_field(iter, iter->field->next);
1197 return true;
1198 }
1199
1200 assert(iter->level >= 0);
1201
1202 if (iter_more_array_elems(iter)) {
1203 iter_advance_array(iter);
1204 return true;
1205 }
1206
1207 /* At this point, we reached the end of the <group> and were on the last
1208 * iteration. So it's time to go back to the parent and then advance the
1209 * field.
1210 */
1211 iter_pop_array(iter);
1212 }
1213
1214 return false;
1215 }
1216
1217 static bool
iter_decode_field_raw(struct intel_field_iterator * iter,uint64_t * qw)1218 iter_decode_field_raw(struct intel_field_iterator *iter, uint64_t *qw)
1219 {
1220 *qw = 0;
1221
1222 int field_start = iter->p_bit + iter->start_bit;
1223 int field_end = iter->p_bit + iter->end_bit;
1224
1225 const uint32_t *p = iter->p + (iter->start_bit / 32);
1226 if (iter->p_end && p >= iter->p_end)
1227 return false;
1228
1229 if ((field_end - field_start) > 32) {
1230 if (!iter->p_end || (p + 1) < iter->p_end)
1231 *qw = ((uint64_t) p[1]) << 32;
1232 *qw |= p[0];
1233 } else
1234 *qw = p[0];
1235
1236 *qw = field_value(*qw, field_start, field_end);
1237
1238 /* Address & offset types have to be aligned to dwords, their start bit is
1239 * a reminder of the alignment requirement.
1240 */
1241 if (iter->field->type.kind == INTEL_TYPE_ADDRESS ||
1242 iter->field->type.kind == INTEL_TYPE_OFFSET)
1243 *qw <<= field_start % 32;
1244
1245 return true;
1246 }
1247
1248 static bool
iter_decode_field(struct intel_field_iterator * iter)1249 iter_decode_field(struct intel_field_iterator *iter)
1250 {
1251 union {
1252 uint64_t qw;
1253 float f;
1254 } v;
1255
1256 if (iter->field->name)
1257 snprintf(iter->name, sizeof(iter->name), "%s", iter->field->name);
1258 else
1259 memset(iter->name, 0, sizeof(iter->name));
1260
1261 memset(&v, 0, sizeof(v));
1262
1263 if (!iter_decode_field_raw(iter, &iter->raw_value))
1264 return false;
1265
1266 const char *enum_name = NULL;
1267
1268 v.qw = iter->raw_value;
1269 switch (iter->field->type.kind) {
1270 case INTEL_TYPE_UNKNOWN:
1271 case INTEL_TYPE_INT: {
1272 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
1273 enum_name = intel_get_enum_name(&iter->field->inline_enum, v.qw);
1274 break;
1275 }
1276 case INTEL_TYPE_MBZ:
1277 case INTEL_TYPE_UINT: {
1278 snprintf(iter->value, sizeof(iter->value), "%"PRIu64, v.qw);
1279 enum_name = intel_get_enum_name(&iter->field->inline_enum, v.qw);
1280 break;
1281 }
1282 case INTEL_TYPE_BOOL: {
1283 const char *true_string =
1284 iter->print_colors ? "\e[0;35mtrue\e[0m" : "true";
1285 snprintf(iter->value, sizeof(iter->value), "%s",
1286 v.qw ? true_string : "false");
1287 break;
1288 }
1289 case INTEL_TYPE_FLOAT:
1290 snprintf(iter->value, sizeof(iter->value), "%f", v.f);
1291 break;
1292 case INTEL_TYPE_ADDRESS:
1293 case INTEL_TYPE_OFFSET:
1294 snprintf(iter->value, sizeof(iter->value), "0x%08"PRIx64, v.qw);
1295 break;
1296 case INTEL_TYPE_STRUCT:
1297 snprintf(iter->value, sizeof(iter->value), "<struct %s>",
1298 iter->field->type.intel_struct->name);
1299 iter->struct_desc =
1300 intel_spec_find_struct(iter->group->spec,
1301 iter->field->type.intel_struct->name);
1302 break;
1303 case INTEL_TYPE_UFIXED:
1304 snprintf(iter->value, sizeof(iter->value), "%f",
1305 (float) v.qw / (1 << iter->field->type.f));
1306 break;
1307 case INTEL_TYPE_SFIXED: {
1308 /* Sign extend before converting */
1309 int bits = iter->field->type.i + iter->field->type.f + 1;
1310 int64_t v_sign_extend = util_mask_sign_extend(v.qw, bits);
1311 snprintf(iter->value, sizeof(iter->value), "%f",
1312 (float) v_sign_extend / (1 << iter->field->type.f));
1313 break;
1314 }
1315 case INTEL_TYPE_MBO:
1316 break;
1317 case INTEL_TYPE_ENUM: {
1318 snprintf(iter->value, sizeof(iter->value), "%"PRId64, v.qw);
1319 enum_name = intel_get_enum_name(iter->field->type.intel_enum, v.qw);
1320 break;
1321 }
1322 }
1323
1324 if (strlen(iter->group->name) == 0) {
1325 int length = strlen(iter->name);
1326 assert(iter->level >= 0);
1327
1328 int level = 1;
1329 char *buf = iter->name + length;
1330 while (level <= iter->level) {
1331 int printed = snprintf(buf, sizeof(iter->name) - length,
1332 "[%i]", iter->array_iter[level]);
1333 level++;
1334 length += printed;
1335 buf += printed;
1336 }
1337 }
1338
1339 if (enum_name) {
1340 int length = strlen(iter->value);
1341 snprintf(iter->value + length, sizeof(iter->value) - length,
1342 " (%s)", enum_name);
1343 } else if (strcmp(iter->name, "Surface Format") == 0 ||
1344 strcmp(iter->name, "Source Element Format") == 0) {
1345 if (isl_format_is_valid((enum isl_format)v.qw)) {
1346 const char *fmt_name = isl_format_get_name((enum isl_format)v.qw);
1347 int length = strlen(iter->value);
1348 snprintf(iter->value + length, sizeof(iter->value) - length,
1349 " (%s)", fmt_name);
1350 }
1351 }
1352
1353 return true;
1354 }
1355
1356 void
intel_field_iterator_init(struct intel_field_iterator * iter,const struct intel_group * group,const uint32_t * p,int p_bit,bool print_colors)1357 intel_field_iterator_init(struct intel_field_iterator *iter,
1358 const struct intel_group *group,
1359 const uint32_t *p, int p_bit,
1360 bool print_colors)
1361 {
1362 memset(iter, 0, sizeof(*iter));
1363
1364 iter->groups[iter->level] = group;
1365 iter->group = group;
1366 iter->p = p;
1367 iter->p_bit = p_bit;
1368
1369 int length = intel_group_get_length(iter->group, iter->p);
1370 assert(length >= 0 && "error the length is unknown!");
1371 iter->p_end = length >= 0 ? &p[length] : NULL;
1372 iter->print_colors = print_colors;
1373 }
1374
1375 bool
intel_field_iterator_next(struct intel_field_iterator * iter)1376 intel_field_iterator_next(struct intel_field_iterator *iter)
1377 {
1378 /* Initial condition */
1379 if (!iter->field) {
1380 if (iter->group->fields)
1381 iter_start_field(iter, iter->group->fields);
1382
1383 bool result = iter_decode_field(iter);
1384 if (!result && iter->p_end) {
1385 /* We're dealing with a non empty struct of length=0 (BLEND_STATE on
1386 * Gen 7.5)
1387 */
1388 assert(iter->group->dw_length == 0);
1389 }
1390
1391 return result;
1392 }
1393
1394 if (!iter_advance_field(iter))
1395 return false;
1396
1397 if (!iter_decode_field(iter))
1398 return false;
1399
1400 return true;
1401 }
1402
1403 static void
print_dword_header(FILE * outfile,struct intel_field_iterator * iter,uint64_t offset,uint32_t dword,const char * spacing)1404 print_dword_header(FILE *outfile,
1405 struct intel_field_iterator *iter,
1406 uint64_t offset, uint32_t dword,
1407 const char *spacing)
1408 {
1409 fprintf(outfile, "%s0x%08"PRIx64": 0x%08x : Dword %d\n",
1410 spacing, offset + 4 * dword, iter->p[dword], dword);
1411 }
1412
1413 bool
intel_field_is_header(const struct intel_field * field)1414 intel_field_is_header(const struct intel_field *field)
1415 {
1416 uint32_t bits;
1417
1418 /* Instructions are identified by the first DWord. */
1419 if (field->start >= 32 ||
1420 field->end >= 32)
1421 return false;
1422
1423 bits = (1ULL << (field->end - field->start + 1)) - 1;
1424 bits <<= field->start;
1425
1426 return (field->parent->opcode_mask & bits) != 0;
1427 }
1428
1429 void
intel_print_group_custom_spacing(FILE * outfile,const struct intel_group * group,uint64_t offset,const uint32_t * p,int p_bit,bool color,const char * spacing_reg,const char * spacing_dword)1430 intel_print_group_custom_spacing(FILE *outfile,
1431 const struct intel_group *group, uint64_t offset,
1432 const uint32_t *p, int p_bit, bool color,
1433 const char *spacing_reg, const char *spacing_dword)
1434 {
1435 struct intel_field_iterator iter;
1436 int last_dword = -1;
1437
1438 intel_field_iterator_init(&iter, group, p, p_bit, color);
1439 while (intel_field_iterator_next(&iter)) {
1440 int iter_dword = iter.end_bit / 32;
1441 if (last_dword != iter_dword) {
1442 for (int i = last_dword + 1; i <= iter_dword; i++)
1443 print_dword_header(outfile, &iter, offset, i, spacing_dword);
1444 last_dword = iter_dword;
1445 }
1446 if (!intel_field_is_header(iter.field)) {
1447 fprintf(outfile, "%s%s: %s\n", spacing_reg, iter.name, iter.value);
1448 if (iter.struct_desc) {
1449 int struct_dword = iter.start_bit / 32;
1450 uint64_t struct_offset = offset + 4 * struct_dword;
1451 intel_print_group(outfile, iter.struct_desc, struct_offset,
1452 &p[struct_dword], iter.start_bit % 32, color);
1453 }
1454 }
1455 }
1456 }
1457
1458 void
intel_print_group(FILE * outfile,const struct intel_group * group,uint64_t offset,const uint32_t * p,int p_bit,bool color)1459 intel_print_group(FILE *outfile,
1460 const struct intel_group *group, uint64_t offset,
1461 const uint32_t *p, int p_bit, bool color)
1462 {
1463 const char *spacing_reg = " ";
1464 const char *spacing_dword = "";
1465
1466 intel_print_group_custom_spacing(outfile, group, offset, p, p_bit, color,
1467 spacing_reg, spacing_dword);
1468 }
1469