defb0873da0f7112d1115b09f855037bd855d0b4
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
32 #include <util/macros.h>
36 #include "genxml/gen6_xml.h"
37 #include "genxml/gen7_xml.h"
38 #include "genxml/gen75_xml.h"
39 #include "genxml/gen8_xml.h"
40 #include "genxml/gen9_xml.h"
42 #define XML_BUFFER_SIZE 4096
44 #define MAKE_GEN(major, minor) ( ((major) << 8) | (minor) )
50 struct gen_group
*commands
[256];
52 struct gen_group
*structs
[256];
54 struct gen_group
*registers
[256];
56 struct gen_enum
*enums
[256];
64 struct parser_context
{
70 struct gen_group
*group
;
71 struct gen_enum
*enoom
;
74 struct gen_field
*fields
[128];
77 struct gen_value
*values
[256];
79 struct gen_spec
*spec
;
83 gen_group_get_name(struct gen_group
*group
)
89 gen_group_get_opcode(struct gen_group
*group
)
95 gen_spec_find_struct(struct gen_spec
*spec
, const char *name
)
97 for (int i
= 0; i
< spec
->nstructs
; i
++)
98 if (strcmp(spec
->structs
[i
]->name
, name
) == 0)
99 return spec
->structs
[i
];
105 gen_spec_find_register(struct gen_spec
*spec
, uint32_t offset
)
107 for (int i
= 0; i
< spec
->nregisters
; i
++)
108 if (spec
->registers
[i
]->register_offset
== offset
)
109 return spec
->registers
[i
];
115 gen_spec_find_enum(struct gen_spec
*spec
, const char *name
)
117 for (int i
= 0; i
< spec
->nenums
; i
++)
118 if (strcmp(spec
->enums
[i
]->name
, name
) == 0)
119 return spec
->enums
[i
];
125 gen_spec_get_gen(struct gen_spec
*spec
)
130 static void __attribute__((noreturn
))
131 fail(struct location
*loc
, const char *msg
, ...)
136 fprintf(stderr
, "%s:%d: error: ",
137 loc
->filename
, loc
->line_number
);
138 vfprintf(stderr
, msg
, ap
);
139 fprintf(stderr
, "\n");
145 fail_on_null(void *p
)
148 fprintf(stderr
, "aubinator: out of memory\n");
156 xstrdup(const char *s
)
158 return fail_on_null(strdup(s
));
170 return fail_on_null(zalloc(s
));
173 static struct gen_group
*
174 create_group(struct parser_context
*ctx
, const char *name
, const char **atts
)
176 struct gen_group
*group
;
178 group
= xzalloc(sizeof(*group
));
180 group
->name
= xstrdup(name
);
182 group
->group_offset
= 0;
183 group
->group_count
= 0;
188 static struct gen_enum
*
189 create_enum(struct parser_context
*ctx
, const char *name
, const char **atts
)
193 e
= xzalloc(sizeof(*e
));
195 e
->name
= xstrdup(name
);
203 get_group_offset_count(struct parser_context
*ctx
, const char *name
,
204 const char **atts
, uint32_t *offset
, uint32_t *count
)
209 for (i
= 0; atts
[i
]; i
+= 2) {
210 if (strcmp(atts
[i
], "count") == 0)
211 *count
= strtoul(atts
[i
+ 1], &p
, 0);
212 else if (strcmp(atts
[i
], "start") == 0)
213 *offset
= strtoul(atts
[i
+ 1], &p
, 0);
219 get_register_offset(const char **atts
, uint32_t *offset
)
224 for (i
= 0; atts
[i
]; i
+= 2) {
225 if (strcmp(atts
[i
], "num") == 0)
226 *offset
= strtoul(atts
[i
+ 1], &p
, 0);
232 get_start_end_pos(int *start
, int *end
)
234 /* start value has to be mod with 32 as we need the relative
235 * start position in the first DWord. For the end position, add
236 * the length of the field to the start position to get the
237 * relative postion in the 64 bit address.
239 if (*end
- *start
> 32) {
240 int len
= *end
- *start
;
241 *start
= *start
% 32;
244 *start
= *start
% 32;
251 static inline uint64_t
252 mask(int start
, int end
)
256 v
= ~0ULL >> (63 - end
+ start
);
261 static inline uint64_t
262 field(uint64_t value
, int start
, int end
)
264 get_start_end_pos(&start
, &end
);
265 return (value
& mask(start
, end
)) >> (start
);
268 static inline uint64_t
269 field_address(uint64_t value
, int start
, int end
)
271 /* no need to right shift for address/offset */
272 get_start_end_pos(&start
, &end
);
273 return (value
& mask(start
, end
));
276 static struct gen_type
277 string_to_type(struct parser_context
*ctx
, const char *s
)
283 if (strcmp(s
, "int") == 0)
284 return (struct gen_type
) { .kind
= GEN_TYPE_INT
};
285 else if (strcmp(s
, "uint") == 0)
286 return (struct gen_type
) { .kind
= GEN_TYPE_UINT
};
287 else if (strcmp(s
, "bool") == 0)
288 return (struct gen_type
) { .kind
= GEN_TYPE_BOOL
};
289 else if (strcmp(s
, "float") == 0)
290 return (struct gen_type
) { .kind
= GEN_TYPE_FLOAT
};
291 else if (strcmp(s
, "address") == 0)
292 return (struct gen_type
) { .kind
= GEN_TYPE_ADDRESS
};
293 else if (strcmp(s
, "offset") == 0)
294 return (struct gen_type
) { .kind
= GEN_TYPE_OFFSET
};
295 else if (sscanf(s
, "u%d.%d", &i
, &f
) == 2)
296 return (struct gen_type
) { .kind
= GEN_TYPE_UFIXED
, .i
= i
, .f
= f
};
297 else if (sscanf(s
, "s%d.%d", &i
, &f
) == 2)
298 return (struct gen_type
) { .kind
= GEN_TYPE_SFIXED
, .i
= i
, .f
= f
};
299 else if (g
= gen_spec_find_struct(ctx
->spec
, s
), g
!= NULL
)
300 return (struct gen_type
) { .kind
= GEN_TYPE_STRUCT
, .gen_struct
= g
};
301 else if (e
= gen_spec_find_enum(ctx
->spec
, s
), e
!= NULL
)
302 return (struct gen_type
) { .kind
= GEN_TYPE_ENUM
, .gen_enum
= e
};
303 else if (strcmp(s
, "mbo") == 0)
304 return (struct gen_type
) { .kind
= GEN_TYPE_MBO
};
306 fail(&ctx
->loc
, "invalid type: %s", s
);
309 static struct gen_field
*
310 create_field(struct parser_context
*ctx
, const char **atts
)
312 struct gen_field
*field
;
316 field
= xzalloc(sizeof(*field
));
318 for (i
= 0; atts
[i
]; i
+= 2) {
319 if (strcmp(atts
[i
], "name") == 0)
320 field
->name
= xstrdup(atts
[i
+ 1]);
321 else if (strcmp(atts
[i
], "start") == 0)
322 field
->start
= ctx
->group
->group_offset
+strtoul(atts
[i
+ 1], &p
, 0);
323 else if (strcmp(atts
[i
], "end") == 0) {
324 field
->end
= ctx
->group
->group_offset
+strtoul(atts
[i
+ 1], &p
, 0);
325 if (ctx
->group
->group_offset
)
326 ctx
->group
->group_offset
= field
->end
+1;
327 } else if (strcmp(atts
[i
], "type") == 0)
328 field
->type
= string_to_type(ctx
, atts
[i
+ 1]);
329 else if (strcmp(atts
[i
], "default") == 0 &&
330 field
->start
>= 16 && field
->end
<= 31) {
331 field
->has_default
= true;
332 field
->default_value
= strtoul(atts
[i
+ 1], &p
, 0);
339 static struct gen_value
*
340 create_value(struct parser_context
*ctx
, const char **atts
)
342 struct gen_value
*value
= xzalloc(sizeof(*value
));
344 for (int i
= 0; atts
[i
]; i
+= 2) {
345 if (strcmp(atts
[i
], "name") == 0)
346 value
->name
= xstrdup(atts
[i
+ 1]);
347 else if (strcmp(atts
[i
], "value") == 0)
348 value
->value
= strtoul(atts
[i
+ 1], NULL
, 0);
355 start_element(void *data
, const char *element_name
, const char **atts
)
357 struct parser_context
*ctx
= data
;
359 const char *name
= NULL
;
360 const char *gen
= NULL
;
362 ctx
->loc
.line_number
= XML_GetCurrentLineNumber(ctx
->parser
);
364 for (i
= 0; atts
[i
]; i
+= 2) {
365 if (strcmp(atts
[i
], "name") == 0)
367 else if (strcmp(atts
[i
], "gen") == 0)
371 if (strcmp(element_name
, "genxml") == 0) {
373 fail(&ctx
->loc
, "no platform name given");
375 fail(&ctx
->loc
, "no gen given");
377 ctx
->platform
= xstrdup(name
);
379 int n
= sscanf(gen
, "%d.%d", &major
, &minor
);
381 fail(&ctx
->loc
, "invalid gen given: %s", gen
);
385 ctx
->spec
->gen
= MAKE_GEN(major
, minor
);
386 } else if (strcmp(element_name
, "instruction") == 0 ||
387 strcmp(element_name
, "struct") == 0) {
388 ctx
->group
= create_group(ctx
, name
, atts
);
389 } else if (strcmp(element_name
, "register") == 0) {
390 ctx
->group
= create_group(ctx
, name
, atts
);
391 get_register_offset(atts
, &ctx
->group
->register_offset
);
392 } else if (strcmp(element_name
, "group") == 0) {
393 get_group_offset_count(ctx
, name
, atts
, &ctx
->group
->group_offset
,
394 &ctx
->group
->group_count
);
395 } else if (strcmp(element_name
, "field") == 0) {
397 ctx
->fields
[ctx
->nfields
++] = create_field(ctx
, atts
);
398 if (ctx
->group
->group_count
)
399 ctx
->group
->group_count
--;
400 } while (ctx
->group
->group_count
> 0);
401 } else if (strcmp(element_name
, "enum") == 0) {
402 ctx
->enoom
= create_enum(ctx
, name
, atts
);
403 } else if (strcmp(element_name
, "value") == 0) {
404 ctx
->values
[ctx
->nvalues
++] = create_value(ctx
, atts
);
409 end_element(void *data
, const char *name
)
411 struct parser_context
*ctx
= data
;
412 struct gen_spec
*spec
= ctx
->spec
;
414 if (strcmp(name
, "instruction") == 0 ||
415 strcmp(name
, "struct") == 0 ||
416 strcmp(name
, "register") == 0) {
417 size_t size
= ctx
->nfields
* sizeof(ctx
->fields
[0]);
418 struct gen_group
*group
= ctx
->group
;
420 group
->fields
= xzalloc(size
);
421 group
->nfields
= ctx
->nfields
;
422 memcpy(group
->fields
, ctx
->fields
, size
);
426 for (int i
= 0; i
< group
->nfields
; i
++) {
427 if (group
->fields
[i
]->start
>= 16 &&
428 group
->fields
[i
]->end
<= 31 &&
429 group
->fields
[i
]->has_default
) {
430 group
->opcode_mask
|=
431 mask(group
->fields
[i
]->start
% 32, group
->fields
[i
]->end
% 32);
433 group
->fields
[i
]->default_value
<< group
->fields
[i
]->start
;
437 if (strcmp(name
, "instruction") == 0)
438 spec
->commands
[spec
->ncommands
++] = group
;
439 else if (strcmp(name
, "struct") == 0)
440 spec
->structs
[spec
->nstructs
++] = group
;
441 else if (strcmp(name
, "register") == 0)
442 spec
->registers
[spec
->nregisters
++] = group
;
443 } else if (strcmp(name
, "group") == 0) {
444 ctx
->group
->group_offset
= 0;
445 ctx
->group
->group_count
= 0;
446 } else if (strcmp(name
, "field") == 0) {
447 assert(ctx
->nfields
> 0);
448 struct gen_field
*field
= ctx
->fields
[ctx
->nfields
- 1];
449 size_t size
= ctx
->nvalues
* sizeof(ctx
->values
[0]);
450 field
->inline_enum
.values
= xzalloc(size
);
451 field
->inline_enum
.nvalues
= ctx
->nvalues
;
452 memcpy(field
->inline_enum
.values
, ctx
->values
, size
);
454 } else if (strcmp(name
, "enum") == 0) {
455 struct gen_enum
*e
= ctx
->enoom
;
456 size_t size
= ctx
->nvalues
* sizeof(ctx
->values
[0]);
457 e
->values
= xzalloc(size
);
458 e
->nvalues
= ctx
->nvalues
;
459 memcpy(e
->values
, ctx
->values
, size
);
462 spec
->enums
[spec
->nenums
++] = e
;
467 character_data(void *data
, const XML_Char
*s
, int len
)
472 devinfo_to_gen(const struct gen_device_info
*devinfo
)
474 int value
= 10 * devinfo
->gen
;
476 if (devinfo
->is_baytrail
|| devinfo
->is_haswell
)
482 static const struct {
487 { .gen
= 60, .data
= gen6_xml
, .data_length
= sizeof(gen6_xml
) },
488 { .gen
= 70, .data
= gen7_xml
, .data_length
= sizeof(gen7_xml
) },
489 { .gen
= 75, .data
= gen75_xml
, .data_length
= sizeof(gen75_xml
) },
490 { .gen
= 80, .data
= gen8_xml
, .data_length
= sizeof(gen8_xml
) },
491 { .gen
= 90, .data
= gen9_xml
, .data_length
= sizeof(gen9_xml
) }
494 static const uint8_t *
495 devinfo_to_xml_data(const struct gen_device_info
*devinfo
,
496 uint32_t *data_length
)
498 int i
, gen
= devinfo_to_gen(devinfo
);
500 for (i
= 0; i
< ARRAY_SIZE(gen_data
); i
++) {
501 if (gen_data
[i
].gen
== gen
) {
502 *data_length
= gen_data
[i
].data_length
;
503 return gen_data
[i
].data
;
507 unreachable("Unknown generation");
512 gen_spec_load(const struct gen_device_info
*devinfo
)
514 struct parser_context ctx
;
517 uint32_t data_length
= 0;
519 memset(&ctx
, 0, sizeof ctx
);
520 ctx
.parser
= XML_ParserCreate(NULL
);
521 XML_SetUserData(ctx
.parser
, &ctx
);
522 if (ctx
.parser
== NULL
) {
523 fprintf(stderr
, "failed to create parser\n");
527 XML_SetElementHandler(ctx
.parser
, start_element
, end_element
);
528 XML_SetCharacterDataHandler(ctx
.parser
, character_data
);
530 ctx
.spec
= xzalloc(sizeof(*ctx
.spec
));
532 data
= devinfo_to_xml_data(devinfo
, &data_length
);
533 buf
= XML_GetBuffer(ctx
.parser
, data_length
);
535 memcpy(buf
, data
, data_length
);
537 if (XML_ParseBuffer(ctx
.parser
, data_length
, true) == 0) {
539 "Error parsing XML at line %ld col %ld: %s\n",
540 XML_GetCurrentLineNumber(ctx
.parser
),
541 XML_GetCurrentColumnNumber(ctx
.parser
),
542 XML_ErrorString(XML_GetErrorCode(ctx
.parser
)));
543 XML_ParserFree(ctx
.parser
);
547 XML_ParserFree(ctx
.parser
);
553 gen_spec_load_from_path(const struct gen_device_info
*devinfo
,
556 struct parser_context ctx
;
557 size_t len
, filename_len
= strlen(path
) + 20;
558 char *filename
= malloc(filename_len
);
562 len
= snprintf(filename
, filename_len
, "%s/gen%i.xml",
563 path
, devinfo_to_gen(devinfo
));
564 assert(len
< filename_len
);
566 input
= fopen(filename
, "r");
568 fprintf(stderr
, "failed to open xml description\n");
573 memset(&ctx
, 0, sizeof ctx
);
574 ctx
.parser
= XML_ParserCreate(NULL
);
575 XML_SetUserData(ctx
.parser
, &ctx
);
576 if (ctx
.parser
== NULL
) {
577 fprintf(stderr
, "failed to create parser\n");
583 XML_SetElementHandler(ctx
.parser
, start_element
, end_element
);
584 XML_SetCharacterDataHandler(ctx
.parser
, character_data
);
585 ctx
.loc
.filename
= filename
;
586 ctx
.spec
= xzalloc(sizeof(*ctx
.spec
));
589 buf
= XML_GetBuffer(ctx
.parser
, XML_BUFFER_SIZE
);
590 len
= fread(buf
, 1, XML_BUFFER_SIZE
, input
);
592 fprintf(stderr
, "fread: %m\n");
597 if (XML_ParseBuffer(ctx
.parser
, len
, len
== 0) == 0) {
599 "Error parsing XML at line %ld col %ld: %s\n",
600 XML_GetCurrentLineNumber(ctx
.parser
),
601 XML_GetCurrentColumnNumber(ctx
.parser
),
602 XML_ErrorString(XML_GetErrorCode(ctx
.parser
)));
609 XML_ParserFree(ctx
.parser
);
618 gen_spec_find_instruction(struct gen_spec
*spec
, const uint32_t *p
)
620 for (int i
= 0; i
< spec
->ncommands
; i
++) {
621 uint32_t opcode
= *p
& spec
->commands
[i
]->opcode_mask
;
622 if (opcode
== spec
->commands
[i
]->opcode
)
623 return spec
->commands
[i
];
630 gen_group_get_length(struct gen_group
*group
, const uint32_t *p
)
633 uint32_t type
= field(h
, 29, 31);
637 uint32_t opcode
= field(h
, 23, 28);
641 return field(h
, 0, 7) + 2;
645 case 3: /* Render */ {
646 uint32_t subtype
= field(h
, 27, 28);
649 return field(h
, 0, 7) + 2;
655 return field(h
, 0, 7) + 2;
660 unreachable("bad opcode");
664 gen_field_iterator_init(struct gen_field_iterator
*iter
,
665 struct gen_group
*group
,
672 iter
->print_colors
= print_colors
;
676 gen_enum_write_value(char *str
, size_t max_length
,
677 struct gen_enum
*e
, uint64_t value
)
679 for (int i
= 0; i
< e
->nvalues
; i
++) {
680 if (e
->values
[i
]->value
== value
) {
681 strncpy(str
, e
->values
[i
]->name
, max_length
);
688 gen_field_iterator_next(struct gen_field_iterator
*iter
)
696 if (iter
->i
== iter
->group
->nfields
)
699 f
= iter
->group
->fields
[iter
->i
++];
700 iter
->name
= f
->name
;
701 int index
= f
->start
/ 32;
703 if ((f
->end
- f
->start
) > 32)
704 v
.qw
= ((uint64_t) iter
->p
[index
+1] << 32) | iter
->p
[index
];
706 v
.qw
= iter
->p
[index
];
708 iter
->description
[0] = '\0';
710 switch (f
->type
.kind
) {
711 case GEN_TYPE_UNKNOWN
:
713 uint64_t value
= field(v
.qw
, f
->start
, f
->end
);
714 snprintf(iter
->value
, sizeof(iter
->value
),
716 gen_enum_write_value(iter
->description
, sizeof(iter
->description
),
717 &f
->inline_enum
, value
);
720 case GEN_TYPE_UINT
: {
721 uint64_t value
= field(v
.qw
, f
->start
, f
->end
);
722 snprintf(iter
->value
, sizeof(iter
->value
),
724 gen_enum_write_value(iter
->description
, sizeof(iter
->description
),
725 &f
->inline_enum
, value
);
728 case GEN_TYPE_BOOL
: {
729 const char *true_string
=
730 iter
->print_colors
? "\e[0;35mtrue\e[0m" : "true";
731 snprintf(iter
->value
, sizeof(iter
->value
),
732 "%s", field(v
.qw
, f
->start
, f
->end
) ? true_string
: "false");
736 snprintf(iter
->value
, sizeof(iter
->value
), "%f", v
.f
);
738 case GEN_TYPE_ADDRESS
:
739 case GEN_TYPE_OFFSET
:
740 snprintf(iter
->value
, sizeof(iter
->value
),
741 "0x%08"PRIx64
, field_address(v
.qw
, f
->start
, f
->end
));
743 case GEN_TYPE_STRUCT
:
744 snprintf(iter
->value
, sizeof(iter
->value
),
745 "<struct %s %d>", f
->type
.gen_struct
->name
, (f
->start
/ 32));
747 case GEN_TYPE_UFIXED
:
748 snprintf(iter
->value
, sizeof(iter
->value
),
749 "%f", (float) field(v
.qw
, f
->start
, f
->end
) / (1 << f
->type
.f
));
751 case GEN_TYPE_SFIXED
:
752 /* FIXME: Sign extend extracted field. */
753 snprintf(iter
->value
, sizeof(iter
->value
), "%s", "foo");
757 case GEN_TYPE_ENUM
: {
758 uint64_t value
= field(v
.qw
, f
->start
, f
->end
);
759 snprintf(iter
->value
, sizeof(iter
->value
),
761 gen_enum_write_value(iter
->description
, sizeof(iter
->description
),
762 f
->type
.gen_enum
, value
);