xref: /openbmc/qemu/hw/acpi/aml-build.c (revision 4d87fcdd)
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 "sysemu/numa.h"
28 #include "hw/boards.h"
29 #include "hw/acpi/tpm.h"
30 #include "hw/pci/pci_host.h"
31 #include "hw/pci/pci_bus.h"
32 #include "hw/pci/pci_bridge.h"
33 #include "qemu/cutils.h"
34 
35 static GArray *build_alloc_array(void)
36 {
37     return g_array_new(false, true /* clear */, 1);
38 }
39 
40 static void build_free_array(GArray *array)
41 {
42     g_array_free(array, true);
43 }
44 
45 static void build_prepend_byte(GArray *array, uint8_t val)
46 {
47     g_array_prepend_val(array, val);
48 }
49 
50 static void build_append_byte(GArray *array, uint8_t val)
51 {
52     g_array_append_val(array, val);
53 }
54 
55 static void build_append_array(GArray *array, GArray *val)
56 {
57     g_array_append_vals(array, val->data, val->len);
58 }
59 
60 #define ACPI_NAMESEG_LEN 4
61 
62 void crs_range_insert(GPtrArray *ranges, uint64_t base, uint64_t limit)
63 {
64     CrsRangeEntry *entry;
65 
66     entry = g_malloc(sizeof(*entry));
67     entry->base = base;
68     entry->limit = limit;
69 
70     g_ptr_array_add(ranges, entry);
71 }
72 
73 static void crs_range_free(gpointer data)
74 {
75     CrsRangeEntry *entry = (CrsRangeEntry *)data;
76     g_free(entry);
77 }
78 
79 void crs_range_set_init(CrsRangeSet *range_set)
80 {
81     range_set->io_ranges = g_ptr_array_new_with_free_func(crs_range_free);
82     range_set->mem_ranges = g_ptr_array_new_with_free_func(crs_range_free);
83     range_set->mem_64bit_ranges =
84             g_ptr_array_new_with_free_func(crs_range_free);
85 }
86 
87 void crs_range_set_free(CrsRangeSet *range_set)
88 {
89     g_ptr_array_free(range_set->io_ranges, true);
90     g_ptr_array_free(range_set->mem_ranges, true);
91     g_ptr_array_free(range_set->mem_64bit_ranges, true);
92 }
93 
94 static gint crs_range_compare(gconstpointer a, gconstpointer b)
95 {
96     CrsRangeEntry *entry_a = *(CrsRangeEntry **)a;
97     CrsRangeEntry *entry_b = *(CrsRangeEntry **)b;
98 
99     if (entry_a->base < entry_b->base) {
100         return -1;
101     } else if (entry_a->base > entry_b->base) {
102         return 1;
103     } else {
104         return 0;
105     }
106 }
107 
108 /*
109  * crs_replace_with_free_ranges - given the 'used' ranges within [start - end]
110  * interval, computes the 'free' ranges from the same interval.
111  * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function
112  * will return { [base - a1], [a2 - b1], [b2 - limit] }.
113  */
114 void crs_replace_with_free_ranges(GPtrArray *ranges,
115                                   uint64_t start, uint64_t end)
116 {
117     GPtrArray *free_ranges = g_ptr_array_new();
118     uint64_t free_base = start;
119     int i;
120 
121     g_ptr_array_sort(ranges, crs_range_compare);
122     for (i = 0; i < ranges->len; i++) {
123         CrsRangeEntry *used = g_ptr_array_index(ranges, i);
124 
125         if (free_base < used->base) {
126             crs_range_insert(free_ranges, free_base, used->base - 1);
127         }
128 
129         free_base = used->limit + 1;
130     }
131 
132     if (free_base < end) {
133         crs_range_insert(free_ranges, free_base, end);
134     }
135 
136     g_ptr_array_set_size(ranges, 0);
137     for (i = 0; i < free_ranges->len; i++) {
138         g_ptr_array_add(ranges, g_ptr_array_index(free_ranges, i));
139     }
140 
141     g_ptr_array_free(free_ranges, true);
142 }
143 
144 /*
145  * crs_range_merge - merges adjacent ranges in the given array.
146  * Array elements are deleted and replaced with the merged ranges.
147  */
148 static void crs_range_merge(GPtrArray *range)
149 {
150     GPtrArray *tmp = g_ptr_array_new_with_free_func(crs_range_free);
151     CrsRangeEntry *entry;
152     uint64_t range_base, range_limit;
153     int i;
154 
155     if (!range->len) {
156         return;
157     }
158 
159     g_ptr_array_sort(range, crs_range_compare);
160 
161     entry = g_ptr_array_index(range, 0);
162     range_base = entry->base;
163     range_limit = entry->limit;
164     for (i = 1; i < range->len; i++) {
165         entry = g_ptr_array_index(range, i);
166         if (entry->base - 1 == range_limit) {
167             range_limit = entry->limit;
168         } else {
169             crs_range_insert(tmp, range_base, range_limit);
170             range_base = entry->base;
171             range_limit = entry->limit;
172         }
173     }
174     crs_range_insert(tmp, range_base, range_limit);
175 
176     g_ptr_array_set_size(range, 0);
177     for (i = 0; i < tmp->len; i++) {
178         entry = g_ptr_array_index(tmp, i);
179         crs_range_insert(range, entry->base, entry->limit);
180     }
181     g_ptr_array_free(tmp, true);
182 }
183 
184 static void
185 build_append_nameseg(GArray *array, const char *seg)
186 {
187     int len;
188 
189     len = strlen(seg);
190     assert(len <= ACPI_NAMESEG_LEN);
191 
192     g_array_append_vals(array, seg, len);
193     /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */
194     g_array_append_vals(array, "____", ACPI_NAMESEG_LEN - len);
195 }
196 
197 static void GCC_FMT_ATTR(2, 0)
198 build_append_namestringv(GArray *array, const char *format, va_list ap)
199 {
200     char *s;
201     char **segs;
202     char **segs_iter;
203     int seg_count = 0;
204 
205     s = g_strdup_vprintf(format, ap);
206     segs = g_strsplit(s, ".", 0);
207     g_free(s);
208 
209     /* count segments */
210     segs_iter = segs;
211     while (*segs_iter) {
212         ++segs_iter;
213         ++seg_count;
214     }
215     /*
216      * ACPI 5.0 spec: 20.2.2 Name Objects Encoding:
217      * "SegCount can be from 1 to 255"
218      */
219     assert(seg_count > 0 && seg_count <= 255);
220 
221     /* handle RootPath || PrefixPath */
222     s = *segs;
223     while (*s == '\\' || *s == '^') {
224         build_append_byte(array, *s);
225         ++s;
226     }
227 
228     switch (seg_count) {
229     case 1:
230         if (!*s) {
231             build_append_byte(array, 0x00); /* NullName */
232         } else {
233             build_append_nameseg(array, s);
234         }
235         break;
236 
237     case 2:
238         build_append_byte(array, 0x2E); /* DualNamePrefix */
239         build_append_nameseg(array, s);
240         build_append_nameseg(array, segs[1]);
241         break;
242     default:
243         build_append_byte(array, 0x2F); /* MultiNamePrefix */
244         build_append_byte(array, seg_count);
245 
246         /* handle the 1st segment manually due to prefix/root path */
247         build_append_nameseg(array, s);
248 
249         /* add the rest of segments */
250         segs_iter = segs + 1;
251         while (*segs_iter) {
252             build_append_nameseg(array, *segs_iter);
253             ++segs_iter;
254         }
255         break;
256     }
257     g_strfreev(segs);
258 }
259 
260 GCC_FMT_ATTR(2, 3)
261 static void build_append_namestring(GArray *array, const char *format, ...)
262 {
263     va_list ap;
264 
265     va_start(ap, format);
266     build_append_namestringv(array, format, ap);
267     va_end(ap);
268 }
269 
270 /* 5.4 Definition Block Encoding */
271 enum {
272     PACKAGE_LENGTH_1BYTE_SHIFT = 6, /* Up to 63 - use extra 2 bits. */
273     PACKAGE_LENGTH_2BYTE_SHIFT = 4,
274     PACKAGE_LENGTH_3BYTE_SHIFT = 12,
275     PACKAGE_LENGTH_4BYTE_SHIFT = 20,
276 };
277 
278 static void
279 build_prepend_package_length(GArray *package, unsigned length, bool incl_self)
280 {
281     uint8_t byte;
282     unsigned length_bytes;
283 
284     if (length + 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT)) {
285         length_bytes = 1;
286     } else if (length + 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT)) {
287         length_bytes = 2;
288     } else if (length + 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT)) {
289         length_bytes = 3;
290     } else {
291         length_bytes = 4;
292     }
293 
294     /*
295      * NamedField uses PkgLength encoding but it doesn't include length
296      * of PkgLength itself.
297      */
298     if (incl_self) {
299         /*
300          * PkgLength is the length of the inclusive length of the data
301          * and PkgLength's length itself when used for terms with
302          * explitit length.
303          */
304         length += length_bytes;
305     }
306 
307     switch (length_bytes) {
308     case 1:
309         byte = length;
310         build_prepend_byte(package, byte);
311         return;
312     case 4:
313         byte = length >> PACKAGE_LENGTH_4BYTE_SHIFT;
314         build_prepend_byte(package, byte);
315         length &= (1 << PACKAGE_LENGTH_4BYTE_SHIFT) - 1;
316         /* fall through */
317     case 3:
318         byte = length >> PACKAGE_LENGTH_3BYTE_SHIFT;
319         build_prepend_byte(package, byte);
320         length &= (1 << PACKAGE_LENGTH_3BYTE_SHIFT) - 1;
321         /* fall through */
322     case 2:
323         byte = length >> PACKAGE_LENGTH_2BYTE_SHIFT;
324         build_prepend_byte(package, byte);
325         length &= (1 << PACKAGE_LENGTH_2BYTE_SHIFT) - 1;
326         /* fall through */
327     }
328     /*
329      * Most significant two bits of byte zero indicate how many following bytes
330      * are in PkgLength encoding.
331      */
332     byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length;
333     build_prepend_byte(package, byte);
334 }
335 
336 static void
337 build_append_pkg_length(GArray *array, unsigned length, bool incl_self)
338 {
339     GArray *tmp = build_alloc_array();
340 
341     build_prepend_package_length(tmp, length, incl_self);
342     build_append_array(array, tmp);
343     build_free_array(tmp);
344 }
345 
346 static void build_package(GArray *package, uint8_t op)
347 {
348     build_prepend_package_length(package, package->len, true);
349     build_prepend_byte(package, op);
350 }
351 
352 static void build_extop_package(GArray *package, uint8_t op)
353 {
354     build_package(package, op);
355     build_prepend_byte(package, 0x5B); /* ExtOpPrefix */
356 }
357 
358 void build_append_int_noprefix(GArray *table, uint64_t value, int size)
359 {
360     int i;
361 
362     for (i = 0; i < size; ++i) {
363         build_append_byte(table, value & 0xFF);
364         value = value >> 8;
365     }
366 }
367 
368 static void build_append_int(GArray *table, uint64_t value)
369 {
370     if (value == 0x00) {
371         build_append_byte(table, 0x00); /* ZeroOp */
372     } else if (value == 0x01) {
373         build_append_byte(table, 0x01); /* OneOp */
374     } else if (value <= 0xFF) {
375         build_append_byte(table, 0x0A); /* BytePrefix */
376         build_append_int_noprefix(table, value, 1);
377     } else if (value <= 0xFFFF) {
378         build_append_byte(table, 0x0B); /* WordPrefix */
379         build_append_int_noprefix(table, value, 2);
380     } else if (value <= 0xFFFFFFFF) {
381         build_append_byte(table, 0x0C); /* DWordPrefix */
382         build_append_int_noprefix(table, value, 4);
383     } else {
384         build_append_byte(table, 0x0E); /* QWordPrefix */
385         build_append_int_noprefix(table, value, 8);
386     }
387 }
388 
389 /* Generic Address Structure (GAS)
390  * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure
391  * 2.0 compat note:
392  *    @access_width must be 0, see ACPI 2.0:Table 5-1
393  */
394 void build_append_gas(GArray *table, AmlAddressSpace as,
395                       uint8_t bit_width, uint8_t bit_offset,
396                       uint8_t access_width, uint64_t address)
397 {
398     build_append_int_noprefix(table, as, 1);
399     build_append_int_noprefix(table, bit_width, 1);
400     build_append_int_noprefix(table, bit_offset, 1);
401     build_append_int_noprefix(table, access_width, 1);
402     build_append_int_noprefix(table, address, 8);
403 }
404 
405 /*
406  * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
407  * and return the offset to 0x00000000 for runtime patching.
408  *
409  * Warning: runtime patching is best avoided. Only use this as
410  * a replacement for DataTableRegion (for guests that don't
411  * support it).
412  */
413 int
414 build_append_named_dword(GArray *array, const char *name_format, ...)
415 {
416     int offset;
417     va_list ap;
418 
419     build_append_byte(array, 0x08); /* NameOp */
420     va_start(ap, name_format);
421     build_append_namestringv(array, name_format, ap);
422     va_end(ap);
423 
424     build_append_byte(array, 0x0C); /* DWordPrefix */
425 
426     offset = array->len;
427     build_append_int_noprefix(array, 0x00000000, 4);
428     assert(array->len == offset + 4);
429 
430     return offset;
431 }
432 
433 static GPtrArray *alloc_list;
434 
435 static Aml *aml_alloc(void)
436 {
437     Aml *var = g_new0(typeof(*var), 1);
438 
439     g_ptr_array_add(alloc_list, var);
440     var->block_flags = AML_NO_OPCODE;
441     var->buf = build_alloc_array();
442     return var;
443 }
444 
445 static Aml *aml_opcode(uint8_t op)
446 {
447     Aml *var = aml_alloc();
448 
449     var->op  = op;
450     var->block_flags = AML_OPCODE;
451     return var;
452 }
453 
454 static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags)
455 {
456     Aml *var = aml_alloc();
457 
458     var->op  = op;
459     var->block_flags = flags;
460     return var;
461 }
462 
463 static void aml_free(gpointer data, gpointer user_data)
464 {
465     Aml *var = data;
466     build_free_array(var->buf);
467     g_free(var);
468 }
469 
470 Aml *init_aml_allocator(void)
471 {
472     assert(!alloc_list);
473     alloc_list = g_ptr_array_new();
474     return aml_alloc();
475 }
476 
477 void free_aml_allocator(void)
478 {
479     g_ptr_array_foreach(alloc_list, aml_free, NULL);
480     g_ptr_array_free(alloc_list, true);
481     alloc_list = 0;
482 }
483 
484 /* pack data with DefBuffer encoding */
485 static void build_buffer(GArray *array, uint8_t op)
486 {
487     GArray *data = build_alloc_array();
488 
489     build_append_int(data, array->len);
490     g_array_prepend_vals(array, data->data, data->len);
491     build_free_array(data);
492     build_package(array, op);
493 }
494 
495 void aml_append(Aml *parent_ctx, Aml *child)
496 {
497     GArray *buf = build_alloc_array();
498     build_append_array(buf, child->buf);
499 
500     switch (child->block_flags) {
501     case AML_OPCODE:
502         build_append_byte(parent_ctx->buf, child->op);
503         break;
504     case AML_EXT_PACKAGE:
505         build_extop_package(buf, child->op);
506         break;
507     case AML_PACKAGE:
508         build_package(buf, child->op);
509         break;
510     case AML_RES_TEMPLATE:
511         build_append_byte(buf, 0x79); /* EndTag */
512         /*
513          * checksum operations are treated as succeeded if checksum
514          * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
515          */
516         build_append_byte(buf, 0);
517         /* fall through, to pack resources in buffer */
518     case AML_BUFFER:
519         build_buffer(buf, child->op);
520         break;
521     case AML_NO_OPCODE:
522         break;
523     default:
524         assert(0);
525         break;
526     }
527     build_append_array(parent_ctx->buf, buf);
528     build_free_array(buf);
529 }
530 
531 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
532 Aml *aml_scope(const char *name_format, ...)
533 {
534     va_list ap;
535     Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE);
536     va_start(ap, name_format);
537     build_append_namestringv(var->buf, name_format, ap);
538     va_end(ap);
539     return var;
540 }
541 
542 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
543 Aml *aml_return(Aml *val)
544 {
545     Aml *var = aml_opcode(0xA4 /* ReturnOp */);
546     aml_append(var, val);
547     return var;
548 }
549 
550 /* ACPI 1.0b: 16.2.6.3 Debug Objects Encoding: DebugObj */
551 Aml *aml_debug(void)
552 {
553     Aml *var = aml_alloc();
554     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
555     build_append_byte(var->buf, 0x31); /* DebugOp */
556     return var;
557 }
558 
559 /*
560  * ACPI 1.0b: 16.2.3 Data Objects Encoding:
561  * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
562  */
563 Aml *aml_int(const uint64_t val)
564 {
565     Aml *var = aml_alloc();
566     build_append_int(var->buf, val);
567     return var;
568 }
569 
570 /*
571  * helper to construct NameString, which returns Aml object
572  * for using with aml_append or other aml_* terms
573  */
574 Aml *aml_name(const char *name_format, ...)
575 {
576     va_list ap;
577     Aml *var = aml_alloc();
578     va_start(ap, name_format);
579     build_append_namestringv(var->buf, name_format, ap);
580     va_end(ap);
581     return var;
582 }
583 
584 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
585 Aml *aml_name_decl(const char *name, Aml *val)
586 {
587     Aml *var = aml_opcode(0x08 /* NameOp */);
588     build_append_namestring(var->buf, "%s", name);
589     aml_append(var, val);
590     return var;
591 }
592 
593 /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
594 Aml *aml_arg(int pos)
595 {
596     uint8_t op = 0x68 /* ARG0 op */ + pos;
597 
598     assert(pos <= 6);
599     return aml_opcode(op);
600 }
601 
602 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToInteger */
603 Aml *aml_to_integer(Aml *arg)
604 {
605     Aml *var = aml_opcode(0x99 /* ToIntegerOp */);
606     aml_append(var, arg);
607     build_append_byte(var->buf, 0x00 /* NullNameOp */);
608     return var;
609 }
610 
611 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToHexString */
612 Aml *aml_to_hexstring(Aml *src, Aml *dst)
613 {
614     Aml *var = aml_opcode(0x98 /* ToHexStringOp */);
615     aml_append(var, src);
616     if (dst) {
617         aml_append(var, dst);
618     } else {
619         build_append_byte(var->buf, 0x00 /* NullNameOp */);
620     }
621     return var;
622 }
623 
624 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToBuffer */
625 Aml *aml_to_buffer(Aml *src, Aml *dst)
626 {
627     Aml *var = aml_opcode(0x96 /* ToBufferOp */);
628     aml_append(var, src);
629     if (dst) {
630         aml_append(var, dst);
631     } else {
632         build_append_byte(var->buf, 0x00 /* NullNameOp */);
633     }
634     return var;
635 }
636 
637 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToDecimalString */
638 Aml *aml_to_decimalstring(Aml *src, Aml *dst)
639 {
640     Aml *var = aml_opcode(0x97 /* ToDecimalStringOp */);
641     aml_append(var, src);
642     if (dst) {
643         aml_append(var, dst);
644     } else {
645         build_append_byte(var->buf, 0x00 /* NullNameOp */);
646     }
647     return var;
648 }
649 
650 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
651 Aml *aml_store(Aml *val, Aml *target)
652 {
653     Aml *var = aml_opcode(0x70 /* StoreOp */);
654     aml_append(var, val);
655     aml_append(var, target);
656     return var;
657 }
658 
659 /**
660  * build_opcode_2arg_dst:
661  * @op: 1-byte opcode
662  * @arg1: 1st operand
663  * @arg2: 2nd operand
664  * @dst: optional target to store to, set to NULL if it's not required
665  *
666  * An internal helper to compose AML terms that have
667  *   "Op Operand Operand Target"
668  * pattern.
669  *
670  * Returns: The newly allocated and composed according to patter Aml object.
671  */
672 static Aml *
673 build_opcode_2arg_dst(uint8_t op, Aml *arg1, Aml *arg2, Aml *dst)
674 {
675     Aml *var = aml_opcode(op);
676     aml_append(var, arg1);
677     aml_append(var, arg2);
678     if (dst) {
679         aml_append(var, dst);
680     } else {
681         build_append_byte(var->buf, 0x00 /* NullNameOp */);
682     }
683     return var;
684 }
685 
686 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
687 Aml *aml_and(Aml *arg1, Aml *arg2, Aml *dst)
688 {
689     return build_opcode_2arg_dst(0x7B /* AndOp */, arg1, arg2, dst);
690 }
691 
692 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */
693 Aml *aml_or(Aml *arg1, Aml *arg2, Aml *dst)
694 {
695     return build_opcode_2arg_dst(0x7D /* OrOp */, arg1, arg2, dst);
696 }
697 
698 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLAnd */
699 Aml *aml_land(Aml *arg1, Aml *arg2)
700 {
701     Aml *var = aml_opcode(0x90 /* LAndOp */);
702     aml_append(var, arg1);
703     aml_append(var, arg2);
704     return var;
705 }
706 
707 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLOr */
708 Aml *aml_lor(Aml *arg1, Aml *arg2)
709 {
710     Aml *var = aml_opcode(0x91 /* LOrOp */);
711     aml_append(var, arg1);
712     aml_append(var, arg2);
713     return var;
714 }
715 
716 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftLeft */
717 Aml *aml_shiftleft(Aml *arg1, Aml *count)
718 {
719     return build_opcode_2arg_dst(0x79 /* ShiftLeftOp */, arg1, count, NULL);
720 }
721 
722 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftRight */
723 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst)
724 {
725     return build_opcode_2arg_dst(0x7A /* ShiftRightOp */, arg1, count, dst);
726 }
727 
728 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLess */
729 Aml *aml_lless(Aml *arg1, Aml *arg2)
730 {
731     Aml *var = aml_opcode(0x95 /* LLessOp */);
732     aml_append(var, arg1);
733     aml_append(var, arg2);
734     return var;
735 }
736 
737 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
738 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst)
739 {
740     return build_opcode_2arg_dst(0x72 /* AddOp */, arg1, arg2, dst);
741 }
742 
743 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSubtract */
744 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst)
745 {
746     return build_opcode_2arg_dst(0x74 /* SubtractOp */, arg1, arg2, dst);
747 }
748 
749 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIncrement */
750 Aml *aml_increment(Aml *arg)
751 {
752     Aml *var = aml_opcode(0x75 /* IncrementOp */);
753     aml_append(var, arg);
754     return var;
755 }
756 
757 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDecrement */
758 Aml *aml_decrement(Aml *arg)
759 {
760     Aml *var = aml_opcode(0x76 /* DecrementOp */);
761     aml_append(var, arg);
762     return var;
763 }
764 
765 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIndex */
766 Aml *aml_index(Aml *arg1, Aml *idx)
767 {
768     return build_opcode_2arg_dst(0x88 /* IndexOp */, arg1, idx, NULL);
769 }
770 
771 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
772 Aml *aml_notify(Aml *arg1, Aml *arg2)
773 {
774     Aml *var = aml_opcode(0x86 /* NotifyOp */);
775     aml_append(var, arg1);
776     aml_append(var, arg2);
777     return var;
778 }
779 
780 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefBreak */
781 Aml *aml_break(void)
782 {
783     Aml *var = aml_opcode(0xa5 /* BreakOp */);
784     return var;
785 }
786 
787 /* helper to call method without argument */
788 Aml *aml_call0(const char *method)
789 {
790     Aml *var = aml_alloc();
791     build_append_namestring(var->buf, "%s", method);
792     return var;
793 }
794 
795 /* helper to call method with 1 argument */
796 Aml *aml_call1(const char *method, Aml *arg1)
797 {
798     Aml *var = aml_alloc();
799     build_append_namestring(var->buf, "%s", method);
800     aml_append(var, arg1);
801     return var;
802 }
803 
804 /* helper to call method with 2 arguments */
805 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2)
806 {
807     Aml *var = aml_alloc();
808     build_append_namestring(var->buf, "%s", method);
809     aml_append(var, arg1);
810     aml_append(var, arg2);
811     return var;
812 }
813 
814 /* helper to call method with 3 arguments */
815 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3)
816 {
817     Aml *var = aml_alloc();
818     build_append_namestring(var->buf, "%s", method);
819     aml_append(var, arg1);
820     aml_append(var, arg2);
821     aml_append(var, arg3);
822     return var;
823 }
824 
825 /* helper to call method with 4 arguments */
826 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4)
827 {
828     Aml *var = aml_alloc();
829     build_append_namestring(var->buf, "%s", method);
830     aml_append(var, arg1);
831     aml_append(var, arg2);
832     aml_append(var, arg3);
833     aml_append(var, arg4);
834     return var;
835 }
836 
837 /* helper to call method with 5 arguments */
838 Aml *aml_call5(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4,
839                Aml *arg5)
840 {
841     Aml *var = aml_alloc();
842     build_append_namestring(var->buf, "%s", method);
843     aml_append(var, arg1);
844     aml_append(var, arg2);
845     aml_append(var, arg3);
846     aml_append(var, arg4);
847     aml_append(var, arg5);
848     return var;
849 }
850 
851 /* helper to call method with 5 arguments */
852 Aml *aml_call6(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4,
853                Aml *arg5, Aml *arg6)
854 {
855     Aml *var = aml_alloc();
856     build_append_namestring(var->buf, "%s", method);
857     aml_append(var, arg1);
858     aml_append(var, arg2);
859     aml_append(var, arg3);
860     aml_append(var, arg4);
861     aml_append(var, arg5);
862     aml_append(var, arg6);
863     return var;
864 }
865 
866 /*
867  * ACPI 5.0: 6.4.3.8.1 GPIO Connection Descriptor
868  * Type 1, Large Item Name 0xC
869  */
870 
871 static Aml *aml_gpio_connection(AmlGpioConnectionType type,
872                                 AmlConsumerAndProducer con_and_pro,
873                                 uint8_t flags, AmlPinConfig pin_config,
874                                 uint16_t output_drive,
875                                 uint16_t debounce_timeout,
876                                 const uint32_t pin_list[], uint32_t pin_count,
877                                 const char *resource_source_name,
878                                 const uint8_t *vendor_data,
879                                 uint16_t vendor_data_len)
880 {
881     Aml *var = aml_alloc();
882     const uint16_t min_desc_len = 0x16;
883     uint16_t resource_source_name_len, length;
884     uint16_t pin_table_offset, resource_source_name_offset, vendor_data_offset;
885     uint32_t i;
886 
887     assert(resource_source_name);
888     resource_source_name_len = strlen(resource_source_name) + 1;
889     length = min_desc_len + resource_source_name_len + vendor_data_len;
890     pin_table_offset = min_desc_len + 1;
891     resource_source_name_offset = pin_table_offset + pin_count * 2;
892     vendor_data_offset = resource_source_name_offset + resource_source_name_len;
893 
894     build_append_byte(var->buf, 0x8C);  /* GPIO Connection Descriptor */
895     build_append_int_noprefix(var->buf, length, 2); /* Length */
896     build_append_byte(var->buf, 1);     /* Revision ID */
897     build_append_byte(var->buf, type);  /* GPIO Connection Type */
898     /* General Flags (2 bytes) */
899     build_append_int_noprefix(var->buf, con_and_pro, 2);
900     /* Interrupt and IO Flags (2 bytes) */
901     build_append_int_noprefix(var->buf, flags, 2);
902     /* Pin Configuration 0 = Default 1 = Pull-up 2 = Pull-down 3 = No Pull */
903     build_append_byte(var->buf, pin_config);
904     /* Output Drive Strength (2 bytes) */
905     build_append_int_noprefix(var->buf, output_drive, 2);
906     /* Debounce Timeout (2 bytes) */
907     build_append_int_noprefix(var->buf, debounce_timeout, 2);
908     /* Pin Table Offset (2 bytes) */
909     build_append_int_noprefix(var->buf, pin_table_offset, 2);
910     build_append_byte(var->buf, 0);     /* Resource Source Index */
911     /* Resource Source Name Offset (2 bytes) */
912     build_append_int_noprefix(var->buf, resource_source_name_offset, 2);
913     /* Vendor Data Offset (2 bytes) */
914     build_append_int_noprefix(var->buf, vendor_data_offset, 2);
915     /* Vendor Data Length (2 bytes) */
916     build_append_int_noprefix(var->buf, vendor_data_len, 2);
917     /* Pin Number (2n bytes)*/
918     for (i = 0; i < pin_count; i++) {
919         build_append_int_noprefix(var->buf, pin_list[i], 2);
920     }
921 
922     /* Resource Source Name */
923     build_append_namestring(var->buf, "%s", resource_source_name);
924     build_append_byte(var->buf, '\0');
925 
926     /* Vendor-defined Data */
927     if (vendor_data != NULL) {
928         g_array_append_vals(var->buf, vendor_data, vendor_data_len);
929     }
930 
931     return var;
932 }
933 
934 /*
935  * ACPI 5.0: 19.5.53
936  * GpioInt(GPIO Interrupt Connection Resource Descriptor Macro)
937  */
938 Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro,
939                   AmlLevelAndEdge edge_level,
940                   AmlActiveHighAndLow active_level, AmlShared shared,
941                   AmlPinConfig pin_config, uint16_t debounce_timeout,
942                   const uint32_t pin_list[], uint32_t pin_count,
943                   const char *resource_source_name,
944                   const uint8_t *vendor_data, uint16_t vendor_data_len)
945 {
946     uint8_t flags = edge_level | (active_level << 1) | (shared << 3);
947 
948     return aml_gpio_connection(AML_INTERRUPT_CONNECTION, con_and_pro, flags,
949                                pin_config, 0, debounce_timeout, pin_list,
950                                pin_count, resource_source_name, vendor_data,
951                                vendor_data_len);
952 }
953 
954 /*
955  * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor
956  * (Type 1, Large Item Name 0x6)
957  */
958 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size,
959                         AmlReadAndWrite read_and_write)
960 {
961     Aml *var = aml_alloc();
962     build_append_byte(var->buf, 0x86); /* Memory32Fixed Resource Descriptor */
963     build_append_byte(var->buf, 9);    /* Length, bits[7:0] value = 9 */
964     build_append_byte(var->buf, 0);    /* Length, bits[15:8] value = 0 */
965     build_append_byte(var->buf, read_and_write); /* Write status, 1 rw 0 ro */
966 
967     /* Range base address */
968     build_append_byte(var->buf, extract32(addr, 0, 8));  /* bits[7:0] */
969     build_append_byte(var->buf, extract32(addr, 8, 8));  /* bits[15:8] */
970     build_append_byte(var->buf, extract32(addr, 16, 8)); /* bits[23:16] */
971     build_append_byte(var->buf, extract32(addr, 24, 8)); /* bits[31:24] */
972 
973     /* Range length */
974     build_append_byte(var->buf, extract32(size, 0, 8));  /* bits[7:0] */
975     build_append_byte(var->buf, extract32(size, 8, 8));  /* bits[15:8] */
976     build_append_byte(var->buf, extract32(size, 16, 8)); /* bits[23:16] */
977     build_append_byte(var->buf, extract32(size, 24, 8)); /* bits[31:24] */
978     return var;
979 }
980 
981 /*
982  * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor
983  * Type 1, Large Item Name 0x9
984  */
985 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro,
986                    AmlLevelAndEdge level_and_edge,
987                    AmlActiveHighAndLow high_and_low, AmlShared shared,
988                    uint32_t *irq_list, uint8_t irq_count)
989 {
990     int i;
991     Aml *var = aml_alloc();
992     uint8_t irq_flags = con_and_pro | (level_and_edge << 1)
993                         | (high_and_low << 2) | (shared << 3);
994     const int header_bytes_in_len = 2;
995     uint16_t len = header_bytes_in_len + irq_count * sizeof(uint32_t);
996 
997     assert(irq_count > 0);
998 
999     build_append_byte(var->buf, 0x89); /* Extended irq descriptor */
1000     build_append_byte(var->buf, len & 0xFF); /* Length, bits[7:0] */
1001     build_append_byte(var->buf, len >> 8); /* Length, bits[15:8] */
1002     build_append_byte(var->buf, irq_flags); /* Interrupt Vector Information. */
1003     build_append_byte(var->buf, irq_count);   /* Interrupt table length */
1004 
1005     /* Interrupt Number List */
1006     for (i = 0; i < irq_count; i++) {
1007         build_append_int_noprefix(var->buf, irq_list[i], 4);
1008     }
1009     return var;
1010 }
1011 
1012 /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */
1013 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
1014             uint8_t aln, uint8_t len)
1015 {
1016     Aml *var = aml_alloc();
1017     build_append_byte(var->buf, 0x47); /* IO port descriptor */
1018     build_append_byte(var->buf, dec);
1019     build_append_byte(var->buf, min_base & 0xff);
1020     build_append_byte(var->buf, (min_base >> 8) & 0xff);
1021     build_append_byte(var->buf, max_base & 0xff);
1022     build_append_byte(var->buf, (max_base >> 8) & 0xff);
1023     build_append_byte(var->buf, aln);
1024     build_append_byte(var->buf, len);
1025     return var;
1026 }
1027 
1028 /*
1029  * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor
1030  *
1031  * More verbose description at:
1032  * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro)
1033  *           6.4.2.1 IRQ Descriptor
1034  */
1035 Aml *aml_irq_no_flags(uint8_t irq)
1036 {
1037     uint16_t irq_mask;
1038     Aml *var = aml_alloc();
1039 
1040     assert(irq < 16);
1041     build_append_byte(var->buf, 0x22); /* IRQ descriptor 2 byte form */
1042 
1043     irq_mask = 1U << irq;
1044     build_append_byte(var->buf, irq_mask & 0xFF); /* IRQ mask bits[7:0] */
1045     build_append_byte(var->buf, irq_mask >> 8); /* IRQ mask bits[15:8] */
1046     return var;
1047 }
1048 
1049 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */
1050 Aml *aml_lnot(Aml *arg)
1051 {
1052     Aml *var = aml_opcode(0x92 /* LNotOp */);
1053     aml_append(var, arg);
1054     return var;
1055 }
1056 
1057 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */
1058 Aml *aml_equal(Aml *arg1, Aml *arg2)
1059 {
1060     Aml *var = aml_opcode(0x93 /* LequalOp */);
1061     aml_append(var, arg1);
1062     aml_append(var, arg2);
1063     return var;
1064 }
1065 
1066 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreater */
1067 Aml *aml_lgreater(Aml *arg1, Aml *arg2)
1068 {
1069     Aml *var = aml_opcode(0x94 /* LGreaterOp */);
1070     aml_append(var, arg1);
1071     aml_append(var, arg2);
1072     return var;
1073 }
1074 
1075 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreaterEqual */
1076 Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2)
1077 {
1078     /* LGreaterEqualOp := LNotOp LLessOp */
1079     Aml *var = aml_opcode(0x92 /* LNotOp */);
1080     build_append_byte(var->buf, 0x95 /* LLessOp */);
1081     aml_append(var, arg1);
1082     aml_append(var, arg2);
1083     return var;
1084 }
1085 
1086 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
1087 Aml *aml_if(Aml *predicate)
1088 {
1089     Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE);
1090     aml_append(var, predicate);
1091     return var;
1092 }
1093 
1094 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */
1095 Aml *aml_else(void)
1096 {
1097     Aml *var = aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE);
1098     return var;
1099 }
1100 
1101 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefWhile */
1102 Aml *aml_while(Aml *predicate)
1103 {
1104     Aml *var = aml_bundle(0xA2 /* WhileOp */, AML_PACKAGE);
1105     aml_append(var, predicate);
1106     return var;
1107 }
1108 
1109 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
1110 Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag)
1111 {
1112     Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE);
1113     int methodflags;
1114 
1115     /*
1116      * MethodFlags:
1117      *   bit 0-2: ArgCount (0-7)
1118      *   bit 3: SerializeFlag
1119      *     0: NotSerialized
1120      *     1: Serialized
1121      *   bit 4-7: reserved (must be 0)
1122      */
1123     assert(arg_count < 8);
1124     methodflags = arg_count | (sflag << 3);
1125 
1126     build_append_namestring(var->buf, "%s", name);
1127     build_append_byte(var->buf, methodflags); /* MethodFlags: ArgCount */
1128     return var;
1129 }
1130 
1131 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
1132 Aml *aml_device(const char *name_format, ...)
1133 {
1134     va_list ap;
1135     Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE);
1136     va_start(ap, name_format);
1137     build_append_namestringv(var->buf, name_format, ap);
1138     va_end(ap);
1139     return var;
1140 }
1141 
1142 /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */
1143 Aml *aml_resource_template(void)
1144 {
1145     /* ResourceTemplate is a buffer of Resources with EndTag at the end */
1146     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE);
1147     return var;
1148 }
1149 
1150 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer
1151  * Pass byte_list as NULL to request uninitialized buffer to reserve space.
1152  */
1153 Aml *aml_buffer(int buffer_size, uint8_t *byte_list)
1154 {
1155     int i;
1156     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1157 
1158     for (i = 0; i < buffer_size; i++) {
1159         if (byte_list == NULL) {
1160             build_append_byte(var->buf, 0x0);
1161         } else {
1162             build_append_byte(var->buf, byte_list[i]);
1163         }
1164     }
1165 
1166     return var;
1167 }
1168 
1169 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */
1170 Aml *aml_package(uint8_t num_elements)
1171 {
1172     Aml *var = aml_bundle(0x12 /* PackageOp */, AML_PACKAGE);
1173     build_append_byte(var->buf, num_elements);
1174     return var;
1175 }
1176 
1177 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */
1178 Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
1179                           Aml *offset, uint32_t len)
1180 {
1181     Aml *var = aml_alloc();
1182     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1183     build_append_byte(var->buf, 0x80); /* OpRegionOp */
1184     build_append_namestring(var->buf, "%s", name);
1185     build_append_byte(var->buf, rs);
1186     aml_append(var, offset);
1187     build_append_int(var->buf, len);
1188     return var;
1189 }
1190 
1191 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */
1192 Aml *aml_named_field(const char *name, unsigned length)
1193 {
1194     Aml *var = aml_alloc();
1195     build_append_nameseg(var->buf, name);
1196     build_append_pkg_length(var->buf, length, false);
1197     return var;
1198 }
1199 
1200 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */
1201 Aml *aml_reserved_field(unsigned length)
1202 {
1203     Aml *var = aml_alloc();
1204     /* ReservedField  := 0x00 PkgLength */
1205     build_append_byte(var->buf, 0x00);
1206     build_append_pkg_length(var->buf, length, false);
1207     return var;
1208 }
1209 
1210 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */
1211 Aml *aml_field(const char *name, AmlAccessType type, AmlLockRule lock,
1212                AmlUpdateRule rule)
1213 {
1214     Aml *var = aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE);
1215     uint8_t flags = rule << 5 | type;
1216 
1217     flags |= lock << 4; /* LockRule at 4 bit offset */
1218 
1219     build_append_namestring(var->buf, "%s", name);
1220     build_append_byte(var->buf, flags);
1221     return var;
1222 }
1223 
1224 static
1225 Aml *create_field_common(int opcode, Aml *srcbuf, Aml *index, const char *name)
1226 {
1227     Aml *var = aml_opcode(opcode);
1228     aml_append(var, srcbuf);
1229     aml_append(var, index);
1230     build_append_namestring(var->buf, "%s", name);
1231     return var;
1232 }
1233 
1234 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateField */
1235 Aml *aml_create_field(Aml *srcbuf, Aml *bit_index, Aml *num_bits,
1236                       const char *name)
1237 {
1238     Aml *var = aml_alloc();
1239     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1240     build_append_byte(var->buf, 0x13); /* CreateFieldOp */
1241     aml_append(var, srcbuf);
1242     aml_append(var, bit_index);
1243     aml_append(var, num_bits);
1244     build_append_namestring(var->buf, "%s", name);
1245     return var;
1246 }
1247 
1248 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */
1249 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name)
1250 {
1251     return create_field_common(0x8A /* CreateDWordFieldOp */,
1252                                srcbuf, index, name);
1253 }
1254 
1255 /* ACPI 2.0a: 17.2.4.2 Named Objects Encoding: DefCreateQWordField */
1256 Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name)
1257 {
1258     return create_field_common(0x8F /* CreateQWordFieldOp */,
1259                                srcbuf, index, name);
1260 }
1261 
1262 /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */
1263 Aml *aml_string(const char *name_format, ...)
1264 {
1265     Aml *var = aml_opcode(0x0D /* StringPrefix */);
1266     va_list ap;
1267     char *s;
1268     int len;
1269 
1270     va_start(ap, name_format);
1271     len = g_vasprintf(&s, name_format, ap);
1272     va_end(ap);
1273 
1274     g_array_append_vals(var->buf, s, len + 1);
1275     g_free(s);
1276 
1277     return var;
1278 }
1279 
1280 /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */
1281 Aml *aml_local(int num)
1282 {
1283     uint8_t op = 0x60 /* Local0Op */ + num;
1284 
1285     assert(num <= 7);
1286     return aml_opcode(op);
1287 }
1288 
1289 /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */
1290 Aml *aml_varpackage(uint32_t num_elements)
1291 {
1292     Aml *var = aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE);
1293     build_append_int(var->buf, num_elements);
1294     return var;
1295 }
1296 
1297 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */
1298 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
1299                    const char *name_format, ...)
1300 {
1301     va_list ap;
1302     Aml *var = aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE);
1303     va_start(ap, name_format);
1304     build_append_namestringv(var->buf, name_format, ap);
1305     va_end(ap);
1306     build_append_byte(var->buf, proc_id); /* ProcID */
1307     build_append_int_noprefix(var->buf, pblk_addr, sizeof(pblk_addr));
1308     build_append_byte(var->buf, pblk_len); /* PblkLen */
1309     return var;
1310 }
1311 
1312 static uint8_t Hex2Digit(char c)
1313 {
1314     if (c >= 'A') {
1315         return c - 'A' + 10;
1316     }
1317 
1318     return c - '0';
1319 }
1320 
1321 /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */
1322 Aml *aml_eisaid(const char *str)
1323 {
1324     Aml *var = aml_alloc();
1325     uint32_t id;
1326 
1327     g_assert(strlen(str) == 7);
1328     id = (str[0] - 0x40) << 26 |
1329     (str[1] - 0x40) << 21 |
1330     (str[2] - 0x40) << 16 |
1331     Hex2Digit(str[3]) << 12 |
1332     Hex2Digit(str[4]) << 8 |
1333     Hex2Digit(str[5]) << 4 |
1334     Hex2Digit(str[6]);
1335 
1336     build_append_byte(var->buf, 0x0C); /* DWordPrefix */
1337     build_append_int_noprefix(var->buf, bswap32(id), sizeof(id));
1338     return var;
1339 }
1340 
1341 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */
1342 static Aml *aml_as_desc_header(AmlResourceType type, AmlMinFixed min_fixed,
1343                                AmlMaxFixed max_fixed, AmlDecode dec,
1344                                uint8_t type_flags)
1345 {
1346     uint8_t flags = max_fixed | min_fixed | dec;
1347     Aml *var = aml_alloc();
1348 
1349     build_append_byte(var->buf, type);
1350     build_append_byte(var->buf, flags);
1351     build_append_byte(var->buf, type_flags); /* Type Specific Flags */
1352     return var;
1353 }
1354 
1355 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */
1356 static Aml *aml_word_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1357                              AmlMaxFixed max_fixed, AmlDecode dec,
1358                              uint16_t addr_gran, uint16_t addr_min,
1359                              uint16_t addr_max, uint16_t addr_trans,
1360                              uint16_t len, uint8_t type_flags)
1361 {
1362     Aml *var = aml_alloc();
1363 
1364     build_append_byte(var->buf, 0x88); /* Word Address Space Descriptor */
1365     /* minimum length since we do not encode optional fields */
1366     build_append_byte(var->buf, 0x0D);
1367     build_append_byte(var->buf, 0x0);
1368 
1369     aml_append(var,
1370         aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1371     build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1372     build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1373     build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1374     build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1375     build_append_int_noprefix(var->buf, len, sizeof(len));
1376     return var;
1377 }
1378 
1379 /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */
1380 static Aml *aml_dword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1381                               AmlMaxFixed max_fixed, AmlDecode dec,
1382                               uint32_t addr_gran, uint32_t addr_min,
1383                               uint32_t addr_max, uint32_t addr_trans,
1384                               uint32_t len, uint8_t type_flags)
1385 {
1386     Aml *var = aml_alloc();
1387 
1388     build_append_byte(var->buf, 0x87); /* DWord Address Space Descriptor */
1389     /* minimum length since we do not encode optional fields */
1390     build_append_byte(var->buf, 23);
1391     build_append_byte(var->buf, 0x0);
1392 
1393 
1394     aml_append(var,
1395         aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1396     build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1397     build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1398     build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1399     build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1400     build_append_int_noprefix(var->buf, len, sizeof(len));
1401     return var;
1402 }
1403 
1404 /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */
1405 static Aml *aml_qword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1406                               AmlMaxFixed max_fixed, AmlDecode dec,
1407                               uint64_t addr_gran, uint64_t addr_min,
1408                               uint64_t addr_max, uint64_t addr_trans,
1409                               uint64_t len, uint8_t type_flags)
1410 {
1411     Aml *var = aml_alloc();
1412 
1413     build_append_byte(var->buf, 0x8A); /* QWord Address Space Descriptor */
1414     /* minimum length since we do not encode optional fields */
1415     build_append_byte(var->buf, 0x2B);
1416     build_append_byte(var->buf, 0x0);
1417 
1418     aml_append(var,
1419         aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1420     build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1421     build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1422     build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1423     build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1424     build_append_int_noprefix(var->buf, len, sizeof(len));
1425     return var;
1426 }
1427 
1428 /*
1429  * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1430  *
1431  * More verbose description at:
1432  * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro)
1433  */
1434 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1435                          AmlDecode dec, uint16_t addr_gran,
1436                          uint16_t addr_min, uint16_t addr_max,
1437                          uint16_t addr_trans, uint16_t len)
1438 
1439 {
1440     return aml_word_as_desc(AML_BUS_NUMBER_RANGE, min_fixed, max_fixed, dec,
1441                             addr_gran, addr_min, addr_max, addr_trans, len, 0);
1442 }
1443 
1444 /*
1445  * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1446  *
1447  * More verbose description at:
1448  * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro)
1449  */
1450 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1451                  AmlDecode dec, AmlISARanges isa_ranges,
1452                  uint16_t addr_gran, uint16_t addr_min,
1453                  uint16_t addr_max, uint16_t addr_trans,
1454                  uint16_t len)
1455 
1456 {
1457     return aml_word_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1458                             addr_gran, addr_min, addr_max, addr_trans, len,
1459                             isa_ranges);
1460 }
1461 
1462 /*
1463  * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor
1464  *
1465  * More verbose description at:
1466  * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro)
1467  */
1468 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1469                  AmlDecode dec, AmlISARanges isa_ranges,
1470                  uint32_t addr_gran, uint32_t addr_min,
1471                  uint32_t addr_max, uint32_t addr_trans,
1472                  uint32_t len)
1473 
1474 {
1475     return aml_dword_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1476                             addr_gran, addr_min, addr_max, addr_trans, len,
1477                             isa_ranges);
1478 }
1479 
1480 /*
1481  * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor
1482  *
1483  * More verbose description at:
1484  * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro)
1485  */
1486 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1487                       AmlMaxFixed max_fixed, AmlCacheable cacheable,
1488                       AmlReadAndWrite read_and_write,
1489                       uint32_t addr_gran, uint32_t addr_min,
1490                       uint32_t addr_max, uint32_t addr_trans,
1491                       uint32_t len)
1492 {
1493     uint8_t flags = read_and_write | (cacheable << 1);
1494 
1495     return aml_dword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1496                              dec, addr_gran, addr_min, addr_max,
1497                              addr_trans, len, flags);
1498 }
1499 
1500 /*
1501  * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor
1502  *
1503  * More verbose description at:
1504  * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro)
1505  */
1506 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1507                       AmlMaxFixed max_fixed, AmlCacheable cacheable,
1508                       AmlReadAndWrite read_and_write,
1509                       uint64_t addr_gran, uint64_t addr_min,
1510                       uint64_t addr_max, uint64_t addr_trans,
1511                       uint64_t len)
1512 {
1513     uint8_t flags = read_and_write | (cacheable << 1);
1514 
1515     return aml_qword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1516                              dec, addr_gran, addr_min, addr_max,
1517                              addr_trans, len, flags);
1518 }
1519 
1520 /* ACPI 1.0b: 6.4.2.2 DMA Format/6.4.2.2.1 ASL Macro for DMA Descriptor */
1521 Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz,
1522              uint8_t channel)
1523 {
1524     Aml *var = aml_alloc();
1525     uint8_t flags = sz | bm << 2 | typ << 5;
1526 
1527     assert(channel < 8);
1528     build_append_byte(var->buf, 0x2A);    /* Byte 0: DMA Descriptor */
1529     build_append_byte(var->buf, 1U << channel); /* Byte 1: _DMA - DmaChannel */
1530     build_append_byte(var->buf, flags);   /* Byte 2 */
1531     return var;
1532 }
1533 
1534 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefSleep */
1535 Aml *aml_sleep(uint64_t msec)
1536 {
1537     Aml *var = aml_alloc();
1538     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1539     build_append_byte(var->buf, 0x22); /* SleepOp */
1540     aml_append(var, aml_int(msec));
1541     return var;
1542 }
1543 
1544 static uint8_t Hex2Byte(const char *src)
1545 {
1546     int hi, lo;
1547 
1548     hi = Hex2Digit(src[0]);
1549     assert(hi >= 0);
1550     assert(hi <= 15);
1551 
1552     lo = Hex2Digit(src[1]);
1553     assert(lo >= 0);
1554     assert(lo <= 15);
1555     return (hi << 4) | lo;
1556 }
1557 
1558 /*
1559  * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro)
1560  * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1561  * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp");
1562  */
1563 Aml *aml_touuid(const char *uuid)
1564 {
1565     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1566 
1567     assert(strlen(uuid) == 36);
1568     assert(uuid[8] == '-');
1569     assert(uuid[13] == '-');
1570     assert(uuid[18] == '-');
1571     assert(uuid[23] == '-');
1572 
1573     build_append_byte(var->buf, Hex2Byte(uuid + 6));  /* dd - at offset 00 */
1574     build_append_byte(var->buf, Hex2Byte(uuid + 4));  /* cc - at offset 01 */
1575     build_append_byte(var->buf, Hex2Byte(uuid + 2));  /* bb - at offset 02 */
1576     build_append_byte(var->buf, Hex2Byte(uuid + 0));  /* aa - at offset 03 */
1577 
1578     build_append_byte(var->buf, Hex2Byte(uuid + 11)); /* ff - at offset 04 */
1579     build_append_byte(var->buf, Hex2Byte(uuid + 9));  /* ee - at offset 05 */
1580 
1581     build_append_byte(var->buf, Hex2Byte(uuid + 16)); /* hh - at offset 06 */
1582     build_append_byte(var->buf, Hex2Byte(uuid + 14)); /* gg - at offset 07 */
1583 
1584     build_append_byte(var->buf, Hex2Byte(uuid + 19)); /* ii - at offset 08 */
1585     build_append_byte(var->buf, Hex2Byte(uuid + 21)); /* jj - at offset 09 */
1586 
1587     build_append_byte(var->buf, Hex2Byte(uuid + 24)); /* kk - at offset 10 */
1588     build_append_byte(var->buf, Hex2Byte(uuid + 26)); /* ll - at offset 11 */
1589     build_append_byte(var->buf, Hex2Byte(uuid + 28)); /* mm - at offset 12 */
1590     build_append_byte(var->buf, Hex2Byte(uuid + 30)); /* nn - at offset 13 */
1591     build_append_byte(var->buf, Hex2Byte(uuid + 32)); /* oo - at offset 14 */
1592     build_append_byte(var->buf, Hex2Byte(uuid + 34)); /* pp - at offset 15 */
1593 
1594     return var;
1595 }
1596 
1597 /*
1598  * ACPI 2.0b: 16.2.3.6.4.3  Unicode Macro (Convert Ascii String To Unicode)
1599  */
1600 Aml *aml_unicode(const char *str)
1601 {
1602     int i = 0;
1603     Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1604 
1605     do {
1606         build_append_byte(var->buf, str[i]);
1607         build_append_byte(var->buf, 0);
1608         i++;
1609     } while (i <= strlen(str));
1610 
1611     return var;
1612 }
1613 
1614 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefRefOf */
1615 Aml *aml_refof(Aml *arg)
1616 {
1617     Aml *var = aml_opcode(0x71 /* RefOfOp */);
1618     aml_append(var, arg);
1619     return var;
1620 }
1621 
1622 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDerefOf */
1623 Aml *aml_derefof(Aml *arg)
1624 {
1625     Aml *var = aml_opcode(0x83 /* DerefOfOp */);
1626     aml_append(var, arg);
1627     return var;
1628 }
1629 
1630 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSizeOf */
1631 Aml *aml_sizeof(Aml *arg)
1632 {
1633     Aml *var = aml_opcode(0x87 /* SizeOfOp */);
1634     aml_append(var, arg);
1635     return var;
1636 }
1637 
1638 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMutex */
1639 Aml *aml_mutex(const char *name, uint8_t sync_level)
1640 {
1641     Aml *var = aml_alloc();
1642     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1643     build_append_byte(var->buf, 0x01); /* MutexOp */
1644     build_append_namestring(var->buf, "%s", name);
1645     assert(!(sync_level & 0xF0));
1646     build_append_byte(var->buf, sync_level);
1647     return var;
1648 }
1649 
1650 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAcquire */
1651 Aml *aml_acquire(Aml *mutex, uint16_t timeout)
1652 {
1653     Aml *var = aml_alloc();
1654     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1655     build_append_byte(var->buf, 0x23); /* AcquireOp */
1656     aml_append(var, mutex);
1657     build_append_int_noprefix(var->buf, timeout, sizeof(timeout));
1658     return var;
1659 }
1660 
1661 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefRelease */
1662 Aml *aml_release(Aml *mutex)
1663 {
1664     Aml *var = aml_alloc();
1665     build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1666     build_append_byte(var->buf, 0x27); /* ReleaseOp */
1667     aml_append(var, mutex);
1668     return var;
1669 }
1670 
1671 /* ACPI 1.0b: 16.2.5.1 Name Space Modifier Objects Encoding: DefAlias */
1672 Aml *aml_alias(const char *source_object, const char *alias_object)
1673 {
1674     Aml *var = aml_opcode(0x06 /* AliasOp */);
1675     aml_append(var, aml_name("%s", source_object));
1676     aml_append(var, aml_name("%s", alias_object));
1677     return var;
1678 }
1679 
1680 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefConcat */
1681 Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target)
1682 {
1683     return build_opcode_2arg_dst(0x73 /* ConcatOp */, source1, source2,
1684                                  target);
1685 }
1686 
1687 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefObjectType */
1688 Aml *aml_object_type(Aml *object)
1689 {
1690     Aml *var = aml_opcode(0x8E /* ObjectTypeOp */);
1691     aml_append(var, object);
1692     return var;
1693 }
1694 
1695 void
1696 build_header(BIOSLinker *linker, GArray *table_data,
1697              AcpiTableHeader *h, const char *sig, int len, uint8_t rev,
1698              const char *oem_id, const char *oem_table_id)
1699 {
1700     unsigned tbl_offset = (char *)h - table_data->data;
1701     unsigned checksum_offset = (char *)&h->checksum - table_data->data;
1702     memcpy(&h->signature, sig, 4);
1703     h->length = cpu_to_le32(len);
1704     h->revision = rev;
1705 
1706     strpadcpy((char *)h->oem_id, sizeof h->oem_id, oem_id, ' ');
1707     strpadcpy((char *)h->oem_table_id, sizeof h->oem_table_id,
1708               oem_table_id, ' ');
1709 
1710     h->oem_revision = cpu_to_le32(1);
1711     memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME8, 4);
1712     h->asl_compiler_revision = cpu_to_le32(1);
1713     /* Checksum to be filled in by Guest linker */
1714     bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE,
1715         tbl_offset, len, checksum_offset);
1716 }
1717 
1718 void *acpi_data_push(GArray *table_data, unsigned size)
1719 {
1720     unsigned off = table_data->len;
1721     g_array_set_size(table_data, off + size);
1722     return table_data->data + off;
1723 }
1724 
1725 unsigned acpi_data_len(GArray *table)
1726 {
1727     assert(g_array_get_element_size(table) == 1);
1728     return table->len;
1729 }
1730 
1731 void acpi_add_table(GArray *table_offsets, GArray *table_data)
1732 {
1733     uint32_t offset = table_data->len;
1734     g_array_append_val(table_offsets, offset);
1735 }
1736 
1737 void acpi_build_tables_init(AcpiBuildTables *tables)
1738 {
1739     tables->rsdp = g_array_new(false, true /* clear */, 1);
1740     tables->table_data = g_array_new(false, true /* clear */, 1);
1741     tables->tcpalog = g_array_new(false, true /* clear */, 1);
1742     tables->vmgenid = g_array_new(false, true /* clear */, 1);
1743     tables->hardware_errors = g_array_new(false, true /* clear */, 1);
1744     tables->linker = bios_linker_loader_init();
1745 }
1746 
1747 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
1748 {
1749     bios_linker_loader_cleanup(tables->linker);
1750     g_array_free(tables->rsdp, true);
1751     g_array_free(tables->table_data, true);
1752     g_array_free(tables->tcpalog, mfre);
1753     g_array_free(tables->vmgenid, mfre);
1754     g_array_free(tables->hardware_errors, mfre);
1755 }
1756 
1757 /*
1758  * ACPI spec 5.2.5.3 Root System Description Pointer (RSDP).
1759  * (Revision 1.0 or later)
1760  */
1761 void
1762 build_rsdp(GArray *tbl, BIOSLinker *linker, AcpiRsdpData *rsdp_data)
1763 {
1764     int tbl_off = tbl->len; /* Table offset in the RSDP file */
1765 
1766     switch (rsdp_data->revision) {
1767     case 0:
1768         /* With ACPI 1.0, we must have an RSDT pointer */
1769         g_assert(rsdp_data->rsdt_tbl_offset);
1770         break;
1771     case 2:
1772         /* With ACPI 2.0+, we must have an XSDT pointer */
1773         g_assert(rsdp_data->xsdt_tbl_offset);
1774         break;
1775     default:
1776         /* Only revisions 0 (ACPI 1.0) and 2 (ACPI 2.0+) are valid for RSDP */
1777         g_assert_not_reached();
1778     }
1779 
1780     bios_linker_loader_alloc(linker, ACPI_BUILD_RSDP_FILE, tbl, 16,
1781                              true /* fseg memory */);
1782 
1783     g_array_append_vals(tbl, "RSD PTR ", 8); /* Signature */
1784     build_append_int_noprefix(tbl, 0, 1); /* Checksum */
1785     g_array_append_vals(tbl, rsdp_data->oem_id, 6); /* OEMID */
1786     build_append_int_noprefix(tbl, rsdp_data->revision, 1); /* Revision */
1787     build_append_int_noprefix(tbl, 0, 4); /* RsdtAddress */
1788     if (rsdp_data->rsdt_tbl_offset) {
1789         /* RSDT address to be filled by guest linker */
1790         bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE,
1791                                        tbl_off + 16, 4,
1792                                        ACPI_BUILD_TABLE_FILE,
1793                                        *rsdp_data->rsdt_tbl_offset);
1794     }
1795 
1796     /* Checksum to be filled by guest linker */
1797     bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE,
1798                                     tbl_off, 20, /* ACPI rev 1.0 RSDP size */
1799                                     8);
1800 
1801     if (rsdp_data->revision == 0) {
1802         /* ACPI 1.0 RSDP, we're done */
1803         return;
1804     }
1805 
1806     build_append_int_noprefix(tbl, 36, 4); /* Length */
1807 
1808     /* XSDT address to be filled by guest linker */
1809     build_append_int_noprefix(tbl, 0, 8); /* XsdtAddress */
1810     /* We already validated our xsdt pointer */
1811     bios_linker_loader_add_pointer(linker, ACPI_BUILD_RSDP_FILE,
1812                                    tbl_off + 24, 8,
1813                                    ACPI_BUILD_TABLE_FILE,
1814                                    *rsdp_data->xsdt_tbl_offset);
1815 
1816     build_append_int_noprefix(tbl, 0, 1); /* Extended Checksum */
1817     build_append_int_noprefix(tbl, 0, 3); /* Reserved */
1818 
1819     /* Extended checksum to be filled by Guest linker */
1820     bios_linker_loader_add_checksum(linker, ACPI_BUILD_RSDP_FILE,
1821                                     tbl_off, 36, /* ACPI rev 2.0 RSDP size */
1822                                     32);
1823 }
1824 
1825 /* Build rsdt table */
1826 void
1827 build_rsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets,
1828            const char *oem_id, const char *oem_table_id)
1829 {
1830     int i;
1831     unsigned rsdt_entries_offset;
1832     AcpiRsdtDescriptorRev1 *rsdt;
1833     const unsigned table_data_len = (sizeof(uint32_t) * table_offsets->len);
1834     const unsigned rsdt_entry_size = sizeof(rsdt->table_offset_entry[0]);
1835     const size_t rsdt_len = sizeof(*rsdt) + table_data_len;
1836 
1837     rsdt = acpi_data_push(table_data, rsdt_len);
1838     rsdt_entries_offset = (char *)rsdt->table_offset_entry - table_data->data;
1839     for (i = 0; i < table_offsets->len; ++i) {
1840         uint32_t ref_tbl_offset = g_array_index(table_offsets, uint32_t, i);
1841         uint32_t rsdt_entry_offset = rsdt_entries_offset + rsdt_entry_size * i;
1842 
1843         /* rsdt->table_offset_entry to be filled by Guest linker */
1844         bios_linker_loader_add_pointer(linker,
1845             ACPI_BUILD_TABLE_FILE, rsdt_entry_offset, rsdt_entry_size,
1846             ACPI_BUILD_TABLE_FILE, ref_tbl_offset);
1847     }
1848     build_header(linker, table_data,
1849                  (void *)rsdt, "RSDT", rsdt_len, 1, oem_id, oem_table_id);
1850 }
1851 
1852 /* Build xsdt table */
1853 void
1854 build_xsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets,
1855            const char *oem_id, const char *oem_table_id)
1856 {
1857     int i;
1858     unsigned xsdt_entries_offset;
1859     AcpiXsdtDescriptorRev2 *xsdt;
1860     const unsigned table_data_len = (sizeof(uint64_t) * table_offsets->len);
1861     const unsigned xsdt_entry_size = sizeof(xsdt->table_offset_entry[0]);
1862     const size_t xsdt_len = sizeof(*xsdt) + table_data_len;
1863 
1864     xsdt = acpi_data_push(table_data, xsdt_len);
1865     xsdt_entries_offset = (char *)xsdt->table_offset_entry - table_data->data;
1866     for (i = 0; i < table_offsets->len; ++i) {
1867         uint64_t ref_tbl_offset = g_array_index(table_offsets, uint32_t, i);
1868         uint64_t xsdt_entry_offset = xsdt_entries_offset + xsdt_entry_size * i;
1869 
1870         /* xsdt->table_offset_entry to be filled by Guest linker */
1871         bios_linker_loader_add_pointer(linker,
1872             ACPI_BUILD_TABLE_FILE, xsdt_entry_offset, xsdt_entry_size,
1873             ACPI_BUILD_TABLE_FILE, ref_tbl_offset);
1874     }
1875     build_header(linker, table_data,
1876                  (void *)xsdt, "XSDT", xsdt_len, 1, oem_id, oem_table_id);
1877 }
1878 
1879 void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
1880                        uint64_t len, int node, MemoryAffinityFlags flags)
1881 {
1882     numamem->type = ACPI_SRAT_MEMORY;
1883     numamem->length = sizeof(*numamem);
1884     numamem->proximity = cpu_to_le32(node);
1885     numamem->flags = cpu_to_le32(flags);
1886     numamem->base_addr = cpu_to_le64(base);
1887     numamem->range_length = cpu_to_le64(len);
1888 }
1889 
1890 /*
1891  * ACPI spec 5.2.17 System Locality Distance Information Table
1892  * (Revision 2.0 or later)
1893  */
1894 void build_slit(GArray *table_data, BIOSLinker *linker, MachineState *ms,
1895                 const char *oem_id, const char *oem_table_id)
1896 {
1897     int slit_start, i, j;
1898     slit_start = table_data->len;
1899     int nb_numa_nodes = ms->numa_state->num_nodes;
1900 
1901     acpi_data_push(table_data, sizeof(AcpiTableHeader));
1902 
1903     build_append_int_noprefix(table_data, nb_numa_nodes, 8);
1904     for (i = 0; i < nb_numa_nodes; i++) {
1905         for (j = 0; j < nb_numa_nodes; j++) {
1906             assert(ms->numa_state->nodes[i].distance[j]);
1907             build_append_int_noprefix(table_data,
1908                                       ms->numa_state->nodes[i].distance[j],
1909                                       1);
1910         }
1911     }
1912 
1913     build_header(linker, table_data,
1914                  (void *)(table_data->data + slit_start),
1915                  "SLIT",
1916                  table_data->len - slit_start, 1, oem_id, oem_table_id);
1917 }
1918 
1919 /* build rev1/rev3/rev5.1 FADT */
1920 void build_fadt(GArray *tbl, BIOSLinker *linker, const AcpiFadtData *f,
1921                 const char *oem_id, const char *oem_table_id)
1922 {
1923     int off;
1924     int fadt_start = tbl->len;
1925 
1926     acpi_data_push(tbl, sizeof(AcpiTableHeader));
1927 
1928     /* FACS address to be filled by Guest linker at runtime */
1929     off = tbl->len;
1930     build_append_int_noprefix(tbl, 0, 4); /* FIRMWARE_CTRL */
1931     if (f->facs_tbl_offset) { /* don't patch if not supported by platform */
1932         bios_linker_loader_add_pointer(linker,
1933             ACPI_BUILD_TABLE_FILE, off, 4,
1934             ACPI_BUILD_TABLE_FILE, *f->facs_tbl_offset);
1935     }
1936 
1937     /* DSDT address to be filled by Guest linker at runtime */
1938     off = tbl->len;
1939     build_append_int_noprefix(tbl, 0, 4); /* DSDT */
1940     if (f->dsdt_tbl_offset) { /* don't patch if not supported by platform */
1941         bios_linker_loader_add_pointer(linker,
1942             ACPI_BUILD_TABLE_FILE, off, 4,
1943             ACPI_BUILD_TABLE_FILE, *f->dsdt_tbl_offset);
1944     }
1945 
1946     /* ACPI1.0: INT_MODEL, ACPI2.0+: Reserved */
1947     build_append_int_noprefix(tbl, f->int_model /* Multiple APIC */, 1);
1948     /* Preferred_PM_Profile */
1949     build_append_int_noprefix(tbl, 0 /* Unspecified */, 1);
1950     build_append_int_noprefix(tbl, f->sci_int, 2); /* SCI_INT */
1951     build_append_int_noprefix(tbl, f->smi_cmd, 4); /* SMI_CMD */
1952     build_append_int_noprefix(tbl, f->acpi_enable_cmd, 1); /* ACPI_ENABLE */
1953     build_append_int_noprefix(tbl, f->acpi_disable_cmd, 1); /* ACPI_DISABLE */
1954     build_append_int_noprefix(tbl, 0 /* not supported */, 1); /* S4BIOS_REQ */
1955     /* ACPI1.0: Reserved, ACPI2.0+: PSTATE_CNT */
1956     build_append_int_noprefix(tbl, 0, 1);
1957     build_append_int_noprefix(tbl, f->pm1a_evt.address, 4); /* PM1a_EVT_BLK */
1958     build_append_int_noprefix(tbl, 0, 4); /* PM1b_EVT_BLK */
1959     build_append_int_noprefix(tbl, f->pm1a_cnt.address, 4); /* PM1a_CNT_BLK */
1960     build_append_int_noprefix(tbl, 0, 4); /* PM1b_CNT_BLK */
1961     build_append_int_noprefix(tbl, 0, 4); /* PM2_CNT_BLK */
1962     build_append_int_noprefix(tbl, f->pm_tmr.address, 4); /* PM_TMR_BLK */
1963     build_append_int_noprefix(tbl, f->gpe0_blk.address, 4); /* GPE0_BLK */
1964     build_append_int_noprefix(tbl, 0, 4); /* GPE1_BLK */
1965     /* PM1_EVT_LEN */
1966     build_append_int_noprefix(tbl, f->pm1a_evt.bit_width / 8, 1);
1967     /* PM1_CNT_LEN */
1968     build_append_int_noprefix(tbl, f->pm1a_cnt.bit_width / 8, 1);
1969     build_append_int_noprefix(tbl, 0, 1); /* PM2_CNT_LEN */
1970     build_append_int_noprefix(tbl, f->pm_tmr.bit_width / 8, 1); /* PM_TMR_LEN */
1971     /* GPE0_BLK_LEN */
1972     build_append_int_noprefix(tbl, f->gpe0_blk.bit_width / 8, 1);
1973     build_append_int_noprefix(tbl, 0, 1); /* GPE1_BLK_LEN */
1974     build_append_int_noprefix(tbl, 0, 1); /* GPE1_BASE */
1975     build_append_int_noprefix(tbl, 0, 1); /* CST_CNT */
1976     build_append_int_noprefix(tbl, f->plvl2_lat, 2); /* P_LVL2_LAT */
1977     build_append_int_noprefix(tbl, f->plvl3_lat, 2); /* P_LVL3_LAT */
1978     build_append_int_noprefix(tbl, 0, 2); /* FLUSH_SIZE */
1979     build_append_int_noprefix(tbl, 0, 2); /* FLUSH_STRIDE */
1980     build_append_int_noprefix(tbl, 0, 1); /* DUTY_OFFSET */
1981     build_append_int_noprefix(tbl, 0, 1); /* DUTY_WIDTH */
1982     build_append_int_noprefix(tbl, 0, 1); /* DAY_ALRM */
1983     build_append_int_noprefix(tbl, 0, 1); /* MON_ALRM */
1984     build_append_int_noprefix(tbl, f->rtc_century, 1); /* CENTURY */
1985     build_append_int_noprefix(tbl, 0, 2); /* IAPC_BOOT_ARCH */
1986     build_append_int_noprefix(tbl, 0, 1); /* Reserved */
1987     build_append_int_noprefix(tbl, f->flags, 4); /* Flags */
1988 
1989     if (f->rev == 1) {
1990         goto build_hdr;
1991     }
1992 
1993     build_append_gas_from_struct(tbl, &f->reset_reg); /* RESET_REG */
1994     build_append_int_noprefix(tbl, f->reset_val, 1); /* RESET_VALUE */
1995     /* Since ACPI 5.1 */
1996     if ((f->rev >= 6) || ((f->rev == 5) && f->minor_ver > 0)) {
1997         build_append_int_noprefix(tbl, f->arm_boot_arch, 2); /* ARM_BOOT_ARCH */
1998         /* FADT Minor Version */
1999         build_append_int_noprefix(tbl, f->minor_ver, 1);
2000     } else {
2001         build_append_int_noprefix(tbl, 0, 3); /* Reserved upto ACPI 5.0 */
2002     }
2003     build_append_int_noprefix(tbl, 0, 8); /* X_FIRMWARE_CTRL */
2004 
2005     /* XDSDT address to be filled by Guest linker at runtime */
2006     off = tbl->len;
2007     build_append_int_noprefix(tbl, 0, 8); /* X_DSDT */
2008     if (f->xdsdt_tbl_offset) {
2009         bios_linker_loader_add_pointer(linker,
2010             ACPI_BUILD_TABLE_FILE, off, 8,
2011             ACPI_BUILD_TABLE_FILE, *f->xdsdt_tbl_offset);
2012     }
2013 
2014     build_append_gas_from_struct(tbl, &f->pm1a_evt); /* X_PM1a_EVT_BLK */
2015     /* X_PM1b_EVT_BLK */
2016     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
2017     build_append_gas_from_struct(tbl, &f->pm1a_cnt); /* X_PM1a_CNT_BLK */
2018     /* X_PM1b_CNT_BLK */
2019     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
2020     /* X_PM2_CNT_BLK */
2021     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
2022     build_append_gas_from_struct(tbl, &f->pm_tmr); /* X_PM_TMR_BLK */
2023     build_append_gas_from_struct(tbl, &f->gpe0_blk); /* X_GPE0_BLK */
2024     build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0); /* X_GPE1_BLK */
2025 
2026     if (f->rev <= 4) {
2027         goto build_hdr;
2028     }
2029 
2030     /* SLEEP_CONTROL_REG */
2031     build_append_gas_from_struct(tbl, &f->sleep_ctl);
2032     /* SLEEP_STATUS_REG */
2033     build_append_gas_from_struct(tbl, &f->sleep_sts);
2034 
2035     /* TODO: extra fields need to be added to support revisions above rev5 */
2036     assert(f->rev == 5);
2037 
2038 build_hdr:
2039     build_header(linker, tbl, (void *)(tbl->data + fadt_start),
2040                  "FACP", tbl->len - fadt_start, f->rev, oem_id, oem_table_id);
2041 }
2042 
2043 /*
2044  * build_tpm2 - Build the TPM2 table as specified in
2045  * table 7: TCG Hardware Interface Description Table Format for TPM 2.0
2046  * of TCG ACPI Specification, Family “1.2” and “2.0”, Version 1.2, Rev 8
2047  */
2048 void build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
2049                 const char *oem_id, const char *oem_table_id)
2050 {
2051     uint8_t start_method_params[12] = {};
2052     unsigned log_addr_offset, tpm2_start;
2053     uint64_t control_area_start_address;
2054     TPMIf *tpmif = tpm_find();
2055     uint32_t start_method;
2056     void *tpm2_ptr;
2057 
2058     tpm2_start = table_data->len;
2059     tpm2_ptr = acpi_data_push(table_data, sizeof(AcpiTableHeader));
2060 
2061     /* Platform Class */
2062     build_append_int_noprefix(table_data, TPM2_ACPI_CLASS_CLIENT, 2);
2063     /* Reserved */
2064     build_append_int_noprefix(table_data, 0, 2);
2065     if (TPM_IS_TIS_ISA(tpmif) || TPM_IS_TIS_SYSBUS(tpmif)) {
2066         control_area_start_address = 0;
2067         start_method = TPM2_START_METHOD_MMIO;
2068     } else if (TPM_IS_CRB(tpmif)) {
2069         control_area_start_address = TPM_CRB_ADDR_CTRL;
2070         start_method = TPM2_START_METHOD_CRB;
2071     } else {
2072         g_assert_not_reached();
2073     }
2074     /* Address of Control Area */
2075     build_append_int_noprefix(table_data, control_area_start_address, 8);
2076     /* Start Method */
2077     build_append_int_noprefix(table_data, start_method, 4);
2078 
2079     /* Platform Specific Parameters */
2080     g_array_append_vals(table_data, &start_method_params,
2081                         ARRAY_SIZE(start_method_params));
2082 
2083     /* Log Area Minimum Length */
2084     build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
2085 
2086     acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
2087     bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
2088                              false);
2089 
2090     log_addr_offset = table_data->len;
2091 
2092     /* Log Area Start Address to be filled by Guest linker */
2093     build_append_int_noprefix(table_data, 0, 8);
2094     bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
2095                                    log_addr_offset, 8,
2096                                    ACPI_BUILD_TPMLOG_FILE, 0);
2097     build_header(linker, table_data,
2098                  tpm2_ptr, "TPM2", table_data->len - tpm2_start, 4, oem_id,
2099                  oem_table_id);
2100 }
2101 
2102 Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set, uint32_t io_offset,
2103                uint32_t mmio32_offset, uint64_t mmio64_offset,
2104                uint16_t bus_nr_offset)
2105 {
2106     Aml *crs = aml_resource_template();
2107     CrsRangeSet temp_range_set;
2108     CrsRangeEntry *entry;
2109     uint8_t max_bus = pci_bus_num(host->bus);
2110     uint8_t type;
2111     int devfn;
2112     int i;
2113 
2114     crs_range_set_init(&temp_range_set);
2115     for (devfn = 0; devfn < ARRAY_SIZE(host->bus->devices); devfn++) {
2116         uint64_t range_base, range_limit;
2117         PCIDevice *dev = host->bus->devices[devfn];
2118 
2119         if (!dev) {
2120             continue;
2121         }
2122 
2123         for (i = 0; i < PCI_NUM_REGIONS; i++) {
2124             PCIIORegion *r = &dev->io_regions[i];
2125 
2126             range_base = r->addr;
2127             range_limit = r->addr + r->size - 1;
2128 
2129             /*
2130              * Work-around for old bioses
2131              * that do not support multiple root buses
2132              */
2133             if (!range_base || range_base > range_limit) {
2134                 continue;
2135             }
2136 
2137             if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
2138                 crs_range_insert(temp_range_set.io_ranges,
2139                                  range_base, range_limit);
2140             } else { /* "memory" */
2141                 uint64_t length = range_limit - range_base + 1;
2142                 if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
2143                     crs_range_insert(temp_range_set.mem_ranges, range_base,
2144                                      range_limit);
2145                 } else {
2146                     crs_range_insert(temp_range_set.mem_64bit_ranges,
2147                                      range_base, range_limit);
2148                 }
2149             }
2150         }
2151 
2152         type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
2153         if (type == PCI_HEADER_TYPE_BRIDGE) {
2154             uint8_t subordinate = dev->config[PCI_SUBORDINATE_BUS];
2155             if (subordinate > max_bus) {
2156                 max_bus = subordinate;
2157             }
2158 
2159             range_base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
2160             range_limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
2161 
2162              /*
2163               * Work-around for old bioses
2164               * that do not support multiple root buses
2165               */
2166             if (range_base && range_base <= range_limit) {
2167                 crs_range_insert(temp_range_set.io_ranges,
2168                                  range_base, range_limit);
2169             }
2170 
2171             range_base =
2172                 pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
2173             range_limit =
2174                 pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
2175 
2176             /*
2177              * Work-around for old bioses
2178              * that do not support multiple root buses
2179              */
2180             if (range_base && range_base <= range_limit) {
2181                 uint64_t length = range_limit - range_base + 1;
2182                 if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
2183                     crs_range_insert(temp_range_set.mem_ranges,
2184                                      range_base, range_limit);
2185                 } else {
2186                     crs_range_insert(temp_range_set.mem_64bit_ranges,
2187                                      range_base, range_limit);
2188                 }
2189             }
2190 
2191             range_base =
2192                 pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
2193             range_limit =
2194                 pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
2195 
2196             /*
2197              * Work-around for old bioses
2198              * that do not support multiple root buses
2199              */
2200             if (range_base && range_base <= range_limit) {
2201                 uint64_t length = range_limit - range_base + 1;
2202                 if (range_limit <= UINT32_MAX && length <= UINT32_MAX) {
2203                     crs_range_insert(temp_range_set.mem_ranges,
2204                                      range_base, range_limit);
2205                 } else {
2206                     crs_range_insert(temp_range_set.mem_64bit_ranges,
2207                                      range_base, range_limit);
2208                 }
2209             }
2210         }
2211     }
2212 
2213     crs_range_merge(temp_range_set.io_ranges);
2214     for (i = 0; i < temp_range_set.io_ranges->len; i++) {
2215         entry = g_ptr_array_index(temp_range_set.io_ranges, i);
2216         aml_append(crs,
2217                    aml_dword_io(AML_MIN_FIXED, AML_MAX_FIXED,
2218                                 AML_POS_DECODE, AML_ENTIRE_RANGE,
2219                                 0, entry->base, entry->limit, io_offset,
2220                                 entry->limit - entry->base + 1));
2221         crs_range_insert(range_set->io_ranges, entry->base, entry->limit);
2222     }
2223 
2224     crs_range_merge(temp_range_set.mem_ranges);
2225     for (i = 0; i < temp_range_set.mem_ranges->len; i++) {
2226         entry = g_ptr_array_index(temp_range_set.mem_ranges, i);
2227         assert(entry->limit <= UINT32_MAX &&
2228                (entry->limit - entry->base + 1) <= UINT32_MAX);
2229         aml_append(crs,
2230                    aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED,
2231                                     AML_MAX_FIXED, AML_NON_CACHEABLE,
2232                                     AML_READ_WRITE,
2233                                     0, entry->base, entry->limit, mmio32_offset,
2234                                     entry->limit - entry->base + 1));
2235         crs_range_insert(range_set->mem_ranges, entry->base, entry->limit);
2236     }
2237 
2238     crs_range_merge(temp_range_set.mem_64bit_ranges);
2239     for (i = 0; i < temp_range_set.mem_64bit_ranges->len; i++) {
2240         entry = g_ptr_array_index(temp_range_set.mem_64bit_ranges, i);
2241         aml_append(crs,
2242                    aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
2243                                     AML_MAX_FIXED, AML_NON_CACHEABLE,
2244                                     AML_READ_WRITE,
2245                                     0, entry->base, entry->limit, mmio64_offset,
2246                                     entry->limit - entry->base + 1));
2247         crs_range_insert(range_set->mem_64bit_ranges,
2248                          entry->base, entry->limit);
2249     }
2250 
2251     crs_range_set_free(&temp_range_set);
2252 
2253     aml_append(crs,
2254         aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
2255                             0,
2256                             pci_bus_num(host->bus),
2257                             max_bus,
2258                             bus_nr_offset,
2259                             max_bus - pci_bus_num(host->bus) + 1));
2260 
2261     return crs;
2262 }
2263 
2264 /* ACPI 5.0: 6.4.3.8.2 Serial Bus Connection Descriptors */
2265 static Aml *aml_serial_bus_device(uint8_t serial_bus_type, uint8_t flags,
2266                                   uint16_t type_flags,
2267                                   uint8_t revid, uint16_t data_length,
2268                                   uint16_t resource_source_len)
2269 {
2270     Aml *var = aml_alloc();
2271     uint16_t length = data_length + resource_source_len + 9;
2272 
2273     build_append_byte(var->buf, 0x8e); /* Serial Bus Connection Descriptor */
2274     build_append_int_noprefix(var->buf, length, sizeof(length));
2275     build_append_byte(var->buf, 1);    /* Revision ID */
2276     build_append_byte(var->buf, 0);    /* Resource Source Index */
2277     build_append_byte(var->buf, serial_bus_type); /* Serial Bus Type */
2278     build_append_byte(var->buf, flags); /* General Flags */
2279     build_append_int_noprefix(var->buf, type_flags, /* Type Specific Flags */
2280                               sizeof(type_flags));
2281     build_append_byte(var->buf, revid); /* Type Specification Revision ID */
2282     build_append_int_noprefix(var->buf, data_length, sizeof(data_length));
2283 
2284     return var;
2285 }
2286 
2287 /* ACPI 5.0: 6.4.3.8.2.1 I2C Serial Bus Connection Resource Descriptor */
2288 Aml *aml_i2c_serial_bus_device(uint16_t address, const char *resource_source)
2289 {
2290     uint16_t resource_source_len = strlen(resource_source) + 1;
2291     Aml *var = aml_serial_bus_device(AML_SERIAL_BUS_TYPE_I2C, 0, 0, 1,
2292                                      6, resource_source_len);
2293 
2294     /* Connection Speed.  Just set to 100K for now, it doesn't really matter. */
2295     build_append_int_noprefix(var->buf, 100000, 4);
2296     build_append_int_noprefix(var->buf, address, sizeof(address));
2297 
2298     /* This is a string, not a name, so just copy it directly in. */
2299     g_array_append_vals(var->buf, resource_source, resource_source_len);
2300 
2301     return var;
2302 }
2303