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 "hw/acpi/bios-linker-loader.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
57 build_append_nameseg(GArray
*array
, const char *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
)
77 s
= g_strdup_vprintf(format
, ap
);
78 segs
= g_strsplit(s
, ".", 0);
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 */
95 while (*s
== '\\' || *s
== '^') {
96 build_append_byte(array
, *s
);
103 build_append_byte(array
, 0x00); /* NullName */
105 build_append_nameseg(array
, s
);
110 build_append_byte(array
, 0x2E); /* DualNamePrefix */
111 build_append_nameseg(array
, s
);
112 build_append_nameseg(array
, segs
[1]);
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;
124 build_append_nameseg(array
, *segs_iter
);
133 static void build_append_namestring(GArray
*array
, const char *format
, ...)
137 va_start(ap
, format
);
138 build_append_namestringv(array
, format
, ap
);
142 /* 5.4 Definition Block Encoding */
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,
151 build_prepend_package_length(GArray
*package
, unsigned length
, bool incl_self
)
154 unsigned length_bytes
;
156 if (length
+ 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT
)) {
158 } else if (length
+ 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT
)) {
160 } else if (length
+ 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT
)) {
167 * NamedField uses PkgLength encoding but it doesn't include length
168 * of PkgLength itself.
172 * PkgLength is the length of the inclusive length of the data
173 * and PkgLength's length itself when used for terms with
176 length
+= length_bytes
;
179 switch (length_bytes
) {
182 build_prepend_byte(package
, byte
);
185 byte
= length
>> PACKAGE_LENGTH_4BYTE_SHIFT
;
186 build_prepend_byte(package
, byte
);
187 length
&= (1 << PACKAGE_LENGTH_4BYTE_SHIFT
) - 1;
190 byte
= length
>> PACKAGE_LENGTH_3BYTE_SHIFT
;
191 build_prepend_byte(package
, byte
);
192 length
&= (1 << PACKAGE_LENGTH_3BYTE_SHIFT
) - 1;
195 byte
= length
>> PACKAGE_LENGTH_2BYTE_SHIFT
;
196 build_prepend_byte(package
, byte
);
197 length
&= (1 << PACKAGE_LENGTH_2BYTE_SHIFT
) - 1;
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
);
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 static void build_append_int_noprefix(GArray
*table
, uint64_t value
, int size
)
234 for (i
= 0; i
< size
; ++i
) {
235 build_append_byte(table
, value
& 0xFF);
240 static void build_append_int(GArray
*table
, uint64_t value
)
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);
256 build_append_byte(table
, 0x0E); /* QWordPrefix */
257 build_append_int_noprefix(table
, value
, 8);
261 static GPtrArray
*alloc_list
;
263 static Aml
*aml_alloc(void)
265 Aml
*var
= g_new0(typeof(*var
), 1);
267 g_ptr_array_add(alloc_list
, var
);
268 var
->block_flags
= AML_NO_OPCODE
;
269 var
->buf
= build_alloc_array();
273 static Aml
*aml_opcode(uint8_t op
)
275 Aml
*var
= aml_alloc();
278 var
->block_flags
= AML_OPCODE
;
282 static Aml
*aml_bundle(uint8_t op
, AmlBlockFlags flags
)
284 Aml
*var
= aml_alloc();
287 var
->block_flags
= flags
;
291 static void aml_free(gpointer data
, gpointer user_data
)
294 build_free_array(var
->buf
);
298 Aml
*init_aml_allocator(void)
303 alloc_list
= g_ptr_array_new();
308 void free_aml_allocator(void)
310 g_ptr_array_foreach(alloc_list
, aml_free
, NULL
);
311 g_ptr_array_free(alloc_list
, true);
315 /* pack data with DefBuffer encoding */
316 static void build_buffer(GArray
*array
, uint8_t op
)
318 GArray
*data
= build_alloc_array();
320 build_append_int(data
, array
->len
);
321 g_array_prepend_vals(array
, data
->data
, data
->len
);
322 build_free_array(data
);
323 build_package(array
, op
);
326 void aml_append(Aml
*parent_ctx
, Aml
*child
)
328 GArray
*buf
= build_alloc_array();
329 build_append_array(buf
, child
->buf
);
331 switch (child
->block_flags
) {
333 build_append_byte(parent_ctx
->buf
, child
->op
);
335 case AML_EXT_PACKAGE
:
336 build_extop_package(buf
, child
->op
);
339 build_package(buf
, child
->op
);
341 case AML_RES_TEMPLATE
:
342 build_append_byte(buf
, 0x79); /* EndTag */
344 * checksum operations are treated as succeeded if checksum
345 * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
347 build_append_byte(buf
, 0);
348 /* fall through, to pack resources in buffer */
350 build_buffer(buf
, child
->op
);
358 build_append_array(parent_ctx
->buf
, buf
);
359 build_free_array(buf
);
362 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
363 Aml
*aml_scope(const char *name_format
, ...)
366 Aml
*var
= aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE
);
367 va_start(ap
, name_format
);
368 build_append_namestringv(var
->buf
, name_format
, ap
);
373 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
374 Aml
*aml_return(Aml
*val
)
376 Aml
*var
= aml_opcode(0xA4 /* ReturnOp */);
377 aml_append(var
, val
);
382 * ACPI 1.0b: 16.2.3 Data Objects Encoding:
383 * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
385 Aml
*aml_int(const uint64_t val
)
387 Aml
*var
= aml_alloc();
388 build_append_int(var
->buf
, val
);
393 * helper to construct NameString, which returns Aml object
394 * for using with aml_append or other aml_* terms
396 Aml
*aml_name(const char *name_format
, ...)
399 Aml
*var
= aml_alloc();
400 va_start(ap
, name_format
);
401 build_append_namestringv(var
->buf
, name_format
, ap
);
406 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
407 Aml
*aml_name_decl(const char *name
, Aml
*val
)
409 Aml
*var
= aml_opcode(0x08 /* NameOp */);
410 build_append_namestring(var
->buf
, "%s", name
);
411 aml_append(var
, val
);
415 /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
416 Aml
*aml_arg(int pos
)
419 uint8_t op
= 0x68 /* ARG0 op */ + pos
;
422 var
= aml_opcode(op
);
426 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToInteger */
427 Aml
*aml_to_integer(Aml
*arg
)
429 Aml
*var
= aml_opcode(0x99 /* ToIntegerOp */);
430 aml_append(var
, arg
);
431 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
435 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToHexString */
436 Aml
*aml_to_hexstring(Aml
*src
, Aml
*dst
)
438 Aml
*var
= aml_opcode(0x98 /* ToHexStringOp */);
439 aml_append(var
, src
);
441 aml_append(var
, dst
);
443 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
448 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToBuffer */
449 Aml
*aml_to_buffer(Aml
*src
, Aml
*dst
)
451 Aml
*var
= aml_opcode(0x96 /* ToBufferOp */);
452 aml_append(var
, src
);
454 aml_append(var
, dst
);
456 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
461 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
462 Aml
*aml_store(Aml
*val
, Aml
*target
)
464 Aml
*var
= aml_opcode(0x70 /* StoreOp */);
465 aml_append(var
, val
);
466 aml_append(var
, target
);
471 * build_opcode_2arg_dst:
475 * @dst: optional target to store to, set to NULL if it's not required
477 * An internal helper to compose AML terms that have
478 * "Op Operand Operand Target"
481 * Returns: The newly allocated and composed according to patter Aml object.
484 build_opcode_2arg_dst(uint8_t op
, Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
486 Aml
*var
= aml_opcode(op
);
487 aml_append(var
, arg1
);
488 aml_append(var
, arg2
);
490 aml_append(var
, dst
);
492 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
497 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
498 Aml
*aml_and(Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
500 return build_opcode_2arg_dst(0x7B /* AndOp */, arg1
, arg2
, dst
);
503 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */
504 Aml
*aml_or(Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
506 return build_opcode_2arg_dst(0x7D /* OrOp */, arg1
, arg2
, dst
);
509 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLOr */
510 Aml
*aml_lor(Aml
*arg1
, Aml
*arg2
)
512 Aml
*var
= aml_opcode(0x91 /* LOrOp */);
513 aml_append(var
, arg1
);
514 aml_append(var
, arg2
);
518 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftLeft */
519 Aml
*aml_shiftleft(Aml
*arg1
, Aml
*count
)
521 return build_opcode_2arg_dst(0x79 /* ShiftLeftOp */, arg1
, count
, NULL
);
524 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftRight */
525 Aml
*aml_shiftright(Aml
*arg1
, Aml
*count
, Aml
*dst
)
527 return build_opcode_2arg_dst(0x7A /* ShiftRightOp */, arg1
, count
, dst
);
530 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLess */
531 Aml
*aml_lless(Aml
*arg1
, Aml
*arg2
)
533 Aml
*var
= aml_opcode(0x95 /* LLessOp */);
534 aml_append(var
, arg1
);
535 aml_append(var
, arg2
);
539 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
540 Aml
*aml_add(Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
542 return build_opcode_2arg_dst(0x72 /* AddOp */, arg1
, arg2
, dst
);
545 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSubtract */
546 Aml
*aml_subtract(Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
548 return build_opcode_2arg_dst(0x74 /* SubtractOp */, arg1
, arg2
, dst
);
551 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIncrement */
552 Aml
*aml_increment(Aml
*arg
)
554 Aml
*var
= aml_opcode(0x75 /* IncrementOp */);
555 aml_append(var
, arg
);
559 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDecrement */
560 Aml
*aml_decrement(Aml
*arg
)
562 Aml
*var
= aml_opcode(0x76 /* DecrementOp */);
563 aml_append(var
, arg
);
567 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIndex */
568 Aml
*aml_index(Aml
*arg1
, Aml
*idx
)
570 return build_opcode_2arg_dst(0x88 /* IndexOp */, arg1
, idx
, NULL
);
573 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
574 Aml
*aml_notify(Aml
*arg1
, Aml
*arg2
)
576 Aml
*var
= aml_opcode(0x86 /* NotifyOp */);
577 aml_append(var
, arg1
);
578 aml_append(var
, arg2
);
582 /* helper to call method with 1 argument */
583 Aml
*aml_call0(const char *method
)
585 Aml
*var
= aml_alloc();
586 build_append_namestring(var
->buf
, "%s", method
);
590 /* helper to call method with 1 argument */
591 Aml
*aml_call1(const char *method
, Aml
*arg1
)
593 Aml
*var
= aml_alloc();
594 build_append_namestring(var
->buf
, "%s", method
);
595 aml_append(var
, arg1
);
599 /* helper to call method with 2 arguments */
600 Aml
*aml_call2(const char *method
, Aml
*arg1
, Aml
*arg2
)
602 Aml
*var
= aml_alloc();
603 build_append_namestring(var
->buf
, "%s", method
);
604 aml_append(var
, arg1
);
605 aml_append(var
, arg2
);
609 /* helper to call method with 3 arguments */
610 Aml
*aml_call3(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
)
612 Aml
*var
= aml_alloc();
613 build_append_namestring(var
->buf
, "%s", method
);
614 aml_append(var
, arg1
);
615 aml_append(var
, arg2
);
616 aml_append(var
, arg3
);
620 /* helper to call method with 4 arguments */
621 Aml
*aml_call4(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
, Aml
*arg4
)
623 Aml
*var
= aml_alloc();
624 build_append_namestring(var
->buf
, "%s", method
);
625 aml_append(var
, arg1
);
626 aml_append(var
, arg2
);
627 aml_append(var
, arg3
);
628 aml_append(var
, arg4
);
633 * ACPI 5.0: 6.4.3.8.1 GPIO Connection Descriptor
634 * Type 1, Large Item Name 0xC
637 static Aml
*aml_gpio_connection(AmlGpioConnectionType type
,
638 AmlConsumerAndProducer con_and_pro
,
639 uint8_t flags
, AmlPinConfig pin_config
,
640 uint16_t output_drive
,
641 uint16_t debounce_timeout
,
642 const uint32_t pin_list
[], uint32_t pin_count
,
643 const char *resource_source_name
,
644 const uint8_t *vendor_data
,
645 uint16_t vendor_data_len
)
647 Aml
*var
= aml_alloc();
648 const uint16_t min_desc_len
= 0x16;
649 uint16_t resource_source_name_len
, length
;
650 uint16_t pin_table_offset
, resource_source_name_offset
, vendor_data_offset
;
653 assert(resource_source_name
);
654 resource_source_name_len
= strlen(resource_source_name
) + 1;
655 length
= min_desc_len
+ resource_source_name_len
+ vendor_data_len
;
656 pin_table_offset
= min_desc_len
+ 1;
657 resource_source_name_offset
= pin_table_offset
+ pin_count
* 2;
658 vendor_data_offset
= resource_source_name_offset
+ resource_source_name_len
;
660 build_append_byte(var
->buf
, 0x8C); /* GPIO Connection Descriptor */
661 build_append_int_noprefix(var
->buf
, length
, 2); /* Length */
662 build_append_byte(var
->buf
, 1); /* Revision ID */
663 build_append_byte(var
->buf
, type
); /* GPIO Connection Type */
664 /* General Flags (2 bytes) */
665 build_append_int_noprefix(var
->buf
, con_and_pro
, 2);
666 /* Interrupt and IO Flags (2 bytes) */
667 build_append_int_noprefix(var
->buf
, flags
, 2);
668 /* Pin Configuration 0 = Default 1 = Pull-up 2 = Pull-down 3 = No Pull */
669 build_append_byte(var
->buf
, pin_config
);
670 /* Output Drive Strength (2 bytes) */
671 build_append_int_noprefix(var
->buf
, output_drive
, 2);
672 /* Debounce Timeout (2 bytes) */
673 build_append_int_noprefix(var
->buf
, debounce_timeout
, 2);
674 /* Pin Table Offset (2 bytes) */
675 build_append_int_noprefix(var
->buf
, pin_table_offset
, 2);
676 build_append_byte(var
->buf
, 0); /* Resource Source Index */
677 /* Resource Source Name Offset (2 bytes) */
678 build_append_int_noprefix(var
->buf
, resource_source_name_offset
, 2);
679 /* Vendor Data Offset (2 bytes) */
680 build_append_int_noprefix(var
->buf
, vendor_data_offset
, 2);
681 /* Vendor Data Length (2 bytes) */
682 build_append_int_noprefix(var
->buf
, vendor_data_len
, 2);
683 /* Pin Number (2n bytes)*/
684 for (i
= 0; i
< pin_count
; i
++) {
685 build_append_int_noprefix(var
->buf
, pin_list
[i
], 2);
688 /* Resource Source Name */
689 build_append_namestring(var
->buf
, "%s", resource_source_name
);
690 build_append_byte(var
->buf
, '\0');
692 /* Vendor-defined Data */
693 if (vendor_data
!= NULL
) {
694 g_array_append_vals(var
->buf
, vendor_data
, vendor_data_len
);
702 * GpioInt(GPIO Interrupt Connection Resource Descriptor Macro)
704 Aml
*aml_gpio_int(AmlConsumerAndProducer con_and_pro
,
705 AmlLevelAndEdge edge_level
,
706 AmlActiveHighAndLow active_level
, AmlShared shared
,
707 AmlPinConfig pin_config
, uint16_t debounce_timeout
,
708 const uint32_t pin_list
[], uint32_t pin_count
,
709 const char *resource_source_name
,
710 const uint8_t *vendor_data
, uint16_t vendor_data_len
)
712 uint8_t flags
= edge_level
| (active_level
<< 1) | (shared
<< 3);
714 return aml_gpio_connection(AML_INTERRUPT_CONNECTION
, con_and_pro
, flags
,
715 pin_config
, 0, debounce_timeout
, pin_list
,
716 pin_count
, resource_source_name
, vendor_data
,
721 * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor
722 * (Type 1, Large Item Name 0x6)
724 Aml
*aml_memory32_fixed(uint32_t addr
, uint32_t size
,
725 AmlReadAndWrite read_and_write
)
727 Aml
*var
= aml_alloc();
728 build_append_byte(var
->buf
, 0x86); /* Memory32Fixed Resource Descriptor */
729 build_append_byte(var
->buf
, 9); /* Length, bits[7:0] value = 9 */
730 build_append_byte(var
->buf
, 0); /* Length, bits[15:8] value = 0 */
731 build_append_byte(var
->buf
, read_and_write
); /* Write status, 1 rw 0 ro */
733 /* Range base address */
734 build_append_byte(var
->buf
, extract32(addr
, 0, 8)); /* bits[7:0] */
735 build_append_byte(var
->buf
, extract32(addr
, 8, 8)); /* bits[15:8] */
736 build_append_byte(var
->buf
, extract32(addr
, 16, 8)); /* bits[23:16] */
737 build_append_byte(var
->buf
, extract32(addr
, 24, 8)); /* bits[31:24] */
740 build_append_byte(var
->buf
, extract32(size
, 0, 8)); /* bits[7:0] */
741 build_append_byte(var
->buf
, extract32(size
, 8, 8)); /* bits[15:8] */
742 build_append_byte(var
->buf
, extract32(size
, 16, 8)); /* bits[23:16] */
743 build_append_byte(var
->buf
, extract32(size
, 24, 8)); /* bits[31:24] */
748 * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor
749 * Type 1, Large Item Name 0x9
751 Aml
*aml_interrupt(AmlConsumerAndProducer con_and_pro
,
752 AmlLevelAndEdge level_and_edge
,
753 AmlActiveHighAndLow high_and_low
, AmlShared shared
,
754 uint32_t *irq_list
, uint8_t irq_count
)
757 Aml
*var
= aml_alloc();
758 uint8_t irq_flags
= con_and_pro
| (level_and_edge
<< 1)
759 | (high_and_low
<< 2) | (shared
<< 3);
760 const int header_bytes_in_len
= 2;
761 uint16_t len
= header_bytes_in_len
+ irq_count
* sizeof(uint32_t);
763 assert(irq_count
> 0);
765 build_append_byte(var
->buf
, 0x89); /* Extended irq descriptor */
766 build_append_byte(var
->buf
, len
& 0xFF); /* Length, bits[7:0] */
767 build_append_byte(var
->buf
, len
>> 8); /* Length, bits[15:8] */
768 build_append_byte(var
->buf
, irq_flags
); /* Interrupt Vector Information. */
769 build_append_byte(var
->buf
, irq_count
); /* Interrupt table length */
771 /* Interrupt Number List */
772 for (i
= 0; i
< irq_count
; i
++) {
773 build_append_int_noprefix(var
->buf
, irq_list
[i
], 4);
778 /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */
779 Aml
*aml_io(AmlIODecode dec
, uint16_t min_base
, uint16_t max_base
,
780 uint8_t aln
, uint8_t len
)
782 Aml
*var
= aml_alloc();
783 build_append_byte(var
->buf
, 0x47); /* IO port descriptor */
784 build_append_byte(var
->buf
, dec
);
785 build_append_byte(var
->buf
, min_base
& 0xff);
786 build_append_byte(var
->buf
, (min_base
>> 8) & 0xff);
787 build_append_byte(var
->buf
, max_base
& 0xff);
788 build_append_byte(var
->buf
, (max_base
>> 8) & 0xff);
789 build_append_byte(var
->buf
, aln
);
790 build_append_byte(var
->buf
, len
);
795 * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor
797 * More verbose description at:
798 * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro)
799 * 6.4.2.1 IRQ Descriptor
801 Aml
*aml_irq_no_flags(uint8_t irq
)
804 Aml
*var
= aml_alloc();
807 build_append_byte(var
->buf
, 0x22); /* IRQ descriptor 2 byte form */
809 irq_mask
= 1U << irq
;
810 build_append_byte(var
->buf
, irq_mask
& 0xFF); /* IRQ mask bits[7:0] */
811 build_append_byte(var
->buf
, irq_mask
>> 8); /* IRQ mask bits[15:8] */
815 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */
816 Aml
*aml_lnot(Aml
*arg
)
818 Aml
*var
= aml_opcode(0x92 /* LNotOp */);
819 aml_append(var
, arg
);
823 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */
824 Aml
*aml_equal(Aml
*arg1
, Aml
*arg2
)
826 Aml
*var
= aml_opcode(0x93 /* LequalOp */);
827 aml_append(var
, arg1
);
828 aml_append(var
, arg2
);
832 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreater */
833 Aml
*aml_lgreater(Aml
*arg1
, Aml
*arg2
)
835 Aml
*var
= aml_opcode(0x94 /* LGreaterOp */);
836 aml_append(var
, arg1
);
837 aml_append(var
, arg2
);
841 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreaterEqual */
842 Aml
*aml_lgreater_equal(Aml
*arg1
, Aml
*arg2
)
844 /* LGreaterEqualOp := LNotOp LLessOp */
845 Aml
*var
= aml_opcode(0x92 /* LNotOp */);
846 build_append_byte(var
->buf
, 0x95 /* LLessOp */);
847 aml_append(var
, arg1
);
848 aml_append(var
, arg2
);
852 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
853 Aml
*aml_if(Aml
*predicate
)
855 Aml
*var
= aml_bundle(0xA0 /* IfOp */, AML_PACKAGE
);
856 aml_append(var
, predicate
);
860 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */
863 Aml
*var
= aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE
);
867 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefWhile */
868 Aml
*aml_while(Aml
*predicate
)
870 Aml
*var
= aml_bundle(0xA2 /* WhileOp */, AML_PACKAGE
);
871 aml_append(var
, predicate
);
875 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
876 Aml
*aml_method(const char *name
, int arg_count
, AmlSerializeFlag sflag
)
878 Aml
*var
= aml_bundle(0x14 /* MethodOp */, AML_PACKAGE
);
883 * bit 0-2: ArgCount (0-7)
884 * bit 3: SerializeFlag
887 * bit 4-7: reserved (must be 0)
889 assert(arg_count
< 8);
890 methodflags
= arg_count
| (sflag
<< 3);
892 build_append_namestring(var
->buf
, "%s", name
);
893 build_append_byte(var
->buf
, methodflags
); /* MethodFlags: ArgCount */
897 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
898 Aml
*aml_device(const char *name_format
, ...)
901 Aml
*var
= aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE
);
902 va_start(ap
, name_format
);
903 build_append_namestringv(var
->buf
, name_format
, ap
);
908 /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */
909 Aml
*aml_resource_template(void)
911 /* ResourceTemplate is a buffer of Resources with EndTag at the end */
912 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE
);
916 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer
917 * Pass byte_list as NULL to request uninitialized buffer to reserve space.
919 Aml
*aml_buffer(int buffer_size
, uint8_t *byte_list
)
922 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_BUFFER
);
924 for (i
= 0; i
< buffer_size
; i
++) {
925 if (byte_list
== NULL
) {
926 build_append_byte(var
->buf
, 0x0);
928 build_append_byte(var
->buf
, byte_list
[i
]);
935 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */
936 Aml
*aml_package(uint8_t num_elements
)
938 Aml
*var
= aml_bundle(0x12 /* PackageOp */, AML_PACKAGE
);
939 build_append_byte(var
->buf
, num_elements
);
943 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */
944 Aml
*aml_operation_region(const char *name
, AmlRegionSpace rs
,
945 uint32_t offset
, uint32_t len
)
947 Aml
*var
= aml_alloc();
948 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
949 build_append_byte(var
->buf
, 0x80); /* OpRegionOp */
950 build_append_namestring(var
->buf
, "%s", name
);
951 build_append_byte(var
->buf
, rs
);
952 build_append_int(var
->buf
, offset
);
953 build_append_int(var
->buf
, len
);
957 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */
958 Aml
*aml_named_field(const char *name
, unsigned length
)
960 Aml
*var
= aml_alloc();
961 build_append_nameseg(var
->buf
, name
);
962 build_append_pkg_length(var
->buf
, length
, false);
966 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */
967 Aml
*aml_reserved_field(unsigned length
)
969 Aml
*var
= aml_alloc();
970 /* ReservedField := 0x00 PkgLength */
971 build_append_byte(var
->buf
, 0x00);
972 build_append_pkg_length(var
->buf
, length
, false);
976 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */
977 Aml
*aml_field(const char *name
, AmlAccessType type
, AmlLockRule lock
,
980 Aml
*var
= aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE
);
981 uint8_t flags
= rule
<< 5 | type
;
983 flags
|= lock
<< 4; /* LockRule at 4 bit offset */
985 build_append_namestring(var
->buf
, "%s", name
);
986 build_append_byte(var
->buf
, flags
);
991 Aml
*create_field_common(int opcode
, Aml
*srcbuf
, Aml
*index
, const char *name
)
993 Aml
*var
= aml_opcode(opcode
);
994 aml_append(var
, srcbuf
);
995 aml_append(var
, index
);
996 build_append_namestring(var
->buf
, "%s", name
);
1000 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */
1001 Aml
*aml_create_dword_field(Aml
*srcbuf
, Aml
*index
, const char *name
)
1003 return create_field_common(0x8A /* CreateDWordFieldOp */,
1004 srcbuf
, index
, name
);
1007 /* ACPI 2.0a: 17.2.4.2 Named Objects Encoding: DefCreateQWordField */
1008 Aml
*aml_create_qword_field(Aml
*srcbuf
, Aml
*index
, const char *name
)
1010 return create_field_common(0x8F /* CreateQWordFieldOp */,
1011 srcbuf
, index
, name
);
1014 /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */
1015 Aml
*aml_string(const char *name_format
, ...)
1017 Aml
*var
= aml_opcode(0x0D /* StringPrefix */);
1022 va_start(ap
, name_format
);
1023 len
= g_vasprintf(&s
, name_format
, ap
);
1026 g_array_append_vals(var
->buf
, s
, len
+ 1);
1032 /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */
1033 Aml
*aml_local(int num
)
1036 uint8_t op
= 0x60 /* Local0Op */ + num
;
1039 var
= aml_opcode(op
);
1043 /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */
1044 Aml
*aml_varpackage(uint32_t num_elements
)
1046 Aml
*var
= aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE
);
1047 build_append_int(var
->buf
, num_elements
);
1051 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */
1052 Aml
*aml_processor(uint8_t proc_id
, uint32_t pblk_addr
, uint8_t pblk_len
,
1053 const char *name_format
, ...)
1056 Aml
*var
= aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE
);
1057 va_start(ap
, name_format
);
1058 build_append_namestringv(var
->buf
, name_format
, ap
);
1060 build_append_byte(var
->buf
, proc_id
); /* ProcID */
1061 build_append_int_noprefix(var
->buf
, pblk_addr
, sizeof(pblk_addr
));
1062 build_append_byte(var
->buf
, pblk_len
); /* PblkLen */
1066 static uint8_t Hex2Digit(char c
)
1069 return c
- 'A' + 10;
1075 /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */
1076 Aml
*aml_eisaid(const char *str
)
1078 Aml
*var
= aml_alloc();
1081 g_assert(strlen(str
) == 7);
1082 id
= (str
[0] - 0x40) << 26 |
1083 (str
[1] - 0x40) << 21 |
1084 (str
[2] - 0x40) << 16 |
1085 Hex2Digit(str
[3]) << 12 |
1086 Hex2Digit(str
[4]) << 8 |
1087 Hex2Digit(str
[5]) << 4 |
1090 build_append_byte(var
->buf
, 0x0C); /* DWordPrefix */
1091 build_append_int_noprefix(var
->buf
, bswap32(id
), sizeof(id
));
1095 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */
1096 static Aml
*aml_as_desc_header(AmlResourceType type
, AmlMinFixed min_fixed
,
1097 AmlMaxFixed max_fixed
, AmlDecode dec
,
1100 uint8_t flags
= max_fixed
| min_fixed
| dec
;
1101 Aml
*var
= aml_alloc();
1103 build_append_byte(var
->buf
, type
);
1104 build_append_byte(var
->buf
, flags
);
1105 build_append_byte(var
->buf
, type_flags
); /* Type Specific Flags */
1109 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */
1110 static Aml
*aml_word_as_desc(AmlResourceType type
, AmlMinFixed min_fixed
,
1111 AmlMaxFixed max_fixed
, AmlDecode dec
,
1112 uint16_t addr_gran
, uint16_t addr_min
,
1113 uint16_t addr_max
, uint16_t addr_trans
,
1114 uint16_t len
, uint8_t type_flags
)
1116 Aml
*var
= aml_alloc();
1118 build_append_byte(var
->buf
, 0x88); /* Word Address Space Descriptor */
1119 /* minimum length since we do not encode optional fields */
1120 build_append_byte(var
->buf
, 0x0D);
1121 build_append_byte(var
->buf
, 0x0);
1124 aml_as_desc_header(type
, min_fixed
, max_fixed
, dec
, type_flags
));
1125 build_append_int_noprefix(var
->buf
, addr_gran
, sizeof(addr_gran
));
1126 build_append_int_noprefix(var
->buf
, addr_min
, sizeof(addr_min
));
1127 build_append_int_noprefix(var
->buf
, addr_max
, sizeof(addr_max
));
1128 build_append_int_noprefix(var
->buf
, addr_trans
, sizeof(addr_trans
));
1129 build_append_int_noprefix(var
->buf
, len
, sizeof(len
));
1133 /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */
1134 static Aml
*aml_dword_as_desc(AmlResourceType type
, AmlMinFixed min_fixed
,
1135 AmlMaxFixed max_fixed
, AmlDecode dec
,
1136 uint32_t addr_gran
, uint32_t addr_min
,
1137 uint32_t addr_max
, uint32_t addr_trans
,
1138 uint32_t len
, uint8_t type_flags
)
1140 Aml
*var
= aml_alloc();
1142 build_append_byte(var
->buf
, 0x87); /* DWord Address Space Descriptor */
1143 /* minimum length since we do not encode optional fields */
1144 build_append_byte(var
->buf
, 23);
1145 build_append_byte(var
->buf
, 0x0);
1149 aml_as_desc_header(type
, min_fixed
, max_fixed
, dec
, type_flags
));
1150 build_append_int_noprefix(var
->buf
, addr_gran
, sizeof(addr_gran
));
1151 build_append_int_noprefix(var
->buf
, addr_min
, sizeof(addr_min
));
1152 build_append_int_noprefix(var
->buf
, addr_max
, sizeof(addr_max
));
1153 build_append_int_noprefix(var
->buf
, addr_trans
, sizeof(addr_trans
));
1154 build_append_int_noprefix(var
->buf
, len
, sizeof(len
));
1158 /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */
1159 static Aml
*aml_qword_as_desc(AmlResourceType type
, AmlMinFixed min_fixed
,
1160 AmlMaxFixed max_fixed
, AmlDecode dec
,
1161 uint64_t addr_gran
, uint64_t addr_min
,
1162 uint64_t addr_max
, uint64_t addr_trans
,
1163 uint64_t len
, uint8_t type_flags
)
1165 Aml
*var
= aml_alloc();
1167 build_append_byte(var
->buf
, 0x8A); /* QWord Address Space Descriptor */
1168 /* minimum length since we do not encode optional fields */
1169 build_append_byte(var
->buf
, 0x2B);
1170 build_append_byte(var
->buf
, 0x0);
1173 aml_as_desc_header(type
, min_fixed
, max_fixed
, dec
, type_flags
));
1174 build_append_int_noprefix(var
->buf
, addr_gran
, sizeof(addr_gran
));
1175 build_append_int_noprefix(var
->buf
, addr_min
, sizeof(addr_min
));
1176 build_append_int_noprefix(var
->buf
, addr_max
, sizeof(addr_max
));
1177 build_append_int_noprefix(var
->buf
, addr_trans
, sizeof(addr_trans
));
1178 build_append_int_noprefix(var
->buf
, len
, sizeof(len
));
1183 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1185 * More verbose description at:
1186 * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro)
1188 Aml
*aml_word_bus_number(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
1189 AmlDecode dec
, uint16_t addr_gran
,
1190 uint16_t addr_min
, uint16_t addr_max
,
1191 uint16_t addr_trans
, uint16_t len
)
1194 return aml_word_as_desc(AML_BUS_NUMBER_RANGE
, min_fixed
, max_fixed
, dec
,
1195 addr_gran
, addr_min
, addr_max
, addr_trans
, len
, 0);
1199 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1201 * More verbose description at:
1202 * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro)
1204 Aml
*aml_word_io(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
1205 AmlDecode dec
, AmlISARanges isa_ranges
,
1206 uint16_t addr_gran
, uint16_t addr_min
,
1207 uint16_t addr_max
, uint16_t addr_trans
,
1211 return aml_word_as_desc(AML_IO_RANGE
, min_fixed
, max_fixed
, dec
,
1212 addr_gran
, addr_min
, addr_max
, addr_trans
, len
,
1217 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor
1219 * More verbose description at:
1220 * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro)
1222 Aml
*aml_dword_io(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
1223 AmlDecode dec
, AmlISARanges isa_ranges
,
1224 uint32_t addr_gran
, uint32_t addr_min
,
1225 uint32_t addr_max
, uint32_t addr_trans
,
1229 return aml_dword_as_desc(AML_IO_RANGE
, min_fixed
, max_fixed
, dec
,
1230 addr_gran
, addr_min
, addr_max
, addr_trans
, len
,
1235 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor
1237 * More verbose description at:
1238 * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro)
1240 Aml
*aml_dword_memory(AmlDecode dec
, AmlMinFixed min_fixed
,
1241 AmlMaxFixed max_fixed
, AmlCacheable cacheable
,
1242 AmlReadAndWrite read_and_write
,
1243 uint32_t addr_gran
, uint32_t addr_min
,
1244 uint32_t addr_max
, uint32_t addr_trans
,
1247 uint8_t flags
= read_and_write
| (cacheable
<< 1);
1249 return aml_dword_as_desc(AML_MEMORY_RANGE
, min_fixed
, max_fixed
,
1250 dec
, addr_gran
, addr_min
, addr_max
,
1251 addr_trans
, len
, flags
);
1255 * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor
1257 * More verbose description at:
1258 * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro)
1260 Aml
*aml_qword_memory(AmlDecode dec
, AmlMinFixed min_fixed
,
1261 AmlMaxFixed max_fixed
, AmlCacheable cacheable
,
1262 AmlReadAndWrite read_and_write
,
1263 uint64_t addr_gran
, uint64_t addr_min
,
1264 uint64_t addr_max
, uint64_t addr_trans
,
1267 uint8_t flags
= read_and_write
| (cacheable
<< 1);
1269 return aml_qword_as_desc(AML_MEMORY_RANGE
, min_fixed
, max_fixed
,
1270 dec
, addr_gran
, addr_min
, addr_max
,
1271 addr_trans
, len
, flags
);
1274 /* ACPI 1.0b: 6.4.2.2 DMA Format/6.4.2.2.1 ASL Macro for DMA Descriptor */
1275 Aml
*aml_dma(AmlDmaType typ
, AmlDmaBusMaster bm
, AmlTransferSize sz
,
1278 Aml
*var
= aml_alloc();
1279 uint8_t flags
= sz
| bm
<< 2 | typ
<< 5;
1281 assert(channel
< 8);
1282 build_append_byte(var
->buf
, 0x2A); /* Byte 0: DMA Descriptor */
1283 build_append_byte(var
->buf
, 1U << channel
); /* Byte 1: _DMA - DmaChannel */
1284 build_append_byte(var
->buf
, flags
); /* Byte 2 */
1288 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefSleep */
1289 Aml
*aml_sleep(uint64_t msec
)
1291 Aml
*var
= aml_alloc();
1292 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1293 build_append_byte(var
->buf
, 0x22); /* SleepOp */
1294 aml_append(var
, aml_int(msec
));
1298 static uint8_t Hex2Byte(const char *src
)
1302 hi
= Hex2Digit(src
[0]);
1306 lo
= Hex2Digit(src
[1]);
1309 return (hi
<< 4) | lo
;
1313 * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro)
1314 * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1315 * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp");
1317 Aml
*aml_touuid(const char *uuid
)
1319 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_BUFFER
);
1321 assert(strlen(uuid
) == 36);
1322 assert(uuid
[8] == '-');
1323 assert(uuid
[13] == '-');
1324 assert(uuid
[18] == '-');
1325 assert(uuid
[23] == '-');
1327 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 6)); /* dd - at offset 00 */
1328 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 4)); /* cc - at offset 01 */
1329 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 2)); /* bb - at offset 02 */
1330 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 0)); /* aa - at offset 03 */
1332 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 11)); /* ff - at offset 04 */
1333 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 9)); /* ee - at offset 05 */
1335 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 16)); /* hh - at offset 06 */
1336 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 14)); /* gg - at offset 07 */
1338 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 19)); /* ii - at offset 08 */
1339 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 21)); /* jj - at offset 09 */
1341 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 24)); /* kk - at offset 10 */
1342 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 26)); /* ll - at offset 11 */
1343 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 28)); /* mm - at offset 12 */
1344 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 30)); /* nn - at offset 13 */
1345 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 32)); /* oo - at offset 14 */
1346 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 34)); /* pp - at offset 15 */
1352 * ACPI 2.0b: 16.2.3.6.4.3 Unicode Macro (Convert Ascii String To Unicode)
1354 Aml
*aml_unicode(const char *str
)
1357 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_BUFFER
);
1360 build_append_byte(var
->buf
, str
[i
]);
1361 build_append_byte(var
->buf
, 0);
1363 } while (i
<= strlen(str
));
1368 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDerefOf */
1369 Aml
*aml_derefof(Aml
*arg
)
1371 Aml
*var
= aml_opcode(0x83 /* DerefOfOp */);
1372 aml_append(var
, arg
);
1376 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSizeOf */
1377 Aml
*aml_sizeof(Aml
*arg
)
1379 Aml
*var
= aml_opcode(0x87 /* SizeOfOp */);
1380 aml_append(var
, arg
);
1384 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMutex */
1385 Aml
*aml_mutex(const char *name
, uint8_t sync_level
)
1387 Aml
*var
= aml_alloc();
1388 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1389 build_append_byte(var
->buf
, 0x01); /* MutexOp */
1390 build_append_namestring(var
->buf
, "%s", name
);
1391 assert(!(sync_level
& 0xF0));
1392 build_append_byte(var
->buf
, sync_level
);
1396 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAcquire */
1397 Aml
*aml_acquire(Aml
*mutex
, uint16_t timeout
)
1399 Aml
*var
= aml_alloc();
1400 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1401 build_append_byte(var
->buf
, 0x23); /* AcquireOp */
1402 aml_append(var
, mutex
);
1403 build_append_int_noprefix(var
->buf
, timeout
, sizeof(timeout
));
1407 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefRelease */
1408 Aml
*aml_release(Aml
*mutex
)
1410 Aml
*var
= aml_alloc();
1411 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1412 build_append_byte(var
->buf
, 0x27); /* ReleaseOp */
1413 aml_append(var
, mutex
);
1417 /* ACPI 1.0b: 16.2.5.1 Name Space Modifier Objects Encoding: DefAlias */
1418 Aml
*aml_alias(const char *source_object
, const char *alias_object
)
1420 Aml
*var
= aml_opcode(0x06 /* AliasOp */);
1421 aml_append(var
, aml_name("%s", source_object
));
1422 aml_append(var
, aml_name("%s", alias_object
));
1427 build_header(GArray
*linker
, GArray
*table_data
,
1428 AcpiTableHeader
*h
, const char *sig
, int len
, uint8_t rev
,
1429 const char *oem_id
, const char *oem_table_id
)
1431 memcpy(&h
->signature
, sig
, 4);
1432 h
->length
= cpu_to_le32(len
);
1436 strncpy((char *)h
->oem_id
, oem_id
, sizeof h
->oem_id
);
1438 memcpy(h
->oem_id
, ACPI_BUILD_APPNAME6
, 6);
1442 strncpy((char *)h
->oem_table_id
, oem_table_id
, sizeof(h
->oem_table_id
));
1444 memcpy(h
->oem_table_id
, ACPI_BUILD_APPNAME4
, 4);
1445 memcpy(h
->oem_table_id
+ 4, sig
, 4);
1448 h
->oem_revision
= cpu_to_le32(1);
1449 memcpy(h
->asl_compiler_id
, ACPI_BUILD_APPNAME4
, 4);
1450 h
->asl_compiler_revision
= cpu_to_le32(1);
1452 /* Checksum to be filled in by Guest linker */
1453 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_TABLE_FILE
,
1454 table_data
, h
, len
, &h
->checksum
);
1457 void *acpi_data_push(GArray
*table_data
, unsigned size
)
1459 unsigned off
= table_data
->len
;
1460 g_array_set_size(table_data
, off
+ size
);
1461 return table_data
->data
+ off
;
1464 unsigned acpi_data_len(GArray
*table
)
1466 assert(g_array_get_element_size(table
) == 1);
1470 void acpi_add_table(GArray
*table_offsets
, GArray
*table_data
)
1472 uint32_t offset
= cpu_to_le32(table_data
->len
);
1473 g_array_append_val(table_offsets
, offset
);
1476 void acpi_build_tables_init(AcpiBuildTables
*tables
)
1478 tables
->rsdp
= g_array_new(false, true /* clear */, 1);
1479 tables
->table_data
= g_array_new(false, true /* clear */, 1);
1480 tables
->tcpalog
= g_array_new(false, true /* clear */, 1);
1481 tables
->linker
= bios_linker_loader_init();
1484 void acpi_build_tables_cleanup(AcpiBuildTables
*tables
, bool mfre
)
1486 void *linker_data
= bios_linker_loader_cleanup(tables
->linker
);
1487 g_free(linker_data
);
1488 g_array_free(tables
->rsdp
, true);
1489 g_array_free(tables
->table_data
, true);
1490 g_array_free(tables
->tcpalog
, mfre
);
1493 /* Build rsdt table */
1495 build_rsdt(GArray
*table_data
, GArray
*linker
, GArray
*table_offsets
,
1496 const char *oem_id
, const char *oem_table_id
)
1498 AcpiRsdtDescriptorRev1
*rsdt
;
1501 const int table_data_len
= (sizeof(uint32_t) * table_offsets
->len
);
1503 rsdt_len
= sizeof(*rsdt
) + table_data_len
;
1504 rsdt
= acpi_data_push(table_data
, rsdt_len
);
1505 memcpy(rsdt
->table_offset_entry
, table_offsets
->data
, table_data_len
);
1506 for (i
= 0; i
< table_offsets
->len
; ++i
) {
1507 /* rsdt->table_offset_entry to be filled by Guest linker */
1508 bios_linker_loader_add_pointer(linker
,
1509 ACPI_BUILD_TABLE_FILE
,
1510 ACPI_BUILD_TABLE_FILE
,
1511 table_data
, &rsdt
->table_offset_entry
[i
],
1514 build_header(linker
, table_data
,
1515 (void *)rsdt
, "RSDT", rsdt_len
, 1, oem_id
, oem_table_id
);