doc: replace x-root with rootdir for usb-mtp
[qemu/ericb.git] / hw / acpi / aml-build.c
blob1e43cd736de914d9d77e546d10d9f5d83f931fe6
1 /* Support for generating ACPI tables and passing them to Guests
3 * Copyright (C) 2015 Red Hat Inc
5 * Author: Michael S. Tsirkin <mst@redhat.com>
6 * Author: Igor Mammedov <imammedo@redhat.com>
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.
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.
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/>.
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"
29 static GArray *build_alloc_array(void)
31 return g_array_new(false, true /* clear */, 1);
34 static void build_free_array(GArray *array)
36 g_array_free(array, true);
39 static void build_prepend_byte(GArray *array, uint8_t val)
41 g_array_prepend_val(array, val);
44 static void build_append_byte(GArray *array, uint8_t val)
46 g_array_append_val(array, val);
49 static void build_append_array(GArray *array, GArray *val)
51 g_array_append_vals(array, val->data, val->len);
54 #define ACPI_NAMESEG_LEN 4
56 static void
57 build_append_nameseg(GArray *array, const char *seg)
59 int len;
61 len = strlen(seg);
62 assert(len <= ACPI_NAMESEG_LEN);
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);
69 static void GCC_FMT_ATTR(2, 0)
70 build_append_namestringv(GArray *array, const char *format, va_list ap)
72 char *s;
73 char **segs;
74 char **segs_iter;
75 int seg_count = 0;
77 s = g_strdup_vprintf(format, ap);
78 segs = g_strsplit(s, ".", 0);
79 g_free(s);
81 /* count segments */
82 segs_iter = segs;
83 while (*segs_iter) {
84 ++segs_iter;
85 ++seg_count;
88 * ACPI 5.0 spec: 20.2.2 Name Objects Encoding:
89 * "SegCount can be from 1 to 255"
91 assert(seg_count > 0 && seg_count <= 255);
93 /* handle RootPath || PrefixPath */
94 s = *segs;
95 while (*s == '\\' || *s == '^') {
96 build_append_byte(array, *s);
97 ++s;
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);
107 break;
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);
118 /* handle the 1st segment manually due to prefix/root path */
119 build_append_nameseg(array, s);
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;
127 break;
129 g_strfreev(segs);
132 GCC_FMT_ATTR(2, 3)
133 static void build_append_namestring(GArray *array, const char *format, ...)
135 va_list ap;
137 va_start(ap, format);
138 build_append_namestringv(array, format, ap);
139 va_end(ap);
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,
150 static void
151 build_prepend_package_length(GArray *package, unsigned length, bool incl_self)
153 uint8_t byte;
154 unsigned length_bytes;
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;
167 * NamedField uses PkgLength encoding but it doesn't include length
168 * of PkgLength itself.
170 if (incl_self) {
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.
176 length += length_bytes;
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 */
201 * Most significant two bits of byte zero indicate how many following bytes
202 * are in PkgLength encoding.
204 byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length;
205 build_prepend_byte(package, byte);
208 static void
209 build_append_pkg_length(GArray *array, unsigned length, bool incl_self)
211 GArray *tmp = build_alloc_array();
213 build_prepend_package_length(tmp, length, incl_self);
214 build_append_array(array, tmp);
215 build_free_array(tmp);
218 static void build_package(GArray *package, uint8_t op)
220 build_prepend_package_length(package, package->len, true);
221 build_prepend_byte(package, op);
224 static void build_extop_package(GArray *package, uint8_t op)
226 build_package(package, op);
227 build_prepend_byte(package, 0x5B); /* ExtOpPrefix */
230 void build_append_int_noprefix(GArray *table, uint64_t value, int size)
232 int i;
234 for (i = 0; i < size; ++i) {
235 build_append_byte(table, value & 0xFF);
236 value = value >> 8;
240 static void build_append_int(GArray *table, uint64_t value)
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);
261 /* Generic Address Structure (GAS)
262 * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure
263 * 2.0 compat note:
264 * @access_width must be 0, see ACPI 2.0:Table 5-1
266 void build_append_gas(GArray *table, AmlAddressSpace as,
267 uint8_t bit_width, uint8_t bit_offset,
268 uint8_t access_width, uint64_t address)
270 build_append_int_noprefix(table, as, 1);
271 build_append_int_noprefix(table, bit_width, 1);
272 build_append_int_noprefix(table, bit_offset, 1);
273 build_append_int_noprefix(table, access_width, 1);
274 build_append_int_noprefix(table, address, 8);
278 * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
279 * and return the offset to 0x00000000 for runtime patching.
281 * Warning: runtime patching is best avoided. Only use this as
282 * a replacement for DataTableRegion (for guests that don't
283 * support it).
286 build_append_named_dword(GArray *array, const char *name_format, ...)
288 int offset;
289 va_list ap;
291 build_append_byte(array, 0x08); /* NameOp */
292 va_start(ap, name_format);
293 build_append_namestringv(array, name_format, ap);
294 va_end(ap);
296 build_append_byte(array, 0x0C); /* DWordPrefix */
298 offset = array->len;
299 build_append_int_noprefix(array, 0x00000000, 4);
300 assert(array->len == offset + 4);
302 return offset;
305 static GPtrArray *alloc_list;
307 static Aml *aml_alloc(void)
309 Aml *var = g_new0(typeof(*var), 1);
311 g_ptr_array_add(alloc_list, var);
312 var->block_flags = AML_NO_OPCODE;
313 var->buf = build_alloc_array();
314 return var;
317 static Aml *aml_opcode(uint8_t op)
319 Aml *var = aml_alloc();
321 var->op = op;
322 var->block_flags = AML_OPCODE;
323 return var;
326 static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags)
328 Aml *var = aml_alloc();
330 var->op = op;
331 var->block_flags = flags;
332 return var;
335 static void aml_free(gpointer data, gpointer user_data)
337 Aml *var = data;
338 build_free_array(var->buf);
339 g_free(var);
342 Aml *init_aml_allocator(void)
344 assert(!alloc_list);
345 alloc_list = g_ptr_array_new();
346 return aml_alloc();
349 void free_aml_allocator(void)
351 g_ptr_array_foreach(alloc_list, aml_free, NULL);
352 g_ptr_array_free(alloc_list, true);
353 alloc_list = 0;
356 /* pack data with DefBuffer encoding */
357 static void build_buffer(GArray *array, uint8_t op)
359 GArray *data = build_alloc_array();
361 build_append_int(data, array->len);
362 g_array_prepend_vals(array, data->data, data->len);
363 build_free_array(data);
364 build_package(array, op);
367 void aml_append(Aml *parent_ctx, Aml *child)
369 GArray *buf = build_alloc_array();
370 build_append_array(buf, child->buf);
372 switch (child->block_flags) {
373 case AML_OPCODE:
374 build_append_byte(parent_ctx->buf, child->op);
375 break;
376 case AML_EXT_PACKAGE:
377 build_extop_package(buf, child->op);
378 break;
379 case AML_PACKAGE:
380 build_package(buf, child->op);
381 break;
382 case AML_RES_TEMPLATE:
383 build_append_byte(buf, 0x79); /* EndTag */
385 * checksum operations are treated as succeeded if checksum
386 * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
388 build_append_byte(buf, 0);
389 /* fall through, to pack resources in buffer */
390 case AML_BUFFER:
391 build_buffer(buf, child->op);
392 break;
393 case AML_NO_OPCODE:
394 break;
395 default:
396 assert(0);
397 break;
399 build_append_array(parent_ctx->buf, buf);
400 build_free_array(buf);
403 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
404 Aml *aml_scope(const char *name_format, ...)
406 va_list ap;
407 Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE);
408 va_start(ap, name_format);
409 build_append_namestringv(var->buf, name_format, ap);
410 va_end(ap);
411 return var;
414 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
415 Aml *aml_return(Aml *val)
417 Aml *var = aml_opcode(0xA4 /* ReturnOp */);
418 aml_append(var, val);
419 return var;
422 /* ACPI 1.0b: 16.2.6.3 Debug Objects Encoding: DebugObj */
423 Aml *aml_debug(void)
425 Aml *var = aml_alloc();
426 build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
427 build_append_byte(var->buf, 0x31); /* DebugOp */
428 return var;
432 * ACPI 1.0b: 16.2.3 Data Objects Encoding:
433 * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
435 Aml *aml_int(const uint64_t val)
437 Aml *var = aml_alloc();
438 build_append_int(var->buf, val);
439 return var;
443 * helper to construct NameString, which returns Aml object
444 * for using with aml_append or other aml_* terms
446 Aml *aml_name(const char *name_format, ...)
448 va_list ap;
449 Aml *var = aml_alloc();
450 va_start(ap, name_format);
451 build_append_namestringv(var->buf, name_format, ap);
452 va_end(ap);
453 return var;
456 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
457 Aml *aml_name_decl(const char *name, Aml *val)
459 Aml *var = aml_opcode(0x08 /* NameOp */);
460 build_append_namestring(var->buf, "%s", name);
461 aml_append(var, val);
462 return var;
465 /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
466 Aml *aml_arg(int pos)
468 uint8_t op = 0x68 /* ARG0 op */ + pos;
470 assert(pos <= 6);
471 return aml_opcode(op);
474 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToInteger */
475 Aml *aml_to_integer(Aml *arg)
477 Aml *var = aml_opcode(0x99 /* ToIntegerOp */);
478 aml_append(var, arg);
479 build_append_byte(var->buf, 0x00 /* NullNameOp */);
480 return var;
483 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToHexString */
484 Aml *aml_to_hexstring(Aml *src, Aml *dst)
486 Aml *var = aml_opcode(0x98 /* ToHexStringOp */);
487 aml_append(var, src);
488 if (dst) {
489 aml_append(var, dst);
490 } else {
491 build_append_byte(var->buf, 0x00 /* NullNameOp */);
493 return var;
496 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToBuffer */
497 Aml *aml_to_buffer(Aml *src, Aml *dst)
499 Aml *var = aml_opcode(0x96 /* ToBufferOp */);
500 aml_append(var, src);
501 if (dst) {
502 aml_append(var, dst);
503 } else {
504 build_append_byte(var->buf, 0x00 /* NullNameOp */);
506 return var;
509 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
510 Aml *aml_store(Aml *val, Aml *target)
512 Aml *var = aml_opcode(0x70 /* StoreOp */);
513 aml_append(var, val);
514 aml_append(var, target);
515 return var;
519 * build_opcode_2arg_dst:
520 * @op: 1-byte opcode
521 * @arg1: 1st operand
522 * @arg2: 2nd operand
523 * @dst: optional target to store to, set to NULL if it's not required
525 * An internal helper to compose AML terms that have
526 * "Op Operand Operand Target"
527 * pattern.
529 * Returns: The newly allocated and composed according to patter Aml object.
531 static Aml *
532 build_opcode_2arg_dst(uint8_t op, Aml *arg1, Aml *arg2, Aml *dst)
534 Aml *var = aml_opcode(op);
535 aml_append(var, arg1);
536 aml_append(var, arg2);
537 if (dst) {
538 aml_append(var, dst);
539 } else {
540 build_append_byte(var->buf, 0x00 /* NullNameOp */);
542 return var;
545 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
546 Aml *aml_and(Aml *arg1, Aml *arg2, Aml *dst)
548 return build_opcode_2arg_dst(0x7B /* AndOp */, arg1, arg2, dst);
551 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */
552 Aml *aml_or(Aml *arg1, Aml *arg2, Aml *dst)
554 return build_opcode_2arg_dst(0x7D /* OrOp */, arg1, arg2, dst);
557 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLOr */
558 Aml *aml_lor(Aml *arg1, Aml *arg2)
560 Aml *var = aml_opcode(0x91 /* LOrOp */);
561 aml_append(var, arg1);
562 aml_append(var, arg2);
563 return var;
566 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftLeft */
567 Aml *aml_shiftleft(Aml *arg1, Aml *count)
569 return build_opcode_2arg_dst(0x79 /* ShiftLeftOp */, arg1, count, NULL);
572 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftRight */
573 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst)
575 return build_opcode_2arg_dst(0x7A /* ShiftRightOp */, arg1, count, dst);
578 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLess */
579 Aml *aml_lless(Aml *arg1, Aml *arg2)
581 Aml *var = aml_opcode(0x95 /* LLessOp */);
582 aml_append(var, arg1);
583 aml_append(var, arg2);
584 return var;
587 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
588 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst)
590 return build_opcode_2arg_dst(0x72 /* AddOp */, arg1, arg2, dst);
593 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSubtract */
594 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst)
596 return build_opcode_2arg_dst(0x74 /* SubtractOp */, arg1, arg2, dst);
599 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIncrement */
600 Aml *aml_increment(Aml *arg)
602 Aml *var = aml_opcode(0x75 /* IncrementOp */);
603 aml_append(var, arg);
604 return var;
607 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDecrement */
608 Aml *aml_decrement(Aml *arg)
610 Aml *var = aml_opcode(0x76 /* DecrementOp */);
611 aml_append(var, arg);
612 return var;
615 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIndex */
616 Aml *aml_index(Aml *arg1, Aml *idx)
618 return build_opcode_2arg_dst(0x88 /* IndexOp */, arg1, idx, NULL);
621 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
622 Aml *aml_notify(Aml *arg1, Aml *arg2)
624 Aml *var = aml_opcode(0x86 /* NotifyOp */);
625 aml_append(var, arg1);
626 aml_append(var, arg2);
627 return var;
630 /* helper to call method without argument */
631 Aml *aml_call0(const char *method)
633 Aml *var = aml_alloc();
634 build_append_namestring(var->buf, "%s", method);
635 return var;
638 /* helper to call method with 1 argument */
639 Aml *aml_call1(const char *method, Aml *arg1)
641 Aml *var = aml_alloc();
642 build_append_namestring(var->buf, "%s", method);
643 aml_append(var, arg1);
644 return var;
647 /* helper to call method with 2 arguments */
648 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2)
650 Aml *var = aml_alloc();
651 build_append_namestring(var->buf, "%s", method);
652 aml_append(var, arg1);
653 aml_append(var, arg2);
654 return var;
657 /* helper to call method with 3 arguments */
658 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3)
660 Aml *var = aml_alloc();
661 build_append_namestring(var->buf, "%s", method);
662 aml_append(var, arg1);
663 aml_append(var, arg2);
664 aml_append(var, arg3);
665 return var;
668 /* helper to call method with 4 arguments */
669 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4)
671 Aml *var = aml_alloc();
672 build_append_namestring(var->buf, "%s", method);
673 aml_append(var, arg1);
674 aml_append(var, arg2);
675 aml_append(var, arg3);
676 aml_append(var, arg4);
677 return var;
680 /* helper to call method with 5 arguments */
681 Aml *aml_call5(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4,
682 Aml *arg5)
684 Aml *var = aml_alloc();
685 build_append_namestring(var->buf, "%s", method);
686 aml_append(var, arg1);
687 aml_append(var, arg2);
688 aml_append(var, arg3);
689 aml_append(var, arg4);
690 aml_append(var, arg5);
691 return var;
695 * ACPI 5.0: 6.4.3.8.1 GPIO Connection Descriptor
696 * Type 1, Large Item Name 0xC
699 static Aml *aml_gpio_connection(AmlGpioConnectionType type,
700 AmlConsumerAndProducer con_and_pro,
701 uint8_t flags, AmlPinConfig pin_config,
702 uint16_t output_drive,
703 uint16_t debounce_timeout,
704 const uint32_t pin_list[], uint32_t pin_count,
705 const char *resource_source_name,
706 const uint8_t *vendor_data,
707 uint16_t vendor_data_len)
709 Aml *var = aml_alloc();
710 const uint16_t min_desc_len = 0x16;
711 uint16_t resource_source_name_len, length;
712 uint16_t pin_table_offset, resource_source_name_offset, vendor_data_offset;
713 uint32_t i;
715 assert(resource_source_name);
716 resource_source_name_len = strlen(resource_source_name) + 1;
717 length = min_desc_len + resource_source_name_len + vendor_data_len;
718 pin_table_offset = min_desc_len + 1;
719 resource_source_name_offset = pin_table_offset + pin_count * 2;
720 vendor_data_offset = resource_source_name_offset + resource_source_name_len;
722 build_append_byte(var->buf, 0x8C); /* GPIO Connection Descriptor */
723 build_append_int_noprefix(var->buf, length, 2); /* Length */
724 build_append_byte(var->buf, 1); /* Revision ID */
725 build_append_byte(var->buf, type); /* GPIO Connection Type */
726 /* General Flags (2 bytes) */
727 build_append_int_noprefix(var->buf, con_and_pro, 2);
728 /* Interrupt and IO Flags (2 bytes) */
729 build_append_int_noprefix(var->buf, flags, 2);
730 /* Pin Configuration 0 = Default 1 = Pull-up 2 = Pull-down 3 = No Pull */
731 build_append_byte(var->buf, pin_config);
732 /* Output Drive Strength (2 bytes) */
733 build_append_int_noprefix(var->buf, output_drive, 2);
734 /* Debounce Timeout (2 bytes) */
735 build_append_int_noprefix(var->buf, debounce_timeout, 2);
736 /* Pin Table Offset (2 bytes) */
737 build_append_int_noprefix(var->buf, pin_table_offset, 2);
738 build_append_byte(var->buf, 0); /* Resource Source Index */
739 /* Resource Source Name Offset (2 bytes) */
740 build_append_int_noprefix(var->buf, resource_source_name_offset, 2);
741 /* Vendor Data Offset (2 bytes) */
742 build_append_int_noprefix(var->buf, vendor_data_offset, 2);
743 /* Vendor Data Length (2 bytes) */
744 build_append_int_noprefix(var->buf, vendor_data_len, 2);
745 /* Pin Number (2n bytes)*/
746 for (i = 0; i < pin_count; i++) {
747 build_append_int_noprefix(var->buf, pin_list[i], 2);
750 /* Resource Source Name */
751 build_append_namestring(var->buf, "%s", resource_source_name);
752 build_append_byte(var->buf, '\0');
754 /* Vendor-defined Data */
755 if (vendor_data != NULL) {
756 g_array_append_vals(var->buf, vendor_data, vendor_data_len);
759 return var;
763 * ACPI 5.0: 19.5.53
764 * GpioInt(GPIO Interrupt Connection Resource Descriptor Macro)
766 Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro,
767 AmlLevelAndEdge edge_level,
768 AmlActiveHighAndLow active_level, AmlShared shared,
769 AmlPinConfig pin_config, uint16_t debounce_timeout,
770 const uint32_t pin_list[], uint32_t pin_count,
771 const char *resource_source_name,
772 const uint8_t *vendor_data, uint16_t vendor_data_len)
774 uint8_t flags = edge_level | (active_level << 1) | (shared << 3);
776 return aml_gpio_connection(AML_INTERRUPT_CONNECTION, con_and_pro, flags,
777 pin_config, 0, debounce_timeout, pin_list,
778 pin_count, resource_source_name, vendor_data,
779 vendor_data_len);
783 * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor
784 * (Type 1, Large Item Name 0x6)
786 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size,
787 AmlReadAndWrite read_and_write)
789 Aml *var = aml_alloc();
790 build_append_byte(var->buf, 0x86); /* Memory32Fixed Resource Descriptor */
791 build_append_byte(var->buf, 9); /* Length, bits[7:0] value = 9 */
792 build_append_byte(var->buf, 0); /* Length, bits[15:8] value = 0 */
793 build_append_byte(var->buf, read_and_write); /* Write status, 1 rw 0 ro */
795 /* Range base address */
796 build_append_byte(var->buf, extract32(addr, 0, 8)); /* bits[7:0] */
797 build_append_byte(var->buf, extract32(addr, 8, 8)); /* bits[15:8] */
798 build_append_byte(var->buf, extract32(addr, 16, 8)); /* bits[23:16] */
799 build_append_byte(var->buf, extract32(addr, 24, 8)); /* bits[31:24] */
801 /* Range length */
802 build_append_byte(var->buf, extract32(size, 0, 8)); /* bits[7:0] */
803 build_append_byte(var->buf, extract32(size, 8, 8)); /* bits[15:8] */
804 build_append_byte(var->buf, extract32(size, 16, 8)); /* bits[23:16] */
805 build_append_byte(var->buf, extract32(size, 24, 8)); /* bits[31:24] */
806 return var;
810 * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor
811 * Type 1, Large Item Name 0x9
813 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro,
814 AmlLevelAndEdge level_and_edge,
815 AmlActiveHighAndLow high_and_low, AmlShared shared,
816 uint32_t *irq_list, uint8_t irq_count)
818 int i;
819 Aml *var = aml_alloc();
820 uint8_t irq_flags = con_and_pro | (level_and_edge << 1)
821 | (high_and_low << 2) | (shared << 3);
822 const int header_bytes_in_len = 2;
823 uint16_t len = header_bytes_in_len + irq_count * sizeof(uint32_t);
825 assert(irq_count > 0);
827 build_append_byte(var->buf, 0x89); /* Extended irq descriptor */
828 build_append_byte(var->buf, len & 0xFF); /* Length, bits[7:0] */
829 build_append_byte(var->buf, len >> 8); /* Length, bits[15:8] */
830 build_append_byte(var->buf, irq_flags); /* Interrupt Vector Information. */
831 build_append_byte(var->buf, irq_count); /* Interrupt table length */
833 /* Interrupt Number List */
834 for (i = 0; i < irq_count; i++) {
835 build_append_int_noprefix(var->buf, irq_list[i], 4);
837 return var;
840 /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */
841 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
842 uint8_t aln, uint8_t len)
844 Aml *var = aml_alloc();
845 build_append_byte(var->buf, 0x47); /* IO port descriptor */
846 build_append_byte(var->buf, dec);
847 build_append_byte(var->buf, min_base & 0xff);
848 build_append_byte(var->buf, (min_base >> 8) & 0xff);
849 build_append_byte(var->buf, max_base & 0xff);
850 build_append_byte(var->buf, (max_base >> 8) & 0xff);
851 build_append_byte(var->buf, aln);
852 build_append_byte(var->buf, len);
853 return var;
857 * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor
859 * More verbose description at:
860 * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro)
861 * 6.4.2.1 IRQ Descriptor
863 Aml *aml_irq_no_flags(uint8_t irq)
865 uint16_t irq_mask;
866 Aml *var = aml_alloc();
868 assert(irq < 16);
869 build_append_byte(var->buf, 0x22); /* IRQ descriptor 2 byte form */
871 irq_mask = 1U << irq;
872 build_append_byte(var->buf, irq_mask & 0xFF); /* IRQ mask bits[7:0] */
873 build_append_byte(var->buf, irq_mask >> 8); /* IRQ mask bits[15:8] */
874 return var;
877 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */
878 Aml *aml_lnot(Aml *arg)
880 Aml *var = aml_opcode(0x92 /* LNotOp */);
881 aml_append(var, arg);
882 return var;
885 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */
886 Aml *aml_equal(Aml *arg1, Aml *arg2)
888 Aml *var = aml_opcode(0x93 /* LequalOp */);
889 aml_append(var, arg1);
890 aml_append(var, arg2);
891 return var;
894 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreater */
895 Aml *aml_lgreater(Aml *arg1, Aml *arg2)
897 Aml *var = aml_opcode(0x94 /* LGreaterOp */);
898 aml_append(var, arg1);
899 aml_append(var, arg2);
900 return var;
903 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreaterEqual */
904 Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2)
906 /* LGreaterEqualOp := LNotOp LLessOp */
907 Aml *var = aml_opcode(0x92 /* LNotOp */);
908 build_append_byte(var->buf, 0x95 /* LLessOp */);
909 aml_append(var, arg1);
910 aml_append(var, arg2);
911 return var;
914 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
915 Aml *aml_if(Aml *predicate)
917 Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE);
918 aml_append(var, predicate);
919 return var;
922 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */
923 Aml *aml_else(void)
925 Aml *var = aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE);
926 return var;
929 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefWhile */
930 Aml *aml_while(Aml *predicate)
932 Aml *var = aml_bundle(0xA2 /* WhileOp */, AML_PACKAGE);
933 aml_append(var, predicate);
934 return var;
937 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
938 Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag)
940 Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE);
941 int methodflags;
944 * MethodFlags:
945 * bit 0-2: ArgCount (0-7)
946 * bit 3: SerializeFlag
947 * 0: NotSerialized
948 * 1: Serialized
949 * bit 4-7: reserved (must be 0)
951 assert(arg_count < 8);
952 methodflags = arg_count | (sflag << 3);
954 build_append_namestring(var->buf, "%s", name);
955 build_append_byte(var->buf, methodflags); /* MethodFlags: ArgCount */
956 return var;
959 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
960 Aml *aml_device(const char *name_format, ...)
962 va_list ap;
963 Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE);
964 va_start(ap, name_format);
965 build_append_namestringv(var->buf, name_format, ap);
966 va_end(ap);
967 return var;
970 /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */
971 Aml *aml_resource_template(void)
973 /* ResourceTemplate is a buffer of Resources with EndTag at the end */
974 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE);
975 return var;
978 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer
979 * Pass byte_list as NULL to request uninitialized buffer to reserve space.
981 Aml *aml_buffer(int buffer_size, uint8_t *byte_list)
983 int i;
984 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
986 for (i = 0; i < buffer_size; i++) {
987 if (byte_list == NULL) {
988 build_append_byte(var->buf, 0x0);
989 } else {
990 build_append_byte(var->buf, byte_list[i]);
994 return var;
997 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */
998 Aml *aml_package(uint8_t num_elements)
1000 Aml *var = aml_bundle(0x12 /* PackageOp */, AML_PACKAGE);
1001 build_append_byte(var->buf, num_elements);
1002 return var;
1005 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */
1006 Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
1007 Aml *offset, uint32_t len)
1009 Aml *var = aml_alloc();
1010 build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1011 build_append_byte(var->buf, 0x80); /* OpRegionOp */
1012 build_append_namestring(var->buf, "%s", name);
1013 build_append_byte(var->buf, rs);
1014 aml_append(var, offset);
1015 build_append_int(var->buf, len);
1016 return var;
1019 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */
1020 Aml *aml_named_field(const char *name, unsigned length)
1022 Aml *var = aml_alloc();
1023 build_append_nameseg(var->buf, name);
1024 build_append_pkg_length(var->buf, length, false);
1025 return var;
1028 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */
1029 Aml *aml_reserved_field(unsigned length)
1031 Aml *var = aml_alloc();
1032 /* ReservedField := 0x00 PkgLength */
1033 build_append_byte(var->buf, 0x00);
1034 build_append_pkg_length(var->buf, length, false);
1035 return var;
1038 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */
1039 Aml *aml_field(const char *name, AmlAccessType type, AmlLockRule lock,
1040 AmlUpdateRule rule)
1042 Aml *var = aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE);
1043 uint8_t flags = rule << 5 | type;
1045 flags |= lock << 4; /* LockRule at 4 bit offset */
1047 build_append_namestring(var->buf, "%s", name);
1048 build_append_byte(var->buf, flags);
1049 return var;
1052 static
1053 Aml *create_field_common(int opcode, Aml *srcbuf, Aml *index, const char *name)
1055 Aml *var = aml_opcode(opcode);
1056 aml_append(var, srcbuf);
1057 aml_append(var, index);
1058 build_append_namestring(var->buf, "%s", name);
1059 return var;
1062 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateField */
1063 Aml *aml_create_field(Aml *srcbuf, Aml *bit_index, Aml *num_bits,
1064 const char *name)
1066 Aml *var = aml_alloc();
1067 build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1068 build_append_byte(var->buf, 0x13); /* CreateFieldOp */
1069 aml_append(var, srcbuf);
1070 aml_append(var, bit_index);
1071 aml_append(var, num_bits);
1072 build_append_namestring(var->buf, "%s", name);
1073 return var;
1076 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */
1077 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name)
1079 return create_field_common(0x8A /* CreateDWordFieldOp */,
1080 srcbuf, index, name);
1083 /* ACPI 2.0a: 17.2.4.2 Named Objects Encoding: DefCreateQWordField */
1084 Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name)
1086 return create_field_common(0x8F /* CreateQWordFieldOp */,
1087 srcbuf, index, name);
1090 /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */
1091 Aml *aml_string(const char *name_format, ...)
1093 Aml *var = aml_opcode(0x0D /* StringPrefix */);
1094 va_list ap;
1095 char *s;
1096 int len;
1098 va_start(ap, name_format);
1099 len = g_vasprintf(&s, name_format, ap);
1100 va_end(ap);
1102 g_array_append_vals(var->buf, s, len + 1);
1103 g_free(s);
1105 return var;
1108 /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */
1109 Aml *aml_local(int num)
1111 uint8_t op = 0x60 /* Local0Op */ + num;
1113 assert(num <= 7);
1114 return aml_opcode(op);
1117 /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */
1118 Aml *aml_varpackage(uint32_t num_elements)
1120 Aml *var = aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE);
1121 build_append_int(var->buf, num_elements);
1122 return var;
1125 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */
1126 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
1127 const char *name_format, ...)
1129 va_list ap;
1130 Aml *var = aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE);
1131 va_start(ap, name_format);
1132 build_append_namestringv(var->buf, name_format, ap);
1133 va_end(ap);
1134 build_append_byte(var->buf, proc_id); /* ProcID */
1135 build_append_int_noprefix(var->buf, pblk_addr, sizeof(pblk_addr));
1136 build_append_byte(var->buf, pblk_len); /* PblkLen */
1137 return var;
1140 static uint8_t Hex2Digit(char c)
1142 if (c >= 'A') {
1143 return c - 'A' + 10;
1146 return c - '0';
1149 /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */
1150 Aml *aml_eisaid(const char *str)
1152 Aml *var = aml_alloc();
1153 uint32_t id;
1155 g_assert(strlen(str) == 7);
1156 id = (str[0] - 0x40) << 26 |
1157 (str[1] - 0x40) << 21 |
1158 (str[2] - 0x40) << 16 |
1159 Hex2Digit(str[3]) << 12 |
1160 Hex2Digit(str[4]) << 8 |
1161 Hex2Digit(str[5]) << 4 |
1162 Hex2Digit(str[6]);
1164 build_append_byte(var->buf, 0x0C); /* DWordPrefix */
1165 build_append_int_noprefix(var->buf, bswap32(id), sizeof(id));
1166 return var;
1169 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */
1170 static Aml *aml_as_desc_header(AmlResourceType type, AmlMinFixed min_fixed,
1171 AmlMaxFixed max_fixed, AmlDecode dec,
1172 uint8_t type_flags)
1174 uint8_t flags = max_fixed | min_fixed | dec;
1175 Aml *var = aml_alloc();
1177 build_append_byte(var->buf, type);
1178 build_append_byte(var->buf, flags);
1179 build_append_byte(var->buf, type_flags); /* Type Specific Flags */
1180 return var;
1183 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */
1184 static Aml *aml_word_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1185 AmlMaxFixed max_fixed, AmlDecode dec,
1186 uint16_t addr_gran, uint16_t addr_min,
1187 uint16_t addr_max, uint16_t addr_trans,
1188 uint16_t len, uint8_t type_flags)
1190 Aml *var = aml_alloc();
1192 build_append_byte(var->buf, 0x88); /* Word Address Space Descriptor */
1193 /* minimum length since we do not encode optional fields */
1194 build_append_byte(var->buf, 0x0D);
1195 build_append_byte(var->buf, 0x0);
1197 aml_append(var,
1198 aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1199 build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1200 build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1201 build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1202 build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1203 build_append_int_noprefix(var->buf, len, sizeof(len));
1204 return var;
1207 /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */
1208 static Aml *aml_dword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1209 AmlMaxFixed max_fixed, AmlDecode dec,
1210 uint32_t addr_gran, uint32_t addr_min,
1211 uint32_t addr_max, uint32_t addr_trans,
1212 uint32_t len, uint8_t type_flags)
1214 Aml *var = aml_alloc();
1216 build_append_byte(var->buf, 0x87); /* DWord Address Space Descriptor */
1217 /* minimum length since we do not encode optional fields */
1218 build_append_byte(var->buf, 23);
1219 build_append_byte(var->buf, 0x0);
1222 aml_append(var,
1223 aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1224 build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1225 build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1226 build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1227 build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1228 build_append_int_noprefix(var->buf, len, sizeof(len));
1229 return var;
1232 /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */
1233 static Aml *aml_qword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
1234 AmlMaxFixed max_fixed, AmlDecode dec,
1235 uint64_t addr_gran, uint64_t addr_min,
1236 uint64_t addr_max, uint64_t addr_trans,
1237 uint64_t len, uint8_t type_flags)
1239 Aml *var = aml_alloc();
1241 build_append_byte(var->buf, 0x8A); /* QWord Address Space Descriptor */
1242 /* minimum length since we do not encode optional fields */
1243 build_append_byte(var->buf, 0x2B);
1244 build_append_byte(var->buf, 0x0);
1246 aml_append(var,
1247 aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
1248 build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
1249 build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
1250 build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
1251 build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
1252 build_append_int_noprefix(var->buf, len, sizeof(len));
1253 return var;
1257 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1259 * More verbose description at:
1260 * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro)
1262 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1263 AmlDecode dec, uint16_t addr_gran,
1264 uint16_t addr_min, uint16_t addr_max,
1265 uint16_t addr_trans, uint16_t len)
1268 return aml_word_as_desc(AML_BUS_NUMBER_RANGE, min_fixed, max_fixed, dec,
1269 addr_gran, addr_min, addr_max, addr_trans, len, 0);
1273 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1275 * More verbose description at:
1276 * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro)
1278 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1279 AmlDecode dec, AmlISARanges isa_ranges,
1280 uint16_t addr_gran, uint16_t addr_min,
1281 uint16_t addr_max, uint16_t addr_trans,
1282 uint16_t len)
1285 return aml_word_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1286 addr_gran, addr_min, addr_max, addr_trans, len,
1287 isa_ranges);
1291 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor
1293 * More verbose description at:
1294 * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro)
1296 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1297 AmlDecode dec, AmlISARanges isa_ranges,
1298 uint32_t addr_gran, uint32_t addr_min,
1299 uint32_t addr_max, uint32_t addr_trans,
1300 uint32_t len)
1303 return aml_dword_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1304 addr_gran, addr_min, addr_max, addr_trans, len,
1305 isa_ranges);
1309 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor
1311 * More verbose description at:
1312 * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro)
1314 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1315 AmlMaxFixed max_fixed, AmlCacheable cacheable,
1316 AmlReadAndWrite read_and_write,
1317 uint32_t addr_gran, uint32_t addr_min,
1318 uint32_t addr_max, uint32_t addr_trans,
1319 uint32_t len)
1321 uint8_t flags = read_and_write | (cacheable << 1);
1323 return aml_dword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1324 dec, addr_gran, addr_min, addr_max,
1325 addr_trans, len, flags);
1329 * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor
1331 * More verbose description at:
1332 * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro)
1334 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1335 AmlMaxFixed max_fixed, AmlCacheable cacheable,
1336 AmlReadAndWrite read_and_write,
1337 uint64_t addr_gran, uint64_t addr_min,
1338 uint64_t addr_max, uint64_t addr_trans,
1339 uint64_t len)
1341 uint8_t flags = read_and_write | (cacheable << 1);
1343 return aml_qword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1344 dec, addr_gran, addr_min, addr_max,
1345 addr_trans, len, flags);
1348 /* ACPI 1.0b: 6.4.2.2 DMA Format/6.4.2.2.1 ASL Macro for DMA Descriptor */
1349 Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz,
1350 uint8_t channel)
1352 Aml *var = aml_alloc();
1353 uint8_t flags = sz | bm << 2 | typ << 5;
1355 assert(channel < 8);
1356 build_append_byte(var->buf, 0x2A); /* Byte 0: DMA Descriptor */
1357 build_append_byte(var->buf, 1U << channel); /* Byte 1: _DMA - DmaChannel */
1358 build_append_byte(var->buf, flags); /* Byte 2 */
1359 return var;
1362 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefSleep */
1363 Aml *aml_sleep(uint64_t msec)
1365 Aml *var = aml_alloc();
1366 build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1367 build_append_byte(var->buf, 0x22); /* SleepOp */
1368 aml_append(var, aml_int(msec));
1369 return var;
1372 static uint8_t Hex2Byte(const char *src)
1374 int hi, lo;
1376 hi = Hex2Digit(src[0]);
1377 assert(hi >= 0);
1378 assert(hi <= 15);
1380 lo = Hex2Digit(src[1]);
1381 assert(lo >= 0);
1382 assert(lo <= 15);
1383 return (hi << 4) | lo;
1387 * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro)
1388 * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1389 * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp");
1391 Aml *aml_touuid(const char *uuid)
1393 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1395 assert(strlen(uuid) == 36);
1396 assert(uuid[8] == '-');
1397 assert(uuid[13] == '-');
1398 assert(uuid[18] == '-');
1399 assert(uuid[23] == '-');
1401 build_append_byte(var->buf, Hex2Byte(uuid + 6)); /* dd - at offset 00 */
1402 build_append_byte(var->buf, Hex2Byte(uuid + 4)); /* cc - at offset 01 */
1403 build_append_byte(var->buf, Hex2Byte(uuid + 2)); /* bb - at offset 02 */
1404 build_append_byte(var->buf, Hex2Byte(uuid + 0)); /* aa - at offset 03 */
1406 build_append_byte(var->buf, Hex2Byte(uuid + 11)); /* ff - at offset 04 */
1407 build_append_byte(var->buf, Hex2Byte(uuid + 9)); /* ee - at offset 05 */
1409 build_append_byte(var->buf, Hex2Byte(uuid + 16)); /* hh - at offset 06 */
1410 build_append_byte(var->buf, Hex2Byte(uuid + 14)); /* gg - at offset 07 */
1412 build_append_byte(var->buf, Hex2Byte(uuid + 19)); /* ii - at offset 08 */
1413 build_append_byte(var->buf, Hex2Byte(uuid + 21)); /* jj - at offset 09 */
1415 build_append_byte(var->buf, Hex2Byte(uuid + 24)); /* kk - at offset 10 */
1416 build_append_byte(var->buf, Hex2Byte(uuid + 26)); /* ll - at offset 11 */
1417 build_append_byte(var->buf, Hex2Byte(uuid + 28)); /* mm - at offset 12 */
1418 build_append_byte(var->buf, Hex2Byte(uuid + 30)); /* nn - at offset 13 */
1419 build_append_byte(var->buf, Hex2Byte(uuid + 32)); /* oo - at offset 14 */
1420 build_append_byte(var->buf, Hex2Byte(uuid + 34)); /* pp - at offset 15 */
1422 return var;
1426 * ACPI 2.0b: 16.2.3.6.4.3 Unicode Macro (Convert Ascii String To Unicode)
1428 Aml *aml_unicode(const char *str)
1430 int i = 0;
1431 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1433 do {
1434 build_append_byte(var->buf, str[i]);
1435 build_append_byte(var->buf, 0);
1436 i++;
1437 } while (i <= strlen(str));
1439 return var;
1442 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefRefOf */
1443 Aml *aml_refof(Aml *arg)
1445 Aml *var = aml_opcode(0x71 /* RefOfOp */);
1446 aml_append(var, arg);
1447 return var;
1450 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDerefOf */
1451 Aml *aml_derefof(Aml *arg)
1453 Aml *var = aml_opcode(0x83 /* DerefOfOp */);
1454 aml_append(var, arg);
1455 return var;
1458 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSizeOf */
1459 Aml *aml_sizeof(Aml *arg)
1461 Aml *var = aml_opcode(0x87 /* SizeOfOp */);
1462 aml_append(var, arg);
1463 return var;
1466 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMutex */
1467 Aml *aml_mutex(const char *name, uint8_t sync_level)
1469 Aml *var = aml_alloc();
1470 build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1471 build_append_byte(var->buf, 0x01); /* MutexOp */
1472 build_append_namestring(var->buf, "%s", name);
1473 assert(!(sync_level & 0xF0));
1474 build_append_byte(var->buf, sync_level);
1475 return var;
1478 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAcquire */
1479 Aml *aml_acquire(Aml *mutex, uint16_t timeout)
1481 Aml *var = aml_alloc();
1482 build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1483 build_append_byte(var->buf, 0x23); /* AcquireOp */
1484 aml_append(var, mutex);
1485 build_append_int_noprefix(var->buf, timeout, sizeof(timeout));
1486 return var;
1489 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefRelease */
1490 Aml *aml_release(Aml *mutex)
1492 Aml *var = aml_alloc();
1493 build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
1494 build_append_byte(var->buf, 0x27); /* ReleaseOp */
1495 aml_append(var, mutex);
1496 return var;
1499 /* ACPI 1.0b: 16.2.5.1 Name Space Modifier Objects Encoding: DefAlias */
1500 Aml *aml_alias(const char *source_object, const char *alias_object)
1502 Aml *var = aml_opcode(0x06 /* AliasOp */);
1503 aml_append(var, aml_name("%s", source_object));
1504 aml_append(var, aml_name("%s", alias_object));
1505 return var;
1508 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefConcat */
1509 Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target)
1511 return build_opcode_2arg_dst(0x73 /* ConcatOp */, source1, source2,
1512 target);
1515 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefObjectType */
1516 Aml *aml_object_type(Aml *object)
1518 Aml *var = aml_opcode(0x8E /* ObjectTypeOp */);
1519 aml_append(var, object);
1520 return var;
1523 void
1524 build_header(BIOSLinker *linker, GArray *table_data,
1525 AcpiTableHeader *h, const char *sig, int len, uint8_t rev,
1526 const char *oem_id, const char *oem_table_id)
1528 unsigned tbl_offset = (char *)h - table_data->data;
1529 unsigned checksum_offset = (char *)&h->checksum - table_data->data;
1530 memcpy(&h->signature, sig, 4);
1531 h->length = cpu_to_le32(len);
1532 h->revision = rev;
1534 if (oem_id) {
1535 strncpy((char *)h->oem_id, oem_id, sizeof h->oem_id);
1536 } else {
1537 memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6);
1540 if (oem_table_id) {
1541 strncpy((char *)h->oem_table_id, oem_table_id, sizeof(h->oem_table_id));
1542 } else {
1543 memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4);
1544 memcpy(h->oem_table_id + 4, sig, 4);
1547 h->oem_revision = cpu_to_le32(1);
1548 memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4);
1549 h->asl_compiler_revision = cpu_to_le32(1);
1550 /* Checksum to be filled in by Guest linker */
1551 bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE,
1552 tbl_offset, len, checksum_offset);
1555 void *acpi_data_push(GArray *table_data, unsigned size)
1557 unsigned off = table_data->len;
1558 g_array_set_size(table_data, off + size);
1559 return table_data->data + off;
1562 unsigned acpi_data_len(GArray *table)
1564 assert(g_array_get_element_size(table) == 1);
1565 return table->len;
1568 void acpi_add_table(GArray *table_offsets, GArray *table_data)
1570 uint32_t offset = table_data->len;
1571 g_array_append_val(table_offsets, offset);
1574 void acpi_build_tables_init(AcpiBuildTables *tables)
1576 tables->rsdp = g_array_new(false, true /* clear */, 1);
1577 tables->table_data = g_array_new(false, true /* clear */, 1);
1578 tables->tcpalog = g_array_new(false, true /* clear */, 1);
1579 tables->vmgenid = g_array_new(false, true /* clear */, 1);
1580 tables->linker = bios_linker_loader_init();
1583 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
1585 bios_linker_loader_cleanup(tables->linker);
1586 g_array_free(tables->rsdp, true);
1587 g_array_free(tables->table_data, true);
1588 g_array_free(tables->tcpalog, mfre);
1589 g_array_free(tables->vmgenid, mfre);
1592 /* Build rsdt table */
1593 void
1594 build_rsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets,
1595 const char *oem_id, const char *oem_table_id)
1597 int i;
1598 unsigned rsdt_entries_offset;
1599 AcpiRsdtDescriptorRev1 *rsdt;
1600 const unsigned table_data_len = (sizeof(uint32_t) * table_offsets->len);
1601 const unsigned rsdt_entry_size = sizeof(rsdt->table_offset_entry[0]);
1602 const size_t rsdt_len = sizeof(*rsdt) + table_data_len;
1604 rsdt = acpi_data_push(table_data, rsdt_len);
1605 rsdt_entries_offset = (char *)rsdt->table_offset_entry - table_data->data;
1606 for (i = 0; i < table_offsets->len; ++i) {
1607 uint32_t ref_tbl_offset = g_array_index(table_offsets, uint32_t, i);
1608 uint32_t rsdt_entry_offset = rsdt_entries_offset + rsdt_entry_size * i;
1610 /* rsdt->table_offset_entry to be filled by Guest linker */
1611 bios_linker_loader_add_pointer(linker,
1612 ACPI_BUILD_TABLE_FILE, rsdt_entry_offset, rsdt_entry_size,
1613 ACPI_BUILD_TABLE_FILE, ref_tbl_offset);
1615 build_header(linker, table_data,
1616 (void *)rsdt, "RSDT", rsdt_len, 1, oem_id, oem_table_id);
1619 /* Build xsdt table */
1620 void
1621 build_xsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets,
1622 const char *oem_id, const char *oem_table_id)
1624 int i;
1625 unsigned xsdt_entries_offset;
1626 AcpiXsdtDescriptorRev2 *xsdt;
1627 const unsigned table_data_len = (sizeof(uint64_t) * table_offsets->len);
1628 const unsigned xsdt_entry_size = sizeof(xsdt->table_offset_entry[0]);
1629 const size_t xsdt_len = sizeof(*xsdt) + table_data_len;
1631 xsdt = acpi_data_push(table_data, xsdt_len);
1632 xsdt_entries_offset = (char *)xsdt->table_offset_entry - table_data->data;
1633 for (i = 0; i < table_offsets->len; ++i) {
1634 uint64_t ref_tbl_offset = g_array_index(table_offsets, uint32_t, i);
1635 uint64_t xsdt_entry_offset = xsdt_entries_offset + xsdt_entry_size * i;
1637 /* xsdt->table_offset_entry to be filled by Guest linker */
1638 bios_linker_loader_add_pointer(linker,
1639 ACPI_BUILD_TABLE_FILE, xsdt_entry_offset, xsdt_entry_size,
1640 ACPI_BUILD_TABLE_FILE, ref_tbl_offset);
1642 build_header(linker, table_data,
1643 (void *)xsdt, "XSDT", xsdt_len, 1, oem_id, oem_table_id);
1646 void build_srat_memory(AcpiSratMemoryAffinity *numamem, uint64_t base,
1647 uint64_t len, int node, MemoryAffinityFlags flags)
1649 numamem->type = ACPI_SRAT_MEMORY;
1650 numamem->length = sizeof(*numamem);
1651 numamem->proximity = cpu_to_le32(node);
1652 numamem->flags = cpu_to_le32(flags);
1653 numamem->base_addr = cpu_to_le64(base);
1654 numamem->range_length = cpu_to_le64(len);
1658 * ACPI spec 5.2.17 System Locality Distance Information Table
1659 * (Revision 2.0 or later)
1661 void build_slit(GArray *table_data, BIOSLinker *linker)
1663 int slit_start, i, j;
1664 slit_start = table_data->len;
1666 acpi_data_push(table_data, sizeof(AcpiTableHeader));
1668 build_append_int_noprefix(table_data, nb_numa_nodes, 8);
1669 for (i = 0; i < nb_numa_nodes; i++) {
1670 for (j = 0; j < nb_numa_nodes; j++) {
1671 assert(numa_info[i].distance[j]);
1672 build_append_int_noprefix(table_data, numa_info[i].distance[j], 1);
1676 build_header(linker, table_data,
1677 (void *)(table_data->data + slit_start),
1678 "SLIT",
1679 table_data->len - slit_start, 1, NULL, NULL);
1682 /* build rev1/rev3/rev5.1 FADT */
1683 void build_fadt(GArray *tbl, BIOSLinker *linker, const AcpiFadtData *f,
1684 const char *oem_id, const char *oem_table_id)
1686 int off;
1687 int fadt_start = tbl->len;
1689 acpi_data_push(tbl, sizeof(AcpiTableHeader));
1691 /* FACS address to be filled by Guest linker at runtime */
1692 off = tbl->len;
1693 build_append_int_noprefix(tbl, 0, 4); /* FIRMWARE_CTRL */
1694 if (f->facs_tbl_offset) { /* don't patch if not supported by platform */
1695 bios_linker_loader_add_pointer(linker,
1696 ACPI_BUILD_TABLE_FILE, off, 4,
1697 ACPI_BUILD_TABLE_FILE, *f->facs_tbl_offset);
1700 /* DSDT address to be filled by Guest linker at runtime */
1701 off = tbl->len;
1702 build_append_int_noprefix(tbl, 0, 4); /* DSDT */
1703 if (f->dsdt_tbl_offset) { /* don't patch if not supported by platform */
1704 bios_linker_loader_add_pointer(linker,
1705 ACPI_BUILD_TABLE_FILE, off, 4,
1706 ACPI_BUILD_TABLE_FILE, *f->dsdt_tbl_offset);
1709 /* ACPI1.0: INT_MODEL, ACPI2.0+: Reserved */
1710 build_append_int_noprefix(tbl, f->int_model /* Multiple APIC */, 1);
1711 /* Preferred_PM_Profile */
1712 build_append_int_noprefix(tbl, 0 /* Unspecified */, 1);
1713 build_append_int_noprefix(tbl, f->sci_int, 2); /* SCI_INT */
1714 build_append_int_noprefix(tbl, f->smi_cmd, 4); /* SMI_CMD */
1715 build_append_int_noprefix(tbl, f->acpi_enable_cmd, 1); /* ACPI_ENABLE */
1716 build_append_int_noprefix(tbl, f->acpi_disable_cmd, 1); /* ACPI_DISABLE */
1717 build_append_int_noprefix(tbl, 0 /* not supported */, 1); /* S4BIOS_REQ */
1718 /* ACPI1.0: Reserved, ACPI2.0+: PSTATE_CNT */
1719 build_append_int_noprefix(tbl, 0, 1);
1720 build_append_int_noprefix(tbl, f->pm1a_evt.address, 4); /* PM1a_EVT_BLK */
1721 build_append_int_noprefix(tbl, 0, 4); /* PM1b_EVT_BLK */
1722 build_append_int_noprefix(tbl, f->pm1a_cnt.address, 4); /* PM1a_CNT_BLK */
1723 build_append_int_noprefix(tbl, 0, 4); /* PM1b_CNT_BLK */
1724 build_append_int_noprefix(tbl, 0, 4); /* PM2_CNT_BLK */
1725 build_append_int_noprefix(tbl, f->pm_tmr.address, 4); /* PM_TMR_BLK */
1726 build_append_int_noprefix(tbl, f->gpe0_blk.address, 4); /* GPE0_BLK */
1727 build_append_int_noprefix(tbl, 0, 4); /* GPE1_BLK */
1728 /* PM1_EVT_LEN */
1729 build_append_int_noprefix(tbl, f->pm1a_evt.bit_width / 8, 1);
1730 /* PM1_CNT_LEN */
1731 build_append_int_noprefix(tbl, f->pm1a_cnt.bit_width / 8, 1);
1732 build_append_int_noprefix(tbl, 0, 1); /* PM2_CNT_LEN */
1733 build_append_int_noprefix(tbl, f->pm_tmr.bit_width / 8, 1); /* PM_TMR_LEN */
1734 /* GPE0_BLK_LEN */
1735 build_append_int_noprefix(tbl, f->gpe0_blk.bit_width / 8, 1);
1736 build_append_int_noprefix(tbl, 0, 1); /* GPE1_BLK_LEN */
1737 build_append_int_noprefix(tbl, 0, 1); /* GPE1_BASE */
1738 build_append_int_noprefix(tbl, 0, 1); /* CST_CNT */
1739 build_append_int_noprefix(tbl, f->plvl2_lat, 2); /* P_LVL2_LAT */
1740 build_append_int_noprefix(tbl, f->plvl3_lat, 2); /* P_LVL3_LAT */
1741 build_append_int_noprefix(tbl, 0, 2); /* FLUSH_SIZE */
1742 build_append_int_noprefix(tbl, 0, 2); /* FLUSH_STRIDE */
1743 build_append_int_noprefix(tbl, 0, 1); /* DUTY_OFFSET */
1744 build_append_int_noprefix(tbl, 0, 1); /* DUTY_WIDTH */
1745 build_append_int_noprefix(tbl, 0, 1); /* DAY_ALRM */
1746 build_append_int_noprefix(tbl, 0, 1); /* MON_ALRM */
1747 build_append_int_noprefix(tbl, f->rtc_century, 1); /* CENTURY */
1748 build_append_int_noprefix(tbl, 0, 2); /* IAPC_BOOT_ARCH */
1749 build_append_int_noprefix(tbl, 0, 1); /* Reserved */
1750 build_append_int_noprefix(tbl, f->flags, 4); /* Flags */
1752 if (f->rev == 1) {
1753 goto build_hdr;
1756 build_append_gas_from_struct(tbl, &f->reset_reg); /* RESET_REG */
1757 build_append_int_noprefix(tbl, f->reset_val, 1); /* RESET_VALUE */
1758 /* Since ACPI 5.1 */
1759 if ((f->rev >= 6) || ((f->rev == 5) && f->minor_ver > 0)) {
1760 build_append_int_noprefix(tbl, f->arm_boot_arch, 2); /* ARM_BOOT_ARCH */
1761 /* FADT Minor Version */
1762 build_append_int_noprefix(tbl, f->minor_ver, 1);
1763 } else {
1764 build_append_int_noprefix(tbl, 0, 3); /* Reserved upto ACPI 5.0 */
1766 build_append_int_noprefix(tbl, 0, 8); /* X_FIRMWARE_CTRL */
1768 /* XDSDT address to be filled by Guest linker at runtime */
1769 off = tbl->len;
1770 build_append_int_noprefix(tbl, 0, 8); /* X_DSDT */
1771 if (f->xdsdt_tbl_offset) {
1772 bios_linker_loader_add_pointer(linker,
1773 ACPI_BUILD_TABLE_FILE, off, 8,
1774 ACPI_BUILD_TABLE_FILE, *f->xdsdt_tbl_offset);
1777 build_append_gas_from_struct(tbl, &f->pm1a_evt); /* X_PM1a_EVT_BLK */
1778 /* X_PM1b_EVT_BLK */
1779 build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1780 build_append_gas_from_struct(tbl, &f->pm1a_cnt); /* X_PM1a_CNT_BLK */
1781 /* X_PM1b_CNT_BLK */
1782 build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1783 /* X_PM2_CNT_BLK */
1784 build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1785 build_append_gas_from_struct(tbl, &f->pm_tmr); /* X_PM_TMR_BLK */
1786 build_append_gas_from_struct(tbl, &f->gpe0_blk); /* X_GPE0_BLK */
1787 build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0); /* X_GPE1_BLK */
1789 if (f->rev <= 4) {
1790 goto build_hdr;
1793 /* SLEEP_CONTROL_REG */
1794 build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1795 /* SLEEP_STATUS_REG */
1796 build_append_gas(tbl, AML_AS_SYSTEM_MEMORY, 0 , 0, 0, 0);
1798 /* TODO: extra fields need to be added to support revisions above rev5 */
1799 assert(f->rev == 5);
1801 build_hdr:
1802 build_header(linker, tbl, (void *)(tbl->data + fadt_start),
1803 "FACP", tbl->len - fadt_start, f->rev, oem_id, oem_table_id);