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"
28 #include "hw/boards.h"
29 #include "hw/acpi/tpm.h"
30 #include "hw/pci/pci_host.h"
31 #include "hw/pci/pci_bus.h"
32 #include "hw/pci/pci_bridge.h"
33 #include "qemu/cutils.h"
35 static GArray
*build_alloc_array(void)
37 return g_array_new(false, true /* clear */, 1);
40 static void build_free_array(GArray
*array
)
42 g_array_free(array
, true);
45 static void build_prepend_byte(GArray
*array
, uint8_t val
)
47 g_array_prepend_val(array
, val
);
50 static void build_append_byte(GArray
*array
, uint8_t val
)
52 g_array_append_val(array
, val
);
55 static void build_append_array(GArray
*array
, GArray
*val
)
57 g_array_append_vals(array
, val
->data
, val
->len
);
60 #define ACPI_NAMESEG_LEN 4
62 void crs_range_insert(GPtrArray
*ranges
, uint64_t base
, uint64_t limit
)
66 entry
= g_malloc(sizeof(*entry
));
70 g_ptr_array_add(ranges
, entry
);
73 static void crs_range_free(gpointer data
)
75 CrsRangeEntry
*entry
= (CrsRangeEntry
*)data
;
79 void crs_range_set_init(CrsRangeSet
*range_set
)
81 range_set
->io_ranges
= g_ptr_array_new_with_free_func(crs_range_free
);
82 range_set
->mem_ranges
= g_ptr_array_new_with_free_func(crs_range_free
);
83 range_set
->mem_64bit_ranges
=
84 g_ptr_array_new_with_free_func(crs_range_free
);
87 void crs_range_set_free(CrsRangeSet
*range_set
)
89 g_ptr_array_free(range_set
->io_ranges
, true);
90 g_ptr_array_free(range_set
->mem_ranges
, true);
91 g_ptr_array_free(range_set
->mem_64bit_ranges
, true);
94 static gint
crs_range_compare(gconstpointer a
, gconstpointer b
)
96 CrsRangeEntry
*entry_a
= *(CrsRangeEntry
**)a
;
97 CrsRangeEntry
*entry_b
= *(CrsRangeEntry
**)b
;
99 if (entry_a
->base
< entry_b
->base
) {
101 } else if (entry_a
->base
> entry_b
->base
) {
109 * crs_replace_with_free_ranges - given the 'used' ranges within [start - end]
110 * interval, computes the 'free' ranges from the same interval.
111 * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function
112 * will return { [base - a1], [a2 - b1], [b2 - limit] }.
114 void crs_replace_with_free_ranges(GPtrArray
*ranges
,
115 uint64_t start
, uint64_t end
)
117 GPtrArray
*free_ranges
= g_ptr_array_new();
118 uint64_t free_base
= start
;
121 g_ptr_array_sort(ranges
, crs_range_compare
);
122 for (i
= 0; i
< ranges
->len
; i
++) {
123 CrsRangeEntry
*used
= g_ptr_array_index(ranges
, i
);
125 if (free_base
< used
->base
) {
126 crs_range_insert(free_ranges
, free_base
, used
->base
- 1);
129 free_base
= used
->limit
+ 1;
132 if (free_base
< end
) {
133 crs_range_insert(free_ranges
, free_base
, end
);
136 g_ptr_array_set_size(ranges
, 0);
137 for (i
= 0; i
< free_ranges
->len
; i
++) {
138 g_ptr_array_add(ranges
, g_ptr_array_index(free_ranges
, i
));
141 g_ptr_array_free(free_ranges
, true);
145 * crs_range_merge - merges adjacent ranges in the given array.
146 * Array elements are deleted and replaced with the merged ranges.
148 static void crs_range_merge(GPtrArray
*range
)
150 GPtrArray
*tmp
= g_ptr_array_new_with_free_func(crs_range_free
);
151 CrsRangeEntry
*entry
;
152 uint64_t range_base
, range_limit
;
159 g_ptr_array_sort(range
, crs_range_compare
);
161 entry
= g_ptr_array_index(range
, 0);
162 range_base
= entry
->base
;
163 range_limit
= entry
->limit
;
164 for (i
= 1; i
< range
->len
; i
++) {
165 entry
= g_ptr_array_index(range
, i
);
166 if (entry
->base
- 1 == range_limit
) {
167 range_limit
= entry
->limit
;
169 crs_range_insert(tmp
, range_base
, range_limit
);
170 range_base
= entry
->base
;
171 range_limit
= entry
->limit
;
174 crs_range_insert(tmp
, range_base
, range_limit
);
176 g_ptr_array_set_size(range
, 0);
177 for (i
= 0; i
< tmp
->len
; i
++) {
178 entry
= g_ptr_array_index(tmp
, i
);
179 crs_range_insert(range
, entry
->base
, entry
->limit
);
181 g_ptr_array_free(tmp
, true);
185 build_append_nameseg(GArray
*array
, const char *seg
)
190 assert(len
<= ACPI_NAMESEG_LEN
);
192 g_array_append_vals(array
, seg
, len
);
193 /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */
194 g_array_append_vals(array
, "____", ACPI_NAMESEG_LEN
- len
);
197 static void GCC_FMT_ATTR(2, 0)
198 build_append_namestringv(GArray
*array
, const char *format
, va_list ap
)
205 s
= g_strdup_vprintf(format
, ap
);
206 segs
= g_strsplit(s
, ".", 0);
216 * ACPI 5.0 spec: 20.2.2 Name Objects Encoding:
217 * "SegCount can be from 1 to 255"
219 assert(seg_count
> 0 && seg_count
<= 255);
221 /* handle RootPath || PrefixPath */
223 while (*s
== '\\' || *s
== '^') {
224 build_append_byte(array
, *s
);
231 build_append_byte(array
, 0x00); /* NullName */
233 build_append_nameseg(array
, s
);
238 build_append_byte(array
, 0x2E); /* DualNamePrefix */
239 build_append_nameseg(array
, s
);
240 build_append_nameseg(array
, segs
[1]);
243 build_append_byte(array
, 0x2F); /* MultiNamePrefix */
244 build_append_byte(array
, seg_count
);
246 /* handle the 1st segment manually due to prefix/root path */
247 build_append_nameseg(array
, s
);
249 /* add the rest of segments */
250 segs_iter
= segs
+ 1;
252 build_append_nameseg(array
, *segs_iter
);
261 static void build_append_namestring(GArray
*array
, const char *format
, ...)
265 va_start(ap
, format
);
266 build_append_namestringv(array
, format
, ap
);
270 /* 5.4 Definition Block Encoding */
272 PACKAGE_LENGTH_1BYTE_SHIFT
= 6, /* Up to 63 - use extra 2 bits. */
273 PACKAGE_LENGTH_2BYTE_SHIFT
= 4,
274 PACKAGE_LENGTH_3BYTE_SHIFT
= 12,
275 PACKAGE_LENGTH_4BYTE_SHIFT
= 20,
279 build_prepend_package_length(GArray
*package
, unsigned length
, bool incl_self
)
282 unsigned length_bytes
;
284 if (length
+ 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT
)) {
286 } else if (length
+ 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT
)) {
288 } else if (length
+ 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT
)) {
295 * NamedField uses PkgLength encoding but it doesn't include length
296 * of PkgLength itself.
300 * PkgLength is the length of the inclusive length of the data
301 * and PkgLength's length itself when used for terms with
304 length
+= length_bytes
;
307 switch (length_bytes
) {
310 build_prepend_byte(package
, byte
);
313 byte
= length
>> PACKAGE_LENGTH_4BYTE_SHIFT
;
314 build_prepend_byte(package
, byte
);
315 length
&= (1 << PACKAGE_LENGTH_4BYTE_SHIFT
) - 1;
318 byte
= length
>> PACKAGE_LENGTH_3BYTE_SHIFT
;
319 build_prepend_byte(package
, byte
);
320 length
&= (1 << PACKAGE_LENGTH_3BYTE_SHIFT
) - 1;
323 byte
= length
>> PACKAGE_LENGTH_2BYTE_SHIFT
;
324 build_prepend_byte(package
, byte
);
325 length
&= (1 << PACKAGE_LENGTH_2BYTE_SHIFT
) - 1;
329 * Most significant two bits of byte zero indicate how many following bytes
330 * are in PkgLength encoding.
332 byte
= ((length_bytes
- 1) << PACKAGE_LENGTH_1BYTE_SHIFT
) | length
;
333 build_prepend_byte(package
, byte
);
337 build_append_pkg_length(GArray
*array
, unsigned length
, bool incl_self
)
339 GArray
*tmp
= build_alloc_array();
341 build_prepend_package_length(tmp
, length
, incl_self
);
342 build_append_array(array
, tmp
);
343 build_free_array(tmp
);
346 static void build_package(GArray
*package
, uint8_t op
)
348 build_prepend_package_length(package
, package
->len
, true);
349 build_prepend_byte(package
, op
);
352 static void build_extop_package(GArray
*package
, uint8_t op
)
354 build_package(package
, op
);
355 build_prepend_byte(package
, 0x5B); /* ExtOpPrefix */
358 void build_append_int_noprefix(GArray
*table
, uint64_t value
, int size
)
362 for (i
= 0; i
< size
; ++i
) {
363 build_append_byte(table
, value
& 0xFF);
368 static void build_append_int(GArray
*table
, uint64_t value
)
371 build_append_byte(table
, 0x00); /* ZeroOp */
372 } else if (value
== 0x01) {
373 build_append_byte(table
, 0x01); /* OneOp */
374 } else if (value
<= 0xFF) {
375 build_append_byte(table
, 0x0A); /* BytePrefix */
376 build_append_int_noprefix(table
, value
, 1);
377 } else if (value
<= 0xFFFF) {
378 build_append_byte(table
, 0x0B); /* WordPrefix */
379 build_append_int_noprefix(table
, value
, 2);
380 } else if (value
<= 0xFFFFFFFF) {
381 build_append_byte(table
, 0x0C); /* DWordPrefix */
382 build_append_int_noprefix(table
, value
, 4);
384 build_append_byte(table
, 0x0E); /* QWordPrefix */
385 build_append_int_noprefix(table
, value
, 8);
389 /* Generic Address Structure (GAS)
390 * ACPI 2.0/3.0: 5.2.3.1 Generic Address Structure
392 * @access_width must be 0, see ACPI 2.0:Table 5-1
394 void build_append_gas(GArray
*table
, AmlAddressSpace as
,
395 uint8_t bit_width
, uint8_t bit_offset
,
396 uint8_t access_width
, uint64_t address
)
398 build_append_int_noprefix(table
, as
, 1);
399 build_append_int_noprefix(table
, bit_width
, 1);
400 build_append_int_noprefix(table
, bit_offset
, 1);
401 build_append_int_noprefix(table
, access_width
, 1);
402 build_append_int_noprefix(table
, address
, 8);
406 * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword,
407 * and return the offset to 0x00000000 for runtime patching.
409 * Warning: runtime patching is best avoided. Only use this as
410 * a replacement for DataTableRegion (for guests that don't
414 build_append_named_dword(GArray
*array
, const char *name_format
, ...)
419 build_append_byte(array
, 0x08); /* NameOp */
420 va_start(ap
, name_format
);
421 build_append_namestringv(array
, name_format
, ap
);
424 build_append_byte(array
, 0x0C); /* DWordPrefix */
427 build_append_int_noprefix(array
, 0x00000000, 4);
428 assert(array
->len
== offset
+ 4);
433 static GPtrArray
*alloc_list
;
435 static Aml
*aml_alloc(void)
437 Aml
*var
= g_new0(typeof(*var
), 1);
439 g_ptr_array_add(alloc_list
, var
);
440 var
->block_flags
= AML_NO_OPCODE
;
441 var
->buf
= build_alloc_array();
445 static Aml
*aml_opcode(uint8_t op
)
447 Aml
*var
= aml_alloc();
450 var
->block_flags
= AML_OPCODE
;
454 static Aml
*aml_bundle(uint8_t op
, AmlBlockFlags flags
)
456 Aml
*var
= aml_alloc();
459 var
->block_flags
= flags
;
463 static void aml_free(gpointer data
, gpointer user_data
)
466 build_free_array(var
->buf
);
470 Aml
*init_aml_allocator(void)
473 alloc_list
= g_ptr_array_new();
477 void free_aml_allocator(void)
479 g_ptr_array_foreach(alloc_list
, aml_free
, NULL
);
480 g_ptr_array_free(alloc_list
, true);
484 /* pack data with DefBuffer encoding */
485 static void build_buffer(GArray
*array
, uint8_t op
)
487 GArray
*data
= build_alloc_array();
489 build_append_int(data
, array
->len
);
490 g_array_prepend_vals(array
, data
->data
, data
->len
);
491 build_free_array(data
);
492 build_package(array
, op
);
495 void aml_append(Aml
*parent_ctx
, Aml
*child
)
497 GArray
*buf
= build_alloc_array();
498 build_append_array(buf
, child
->buf
);
500 switch (child
->block_flags
) {
502 build_append_byte(parent_ctx
->buf
, child
->op
);
504 case AML_EXT_PACKAGE
:
505 build_extop_package(buf
, child
->op
);
508 build_package(buf
, child
->op
);
510 case AML_RES_TEMPLATE
:
511 build_append_byte(buf
, 0x79); /* EndTag */
513 * checksum operations are treated as succeeded if checksum
514 * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
516 build_append_byte(buf
, 0);
517 /* fall through, to pack resources in buffer */
519 build_buffer(buf
, child
->op
);
527 build_append_array(parent_ctx
->buf
, buf
);
528 build_free_array(buf
);
531 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
532 Aml
*aml_scope(const char *name_format
, ...)
535 Aml
*var
= aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE
);
536 va_start(ap
, name_format
);
537 build_append_namestringv(var
->buf
, name_format
, ap
);
542 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
543 Aml
*aml_return(Aml
*val
)
545 Aml
*var
= aml_opcode(0xA4 /* ReturnOp */);
546 aml_append(var
, val
);
550 /* ACPI 1.0b: 16.2.6.3 Debug Objects Encoding: DebugObj */
553 Aml
*var
= aml_alloc();
554 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
555 build_append_byte(var
->buf
, 0x31); /* DebugOp */
560 * ACPI 1.0b: 16.2.3 Data Objects Encoding:
561 * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
563 Aml
*aml_int(const uint64_t val
)
565 Aml
*var
= aml_alloc();
566 build_append_int(var
->buf
, val
);
571 * helper to construct NameString, which returns Aml object
572 * for using with aml_append or other aml_* terms
574 Aml
*aml_name(const char *name_format
, ...)
577 Aml
*var
= aml_alloc();
578 va_start(ap
, name_format
);
579 build_append_namestringv(var
->buf
, name_format
, ap
);
584 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
585 Aml
*aml_name_decl(const char *name
, Aml
*val
)
587 Aml
*var
= aml_opcode(0x08 /* NameOp */);
588 build_append_namestring(var
->buf
, "%s", name
);
589 aml_append(var
, val
);
593 /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
594 Aml
*aml_arg(int pos
)
596 uint8_t op
= 0x68 /* ARG0 op */ + pos
;
599 return aml_opcode(op
);
602 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToInteger */
603 Aml
*aml_to_integer(Aml
*arg
)
605 Aml
*var
= aml_opcode(0x99 /* ToIntegerOp */);
606 aml_append(var
, arg
);
607 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
611 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToHexString */
612 Aml
*aml_to_hexstring(Aml
*src
, Aml
*dst
)
614 Aml
*var
= aml_opcode(0x98 /* ToHexStringOp */);
615 aml_append(var
, src
);
617 aml_append(var
, dst
);
619 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
624 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToBuffer */
625 Aml
*aml_to_buffer(Aml
*src
, Aml
*dst
)
627 Aml
*var
= aml_opcode(0x96 /* ToBufferOp */);
628 aml_append(var
, src
);
630 aml_append(var
, dst
);
632 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
637 /* ACPI 2.0a: 17.2.4.4 Type 2 Opcodes Encoding: DefToDecimalString */
638 Aml
*aml_to_decimalstring(Aml
*src
, Aml
*dst
)
640 Aml
*var
= aml_opcode(0x97 /* ToDecimalStringOp */);
641 aml_append(var
, src
);
643 aml_append(var
, dst
);
645 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
650 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
651 Aml
*aml_store(Aml
*val
, Aml
*target
)
653 Aml
*var
= aml_opcode(0x70 /* StoreOp */);
654 aml_append(var
, val
);
655 aml_append(var
, target
);
660 * build_opcode_2arg_dst:
664 * @dst: optional target to store to, set to NULL if it's not required
666 * An internal helper to compose AML terms that have
667 * "Op Operand Operand Target"
670 * Returns: The newly allocated and composed according to patter Aml object.
673 build_opcode_2arg_dst(uint8_t op
, Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
675 Aml
*var
= aml_opcode(op
);
676 aml_append(var
, arg1
);
677 aml_append(var
, arg2
);
679 aml_append(var
, dst
);
681 build_append_byte(var
->buf
, 0x00 /* NullNameOp */);
686 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
687 Aml
*aml_and(Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
689 return build_opcode_2arg_dst(0x7B /* AndOp */, arg1
, arg2
, dst
);
692 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */
693 Aml
*aml_or(Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
695 return build_opcode_2arg_dst(0x7D /* OrOp */, arg1
, arg2
, dst
);
698 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLAnd */
699 Aml
*aml_land(Aml
*arg1
, Aml
*arg2
)
701 Aml
*var
= aml_opcode(0x90 /* LAndOp */);
702 aml_append(var
, arg1
);
703 aml_append(var
, arg2
);
707 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLOr */
708 Aml
*aml_lor(Aml
*arg1
, Aml
*arg2
)
710 Aml
*var
= aml_opcode(0x91 /* LOrOp */);
711 aml_append(var
, arg1
);
712 aml_append(var
, arg2
);
716 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftLeft */
717 Aml
*aml_shiftleft(Aml
*arg1
, Aml
*count
)
719 return build_opcode_2arg_dst(0x79 /* ShiftLeftOp */, arg1
, count
, NULL
);
722 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftRight */
723 Aml
*aml_shiftright(Aml
*arg1
, Aml
*count
, Aml
*dst
)
725 return build_opcode_2arg_dst(0x7A /* ShiftRightOp */, arg1
, count
, dst
);
728 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLess */
729 Aml
*aml_lless(Aml
*arg1
, Aml
*arg2
)
731 Aml
*var
= aml_opcode(0x95 /* LLessOp */);
732 aml_append(var
, arg1
);
733 aml_append(var
, arg2
);
737 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
738 Aml
*aml_add(Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
740 return build_opcode_2arg_dst(0x72 /* AddOp */, arg1
, arg2
, dst
);
743 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSubtract */
744 Aml
*aml_subtract(Aml
*arg1
, Aml
*arg2
, Aml
*dst
)
746 return build_opcode_2arg_dst(0x74 /* SubtractOp */, arg1
, arg2
, dst
);
749 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIncrement */
750 Aml
*aml_increment(Aml
*arg
)
752 Aml
*var
= aml_opcode(0x75 /* IncrementOp */);
753 aml_append(var
, arg
);
757 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDecrement */
758 Aml
*aml_decrement(Aml
*arg
)
760 Aml
*var
= aml_opcode(0x76 /* DecrementOp */);
761 aml_append(var
, arg
);
765 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIndex */
766 Aml
*aml_index(Aml
*arg1
, Aml
*idx
)
768 return build_opcode_2arg_dst(0x88 /* IndexOp */, arg1
, idx
, NULL
);
771 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
772 Aml
*aml_notify(Aml
*arg1
, Aml
*arg2
)
774 Aml
*var
= aml_opcode(0x86 /* NotifyOp */);
775 aml_append(var
, arg1
);
776 aml_append(var
, arg2
);
780 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefBreak */
783 Aml
*var
= aml_opcode(0xa5 /* BreakOp */);
787 /* helper to call method without argument */
788 Aml
*aml_call0(const char *method
)
790 Aml
*var
= aml_alloc();
791 build_append_namestring(var
->buf
, "%s", method
);
795 /* helper to call method with 1 argument */
796 Aml
*aml_call1(const char *method
, Aml
*arg1
)
798 Aml
*var
= aml_alloc();
799 build_append_namestring(var
->buf
, "%s", method
);
800 aml_append(var
, arg1
);
804 /* helper to call method with 2 arguments */
805 Aml
*aml_call2(const char *method
, Aml
*arg1
, Aml
*arg2
)
807 Aml
*var
= aml_alloc();
808 build_append_namestring(var
->buf
, "%s", method
);
809 aml_append(var
, arg1
);
810 aml_append(var
, arg2
);
814 /* helper to call method with 3 arguments */
815 Aml
*aml_call3(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
)
817 Aml
*var
= aml_alloc();
818 build_append_namestring(var
->buf
, "%s", method
);
819 aml_append(var
, arg1
);
820 aml_append(var
, arg2
);
821 aml_append(var
, arg3
);
825 /* helper to call method with 4 arguments */
826 Aml
*aml_call4(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
, Aml
*arg4
)
828 Aml
*var
= aml_alloc();
829 build_append_namestring(var
->buf
, "%s", method
);
830 aml_append(var
, arg1
);
831 aml_append(var
, arg2
);
832 aml_append(var
, arg3
);
833 aml_append(var
, arg4
);
837 /* helper to call method with 5 arguments */
838 Aml
*aml_call5(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
, Aml
*arg4
,
841 Aml
*var
= aml_alloc();
842 build_append_namestring(var
->buf
, "%s", method
);
843 aml_append(var
, arg1
);
844 aml_append(var
, arg2
);
845 aml_append(var
, arg3
);
846 aml_append(var
, arg4
);
847 aml_append(var
, arg5
);
851 /* helper to call method with 5 arguments */
852 Aml
*aml_call6(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
, Aml
*arg4
,
853 Aml
*arg5
, Aml
*arg6
)
855 Aml
*var
= aml_alloc();
856 build_append_namestring(var
->buf
, "%s", method
);
857 aml_append(var
, arg1
);
858 aml_append(var
, arg2
);
859 aml_append(var
, arg3
);
860 aml_append(var
, arg4
);
861 aml_append(var
, arg5
);
862 aml_append(var
, arg6
);
867 * ACPI 5.0: 6.4.3.8.1 GPIO Connection Descriptor
868 * Type 1, Large Item Name 0xC
871 static Aml
*aml_gpio_connection(AmlGpioConnectionType type
,
872 AmlConsumerAndProducer con_and_pro
,
873 uint8_t flags
, AmlPinConfig pin_config
,
874 uint16_t output_drive
,
875 uint16_t debounce_timeout
,
876 const uint32_t pin_list
[], uint32_t pin_count
,
877 const char *resource_source_name
,
878 const uint8_t *vendor_data
,
879 uint16_t vendor_data_len
)
881 Aml
*var
= aml_alloc();
882 const uint16_t min_desc_len
= 0x16;
883 uint16_t resource_source_name_len
, length
;
884 uint16_t pin_table_offset
, resource_source_name_offset
, vendor_data_offset
;
887 assert(resource_source_name
);
888 resource_source_name_len
= strlen(resource_source_name
) + 1;
889 length
= min_desc_len
+ resource_source_name_len
+ vendor_data_len
;
890 pin_table_offset
= min_desc_len
+ 1;
891 resource_source_name_offset
= pin_table_offset
+ pin_count
* 2;
892 vendor_data_offset
= resource_source_name_offset
+ resource_source_name_len
;
894 build_append_byte(var
->buf
, 0x8C); /* GPIO Connection Descriptor */
895 build_append_int_noprefix(var
->buf
, length
, 2); /* Length */
896 build_append_byte(var
->buf
, 1); /* Revision ID */
897 build_append_byte(var
->buf
, type
); /* GPIO Connection Type */
898 /* General Flags (2 bytes) */
899 build_append_int_noprefix(var
->buf
, con_and_pro
, 2);
900 /* Interrupt and IO Flags (2 bytes) */
901 build_append_int_noprefix(var
->buf
, flags
, 2);
902 /* Pin Configuration 0 = Default 1 = Pull-up 2 = Pull-down 3 = No Pull */
903 build_append_byte(var
->buf
, pin_config
);
904 /* Output Drive Strength (2 bytes) */
905 build_append_int_noprefix(var
->buf
, output_drive
, 2);
906 /* Debounce Timeout (2 bytes) */
907 build_append_int_noprefix(var
->buf
, debounce_timeout
, 2);
908 /* Pin Table Offset (2 bytes) */
909 build_append_int_noprefix(var
->buf
, pin_table_offset
, 2);
910 build_append_byte(var
->buf
, 0); /* Resource Source Index */
911 /* Resource Source Name Offset (2 bytes) */
912 build_append_int_noprefix(var
->buf
, resource_source_name_offset
, 2);
913 /* Vendor Data Offset (2 bytes) */
914 build_append_int_noprefix(var
->buf
, vendor_data_offset
, 2);
915 /* Vendor Data Length (2 bytes) */
916 build_append_int_noprefix(var
->buf
, vendor_data_len
, 2);
917 /* Pin Number (2n bytes)*/
918 for (i
= 0; i
< pin_count
; i
++) {
919 build_append_int_noprefix(var
->buf
, pin_list
[i
], 2);
922 /* Resource Source Name */
923 build_append_namestring(var
->buf
, "%s", resource_source_name
);
924 build_append_byte(var
->buf
, '\0');
926 /* Vendor-defined Data */
927 if (vendor_data
!= NULL
) {
928 g_array_append_vals(var
->buf
, vendor_data
, vendor_data_len
);
936 * GpioInt(GPIO Interrupt Connection Resource Descriptor Macro)
938 Aml
*aml_gpio_int(AmlConsumerAndProducer con_and_pro
,
939 AmlLevelAndEdge edge_level
,
940 AmlActiveHighAndLow active_level
, AmlShared shared
,
941 AmlPinConfig pin_config
, uint16_t debounce_timeout
,
942 const uint32_t pin_list
[], uint32_t pin_count
,
943 const char *resource_source_name
,
944 const uint8_t *vendor_data
, uint16_t vendor_data_len
)
946 uint8_t flags
= edge_level
| (active_level
<< 1) | (shared
<< 3);
948 return aml_gpio_connection(AML_INTERRUPT_CONNECTION
, con_and_pro
, flags
,
949 pin_config
, 0, debounce_timeout
, pin_list
,
950 pin_count
, resource_source_name
, vendor_data
,
955 * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor
956 * (Type 1, Large Item Name 0x6)
958 Aml
*aml_memory32_fixed(uint32_t addr
, uint32_t size
,
959 AmlReadAndWrite read_and_write
)
961 Aml
*var
= aml_alloc();
962 build_append_byte(var
->buf
, 0x86); /* Memory32Fixed Resource Descriptor */
963 build_append_byte(var
->buf
, 9); /* Length, bits[7:0] value = 9 */
964 build_append_byte(var
->buf
, 0); /* Length, bits[15:8] value = 0 */
965 build_append_byte(var
->buf
, read_and_write
); /* Write status, 1 rw 0 ro */
967 /* Range base address */
968 build_append_byte(var
->buf
, extract32(addr
, 0, 8)); /* bits[7:0] */
969 build_append_byte(var
->buf
, extract32(addr
, 8, 8)); /* bits[15:8] */
970 build_append_byte(var
->buf
, extract32(addr
, 16, 8)); /* bits[23:16] */
971 build_append_byte(var
->buf
, extract32(addr
, 24, 8)); /* bits[31:24] */
974 build_append_byte(var
->buf
, extract32(size
, 0, 8)); /* bits[7:0] */
975 build_append_byte(var
->buf
, extract32(size
, 8, 8)); /* bits[15:8] */
976 build_append_byte(var
->buf
, extract32(size
, 16, 8)); /* bits[23:16] */
977 build_append_byte(var
->buf
, extract32(size
, 24, 8)); /* bits[31:24] */
982 * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor
983 * Type 1, Large Item Name 0x9
985 Aml
*aml_interrupt(AmlConsumerAndProducer con_and_pro
,
986 AmlLevelAndEdge level_and_edge
,
987 AmlActiveHighAndLow high_and_low
, AmlShared shared
,
988 uint32_t *irq_list
, uint8_t irq_count
)
991 Aml
*var
= aml_alloc();
992 uint8_t irq_flags
= con_and_pro
| (level_and_edge
<< 1)
993 | (high_and_low
<< 2) | (shared
<< 3);
994 const int header_bytes_in_len
= 2;
995 uint16_t len
= header_bytes_in_len
+ irq_count
* sizeof(uint32_t);
997 assert(irq_count
> 0);
999 build_append_byte(var
->buf
, 0x89); /* Extended irq descriptor */
1000 build_append_byte(var
->buf
, len
& 0xFF); /* Length, bits[7:0] */
1001 build_append_byte(var
->buf
, len
>> 8); /* Length, bits[15:8] */
1002 build_append_byte(var
->buf
, irq_flags
); /* Interrupt Vector Information. */
1003 build_append_byte(var
->buf
, irq_count
); /* Interrupt table length */
1005 /* Interrupt Number List */
1006 for (i
= 0; i
< irq_count
; i
++) {
1007 build_append_int_noprefix(var
->buf
, irq_list
[i
], 4);
1012 /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */
1013 Aml
*aml_io(AmlIODecode dec
, uint16_t min_base
, uint16_t max_base
,
1014 uint8_t aln
, uint8_t len
)
1016 Aml
*var
= aml_alloc();
1017 build_append_byte(var
->buf
, 0x47); /* IO port descriptor */
1018 build_append_byte(var
->buf
, dec
);
1019 build_append_byte(var
->buf
, min_base
& 0xff);
1020 build_append_byte(var
->buf
, (min_base
>> 8) & 0xff);
1021 build_append_byte(var
->buf
, max_base
& 0xff);
1022 build_append_byte(var
->buf
, (max_base
>> 8) & 0xff);
1023 build_append_byte(var
->buf
, aln
);
1024 build_append_byte(var
->buf
, len
);
1029 * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor
1031 * More verbose description at:
1032 * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro)
1033 * 6.4.2.1 IRQ Descriptor
1035 Aml
*aml_irq_no_flags(uint8_t irq
)
1038 Aml
*var
= aml_alloc();
1041 build_append_byte(var
->buf
, 0x22); /* IRQ descriptor 2 byte form */
1043 irq_mask
= 1U << irq
;
1044 build_append_byte(var
->buf
, irq_mask
& 0xFF); /* IRQ mask bits[7:0] */
1045 build_append_byte(var
->buf
, irq_mask
>> 8); /* IRQ mask bits[15:8] */
1049 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */
1050 Aml
*aml_lnot(Aml
*arg
)
1052 Aml
*var
= aml_opcode(0x92 /* LNotOp */);
1053 aml_append(var
, arg
);
1057 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */
1058 Aml
*aml_equal(Aml
*arg1
, Aml
*arg2
)
1060 Aml
*var
= aml_opcode(0x93 /* LequalOp */);
1061 aml_append(var
, arg1
);
1062 aml_append(var
, arg2
);
1066 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreater */
1067 Aml
*aml_lgreater(Aml
*arg1
, Aml
*arg2
)
1069 Aml
*var
= aml_opcode(0x94 /* LGreaterOp */);
1070 aml_append(var
, arg1
);
1071 aml_append(var
, arg2
);
1075 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLGreaterEqual */
1076 Aml
*aml_lgreater_equal(Aml
*arg1
, Aml
*arg2
)
1078 /* LGreaterEqualOp := LNotOp LLessOp */
1079 Aml
*var
= aml_opcode(0x92 /* LNotOp */);
1080 build_append_byte(var
->buf
, 0x95 /* LLessOp */);
1081 aml_append(var
, arg1
);
1082 aml_append(var
, arg2
);
1086 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
1087 Aml
*aml_if(Aml
*predicate
)
1089 Aml
*var
= aml_bundle(0xA0 /* IfOp */, AML_PACKAGE
);
1090 aml_append(var
, predicate
);
1094 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */
1097 Aml
*var
= aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE
);
1101 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefWhile */
1102 Aml
*aml_while(Aml
*predicate
)
1104 Aml
*var
= aml_bundle(0xA2 /* WhileOp */, AML_PACKAGE
);
1105 aml_append(var
, predicate
);
1109 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
1110 Aml
*aml_method(const char *name
, int arg_count
, AmlSerializeFlag sflag
)
1112 Aml
*var
= aml_bundle(0x14 /* MethodOp */, AML_PACKAGE
);
1117 * bit 0-2: ArgCount (0-7)
1118 * bit 3: SerializeFlag
1121 * bit 4-7: reserved (must be 0)
1123 assert(arg_count
< 8);
1124 methodflags
= arg_count
| (sflag
<< 3);
1126 build_append_namestring(var
->buf
, "%s", name
);
1127 build_append_byte(var
->buf
, methodflags
); /* MethodFlags: ArgCount */
1131 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
1132 Aml
*aml_device(const char *name_format
, ...)
1135 Aml
*var
= aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE
);
1136 va_start(ap
, name_format
);
1137 build_append_namestringv(var
->buf
, name_format
, ap
);
1142 /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */
1143 Aml
*aml_resource_template(void)
1145 /* ResourceTemplate is a buffer of Resources with EndTag at the end */
1146 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE
);
1150 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer
1151 * Pass byte_list as NULL to request uninitialized buffer to reserve space.
1153 Aml
*aml_buffer(int buffer_size
, uint8_t *byte_list
)
1156 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_BUFFER
);
1158 for (i
= 0; i
< buffer_size
; i
++) {
1159 if (byte_list
== NULL
) {
1160 build_append_byte(var
->buf
, 0x0);
1162 build_append_byte(var
->buf
, byte_list
[i
]);
1169 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */
1170 Aml
*aml_package(uint8_t num_elements
)
1172 Aml
*var
= aml_bundle(0x12 /* PackageOp */, AML_PACKAGE
);
1173 build_append_byte(var
->buf
, num_elements
);
1177 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */
1178 Aml
*aml_operation_region(const char *name
, AmlRegionSpace rs
,
1179 Aml
*offset
, uint32_t len
)
1181 Aml
*var
= aml_alloc();
1182 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1183 build_append_byte(var
->buf
, 0x80); /* OpRegionOp */
1184 build_append_namestring(var
->buf
, "%s", name
);
1185 build_append_byte(var
->buf
, rs
);
1186 aml_append(var
, offset
);
1187 build_append_int(var
->buf
, len
);
1191 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */
1192 Aml
*aml_named_field(const char *name
, unsigned length
)
1194 Aml
*var
= aml_alloc();
1195 build_append_nameseg(var
->buf
, name
);
1196 build_append_pkg_length(var
->buf
, length
, false);
1200 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */
1201 Aml
*aml_reserved_field(unsigned length
)
1203 Aml
*var
= aml_alloc();
1204 /* ReservedField := 0x00 PkgLength */
1205 build_append_byte(var
->buf
, 0x00);
1206 build_append_pkg_length(var
->buf
, length
, false);
1210 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */
1211 Aml
*aml_field(const char *name
, AmlAccessType type
, AmlLockRule lock
,
1214 Aml
*var
= aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE
);
1215 uint8_t flags
= rule
<< 5 | type
;
1217 flags
|= lock
<< 4; /* LockRule at 4 bit offset */
1219 build_append_namestring(var
->buf
, "%s", name
);
1220 build_append_byte(var
->buf
, flags
);
1225 Aml
*create_field_common(int opcode
, Aml
*srcbuf
, Aml
*index
, const char *name
)
1227 Aml
*var
= aml_opcode(opcode
);
1228 aml_append(var
, srcbuf
);
1229 aml_append(var
, index
);
1230 build_append_namestring(var
->buf
, "%s", name
);
1234 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateField */
1235 Aml
*aml_create_field(Aml
*srcbuf
, Aml
*bit_index
, Aml
*num_bits
,
1238 Aml
*var
= aml_alloc();
1239 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1240 build_append_byte(var
->buf
, 0x13); /* CreateFieldOp */
1241 aml_append(var
, srcbuf
);
1242 aml_append(var
, bit_index
);
1243 aml_append(var
, num_bits
);
1244 build_append_namestring(var
->buf
, "%s", name
);
1248 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */
1249 Aml
*aml_create_dword_field(Aml
*srcbuf
, Aml
*index
, const char *name
)
1251 return create_field_common(0x8A /* CreateDWordFieldOp */,
1252 srcbuf
, index
, name
);
1255 /* ACPI 2.0a: 17.2.4.2 Named Objects Encoding: DefCreateQWordField */
1256 Aml
*aml_create_qword_field(Aml
*srcbuf
, Aml
*index
, const char *name
)
1258 return create_field_common(0x8F /* CreateQWordFieldOp */,
1259 srcbuf
, index
, name
);
1262 /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */
1263 Aml
*aml_string(const char *name_format
, ...)
1265 Aml
*var
= aml_opcode(0x0D /* StringPrefix */);
1270 va_start(ap
, name_format
);
1271 len
= g_vasprintf(&s
, name_format
, ap
);
1274 g_array_append_vals(var
->buf
, s
, len
+ 1);
1280 /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */
1281 Aml
*aml_local(int num
)
1283 uint8_t op
= 0x60 /* Local0Op */ + num
;
1286 return aml_opcode(op
);
1289 /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */
1290 Aml
*aml_varpackage(uint32_t num_elements
)
1292 Aml
*var
= aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE
);
1293 build_append_int(var
->buf
, num_elements
);
1297 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */
1298 Aml
*aml_processor(uint8_t proc_id
, uint32_t pblk_addr
, uint8_t pblk_len
,
1299 const char *name_format
, ...)
1302 Aml
*var
= aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE
);
1303 va_start(ap
, name_format
);
1304 build_append_namestringv(var
->buf
, name_format
, ap
);
1306 build_append_byte(var
->buf
, proc_id
); /* ProcID */
1307 build_append_int_noprefix(var
->buf
, pblk_addr
, sizeof(pblk_addr
));
1308 build_append_byte(var
->buf
, pblk_len
); /* PblkLen */
1312 static uint8_t Hex2Digit(char c
)
1315 return c
- 'A' + 10;
1321 /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */
1322 Aml
*aml_eisaid(const char *str
)
1324 Aml
*var
= aml_alloc();
1327 g_assert(strlen(str
) == 7);
1328 id
= (str
[0] - 0x40) << 26 |
1329 (str
[1] - 0x40) << 21 |
1330 (str
[2] - 0x40) << 16 |
1331 Hex2Digit(str
[3]) << 12 |
1332 Hex2Digit(str
[4]) << 8 |
1333 Hex2Digit(str
[5]) << 4 |
1336 build_append_byte(var
->buf
, 0x0C); /* DWordPrefix */
1337 build_append_int_noprefix(var
->buf
, bswap32(id
), sizeof(id
));
1341 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */
1342 static Aml
*aml_as_desc_header(AmlResourceType type
, AmlMinFixed min_fixed
,
1343 AmlMaxFixed max_fixed
, AmlDecode dec
,
1346 uint8_t flags
= max_fixed
| min_fixed
| dec
;
1347 Aml
*var
= aml_alloc();
1349 build_append_byte(var
->buf
, type
);
1350 build_append_byte(var
->buf
, flags
);
1351 build_append_byte(var
->buf
, type_flags
); /* Type Specific Flags */
1355 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */
1356 static Aml
*aml_word_as_desc(AmlResourceType type
, AmlMinFixed min_fixed
,
1357 AmlMaxFixed max_fixed
, AmlDecode dec
,
1358 uint16_t addr_gran
, uint16_t addr_min
,
1359 uint16_t addr_max
, uint16_t addr_trans
,
1360 uint16_t len
, uint8_t type_flags
)
1362 Aml
*var
= aml_alloc();
1364 build_append_byte(var
->buf
, 0x88); /* Word Address Space Descriptor */
1365 /* minimum length since we do not encode optional fields */
1366 build_append_byte(var
->buf
, 0x0D);
1367 build_append_byte(var
->buf
, 0x0);
1370 aml_as_desc_header(type
, min_fixed
, max_fixed
, dec
, type_flags
));
1371 build_append_int_noprefix(var
->buf
, addr_gran
, sizeof(addr_gran
));
1372 build_append_int_noprefix(var
->buf
, addr_min
, sizeof(addr_min
));
1373 build_append_int_noprefix(var
->buf
, addr_max
, sizeof(addr_max
));
1374 build_append_int_noprefix(var
->buf
, addr_trans
, sizeof(addr_trans
));
1375 build_append_int_noprefix(var
->buf
, len
, sizeof(len
));
1379 /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */
1380 static Aml
*aml_dword_as_desc(AmlResourceType type
, AmlMinFixed min_fixed
,
1381 AmlMaxFixed max_fixed
, AmlDecode dec
,
1382 uint32_t addr_gran
, uint32_t addr_min
,
1383 uint32_t addr_max
, uint32_t addr_trans
,
1384 uint32_t len
, uint8_t type_flags
)
1386 Aml
*var
= aml_alloc();
1388 build_append_byte(var
->buf
, 0x87); /* DWord Address Space Descriptor */
1389 /* minimum length since we do not encode optional fields */
1390 build_append_byte(var
->buf
, 23);
1391 build_append_byte(var
->buf
, 0x0);
1395 aml_as_desc_header(type
, min_fixed
, max_fixed
, dec
, type_flags
));
1396 build_append_int_noprefix(var
->buf
, addr_gran
, sizeof(addr_gran
));
1397 build_append_int_noprefix(var
->buf
, addr_min
, sizeof(addr_min
));
1398 build_append_int_noprefix(var
->buf
, addr_max
, sizeof(addr_max
));
1399 build_append_int_noprefix(var
->buf
, addr_trans
, sizeof(addr_trans
));
1400 build_append_int_noprefix(var
->buf
, len
, sizeof(len
));
1404 /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */
1405 static Aml
*aml_qword_as_desc(AmlResourceType type
, AmlMinFixed min_fixed
,
1406 AmlMaxFixed max_fixed
, AmlDecode dec
,
1407 uint64_t addr_gran
, uint64_t addr_min
,
1408 uint64_t addr_max
, uint64_t addr_trans
,
1409 uint64_t len
, uint8_t type_flags
)
1411 Aml
*var
= aml_alloc();
1413 build_append_byte(var
->buf
, 0x8A); /* QWord Address Space Descriptor */
1414 /* minimum length since we do not encode optional fields */
1415 build_append_byte(var
->buf
, 0x2B);
1416 build_append_byte(var
->buf
, 0x0);
1419 aml_as_desc_header(type
, min_fixed
, max_fixed
, dec
, type_flags
));
1420 build_append_int_noprefix(var
->buf
, addr_gran
, sizeof(addr_gran
));
1421 build_append_int_noprefix(var
->buf
, addr_min
, sizeof(addr_min
));
1422 build_append_int_noprefix(var
->buf
, addr_max
, sizeof(addr_max
));
1423 build_append_int_noprefix(var
->buf
, addr_trans
, sizeof(addr_trans
));
1424 build_append_int_noprefix(var
->buf
, len
, sizeof(len
));
1429 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1431 * More verbose description at:
1432 * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro)
1434 Aml
*aml_word_bus_number(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
1435 AmlDecode dec
, uint16_t addr_gran
,
1436 uint16_t addr_min
, uint16_t addr_max
,
1437 uint16_t addr_trans
, uint16_t len
)
1440 return aml_word_as_desc(AML_BUS_NUMBER_RANGE
, min_fixed
, max_fixed
, dec
,
1441 addr_gran
, addr_min
, addr_max
, addr_trans
, len
, 0);
1445 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
1447 * More verbose description at:
1448 * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro)
1450 Aml
*aml_word_io(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
1451 AmlDecode dec
, AmlISARanges isa_ranges
,
1452 uint16_t addr_gran
, uint16_t addr_min
,
1453 uint16_t addr_max
, uint16_t addr_trans
,
1457 return aml_word_as_desc(AML_IO_RANGE
, min_fixed
, max_fixed
, dec
,
1458 addr_gran
, addr_min
, addr_max
, addr_trans
, len
,
1463 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor
1465 * More verbose description at:
1466 * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro)
1468 Aml
*aml_dword_io(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
1469 AmlDecode dec
, AmlISARanges isa_ranges
,
1470 uint32_t addr_gran
, uint32_t addr_min
,
1471 uint32_t addr_max
, uint32_t addr_trans
,
1475 return aml_dword_as_desc(AML_IO_RANGE
, min_fixed
, max_fixed
, dec
,
1476 addr_gran
, addr_min
, addr_max
, addr_trans
, len
,
1481 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor
1483 * More verbose description at:
1484 * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro)
1486 Aml
*aml_dword_memory(AmlDecode dec
, AmlMinFixed min_fixed
,
1487 AmlMaxFixed max_fixed
, AmlCacheable cacheable
,
1488 AmlReadAndWrite read_and_write
,
1489 uint32_t addr_gran
, uint32_t addr_min
,
1490 uint32_t addr_max
, uint32_t addr_trans
,
1493 uint8_t flags
= read_and_write
| (cacheable
<< 1);
1495 return aml_dword_as_desc(AML_MEMORY_RANGE
, min_fixed
, max_fixed
,
1496 dec
, addr_gran
, addr_min
, addr_max
,
1497 addr_trans
, len
, flags
);
1501 * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor
1503 * More verbose description at:
1504 * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro)
1506 Aml
*aml_qword_memory(AmlDecode dec
, AmlMinFixed min_fixed
,
1507 AmlMaxFixed max_fixed
, AmlCacheable cacheable
,
1508 AmlReadAndWrite read_and_write
,
1509 uint64_t addr_gran
, uint64_t addr_min
,
1510 uint64_t addr_max
, uint64_t addr_trans
,
1513 uint8_t flags
= read_and_write
| (cacheable
<< 1);
1515 return aml_qword_as_desc(AML_MEMORY_RANGE
, min_fixed
, max_fixed
,
1516 dec
, addr_gran
, addr_min
, addr_max
,
1517 addr_trans
, len
, flags
);
1520 /* ACPI 1.0b: 6.4.2.2 DMA Format/6.4.2.2.1 ASL Macro for DMA Descriptor */
1521 Aml
*aml_dma(AmlDmaType typ
, AmlDmaBusMaster bm
, AmlTransferSize sz
,
1524 Aml
*var
= aml_alloc();
1525 uint8_t flags
= sz
| bm
<< 2 | typ
<< 5;
1527 assert(channel
< 8);
1528 build_append_byte(var
->buf
, 0x2A); /* Byte 0: DMA Descriptor */
1529 build_append_byte(var
->buf
, 1U << channel
); /* Byte 1: _DMA - DmaChannel */
1530 build_append_byte(var
->buf
, flags
); /* Byte 2 */
1534 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefSleep */
1535 Aml
*aml_sleep(uint64_t msec
)
1537 Aml
*var
= aml_alloc();
1538 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1539 build_append_byte(var
->buf
, 0x22); /* SleepOp */
1540 aml_append(var
, aml_int(msec
));
1544 static uint8_t Hex2Byte(const char *src
)
1548 hi
= Hex2Digit(src
[0]);
1552 lo
= Hex2Digit(src
[1]);
1555 return (hi
<< 4) | lo
;
1559 * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro)
1560 * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1561 * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp");
1563 Aml
*aml_touuid(const char *uuid
)
1565 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_BUFFER
);
1567 assert(strlen(uuid
) == 36);
1568 assert(uuid
[8] == '-');
1569 assert(uuid
[13] == '-');
1570 assert(uuid
[18] == '-');
1571 assert(uuid
[23] == '-');
1573 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 6)); /* dd - at offset 00 */
1574 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 4)); /* cc - at offset 01 */
1575 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 2)); /* bb - at offset 02 */
1576 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 0)); /* aa - at offset 03 */
1578 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 11)); /* ff - at offset 04 */
1579 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 9)); /* ee - at offset 05 */
1581 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 16)); /* hh - at offset 06 */
1582 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 14)); /* gg - at offset 07 */
1584 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 19)); /* ii - at offset 08 */
1585 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 21)); /* jj - at offset 09 */
1587 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 24)); /* kk - at offset 10 */
1588 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 26)); /* ll - at offset 11 */
1589 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 28)); /* mm - at offset 12 */
1590 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 30)); /* nn - at offset 13 */
1591 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 32)); /* oo - at offset 14 */
1592 build_append_byte(var
->buf
, Hex2Byte(uuid
+ 34)); /* pp - at offset 15 */
1598 * ACPI 2.0b: 16.2.3.6.4.3 Unicode Macro (Convert Ascii String To Unicode)
1600 Aml
*aml_unicode(const char *str
)
1603 Aml
*var
= aml_bundle(0x11 /* BufferOp */, AML_BUFFER
);
1606 build_append_byte(var
->buf
, str
[i
]);
1607 build_append_byte(var
->buf
, 0);
1609 } while (i
<= strlen(str
));
1614 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefRefOf */
1615 Aml
*aml_refof(Aml
*arg
)
1617 Aml
*var
= aml_opcode(0x71 /* RefOfOp */);
1618 aml_append(var
, arg
);
1622 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDerefOf */
1623 Aml
*aml_derefof(Aml
*arg
)
1625 Aml
*var
= aml_opcode(0x83 /* DerefOfOp */);
1626 aml_append(var
, arg
);
1630 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSizeOf */
1631 Aml
*aml_sizeof(Aml
*arg
)
1633 Aml
*var
= aml_opcode(0x87 /* SizeOfOp */);
1634 aml_append(var
, arg
);
1638 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMutex */
1639 Aml
*aml_mutex(const char *name
, uint8_t sync_level
)
1641 Aml
*var
= aml_alloc();
1642 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1643 build_append_byte(var
->buf
, 0x01); /* MutexOp */
1644 build_append_namestring(var
->buf
, "%s", name
);
1645 assert(!(sync_level
& 0xF0));
1646 build_append_byte(var
->buf
, sync_level
);
1650 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAcquire */
1651 Aml
*aml_acquire(Aml
*mutex
, uint16_t timeout
)
1653 Aml
*var
= aml_alloc();
1654 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1655 build_append_byte(var
->buf
, 0x23); /* AcquireOp */
1656 aml_append(var
, mutex
);
1657 build_append_int_noprefix(var
->buf
, timeout
, sizeof(timeout
));
1661 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefRelease */
1662 Aml
*aml_release(Aml
*mutex
)
1664 Aml
*var
= aml_alloc();
1665 build_append_byte(var
->buf
, 0x5B); /* ExtOpPrefix */
1666 build_append_byte(var
->buf
, 0x27); /* ReleaseOp */
1667 aml_append(var
, mutex
);
1671 /* ACPI 1.0b: 16.2.5.1 Name Space Modifier Objects Encoding: DefAlias */
1672 Aml
*aml_alias(const char *source_object
, const char *alias_object
)
1674 Aml
*var
= aml_opcode(0x06 /* AliasOp */);
1675 aml_append(var
, aml_name("%s", source_object
));
1676 aml_append(var
, aml_name("%s", alias_object
));
1680 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefConcat */
1681 Aml
*aml_concatenate(Aml
*source1
, Aml
*source2
, Aml
*target
)
1683 return build_opcode_2arg_dst(0x73 /* ConcatOp */, source1
, source2
,
1687 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefObjectType */
1688 Aml
*aml_object_type(Aml
*object
)
1690 Aml
*var
= aml_opcode(0x8E /* ObjectTypeOp */);
1691 aml_append(var
, object
);
1696 build_header(BIOSLinker
*linker
, GArray
*table_data
,
1697 AcpiTableHeader
*h
, const char *sig
, int len
, uint8_t rev
,
1698 const char *oem_id
, const char *oem_table_id
)
1700 unsigned tbl_offset
= (char *)h
- table_data
->data
;
1701 unsigned checksum_offset
= (char *)&h
->checksum
- table_data
->data
;
1702 memcpy(&h
->signature
, sig
, 4);
1703 h
->length
= cpu_to_le32(len
);
1706 strpadcpy((char *)h
->oem_id
, sizeof h
->oem_id
, oem_id
, ' ');
1707 strpadcpy((char *)h
->oem_table_id
, sizeof h
->oem_table_id
,
1710 h
->oem_revision
= cpu_to_le32(1);
1711 memcpy(h
->asl_compiler_id
, ACPI_BUILD_APPNAME8
, 4);
1712 h
->asl_compiler_revision
= cpu_to_le32(1);
1713 /* Checksum to be filled in by Guest linker */
1714 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_TABLE_FILE
,
1715 tbl_offset
, len
, checksum_offset
);
1718 void *acpi_data_push(GArray
*table_data
, unsigned size
)
1720 unsigned off
= table_data
->len
;
1721 g_array_set_size(table_data
, off
+ size
);
1722 return table_data
->data
+ off
;
1725 unsigned acpi_data_len(GArray
*table
)
1727 assert(g_array_get_element_size(table
) == 1);
1731 void acpi_add_table(GArray
*table_offsets
, GArray
*table_data
)
1733 uint32_t offset
= table_data
->len
;
1734 g_array_append_val(table_offsets
, offset
);
1737 void acpi_build_tables_init(AcpiBuildTables
*tables
)
1739 tables
->rsdp
= g_array_new(false, true /* clear */, 1);
1740 tables
->table_data
= g_array_new(false, true /* clear */, 1);
1741 tables
->tcpalog
= g_array_new(false, true /* clear */, 1);
1742 tables
->vmgenid
= g_array_new(false, true /* clear */, 1);
1743 tables
->hardware_errors
= g_array_new(false, true /* clear */, 1);
1744 tables
->linker
= bios_linker_loader_init();
1747 void acpi_build_tables_cleanup(AcpiBuildTables
*tables
, bool mfre
)
1749 bios_linker_loader_cleanup(tables
->linker
);
1750 g_array_free(tables
->rsdp
, true);
1751 g_array_free(tables
->table_data
, true);
1752 g_array_free(tables
->tcpalog
, mfre
);
1753 g_array_free(tables
->vmgenid
, mfre
);
1754 g_array_free(tables
->hardware_errors
, mfre
);
1758 * ACPI spec 5.2.5.3 Root System Description Pointer (RSDP).
1759 * (Revision 1.0 or later)
1762 build_rsdp(GArray
*tbl
, BIOSLinker
*linker
, AcpiRsdpData
*rsdp_data
)
1764 int tbl_off
= tbl
->len
; /* Table offset in the RSDP file */
1766 switch (rsdp_data
->revision
) {
1768 /* With ACPI 1.0, we must have an RSDT pointer */
1769 g_assert(rsdp_data
->rsdt_tbl_offset
);
1772 /* With ACPI 2.0+, we must have an XSDT pointer */
1773 g_assert(rsdp_data
->xsdt_tbl_offset
);
1776 /* Only revisions 0 (ACPI 1.0) and 2 (ACPI 2.0+) are valid for RSDP */
1777 g_assert_not_reached();
1780 bios_linker_loader_alloc(linker
, ACPI_BUILD_RSDP_FILE
, tbl
, 16,
1781 true /* fseg memory */);
1783 g_array_append_vals(tbl
, "RSD PTR ", 8); /* Signature */
1784 build_append_int_noprefix(tbl
, 0, 1); /* Checksum */
1785 g_array_append_vals(tbl
, rsdp_data
->oem_id
, 6); /* OEMID */
1786 build_append_int_noprefix(tbl
, rsdp_data
->revision
, 1); /* Revision */
1787 build_append_int_noprefix(tbl
, 0, 4); /* RsdtAddress */
1788 if (rsdp_data
->rsdt_tbl_offset
) {
1789 /* RSDT address to be filled by guest linker */
1790 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_RSDP_FILE
,
1792 ACPI_BUILD_TABLE_FILE
,
1793 *rsdp_data
->rsdt_tbl_offset
);
1796 /* Checksum to be filled by guest linker */
1797 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_RSDP_FILE
,
1798 tbl_off
, 20, /* ACPI rev 1.0 RSDP size */
1801 if (rsdp_data
->revision
== 0) {
1802 /* ACPI 1.0 RSDP, we're done */
1806 build_append_int_noprefix(tbl
, 36, 4); /* Length */
1808 /* XSDT address to be filled by guest linker */
1809 build_append_int_noprefix(tbl
, 0, 8); /* XsdtAddress */
1810 /* We already validated our xsdt pointer */
1811 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_RSDP_FILE
,
1813 ACPI_BUILD_TABLE_FILE
,
1814 *rsdp_data
->xsdt_tbl_offset
);
1816 build_append_int_noprefix(tbl
, 0, 1); /* Extended Checksum */
1817 build_append_int_noprefix(tbl
, 0, 3); /* Reserved */
1819 /* Extended checksum to be filled by Guest linker */
1820 bios_linker_loader_add_checksum(linker
, ACPI_BUILD_RSDP_FILE
,
1821 tbl_off
, 36, /* ACPI rev 2.0 RSDP size */
1825 /* Build rsdt table */
1827 build_rsdt(GArray
*table_data
, BIOSLinker
*linker
, GArray
*table_offsets
,
1828 const char *oem_id
, const char *oem_table_id
)
1831 unsigned rsdt_entries_offset
;
1832 AcpiRsdtDescriptorRev1
*rsdt
;
1833 const unsigned table_data_len
= (sizeof(uint32_t) * table_offsets
->len
);
1834 const unsigned rsdt_entry_size
= sizeof(rsdt
->table_offset_entry
[0]);
1835 const size_t rsdt_len
= sizeof(*rsdt
) + table_data_len
;
1837 rsdt
= acpi_data_push(table_data
, rsdt_len
);
1838 rsdt_entries_offset
= (char *)rsdt
->table_offset_entry
- table_data
->data
;
1839 for (i
= 0; i
< table_offsets
->len
; ++i
) {
1840 uint32_t ref_tbl_offset
= g_array_index(table_offsets
, uint32_t, i
);
1841 uint32_t rsdt_entry_offset
= rsdt_entries_offset
+ rsdt_entry_size
* i
;
1843 /* rsdt->table_offset_entry to be filled by Guest linker */
1844 bios_linker_loader_add_pointer(linker
,
1845 ACPI_BUILD_TABLE_FILE
, rsdt_entry_offset
, rsdt_entry_size
,
1846 ACPI_BUILD_TABLE_FILE
, ref_tbl_offset
);
1848 build_header(linker
, table_data
,
1849 (void *)rsdt
, "RSDT", rsdt_len
, 1, oem_id
, oem_table_id
);
1852 /* Build xsdt table */
1854 build_xsdt(GArray
*table_data
, BIOSLinker
*linker
, GArray
*table_offsets
,
1855 const char *oem_id
, const char *oem_table_id
)
1858 unsigned xsdt_entries_offset
;
1859 AcpiXsdtDescriptorRev2
*xsdt
;
1860 const unsigned table_data_len
= (sizeof(uint64_t) * table_offsets
->len
);
1861 const unsigned xsdt_entry_size
= sizeof(xsdt
->table_offset_entry
[0]);
1862 const size_t xsdt_len
= sizeof(*xsdt
) + table_data_len
;
1864 xsdt
= acpi_data_push(table_data
, xsdt_len
);
1865 xsdt_entries_offset
= (char *)xsdt
->table_offset_entry
- table_data
->data
;
1866 for (i
= 0; i
< table_offsets
->len
; ++i
) {
1867 uint64_t ref_tbl_offset
= g_array_index(table_offsets
, uint32_t, i
);
1868 uint64_t xsdt_entry_offset
= xsdt_entries_offset
+ xsdt_entry_size
* i
;
1870 /* xsdt->table_offset_entry to be filled by Guest linker */
1871 bios_linker_loader_add_pointer(linker
,
1872 ACPI_BUILD_TABLE_FILE
, xsdt_entry_offset
, xsdt_entry_size
,
1873 ACPI_BUILD_TABLE_FILE
, ref_tbl_offset
);
1875 build_header(linker
, table_data
,
1876 (void *)xsdt
, "XSDT", xsdt_len
, 1, oem_id
, oem_table_id
);
1879 void build_srat_memory(AcpiSratMemoryAffinity
*numamem
, uint64_t base
,
1880 uint64_t len
, int node
, MemoryAffinityFlags flags
)
1882 numamem
->type
= ACPI_SRAT_MEMORY
;
1883 numamem
->length
= sizeof(*numamem
);
1884 numamem
->proximity
= cpu_to_le32(node
);
1885 numamem
->flags
= cpu_to_le32(flags
);
1886 numamem
->base_addr
= cpu_to_le64(base
);
1887 numamem
->range_length
= cpu_to_le64(len
);
1891 * ACPI spec 5.2.17 System Locality Distance Information Table
1892 * (Revision 2.0 or later)
1894 void build_slit(GArray
*table_data
, BIOSLinker
*linker
, MachineState
*ms
,
1895 const char *oem_id
, const char *oem_table_id
)
1897 int slit_start
, i
, j
;
1898 slit_start
= table_data
->len
;
1899 int nb_numa_nodes
= ms
->numa_state
->num_nodes
;
1901 acpi_data_push(table_data
, sizeof(AcpiTableHeader
));
1903 build_append_int_noprefix(table_data
, nb_numa_nodes
, 8);
1904 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1905 for (j
= 0; j
< nb_numa_nodes
; j
++) {
1906 assert(ms
->numa_state
->nodes
[i
].distance
[j
]);
1907 build_append_int_noprefix(table_data
,
1908 ms
->numa_state
->nodes
[i
].distance
[j
],
1913 build_header(linker
, table_data
,
1914 (void *)(table_data
->data
+ slit_start
),
1916 table_data
->len
- slit_start
, 1, oem_id
, oem_table_id
);
1919 /* build rev1/rev3/rev5.1 FADT */
1920 void build_fadt(GArray
*tbl
, BIOSLinker
*linker
, const AcpiFadtData
*f
,
1921 const char *oem_id
, const char *oem_table_id
)
1924 int fadt_start
= tbl
->len
;
1926 acpi_data_push(tbl
, sizeof(AcpiTableHeader
));
1928 /* FACS address to be filled by Guest linker at runtime */
1930 build_append_int_noprefix(tbl
, 0, 4); /* FIRMWARE_CTRL */
1931 if (f
->facs_tbl_offset
) { /* don't patch if not supported by platform */
1932 bios_linker_loader_add_pointer(linker
,
1933 ACPI_BUILD_TABLE_FILE
, off
, 4,
1934 ACPI_BUILD_TABLE_FILE
, *f
->facs_tbl_offset
);
1937 /* DSDT address to be filled by Guest linker at runtime */
1939 build_append_int_noprefix(tbl
, 0, 4); /* DSDT */
1940 if (f
->dsdt_tbl_offset
) { /* don't patch if not supported by platform */
1941 bios_linker_loader_add_pointer(linker
,
1942 ACPI_BUILD_TABLE_FILE
, off
, 4,
1943 ACPI_BUILD_TABLE_FILE
, *f
->dsdt_tbl_offset
);
1946 /* ACPI1.0: INT_MODEL, ACPI2.0+: Reserved */
1947 build_append_int_noprefix(tbl
, f
->int_model
/* Multiple APIC */, 1);
1948 /* Preferred_PM_Profile */
1949 build_append_int_noprefix(tbl
, 0 /* Unspecified */, 1);
1950 build_append_int_noprefix(tbl
, f
->sci_int
, 2); /* SCI_INT */
1951 build_append_int_noprefix(tbl
, f
->smi_cmd
, 4); /* SMI_CMD */
1952 build_append_int_noprefix(tbl
, f
->acpi_enable_cmd
, 1); /* ACPI_ENABLE */
1953 build_append_int_noprefix(tbl
, f
->acpi_disable_cmd
, 1); /* ACPI_DISABLE */
1954 build_append_int_noprefix(tbl
, 0 /* not supported */, 1); /* S4BIOS_REQ */
1955 /* ACPI1.0: Reserved, ACPI2.0+: PSTATE_CNT */
1956 build_append_int_noprefix(tbl
, 0, 1);
1957 build_append_int_noprefix(tbl
, f
->pm1a_evt
.address
, 4); /* PM1a_EVT_BLK */
1958 build_append_int_noprefix(tbl
, 0, 4); /* PM1b_EVT_BLK */
1959 build_append_int_noprefix(tbl
, f
->pm1a_cnt
.address
, 4); /* PM1a_CNT_BLK */
1960 build_append_int_noprefix(tbl
, 0, 4); /* PM1b_CNT_BLK */
1961 build_append_int_noprefix(tbl
, 0, 4); /* PM2_CNT_BLK */
1962 build_append_int_noprefix(tbl
, f
->pm_tmr
.address
, 4); /* PM_TMR_BLK */
1963 build_append_int_noprefix(tbl
, f
->gpe0_blk
.address
, 4); /* GPE0_BLK */
1964 build_append_int_noprefix(tbl
, 0, 4); /* GPE1_BLK */
1966 build_append_int_noprefix(tbl
, f
->pm1a_evt
.bit_width
/ 8, 1);
1968 build_append_int_noprefix(tbl
, f
->pm1a_cnt
.bit_width
/ 8, 1);
1969 build_append_int_noprefix(tbl
, 0, 1); /* PM2_CNT_LEN */
1970 build_append_int_noprefix(tbl
, f
->pm_tmr
.bit_width
/ 8, 1); /* PM_TMR_LEN */
1972 build_append_int_noprefix(tbl
, f
->gpe0_blk
.bit_width
/ 8, 1);
1973 build_append_int_noprefix(tbl
, 0, 1); /* GPE1_BLK_LEN */
1974 build_append_int_noprefix(tbl
, 0, 1); /* GPE1_BASE */
1975 build_append_int_noprefix(tbl
, 0, 1); /* CST_CNT */
1976 build_append_int_noprefix(tbl
, f
->plvl2_lat
, 2); /* P_LVL2_LAT */
1977 build_append_int_noprefix(tbl
, f
->plvl3_lat
, 2); /* P_LVL3_LAT */
1978 build_append_int_noprefix(tbl
, 0, 2); /* FLUSH_SIZE */
1979 build_append_int_noprefix(tbl
, 0, 2); /* FLUSH_STRIDE */
1980 build_append_int_noprefix(tbl
, 0, 1); /* DUTY_OFFSET */
1981 build_append_int_noprefix(tbl
, 0, 1); /* DUTY_WIDTH */
1982 build_append_int_noprefix(tbl
, 0, 1); /* DAY_ALRM */
1983 build_append_int_noprefix(tbl
, 0, 1); /* MON_ALRM */
1984 build_append_int_noprefix(tbl
, f
->rtc_century
, 1); /* CENTURY */
1985 build_append_int_noprefix(tbl
, 0, 2); /* IAPC_BOOT_ARCH */
1986 build_append_int_noprefix(tbl
, 0, 1); /* Reserved */
1987 build_append_int_noprefix(tbl
, f
->flags
, 4); /* Flags */
1993 build_append_gas_from_struct(tbl
, &f
->reset_reg
); /* RESET_REG */
1994 build_append_int_noprefix(tbl
, f
->reset_val
, 1); /* RESET_VALUE */
1995 /* Since ACPI 5.1 */
1996 if ((f
->rev
>= 6) || ((f
->rev
== 5) && f
->minor_ver
> 0)) {
1997 build_append_int_noprefix(tbl
, f
->arm_boot_arch
, 2); /* ARM_BOOT_ARCH */
1998 /* FADT Minor Version */
1999 build_append_int_noprefix(tbl
, f
->minor_ver
, 1);
2001 build_append_int_noprefix(tbl
, 0, 3); /* Reserved upto ACPI 5.0 */
2003 build_append_int_noprefix(tbl
, 0, 8); /* X_FIRMWARE_CTRL */
2005 /* XDSDT address to be filled by Guest linker at runtime */
2007 build_append_int_noprefix(tbl
, 0, 8); /* X_DSDT */
2008 if (f
->xdsdt_tbl_offset
) {
2009 bios_linker_loader_add_pointer(linker
,
2010 ACPI_BUILD_TABLE_FILE
, off
, 8,
2011 ACPI_BUILD_TABLE_FILE
, *f
->xdsdt_tbl_offset
);
2014 build_append_gas_from_struct(tbl
, &f
->pm1a_evt
); /* X_PM1a_EVT_BLK */
2015 /* X_PM1b_EVT_BLK */
2016 build_append_gas(tbl
, AML_AS_SYSTEM_MEMORY
, 0 , 0, 0, 0);
2017 build_append_gas_from_struct(tbl
, &f
->pm1a_cnt
); /* X_PM1a_CNT_BLK */
2018 /* X_PM1b_CNT_BLK */
2019 build_append_gas(tbl
, AML_AS_SYSTEM_MEMORY
, 0 , 0, 0, 0);
2021 build_append_gas(tbl
, AML_AS_SYSTEM_MEMORY
, 0 , 0, 0, 0);
2022 build_append_gas_from_struct(tbl
, &f
->pm_tmr
); /* X_PM_TMR_BLK */
2023 build_append_gas_from_struct(tbl
, &f
->gpe0_blk
); /* X_GPE0_BLK */
2024 build_append_gas(tbl
, AML_AS_SYSTEM_MEMORY
, 0 , 0, 0, 0); /* X_GPE1_BLK */
2030 /* SLEEP_CONTROL_REG */
2031 build_append_gas_from_struct(tbl
, &f
->sleep_ctl
);
2032 /* SLEEP_STATUS_REG */
2033 build_append_gas_from_struct(tbl
, &f
->sleep_sts
);
2035 /* TODO: extra fields need to be added to support revisions above rev5 */
2036 assert(f
->rev
== 5);
2039 build_header(linker
, tbl
, (void *)(tbl
->data
+ fadt_start
),
2040 "FACP", tbl
->len
- fadt_start
, f
->rev
, oem_id
, oem_table_id
);
2044 * build_tpm2 - Build the TPM2 table as specified in
2045 * table 7: TCG Hardware Interface Description Table Format for TPM 2.0
2046 * of TCG ACPI Specification, Family “1.2” and “2.0”, Version 1.2, Rev 8
2048 void build_tpm2(GArray
*table_data
, BIOSLinker
*linker
, GArray
*tcpalog
,
2049 const char *oem_id
, const char *oem_table_id
)
2051 uint8_t start_method_params
[12] = {};
2052 unsigned log_addr_offset
, tpm2_start
;
2053 uint64_t control_area_start_address
;
2054 TPMIf
*tpmif
= tpm_find();
2055 uint32_t start_method
;
2058 tpm2_start
= table_data
->len
;
2059 tpm2_ptr
= acpi_data_push(table_data
, sizeof(AcpiTableHeader
));
2061 /* Platform Class */
2062 build_append_int_noprefix(table_data
, TPM2_ACPI_CLASS_CLIENT
, 2);
2064 build_append_int_noprefix(table_data
, 0, 2);
2065 if (TPM_IS_TIS_ISA(tpmif
) || TPM_IS_TIS_SYSBUS(tpmif
)) {
2066 control_area_start_address
= 0;
2067 start_method
= TPM2_START_METHOD_MMIO
;
2068 } else if (TPM_IS_CRB(tpmif
)) {
2069 control_area_start_address
= TPM_CRB_ADDR_CTRL
;
2070 start_method
= TPM2_START_METHOD_CRB
;
2072 g_assert_not_reached();
2074 /* Address of Control Area */
2075 build_append_int_noprefix(table_data
, control_area_start_address
, 8);
2077 build_append_int_noprefix(table_data
, start_method
, 4);
2079 /* Platform Specific Parameters */
2080 g_array_append_vals(table_data
, &start_method_params
,
2081 ARRAY_SIZE(start_method_params
));
2083 /* Log Area Minimum Length */
2084 build_append_int_noprefix(table_data
, TPM_LOG_AREA_MINIMUM_SIZE
, 4);
2086 acpi_data_push(tcpalog
, TPM_LOG_AREA_MINIMUM_SIZE
);
2087 bios_linker_loader_alloc(linker
, ACPI_BUILD_TPMLOG_FILE
, tcpalog
, 1,
2090 log_addr_offset
= table_data
->len
;
2092 /* Log Area Start Address to be filled by Guest linker */
2093 build_append_int_noprefix(table_data
, 0, 8);
2094 bios_linker_loader_add_pointer(linker
, ACPI_BUILD_TABLE_FILE
,
2096 ACPI_BUILD_TPMLOG_FILE
, 0);
2097 build_header(linker
, table_data
,
2098 tpm2_ptr
, "TPM2", table_data
->len
- tpm2_start
, 4, oem_id
,
2102 Aml
*build_crs(PCIHostState
*host
, CrsRangeSet
*range_set
, uint32_t io_offset
,
2103 uint32_t mmio32_offset
, uint64_t mmio64_offset
,
2104 uint16_t bus_nr_offset
)
2106 Aml
*crs
= aml_resource_template();
2107 CrsRangeSet temp_range_set
;
2108 CrsRangeEntry
*entry
;
2109 uint8_t max_bus
= pci_bus_num(host
->bus
);
2114 crs_range_set_init(&temp_range_set
);
2115 for (devfn
= 0; devfn
< ARRAY_SIZE(host
->bus
->devices
); devfn
++) {
2116 uint64_t range_base
, range_limit
;
2117 PCIDevice
*dev
= host
->bus
->devices
[devfn
];
2123 for (i
= 0; i
< PCI_NUM_REGIONS
; i
++) {
2124 PCIIORegion
*r
= &dev
->io_regions
[i
];
2126 range_base
= r
->addr
;
2127 range_limit
= r
->addr
+ r
->size
- 1;
2130 * Work-around for old bioses
2131 * that do not support multiple root buses
2133 if (!range_base
|| range_base
> range_limit
) {
2137 if (r
->type
& PCI_BASE_ADDRESS_SPACE_IO
) {
2138 crs_range_insert(temp_range_set
.io_ranges
,
2139 range_base
, range_limit
);
2140 } else { /* "memory" */
2141 uint64_t length
= range_limit
- range_base
+ 1;
2142 if (range_limit
<= UINT32_MAX
&& length
<= UINT32_MAX
) {
2143 crs_range_insert(temp_range_set
.mem_ranges
, range_base
,
2146 crs_range_insert(temp_range_set
.mem_64bit_ranges
,
2147 range_base
, range_limit
);
2152 type
= dev
->config
[PCI_HEADER_TYPE
] & ~PCI_HEADER_TYPE_MULTI_FUNCTION
;
2153 if (type
== PCI_HEADER_TYPE_BRIDGE
) {
2154 uint8_t subordinate
= dev
->config
[PCI_SUBORDINATE_BUS
];
2155 if (subordinate
> max_bus
) {
2156 max_bus
= subordinate
;
2159 range_base
= pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_IO
);
2160 range_limit
= pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_IO
);
2163 * Work-around for old bioses
2164 * that do not support multiple root buses
2166 if (range_base
&& range_base
<= range_limit
) {
2167 crs_range_insert(temp_range_set
.io_ranges
,
2168 range_base
, range_limit
);
2172 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
);
2174 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_SPACE_MEMORY
);
2177 * Work-around for old bioses
2178 * that do not support multiple root buses
2180 if (range_base
&& range_base
<= range_limit
) {
2181 uint64_t length
= range_limit
- range_base
+ 1;
2182 if (range_limit
<= UINT32_MAX
&& length
<= UINT32_MAX
) {
2183 crs_range_insert(temp_range_set
.mem_ranges
,
2184 range_base
, range_limit
);
2186 crs_range_insert(temp_range_set
.mem_64bit_ranges
,
2187 range_base
, range_limit
);
2192 pci_bridge_get_base(dev
, PCI_BASE_ADDRESS_MEM_PREFETCH
);
2194 pci_bridge_get_limit(dev
, PCI_BASE_ADDRESS_MEM_PREFETCH
);
2197 * Work-around for old bioses
2198 * that do not support multiple root buses
2200 if (range_base
&& range_base
<= range_limit
) {
2201 uint64_t length
= range_limit
- range_base
+ 1;
2202 if (range_limit
<= UINT32_MAX
&& length
<= UINT32_MAX
) {
2203 crs_range_insert(temp_range_set
.mem_ranges
,
2204 range_base
, range_limit
);
2206 crs_range_insert(temp_range_set
.mem_64bit_ranges
,
2207 range_base
, range_limit
);
2213 crs_range_merge(temp_range_set
.io_ranges
);
2214 for (i
= 0; i
< temp_range_set
.io_ranges
->len
; i
++) {
2215 entry
= g_ptr_array_index(temp_range_set
.io_ranges
, i
);
2217 aml_dword_io(AML_MIN_FIXED
, AML_MAX_FIXED
,
2218 AML_POS_DECODE
, AML_ENTIRE_RANGE
,
2219 0, entry
->base
, entry
->limit
, io_offset
,
2220 entry
->limit
- entry
->base
+ 1));
2221 crs_range_insert(range_set
->io_ranges
, entry
->base
, entry
->limit
);
2224 crs_range_merge(temp_range_set
.mem_ranges
);
2225 for (i
= 0; i
< temp_range_set
.mem_ranges
->len
; i
++) {
2226 entry
= g_ptr_array_index(temp_range_set
.mem_ranges
, i
);
2227 assert(entry
->limit
<= UINT32_MAX
&&
2228 (entry
->limit
- entry
->base
+ 1) <= UINT32_MAX
);
2230 aml_dword_memory(AML_POS_DECODE
, AML_MIN_FIXED
,
2231 AML_MAX_FIXED
, AML_NON_CACHEABLE
,
2233 0, entry
->base
, entry
->limit
, mmio32_offset
,
2234 entry
->limit
- entry
->base
+ 1));
2235 crs_range_insert(range_set
->mem_ranges
, entry
->base
, entry
->limit
);
2238 crs_range_merge(temp_range_set
.mem_64bit_ranges
);
2239 for (i
= 0; i
< temp_range_set
.mem_64bit_ranges
->len
; i
++) {
2240 entry
= g_ptr_array_index(temp_range_set
.mem_64bit_ranges
, i
);
2242 aml_qword_memory(AML_POS_DECODE
, AML_MIN_FIXED
,
2243 AML_MAX_FIXED
, AML_NON_CACHEABLE
,
2245 0, entry
->base
, entry
->limit
, mmio64_offset
,
2246 entry
->limit
- entry
->base
+ 1));
2247 crs_range_insert(range_set
->mem_64bit_ranges
,
2248 entry
->base
, entry
->limit
);
2251 crs_range_set_free(&temp_range_set
);
2254 aml_word_bus_number(AML_MIN_FIXED
, AML_MAX_FIXED
, AML_POS_DECODE
,
2256 pci_bus_num(host
->bus
),
2259 max_bus
- pci_bus_num(host
->bus
) + 1));
2264 /* ACPI 5.0: 6.4.3.8.2 Serial Bus Connection Descriptors */
2265 static Aml
*aml_serial_bus_device(uint8_t serial_bus_type
, uint8_t flags
,
2266 uint16_t type_flags
,
2267 uint8_t revid
, uint16_t data_length
,
2268 uint16_t resource_source_len
)
2270 Aml
*var
= aml_alloc();
2271 uint16_t length
= data_length
+ resource_source_len
+ 9;
2273 build_append_byte(var
->buf
, 0x8e); /* Serial Bus Connection Descriptor */
2274 build_append_int_noprefix(var
->buf
, length
, sizeof(length
));
2275 build_append_byte(var
->buf
, 1); /* Revision ID */
2276 build_append_byte(var
->buf
, 0); /* Resource Source Index */
2277 build_append_byte(var
->buf
, serial_bus_type
); /* Serial Bus Type */
2278 build_append_byte(var
->buf
, flags
); /* General Flags */
2279 build_append_int_noprefix(var
->buf
, type_flags
, /* Type Specific Flags */
2280 sizeof(type_flags
));
2281 build_append_byte(var
->buf
, revid
); /* Type Specification Revision ID */
2282 build_append_int_noprefix(var
->buf
, data_length
, sizeof(data_length
));
2287 /* ACPI 5.0: 6.4.3.8.2.1 I2C Serial Bus Connection Resource Descriptor */
2288 Aml
*aml_i2c_serial_bus_device(uint16_t address
, const char *resource_source
)
2290 uint16_t resource_source_len
= strlen(resource_source
) + 1;
2291 Aml
*var
= aml_serial_bus_device(AML_SERIAL_BUS_TYPE_I2C
, 0, 0, 1,
2292 6, resource_source_len
);
2294 /* Connection Speed. Just set to 100K for now, it doesn't really matter. */
2295 build_append_int_noprefix(var
->buf
, 100000, 4);
2296 build_append_int_noprefix(var
->buf
, address
, sizeof(address
));
2298 /* This is a string, not a name, so just copy it directly in. */
2299 g_array_append_vals(var
->buf
, resource_source
, resource_source_len
);