qom: Add object_new_with_props() / object_new_withpropv() helpers
[qemu/ar7.git] / hw / acpi / aml-build.c
blob0d4b3247b7af5f96abbcf645d956e2017fe9185f
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 <glib/gprintf.h>
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <assert.h>
26 #include <stdbool.h>
27 #include <string.h>
28 #include "hw/acpi/aml-build.h"
29 #include "qemu/bswap.h"
30 #include "qemu/bitops.h"
31 #include "hw/acpi/bios-linker-loader.h"
33 static GArray *build_alloc_array(void)
35 return g_array_new(false, true /* clear */, 1);
38 static void build_free_array(GArray *array)
40 g_array_free(array, true);
43 static void build_prepend_byte(GArray *array, uint8_t val)
45 g_array_prepend_val(array, val);
48 static void build_append_byte(GArray *array, uint8_t val)
50 g_array_append_val(array, val);
53 static void build_append_array(GArray *array, GArray *val)
55 g_array_append_vals(array, val->data, val->len);
58 #define ACPI_NAMESEG_LEN 4
60 static void
61 build_append_nameseg(GArray *array, const char *seg)
63 int len;
65 len = strlen(seg);
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 char *s;
77 char **segs;
78 char **segs_iter;
79 int seg_count = 0;
81 s = g_strdup_vprintf(format, ap);
82 segs = g_strsplit(s, ".", 0);
83 g_free(s);
85 /* count segments */
86 segs_iter = segs;
87 while (*segs_iter) {
88 ++segs_iter;
89 ++seg_count;
92 * ACPI 5.0 spec: 20.2.2 Name Objects Encoding:
93 * "SegCount can be from 1 to 255"
95 assert(seg_count > 0 && seg_count <= 255);
97 /* handle RootPath || PrefixPath */
98 s = *segs;
99 while (*s == '\\' || *s == '^') {
100 build_append_byte(array, *s);
101 ++s;
104 switch (seg_count) {
105 case 1:
106 if (!*s) {
107 build_append_byte(array, 0x00); /* NullName */
108 } else {
109 build_append_nameseg(array, s);
111 break;
113 case 2:
114 build_append_byte(array, 0x2E); /* DualNamePrefix */
115 build_append_nameseg(array, s);
116 build_append_nameseg(array, segs[1]);
117 break;
118 default:
119 build_append_byte(array, 0x2F); /* MultiNamePrefix */
120 build_append_byte(array, seg_count);
122 /* handle the 1st segment manually due to prefix/root path */
123 build_append_nameseg(array, s);
125 /* add the rest of segments */
126 segs_iter = segs + 1;
127 while (*segs_iter) {
128 build_append_nameseg(array, *segs_iter);
129 ++segs_iter;
131 break;
133 g_strfreev(segs);
136 GCC_FMT_ATTR(2, 3)
137 static void build_append_namestring(GArray *array, const char *format, ...)
139 va_list ap;
141 va_start(ap, format);
142 build_append_namestringv(array, format, ap);
143 va_end(ap);
146 /* 5.4 Definition Block Encoding */
147 enum {
148 PACKAGE_LENGTH_1BYTE_SHIFT = 6, /* Up to 63 - use extra 2 bits. */
149 PACKAGE_LENGTH_2BYTE_SHIFT = 4,
150 PACKAGE_LENGTH_3BYTE_SHIFT = 12,
151 PACKAGE_LENGTH_4BYTE_SHIFT = 20,
154 static void
155 build_prepend_package_length(GArray *package, unsigned length, bool incl_self)
157 uint8_t byte;
158 unsigned length_bytes;
160 if (length + 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT)) {
161 length_bytes = 1;
162 } else if (length + 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT)) {
163 length_bytes = 2;
164 } else if (length + 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT)) {
165 length_bytes = 3;
166 } else {
167 length_bytes = 4;
171 * NamedField uses PkgLength encoding but it doesn't include length
172 * of PkgLength itself.
174 if (incl_self) {
176 * PkgLength is the length of the inclusive length of the data
177 * and PkgLength's length itself when used for terms with
178 * explitit length.
180 length += length_bytes;
183 switch (length_bytes) {
184 case 1:
185 byte = length;
186 build_prepend_byte(package, byte);
187 return;
188 case 4:
189 byte = length >> PACKAGE_LENGTH_4BYTE_SHIFT;
190 build_prepend_byte(package, byte);
191 length &= (1 << PACKAGE_LENGTH_4BYTE_SHIFT) - 1;
192 /* fall through */
193 case 3:
194 byte = length >> PACKAGE_LENGTH_3BYTE_SHIFT;
195 build_prepend_byte(package, byte);
196 length &= (1 << PACKAGE_LENGTH_3BYTE_SHIFT) - 1;
197 /* fall through */
198 case 2:
199 byte = length >> PACKAGE_LENGTH_2BYTE_SHIFT;
200 build_prepend_byte(package, byte);
201 length &= (1 << PACKAGE_LENGTH_2BYTE_SHIFT) - 1;
202 /* fall through */
205 * Most significant two bits of byte zero indicate how many following bytes
206 * are in PkgLength encoding.
208 byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length;
209 build_prepend_byte(package, byte);
212 static void
213 build_append_pkg_length(GArray *array, unsigned length, bool incl_self)
215 GArray *tmp = build_alloc_array();
217 build_prepend_package_length(tmp, length, incl_self);
218 build_append_array(array, tmp);
219 build_free_array(tmp);
222 static void build_package(GArray *package, uint8_t op)
224 build_prepend_package_length(package, package->len, true);
225 build_prepend_byte(package, op);
228 static void build_extop_package(GArray *package, uint8_t op)
230 build_package(package, op);
231 build_prepend_byte(package, 0x5B); /* ExtOpPrefix */
234 static void build_append_int_noprefix(GArray *table, uint64_t value, int size)
236 int i;
238 for (i = 0; i < size; ++i) {
239 build_append_byte(table, value & 0xFF);
240 value = value >> 8;
244 static void build_append_int(GArray *table, uint64_t value)
246 if (value == 0x00) {
247 build_append_byte(table, 0x00); /* ZeroOp */
248 } else if (value == 0x01) {
249 build_append_byte(table, 0x01); /* OneOp */
250 } else if (value <= 0xFF) {
251 build_append_byte(table, 0x0A); /* BytePrefix */
252 build_append_int_noprefix(table, value, 1);
253 } else if (value <= 0xFFFF) {
254 build_append_byte(table, 0x0B); /* WordPrefix */
255 build_append_int_noprefix(table, value, 2);
256 } else if (value <= 0xFFFFFFFF) {
257 build_append_byte(table, 0x0C); /* DWordPrefix */
258 build_append_int_noprefix(table, value, 4);
259 } else {
260 build_append_byte(table, 0x0E); /* QWordPrefix */
261 build_append_int_noprefix(table, value, 8);
265 static GPtrArray *alloc_list;
267 static Aml *aml_alloc(void)
269 Aml *var = g_new0(typeof(*var), 1);
271 g_ptr_array_add(alloc_list, var);
272 var->block_flags = AML_NO_OPCODE;
273 var->buf = build_alloc_array();
274 return var;
277 static Aml *aml_opcode(uint8_t op)
279 Aml *var = aml_alloc();
281 var->op = op;
282 var->block_flags = AML_OPCODE;
283 return var;
286 static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags)
288 Aml *var = aml_alloc();
290 var->op = op;
291 var->block_flags = flags;
292 return var;
295 static void aml_free(gpointer data, gpointer user_data)
297 Aml *var = data;
298 build_free_array(var->buf);
299 g_free(var);
302 Aml *init_aml_allocator(void)
304 Aml *var;
306 assert(!alloc_list);
307 alloc_list = g_ptr_array_new();
308 var = aml_alloc();
309 return var;
312 void free_aml_allocator(void)
314 g_ptr_array_foreach(alloc_list, aml_free, NULL);
315 g_ptr_array_free(alloc_list, true);
316 alloc_list = 0;
319 /* pack data with DefBuffer encoding */
320 static void build_buffer(GArray *array, uint8_t op)
322 GArray *data = build_alloc_array();
324 build_append_int(data, array->len);
325 g_array_prepend_vals(array, data->data, data->len);
326 build_free_array(data);
327 build_package(array, op);
330 void aml_append(Aml *parent_ctx, Aml *child)
332 GArray *buf = build_alloc_array();
333 build_append_array(buf, child->buf);
335 switch (child->block_flags) {
336 case AML_OPCODE:
337 build_append_byte(parent_ctx->buf, child->op);
338 break;
339 case AML_EXT_PACKAGE:
340 build_extop_package(buf, child->op);
341 break;
342 case AML_PACKAGE:
343 build_package(buf, child->op);
344 break;
345 case AML_RES_TEMPLATE:
346 build_append_byte(buf, 0x79); /* EndTag */
348 * checksum operations are treated as succeeded if checksum
349 * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag]
351 build_append_byte(buf, 0);
352 /* fall through, to pack resources in buffer */
353 case AML_BUFFER:
354 build_buffer(buf, child->op);
355 break;
356 case AML_NO_OPCODE:
357 break;
358 default:
359 assert(0);
360 break;
362 build_append_array(parent_ctx->buf, buf);
363 build_free_array(buf);
366 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */
367 Aml *aml_scope(const char *name_format, ...)
369 va_list ap;
370 Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE);
371 va_start(ap, name_format);
372 build_append_namestringv(var->buf, name_format, ap);
373 va_end(ap);
374 return var;
377 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */
378 Aml *aml_return(Aml *val)
380 Aml *var = aml_opcode(0xA4 /* ReturnOp */);
381 aml_append(var, val);
382 return var;
386 * ACPI 1.0b: 16.2.3 Data Objects Encoding:
387 * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp
389 Aml *aml_int(const uint64_t val)
391 Aml *var = aml_alloc();
392 build_append_int(var->buf, val);
393 return var;
397 * helper to construct NameString, which returns Aml object
398 * for using with aml_append or other aml_* terms
400 Aml *aml_name(const char *name_format, ...)
402 va_list ap;
403 Aml *var = aml_alloc();
404 va_start(ap, name_format);
405 build_append_namestringv(var->buf, name_format, ap);
406 va_end(ap);
407 return var;
410 /* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */
411 Aml *aml_name_decl(const char *name, Aml *val)
413 Aml *var = aml_opcode(0x08 /* NameOp */);
414 build_append_namestring(var->buf, "%s", name);
415 aml_append(var, val);
416 return var;
419 /* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */
420 Aml *aml_arg(int pos)
422 Aml *var;
423 uint8_t op = 0x68 /* ARG0 op */ + pos;
425 assert(pos <= 6);
426 var = aml_opcode(op);
427 return var;
430 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */
431 Aml *aml_store(Aml *val, Aml *target)
433 Aml *var = aml_opcode(0x70 /* StoreOp */);
434 aml_append(var, val);
435 aml_append(var, target);
436 return var;
439 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */
440 Aml *aml_and(Aml *arg1, Aml *arg2)
442 Aml *var = aml_opcode(0x7B /* AndOp */);
443 aml_append(var, arg1);
444 aml_append(var, arg2);
445 build_append_byte(var->buf, 0x00 /* NullNameOp */);
446 return var;
449 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefOr */
450 Aml *aml_or(Aml *arg1, Aml *arg2)
452 Aml *var = aml_opcode(0x7D /* OrOp */);
453 aml_append(var, arg1);
454 aml_append(var, arg2);
455 build_append_byte(var->buf, 0x00 /* NullNameOp */);
456 return var;
459 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftLeft */
460 Aml *aml_shiftleft(Aml *arg1, Aml *count)
462 Aml *var = aml_opcode(0x79 /* ShiftLeftOp */);
463 aml_append(var, arg1);
464 aml_append(var, count);
465 build_append_byte(var->buf, 0x00); /* NullNameOp */
466 return var;
469 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefShiftRight */
470 Aml *aml_shiftright(Aml *arg1, Aml *count)
472 Aml *var = aml_opcode(0x7A /* ShiftRightOp */);
473 aml_append(var, arg1);
474 aml_append(var, count);
475 build_append_byte(var->buf, 0x00); /* NullNameOp */
476 return var;
479 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLLess */
480 Aml *aml_lless(Aml *arg1, Aml *arg2)
482 Aml *var = aml_opcode(0x95 /* LLessOp */);
483 aml_append(var, arg1);
484 aml_append(var, arg2);
485 return var;
488 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAdd */
489 Aml *aml_add(Aml *arg1, Aml *arg2)
491 Aml *var = aml_opcode(0x72 /* AddOp */);
492 aml_append(var, arg1);
493 aml_append(var, arg2);
494 build_append_byte(var->buf, 0x00 /* NullNameOp */);
495 return var;
498 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIncrement */
499 Aml *aml_increment(Aml *arg)
501 Aml *var = aml_opcode(0x75 /* IncrementOp */);
502 aml_append(var, arg);
503 return var;
506 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefIndex */
507 Aml *aml_index(Aml *arg1, Aml *idx)
509 Aml *var = aml_opcode(0x88 /* IndexOp */);
510 aml_append(var, arg1);
511 aml_append(var, idx);
512 build_append_byte(var->buf, 0x00 /* NullNameOp */);
513 return var;
516 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */
517 Aml *aml_notify(Aml *arg1, Aml *arg2)
519 Aml *var = aml_opcode(0x86 /* NotifyOp */);
520 aml_append(var, arg1);
521 aml_append(var, arg2);
522 return var;
525 /* helper to call method with 1 argument */
526 Aml *aml_call1(const char *method, Aml *arg1)
528 Aml *var = aml_alloc();
529 build_append_namestring(var->buf, "%s", method);
530 aml_append(var, arg1);
531 return var;
534 /* helper to call method with 2 arguments */
535 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2)
537 Aml *var = aml_alloc();
538 build_append_namestring(var->buf, "%s", method);
539 aml_append(var, arg1);
540 aml_append(var, arg2);
541 return var;
544 /* helper to call method with 3 arguments */
545 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3)
547 Aml *var = aml_alloc();
548 build_append_namestring(var->buf, "%s", method);
549 aml_append(var, arg1);
550 aml_append(var, arg2);
551 aml_append(var, arg3);
552 return var;
555 /* helper to call method with 4 arguments */
556 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4)
558 Aml *var = aml_alloc();
559 build_append_namestring(var->buf, "%s", method);
560 aml_append(var, arg1);
561 aml_append(var, arg2);
562 aml_append(var, arg3);
563 aml_append(var, arg4);
564 return var;
568 * ACPI 1.0b: 6.4.3.4 32-Bit Fixed Location Memory Range Descriptor
569 * (Type 1, Large Item Name 0x6)
571 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size,
572 AmlReadAndWrite read_and_write)
574 Aml *var = aml_alloc();
575 build_append_byte(var->buf, 0x86); /* Memory32Fixed Resource Descriptor */
576 build_append_byte(var->buf, 9); /* Length, bits[7:0] value = 9 */
577 build_append_byte(var->buf, 0); /* Length, bits[15:8] value = 0 */
578 build_append_byte(var->buf, read_and_write); /* Write status, 1 rw 0 ro */
580 /* Range base address */
581 build_append_byte(var->buf, extract32(addr, 0, 8)); /* bits[7:0] */
582 build_append_byte(var->buf, extract32(addr, 8, 8)); /* bits[15:8] */
583 build_append_byte(var->buf, extract32(addr, 16, 8)); /* bits[23:16] */
584 build_append_byte(var->buf, extract32(addr, 24, 8)); /* bits[31:24] */
586 /* Range length */
587 build_append_byte(var->buf, extract32(size, 0, 8)); /* bits[7:0] */
588 build_append_byte(var->buf, extract32(size, 8, 8)); /* bits[15:8] */
589 build_append_byte(var->buf, extract32(size, 16, 8)); /* bits[23:16] */
590 build_append_byte(var->buf, extract32(size, 24, 8)); /* bits[31:24] */
591 return var;
595 * ACPI 5.0: 6.4.3.6 Extended Interrupt Descriptor
596 * Type 1, Large Item Name 0x9
598 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro,
599 AmlLevelAndEdge level_and_edge,
600 AmlActiveHighAndLow high_and_low, AmlShared shared,
601 uint32_t irq)
603 Aml *var = aml_alloc();
604 uint8_t irq_flags = con_and_pro | (level_and_edge << 1)
605 | (high_and_low << 2) | (shared << 3);
607 build_append_byte(var->buf, 0x89); /* Extended irq descriptor */
608 build_append_byte(var->buf, 6); /* Length, bits[7:0] minimum value = 6 */
609 build_append_byte(var->buf, 0); /* Length, bits[15:8] minimum value = 0 */
610 build_append_byte(var->buf, irq_flags); /* Interrupt Vector Information. */
611 build_append_byte(var->buf, 0x01); /* Interrupt table length = 1 */
613 /* Interrupt Number */
614 build_append_byte(var->buf, extract32(irq, 0, 8)); /* bits[7:0] */
615 build_append_byte(var->buf, extract32(irq, 8, 8)); /* bits[15:8] */
616 build_append_byte(var->buf, extract32(irq, 16, 8)); /* bits[23:16] */
617 build_append_byte(var->buf, extract32(irq, 24, 8)); /* bits[31:24] */
618 return var;
621 /* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */
622 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
623 uint8_t aln, uint8_t len)
625 Aml *var = aml_alloc();
626 build_append_byte(var->buf, 0x47); /* IO port descriptor */
627 build_append_byte(var->buf, dec);
628 build_append_byte(var->buf, min_base & 0xff);
629 build_append_byte(var->buf, (min_base >> 8) & 0xff);
630 build_append_byte(var->buf, max_base & 0xff);
631 build_append_byte(var->buf, (max_base >> 8) & 0xff);
632 build_append_byte(var->buf, aln);
633 build_append_byte(var->buf, len);
634 return var;
638 * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor
640 * More verbose description at:
641 * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro)
642 * 6.4.2.1 IRQ Descriptor
644 Aml *aml_irq_no_flags(uint8_t irq)
646 uint16_t irq_mask;
647 Aml *var = aml_alloc();
649 assert(irq < 16);
650 build_append_byte(var->buf, 0x22); /* IRQ descriptor 2 byte form */
652 irq_mask = 1U << irq;
653 build_append_byte(var->buf, irq_mask & 0xFF); /* IRQ mask bits[7:0] */
654 build_append_byte(var->buf, irq_mask >> 8); /* IRQ mask bits[15:8] */
655 return var;
658 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLNot */
659 Aml *aml_lnot(Aml *arg)
661 Aml *var = aml_opcode(0x92 /* LNotOp */);
662 aml_append(var, arg);
663 return var;
666 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */
667 Aml *aml_equal(Aml *arg1, Aml *arg2)
669 Aml *var = aml_opcode(0x93 /* LequalOp */);
670 aml_append(var, arg1);
671 aml_append(var, arg2);
672 return var;
675 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */
676 Aml *aml_if(Aml *predicate)
678 Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE);
679 aml_append(var, predicate);
680 return var;
683 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefElse */
684 Aml *aml_else(void)
686 Aml *var = aml_bundle(0xA1 /* ElseOp */, AML_PACKAGE);
687 return var;
690 /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefWhile */
691 Aml *aml_while(Aml *predicate)
693 Aml *var = aml_bundle(0xA2 /* WhileOp */, AML_PACKAGE);
694 aml_append(var, predicate);
695 return var;
698 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */
699 Aml *aml_method(const char *name, int arg_count)
701 Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE);
702 build_append_namestring(var->buf, "%s", name);
703 build_append_byte(var->buf, arg_count); /* MethodFlags: ArgCount */
704 return var;
707 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */
708 Aml *aml_device(const char *name_format, ...)
710 va_list ap;
711 Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE);
712 va_start(ap, name_format);
713 build_append_namestringv(var->buf, name_format, ap);
714 va_end(ap);
715 return var;
718 /* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */
719 Aml *aml_resource_template(void)
721 /* ResourceTemplate is a buffer of Resources with EndTag at the end */
722 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE);
723 return var;
726 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer
727 * Pass byte_list as NULL to request uninitialized buffer to reserve space.
729 Aml *aml_buffer(int buffer_size, uint8_t *byte_list)
731 int i;
732 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
734 for (i = 0; i < buffer_size; i++) {
735 if (byte_list == NULL) {
736 build_append_byte(var->buf, 0x0);
737 } else {
738 build_append_byte(var->buf, byte_list[i]);
742 return var;
745 /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */
746 Aml *aml_package(uint8_t num_elements)
748 Aml *var = aml_bundle(0x12 /* PackageOp */, AML_PACKAGE);
749 build_append_byte(var->buf, num_elements);
750 return var;
753 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */
754 Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
755 uint32_t offset, uint32_t len)
757 Aml *var = aml_alloc();
758 build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */
759 build_append_byte(var->buf, 0x80); /* OpRegionOp */
760 build_append_namestring(var->buf, "%s", name);
761 build_append_byte(var->buf, rs);
762 build_append_int(var->buf, offset);
763 build_append_int(var->buf, len);
764 return var;
767 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */
768 Aml *aml_named_field(const char *name, unsigned length)
770 Aml *var = aml_alloc();
771 build_append_nameseg(var->buf, name);
772 build_append_pkg_length(var->buf, length, false);
773 return var;
776 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */
777 Aml *aml_reserved_field(unsigned length)
779 Aml *var = aml_alloc();
780 /* ReservedField := 0x00 PkgLength */
781 build_append_byte(var->buf, 0x00);
782 build_append_pkg_length(var->buf, length, false);
783 return var;
786 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */
787 Aml *aml_field(const char *name, AmlAccessType type, AmlUpdateRule rule)
789 Aml *var = aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE);
790 uint8_t flags = rule << 5 | type;
792 build_append_namestring(var->buf, "%s", name);
793 build_append_byte(var->buf, flags);
794 return var;
797 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefCreateDWordField */
798 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name)
800 Aml *var = aml_alloc();
801 build_append_byte(var->buf, 0x8A); /* CreateDWordFieldOp */
802 aml_append(var, srcbuf);
803 aml_append(var, index);
804 build_append_namestring(var->buf, "%s", name);
805 return var;
808 /* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */
809 Aml *aml_string(const char *name_format, ...)
811 Aml *var = aml_opcode(0x0D /* StringPrefix */);
812 va_list ap;
813 char *s;
814 int len;
816 va_start(ap, name_format);
817 len = g_vasprintf(&s, name_format, ap);
818 va_end(ap);
820 g_array_append_vals(var->buf, s, len + 1);
821 g_free(s);
823 return var;
826 /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */
827 Aml *aml_local(int num)
829 Aml *var;
830 uint8_t op = 0x60 /* Local0Op */ + num;
832 assert(num <= 7);
833 var = aml_opcode(op);
834 return var;
837 /* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */
838 Aml *aml_varpackage(uint32_t num_elements)
840 Aml *var = aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE);
841 build_append_int(var->buf, num_elements);
842 return var;
845 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */
846 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
847 const char *name_format, ...)
849 va_list ap;
850 Aml *var = aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE);
851 va_start(ap, name_format);
852 build_append_namestringv(var->buf, name_format, ap);
853 va_end(ap);
854 build_append_byte(var->buf, proc_id); /* ProcID */
855 build_append_int_noprefix(var->buf, pblk_addr, sizeof(pblk_addr));
856 build_append_byte(var->buf, pblk_len); /* PblkLen */
857 return var;
860 static uint8_t Hex2Digit(char c)
862 if (c >= 'A') {
863 return c - 'A' + 10;
866 return c - '0';
869 /* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */
870 Aml *aml_eisaid(const char *str)
872 Aml *var = aml_alloc();
873 uint32_t id;
875 g_assert(strlen(str) == 7);
876 id = (str[0] - 0x40) << 26 |
877 (str[1] - 0x40) << 21 |
878 (str[2] - 0x40) << 16 |
879 Hex2Digit(str[3]) << 12 |
880 Hex2Digit(str[4]) << 8 |
881 Hex2Digit(str[5]) << 4 |
882 Hex2Digit(str[6]);
884 build_append_byte(var->buf, 0x0C); /* DWordPrefix */
885 build_append_int_noprefix(var->buf, bswap32(id), sizeof(id));
886 return var;
889 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */
890 static Aml *aml_as_desc_header(AmlResourceType type, AmlMinFixed min_fixed,
891 AmlMaxFixed max_fixed, AmlDecode dec,
892 uint8_t type_flags)
894 uint8_t flags = max_fixed | min_fixed | dec;
895 Aml *var = aml_alloc();
897 build_append_byte(var->buf, type);
898 build_append_byte(var->buf, flags);
899 build_append_byte(var->buf, type_flags); /* Type Specific Flags */
900 return var;
903 /* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */
904 static Aml *aml_word_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
905 AmlMaxFixed max_fixed, AmlDecode dec,
906 uint16_t addr_gran, uint16_t addr_min,
907 uint16_t addr_max, uint16_t addr_trans,
908 uint16_t len, uint8_t type_flags)
910 Aml *var = aml_alloc();
912 build_append_byte(var->buf, 0x88); /* Word Address Space Descriptor */
913 /* minimum length since we do not encode optional fields */
914 build_append_byte(var->buf, 0x0D);
915 build_append_byte(var->buf, 0x0);
917 aml_append(var,
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));
924 return var;
927 /* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */
928 static Aml *aml_dword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
929 AmlMaxFixed max_fixed, AmlDecode dec,
930 uint32_t addr_gran, uint32_t addr_min,
931 uint32_t addr_max, uint32_t addr_trans,
932 uint32_t len, uint8_t type_flags)
934 Aml *var = aml_alloc();
936 build_append_byte(var->buf, 0x87); /* DWord Address Space Descriptor */
937 /* minimum length since we do not encode optional fields */
938 build_append_byte(var->buf, 23);
939 build_append_byte(var->buf, 0x0);
942 aml_append(var,
943 aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
944 build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
945 build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
946 build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
947 build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
948 build_append_int_noprefix(var->buf, len, sizeof(len));
949 return var;
952 /* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */
953 static Aml *aml_qword_as_desc(AmlResourceType type, AmlMinFixed min_fixed,
954 AmlMaxFixed max_fixed, AmlDecode dec,
955 uint64_t addr_gran, uint64_t addr_min,
956 uint64_t addr_max, uint64_t addr_trans,
957 uint64_t len, uint8_t type_flags)
959 Aml *var = aml_alloc();
961 build_append_byte(var->buf, 0x8A); /* QWord Address Space Descriptor */
962 /* minimum length since we do not encode optional fields */
963 build_append_byte(var->buf, 0x2B);
964 build_append_byte(var->buf, 0x0);
966 aml_append(var,
967 aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags));
968 build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran));
969 build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min));
970 build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max));
971 build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans));
972 build_append_int_noprefix(var->buf, len, sizeof(len));
973 return var;
977 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
979 * More verbose description at:
980 * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro)
982 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
983 AmlDecode dec, uint16_t addr_gran,
984 uint16_t addr_min, uint16_t addr_max,
985 uint16_t addr_trans, uint16_t len)
988 return aml_word_as_desc(AML_BUS_NUMBER_RANGE, min_fixed, max_fixed, dec,
989 addr_gran, addr_min, addr_max, addr_trans, len, 0);
993 * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor
995 * More verbose description at:
996 * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro)
998 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
999 AmlDecode dec, AmlISARanges isa_ranges,
1000 uint16_t addr_gran, uint16_t addr_min,
1001 uint16_t addr_max, uint16_t addr_trans,
1002 uint16_t len)
1005 return aml_word_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1006 addr_gran, addr_min, addr_max, addr_trans, len,
1007 isa_ranges);
1011 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Descriptor
1013 * More verbose description at:
1014 * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro)
1016 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
1017 AmlDecode dec, AmlISARanges isa_ranges,
1018 uint32_t addr_gran, uint32_t addr_min,
1019 uint32_t addr_max, uint32_t addr_trans,
1020 uint32_t len)
1023 return aml_dword_as_desc(AML_IO_RANGE, min_fixed, max_fixed, dec,
1024 addr_gran, addr_min, addr_max, addr_trans, len,
1025 isa_ranges);
1029 * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor
1031 * More verbose description at:
1032 * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro)
1034 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1035 AmlMaxFixed max_fixed, AmlCacheable cacheable,
1036 AmlReadAndWrite read_and_write,
1037 uint32_t addr_gran, uint32_t addr_min,
1038 uint32_t addr_max, uint32_t addr_trans,
1039 uint32_t len)
1041 uint8_t flags = read_and_write | (cacheable << 1);
1043 return aml_dword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1044 dec, addr_gran, addr_min, addr_max,
1045 addr_trans, len, flags);
1049 * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor
1051 * More verbose description at:
1052 * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro)
1054 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed,
1055 AmlMaxFixed max_fixed, AmlCacheable cacheable,
1056 AmlReadAndWrite read_and_write,
1057 uint64_t addr_gran, uint64_t addr_min,
1058 uint64_t addr_max, uint64_t addr_trans,
1059 uint64_t len)
1061 uint8_t flags = read_and_write | (cacheable << 1);
1063 return aml_qword_as_desc(AML_MEMORY_RANGE, min_fixed, max_fixed,
1064 dec, addr_gran, addr_min, addr_max,
1065 addr_trans, len, flags);
1068 static uint8_t Hex2Byte(const char *src)
1070 int hi, lo;
1072 hi = Hex2Digit(src[0]);
1073 assert(hi >= 0);
1074 assert(hi <= 15);
1076 lo = Hex2Digit(src[1]);
1077 assert(lo >= 0);
1078 assert(lo <= 15);
1079 return (hi << 4) | lo;
1083 * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro)
1084 * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
1085 * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp");
1087 Aml *aml_touuid(const char *uuid)
1089 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1091 assert(strlen(uuid) == 36);
1092 assert(uuid[8] == '-');
1093 assert(uuid[13] == '-');
1094 assert(uuid[18] == '-');
1095 assert(uuid[23] == '-');
1097 build_append_byte(var->buf, Hex2Byte(uuid + 6)); /* dd - at offset 00 */
1098 build_append_byte(var->buf, Hex2Byte(uuid + 4)); /* cc - at offset 01 */
1099 build_append_byte(var->buf, Hex2Byte(uuid + 2)); /* bb - at offset 02 */
1100 build_append_byte(var->buf, Hex2Byte(uuid + 0)); /* aa - at offset 03 */
1102 build_append_byte(var->buf, Hex2Byte(uuid + 11)); /* ff - at offset 04 */
1103 build_append_byte(var->buf, Hex2Byte(uuid + 9)); /* ee - at offset 05 */
1105 build_append_byte(var->buf, Hex2Byte(uuid + 16)); /* hh - at offset 06 */
1106 build_append_byte(var->buf, Hex2Byte(uuid + 14)); /* gg - at offset 07 */
1108 build_append_byte(var->buf, Hex2Byte(uuid + 19)); /* ii - at offset 08 */
1109 build_append_byte(var->buf, Hex2Byte(uuid + 21)); /* jj - at offset 09 */
1111 build_append_byte(var->buf, Hex2Byte(uuid + 24)); /* kk - at offset 10 */
1112 build_append_byte(var->buf, Hex2Byte(uuid + 26)); /* ll - at offset 11 */
1113 build_append_byte(var->buf, Hex2Byte(uuid + 28)); /* mm - at offset 12 */
1114 build_append_byte(var->buf, Hex2Byte(uuid + 30)); /* nn - at offset 13 */
1115 build_append_byte(var->buf, Hex2Byte(uuid + 32)); /* oo - at offset 14 */
1116 build_append_byte(var->buf, Hex2Byte(uuid + 34)); /* pp - at offset 15 */
1118 return var;
1122 * ACPI 2.0b: 16.2.3.6.4.3 Unicode Macro (Convert Ascii String To Unicode)
1124 Aml *aml_unicode(const char *str)
1126 int i = 0;
1127 Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER);
1129 do {
1130 build_append_byte(var->buf, str[i]);
1131 build_append_byte(var->buf, 0);
1132 i++;
1133 } while (i <= strlen(str));
1135 return var;
1138 void
1139 build_header(GArray *linker, GArray *table_data,
1140 AcpiTableHeader *h, const char *sig, int len, uint8_t rev)
1142 memcpy(&h->signature, sig, 4);
1143 h->length = cpu_to_le32(len);
1144 h->revision = rev;
1145 memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6);
1146 memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4);
1147 memcpy(h->oem_table_id + 4, sig, 4);
1148 h->oem_revision = cpu_to_le32(1);
1149 memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4);
1150 h->asl_compiler_revision = cpu_to_le32(1);
1151 h->checksum = 0;
1152 /* Checksum to be filled in by Guest linker */
1153 bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE,
1154 table_data->data, h, len, &h->checksum);
1157 void *acpi_data_push(GArray *table_data, unsigned size)
1159 unsigned off = table_data->len;
1160 g_array_set_size(table_data, off + size);
1161 return table_data->data + off;
1164 unsigned acpi_data_len(GArray *table)
1166 #if GLIB_CHECK_VERSION(2, 22, 0)
1167 assert(g_array_get_element_size(table) == 1);
1168 #endif
1169 return table->len;
1172 void acpi_add_table(GArray *table_offsets, GArray *table_data)
1174 uint32_t offset = cpu_to_le32(table_data->len);
1175 g_array_append_val(table_offsets, offset);
1178 void acpi_build_tables_init(AcpiBuildTables *tables)
1180 tables->rsdp = g_array_new(false, true /* clear */, 1);
1181 tables->table_data = g_array_new(false, true /* clear */, 1);
1182 tables->tcpalog = g_array_new(false, true /* clear */, 1);
1183 tables->linker = bios_linker_loader_init();
1186 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre)
1188 void *linker_data = bios_linker_loader_cleanup(tables->linker);
1189 g_free(linker_data);
1190 g_array_free(tables->rsdp, true);
1191 g_array_free(tables->table_data, true);
1192 g_array_free(tables->tcpalog, mfre);
1195 /* Build rsdt table */
1196 void
1197 build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets)
1199 AcpiRsdtDescriptorRev1 *rsdt;
1200 size_t rsdt_len;
1201 int i;
1202 const int table_data_len = (sizeof(uint32_t) * table_offsets->len);
1204 rsdt_len = sizeof(*rsdt) + table_data_len;
1205 rsdt = acpi_data_push(table_data, rsdt_len);
1206 memcpy(rsdt->table_offset_entry, table_offsets->data, table_data_len);
1207 for (i = 0; i < table_offsets->len; ++i) {
1208 /* rsdt->table_offset_entry to be filled by Guest linker */
1209 bios_linker_loader_add_pointer(linker,
1210 ACPI_BUILD_TABLE_FILE,
1211 ACPI_BUILD_TABLE_FILE,
1212 table_data, &rsdt->table_offset_entry[i],
1213 sizeof(uint32_t));
1215 build_header(linker, table_data,
1216 (void *)rsdt, "RSDT", rsdt_len, 1);