panfrost: Add optional opaque packs to GenXML
[mesa.git] / src / panfrost / lib / gen_pack.py
1 #encoding=utf-8
2
3 # Copyright (C) 2016 Intel Corporation
4 # Copyright (C) 2016 Broadcom
5 # Copyright (C) 2020 Collabora, Ltd.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a
8 # copy of this software and associated documentation files (the "Software"),
9 # to deal in the Software without restriction, including without limitation
10 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 # and/or sell copies of the Software, and to permit persons to whom the
12 # Software is furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice (including the next
15 # paragraph) shall be included in all copies or substantial portions of the
16 # Software.
17 #
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 # IN THE SOFTWARE.
25
26 import xml.parsers.expat
27 import sys
28 import operator
29 from functools import reduce
30
31 global_prefix = "mali"
32
33 pack_header = """
34 /* Generated code, see midgard.xml and gen_pack_header.py
35 *
36 * Packets, enums and structures for Panfrost.
37 *
38 * This file has been generated, do not hand edit.
39 */
40
41 #ifndef PAN_PACK_H
42 #define PAN_PACK_H
43
44 #include <stdio.h>
45 #include <stdint.h>
46 #include <stdbool.h>
47 #include <assert.h>
48 #include <math.h>
49 #include <inttypes.h>
50 #include "util/u_math.h"
51
52 #define __gen_unpack_float(x, y, z) uif(__gen_unpack_uint(x, y, z))
53
54 static inline uint64_t
55 __gen_uint(uint64_t v, uint32_t start, uint32_t end)
56 {
57 #ifndef NDEBUG
58 const int width = end - start + 1;
59 if (width < 64) {
60 const uint64_t max = (1ull << width) - 1;
61 assert(v <= max);
62 }
63 #endif
64
65 return v << start;
66 }
67
68 static inline uint32_t
69 __gen_sint(int32_t v, uint32_t start, uint32_t end)
70 {
71 const int width = end - start + 1;
72
73 #ifndef NDEBUG
74 if (width < 64) {
75 const int64_t max = (1ll << (width - 1)) - 1;
76 const int64_t min = -(1ll << (width - 1));
77 assert(min <= v && v <= max);
78 }
79 #endif
80
81 return ((uint32_t) v) << start;
82 }
83
84 static inline uint64_t
85 __gen_unpack_uint(const uint8_t *restrict cl, uint32_t start, uint32_t end)
86 {
87 uint64_t val = 0;
88 const int width = end - start + 1;
89 const uint32_t mask = (width == 32 ? ~0 : (1 << width) - 1 );
90
91 for (int byte = start / 8; byte <= end / 8; byte++) {
92 val |= cl[byte] << ((byte - start / 8) * 8);
93 }
94
95 return (val >> (start % 8)) & mask;
96 }
97
98 static inline uint64_t
99 __gen_unpack_sint(const uint8_t *restrict cl, uint32_t start, uint32_t end)
100 {
101 int size = end - start + 1;
102 int64_t val = __gen_unpack_uint(cl, start, end);
103
104 /* Get the sign bit extended. */
105 return (val << (64 - size)) >> (64 - size);
106 }
107
108 #define pan_pack(dst, T, name) \
109 for (struct MALI_ ## T name = { MALI_ ## T ## _header }, \
110 *_loop_terminate = (void *) (dst); \
111 __builtin_expect(_loop_terminate != NULL, 1); \
112 ({ MALI_ ## T ## _pack((uint32_t *) (dst), &name); \
113 _loop_terminate = NULL; }))
114
115 """
116
117 def to_alphanum(name):
118 substitutions = {
119 ' ': '_',
120 '/': '_',
121 '[': '',
122 ']': '',
123 '(': '',
124 ')': '',
125 '-': '_',
126 ':': '',
127 '.': '',
128 ',': '',
129 '=': '',
130 '>': '',
131 '#': '',
132 '&': '',
133 '*': '',
134 '"': '',
135 '+': '',
136 '\'': '',
137 }
138
139 for i, j in substitutions.items():
140 name = name.replace(i, j)
141
142 return name
143
144 def safe_name(name):
145 name = to_alphanum(name)
146 if not name[0].isalpha():
147 name = '_' + name
148
149 return name
150
151 def prefixed_upper_name(prefix, name):
152 if prefix:
153 name = prefix + "_" + name
154 return safe_name(name).upper()
155
156 def enum_name(name):
157 return "{}_{}".format(global_prefix, safe_name(name)).lower()
158
159 def num_from_str(num_str):
160 if num_str.lower().startswith('0x'):
161 return int(num_str, base=16)
162 else:
163 assert(not num_str.startswith('0') and 'octals numbers not allowed')
164 return int(num_str)
165
166 MODIFIERS = ["shr", "minus"]
167
168 def parse_modifier(modifier):
169 if modifier is None:
170 return None
171
172 for mod in MODIFIERS:
173 if modifier[0:len(mod)] == mod and modifier[len(mod)] == '(' and modifier[-1] == ')':
174 return [mod, int(modifier[(len(mod) + 1):-1])]
175
176 print("Invalid modifier")
177 assert(False)
178
179 class Field(object):
180 def __init__(self, parser, attrs):
181 self.parser = parser
182 if "name" in attrs:
183 self.name = safe_name(attrs["name"]).lower()
184 self.human_name = attrs["name"]
185
186 if ":" in str(attrs["start"]):
187 (word, bit) = attrs["start"].split(":")
188 self.start = (int(word) * 32) + int(bit)
189 else:
190 self.start = int(attrs["start"])
191
192 self.end = self.start + int(attrs["size"]) - 1
193 self.type = attrs["type"]
194
195 if self.type == 'bool' and self.start != self.end:
196 print("#error Field {} has bool type but more than one bit of size".format(self.name));
197
198 if "prefix" in attrs:
199 self.prefix = safe_name(attrs["prefix"]).upper()
200 else:
201 self.prefix = None
202
203 if "exact" in attrs:
204 self.exact = int(attrs["exact"])
205 else:
206 self.exact = None
207
208 self.default = attrs.get("default")
209
210 # Map enum values
211 if self.type in self.parser.enums and self.default is not None:
212 self.default = safe_name('{}_{}_{}'.format(global_prefix, self.type, self.default)).upper()
213
214 self.modifier = parse_modifier(attrs.get("modifier"))
215
216 def emit_template_struct(self, dim, opaque_structs):
217 if self.type == 'address':
218 type = 'uint64_t'
219 elif self.type == 'bool':
220 type = 'bool'
221 elif self.type == 'float':
222 type = 'float'
223 elif self.type == 'uint' and self.end - self.start > 32:
224 type = 'uint64_t'
225 elif self.type == 'int':
226 type = 'int32_t'
227 elif self.type == 'uint':
228 type = 'uint32_t'
229 elif self.type in self.parser.structs:
230 type = 'struct ' + self.parser.gen_prefix(safe_name(self.type.upper()))
231
232 if opaque_structs:
233 type = type.lower() + '_packed'
234 elif self.type in self.parser.enums:
235 type = 'enum ' + enum_name(self.type)
236 else:
237 print("#error unhandled type: %s" % self.type)
238 type = "uint32_t"
239
240 print(" %-36s %s%s;" % (type, self.name, dim))
241
242 for value in self.values:
243 name = prefixed_upper_name(self.prefix, value.name)
244 print("#define %-40s %d" % (name, value.value))
245
246 def overlaps(self, field):
247 return self != field and max(self.start, field.start) <= min(self.end, field.end)
248
249
250 class Group(object):
251 def __init__(self, parser, parent, start, count):
252 self.parser = parser
253 self.parent = parent
254 self.start = start
255 self.count = count
256 self.size = 0
257 self.length = 0
258 self.fields = []
259
260 def emit_template_struct(self, dim, opaque_structs):
261 if self.count == 0:
262 print(" /* variable length fields follow */")
263 else:
264 if self.count > 1:
265 dim = "%s[%d]" % (dim, self.count)
266
267 for field in self.fields:
268 if field.exact is not None:
269 continue
270
271 field.emit_template_struct(dim, opaque_structs)
272
273 class Word:
274 def __init__(self):
275 self.size = 32
276 self.fields = []
277
278 def collect_words(self, words):
279 for field in self.fields:
280 first_word = field.start // 32
281 last_word = field.end // 32
282
283 for b in range(first_word, last_word + 1):
284 if not b in words:
285 words[b] = self.Word()
286
287 words[b].fields.append(field)
288
289 def emit_pack_function(self, opaque_structs):
290 # Determine number of bytes in this group.
291 calculated = max(field.end // 8 for field in self.fields) + 1
292
293 if self.length > 0:
294 assert(self.length >= calculated)
295 else:
296 self.length = calculated
297
298 words = {}
299 self.collect_words(words)
300
301 emitted_structs = set()
302
303 # Validate the modifier is lossless
304 for field in self.fields:
305 if field.modifier is None:
306 continue
307
308 assert(field.exact is None)
309
310 if field.modifier[0] == "shr":
311 shift = field.modifier[1]
312 mask = hex((1 << shift) - 1)
313 print(" assert((values->{} & {}) == 0);".format(field.name, mask))
314 elif field.modifier[0] == "minus":
315 print(" assert(values->{} >= {});".format(field.name, field.modifier[1]))
316
317 for index in range(self.length // 4):
318 # Handle MBZ words
319 if not index in words:
320 print(" cl[%2d] = 0;" % index)
321 continue
322
323 word = words[index]
324
325 word_start = index * 32
326
327 v = None
328 prefix = " cl[%2d] =" % index
329
330 first = word.fields[0]
331 if first.type in self.parser.structs and first.start not in emitted_structs:
332 pack_name = self.parser.gen_prefix(safe_name(first.type.upper()))
333 start = first.start
334 assert((first.start % 32) == 0)
335 assert(first.end == first.start + (self.parser.structs[first.type].length * 8) - 1)
336 emitted_structs.add(first.start)
337
338 if opaque_structs:
339 print(" memcpy(cl + {}, &values->{}, {});".format(first.start // 32, first.name, (first.end - first.start + 1) // 8))
340 else:
341 print(" {}_pack(cl + {}, &values->{});".format(pack_name, first.start // 32, first.name))
342
343 for field in word.fields:
344 name = field.name
345 start = field.start
346 end = field.end
347 field_word_start = (field.start // 32) * 32
348 start -= field_word_start
349 end -= field_word_start
350
351 value = str(field.exact) if field.exact is not None else "values->%s" % name
352 if field.modifier is not None:
353 if field.modifier[0] == "shr":
354 value = "{} >> {}".format(value, field.modifier[1])
355 elif field.modifier[0] == "minus":
356 value = "{} - {}".format(value, field.modifier[1])
357
358 if field.type == "uint" or field.type == "address":
359 s = "__gen_uint(%s, %d, %d)" % \
360 (value, start, end)
361 elif field.type in self.parser.enums:
362 s = "__gen_uint(%s, %d, %d)" % \
363 (value, start, end)
364 elif field.type == "int":
365 s = "__gen_sint(%s, %d, %d)" % \
366 (value, start, end)
367 elif field.type == "bool":
368 s = "__gen_uint(%s, %d, %d)" % \
369 (value, start, end)
370 elif field.type == "float":
371 assert(start == 0 and end == 31)
372 s = "__gen_uint(fui({}), 0, 32)".format(value)
373 elif field.type in self.parser.structs:
374 # Structs are packed directly
375 assert(len(word.fields) == 1)
376 continue
377 else:
378 s = "#error unhandled field {}, type {}".format(name, field.type)
379
380 if not s == None:
381 shift = word_start - field_word_start
382 if shift:
383 s = "%s >> %d" % (s, shift)
384
385 if field == word.fields[-1]:
386 print("%s %s;" % (prefix, s))
387 else:
388 print("%s %s |" % (prefix, s))
389 prefix = " "
390
391 continue
392
393 # Given a field (start, end) contained in word `index`, generate the 32-bit
394 # mask of present bits relative to the word
395 def mask_for_word(self, index, start, end):
396 field_word_start = index * 32
397 start -= field_word_start
398 end -= field_word_start
399 # Cap multiword at one word
400 start = max(start, 0)
401 end = min(end, 32 - 1)
402 count = (end - start + 1)
403 return (((1 << count) - 1) << start)
404
405 def emit_unpack_function(self):
406 # First, verify there is no garbage in unused bits
407 words = {}
408 self.collect_words(words)
409
410 for index in range(self.length // 4):
411 base = index * 32
412 word = words.get(index, self.Word())
413 masks = [self.mask_for_word(index, f.start, f.end) for f in word.fields]
414 mask = reduce(lambda x,y: x | y, masks, 0)
415
416 ALL_ONES = 0xffffffff
417
418 if mask != ALL_ONES:
419 TMPL = ' if (((const uint32_t *) cl)[{}] & {}) fprintf(stderr, "XXX: Invalid field unpacked at word {}\\n");'
420 print(TMPL.format(index, hex(mask ^ ALL_ONES), index))
421
422 for field in self.fields:
423 # Recurse for structs, see pack() for validation
424 if field.type in self.parser.structs:
425 pack_name = self.parser.gen_prefix(safe_name(field.type)).upper()
426 print(" {}_unpack(cl + {}, &values->{});".format(pack_name, field.start // 8, field.name))
427 continue
428
429 convert = None
430
431 args = []
432 args.append('cl')
433 args.append(str(field.start))
434 args.append(str(field.end))
435
436 if field.type in set(["uint", "address"]) | self.parser.enums:
437 convert = "__gen_unpack_uint"
438 elif field.type == "int":
439 convert = "__gen_unpack_sint"
440 elif field.type == "bool":
441 convert = "__gen_unpack_uint"
442 elif field.type == "float":
443 convert = "__gen_unpack_float"
444 else:
445 s = "/* unhandled field %s, type %s */\n" % (field.name, field.type)
446
447 suffix = ""
448 if field.modifier:
449 if field.modifier[0] == "minus":
450 suffix = " + {}".format(field.modifier[1])
451 elif field.modifier[0] == "shr":
452 suffix = " << {}".format(field.modifier[1])
453
454 decoded = '{}({}){}'.format(convert, ', '.join(args), suffix)
455
456 print(' values->{} = {};'.format(field.name, decoded))
457
458 def emit_print_function(self):
459 for field in self.fields:
460 convert = None
461 name, val = field.human_name, 'values->{}'.format(field.name)
462
463 if field.type in self.parser.structs:
464 pack_name = self.parser.gen_prefix(safe_name(field.type)).upper()
465 print(' fprintf(fp, "%*s{}:\\n", indent, "");'.format(field.human_name))
466 print(" {}_print(fp, &values->{}, indent + 2);".format(pack_name, field.name))
467 elif field.type == "address":
468 # TODO resolve to name
469 print(' fprintf(fp, "%*s{}: 0x%" PRIx64 "\\n", indent, "", {});'.format(name, val))
470 elif field.type in self.parser.enums:
471 print(' fprintf(fp, "%*s{}: %s\\n", indent, "", {}_as_str({}));'.format(name, enum_name(field.type), val))
472 elif field.type == "int":
473 print(' fprintf(fp, "%*s{}: %d\\n", indent, "", {});'.format(name, val))
474 elif field.type == "bool":
475 print(' fprintf(fp, "%*s{}: %s\\n", indent, "", {} ? "true" : "false");'.format(name, val))
476 elif field.type == "float":
477 print(' fprintf(fp, "%*s{}: %f\\n", indent, "", {});'.format(name, val))
478 elif field.type == "uint" and (field.end - field.start) >= 32:
479 print(' fprintf(fp, "%*s{}: 0x%" PRIx64 "\\n", indent, "", {});'.format(name, val))
480 else:
481 print(' fprintf(fp, "%*s{}: %u\\n", indent, "", {});'.format(name, val))
482
483 class Value(object):
484 def __init__(self, attrs):
485 self.name = attrs["name"]
486 self.value = int(attrs["value"])
487
488 class Parser(object):
489 def __init__(self):
490 self.parser = xml.parsers.expat.ParserCreate()
491 self.parser.StartElementHandler = self.start_element
492 self.parser.EndElementHandler = self.end_element
493
494 self.struct = None
495 self.structs = {}
496 # Set of enum names we've seen.
497 self.enums = set()
498
499 def gen_prefix(self, name):
500 return '{}_{}'.format(global_prefix.upper(), name)
501
502 def start_element(self, name, attrs):
503 if name == "panxml":
504 print(pack_header)
505 elif name == "struct":
506 name = attrs["name"]
507 self.with_opaque = attrs.get("with_opaque", False)
508
509 object_name = self.gen_prefix(safe_name(name.upper()))
510 self.struct = object_name
511
512 self.group = Group(self, None, 0, 1)
513 if "size" in attrs:
514 self.group.length = int(attrs["size"]) * 4
515 self.structs[attrs["name"]] = self.group
516 elif name == "field":
517 self.group.fields.append(Field(self, attrs))
518 self.values = []
519 elif name == "enum":
520 self.values = []
521 self.enum = safe_name(attrs["name"])
522 self.enums.add(attrs["name"])
523 if "prefix" in attrs:
524 self.prefix = attrs["prefix"]
525 else:
526 self.prefix= None
527 elif name == "value":
528 self.values.append(Value(attrs))
529
530 def end_element(self, name):
531 if name == "struct":
532 self.emit_struct()
533 self.struct = None
534 self.group = None
535 elif name == "field":
536 self.group.fields[-1].values = self.values
537 elif name == "enum":
538 self.emit_enum()
539 self.enum = None
540 elif name == "panxml":
541 # Include at the end so it can depend on us but not the converse
542 print('#include "panfrost-job.h"')
543 print('#endif')
544
545 def emit_header(self, name):
546 default_fields = []
547 for field in self.group.fields:
548 if not type(field) is Field:
549 continue
550 if field.default is not None:
551 default_fields.append(" .{} = {}".format(field.name, field.default))
552 elif field.type in self.structs:
553 default_fields.append(" .{} = {{ {}_header }}".format(field.name, self.gen_prefix(safe_name(field.type.upper()))))
554
555 print('#define %-40s\\' % (name + '_header'))
556 print(", \\\n".join(default_fields))
557 print('')
558
559 def emit_template_struct(self, name, group, opaque_structs):
560 print("struct %s {" % (name + ('_OPAQUE' if opaque_structs else '')))
561 group.emit_template_struct("", opaque_structs)
562 print("};\n")
563
564 if opaque_structs:
565 # Just so it isn't left undefined
566 print('#define %-40s 0' % (name + '_OPAQUE_header'))
567
568 def emit_pack_function(self, name, group, with_opaque):
569 print("static inline void\n%s_pack(uint32_t * restrict cl,\n%sconst struct %s * restrict values)\n{" %
570 (name, ' ' * (len(name) + 6), name))
571
572 group.emit_pack_function(False)
573
574 print("}\n\n")
575
576 if with_opaque:
577 print("static inline void\n%s_OPAQUE_pack(uint32_t * restrict cl,\n%sconst struct %s_OPAQUE * restrict values)\n{" %
578 (name, ' ' * (len(name) + 6), name))
579
580 group.emit_pack_function(True)
581
582 print("}\n")
583
584 # Should be a whole number of words
585 assert((self.group.length % 4) == 0)
586
587 print('#define {} {}'.format (name + "_LENGTH", self.group.length))
588 print('struct {}_packed {{ uint32_t opaque[{}]; }};'.format(name.lower(), self.group.length // 4))
589
590 def emit_unpack_function(self, name, group):
591 print("static inline void")
592 print("%s_unpack(const uint8_t * restrict cl,\n%sstruct %s * restrict values)\n{" %
593 (name.upper(), ' ' * (len(name) + 8), name))
594
595 group.emit_unpack_function()
596
597 print("}\n")
598
599 def emit_print_function(self, name, group):
600 print("static inline void")
601 print("{}_print(FILE *fp, const struct {} * values, unsigned indent)\n{{".format(name.upper(), name))
602
603 group.emit_print_function()
604
605 print("}\n")
606
607 def emit_struct(self):
608 name = self.struct
609
610 self.emit_template_struct(self.struct, self.group, False)
611 if self.with_opaque:
612 self.emit_template_struct(self.struct, self.group, True)
613 self.emit_header(name)
614 self.emit_pack_function(self.struct, self.group, self.with_opaque)
615 self.emit_unpack_function(self.struct, self.group)
616 self.emit_print_function(self.struct, self.group)
617
618 def enum_prefix(self, name):
619 return
620
621 def emit_enum(self):
622 e_name = enum_name(self.enum)
623 prefix = e_name if self.enum != 'Format' else global_prefix
624 print('enum {} {{'.format(e_name))
625
626 for value in self.values:
627 name = '{}_{}'.format(prefix, value.name)
628 name = safe_name(name).upper()
629 print(' % -36s = %6d,' % (name, value.value))
630 print('};\n')
631
632 print("static inline const char *")
633 print("{}_as_str(enum {} imm)\n{{".format(e_name.lower(), e_name))
634 print(" switch (imm) {")
635 for value in self.values:
636 name = '{}_{}'.format(prefix, value.name)
637 name = safe_name(name).upper()
638 print(' case {}: return "{}";'.format(name, value.name))
639 print(' default: return "XXX: INVALID";')
640 print(" }")
641 print("}\n")
642
643 def parse(self, filename):
644 file = open(filename, "rb")
645 self.parser.ParseFile(file)
646 file.close()
647
648 if len(sys.argv) < 2:
649 print("No input xml file specified")
650 sys.exit(1)
651
652 input_file = sys.argv[1]
653
654 p = Parser()
655 p.parse(input_file)