panfrost: Adopt gen_pack_header.py via v3d
[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):
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 elif self.type in self.parser.enums:
232 type = 'enum ' + enum_name(self.type)
233 else:
234 print("#error unhandled type: %s" % self.type)
235 type = "uint32_t"
236
237 print(" %-36s %s%s;" % (type, self.name, dim))
238
239 for value in self.values:
240 name = prefixed_upper_name(self.prefix, value.name)
241 print("#define %-40s %d" % (name, value.value))
242
243 def overlaps(self, field):
244 return self != field and max(self.start, field.start) <= min(self.end, field.end)
245
246
247 class Group(object):
248 def __init__(self, parser, parent, start, count):
249 self.parser = parser
250 self.parent = parent
251 self.start = start
252 self.count = count
253 self.size = 0
254 self.length = 0
255 self.fields = []
256
257 def emit_template_struct(self, dim):
258 if self.count == 0:
259 print(" /* variable length fields follow */")
260 else:
261 if self.count > 1:
262 dim = "%s[%d]" % (dim, self.count)
263
264 for field in self.fields:
265 if field.exact is not None:
266 continue
267
268 field.emit_template_struct(dim)
269
270 class Word:
271 def __init__(self):
272 self.size = 32
273 self.fields = []
274
275 def collect_words(self, words):
276 for field in self.fields:
277 first_word = field.start // 32
278 last_word = field.end // 32
279
280 for b in range(first_word, last_word + 1):
281 if not b in words:
282 words[b] = self.Word()
283
284 words[b].fields.append(field)
285
286 def emit_pack_function(self):
287 # Determine number of bytes in this group.
288 calculated = max(field.end // 8 for field in self.fields) + 1
289
290 if self.length > 0:
291 assert(self.length >= calculated)
292 else:
293 self.length = calculated
294
295 words = {}
296 self.collect_words(words)
297
298 emitted_structs = set()
299
300 # Validate the modifier is lossless
301 for field in self.fields:
302 if field.modifier is None:
303 continue
304
305 assert(field.exact is None)
306
307 if field.modifier[0] == "shr":
308 shift = field.modifier[1]
309 mask = hex((1 << shift) - 1)
310 print(" assert((values->{} & {}) == 0);".format(field.name, mask))
311 elif field.modifier[0] == "minus":
312 print(" assert(values->{} >= {});".format(field.name, field.modifier[1]))
313
314 for index in range(self.length // 4):
315 # Handle MBZ words
316 if not index in words:
317 print(" cl[%2d] = 0;" % index)
318 continue
319
320 word = words[index]
321
322 word_start = index * 32
323
324 v = None
325 prefix = " cl[%2d] =" % index
326
327 first = word.fields[0]
328 if first.type in self.parser.structs and first.start not in emitted_structs:
329 pack_name = self.parser.gen_prefix(safe_name(first.type.upper()))
330 start = first.start
331 assert((first.start % 32) == 0)
332 assert(first.end == first.start + (self.parser.structs[first.type].length * 8) - 1)
333 print(" {}_pack(cl + {}, &values->{});".format(pack_name, first.start // 32, first.name))
334 emitted_structs.add(first.start)
335
336 for field in word.fields:
337 name = field.name
338 start = field.start
339 end = field.end
340 field_word_start = (field.start // 32) * 32
341 start -= field_word_start
342 end -= field_word_start
343
344 value = str(field.exact) if field.exact is not None else "values->%s" % name
345 if field.modifier is not None:
346 if field.modifier[0] == "shr":
347 value = "{} >> {}".format(value, field.modifier[1])
348 elif field.modifier[0] == "minus":
349 value = "{} - {}".format(value, field.modifier[1])
350
351 if field.type == "uint" or field.type == "address":
352 s = "__gen_uint(%s, %d, %d)" % \
353 (value, start, end)
354 elif field.type in self.parser.enums:
355 s = "__gen_uint(%s, %d, %d)" % \
356 (value, start, end)
357 elif field.type == "int":
358 s = "__gen_sint(%s, %d, %d)" % \
359 (value, start, end)
360 elif field.type == "bool":
361 s = "__gen_uint(%s, %d, %d)" % \
362 (value, start, end)
363 elif field.type == "float":
364 assert(start == 0 and end == 31)
365 s = "__gen_uint(fui({}), 0, 32)".format(value)
366 elif field.type in self.parser.structs:
367 # Structs are packed directly
368 assert(len(word.fields) == 1)
369 continue
370 else:
371 s = "#error unhandled field {}, type {}".format(name, field.type)
372
373 if not s == None:
374 shift = word_start - field_word_start
375 if shift:
376 s = "%s >> %d" % (s, shift)
377
378 if field == word.fields[-1]:
379 print("%s %s;" % (prefix, s))
380 else:
381 print("%s %s |" % (prefix, s))
382 prefix = " "
383
384 continue
385
386 # Given a field (start, end) contained in word `index`, generate the 32-bit
387 # mask of present bits relative to the word
388 def mask_for_word(self, index, start, end):
389 field_word_start = index * 32
390 start -= field_word_start
391 end -= field_word_start
392 # Cap multiword at one word
393 start = max(start, 0)
394 end = min(end, 32 - 1)
395 count = (end - start + 1)
396 return (((1 << count) - 1) << start)
397
398 def emit_unpack_function(self):
399 # First, verify there is no garbage in unused bits
400 words = {}
401 self.collect_words(words)
402
403 for index in range(self.length // 4):
404 base = index * 32
405 word = words.get(index, self.Word())
406 masks = [self.mask_for_word(index, f.start, f.end) for f in word.fields]
407 mask = reduce(lambda x,y: x | y, masks, 0)
408
409 ALL_ONES = 0xffffffff
410
411 if mask != ALL_ONES:
412 TMPL = ' if (((const uint32_t *) cl)[{}] & {}) fprintf(stderr, "XXX: Invalid field unpacked at word {}\\n");'
413 print(TMPL.format(index, hex(mask ^ ALL_ONES), index))
414
415 for field in self.fields:
416 # Recurse for structs, see pack() for validation
417 if field.type in self.parser.structs:
418 pack_name = self.parser.gen_prefix(safe_name(field.type)).upper()
419 print(" {}_unpack(cl + {}, &values->{});".format(pack_name, field.start // 32, field.name))
420 continue
421
422 convert = None
423
424 args = []
425 args.append('cl')
426 args.append(str(field.start))
427 args.append(str(field.end))
428
429 if field.type in set(["uint", "address"]) | self.parser.enums:
430 convert = "__gen_unpack_uint"
431 elif field.type == "int":
432 convert = "__gen_unpack_sint"
433 elif field.type == "bool":
434 convert = "__gen_unpack_uint"
435 elif field.type == "float":
436 convert = "__gen_unpack_float"
437 else:
438 s = "/* unhandled field %s, type %s */\n" % (field.name, field.type)
439
440 suffix = ""
441 if field.modifier:
442 if field.modifier[0] == "minus":
443 suffix = " + {}".format(field.modifier[1])
444 elif field.modifier[0] == "shr":
445 suffix = " << {}".format(field.modifier[1])
446
447 decoded = '{}({}){}'.format(convert, ', '.join(args), suffix)
448
449 print(' values->{} = {};'.format(field.name, decoded))
450
451 def emit_print_function(self):
452 for field in self.fields:
453 convert = None
454 name, val = field.human_name, 'values->{}'.format(field.name)
455
456 if field.type in self.parser.structs:
457 pack_name = self.parser.gen_prefix(safe_name(field.type)).upper()
458 print(' fprintf(fp, "%*s{}:\\n", indent, "");'.format(field.human_name))
459 print(" {}_print(fp, &values->{}, indent + 2);".format(pack_name, field.name))
460 elif field.type == "address":
461 # TODO resolve to name
462 print(' fprintf(fp, "%*s{}: 0x%" PRIx64 "\\n", indent, "", {});'.format(name, val))
463 elif field.type in self.parser.enums:
464 print(' fprintf(fp, "%*s{}: %s\\n", indent, "", {}_as_str({}));'.format(name, enum_name(field.type), val))
465 elif field.type == "int":
466 print(' fprintf(fp, "%*s{}: %d\\n", indent, "", {});'.format(name, val))
467 elif field.type == "bool":
468 print(' fprintf(fp, "%*s{}: %s\\n", indent, "", {} ? "true" : "false");'.format(name, val))
469 elif field.type == "float":
470 print(' fprintf(fp, "%*s{}: %f\\n", indent, "", {});'.format(name, val))
471 elif field.type == "uint" and (field.end - field.start) >= 32:
472 print(' fprintf(fp, "%*s{}: 0x%" PRIx64 "\\n", indent, "", {});'.format(name, val))
473 else:
474 print(' fprintf(fp, "%*s{}: %u\\n", indent, "", {});'.format(name, val))
475
476 class Value(object):
477 def __init__(self, attrs):
478 self.name = attrs["name"]
479 self.value = int(attrs["value"])
480
481 class Parser(object):
482 def __init__(self):
483 self.parser = xml.parsers.expat.ParserCreate()
484 self.parser.StartElementHandler = self.start_element
485 self.parser.EndElementHandler = self.end_element
486
487 self.struct = None
488 self.structs = {}
489 # Set of enum names we've seen.
490 self.enums = set()
491
492 def gen_prefix(self, name):
493 return '{}_{}'.format(global_prefix.upper(), name)
494
495 def start_element(self, name, attrs):
496 if name == "panxml":
497 print(pack_header)
498 elif name == "struct":
499 name = attrs["name"]
500
501 object_name = self.gen_prefix(safe_name(name.upper()))
502 self.struct = object_name
503
504 self.group = Group(self, None, 0, 1)
505 if "size" in attrs:
506 self.group.length = int(attrs["size"]) * 4
507 self.structs[attrs["name"]] = self.group
508 elif name == "field":
509 self.group.fields.append(Field(self, attrs))
510 self.values = []
511 elif name == "enum":
512 self.values = []
513 self.enum = safe_name(attrs["name"])
514 self.enums.add(attrs["name"])
515 if "prefix" in attrs:
516 self.prefix = attrs["prefix"]
517 else:
518 self.prefix= None
519 elif name == "value":
520 self.values.append(Value(attrs))
521
522 def end_element(self, name):
523 if name == "struct":
524 self.emit_struct()
525 self.struct = None
526 self.group = None
527 elif name == "field":
528 self.group.fields[-1].values = self.values
529 elif name == "enum":
530 self.emit_enum()
531 self.enum = None
532 elif name == "panxml":
533 # Include at the end so it can depend on us but not the converse
534 print('#include "panfrost-job.h"')
535 print('#endif')
536
537 def emit_header(self, name):
538 default_fields = []
539 for field in self.group.fields:
540 if not type(field) is Field:
541 continue
542 if field.default == None:
543 continue
544 default_fields.append(" .{} = {}".format(field.name, field.default))
545
546 print('#define %-40s\\' % (name + '_header'))
547 print(", \\\n".join(default_fields))
548 print('')
549
550 def emit_template_struct(self, name, group):
551 print("struct %s {" % name)
552 group.emit_template_struct("")
553 print("};\n")
554
555 def emit_pack_function(self, name, group):
556 print("static inline void\n%s_pack(uint32_t * restrict cl,\n%sconst struct %s * restrict values)\n{" %
557 (name, ' ' * (len(name) + 6), name))
558
559 group.emit_pack_function()
560
561 print("}\n")
562
563 # Should be a whole number of words
564 assert((self.group.length % 4) == 0)
565
566 print('#define {} {}'.format (name + "_LENGTH", self.group.length))
567 print('struct {}_packed {{ uint32_t opaque[{}]; }};'.format(name.lower(), self.group.length // 4))
568
569 def emit_unpack_function(self, name, group):
570 print("static inline void")
571 print("%s_unpack(const uint8_t * restrict cl,\n%sstruct %s * restrict values)\n{" %
572 (name.upper(), ' ' * (len(name) + 8), name))
573
574 group.emit_unpack_function()
575
576 print("}\n")
577
578 def emit_print_function(self, name, group):
579 print("static inline void")
580 print("{}_print(FILE *fp, const struct {} * values, unsigned indent)\n{{".format(name.upper(), name))
581
582 group.emit_print_function()
583
584 print("}\n")
585
586 def emit_struct(self):
587 name = self.struct
588
589 self.emit_template_struct(self.struct, self.group)
590 self.emit_header(name)
591 self.emit_pack_function(self.struct, self.group)
592 self.emit_unpack_function(self.struct, self.group)
593 self.emit_print_function(self.struct, self.group)
594
595 def emit_enum(self):
596 e_name = enum_name(self.enum)
597 print('enum {} {{'.format(e_name))
598 for value in self.values:
599 name = '{}_{}'.format(e_name, value.name)
600 name = safe_name(name).upper()
601 print(' % -36s = %6d,' % (name, value.value))
602 print('};\n')
603
604 print("static inline const char *")
605 print("{}_as_str(enum {} imm)\n{{".format(e_name.lower(), e_name))
606 print(" switch (imm) {")
607 for value in self.values:
608 name = '{}_{}'.format(e_name, value.name)
609 name = safe_name(name).upper()
610 print(' case {}: return "{}";'.format(name, value.name))
611 print(' default: return "XXX: INVALID";')
612 print(" }")
613 print("}\n")
614
615 def parse(self, filename):
616 file = open(filename, "rb")
617 self.parser.ParseFile(file)
618 file.close()
619
620 if len(sys.argv) < 2:
621 print("No input xml file specified")
622 sys.exit(1)
623
624 input_file = sys.argv[1]
625
626 p = Parser()
627 p.parse(input_file)