xref: /openbmc/qemu/hw/acpi/aml-build.c (revision b917da4c)
1 /* Support for generating ACPI tables and passing them to Guests
2  *
3  * Copyright (C) 2015 Red Hat Inc
4  *
5  * Author: Michael S. Tsirkin <mst@redhat.com>
6  * Author: Igor Mammedov <imammedo@redhat.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17 
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "qemu/osdep.h"
23 #include <glib/gprintf.h>
24 #include "hw/acpi/aml-build.h"
25 #include "qemu/bswap.h"
26 #include "qemu/bitops.h"
27 #include "hw/acpi/bios-linker-loader.h"
28 
29 static GArray *build_alloc_array(void)
30 {
31     return g_array_new(false, true /* clear */, 1);
32 }
33 
34 static void build_free_array(GArray *array)
35 {
36     g_array_free(array, true);
37 }
38 
39 static void build_prepend_byte(GArray *array, uint8_t val)
40 {
41     g_array_prepend_val(array, val);
42 }
43 
44 static void build_append_byte(GArray *array, uint8_t val)
45 {
46     g_array_append_val(array, val);
47 }
48 
49 static void build_append_array(GArray *array, GArray *val)
50 {
51     g_array_append_vals(array, val->data, val->len);
52 }
53 
54 #define ACPI_NAMESEG_LEN 4
55 
56 static void
57 build_append_nameseg(GArray *array, const char *seg)
58 {
59     int len;
60 
61     len = strlen(seg);
62     assert(len <= ACPI_NAMESEG_LEN);
63 
64     g_array_append_vals(array, seg, len);
65     /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */
66     g_array_append_vals(array, "____", ACPI_NAMESEG_LEN - len);
67 }
68 
69 static void GCC_FMT_ATTR(2, 0)
70 build_append_namestringv(GArray *array, const char *format, va_list ap)
71 {
72     char *s;
73     char **segs;
74     char **segs_iter;
75     int seg_count = 0;
76 
77     s = g_strdup_vprintf(format, ap);
78     segs = g_strsplit(s, ".", 0);
79     g_free(s);
80 
81     /* count segments */
82     segs_iter = segs;
83     while (*segs_iter) {
84         ++segs_iter;
85         ++seg_count;
86     }
87     /*
88      * ACPI 5.0 spec: 20.2.2 Name Objects Encoding:
89      * "SegCount can be from 1 to 255"
90      */
91     assert(seg_count > 0 && seg_count <= 255);
92 
93     /* handle RootPath || PrefixPath */
94     s = *segs;
95     while (*s == '\\' || *s == '^') {
96         build_append_byte(array, *s);
97         ++s;
98     }
99 
100     switch (seg_count) {
101     case 1:
102         if (!*s) {
103             build_append_byte(array, 0x00); /* NullName */
104         } else {
105             build_append_nameseg(array, s);
106         }
107         break;
108 
109     case 2:
110         build_append_byte(array, 0x2E); /* DualNamePrefix */
111         build_append_nameseg(array, s);
112         build_append_nameseg(array, segs[1]);
113         break;
114     default:
115         build_append_byte(array, 0x2F); /* MultiNamePrefix */
116         build_append_byte(array, seg_count);
117 
118         /* handle the 1st segment manually due to prefix/root path */
119         build_append_nameseg(array, s);
120 
121         /* add the rest of segments */
122         segs_iter = segs + 1;
123         while (*segs_iter) {
124             build_append_nameseg(array, *segs_iter);
125             ++segs_iter;
126         }
127         break;
128     }
129     g_strfreev(segs);
130 }
131 
132 GCC_FMT_ATTR(2, 3)
133 static void build_append_namestring(GArray *array, const char *format, ...)
134 {
135     va_list ap;
136 
137     va_start(ap, format);
138     build_append_namestringv(array, format, ap);
139     va_end(ap);
140 }
141 
142 /* 5.4 Definition Block Encoding */
143 enum {
144     PACKAGE_LENGTH_1BYTE_SHIFT = 6, /* Up to 63 - use extra 2 bits. */
145     PACKAGE_LENGTH_2BYTE_SHIFT = 4,
146     PACKAGE_LENGTH_3BYTE_SHIFT = 12,
147     PACKAGE_LENGTH_4BYTE_SHIFT = 20,
148 };
149 
150 static void
151 build_prepend_package_length(GArray *package, unsigned length, bool incl_self)
152 {
153     uint8_t byte;
154     unsigned length_bytes;
155 
156     if (length + 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT)) {
157         length_bytes = 1;
158     } else if (length + 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT)) {
159         length_bytes = 2;
160     } else if (length + 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT)) {
161         length_bytes = 3;
162     } else {
163         length_bytes = 4;
164     }
165 
166     /*
167      * NamedField uses PkgLength encoding but it doesn't include length
168      * of PkgLength itself.
169      */
170     if (incl_self) {
171         /*
172          * PkgLength is the length of the inclusive length of the data
173          * and PkgLength's length itself when used for terms with
174          * explitit length.
175          */
176         length += length_bytes;
177     }
178 
179     switch (length_bytes) {
180     case 1:
181         byte = length;
182         build_prepend_byte(package, byte);
183         return;
184     case 4:
185         byte = length >> PACKAGE_LENGTH_4BYTE_SHIFT;
186         build_prepend_byte(package, byte);
187         length &= (1 << PACKAGE_LENGTH_4BYTE_SHIFT) - 1;
188         /* fall through */
189     case 3:
190         byte = length >> PACKAGE_LENGTH_3BYTE_SHIFT;
191         build_prepend_byte(package, byte);
192         length &= (1 << PACKAGE_LENGTH_3BYTE_SHIFT) - 1;
193         /* fall through */
194     case 2:
195         byte = length >> PACKAGE_LENGTH_2BYTE_SHIFT;
196         build_prepend_byte(package, byte);
197         length &= (1 << PACKAGE_LENGTH_2BYTE_SHIFT) - 1;
198         /* fall through */
199     }
200     /*
201      * Most significant two bits of byte zero indicate how many following bytes
202      * are in PkgLength encoding.
203      */
204     byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length;
205     build_prepend_byte(package, byte);
206 }
207 
208 static void
209 build_append_pkg_length(GArray *array, unsigned length, bool incl_self)
210 {
211     GArray *tmp = build_alloc_array();
212 
213     build_prepend_package_length(tmp, length, incl_self);
214     build_append_array(array, tmp);
215     build_free_array(tmp);
216 }
217 
218 static void build_package(GArray *package, uint8_t op)
219 {
220     build_prepend_package_length(package, package->len, true);
221     build_prepend_byte(package, op);
222 }
223 
224 static void build_extop_package(GArray *package, uint8_t op)
225 {
226     build_package(package, op);
227     build_prepend_byte(package, 0x5B); /* ExtOpPrefix */
228 }
229 
230 static void build_append_int_noprefix(GArray *table, uint64_t value, int size)
231 {
232     int i;
233 
234     for (i = 0; i < size; ++i) {
235         build_append_byte(table, value & 0xFF);
236         value = value >> 8;
237     }
238 }
239 
240 static void build_append_int(GArray *table, uint64_t value)
241 {
242     if (value == 0x00) {
243         build_append_byte(table, 0x00); /* ZeroOp */
244     } else if (value == 0x01) {
245         build_append_byte(table, 0x01); /* OneOp */
246     } else if (value <= 0xFF) {
247         build_append_byte(table, 0x0A); /* BytePrefix */
248         build_append_int_noprefix(table, value, 1);
249     } else if (value <= 0xFFFF) {
250         build_append_byte(table, 0x0B); /* WordPrefix */
251         build_append_int_noprefix(table, value, 2);
252     } else if (value <= 0xFFFFFFFF) {
253         build_append_byte(table, 0x0C); /* DWordPrefix */
254         build_append_int_noprefix(table, value, 4);
255     } else {
256         build_append_byte(table, 0x0E); /* QWordPrefix */
257         build_append_int_noprefix(table, value, 8);
258     }
259 }
260 
261 /*
262  * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
263  * and return the offset to 0x00000000 for runtime patching.
264  *
265  * Warning: runtime patching is best avoided. Only use this as
266  * a replacement for DataTableRegion (for guests that don't
267  * support it).
268  */
269 int
270 build_append_named_dword(GArray *array, const char *name_format, ...)
271 {
272     int offset;
273     va_list ap;
274 
275     build_append_byte(array, 0x08); /* NameOp */
276     va_start(ap, name_format);
277     build_append_namestringv(array, name_format, ap);
278     va_end(ap);
279 
280     build_append_byte(array, 0x0C); /* DWordPrefix */
281 
282     offset = array->len;
283     build_append_int_noprefix(array, 0x00000000, 4);
284     assert(array->len == offset + 4);
285 
286     return offset;
287 }
288 
289 static GPtrArray *alloc_list;
290 
291 static Aml *aml_alloc(void)
292 {
293     Aml *var = g_new0(typeof(*var), 1);
294 
295     g_ptr_array_add(alloc_list, var);
296     var->block_flags = AML_NO_OPCODE;
297     var->buf = build_alloc_array();
298     return var;
299 }
300 
301 static Aml *aml_opcode(uint8_t op)
302 {
303     Aml *var = aml_alloc();
304 
305     var->op  = op;
306     var->block_flags = AML_OPCODE;
307     return var;
308 }
309 
310 static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags)
311 {
312     Aml *var = aml_alloc();
313 
314     var->op  = op;
315     var->block_flags = flags;
316     return var;
317 }
318 
319 static void aml_free(gpointer data, gpointer user_data)
320 {
321     Aml *var = data;
322     build_free_array(var->buf);
323     g_free(var);
324 }
325 
326 Aml *init_aml_allocator(void)
327 {
328     Aml *var;
329 
330     assert(!alloc_list);
331     alloc_list = g_ptr_array_new();
332     var = aml_alloc();
333     return var;
334 }
335 
336 void free_aml_allocator(void)
337 {
338     g_ptr_array_foreach(alloc_list, aml_free, NULL);
339     g_ptr_array_free(alloc_list, true);
340     alloc_list = 0;
341 }
342 
343 /* pack data with DefBuffer encoding */
344 static void build_buffer(GArray *array, uint8_t op)
345 {
346     GArray *data = build_alloc_array();
347 
348     build_append_int(data, array->len);
349     g_array_prepend_vals(array, data->data, data->len);
350     build_free_array(data);
351     build_package(array, op);
352 }
353 
354 void aml_append(Aml *parent_ctx, Aml *child)
355 {
356     GArray *buf = build_alloc_array();
357     build_append_array(buf, child->buf);
358 
359     switch (child->block_flags) {
360     case AML_OPCODE:
361         build_append_byte(parent_ctx->buf, child->op);
362         break;
363     case AML_EXT_PACKAGE:
364         build_extop_package(buf, child->op);
365         break;
366     case AML_PACKAGE:
367         build_package(buf, child->op);
368         break;
369     case AML_RES_TEMPLATE:
370         build_append_byte(buf, 0x79); /* EndTag */
371         /*
372          * checksum operations are treated as succeeded if checksum
373          * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
374          */
375         build_append_byte(buf, 0);
376         /* fall through, to pack resources in buffer */
377     case AML_BUFFER:
378         build_buffer(buf, child->op);
379         break;
380     case AML_NO_OPCODE:
381         break;
382     default:
383         assert(0);
384         break;
385     }
386     build_append_array(parent_ctx->buf, buf);
387     build_free_array(buf);
388 }
389 
390 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
391 Aml *aml_scope(const char *name_format, ...)
392 {
393     va_list ap;
394     Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE);
395     va_start(ap, name_format);
396     build_append_namestringv(var->buf, name_format, ap);
397     va_end(ap);
398     return var;
399 }
400 
401 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
402 Aml *aml_return(Aml *val)
403 {
404     Aml *var = aml_opcode(0xA4 /* ReturnOp */);
405     aml_append(var, val);
406     return var;
407 }
408 
409 /*
410  * ACPI 1.0b: 16.2.3 Data Objects Encoding:
411  * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
412  */
413 Aml *aml_int(const uint64_t val)
414 {
415     Aml *var = aml_alloc();
416     build_append_int(var->buf, val);
417     return var;
418 }
419 
420 /*
421  * helper to construct NameString, which returns Aml object
422  * for using with aml_append or other aml_* terms
423  */
424 Aml *aml_name(const char *name_format, ...)
425 {
426     va_list ap;
427     Aml *var = aml_alloc();
428     va_start(ap, name_format);
429     build_append_namestringv(var->buf, name_format, ap);
430     va_end(ap);
431     return var;
432 }
433 
434 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
435 Aml *aml_name_decl(const char *name, Aml *val)
436 {
437     Aml *var = aml_opcode(0x08 /* NameOp */);
438     build_append_namestring(var->buf, "%s", name);
439     aml_append(var, val);
440     return var;
441 }
442 
443 /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
444 Aml *aml_arg(int pos)
445 {
446     Aml *var;
447     uint8_t op = 0x68 /* ARG0 op */ + pos;
448 
449     assert(pos <= 6);
450     var = aml_opcode(op);
451     return var;
452 }
453 
454 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToInteger */
455 Aml *aml_to_integer(Aml *arg)
456 {
457     Aml *var = aml_opcode(0x99 /* ToIntegerOp */);
458     aml_append(var, arg);
459     build_append_byte(var->buf, 0x00 /* NullNameOp */);
460     return var;
461 }
462 
463 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToHexString */
464 Aml *aml_to_hexstring(Aml *src, Aml *dst)
465 {
466     Aml *var = aml_opcode(0x98 /* ToHexStringOp */);
467     aml_append(var, src);
468     if (dst) {
469         aml_append(var, dst);
470     } else {
471         build_append_byte(var->buf, 0x00 /* NullNameOp */);
472     }
473     return var;
474 }
475 
476 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToBuffer */
477 Aml *aml_to_buffer(Aml *src, Aml *dst)
478 {
479     Aml *var = aml_opcode(0x96 /* ToBufferOp */);
480     aml_append(var, src);
481     if (dst) {
482         aml_append(var, dst);
483     } else {
484         build_append_byte(var->buf, 0x00 /* NullNameOp */);
485     }
486     return var;
487 }
488 
489 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
490 Aml *aml_store(Aml *val, Aml *target)
491 {
492     Aml *var = aml_opcode(0x70 /* StoreOp */);
493     aml_append(var, val);
494     aml_append(var, target);
495     return var;
496 }
497 
498 /**
499  * build_opcode_2arg_dst:
500  * @op: 1-byte opcode
501  * @arg1: 1st operand
502  * @arg2: 2nd operand
503  * @dst: optional target to store to, set to NULL if it's not required
504  *
505  * An internal helper to compose AML terms that have
506  *   "Op Operand Operand Target"
507  * pattern.
508  *
509  * Returns: The newly allocated and composed according to patter Aml object.
510  */
511 static Aml *
512 build_opcode_2arg_dst(uint8_t op, Aml *arg1, Aml *arg2, Aml *dst)
513 {
514     Aml *var = aml_opcode(op);
515     aml_append(var, arg1);
516     aml_append(var, arg2);
517     if (dst) {
518         aml_append(var, dst);
519     } else {
520         build_append_byte(var->buf, 0x00 /* NullNameOp */);
521     }
522     return var;
523 }
524 
525 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
526 Aml *aml_and(Aml *arg1, Aml *arg2, Aml *dst)
527 {
528     return build_opcode_2arg_dst(0x7B /* AndOp */, arg1, arg2, dst);
529 }
530 
531 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */
532 Aml *aml_or(Aml *arg1, Aml *arg2, Aml *dst)
533 {
534     return build_opcode_2arg_dst(0x7D /* OrOp */, arg1, arg2, dst);
535 }
536 
537 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLOr */
538 Aml *aml_lor(Aml *arg1, Aml *arg2)
539 {
540     Aml *var = aml_opcode(0x91 /* LOrOp */);
541     aml_append(var, arg1);
542     aml_append(var, arg2);
543     return var;
544 }
545 
546 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftLeft */
547 Aml *aml_shiftleft(Aml *arg1, Aml *count)
548 {
549     return build_opcode_2arg_dst(0x79 /* ShiftLeftOp */, arg1, count, NULL);
550 }
551 
552 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftRight */
553 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst)
554 {
555     return build_opcode_2arg_dst(0x7A /* ShiftRightOp */, arg1, count, dst);
556 }
557 
558 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLess */
559 Aml *aml_lless(Aml *arg1, Aml *arg2)
560 {
561     Aml *var = aml_opcode(0x95 /* LLessOp */);
562     aml_append(var, arg1);
563     aml_append(var, arg2);
564     return var;
565 }
566 
567 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
568 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst)
569 {
570     return build_opcode_2arg_dst(0x72 /* AddOp */, arg1, arg2, dst);
571 }
572 
573 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSubtract */
574 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst)
575 {
576     return build_opcode_2arg_dst(0x74 /* SubtractOp */, arg1, arg2, dst);
577 }
578 
579 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIncrement */
580 Aml *aml_increment(Aml *arg)
581 {
582     Aml *var = aml_opcode(0x75 /* IncrementOp */);
583     aml_append(var, arg);
584     return var;
585 }
586 
587 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDecrement */
588 Aml *aml_decrement(Aml *arg)
589 {
590     Aml *var = aml_opcode(0x76 /* DecrementOp */);
591     aml_append(var, arg);
592     return var;
593 }
594 
595 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIndex */
596 Aml *aml_index(Aml *arg1, Aml *idx)
597 {
598     return build_opcode_2arg_dst(0x88 /* IndexOp */, arg1, idx, NULL);
599 }
600 
601 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
602 Aml *aml_notify(Aml *arg1, Aml *arg2)
603 {
604     Aml *var = aml_opcode(0x86 /* NotifyOp */);
605     aml_append(var, arg1);
606     aml_append(var, arg2);
607     return var;
608 }
609 
610 /* helper to call method with 1 argument */
611 Aml *aml_call0(const char *method)
612 {
613     Aml *var = aml_alloc();
614     build_append_namestring(var->buf, "%s", method);
615     return var;
616 }
617 
618 /* helper to call method with 1 argument */
619 Aml *aml_call1(const char *method, Aml *arg1)
620 {
621     Aml *var = aml_alloc();
622     build_append_namestring(var->buf, "%s", method);
623     aml_append(var, arg1);
624     return var;
625 }
626 
627 /* helper to call method with 2 arguments */
628 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2)
629 {
630     Aml *var = aml_alloc();
631     build_append_namestring(var->buf, "%s", method);
632     aml_append(var, arg1);
633     aml_append(var, arg2);
634     return var;
635 }
636 
637 /* helper to call method with 3 arguments */
638 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3)
639 {
640     Aml *var = aml_alloc();
641     build_append_namestring(var->buf, "%s", method);
642     aml_append(var, arg1);
643     aml_append(var, arg2);
644     aml_append(var, arg3);
645     return var;
646 }
647 
648 /* helper to call method with 4 arguments */
649 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4)
650 {
651     Aml *var = aml_alloc();
652     build_append_namestring(var->buf, "%s", method);
653     aml_append(var, arg1);
654     aml_append(var, arg2);
655     aml_append(var, arg3);
656     aml_append(var, arg4);
657     return var;
658 }
659 
660 /*
661  * ACPI 5.0: 6.4.3.8.1 GPIO Connection Descriptor
662  * Type 1, Large Item Name 0xC
663  */
664 
665 static Aml *aml_gpio_connection(AmlGpioConnectionType type,
666                                 AmlConsumerAndProducer con_and_pro,
667                                 uint8_t flags, AmlPinConfig pin_config,
668                                 uint16_t output_drive,
669                                 uint16_t debounce_timeout,
670                                 const uint32_t pin_list[], uint32_t pin_count,
671                                 const char *resource_source_name,
672                                 const uint8_t *vendor_data,
673                                 uint16_t vendor_data_len)
674 {
675     Aml *var = aml_alloc();
676     const uint16_t min_desc_len = 0x16;
677     uint16_t resource_source_name_len, length;
678     uint16_t pin_table_offset, resource_source_name_offset, vendor_data_offset;
679     uint32_t i;
680 
681     assert(resource_source_name);
682     resource_source_name_len = strlen(resource_source_name) + 1;
683     length = min_desc_len + resource_source_name_len + vendor_data_len;
684     pin_table_offset = min_desc_len + 1;
685     resource_source_name_offset = pin_table_offset + pin_count * 2;
686     vendor_data_offset = resource_source_name_offset + resource_source_name_len;
687 
688     build_append_byte(var->buf, 0x8C);  /* GPIO Connection Descriptor */
689     build_append_int_noprefix(var->buf, length, 2); /* Length */
690     build_append_byte(var->buf, 1);     /* Revision ID */
691     build_append_byte(var->buf, type);  /* GPIO Connection Type */
692     /* General Flags (2 bytes) */
693     build_append_int_noprefix(var->buf, con_and_pro, 2);
694     /* Interrupt and IO Flags (2 bytes) */
695     build_append_int_noprefix(var->buf, flags, 2);
696     /* Pin Configuration 0 = Default 1 = Pull-up 2 = Pull-down 3 = No Pull */
697     build_append_byte(var->buf, pin_config);
698     /* Output Drive Strength (2 bytes) */
699     build_append_int_noprefix(var->buf, output_drive, 2);
700     /* Debounce Timeout (2 bytes) */
701     build_append_int_noprefix(var->buf, debounce_timeout, 2);
702     /* Pin Table Offset (2 bytes) */
703     build_append_int_noprefix(var->buf, pin_table_offset, 2);
704     build_append_byte(var->buf, 0);     /* Resource Source Index */
705     /* Resource Source Name Offset (2 bytes) */
706     build_append_int_noprefix(var->buf, resource_source_name_offset, 2);
707     /* Vendor Data Offset (2 bytes) */
708     build_append_int_noprefix(var->buf, vendor_data_offset, 2);
709     /* Vendor Data Length (2 bytes) */
710     build_append_int_noprefix(var->buf, vendor_data_len, 2);
711     /* Pin Number (2n bytes)*/
712     for (i = 0; i < pin_count; i++) {
713         build_append_int_noprefix(var->buf, pin_list[i], 2);
714     }
715 
716     /* Resource Source Name */
717     build_append_namestring(var->buf, "%s", resource_source_name);
718     build_append_byte(var->buf, '\0');
719 
720     /* Vendor-defined Data */
721     if (vendor_data != NULL) {
722         g_array_append_vals(var->buf, vendor_data, vendor_data_len);
723     }
724 
725     return var;
726 }
727 
728 /*
729  * ACPI 5.0: 19.5.53
730  * GpioInt(GPIO Interrupt Connection Resource Descriptor Macro)
731  */
732 Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro,
733                   AmlLevelAndEdge edge_level,
734                   AmlActiveHighAndLow active_level, AmlShared shared,
735                   AmlPinConfig pin_config, uint16_t debounce_timeout,
736                   const uint32_t pin_list[], uint32_t pin_count,
737                   const char *resource_source_name,
738                   const uint8_t *vendor_data, uint16_t vendor_data_len)
739 {
740     uint8_t flags = edge_level | (active_level << 1) | (shared << 3);
741 
742     return aml_gpio_connection(AML_INTERRUPT_CONNECTION, con_and_pro, flags,
743                                pin_config, 0, debounce_timeout, pin_list,
744                                pin_count, resource_source_name, vendor_data,
745                                vendor_data_len);
746 }
747 
748 /*
749  * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor
750  * (Type 1, Large Item Name 0x6)
751  */
752 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size,
753                         AmlReadAndWrite read_and_write)
754 {
755     Aml *var = aml_alloc();
756     build_append_byte(var->buf, 0x86); /* Memory32Fixed Resource Descriptor */
757     build_append_byte(var->buf, 9);    /* Length, bits[7:0] value = 9 */
758     build_append_byte(var->buf, 0);    /* Length, bits[15:8] value = 0 */
759     build_append_byte(var->buf, read_and_write); /* Write status, 1 rw 0 ro */
760 
761     /* Range base address */
762     build_append_byte(var->buf, extract32(addr, 0, 8));  /* bits[7:0] */
763     build_append_byte(var->buf, extract32(addr, 8, 8));  /* bits[15:8] */
764     build_append_byte(var->buf, extract32(addr, 16, 8)); /* bits[23:16] */
765     build_append_byte(var->buf, extract32(addr, 24, 8)); /* bits[31:24] */
766 
767     /* Range length */
768     build_append_byte(var->buf, extract32(size, 0, 8));  /* bits[7:0] */
769     build_append_byte(var->buf, extract32(size, 8, 8));  /* bits[15:8] */
770     build_append_byte(var->buf, extract32(size, 16, 8)); /* bits[23:16] */
771     build_append_byte(var->buf, extract32(size, 24, 8)); /* bits[31:24] */
772     return var;
773 }
774 
775 /*
776  * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor
777  * Type 1, Large Item Name 0x9
778  */
779 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro,
780                    AmlLevelAndEdge level_and_edge,
781                    AmlActiveHighAndLow high_and_low, AmlShared shared,
782                    uint32_t *irq_list, uint8_t irq_count)
783 {
784     int i;
785     Aml *var = aml_alloc();
786     uint8_t irq_flags = con_and_pro | (level_and_edge << 1)
787                         | (high_and_low << 2) | (shared << 3);
788     const int header_bytes_in_len = 2;
789     uint16_t len = header_bytes_in_len + irq_count * sizeof(uint32_t);
790 
791     assert(irq_count > 0);
792 
793     build_append_byte(var->buf, 0x89); /* Extended irq descriptor */
794     build_append_byte(var->buf, len & 0xFF); /* Length, bits[7:0] */
795     build_append_byte(var->buf, len >> 8); /* Length, bits[15:8] */
796     build_append_byte(var->buf, irq_flags); /* Interrupt Vector Information. */
797     build_append_byte(var->buf, irq_count);   /* Interrupt table length */
798 
799     /* Interrupt Number List */
800     for (i = 0; i < irq_count; i++) {
801         build_append_int_noprefix(var->buf, irq_list[i], 4);
802     }
803     return var;
804 }
805 
806 /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */
807 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
808             uint8_t aln, uint8_t len)
809 {
810     Aml *var = aml_alloc();
811     build_append_byte(var->buf, 0x47); /* IO port descriptor */
812     build_append_byte(var->buf, dec);
813     build_append_byte(var->buf, min_base & 0xff);
814     build_append_byte(var->buf, (min_base >> 8) & 0xff);
815     build_append_byte(var->buf, max_base & 0xff);
816     build_append_byte(var->buf, (max_base >> 8) & 0xff);
817     build_append_byte(var->buf, aln);
818     build_append_byte(var->buf, len);
819     return var;
820 }
821 
822 /*
823  * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor
824  *
825  * More verbose description at:
826  * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro)
827  *           6.4.2.1 IRQ Descriptor
828  */
829 Aml *aml_irq_no_flags(uint8_t irq)
830 {
831     uint16_t irq_mask;
832     Aml *var = aml_alloc();
833 
834     assert(irq < 16);
835     build_append_byte(var->buf, 0x22); /* IRQ descriptor 2 byte form */
836 
837     irq_mask = 1U << irq;
838     build_append_byte(var->buf, irq_mask & 0xFF); /* IRQ mask bits[7:0] */
839     build_append_byte(var->buf, irq_mask >> 8); /* IRQ mask bits[15:8] */
840     return var;
841 }
842 
843 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */
844 Aml *aml_lnot(Aml *arg)
845 {
846     Aml *var = aml_opcode(0x92 /* LNotOp */);
847     aml_append(var, arg);
848     return var;
849 }
850 
851 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */
852 Aml *aml_equal(Aml *arg1, Aml *arg2)
853 {
854     Aml *var = aml_opcode(0x93 /* LequalOp */);
855     aml_append(var, arg1);
856     aml_append(var, arg2);
857     return var;
858 }
859 
860 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreater */
861 Aml *aml_lgreater(Aml *arg1, Aml *arg2)
862 {
863     Aml *var = aml_opcode(0x94 /* LGreaterOp */);
864     aml_append(var, arg1);
865     aml_append(var, arg2);
866     return var;
867 }
868 
869 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreaterEqual */
870 Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2)
871 {
872     /* LGreaterEqualOp := LNotOp LLessOp */
873     Aml *var = aml_opcode(0x92 /* LNotOp */);
874     build_append_byte(var->buf, 0x95 /* LLessOp */);
875     aml_append(var, arg1);
876     aml_append(var, arg2);
877     return var;
878 }
879 
880 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
881 Aml *aml_if(Aml *predicate)
882 {
883     Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE);
884     aml_append(var, predicate);
885     return var;
886 }
887 
888 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */
889 Aml *aml_else(void)
890 {
891     Aml *var = aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE);
892     return var;
893 }
894 
895 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefWhile */
896 Aml *aml_while(Aml *predicate)
897 {
898     Aml *var = aml_bundle(0xA2 /* WhileOp */, AML_PACKAGE);
899     aml_append(var, predicate);
900     return var;
901 }
902 
903 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
904 Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag)
905 {
906     Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE);
907     int methodflags;
908 
909     /*
910      * MethodFlags:
911      *   bit 0-2: ArgCount (0-7)
912      *   bit 3: SerializeFlag
913      *     0: NotSerialized
914      *     1: Serialized
915      *   bit 4-7: reserved (must be 0)
916      */
917     assert(arg_count < 8);
918     methodflags = arg_count | (sflag << 3);
919 
920     build_append_namestring(var->buf, "%s", name);
921     build_append_byte(var->buf, methodflags); /* MethodFlags: ArgCount */
922     return var;
923 }
924 
925 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
926 Aml *aml_device(const char *name_format, ...)
927 {
928     va_list ap;
929     Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE);
930     va_start(ap, name_format);
931     build_append_namestringv(var->buf, name_format, ap);
932     va_end(ap);
933     return var;
934 }
935 
936 /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */
937 Aml *aml_resource_template(void)
938 {
939     /* ResourceTemplate is a buffer of Resources with EndTag at the end */
940     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE);
941     return var;
942 }
943 
944 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer
945  * Pass byte_list as NULL to request uninitialized buffer to reserve space.
946  */
947 Aml *aml_buffer(int buffer_size, uint8_t *byte_list)
948 {
949     int i;
950     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
951 
952     for (i = 0; i < buffer_size; i++) {
953         if (byte_list == NULL) {
954             build_append_byte(var->buf, 0x0);
955         } else {
956             build_append_byte(var->buf, byte_list[i]);
957         }
958     }
959 
960     return var;
961 }
962 
963 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */
964 Aml *aml_package(uint8_t num_elements)
965 {
966     Aml *var = aml_bundle(0x12 /* PackageOp */, AML_PACKAGE);
967     build_append_byte(var->buf, num_elements);
968     return var;
969 }
970 
971 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */
972 Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
973                           Aml *offset, uint32_t len)
974 {
975     Aml *var = aml_alloc();
976     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
977     build_append_byte(var->buf, 0x80); /* OpRegionOp */
978     build_append_namestring(var->buf, "%s", name);
979     build_append_byte(var->buf, rs);
980     aml_append(var, offset);
981     build_append_int(var->buf, len);
982     return var;
983 }
984 
985 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */
986 Aml *aml_named_field(const char *name, unsigned length)
987 {
988     Aml *var = aml_alloc();
989     build_append_nameseg(var->buf, name);
990     build_append_pkg_length(var->buf, length, false);
991     return var;
992 }
993 
994 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */
995 Aml *aml_reserved_field(unsigned length)
996 {
997     Aml *var = aml_alloc();
998     /* ReservedField  := 0x00 PkgLength */
999     build_append_byte(var->buf, 0x00);
1000     build_append_pkg_length(var->buf, length, false);
1001     return var;
1002 }
1003 
1004 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */
1005 Aml *aml_field(const char *name, AmlAccessType type, AmlLockRule lock,
1006                AmlUpdateRule rule)
1007 {
1008     Aml *var = aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE);
1009     uint8_t flags = rule << 5 | type;
1010 
1011     flags |= lock << 4; /* LockRule at 4 bit offset */
1012 
1013     build_append_namestring(var->buf, "%s", name);
1014     build_append_byte(var->buf, flags);
1015     return var;
1016 }
1017 
1018 static
1019 Aml *create_field_common(int opcode, Aml *srcbuf, Aml *index, const char *name)
1020 {
1021     Aml *var = aml_opcode(opcode);
1022     aml_append(var, srcbuf);
1023     aml_append(var, index);
1024     build_append_namestring(var->buf, "%s", name);
1025     return var;
1026 }
1027 
1028 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateField */
1029 Aml *aml_create_field(Aml *srcbuf, Aml *bit_index, Aml *num_bits,
1030                       const char *name)
1031 {
1032     Aml *var = aml_alloc();
1033     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1034     build_append_byte(var->buf, 0x13); /* CreateFieldOp */
1035     aml_append(var, srcbuf);
1036     aml_append(var, bit_index);
1037     aml_append(var, num_bits);
1038     build_append_namestring(var->buf, "%s", name);
1039     return var;
1040 }
1041 
1042 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */
1043 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name)
1044 {
1045     return create_field_common(0x8A /* CreateDWordFieldOp */,
1046                                srcbuf, index, name);
1047 }
1048 
1049 /* ACPI 2.0a: 17.2.4.2 Named Objects Encoding: DefCreateQWordField */
1050 Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name)
1051 {
1052     return create_field_common(0x8F /* CreateQWordFieldOp */,
1053                                srcbuf, index, name);
1054 }
1055 
1056 /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */
1057 Aml *aml_string(const char *name_format, ...)
1058 {
1059     Aml *var = aml_opcode(0x0D /* StringPrefix */);
1060     va_list ap;
1061     char *s;
1062     int len;
1063 
1064     va_start(ap, name_format);
1065     len = g_vasprintf(&s, name_format, ap);
1066     va_end(ap);
1067 
1068     g_array_append_vals(var->buf, s, len + 1);
1069     g_free(s);
1070 
1071     return var;
1072 }
1073 
1074 /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */
1075 Aml *aml_local(int num)
1076 {
1077     Aml *var;
1078     uint8_t op = 0x60 /* Local0Op */ + num;
1079 
1080     assert(num <= 7);
1081     var = aml_opcode(op);
1082     return var;
1083 }
1084 
1085 /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */
1086 Aml *aml_varpackage(uint32_t num_elements)
1087 {
1088     Aml *var = aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE);
1089     build_append_int(var->buf, num_elements);
1090     return var;
1091 }
1092 
1093 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */
1094 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
1095                    const char *name_format, ...)
1096 {
1097     va_list ap;
1098     Aml *var = aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE);
1099     va_start(ap, name_format);
1100     build_append_namestringv(var->buf, name_format, ap);
1101     va_end(ap);
1102     build_append_byte(var->buf, proc_id); /* ProcID */
1103     build_append_int_noprefix(var->buf, pblk_addr, sizeof(pblk_addr));
1104     build_append_byte(var->buf, pblk_len); /* PblkLen */
1105     return var;
1106 }
1107 
1108 static uint8_t Hex2Digit(char c)
1109 {
1110     if (c >= 'A') {
1111         return c - 'A' + 10;
1112     }
1113 
1114     return c - '0';
1115 }
1116 
1117 /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */
1118 Aml *aml_eisaid(const char *str)
1119 {
1120     Aml *var = aml_alloc();
1121     uint32_t id;
1122 
1123     g_assert(strlen(str) == 7);
1124     id = (str[0] - 0x40) << 26 |
1125     (str[1] - 0x40) << 21 |
1126     (str[2] - 0x40) << 16 |
1127     Hex2Digit(str[3]) << 12 |
1128     Hex2Digit(str[4]) << 8 |
1129     Hex2Digit(str[5]) << 4 |
1130     Hex2Digit(str[6]);
1131 
1132     build_append_byte(var->buf, 0x0C); /* DWordPrefix */
1133     build_append_int_noprefix(var->buf, bswap32(id), sizeof(id));
1134     return var;
1135 }
1136 
1137 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */
1138 static Aml *aml_as_desc_header(AmlResourceType type, AmlMinFixed min_fixed,
1139                                AmlMaxFixed max_fixed, AmlDecode dec,
1140                                uint8_t type_flags)
1141 {
1142     uint8_t flags = max_fixed | min_fixed | dec;
1143     Aml *var = aml_alloc();
1144 
1145     build_append_byte(var->buf, type);
1146     build_append_byte(var->buf, flags);
1147     build_append_byte(var->buf, type_flags); /* Type Specific Flags */
1148     return var;
1149 }
1150 
1151 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */
1152 static Aml *aml_word_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1153                              AmlMaxFixed max_fixed, AmlDecode dec,
1154                              uint16_t addr_gran, uint16_t addr_min,
1155                              uint16_t addr_max, uint16_t addr_trans,
1156                              uint16_t len, uint8_t type_flags)
1157 {
1158     Aml *var = aml_alloc();
1159 
1160     build_append_byte(var->buf, 0x88); /* Word Address Space Descriptor */
1161     /* minimum length since we do not encode optional fields */
1162     build_append_byte(var->buf, 0x0D);
1163     build_append_byte(var->buf, 0x0);
1164 
1165     aml_append(var,
1166         aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1167     build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1168     build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1169     build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1170     build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1171     build_append_int_noprefix(var->buf, len, sizeof(len));
1172     return var;
1173 }
1174 
1175 /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */
1176 static Aml *aml_dword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1177                               AmlMaxFixed max_fixed, AmlDecode dec,
1178                               uint32_t addr_gran, uint32_t addr_min,
1179                               uint32_t addr_max, uint32_t addr_trans,
1180                               uint32_t len, uint8_t type_flags)
1181 {
1182     Aml *var = aml_alloc();
1183 
1184     build_append_byte(var->buf, 0x87); /* DWord Address Space Descriptor */
1185     /* minimum length since we do not encode optional fields */
1186     build_append_byte(var->buf, 23);
1187     build_append_byte(var->buf, 0x0);
1188 
1189 
1190     aml_append(var,
1191         aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1192     build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1193     build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1194     build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1195     build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1196     build_append_int_noprefix(var->buf, len, sizeof(len));
1197     return var;
1198 }
1199 
1200 /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */
1201 static Aml *aml_qword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1202                               AmlMaxFixed max_fixed, AmlDecode dec,
1203                               uint64_t addr_gran, uint64_t addr_min,
1204                               uint64_t addr_max, uint64_t addr_trans,
1205                               uint64_t len, uint8_t type_flags)
1206 {
1207     Aml *var = aml_alloc();
1208 
1209     build_append_byte(var->buf, 0x8A); /* QWord Address Space Descriptor */
1210     /* minimum length since we do not encode optional fields */
1211     build_append_byte(var->buf, 0x2B);
1212     build_append_byte(var->buf, 0x0);
1213 
1214     aml_append(var,
1215         aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1216     build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1217     build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1218     build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1219     build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1220     build_append_int_noprefix(var->buf, len, sizeof(len));
1221     return var;
1222 }
1223 
1224 /*
1225  * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1226  *
1227  * More verbose description at:
1228  * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro)
1229  */
1230 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1231                          AmlDecode dec, uint16_t addr_gran,
1232                          uint16_t addr_min, uint16_t addr_max,
1233                          uint16_t addr_trans, uint16_t len)
1234 
1235 {
1236     return aml_word_as_desc(AML_BUS_NUMBER_RANGE, min_fixed, max_fixed, dec,
1237                             addr_gran, addr_min, addr_max, addr_trans, len, 0);
1238 }
1239 
1240 /*
1241  * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1242  *
1243  * More verbose description at:
1244  * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro)
1245  */
1246 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1247                  AmlDecode dec, AmlISARanges isa_ranges,
1248                  uint16_t addr_gran, uint16_t addr_min,
1249                  uint16_t addr_max, uint16_t addr_trans,
1250                  uint16_t len)
1251 
1252 {
1253     return aml_word_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1254                             addr_gran, addr_min, addr_max, addr_trans, len,
1255                             isa_ranges);
1256 }
1257 
1258 /*
1259  * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor
1260  *
1261  * More verbose description at:
1262  * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro)
1263  */
1264 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1265                  AmlDecode dec, AmlISARanges isa_ranges,
1266                  uint32_t addr_gran, uint32_t addr_min,
1267                  uint32_t addr_max, uint32_t addr_trans,
1268                  uint32_t len)
1269 
1270 {
1271     return aml_dword_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1272                             addr_gran, addr_min, addr_max, addr_trans, len,
1273                             isa_ranges);
1274 }
1275 
1276 /*
1277  * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor
1278  *
1279  * More verbose description at:
1280  * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro)
1281  */
1282 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1283                       AmlMaxFixed max_fixed, AmlCacheable cacheable,
1284                       AmlReadAndWrite read_and_write,
1285                       uint32_t addr_gran, uint32_t addr_min,
1286                       uint32_t addr_max, uint32_t addr_trans,
1287                       uint32_t len)
1288 {
1289     uint8_t flags = read_and_write | (cacheable << 1);
1290 
1291     return aml_dword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1292                              dec, addr_gran, addr_min, addr_max,
1293                              addr_trans, len, flags);
1294 }
1295 
1296 /*
1297  * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor
1298  *
1299  * More verbose description at:
1300  * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro)
1301  */
1302 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1303                       AmlMaxFixed max_fixed, AmlCacheable cacheable,
1304                       AmlReadAndWrite read_and_write,
1305                       uint64_t addr_gran, uint64_t addr_min,
1306                       uint64_t addr_max, uint64_t addr_trans,
1307                       uint64_t len)
1308 {
1309     uint8_t flags = read_and_write | (cacheable << 1);
1310 
1311     return aml_qword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1312                              dec, addr_gran, addr_min, addr_max,
1313                              addr_trans, len, flags);
1314 }
1315 
1316 /* ACPI 1.0b: 6.4.2.2 DMA Format/6.4.2.2.1 ASL Macro for DMA Descriptor */
1317 Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz,
1318              uint8_t channel)
1319 {
1320     Aml *var = aml_alloc();
1321     uint8_t flags = sz | bm << 2 | typ << 5;
1322 
1323     assert(channel < 8);
1324     build_append_byte(var->buf, 0x2A);    /* Byte 0: DMA Descriptor */
1325     build_append_byte(var->buf, 1U << channel); /* Byte 1: _DMA - DmaChannel */
1326     build_append_byte(var->buf, flags);   /* Byte 2 */
1327     return var;
1328 }
1329 
1330 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefSleep */
1331 Aml *aml_sleep(uint64_t msec)
1332 {
1333     Aml *var = aml_alloc();
1334     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1335     build_append_byte(var->buf, 0x22); /* SleepOp */
1336     aml_append(var, aml_int(msec));
1337     return var;
1338 }
1339 
1340 static uint8_t Hex2Byte(const char *src)
1341 {
1342     int hi, lo;
1343 
1344     hi = Hex2Digit(src[0]);
1345     assert(hi >= 0);
1346     assert(hi <= 15);
1347 
1348     lo = Hex2Digit(src[1]);
1349     assert(lo >= 0);
1350     assert(lo <= 15);
1351     return (hi << 4) | lo;
1352 }
1353 
1354 /*
1355  * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro)
1356  * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1357  * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp");
1358  */
1359 Aml *aml_touuid(const char *uuid)
1360 {
1361     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1362 
1363     assert(strlen(uuid) == 36);
1364     assert(uuid[8] == '-');
1365     assert(uuid[13] == '-');
1366     assert(uuid[18] == '-');
1367     assert(uuid[23] == '-');
1368 
1369     build_append_byte(var->buf, Hex2Byte(uuid + 6));  /* dd - at offset 00 */
1370     build_append_byte(var->buf, Hex2Byte(uuid + 4));  /* cc - at offset 01 */
1371     build_append_byte(var->buf, Hex2Byte(uuid + 2));  /* bb - at offset 02 */
1372     build_append_byte(var->buf, Hex2Byte(uuid + 0));  /* aa - at offset 03 */
1373 
1374     build_append_byte(var->buf, Hex2Byte(uuid + 11)); /* ff - at offset 04 */
1375     build_append_byte(var->buf, Hex2Byte(uuid + 9));  /* ee - at offset 05 */
1376 
1377     build_append_byte(var->buf, Hex2Byte(uuid + 16)); /* hh - at offset 06 */
1378     build_append_byte(var->buf, Hex2Byte(uuid + 14)); /* gg - at offset 07 */
1379 
1380     build_append_byte(var->buf, Hex2Byte(uuid + 19)); /* ii - at offset 08 */
1381     build_append_byte(var->buf, Hex2Byte(uuid + 21)); /* jj - at offset 09 */
1382 
1383     build_append_byte(var->buf, Hex2Byte(uuid + 24)); /* kk - at offset 10 */
1384     build_append_byte(var->buf, Hex2Byte(uuid + 26)); /* ll - at offset 11 */
1385     build_append_byte(var->buf, Hex2Byte(uuid + 28)); /* mm - at offset 12 */
1386     build_append_byte(var->buf, Hex2Byte(uuid + 30)); /* nn - at offset 13 */
1387     build_append_byte(var->buf, Hex2Byte(uuid + 32)); /* oo - at offset 14 */
1388     build_append_byte(var->buf, Hex2Byte(uuid + 34)); /* pp - at offset 15 */
1389 
1390     return var;
1391 }
1392 
1393 /*
1394  * ACPI 2.0b: 16.2.3.6.4.3  Unicode Macro (Convert Ascii String To Unicode)
1395  */
1396 Aml *aml_unicode(const char *str)
1397 {
1398     int i = 0;
1399     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1400 
1401     do {
1402         build_append_byte(var->buf, str[i]);
1403         build_append_byte(var->buf, 0);
1404         i++;
1405     } while (i <= strlen(str));
1406 
1407     return var;
1408 }
1409 
1410 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDerefOf */
1411 Aml *aml_derefof(Aml *arg)
1412 {
1413     Aml *var = aml_opcode(0x83 /* DerefOfOp */);
1414     aml_append(var, arg);
1415     return var;
1416 }
1417 
1418 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSizeOf */
1419 Aml *aml_sizeof(Aml *arg)
1420 {
1421     Aml *var = aml_opcode(0x87 /* SizeOfOp */);
1422     aml_append(var, arg);
1423     return var;
1424 }
1425 
1426 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMutex */
1427 Aml *aml_mutex(const char *name, uint8_t sync_level)
1428 {
1429     Aml *var = aml_alloc();
1430     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1431     build_append_byte(var->buf, 0x01); /* MutexOp */
1432     build_append_namestring(var->buf, "%s", name);
1433     assert(!(sync_level & 0xF0));
1434     build_append_byte(var->buf, sync_level);
1435     return var;
1436 }
1437 
1438 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAcquire */
1439 Aml *aml_acquire(Aml *mutex, uint16_t timeout)
1440 {
1441     Aml *var = aml_alloc();
1442     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1443     build_append_byte(var->buf, 0x23); /* AcquireOp */
1444     aml_append(var, mutex);
1445     build_append_int_noprefix(var->buf, timeout, sizeof(timeout));
1446     return var;
1447 }
1448 
1449 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefRelease */
1450 Aml *aml_release(Aml *mutex)
1451 {
1452     Aml *var = aml_alloc();
1453     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1454     build_append_byte(var->buf, 0x27); /* ReleaseOp */
1455     aml_append(var, mutex);
1456     return var;
1457 }
1458 
1459 /* ACPI 1.0b: 16.2.5.1 Name Space Modifier Objects Encoding: DefAlias */
1460 Aml *aml_alias(const char *source_object, const char *alias_object)
1461 {
1462     Aml *var = aml_opcode(0x06 /* AliasOp */);
1463     aml_append(var, aml_name("%s", source_object));
1464     aml_append(var, aml_name("%s", alias_object));
1465     return var;
1466 }
1467 
1468 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefConcat */
1469 Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target)
1470 {
1471     return build_opcode_2arg_dst(0x73 /* ConcatOp */, source1, source2,
1472                                  target);
1473 }
1474 
1475 void
1476 build_header(GArray *linker, GArray *table_data,
1477              AcpiTableHeader *h, const char *sig, int len, uint8_t rev,
1478              const char *oem_id, const char *oem_table_id)
1479 {
1480     memcpy(&h->signature, sig, 4);
1481     h->length = cpu_to_le32(len);
1482     h->revision = rev;
1483 
1484     if (oem_id) {
1485         strncpy((char *)h->oem_id, oem_id, sizeof h->oem_id);
1486     } else {
1487         memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6);
1488     }
1489 
1490     if (oem_table_id) {
1491         strncpy((char *)h->oem_table_id, oem_table_id, sizeof(h->oem_table_id));
1492     } else {
1493         memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4);
1494         memcpy(h->oem_table_id + 4, sig, 4);
1495     }
1496 
1497     h->oem_revision = cpu_to_le32(1);
1498     memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4);
1499     h->asl_compiler_revision = cpu_to_le32(1);
1500     h->checksum = 0;
1501     /* Checksum to be filled in by Guest linker */
1502     bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE,
1503                                     table_data, h, len, &h->checksum);
1504 }
1505 
1506 void *acpi_data_push(GArray *table_data, unsigned size)
1507 {
1508     unsigned off = table_data->len;
1509     g_array_set_size(table_data, off + size);
1510     return table_data->data + off;
1511 }
1512 
1513 unsigned acpi_data_len(GArray *table)
1514 {
1515     assert(g_array_get_element_size(table) == 1);
1516     return table->len;
1517 }
1518 
1519 void acpi_add_table(GArray *table_offsets, GArray *table_data)
1520 {
1521     uint32_t offset = cpu_to_le32(table_data->len);
1522     g_array_append_val(table_offsets, offset);
1523 }
1524 
1525 void acpi_build_tables_init(AcpiBuildTables *tables)
1526 {
1527     tables->rsdp = g_array_new(false, true /* clear */, 1);
1528     tables->table_data = g_array_new(false, true /* clear */, 1);
1529     tables->tcpalog = g_array_new(false, true /* clear */, 1);
1530     tables->linker = bios_linker_loader_init();
1531 }
1532 
1533 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
1534 {
1535     void *linker_data = bios_linker_loader_cleanup(tables->linker);
1536     g_free(linker_data);
1537     g_array_free(tables->rsdp, true);
1538     g_array_free(tables->table_data, true);
1539     g_array_free(tables->tcpalog, mfre);
1540 }
1541 
1542 /* Build rsdt table */
1543 void
1544 build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets,
1545            const char *oem_id, const char *oem_table_id)
1546 {
1547     AcpiRsdtDescriptorRev1 *rsdt;
1548     size_t rsdt_len;
1549     int i;
1550     const int table_data_len = (sizeof(uint32_t) * table_offsets->len);
1551 
1552     rsdt_len = sizeof(*rsdt) + table_data_len;
1553     rsdt = acpi_data_push(table_data, rsdt_len);
1554     memcpy(rsdt->table_offset_entry, table_offsets->data, table_data_len);
1555     for (i = 0; i < table_offsets->len; ++i) {
1556         /* rsdt->table_offset_entry to be filled by Guest linker */
1557         bios_linker_loader_add_pointer(linker,
1558                                        ACPI_BUILD_TABLE_FILE,
1559                                        ACPI_BUILD_TABLE_FILE,
1560                                        table_data, &rsdt->table_offset_entry[i],
1561                                        sizeof(uint32_t));
1562     }
1563     build_header(linker, table_data,
1564                  (void *)rsdt, "RSDT", rsdt_len, 1, oem_id, oem_table_id);
1565 }
1566