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