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/>.
27 #include "hw/acpi/aml-build.h"
28 #include "qemu/bswap.h"
29 #include "qemu/bitops.h"
30 #include "hw/acpi/bios-linker-loader.h"
32 static GArray
*build_alloc_array(void)
34 return g_array_new(false, true /* clear */, 1);
37 static void build_free_array(GArray
*array
)
39 g_array_free(array
, true);
42 static void build_prepend_byte(GArray
*array
, uint8_t val
)
44 g_array_prepend_val(array
, val
);
47 static void build_append_byte(GArray
*array
, uint8_t val
)
49 g_array_append_val(array
, val
);
52 static void build_append_array(GArray
*array
, GArray
*val
)
54 g_array_append_vals(array
, val
->data
, val
->len
);
57 #define ACPI_NAMESEG_LEN 4
60 build_append_nameseg(GArray
*array
, const char *seg
)
62 /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */
66 assert(len
<= ACPI_NAMESEG_LEN
);
68 g_array_append_vals(array
, seg
, len
);
69 /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */
70 g_array_append_vals(array
, "____", ACPI_NAMESEG_LEN
- len
);
73 static void GCC_FMT_ATTR(2, 0)
74 build_append_namestringv(GArray
*array
, const char *format
, va_list ap
)
76 /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */
85 len
= vsnprintf(NULL
, 0, format
, va_len
);
88 s
= g_new(typeof(*s
), len
);
90 len
= vsnprintf(s
, len
, format
, ap
);
92 segs
= g_strsplit(s
, ".", 0);
102 * ACPI 5.0 spec: 20.2.2 Name Objects Encoding:
103 * "SegCount can be from 1 to 255"
105 assert(seg_count
> 0 && seg_count
<= 255);
107 /* handle RootPath || PrefixPath */
109 while (*s
== '\\' || *s
== '^') {
110 build_append_byte(array
, *s
);
117 build_append_byte(array
, 0x00); /* NullName */
119 build_append_nameseg(array
, s
);
124 build_append_byte(array
, 0x2E); /* DualNamePrefix */
125 build_append_nameseg(array
, s
);
126 build_append_nameseg(array
, segs
[1]);
129 build_append_byte(array
, 0x2F); /* MultiNamePrefix */
130 build_append_byte(array
, seg_count
);
132 /* handle the 1st segment manually due to prefix/root path */
133 build_append_nameseg(array
, s
);
135 /* add the rest of segments */
136 segs_iter
= segs
+ 1;
138 build_append_nameseg(array
, *segs_iter
);
147 static void build_append_namestring(GArray
*array
, const char *format
, ...)
151 va_start(ap
, format
);
152 build_append_namestringv(array
, format
, ap
);
156 /* 5.4 Definition Block Encoding */
158 PACKAGE_LENGTH_1BYTE_SHIFT
= 6, /* Up to 63 - use extra 2 bits. */
159 PACKAGE_LENGTH_2BYTE_SHIFT
= 4,
160 PACKAGE_LENGTH_3BYTE_SHIFT
= 12,
161 PACKAGE_LENGTH_4BYTE_SHIFT
= 20,
165 build_prepend_package_length(GArray
*package
, unsigned length
, bool incl_self
)
168 unsigned length_bytes
;
170 if (length
+ 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT
)) {
172 } else if (length
+ 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT
)) {
174 } else if (length
+ 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT
)) {
181 * NamedField uses PkgLength encoding but it doesn't include length
182 * of PkgLength itself.
186 * PkgLength is the length of the inclusive length of the data
187 * and PkgLength's length itself when used for terms with
190 length
+= length_bytes
;
193 switch (length_bytes
) {
196 build_prepend_byte(package
, byte
);
199 byte
= length
>> PACKAGE_LENGTH_4BYTE_SHIFT
;
200 build_prepend_byte(package
, byte
);
201 length
&= (1 << PACKAGE_LENGTH_4BYTE_SHIFT
) - 1;
204 byte
= length
>> PACKAGE_LENGTH_3BYTE_SHIFT
;
205 build_prepend_byte(package
, byte
);
206 length
&= (1 << PACKAGE_LENGTH_3BYTE_SHIFT
) - 1;
209 byte
= length
>> PACKAGE_LENGTH_2BYTE_SHIFT
;
210 build_prepend_byte(package
, byte
);
211 length
&= (1 << PACKAGE_LENGTH_2BYTE_SHIFT
) - 1;
215 * Most significant two bits of byte zero indicate how many following bytes
216 * are in PkgLength encoding.
218 byte
= ((length_bytes
- 1) << PACKAGE_LENGTH_1BYTE_SHIFT
) | length
;
219 build_prepend_byte(package
, byte
);
223 build_append_pkg_length(GArray
*array
, unsigned length
, bool incl_self
)
225 GArray
*tmp
= build_alloc_array();
227 build_prepend_package_length(tmp
, length
, incl_self
);
228 build_append_array(array
, tmp
);
229 build_free_array(tmp
);
232 static void build_package(GArray
*package
, uint8_t op
)
234 build_prepend_package_length(package
, package
->len
, true);
235 build_prepend_byte(package
, op
);
238 static void build_extop_package(GArray
*package
, uint8_t op
)
240 build_package(package
, op
);
241 build_prepend_byte(package
, 0x5B); /* ExtOpPrefix */
244 static void build_append_int_noprefix(GArray
*table
, uint64_t value
, int size
)
248 for (i
= 0; i
< size
; ++i
) {
249 build_append_byte(table
, value
& 0xFF);
254 static void build_append_int(GArray
*table
, uint64_t value
)
257 build_append_byte(table
, 0x00); /* ZeroOp */
258 } else if (value
== 0x01) {
259 build_append_byte(table
, 0x01); /* OneOp */
260 } else if (value
<= 0xFF) {
261 build_append_byte(table
, 0x0A); /* BytePrefix */
262 build_append_int_noprefix(table
, value
, 1);
263 } else if (value
<= 0xFFFF) {
264 build_append_byte(table
, 0x0B); /* WordPrefix */
265 build_append_int_noprefix(table
, value
, 2);
266 } else if (value
<= 0xFFFFFFFF) {
267 build_append_byte(table
, 0x0C); /* DWordPrefix */
268 build_append_int_noprefix(table
, value
, 4);
270 build_append_byte(table
, 0x0E); /* QWordPrefix */
271 build_append_int_noprefix(table
, value
, 8);
275 static GPtrArray
*alloc_list
;
277 static Aml
*aml_alloc(void)
279 Aml
*var
= g_new0(typeof(*var
), 1);
281 g_ptr_array_add(alloc_list
, var
);
282 var
->block_flags
= AML_NO_OPCODE
;
283 var
->buf
= build_alloc_array();
287 static Aml
*aml_opcode(uint8_t op
)
289 Aml
*var
= aml_alloc();
292 var
->block_flags
= AML_OPCODE
;
296 static Aml
*aml_bundle(uint8_t op
, AmlBlockFlags flags
)
298 Aml
*var
= aml_alloc();
301 var
->block_flags
= flags
;
305 static void aml_free(gpointer data
, gpointer user_data
)
308 build_free_array(var
->buf
);
311 Aml
*init_aml_allocator(void)
316 alloc_list
= g_ptr_array_new();
321 void free_aml_allocator(void)
323 g_ptr_array_foreach(alloc_list
, aml_free
, NULL
);
324 g_ptr_array_free(alloc_list
, true);
328 /* pack data with DefBuffer encoding */
329 static void build_buffer(GArray
*array
, uint8_t op
)
331 GArray
*data
= build_alloc_array();
333 build_append_int(data
, array
->len
);
334 g_array_prepend_vals(array
, data
->data
, data
->len
);
335 build_free_array(data
);
336 build_package(array
, op
);
339 void aml_append(Aml
*parent_ctx
, Aml
*child
)
341 GArray
*buf
= build_alloc_array();
342 build_append_array(buf
, child
->buf
);
344 switch (child
->block_flags
) {
346 build_append_byte(parent_ctx
->buf
, child
->op
);
348 case AML_EXT_PACKAGE
:
349 build_extop_package(buf
, child
->op
);
352 build_package(buf
, child
->op
);
354 case AML_RES_TEMPLATE
:
355 build_append_byte(buf
, 0x79); /* EndTag */
357 * checksum operations are treated as succeeded if checksum
358 * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
360 build_append_byte(buf
, 0);
361 /* fall through, to pack resources in buffer */
363 build_buffer(buf
, child
->op
);
371 build_append_array(parent_ctx
->buf
, buf
);
372 build_free_array(buf
);
375 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
376 Aml
*aml_scope(const char *name_format
, ...)
379 Aml
*var
= aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE
);
380 va_start(ap
, name_format
);
381 build_append_namestringv(var
->buf
, name_format
, ap
);
386 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
387 Aml
*aml_return(Aml
*val
)
389 Aml
*var
= aml_opcode(0xA4 /* ReturnOp */);
390 aml_append(var
, val
);
395 * ACPI 1.0b: 16.2.3 Data Objects Encoding:
396 * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
398 Aml
*aml_int(const uint64_t val
)
400 Aml
*var
= aml_alloc();
401 build_append_int(var
->buf
, val
);
406 * helper to construct NameString, which returns Aml object
407 * for using with aml_append or other aml_* terms
409 Aml
*aml_name(const char *name_format
, ...)
412 Aml
*var
= aml_alloc();
413 va_start(ap
, name_format
);
414 build_append_namestringv(var
->buf
, name_format
, ap
);
419 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
420 Aml
*aml_name_decl(const char *name
, Aml
*val
)
422 Aml
*var
= aml_opcode(0x08 /* NameOp */);
423 build_append_namestring(var
->buf
, "%s", name
);
424 aml_append(var
, val
);
428 /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
429 Aml
*aml_arg(int pos
)
432 uint8_t op
= 0x68 /* ARG0 op */ + pos
;
435 var
= aml_opcode(op
);
439 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
440 Aml
*aml_store(Aml
*val
, Aml
*target
)
442 Aml
*var
= aml_opcode(0x70 /* StoreOp */);
443 aml_append(var
, val
);
444 aml_append(var
, target
);
448 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
449 Aml
*aml_and(Aml
*arg1
, Aml
*arg2
)
451 Aml
*var
= aml_opcode(0x7B /* AndOp */);
452 aml_append(var
, arg1
);
453 aml_append(var
, arg2
);
454 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
458 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */
459 Aml
*aml_or(Aml
*arg1
, Aml
*arg2
)
461 Aml
*var
= aml_opcode(0x7D /* OrOp */);
462 aml_append(var
, arg1
);
463 aml_append(var
, arg2
);
464 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
468 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
469 Aml
*aml_notify(Aml
*arg1
, Aml
*arg2
)
471 Aml
*var
= aml_opcode(0x86 /* NotifyOp */);
472 aml_append(var
, arg1
);
473 aml_append(var
, arg2
);
477 /* helper to call method with 1 argument */
478 Aml
*aml_call1(const char *method
, Aml
*arg1
)
480 Aml
*var
= aml_alloc();
481 build_append_namestring(var
->buf
, "%s", method
);
482 aml_append(var
, arg1
);
486 /* helper to call method with 2 arguments */
487 Aml
*aml_call2(const char *method
, Aml
*arg1
, Aml
*arg2
)
489 Aml
*var
= aml_alloc();
490 build_append_namestring(var
->buf
, "%s", method
);
491 aml_append(var
, arg1
);
492 aml_append(var
, arg2
);
496 /* helper to call method with 3 arguments */
497 Aml
*aml_call3(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
)
499 Aml
*var
= aml_alloc();
500 build_append_namestring(var
->buf
, "%s", method
);
501 aml_append(var
, arg1
);
502 aml_append(var
, arg2
);
503 aml_append(var
, arg3
);
507 /* helper to call method with 4 arguments */
508 Aml
*aml_call4(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
, Aml
*arg4
)
510 Aml
*var
= aml_alloc();
511 build_append_namestring(var
->buf
, "%s", method
);
512 aml_append(var
, arg1
);
513 aml_append(var
, arg2
);
514 aml_append(var
, arg3
);
515 aml_append(var
, arg4
);
520 * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor
521 * (Type 1, Large Item Name 0x6)
523 Aml
*aml_memory32_fixed(uint32_t addr
, uint32_t size
,
524 AmlReadAndWrite read_and_write
)
526 Aml
*var
= aml_alloc();
527 build_append_byte(var
->buf
, 0x86); /* Memory32Fixed Resource Descriptor */
528 build_append_byte(var
->buf
, 9); /* Length, bits[7:0] value = 9 */
529 build_append_byte(var
->buf
, 0); /* Length, bits[15:8] value = 0 */
530 build_append_byte(var
->buf
, read_and_write
); /* Write status, 1 rw 0 ro */
532 /* Range base address */
533 build_append_byte(var
->buf
, extract32(addr
, 0, 8)); /* bits[7:0] */
534 build_append_byte(var
->buf
, extract32(addr
, 8, 8)); /* bits[15:8] */
535 build_append_byte(var
->buf
, extract32(addr
, 16, 8)); /* bits[23:16] */
536 build_append_byte(var
->buf
, extract32(addr
, 24, 8)); /* bits[31:24] */
539 build_append_byte(var
->buf
, extract32(size
, 0, 8)); /* bits[7:0] */
540 build_append_byte(var
->buf
, extract32(size
, 8, 8)); /* bits[15:8] */
541 build_append_byte(var
->buf
, extract32(size
, 16, 8)); /* bits[23:16] */
542 build_append_byte(var
->buf
, extract32(size
, 24, 8)); /* bits[31:24] */
547 * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor
548 * Type 1, Large Item Name 0x9
550 Aml
*aml_interrupt(AmlConsumerAndProducer con_and_pro
,
551 AmlLevelAndEdge level_and_edge
,
552 AmlActiveHighAndLow high_and_low
, AmlShared shared
,
555 Aml
*var
= aml_alloc();
556 uint8_t irq_flags
= con_and_pro
| (level_and_edge
<< 1)
557 | (high_and_low
<< 2) | (shared
<< 3);
559 build_append_byte(var
->buf
, 0x89); /* Extended irq descriptor */
560 build_append_byte(var
->buf
, 6); /* Length, bits[7:0] minimum value = 6 */
561 build_append_byte(var
->buf
, 0); /* Length, bits[15:8] minimum value = 0 */
562 build_append_byte(var
->buf
, irq_flags
); /* Interrupt Vector Information. */
563 build_append_byte(var
->buf
, 0x01); /* Interrupt table length = 1 */
565 /* Interrupt Number */
566 build_append_byte(var
->buf
, extract32(irq
, 0, 8)); /* bits[7:0] */
567 build_append_byte(var
->buf
, extract32(irq
, 8, 8)); /* bits[15:8] */
568 build_append_byte(var
->buf
, extract32(irq
, 16, 8)); /* bits[23:16] */
569 build_append_byte(var
->buf
, extract32(irq
, 24, 8)); /* bits[31:24] */
573 /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */
574 Aml
*aml_io(AmlIODecode dec
, uint16_t min_base
, uint16_t max_base
,
575 uint8_t aln
, uint8_t len
)
577 Aml
*var
= aml_alloc();
578 build_append_byte(var
->buf
, 0x47); /* IO port descriptor */
579 build_append_byte(var
->buf
, dec
);
580 build_append_byte(var
->buf
, min_base
& 0xff);
581 build_append_byte(var
->buf
, (min_base
>> 8) & 0xff);
582 build_append_byte(var
->buf
, max_base
& 0xff);
583 build_append_byte(var
->buf
, (max_base
>> 8) & 0xff);
584 build_append_byte(var
->buf
, aln
);
585 build_append_byte(var
->buf
, len
);
590 * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor
592 * More verbose description at:
593 * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro)
594 * 6.4.2.1 IRQ Descriptor
596 Aml
*aml_irq_no_flags(uint8_t irq
)
599 Aml
*var
= aml_alloc();
602 build_append_byte(var
->buf
, 0x22); /* IRQ descriptor 2 byte form */
604 irq_mask
= 1U << irq
;
605 build_append_byte(var
->buf
, irq_mask
& 0xFF); /* IRQ mask bits[7:0] */
606 build_append_byte(var
->buf
, irq_mask
>> 8); /* IRQ mask bits[15:8] */
610 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */
611 Aml
*aml_lnot(Aml
*arg
)
613 Aml
*var
= aml_opcode(0x92 /* LNotOp */);
614 aml_append(var
, arg
);
618 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */
619 Aml
*aml_equal(Aml
*arg1
, Aml
*arg2
)
621 Aml
*var
= aml_opcode(0x93 /* LequalOp */);
622 aml_append(var
, arg1
);
623 aml_append(var
, arg2
);
627 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
628 Aml
*aml_if(Aml
*predicate
)
630 Aml
*var
= aml_bundle(0xA0 /* IfOp */, AML_PACKAGE
);
631 aml_append(var
, predicate
);
635 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */
638 Aml
*var
= aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE
);
642 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
643 Aml
*aml_method(const char *name
, int arg_count
)
645 Aml
*var
= aml_bundle(0x14 /* MethodOp */, AML_PACKAGE
);
646 build_append_namestring(var
->buf
, "%s", name
);
647 build_append_byte(var
->buf
, arg_count
); /* MethodFlags: ArgCount */
651 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
652 Aml
*aml_device(const char *name_format
, ...)
655 Aml
*var
= aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE
);
656 va_start(ap
, name_format
);
657 build_append_namestringv(var
->buf
, name_format
, ap
);
662 /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */
663 Aml
*aml_resource_template(void)
665 /* ResourceTemplate is a buffer of Resources with EndTag at the end */
666 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE
);
670 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer
671 * Pass byte_list as NULL to request uninitialized buffer to reserve space.
673 Aml
*aml_buffer(int buffer_size
, uint8_t *byte_list
)
676 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_BUFFER
);
678 for (i
= 0; i
< buffer_size
; i
++) {
679 if (byte_list
== NULL
) {
680 build_append_byte(var
->buf
, 0x0);
682 build_append_byte(var
->buf
, byte_list
[i
]);
689 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */
690 Aml
*aml_package(uint8_t num_elements
)
692 Aml
*var
= aml_bundle(0x12 /* PackageOp */, AML_PACKAGE
);
693 build_append_byte(var
->buf
, num_elements
);
697 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */
698 Aml
*aml_operation_region(const char *name
, AmlRegionSpace rs
,
699 uint32_t offset
, uint32_t len
)
701 Aml
*var
= aml_alloc();
702 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
703 build_append_byte(var
->buf
, 0x80); /* OpRegionOp */
704 build_append_namestring(var
->buf
, "%s", name
);
705 build_append_byte(var
->buf
, rs
);
706 build_append_int(var
->buf
, offset
);
707 build_append_int(var
->buf
, len
);
711 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */
712 Aml
*aml_named_field(const char *name
, unsigned length
)
714 Aml
*var
= aml_alloc();
715 build_append_nameseg(var
->buf
, name
);
716 build_append_pkg_length(var
->buf
, length
, false);
720 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */
721 Aml
*aml_reserved_field(unsigned length
)
723 Aml
*var
= aml_alloc();
724 /* ReservedField := 0x00 PkgLength */
725 build_append_byte(var
->buf
, 0x00);
726 build_append_pkg_length(var
->buf
, length
, false);
730 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */
731 Aml
*aml_field(const char *name
, AmlAccessType type
, AmlUpdateRule rule
)
733 Aml
*var
= aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE
);
734 uint8_t flags
= rule
<< 5 | type
;
736 build_append_namestring(var
->buf
, "%s", name
);
737 build_append_byte(var
->buf
, flags
);
741 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */
742 Aml
*aml_create_dword_field(Aml
*srcbuf
, Aml
*index
, const char *name
)
744 Aml
*var
= aml_alloc();
745 build_append_byte(var
->buf
, 0x8A); /* CreateDWordFieldOp */
746 aml_append(var
, srcbuf
);
747 aml_append(var
, index
);
748 build_append_namestring(var
->buf
, "%s", name
);
752 /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */
753 Aml
*aml_string(const char *name_format
, ...)
755 Aml
*var
= aml_opcode(0x0D /* StringPrefix */);
760 va_start(ap
, name_format
);
762 len
= vsnprintf(NULL
, 0, name_format
, va_len
);
765 s
= g_new0(typeof(*s
), len
);
767 len
= vsnprintf(s
, len
, name_format
, ap
);
770 g_array_append_vals(var
->buf
, s
, len
);
771 build_append_byte(var
->buf
, 0x0); /* NullChar */
777 /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */
778 Aml
*aml_local(int num
)
781 uint8_t op
= 0x60 /* Local0Op */ + num
;
784 var
= aml_opcode(op
);
788 /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */
789 Aml
*aml_varpackage(uint32_t num_elements
)
791 Aml
*var
= aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE
);
792 build_append_int(var
->buf
, num_elements
);
796 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */
797 Aml
*aml_processor(uint8_t proc_id
, uint32_t pblk_addr
, uint8_t pblk_len
,
798 const char *name_format
, ...)
801 Aml
*var
= aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE
);
802 va_start(ap
, name_format
);
803 build_append_namestringv(var
->buf
, name_format
, ap
);
805 build_append_byte(var
->buf
, proc_id
); /* ProcID */
806 build_append_int_noprefix(var
->buf
, pblk_addr
, sizeof(pblk_addr
));
807 build_append_byte(var
->buf
, pblk_len
); /* PblkLen */
811 static uint8_t Hex2Digit(char c
)
820 /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */
821 Aml
*aml_eisaid(const char *str
)
823 Aml
*var
= aml_alloc();
826 g_assert(strlen(str
) == 7);
827 id
= (str
[0] - 0x40) << 26 |
828 (str
[1] - 0x40) << 21 |
829 (str
[2] - 0x40) << 16 |
830 Hex2Digit(str
[3]) << 12 |
831 Hex2Digit(str
[4]) << 8 |
832 Hex2Digit(str
[5]) << 4 |
835 build_append_byte(var
->buf
, 0x0C); /* DWordPrefix */
836 build_append_int_noprefix(var
->buf
, bswap32(id
), sizeof(id
));
840 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */
841 static Aml
*aml_as_desc_header(AmlResourceType type
, AmlMinFixed min_fixed
,
842 AmlMaxFixed max_fixed
, AmlDecode dec
,
845 uint8_t flags
= max_fixed
| min_fixed
| dec
;
846 Aml
*var
= aml_alloc();
848 build_append_byte(var
->buf
, type
);
849 build_append_byte(var
->buf
, flags
);
850 build_append_byte(var
->buf
, type_flags
); /* Type Specific Flags */
854 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */
855 static Aml
*aml_word_as_desc(AmlResourceType type
, AmlMinFixed min_fixed
,
856 AmlMaxFixed max_fixed
, AmlDecode dec
,
857 uint16_t addr_gran
, uint16_t addr_min
,
858 uint16_t addr_max
, uint16_t addr_trans
,
859 uint16_t len
, uint8_t type_flags
)
861 Aml
*var
= aml_alloc();
863 build_append_byte(var
->buf
, 0x88); /* Word Address Space Descriptor */
864 /* minimum length since we do not encode optional fields */
865 build_append_byte(var
->buf
, 0x0D);
866 build_append_byte(var
->buf
, 0x0);
869 aml_as_desc_header(type
, min_fixed
, max_fixed
, dec
, type_flags
));
870 build_append_int_noprefix(var
->buf
, addr_gran
, sizeof(addr_gran
));
871 build_append_int_noprefix(var
->buf
, addr_min
, sizeof(addr_min
));
872 build_append_int_noprefix(var
->buf
, addr_max
, sizeof(addr_max
));
873 build_append_int_noprefix(var
->buf
, addr_trans
, sizeof(addr_trans
));
874 build_append_int_noprefix(var
->buf
, len
, sizeof(len
));
878 /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */
879 static Aml
*aml_dword_as_desc(AmlResourceType type
, AmlMinFixed min_fixed
,
880 AmlMaxFixed max_fixed
, AmlDecode dec
,
881 uint32_t addr_gran
, uint32_t addr_min
,
882 uint32_t addr_max
, uint32_t addr_trans
,
883 uint32_t len
, uint8_t type_flags
)
885 Aml
*var
= aml_alloc();
887 build_append_byte(var
->buf
, 0x87); /* DWord Address Space Descriptor */
888 /* minimum length since we do not encode optional fields */
889 build_append_byte(var
->buf
, 23);
890 build_append_byte(var
->buf
, 0x0);
894 aml_as_desc_header(type
, min_fixed
, max_fixed
, dec
, type_flags
));
895 build_append_int_noprefix(var
->buf
, addr_gran
, sizeof(addr_gran
));
896 build_append_int_noprefix(var
->buf
, addr_min
, sizeof(addr_min
));
897 build_append_int_noprefix(var
->buf
, addr_max
, sizeof(addr_max
));
898 build_append_int_noprefix(var
->buf
, addr_trans
, sizeof(addr_trans
));
899 build_append_int_noprefix(var
->buf
, len
, sizeof(len
));
903 /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */
904 static Aml
*aml_qword_as_desc(AmlResourceType type
, AmlMinFixed min_fixed
,
905 AmlMaxFixed max_fixed
, AmlDecode dec
,
906 uint64_t addr_gran
, uint64_t addr_min
,
907 uint64_t addr_max
, uint64_t addr_trans
,
908 uint64_t len
, uint8_t type_flags
)
910 Aml
*var
= aml_alloc();
912 build_append_byte(var
->buf
, 0x8A); /* QWord Address Space Descriptor */
913 /* minimum length since we do not encode optional fields */
914 build_append_byte(var
->buf
, 0x2B);
915 build_append_byte(var
->buf
, 0x0);
918 aml_as_desc_header(type
, min_fixed
, max_fixed
, dec
, type_flags
));
919 build_append_int_noprefix(var
->buf
, addr_gran
, sizeof(addr_gran
));
920 build_append_int_noprefix(var
->buf
, addr_min
, sizeof(addr_min
));
921 build_append_int_noprefix(var
->buf
, addr_max
, sizeof(addr_max
));
922 build_append_int_noprefix(var
->buf
, addr_trans
, sizeof(addr_trans
));
923 build_append_int_noprefix(var
->buf
, len
, sizeof(len
));
928 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
930 * More verbose description at:
931 * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro)
933 Aml
*aml_word_bus_number(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
934 AmlDecode dec
, uint16_t addr_gran
,
935 uint16_t addr_min
, uint16_t addr_max
,
936 uint16_t addr_trans
, uint16_t len
)
939 return aml_word_as_desc(AML_BUS_NUMBER_RANGE
, min_fixed
, max_fixed
, dec
,
940 addr_gran
, addr_min
, addr_max
, addr_trans
, len
, 0);
944 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
946 * More verbose description at:
947 * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro)
949 Aml
*aml_word_io(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
950 AmlDecode dec
, AmlISARanges isa_ranges
,
951 uint16_t addr_gran
, uint16_t addr_min
,
952 uint16_t addr_max
, uint16_t addr_trans
,
956 return aml_word_as_desc(AML_IO_RANGE
, min_fixed
, max_fixed
, dec
,
957 addr_gran
, addr_min
, addr_max
, addr_trans
, len
,
962 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor
964 * More verbose description at:
965 * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro)
967 Aml
*aml_dword_io(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
968 AmlDecode dec
, AmlISARanges isa_ranges
,
969 uint32_t addr_gran
, uint32_t addr_min
,
970 uint32_t addr_max
, uint32_t addr_trans
,
974 return aml_dword_as_desc(AML_IO_RANGE
, min_fixed
, max_fixed
, dec
,
975 addr_gran
, addr_min
, addr_max
, addr_trans
, len
,
980 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor
982 * More verbose description at:
983 * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro)
985 Aml
*aml_dword_memory(AmlDecode dec
, AmlMinFixed min_fixed
,
986 AmlMaxFixed max_fixed
, AmlCacheable cacheable
,
987 AmlReadAndWrite read_and_write
,
988 uint32_t addr_gran
, uint32_t addr_min
,
989 uint32_t addr_max
, uint32_t addr_trans
,
992 uint8_t flags
= read_and_write
| (cacheable
<< 1);
994 return aml_dword_as_desc(AML_MEMORY_RANGE
, min_fixed
, max_fixed
,
995 dec
, addr_gran
, addr_min
, addr_max
,
996 addr_trans
, len
, flags
);
1000 * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor
1002 * More verbose description at:
1003 * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro)
1005 Aml
*aml_qword_memory(AmlDecode dec
, AmlMinFixed min_fixed
,
1006 AmlMaxFixed max_fixed
, AmlCacheable cacheable
,
1007 AmlReadAndWrite read_and_write
,
1008 uint64_t addr_gran
, uint64_t addr_min
,
1009 uint64_t addr_max
, uint64_t addr_trans
,
1012 uint8_t flags
= read_and_write
| (cacheable
<< 1);
1014 return aml_qword_as_desc(AML_MEMORY_RANGE
, min_fixed
, max_fixed
,
1015 dec
, addr_gran
, addr_min
, addr_max
,
1016 addr_trans
, len
, flags
);
1019 static uint8_t Hex2Byte(const char *src
)
1023 hi
= Hex2Digit(src
[0]);
1027 lo
= Hex2Digit(src
[1]);
1030 return (hi
<< 4) | lo
;
1034 * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro)
1035 * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1036 * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp");
1038 Aml
*aml_touuid(const char *uuid
)
1040 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_BUFFER
);
1042 assert(strlen(uuid
) == 36);
1043 assert(uuid
[8] == '-');
1044 assert(uuid
[13] == '-');
1045 assert(uuid
[18] == '-');
1046 assert(uuid
[23] == '-');
1048 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 6)); /* dd - at offset 00 */
1049 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 4)); /* cc - at offset 01 */
1050 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 2)); /* bb - at offset 02 */
1051 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 0)); /* aa - at offset 03 */
1053 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 11)); /* ff - at offset 04 */
1054 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 9)); /* ee - at offset 05 */
1056 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 16)); /* hh - at offset 06 */
1057 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 14)); /* gg - at offset 07 */
1059 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 19)); /* ii - at offset 08 */
1060 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 21)); /* jj - at offset 09 */
1062 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 24)); /* kk - at offset 10 */
1063 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 26)); /* ll - at offset 11 */
1064 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 28)); /* mm - at offset 12 */
1065 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 30)); /* nn - at offset 13 */
1066 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 32)); /* oo - at offset 14 */
1067 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 34)); /* pp - at offset 15 */
1073 * ACPI 2.0b: 16.2.3.6.4.3 Unicode Macro (Convert Ascii String To Unicode)
1075 Aml
*aml_unicode(const char *str
)
1078 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_BUFFER
);
1081 build_append_byte(var
->buf
, str
[i
]);
1082 build_append_byte(var
->buf
, 0);
1084 } while (i
<= strlen(str
));
1090 build_header(GArray
*linker
, GArray
*table_data
,
1091 AcpiTableHeader
*h
, const char *sig
, int len
, uint8_t rev
)
1093 memcpy(&h
->signature
, sig
, 4);
1094 h
->length
= cpu_to_le32(len
);
1096 memcpy(h
->oem_id
, ACPI_BUILD_APPNAME6
, 6);
1097 memcpy(h
->oem_table_id
, ACPI_BUILD_APPNAME4
, 4);
1098 memcpy(h
->oem_table_id
+ 4, sig
, 4);
1099 h
->oem_revision
= cpu_to_le32(1);
1100 memcpy(h
->asl_compiler_id
, ACPI_BUILD_APPNAME4
, 4);
1101 h
->asl_compiler_revision
= cpu_to_le32(1);
1103 /* Checksum to be filled in by Guest linker */
1104 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_TABLE_FILE
,
1105 table_data
->data
, h
, len
, &h
->checksum
);
1108 void *acpi_data_push(GArray
*table_data
, unsigned size
)
1110 unsigned off
= table_data
->len
;
1111 g_array_set_size(table_data
, off
+ size
);
1112 return table_data
->data
+ off
;
1115 unsigned acpi_data_len(GArray
*table
)
1117 #if GLIB_CHECK_VERSION(2, 22, 0)
1118 assert(g_array_get_element_size(table
) == 1);
1123 void acpi_add_table(GArray
*table_offsets
, GArray
*table_data
)
1125 uint32_t offset
= cpu_to_le32(table_data
->len
);
1126 g_array_append_val(table_offsets
, offset
);
1129 void acpi_build_tables_init(AcpiBuildTables
*tables
)
1131 tables
->rsdp
= g_array_new(false, true /* clear */, 1);
1132 tables
->table_data
= g_array_new(false, true /* clear */, 1);
1133 tables
->tcpalog
= g_array_new(false, true /* clear */, 1);
1134 tables
->linker
= bios_linker_loader_init();
1137 void acpi_build_tables_cleanup(AcpiBuildTables
*tables
, bool mfre
)
1139 void *linker_data
= bios_linker_loader_cleanup(tables
->linker
);
1140 g_free(linker_data
);
1141 g_array_free(tables
->rsdp
, true);
1142 g_array_free(tables
->table_data
, true);
1143 g_array_free(tables
->tcpalog
, mfre
);
1146 /* Build rsdt table */
1148 build_rsdt(GArray
*table_data
, GArray
*linker
, GArray
*table_offsets
)
1150 AcpiRsdtDescriptorRev1
*rsdt
;
1153 const int table_data_len
= (sizeof(uint32_t) * table_offsets
->len
);
1155 rsdt_len
= sizeof(*rsdt
) + table_data_len
;
1156 rsdt
= acpi_data_push(table_data
, rsdt_len
);
1157 memcpy(rsdt
->table_offset_entry
, table_offsets
->data
, table_data_len
);
1158 for (i
= 0; i
< table_offsets
->len
; ++i
) {
1159 /* rsdt->table_offset_entry to be filled by Guest linker */
1160 bios_linker_loader_add_pointer(linker
,
1161 ACPI_BUILD_TABLE_FILE
,
1162 ACPI_BUILD_TABLE_FILE
,
1163 table_data
, &rsdt
->table_offset_entry
[i
],
1166 build_header(linker
, table_data
,
1167 (void *)rsdt
, "RSDT", rsdt_len
, 1);