From 19934e0e3d0f70eefe8d9e51e71c1cb80f4659d0 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Fri, 30 Jan 2015 13:29:36 +0000 Subject: acpi: move generic aml building helpers into dedictated file the will be later used for composing AML primitives and all that could be reused later for ARM machines as well. Signed-off-by: Igor Mammedov Acked-by: Marcel Apfelbaum Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 187 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 hw/acpi/aml-build.c (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c new file mode 100644 index 0000000000..b28c1f41a5 --- /dev/null +++ b/hw/acpi/aml-build.c @@ -0,0 +1,187 @@ +/* Support for generating ACPI tables and passing them to Guests + * + * Copyright (C) 2015 Red Hat Inc + * + * Author: Michael S. Tsirkin + * Author: Igor Mammedov + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +#include +#include +#include +#include +#include "hw/acpi/aml-build.h" + +GArray *build_alloc_array(void) +{ + return g_array_new(false, true /* clear */, 1); +} + +void build_free_array(GArray *array) +{ + g_array_free(array, true); +} + +void build_prepend_byte(GArray *array, uint8_t val) +{ + g_array_prepend_val(array, val); +} + +void build_append_byte(GArray *array, uint8_t val) +{ + g_array_append_val(array, val); +} + +void build_append_array(GArray *array, GArray *val) +{ + g_array_append_vals(array, val->data, val->len); +} + +#define ACPI_NAMESEG_LEN 4 + +void GCC_FMT_ATTR(2, 3) +build_append_nameseg(GArray *array, const char *format, ...) +{ + /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */ + char s[] = "XXXX"; + int len; + va_list args; + + va_start(args, format); + len = vsnprintf(s, sizeof s, format, args); + va_end(args); + + assert(len <= ACPI_NAMESEG_LEN); + + g_array_append_vals(array, s, len); + /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */ + g_array_append_vals(array, "____", ACPI_NAMESEG_LEN - len); +} + +/* 5.4 Definition Block Encoding */ +enum { + PACKAGE_LENGTH_1BYTE_SHIFT = 6, /* Up to 63 - use extra 2 bits. */ + PACKAGE_LENGTH_2BYTE_SHIFT = 4, + PACKAGE_LENGTH_3BYTE_SHIFT = 12, + PACKAGE_LENGTH_4BYTE_SHIFT = 20, +}; + +void build_prepend_package_length(GArray *package, unsigned min_bytes) +{ + uint8_t byte; + unsigned length = package->len; + unsigned length_bytes; + + if (length + 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT)) { + length_bytes = 1; + } else if (length + 2 < (1 << PACKAGE_LENGTH_3BYTE_SHIFT)) { + length_bytes = 2; + } else if (length + 3 < (1 << PACKAGE_LENGTH_4BYTE_SHIFT)) { + length_bytes = 3; + } else { + length_bytes = 4; + } + + /* Force length to at least min_bytes. + * This wastes memory but that's how bios did it. + */ + length_bytes = MAX(length_bytes, min_bytes); + + /* PkgLength is the length of the inclusive length of the data. */ + length += length_bytes; + + switch (length_bytes) { + case 1: + byte = length; + build_prepend_byte(package, byte); + return; + case 4: + byte = length >> PACKAGE_LENGTH_4BYTE_SHIFT; + build_prepend_byte(package, byte); + length &= (1 << PACKAGE_LENGTH_4BYTE_SHIFT) - 1; + /* fall through */ + case 3: + byte = length >> PACKAGE_LENGTH_3BYTE_SHIFT; + build_prepend_byte(package, byte); + length &= (1 << PACKAGE_LENGTH_3BYTE_SHIFT) - 1; + /* fall through */ + case 2: + byte = length >> PACKAGE_LENGTH_2BYTE_SHIFT; + build_prepend_byte(package, byte); + length &= (1 << PACKAGE_LENGTH_2BYTE_SHIFT) - 1; + /* fall through */ + } + /* + * Most significant two bits of byte zero indicate how many following bytes + * are in PkgLength encoding. + */ + byte = ((length_bytes - 1) << PACKAGE_LENGTH_1BYTE_SHIFT) | length; + build_prepend_byte(package, byte); +} + +void build_package(GArray *package, uint8_t op, unsigned min_bytes) +{ + build_prepend_package_length(package, min_bytes); + build_prepend_byte(package, op); +} + +void build_extop_package(GArray *package, uint8_t op) +{ + build_package(package, op, 1); + build_prepend_byte(package, 0x5B); /* ExtOpPrefix */ +} + +void build_append_value(GArray *table, uint32_t value, int size) +{ + uint8_t prefix; + int i; + + switch (size) { + case 1: + prefix = 0x0A; /* BytePrefix */ + break; + case 2: + prefix = 0x0B; /* WordPrefix */ + break; + case 4: + prefix = 0x0C; /* DWordPrefix */ + break; + default: + assert(0); + return; + } + build_append_byte(table, prefix); + for (i = 0; i < size; ++i) { + build_append_byte(table, value & 0xFF); + value = value >> 8; + } +} + +void build_append_int(GArray *table, uint32_t value) +{ + if (value == 0x00) { + build_append_byte(table, 0x00); /* ZeroOp */ + } else if (value == 0x01) { + build_append_byte(table, 0x01); /* OneOp */ + } else if (value <= 0xFF) { + build_append_value(table, value, 1); + } else if (value <= 0xFFFF) { + build_append_value(table, value, 2); + } else { + build_append_value(table, value, 4); + } +} + -- cgit 1.4.1 From eae8bded9a1a19561054654f5113aeb978d7b4c1 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Fri, 30 Jan 2015 13:29:37 +0000 Subject: acpi: add build_append_namestring() helper Use build_append_namestring() instead of build_append_nameseg() So user won't have to care whether name is NameSeg, NamePath or NameString. See for reference ACPI 5.0: 20.2.2 Name Objects Encoding Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 96 ++++++++++++++++++++++++++++++++++++++++----- hw/i386/acpi-build.c | 37 ++++++++--------- include/hw/acpi/aml-build.h | 2 +- 3 files changed, 105 insertions(+), 30 deletions(-) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index b28c1f41a5..6a02474e17 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "hw/acpi/aml-build.h" GArray *build_alloc_array(void) @@ -52,25 +53,102 @@ void build_append_array(GArray *array, GArray *val) #define ACPI_NAMESEG_LEN 4 -void GCC_FMT_ATTR(2, 3) -build_append_nameseg(GArray *array, const char *format, ...) +static void +build_append_nameseg(GArray *array, const char *seg) { /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */ - char s[] = "XXXX"; int len; - va_list args; - - va_start(args, format); - len = vsnprintf(s, sizeof s, format, args); - va_end(args); + len = strlen(seg); assert(len <= ACPI_NAMESEG_LEN); - g_array_append_vals(array, s, len); + g_array_append_vals(array, seg, len); /* Pad up to ACPI_NAMESEG_LEN characters if necessary. */ g_array_append_vals(array, "____", ACPI_NAMESEG_LEN - len); } +static void +build_append_namestringv(GArray *array, const char *format, va_list ap) +{ + /* It would be nicer to use g_string_vprintf but it's only there in 2.22 */ + char *s; + int len; + va_list va_len; + char **segs; + char **segs_iter; + int seg_count = 0; + + va_copy(va_len, ap); + len = vsnprintf(NULL, 0, format, va_len); + va_end(va_len); + len += 1; + s = g_new(typeof(*s), len); + + len = vsnprintf(s, len, format, ap); + + segs = g_strsplit(s, ".", 0); + g_free(s); + + /* count segments */ + segs_iter = segs; + while (*segs_iter) { + ++segs_iter; + ++seg_count; + } + /* + * ACPI 5.0 spec: 20.2.2 Name Objects Encoding: + * "SegCount can be from 1 to 255" + */ + assert(seg_count > 0 && seg_count <= 255); + + /* handle RootPath || PrefixPath */ + s = *segs; + while (*s == '\\' || *s == '^') { + build_append_byte(array, *s); + ++s; + } + + switch (seg_count) { + case 1: + if (!*s) { + build_append_byte(array, 0x0); /* NullName */ + } else { + build_append_nameseg(array, s); + } + break; + + case 2: + build_append_byte(array, 0x2E); /* DualNamePrefix */ + build_append_nameseg(array, s); + build_append_nameseg(array, segs[1]); + break; + default: + build_append_byte(array, 0x2F); /* MultiNamePrefix */ + build_append_byte(array, seg_count); + + /* handle the 1st segment manually due to prefix/root path */ + build_append_nameseg(array, s); + + /* add the rest of segments */ + segs_iter = segs + 1; + while (*segs_iter) { + build_append_nameseg(array, *segs_iter); + ++segs_iter; + } + break; + } + g_strfreev(segs); +} + +void build_append_namestring(GArray *array, const char *format, ...) +{ + va_list ap; + + va_start(ap, format); + build_append_namestringv(array, format, ap); + va_end(ap); +} + /* 5.4 Definition Block Encoding */ enum { PACKAGE_LENGTH_1BYTE_SHIFT = 6, /* Up to 63 - use extra 2 bits. */ diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index cc638a9ab0..df2bad0a3b 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -286,7 +286,7 @@ static GArray *build_alloc_method(const char *name, uint8_t arg_count) { GArray *method = build_alloc_array(); - build_append_nameseg(method, "%s", name); + build_append_namestring(method, "%s", name); build_append_byte(method, arg_count); /* MethodFlags: ArgCount */ return method; @@ -578,7 +578,7 @@ build_append_notify_method(GArray *device, const char *name, for (i = 0; i < count; i++) { GArray *target = build_alloc_array(); - build_append_nameseg(target, format, i); + build_append_namestring(target, format, i); assert(i < 256); /* Fits in 1 byte */ build_append_notify_target_ifequal(method, target, i, 1); build_free_array(target); @@ -709,24 +709,24 @@ static void build_pci_bus_end(PCIBus *bus, void *bus_state) if (bus->parent_dev) { op = 0x82; /* DeviceOp */ - build_append_nameseg(bus_table, "S%.02X", + build_append_namestring(bus_table, "S%.02X", bus->parent_dev->devfn); build_append_byte(bus_table, 0x08); /* NameOp */ - build_append_nameseg(bus_table, "_SUN"); + build_append_namestring(bus_table, "_SUN"); build_append_value(bus_table, PCI_SLOT(bus->parent_dev->devfn), 1); build_append_byte(bus_table, 0x08); /* NameOp */ - build_append_nameseg(bus_table, "_ADR"); + build_append_namestring(bus_table, "_ADR"); build_append_value(bus_table, (PCI_SLOT(bus->parent_dev->devfn) << 16) | PCI_FUNC(bus->parent_dev->devfn), 4); } else { op = 0x10; /* ScopeOp */; - build_append_nameseg(bus_table, "PCI0"); + build_append_namestring(bus_table, "PCI0"); } bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL); if (bsel) { build_append_byte(bus_table, 0x08); /* NameOp */ - build_append_nameseg(bus_table, "BSEL"); + build_append_namestring(bus_table, "BSEL"); build_append_int(bus_table, qint_get_int(qobject_to_qint(bsel))); memset(slot_hotplug_enable, 0xff, sizeof slot_hotplug_enable); } else { @@ -829,7 +829,7 @@ static void build_pci_bus_end(PCIBus *bus, void *bus_state) build_append_int(notify, 0x1U << i); build_append_byte(notify, 0x00); /* NullName */ build_append_byte(notify, 0x86); /* NotifyOp */ - build_append_nameseg(notify, "S%.02X", PCI_DEVFN(i, 0)); + build_append_namestring(notify, "S%.02X", PCI_DEVFN(i, 0)); build_append_byte(notify, 0x69); /* Arg1Op */ /* Pack it up */ @@ -853,12 +853,12 @@ static void build_pci_bus_end(PCIBus *bus, void *bus_state) if (bsel) { build_append_byte(method, 0x70); /* StoreOp */ build_append_int(method, qint_get_int(qobject_to_qint(bsel))); - build_append_nameseg(method, "BNUM"); - build_append_nameseg(method, "DVNT"); - build_append_nameseg(method, "PCIU"); + build_append_namestring(method, "BNUM"); + build_append_namestring(method, "DVNT"); + build_append_namestring(method, "PCIU"); build_append_int(method, 1); /* Device Check */ - build_append_nameseg(method, "DVNT"); - build_append_nameseg(method, "PCID"); + build_append_namestring(method, "DVNT"); + build_append_namestring(method, "PCID"); build_append_int(method, 3); /* Eject Request */ } @@ -884,11 +884,8 @@ static void build_pci_bus_end(PCIBus *bus, void *bus_state) * At the moment this is not needed for root as we have a single root. */ if (bus->parent_dev) { - build_append_byte(parent->notify_table, '^'); /* ParentPrefixChar */ - build_append_byte(parent->notify_table, 0x2E); /* DualNamePrefix */ - build_append_nameseg(parent->notify_table, "S%.02X", - bus->parent_dev->devfn); - build_append_nameseg(parent->notify_table, "PCNT"); + build_append_namestring(parent->notify_table, "^PCNT.S%.02X", + bus->parent_dev->devfn); } } @@ -956,7 +953,7 @@ build_ssdt(GArray *table_data, GArray *linker, GArray *sb_scope = build_alloc_array(); uint8_t op = 0x10; /* ScopeOp */ - build_append_nameseg(sb_scope, "_SB"); + build_append_namestring(sb_scope, "_SB"); /* build Processor object for each processor */ for (i = 0; i < acpi_cpus; i++) { @@ -976,7 +973,7 @@ build_ssdt(GArray *table_data, GArray *linker, /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })" */ build_append_byte(sb_scope, 0x08); /* NameOp */ - build_append_nameseg(sb_scope, "CPON"); + build_append_namestring(sb_scope, "CPON"); { GArray *package = build_alloc_array(); diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index e6a0b28990..fd506252bd 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -12,7 +12,7 @@ void build_append_byte(GArray *array, uint8_t val); void build_append_array(GArray *array, GArray *val); void GCC_FMT_ATTR(2, 3) -build_append_nameseg(GArray *array, const char *format, ...); +build_append_namestring(GArray *array, const char *format, ...); void build_prepend_package_length(GArray *package, unsigned min_bytes); void build_package(GArray *package, uint8_t op, unsigned min_bytes); -- cgit 1.4.1 From 661875e948ece4723ebe4e7628060c27cad06df1 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Fri, 30 Jan 2015 13:29:38 +0000 Subject: acpi: drop min-bytes in build_package() Signed-off-by: Igor Mammedov Reviewed-by: Claudio Fontana Reviewed-by: Marcel Apfelbaum Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 14 ++++---------- hw/i386/acpi-build.c | 13 ++++++------- include/hw/acpi/aml-build.h | 4 ++-- 3 files changed, 12 insertions(+), 19 deletions(-) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 6a02474e17..bcb288eab4 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -157,7 +157,7 @@ enum { PACKAGE_LENGTH_4BYTE_SHIFT = 20, }; -void build_prepend_package_length(GArray *package, unsigned min_bytes) +void build_prepend_package_length(GArray *package) { uint8_t byte; unsigned length = package->len; @@ -173,11 +173,6 @@ void build_prepend_package_length(GArray *package, unsigned min_bytes) length_bytes = 4; } - /* Force length to at least min_bytes. - * This wastes memory but that's how bios did it. - */ - length_bytes = MAX(length_bytes, min_bytes); - /* PkgLength is the length of the inclusive length of the data. */ length += length_bytes; @@ -210,15 +205,15 @@ void build_prepend_package_length(GArray *package, unsigned min_bytes) build_prepend_byte(package, byte); } -void build_package(GArray *package, uint8_t op, unsigned min_bytes) +void build_package(GArray *package, uint8_t op) { - build_prepend_package_length(package, min_bytes); + build_prepend_package_length(package); build_prepend_byte(package, op); } void build_extop_package(GArray *package, uint8_t op) { - build_package(package, op, 1); + build_package(package, op); build_prepend_byte(package, 0x5B); /* ExtOpPrefix */ } @@ -262,4 +257,3 @@ void build_append_int(GArray *table, uint32_t value) build_append_value(table, value, 4); } } - diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index df2bad0a3b..5737ed061c 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -296,7 +296,7 @@ static void build_append_and_cleanup_method(GArray *device, GArray *method) { uint8_t op = 0x14; /* MethodOp */ - build_package(method, op, 0); + build_package(method, op); build_append_array(device, method); build_free_array(method); @@ -317,7 +317,7 @@ static void build_append_notify_target_ifequal(GArray *method, build_append_byte(notify, 0x69); /* Arg1Op */ /* Pack it up */ - build_package(notify, op, 1); + build_package(notify, op); build_append_array(method, notify); @@ -833,7 +833,7 @@ static void build_pci_bus_end(PCIBus *bus, void *bus_state) build_append_byte(notify, 0x69); /* Arg1Op */ /* Pack it up */ - build_package(notify, op, 0); + build_package(notify, op); build_append_array(method, notify); @@ -874,7 +874,7 @@ static void build_pci_bus_end(PCIBus *bus, void *bus_state) if (bus->parent_dev) { build_extop_package(bus_table, op); } else { - build_package(bus_table, op, 0); + build_package(bus_table, op); } /* Append our bus description to parent table */ @@ -997,7 +997,7 @@ build_ssdt(GArray *table_data, GArray *linker, build_append_byte(package, b); } - build_package(package, op, 2); + build_package(package, op); build_append_array(sb_scope, package); build_free_array(package); } @@ -1045,8 +1045,7 @@ build_ssdt(GArray *table_data, GArray *linker, build_append_array(sb_scope, hotplug_state.device_table); build_pci_bus_state_cleanup(&hotplug_state); } - - build_package(sb_scope, op, 3); + build_package(sb_scope, op); build_append_array(table_data, sb_scope); build_free_array(sb_scope); } diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index fd506252bd..199f003265 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -14,8 +14,8 @@ void build_append_array(GArray *array, GArray *val); void GCC_FMT_ATTR(2, 3) build_append_namestring(GArray *array, const char *format, ...); -void build_prepend_package_length(GArray *package, unsigned min_bytes); -void build_package(GArray *package, uint8_t op, unsigned min_bytes); +void build_prepend_package_length(GArray *package); +void build_package(GArray *package, uint8_t op); void build_append_value(GArray *table, uint32_t value, int size); void build_append_int(GArray *table, uint32_t value); void build_extop_package(GArray *package, uint8_t op); -- cgit 1.4.1 From 0f2707e4e733614f2cd566e7210c1cff6f3b5b42 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:14 +0000 Subject: acpi: introduce AML composer aml_append() Adds for dynamic AML creation, which will be used for piecing ASL/AML primitives together and hiding from user/caller details about how nested context should be closed/packed leaving less space for mistakes and necessity to know how AML should be encoded, allowing user to concentrate on ASL representation instead. For example it will allow to create AML like this: init_aml_allocator(); ... Aml *scope = aml_scope("PCI0") Aml *dev = aml_device("PM") aml_append(dev, aml_name_decl("_ADR", aml_int(addr))) aml_append(scope, dev); ... free_aml_allocator(); Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 78 +++++++++++++++++++++++++++++++++++++++++++++ include/hw/acpi/aml-build.h | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index bcb288eab4..caf792b5d2 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -25,6 +25,7 @@ #include #include #include "hw/acpi/aml-build.h" +#include "qemu/bswap.h" GArray *build_alloc_array(void) { @@ -257,3 +258,80 @@ void build_append_int(GArray *table, uint32_t value) build_append_value(table, value, 4); } } + +static GPtrArray *alloc_list; + +static Aml *aml_alloc(void) +{ + Aml *var = g_new0(typeof(*var), 1); + + g_ptr_array_add(alloc_list, var); + var->block_flags = AML_NO_OPCODE; + var->buf = build_alloc_array(); + return var; +} + +static void aml_free(gpointer data) +{ + Aml *var = data; + build_free_array(var->buf); +} + +Aml *init_aml_allocator(void) +{ + Aml *var; + + assert(!alloc_list); + alloc_list = g_ptr_array_new_with_free_func(aml_free); + var = aml_alloc(); + return var; +} + +void free_aml_allocator(void) +{ + g_ptr_array_free(alloc_list, true); + alloc_list = 0; +} + +/* pack data with DefBuffer encoding */ +static void build_buffer(GArray *array, uint8_t op) +{ + GArray *data = build_alloc_array(); + + build_append_int(data, array->len); + g_array_prepend_vals(array, data->data, data->len); + build_free_array(data); + build_package(array, op); +} + +void aml_append(Aml *parent_ctx, Aml *child) +{ + switch (child->block_flags) { + case AML_OPCODE: + build_append_byte(parent_ctx->buf, child->op); + break; + case AML_EXT_PACKAGE: + build_extop_package(child->buf, child->op); + break; + case AML_PACKAGE: + build_package(child->buf, child->op); + break; + case AML_RES_TEMPLATE: + build_append_byte(child->buf, 0x79); /* EndTag */ + /* + * checksum operations are treated as succeeded if checksum + * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag] + */ + build_append_byte(child->buf, 0); + /* fall through, to pack resources in buffer */ + case AML_BUFFER: + build_buffer(child->buf, child->op); + break; + case AML_NO_OPCODE: + break; + default: + assert(0); + break; + } + build_append_array(parent_ctx->buf, child->buf); +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 199f003265..1e1b03bc4e 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -5,6 +5,61 @@ #include #include "qemu/compiler.h" +typedef enum { + AML_NO_OPCODE = 0,/* has only data */ + AML_OPCODE, /* has opcode optionally followed by data */ + AML_PACKAGE, /* has opcode and uses PkgLength for its length */ + AML_EXT_PACKAGE, /* ame as AML_PACKAGE but also has 'ExOpPrefix' */ + AML_BUFFER, /* data encoded as 'DefBuffer' */ + AML_RES_TEMPLATE, /* encoded as ResourceTemplate macro */ +} AmlBlockFlags; + +struct Aml { + GArray *buf; + + /*< private >*/ + uint8_t op; + AmlBlockFlags block_flags; +}; +typedef struct Aml Aml; + +/** + * init_aml_allocator: + * + * Called for initializing API allocator which allow to use + * AML API. + * Returns: toplevel container which accumulates all other + * AML elements for a table. + */ +Aml *init_aml_allocator(void); + +/** + * free_aml_allocator: + * + * Releases all elements used by AML API, frees associated memory + * and invalidates AML allocator. After this call @init_aml_allocator + * should be called again if AML API is to be used again. + */ +void free_aml_allocator(void); + +/** + * aml_append: + * @parent_ctx: context to which @child element is added + * @child: element that is copied into @parent_ctx context + * + * Joins Aml elements together and helps to construct AML tables + * Examle of usage: + * Aml *table = aml_def_block("SSDT", ...); + * Aml *sb = aml_scope("\_SB"); + * Aml *dev = aml_device("PCI0"); + * + * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03"))); + * aml_append(sb, dev); + * aml_append(table, sb); + */ +void aml_append(Aml *parent_ctx, Aml *child); + +/* other helpers */ GArray *build_alloc_array(void); void build_free_array(GArray *array); void build_prepend_byte(GArray *array, uint8_t val); -- cgit 1.4.1 From 2ef7c27b78c663557771076b45e6f4be1b3c78d6 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:15 +0000 Subject: acpi: add aml_scope() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 20 ++++++++++++++++++++ include/hw/acpi/aml-build.h | 3 +++ 2 files changed, 23 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index caf792b5d2..f891406d5f 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -271,6 +271,15 @@ static Aml *aml_alloc(void) return var; } +static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags) +{ + Aml *var = aml_alloc(); + + var->op = op; + var->block_flags = flags; + return var; +} + static void aml_free(gpointer data) { Aml *var = data; @@ -335,3 +344,14 @@ void aml_append(Aml *parent_ctx, Aml *child) } build_append_array(parent_ctx->buf, child->buf); } + +/* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefScope */ +Aml *aml_scope(const char *name_format, ...) +{ + va_list ap; + Aml *var = aml_bundle(0x10 /* ScopeOp */, AML_PACKAGE); + va_start(ap, name_format); + build_append_namestringv(var->buf, name_format, ap); + va_end(ap); + return var; +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 1e1b03bc4e..05dd3fcbb2 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -59,6 +59,9 @@ void free_aml_allocator(void); */ void aml_append(Aml *parent_ctx, Aml *child); +/* Block AML object primitives */ +Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); + /* other helpers */ GArray *build_alloc_array(void); void build_free_array(GArray *array); -- cgit 1.4.1 From be06ebd0a4d42ccd3d79fae24e48c264be377356 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:17 +0000 Subject: acpi: add aml_device() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 11 +++++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 12 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index f891406d5f..c955d664ae 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -355,3 +355,14 @@ Aml *aml_scope(const char *name_format, ...) va_end(ap); return var; } + +/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */ +Aml *aml_device(const char *name_format, ...) +{ + va_list ap; + Aml *var = aml_bundle(0x82 /* DeviceOp */, AML_EXT_PACKAGE); + va_start(ap, name_format); + build_append_namestringv(var->buf, name_format, ap); + va_end(ap); + return var; +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 05dd3fcbb2..004fa05cdc 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -61,6 +61,7 @@ void aml_append(Aml *parent_ctx, Aml *child); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); +Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); /* other helpers */ GArray *build_alloc_array(void); -- cgit 1.4.1 From ea2407d7e8adae1ab6cd0ca0366752d101d2344f Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:18 +0000 Subject: acpi: add aml_method() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 9 +++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 10 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index c955d664ae..4e0685e37a 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -356,6 +356,15 @@ Aml *aml_scope(const char *name_format, ...) return var; } +/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */ +Aml *aml_method(const char *name, int arg_count) +{ + Aml *var = aml_bundle(0x14 /* MethodOp */, AML_PACKAGE); + build_append_namestring(var->buf, "%s", name); + build_append_byte(var->buf, arg_count); /* MethodFlags: ArgCount */ + return var; +} + /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefDevice */ Aml *aml_device(const char *name_format, ...) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 004fa05cdc..97e210a1f3 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -62,6 +62,7 @@ void aml_append(Aml *parent_ctx, Aml *child); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); +Aml *aml_method(const char *name, int arg_count); /* other helpers */ GArray *build_alloc_array(void); -- cgit 1.4.1 From 32acac9eb3478cb1229654883f938ab21d28d998 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:19 +0000 Subject: acpi: add aml_if() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 8 ++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 9 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 4e0685e37a..d19d1fb74a 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -356,6 +356,14 @@ Aml *aml_scope(const char *name_format, ...) return var; } +/* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ +Aml *aml_if(Aml *predicate) +{ + Aml *var = aml_bundle(0xA0 /* IfOp */, AML_PACKAGE); + aml_append(var, predicate); + return var; +} + /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMethod */ Aml *aml_method(const char *name, int arg_count) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 97e210a1f3..13cc9e9db9 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -63,6 +63,7 @@ void aml_append(Aml *parent_ctx, Aml *child); Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_method(const char *name, int arg_count); +Aml *aml_if(Aml *predicate); /* other helpers */ GArray *build_alloc_array(void); -- cgit 1.4.1 From 3c054bd51a132a69e5180f8c6ffa9d46724e4a50 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:20 +0000 Subject: acpi: add aml_name() & aml_name_decl() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 32 ++++++++++++++++++++++++++++++++ include/hw/acpi/aml-build.h | 4 ++++ 2 files changed, 36 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index d19d1fb74a..cb00a1ba80 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -271,6 +271,15 @@ static Aml *aml_alloc(void) return var; } +static Aml *aml_opcode(uint8_t op) +{ + Aml *var = aml_alloc(); + + var->op = op; + var->block_flags = AML_OPCODE; + return var; +} + static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags) { Aml *var = aml_alloc(); @@ -356,6 +365,29 @@ Aml *aml_scope(const char *name_format, ...) return var; } +/* + * helper to construct NameString, which returns Aml object + * for using with aml_append or other aml_* terms + */ +Aml *aml_name(const char *name_format, ...) +{ + va_list ap; + Aml *var = aml_alloc(); + va_start(ap, name_format); + build_append_namestringv(var->buf, name_format, ap); + va_end(ap); + return var; +} + +/* ACPI 1.0b: 16.2.5.1 Namespace Modifier Objects Encoding: DefName */ +Aml *aml_name_decl(const char *name, Aml *val) +{ + Aml *var = aml_opcode(0x08 /* NameOp */); + build_append_namestring(var->buf, "%s", name); + aml_append(var, val); + return var; +} + /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ Aml *aml_if(Aml *predicate) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 13cc9e9db9..946aece4ba 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -59,6 +59,10 @@ void free_aml_allocator(void); */ void aml_append(Aml *parent_ctx, Aml *child); +/* non block AML object primitives */ +Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); +Aml *aml_name_decl(const char *name, Aml *val); + /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From 295a515df0df655a902df6ebfc301096a3ee88ed Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:21 +0000 Subject: acpi: add aml_int() term * factor out ACPI const int packing out of build_append_value() and rename build_append_value() to build_append_int_noprefix() it will be reused for adding a plain integer value into AML. will be used by is aml_processor() and CRS macro helpers * extend build_append_int{_noprefix}() to support 64-bit values it will be used PCI for generating 64bit _CRS entries Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 43 ++++++++++++++++++++++--------------------- hw/i386/acpi-build.c | 12 ++++++------ include/hw/acpi/aml-build.h | 4 ++-- 3 files changed, 30 insertions(+), 29 deletions(-) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index cb00a1ba80..aaa80e575c 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -218,44 +218,34 @@ void build_extop_package(GArray *package, uint8_t op) build_prepend_byte(package, 0x5B); /* ExtOpPrefix */ } -void build_append_value(GArray *table, uint32_t value, int size) +static void build_append_int_noprefix(GArray *table, uint64_t value, int size) { - uint8_t prefix; int i; - switch (size) { - case 1: - prefix = 0x0A; /* BytePrefix */ - break; - case 2: - prefix = 0x0B; /* WordPrefix */ - break; - case 4: - prefix = 0x0C; /* DWordPrefix */ - break; - default: - assert(0); - return; - } - build_append_byte(table, prefix); for (i = 0; i < size; ++i) { build_append_byte(table, value & 0xFF); value = value >> 8; } } -void build_append_int(GArray *table, uint32_t value) +void build_append_int(GArray *table, uint64_t value) { if (value == 0x00) { build_append_byte(table, 0x00); /* ZeroOp */ } else if (value == 0x01) { build_append_byte(table, 0x01); /* OneOp */ } else if (value <= 0xFF) { - build_append_value(table, value, 1); + build_append_byte(table, 0x0A); /* BytePrefix */ + build_append_int_noprefix(table, value, 1); } else if (value <= 0xFFFF) { - build_append_value(table, value, 2); + build_append_byte(table, 0x0B); /* WordPrefix */ + build_append_int_noprefix(table, value, 2); + } else if (value <= 0xFFFFFFFF) { + build_append_byte(table, 0x0C); /* DWordPrefix */ + build_append_int_noprefix(table, value, 4); } else { - build_append_value(table, value, 4); + build_append_byte(table, 0x0E); /* QWordPrefix */ + build_append_int_noprefix(table, value, 8); } } @@ -365,6 +355,17 @@ Aml *aml_scope(const char *name_format, ...) return var; } +/* + * ACPI 1.0b: 16.2.3 Data Objects Encoding: + * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp + */ +Aml *aml_int(const uint64_t val) +{ + Aml *var = aml_alloc(); + build_append_int(var->buf, val); + return var; +} + /* * helper to construct NameString, which returns Aml object * for using with aml_append or other aml_* terms diff --git a/hw/i386/acpi-build.c b/hw/i386/acpi-build.c index c66fe568c1..bf344151f5 100644 --- a/hw/i386/acpi-build.c +++ b/hw/i386/acpi-build.c @@ -304,14 +304,14 @@ static void build_append_and_cleanup_method(GArray *device, GArray *method) static void build_append_notify_target_ifequal(GArray *method, GArray *target_name, - uint32_t value, int size) + uint32_t value) { GArray *notify = build_alloc_array(); uint8_t op = 0xA0; /* IfOp */ build_append_byte(notify, 0x93); /* LEqualOp */ build_append_byte(notify, 0x68); /* Arg0Op */ - build_append_value(notify, value, size); + build_append_int(notify, value); build_append_byte(notify, 0x86); /* NotifyOp */ build_append_array(notify, target_name); build_append_byte(notify, 0x69); /* Arg1Op */ @@ -580,7 +580,7 @@ build_append_notify_method(GArray *device, const char *name, GArray *target = build_alloc_array(); build_append_namestring(target, format, i); assert(i < 256); /* Fits in 1 byte */ - build_append_notify_target_ifequal(method, target, i, 1); + build_append_notify_target_ifequal(method, target, i); build_free_array(target); } @@ -715,11 +715,11 @@ static void build_pci_bus_end(PCIBus *bus, void *bus_state) bus->parent_dev->devfn); build_append_byte(bus_table, 0x08); /* NameOp */ build_append_namestring(bus_table, "_SUN"); - build_append_value(bus_table, PCI_SLOT(bus->parent_dev->devfn), 1); + build_append_int(bus_table, PCI_SLOT(bus->parent_dev->devfn)); build_append_byte(bus_table, 0x08); /* NameOp */ build_append_namestring(bus_table, "_ADR"); - build_append_value(bus_table, (PCI_SLOT(bus->parent_dev->devfn) << 16) | - PCI_FUNC(bus->parent_dev->devfn), 4); + build_append_int(bus_table, (PCI_SLOT(bus->parent_dev->devfn) << 16) | + PCI_FUNC(bus->parent_dev->devfn)); } else { op = 0x10; /* ScopeOp */; build_append_namestring(bus_table, "PCI0"); diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 946aece4ba..a385132b8e 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -62,6 +62,7 @@ void aml_append(Aml *parent_ctx, Aml *child); /* non block AML object primitives */ Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_name_decl(const char *name, Aml *val); +Aml *aml_int(const uint64_t val); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); @@ -81,8 +82,7 @@ build_append_namestring(GArray *array, const char *format, ...); void build_prepend_package_length(GArray *package); void build_package(GArray *package, uint8_t op); -void build_append_value(GArray *table, uint32_t value, int size); -void build_append_int(GArray *table, uint32_t value); +void build_append_int(GArray *table, uint64_t value); void build_extop_package(GArray *package, uint8_t op); #endif -- cgit 1.4.1 From b25af5ad5953c9587e59c82cc668e574384f5245 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:22 +0000 Subject: acpi: add aml_return() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 8 ++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 9 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index aaa80e575c..8207d9c057 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -355,6 +355,14 @@ Aml *aml_scope(const char *name_format, ...) return var; } +/* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefReturn */ +Aml *aml_return(Aml *val) +{ + Aml *var = aml_opcode(0xA4 /* ReturnOp */); + aml_append(var, val); + return var; +} + /* * ACPI 1.0b: 16.2.3 Data Objects Encoding: * encodes: ByteConst, WordConst, DWordConst, QWordConst, ZeroOp, OneOp diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index a385132b8e..e7fa9771b1 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -62,6 +62,7 @@ void aml_append(Aml *parent_ctx, Aml *child); /* non block AML object primitives */ Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_name_decl(const char *name, Aml *val); +Aml *aml_return(Aml *val); Aml *aml_int(const uint64_t val); /* Block AML object primitives */ -- cgit 1.4.1 From 7193f3a67ee4794096255396eeac99f9624144e1 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:23 +0000 Subject: acpi: add aml_arg() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 11 +++++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 12 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 8207d9c057..99d213c182 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -397,6 +397,17 @@ Aml *aml_name_decl(const char *name, Aml *val) return var; } +/* ACPI 1.0b: 16.2.6.1 Arg Objects Encoding */ +Aml *aml_arg(int pos) +{ + Aml *var; + uint8_t op = 0x68 /* ARG0 op */ + pos; + + assert(pos <= 6); + var = aml_opcode(op); + return var; +} + /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ Aml *aml_if(Aml *predicate) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index e7fa9771b1..63e9754ad6 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -64,6 +64,7 @@ Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_name_decl(const char *name, Aml *val); Aml *aml_return(Aml *val); Aml *aml_int(const uint64_t val); +Aml *aml_arg(int pos); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From c263b3f754325ae76946a5ea0e1df9368fe289bd Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:24 +0000 Subject: acpi: add aml_store() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 9 +++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 10 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 99d213c182..57e3176c62 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -408,6 +408,15 @@ Aml *aml_arg(int pos) return var; } +/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefStore */ +Aml *aml_store(Aml *val, Aml *target) +{ + Aml *var = aml_opcode(0x70 /* StoreOp */); + aml_append(var, val); + aml_append(var, target); + return var; +} + /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ Aml *aml_if(Aml *predicate) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 63e9754ad6..6ad76ddc62 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -65,6 +65,7 @@ Aml *aml_name_decl(const char *name, Aml *val); Aml *aml_return(Aml *val); Aml *aml_int(const uint64_t val); Aml *aml_arg(int pos); +Aml *aml_store(Aml *val, Aml *target); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From 926f5aaefad164dabcdd771f4c15c9773383d9e9 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:25 +0000 Subject: acpi: add aml_and() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 10 ++++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 11 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 57e3176c62..bb1fbbce44 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -417,6 +417,16 @@ Aml *aml_store(Aml *val, Aml *target) return var; } +/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAnd */ +Aml *aml_and(Aml *arg1, Aml *arg2) +{ + Aml *var = aml_opcode(0x7B /* AndOp */); + aml_append(var, arg1); + aml_append(var, arg2); + build_append_int(var->buf, 0x00 /* NullNameOp */); + return var; +} + /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ Aml *aml_if(Aml *predicate) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 6ad76ddc62..2fca361e7c 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -66,6 +66,7 @@ Aml *aml_return(Aml *val); Aml *aml_int(const uint64_t val); Aml *aml_arg(int pos); Aml *aml_store(Aml *val, Aml *target); +Aml *aml_and(Aml *arg1, Aml *arg2); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From 34189453f1783c934d3824ff4636684f3ab20810 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:26 +0000 Subject: acpi: add aml_notify() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 9 +++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 10 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index bb1fbbce44..3e187fabce 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -427,6 +427,15 @@ Aml *aml_and(Aml *arg1, Aml *arg2) return var; } +/* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefNotify */ +Aml *aml_notify(Aml *arg1, Aml *arg2) +{ + Aml *var = aml_opcode(0x86 /* NotifyOp */); + aml_append(var, arg1); + aml_append(var, arg2); + return var; +} + /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ Aml *aml_if(Aml *predicate) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 2fca361e7c..cbef2368d2 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -67,6 +67,7 @@ Aml *aml_int(const uint64_t val); Aml *aml_arg(int pos); Aml *aml_store(Aml *val, Aml *target); Aml *aml_and(Aml *arg1, Aml *arg2); +Aml *aml_notify(Aml *arg1, Aml *arg2); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From 3f3992b7c4ed9850e7f5502a1294cd017a891b38 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:27 +0000 Subject: acpi: add aml_call1(), aml_call2(), aml_call3(), aml_call4() helpers Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 42 ++++++++++++++++++++++++++++++++++++++++++ include/hw/acpi/aml-build.h | 4 ++++ 2 files changed, 46 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 3e187fabce..18427b6407 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -436,6 +436,48 @@ Aml *aml_notify(Aml *arg1, Aml *arg2) return var; } +/* helper to call method with 1 argument */ +Aml *aml_call1(const char *method, Aml *arg1) +{ + Aml *var = aml_alloc(); + build_append_namestring(var->buf, "%s", method); + aml_append(var, arg1); + return var; +} + +/* helper to call method with 2 arguments */ +Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2) +{ + Aml *var = aml_alloc(); + build_append_namestring(var->buf, "%s", method); + aml_append(var, arg1); + aml_append(var, arg2); + return var; +} + +/* helper to call method with 3 arguments */ +Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3) +{ + Aml *var = aml_alloc(); + build_append_namestring(var->buf, "%s", method); + aml_append(var, arg1); + aml_append(var, arg2); + aml_append(var, arg3); + return var; +} + +/* helper to call method with 4 arguments */ +Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4) +{ + Aml *var = aml_alloc(); + build_append_namestring(var->buf, "%s", method); + aml_append(var, arg1); + aml_append(var, arg2); + aml_append(var, arg3); + aml_append(var, arg4); + return var; +} + /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ Aml *aml_if(Aml *predicate) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index cbef2368d2..5f00b07c30 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -68,6 +68,10 @@ Aml *aml_arg(int pos); Aml *aml_store(Aml *val, Aml *target); Aml *aml_and(Aml *arg1, Aml *arg2); Aml *aml_notify(Aml *arg1, Aml *arg2); +Aml *aml_call1(const char *method, Aml *arg1); +Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2); +Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3); +Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From 3bfa74a7e8c0787877e0f23a873413ad8817c66c Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:28 +0000 Subject: acpi: add aml_package() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 8 ++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 9 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 18427b6407..fdeeacc993 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -505,3 +505,11 @@ Aml *aml_device(const char *name_format, ...) va_end(ap); return var; } + +/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */ +Aml *aml_package(uint8_t num_elements) +{ + Aml *var = aml_bundle(0x12 /* PackageOp */, AML_PACKAGE); + build_append_byte(var->buf, num_elements); + return var; +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 5f00b07c30..92097b5945 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -78,6 +78,7 @@ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_method(const char *name, int arg_count); Aml *aml_if(Aml *predicate); +Aml *aml_package(uint8_t num_elements); /* other helpers */ GArray *build_alloc_array(void); -- cgit 1.4.1 From 04b8da543dd16afaea014032939db4cb54b15459 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:30 +0000 Subject: acpi: add aml_buffer() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 7 +++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 8 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index fdeeacc993..98b1540bbb 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -506,6 +506,13 @@ Aml *aml_device(const char *name_format, ...) return var; } +/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer */ +Aml *aml_buffer(void) +{ + Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER); + return var; +} + /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefPackage */ Aml *aml_package(uint8_t num_elements) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 92097b5945..5fe66c68da 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -79,6 +79,7 @@ Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_method(const char *name, int arg_count); Aml *aml_if(Aml *predicate); Aml *aml_package(uint8_t num_elements); +Aml *aml_buffer(void); /* other helpers */ GArray *build_alloc_array(void); -- cgit 1.4.1 From ad4a80bc6f4aa7ce8e67e924574521f04b8b19a4 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:31 +0000 Subject: acpi: add aml_resource_template() helper Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 8 ++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 9 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 98b1540bbb..070ca81110 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -506,6 +506,14 @@ Aml *aml_device(const char *name_format, ...) return var; } +/* ACPI 1.0b: 6.4.1 ASL Macros for Resource Descriptors */ +Aml *aml_resource_template(void) +{ + /* ResourceTemplate is a buffer of Resources with EndTag at the end */ + Aml *var = aml_bundle(0x11 /* BufferOp */, AML_RES_TEMPLATE); + return var; +} + /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefBuffer */ Aml *aml_buffer(void) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 5fe66c68da..2810b4c43f 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -80,6 +80,7 @@ Aml *aml_method(const char *name, int arg_count); Aml *aml_if(Aml *predicate); Aml *aml_package(uint8_t num_elements); Aml *aml_buffer(void); +Aml *aml_resource_template(void); /* other helpers */ GArray *build_alloc_array(void); -- cgit 1.4.1 From 52fa397c58667080979e8aa64177c0f69b1851b7 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:32 +0000 Subject: acpi: add aml_io() helper Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 16 ++++++++++++++++ include/hw/acpi/aml-build.h | 7 +++++++ 2 files changed, 23 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 070ca81110..d7edda1097 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -478,6 +478,22 @@ Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4) return var; } +/* ACPI 1.0b: 6.4.2.5 I/O Port Descriptor */ +Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, + uint8_t aln, uint8_t len) +{ + Aml *var = aml_alloc(); + build_append_byte(var->buf, 0x47); /* IO port descriptor */ + build_append_byte(var->buf, dec); + build_append_byte(var->buf, min_base & 0xff); + build_append_byte(var->buf, (min_base >> 8) & 0xff); + build_append_byte(var->buf, max_base & 0xff); + build_append_byte(var->buf, (max_base >> 8) & 0xff); + build_append_byte(var->buf, aln); + build_append_byte(var->buf, len); + return var; +} + /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ Aml *aml_if(Aml *predicate) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 2810b4c43f..298e2ef3b1 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -23,6 +23,11 @@ struct Aml { }; typedef struct Aml Aml; +typedef enum { + aml_decode10 = 0, + aml_decode16 = 1, +} AmlIODecode; + /** * init_aml_allocator: * @@ -72,6 +77,8 @@ Aml *aml_call1(const char *method, Aml *arg1); Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2); Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3); Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4); +Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, + uint8_t aln, uint8_t len); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From 19fff2d405d3bcc88fd7b860ec47797b3f7af977 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:33 +0000 Subject: acpi: include PkgLength size only when requested Named/Reserved{Field} definition uses PkgLength [1] encoding to specify field length, however it doesn't include size of PkgLength field itself, while other block objects that have explicit length of its body account for PkgLength size while encoding it [2]. This special casing isn't mentioned in ACPI spec, but that's what 'iasl' compiles NamedField to so add extra argument to build_prepend_pkg_length() to allow it handle the case. -- 1. ACPI Spec 5.0, 20.2.5.2 Named Objects Encoding, page 822 2. ACPI Spec 5.0, 5.4 Definition Block Encoding Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 20 +++++++++++++++----- include/hw/acpi/aml-build.h | 3 ++- 2 files changed, 17 insertions(+), 6 deletions(-) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index d7edda1097..cc883ccb95 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -158,10 +158,10 @@ enum { PACKAGE_LENGTH_4BYTE_SHIFT = 20, }; -void build_prepend_package_length(GArray *package) +void +build_prepend_package_length(GArray *package, unsigned length, bool incl_self) { uint8_t byte; - unsigned length = package->len; unsigned length_bytes; if (length + 1 < (1 << PACKAGE_LENGTH_1BYTE_SHIFT)) { @@ -174,8 +174,18 @@ void build_prepend_package_length(GArray *package) length_bytes = 4; } - /* PkgLength is the length of the inclusive length of the data. */ - length += length_bytes; + /* + * NamedField uses PkgLength encoding but it doesn't include length + * of PkgLength itself. + */ + if (incl_self) { + /* + * PkgLength is the length of the inclusive length of the data + * and PkgLength's length itself when used for terms with + * explitit length. + */ + length += length_bytes; + } switch (length_bytes) { case 1: @@ -208,7 +218,7 @@ void build_prepend_package_length(GArray *package) void build_package(GArray *package, uint8_t op) { - build_prepend_package_length(package); + build_prepend_package_length(package, package->len, true); build_prepend_byte(package, op); } diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 298e2ef3b1..b7f491ef02 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -99,7 +99,8 @@ void build_append_array(GArray *array, GArray *val); void GCC_FMT_ATTR(2, 3) build_append_namestring(GArray *array, const char *format, ...); -void build_prepend_package_length(GArray *package); +void +build_prepend_package_length(GArray *package, unsigned length, bool incl_self); void build_package(GArray *package, uint8_t op); void build_append_int(GArray *table, uint64_t value); void build_extop_package(GArray *package, uint8_t op); -- cgit 1.4.1 From 31127938f496f56eb05dc407a31e4c5941fb436a Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:34 +0000 Subject: acpi: add aml_operation_region() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 14 ++++++++++++++ include/hw/acpi/aml-build.h | 7 +++++++ 2 files changed, 21 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index cc883ccb95..d72d5b45c8 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -554,3 +554,17 @@ Aml *aml_package(uint8_t num_elements) build_append_byte(var->buf, num_elements); return var; } + +/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefOpRegion */ +Aml *aml_operation_region(const char *name, AmlRegionSpace rs, + uint32_t offset, uint32_t len) +{ + Aml *var = aml_alloc(); + build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ + build_append_byte(var->buf, 0x80); /* OpRegionOp */ + build_append_namestring(var->buf, "%s", name); + build_append_byte(var->buf, rs); + build_append_int(var->buf, offset); + build_append_int(var->buf, len); + return var; +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index b7f491ef02..57926e5b7f 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -28,6 +28,11 @@ typedef enum { aml_decode16 = 1, } AmlIODecode; +typedef enum { + aml_system_memory = 0x00, + aml_system_io = 0x01, +} AmlRegionSpace; + /** * init_aml_allocator: * @@ -79,6 +84,8 @@ Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3); Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4); Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, uint8_t aln, uint8_t len); +Aml *aml_operation_region(const char *name, AmlRegionSpace rs, + uint32_t offset, uint32_t len); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From 214ae59f8e1c33db0f3fbbc4347bb3dacc6ce876 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:35 +0000 Subject: acpi: add aml_field() & aml_named_field() terms Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 28 ++++++++++++++++++++++++++++ include/hw/acpi/aml-build.h | 6 ++++++ 2 files changed, 34 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index d72d5b45c8..b91141e320 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -216,6 +216,16 @@ build_prepend_package_length(GArray *package, unsigned length, bool incl_self) build_prepend_byte(package, byte); } +static void +build_append_pkg_length(GArray *array, unsigned length, bool incl_self) +{ + GArray *tmp = build_alloc_array(); + + build_prepend_package_length(tmp, length, incl_self); + build_append_array(array, tmp); + build_free_array(tmp); +} + void build_package(GArray *package, uint8_t op) { build_prepend_package_length(package, package->len, true); @@ -568,3 +578,21 @@ Aml *aml_operation_region(const char *name, AmlRegionSpace rs, build_append_int(var->buf, len); return var; } + +/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: NamedField */ +Aml *aml_named_field(const char *name, unsigned length) +{ + Aml *var = aml_alloc(); + build_append_nameseg(var->buf, name); + build_append_pkg_length(var->buf, length, false); + return var; +} + +/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */ +Aml *aml_field(const char *name, AmlFieldFlags flags) +{ + Aml *var = aml_bundle(0x81 /* FieldOp */, AML_EXT_PACKAGE); + build_append_namestring(var->buf, "%s", name); + build_append_byte(var->buf, flags); + return var; +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 57926e5b7f..2bb5f3938c 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -28,6 +28,10 @@ typedef enum { aml_decode16 = 1, } AmlIODecode; +typedef enum { + aml_byte_acc = 1, +} AmlFieldFlags; + typedef enum { aml_system_memory = 0x00, aml_system_io = 0x01, @@ -86,6 +90,7 @@ Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, uint8_t aln, uint8_t len); Aml *aml_operation_region(const char *name, AmlRegionSpace rs, uint32_t offset, uint32_t len); +Aml *aml_named_field(const char *name, unsigned length); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); @@ -95,6 +100,7 @@ Aml *aml_if(Aml *predicate); Aml *aml_package(uint8_t num_elements); Aml *aml_buffer(void); Aml *aml_resource_template(void); +Aml *aml_field(const char *name, AmlFieldFlags flags); /* other helpers */ GArray *build_alloc_array(void); -- cgit 1.4.1 From b8a5d6894d94c8f7e815c64adff78b0d28a98ca6 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:36 +0000 Subject: acpi: add aml_local() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 11 +++++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 12 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index b91141e320..fabd0adf28 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -596,3 +596,14 @@ Aml *aml_field(const char *name, AmlFieldFlags flags) build_append_byte(var->buf, flags); return var; } + +/* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */ +Aml *aml_local(int num) +{ + Aml *var; + uint8_t op = 0x60 /* Local0Op */ + num; + + assert(num <= 7); + var = aml_opcode(op); + return var; +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 2bb5f3938c..de081d6805 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -91,6 +91,7 @@ Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, Aml *aml_operation_region(const char *name, AmlRegionSpace rs, uint32_t offset, uint32_t len); Aml *aml_named_field(const char *name, unsigned length); +Aml *aml_local(int num); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From d5e5830f56452d857f9c26c06a886f09056482c1 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:37 +0000 Subject: acpi: add aml_string() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 25 +++++++++++++++++++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 26 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index fabd0adf28..57a65dd695 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -597,6 +597,31 @@ Aml *aml_field(const char *name, AmlFieldFlags flags) return var; } +/* ACPI 1.0b: 16.2.3 Data Objects Encoding: String */ +Aml *aml_string(const char *name_format, ...) +{ + Aml *var = aml_opcode(0x0D /* StringPrefix */); + va_list ap, va_len; + char *s; + int len; + + va_start(ap, name_format); + va_copy(va_len, ap); + len = vsnprintf(NULL, 0, name_format, va_len); + va_end(va_len); + len += 1; + s = g_new0(typeof(*s), len); + + len = vsnprintf(s, len, name_format, ap); + va_end(ap); + + g_array_append_vals(var->buf, s, len); + build_append_byte(var->buf, 0x0); /* NullChar */ + g_free(s); + + return var; +} + /* ACPI 1.0b: 16.2.6.2 Local Objects Encoding */ Aml *aml_local(int num) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index de081d6805..b1eab3c126 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -92,6 +92,7 @@ Aml *aml_operation_region(const char *name, AmlRegionSpace rs, uint32_t offset, uint32_t len); Aml *aml_named_field(const char *name, unsigned length); Aml *aml_local(int num); +Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From a678508e46ac806d81bd401a483aef1b98734ae4 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:39 +0000 Subject: acpi: add aml_varpackage() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 8 ++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 9 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 57a65dd695..36aafe4bfb 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -632,3 +632,11 @@ Aml *aml_local(int num) var = aml_opcode(op); return var; } + +/* ACPI 2.0a: 17.2.2 Data Objects Encoding: DefVarPackage */ +Aml *aml_varpackage(uint32_t num_elements) +{ + Aml *var = aml_bundle(0x13 /* VarPackageOp */, AML_PACKAGE); + build_append_int(var->buf, num_elements); + return var; +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index b1eab3c126..7f3886fc8f 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -103,6 +103,7 @@ Aml *aml_package(uint8_t num_elements); Aml *aml_buffer(void); Aml *aml_resource_template(void); Aml *aml_field(const char *name, AmlFieldFlags flags); +Aml *aml_varpackage(uint32_t num_elements); /* other helpers */ GArray *build_alloc_array(void); -- cgit 1.4.1 From 15e44e56d7da9d4569c10aa8de5f109a71570670 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:40 +0000 Subject: acpi: add aml_equal() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 10 ++++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 11 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 36aafe4bfb..97b027c380 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -514,6 +514,16 @@ Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, return var; } +/* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */ +Aml *aml_equal(Aml *arg1, Aml *arg2) +{ + Aml *var = aml_opcode(0x93 /* LequalOp */); + aml_append(var, arg1); + aml_append(var, arg2); + build_append_int(var->buf, 0x00); /* NullNameOp */ + return var; +} + /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefIfElse */ Aml *aml_if(Aml *predicate) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 7f3886fc8f..5e2b43498b 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -93,6 +93,7 @@ Aml *aml_operation_region(const char *name, AmlRegionSpace rs, Aml *aml_named_field(const char *name, unsigned length); Aml *aml_local(int num); Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); +Aml *aml_equal(Aml *arg1, Aml *arg2); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From 3dd156435369153c1c1d890b9ef525f1d033a971 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:41 +0000 Subject: acpi: add aml_processor() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 15 +++++++++++++++ include/hw/acpi/aml-build.h | 2 ++ 2 files changed, 17 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 97b027c380..9081b49358 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -650,3 +650,18 @@ Aml *aml_varpackage(uint32_t num_elements) build_append_int(var->buf, num_elements); return var; } + +/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefProcessor */ +Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, + const char *name_format, ...) +{ + va_list ap; + Aml *var = aml_bundle(0x83 /* ProcessorOp */, AML_EXT_PACKAGE); + va_start(ap, name_format); + build_append_namestringv(var->buf, name_format, ap); + va_end(ap); + build_append_byte(var->buf, proc_id); /* ProcID */ + build_append_int_noprefix(var->buf, pblk_addr, sizeof(pblk_addr)); + build_append_byte(var->buf, pblk_len); /* PblkLen */ + return var; +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 5e2b43498b..58bbbfead1 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -94,6 +94,8 @@ Aml *aml_named_field(const char *name, unsigned length); Aml *aml_local(int num); Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_equal(Aml *arg1, Aml *arg2); +Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, + const char *name_format, ...) GCC_FMT_ATTR(4, 5); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From a7891dacadebb9c02e027c456a1d17ea0bdf6df8 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:42 +0000 Subject: acpi: add aml_eisaid() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 29 +++++++++++++++++++++++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 30 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 9081b49358..eeb006f416 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -665,3 +665,32 @@ Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, build_append_byte(var->buf, pblk_len); /* PblkLen */ return var; } + +static uint8_t Hex2Digit(char c) +{ + if (c >= 'A') { + return c - 'A' + 10; + } + + return c - '0'; +} + +/* ACPI 1.0b: 15.2.3.6.4.1 EISAID Macro - Convert EISA ID String To Integer */ +Aml *aml_eisaid(const char *str) +{ + Aml *var = aml_alloc(); + uint32_t id; + + g_assert(strlen(str) == 7); + id = (str[0] - 0x40) << 26 | + (str[1] - 0x40) << 21 | + (str[2] - 0x40) << 16 | + Hex2Digit(str[3]) << 12 | + Hex2Digit(str[4]) << 8 | + Hex2Digit(str[5]) << 4 | + Hex2Digit(str[6]); + + build_append_byte(var->buf, 0x0C); /* DWordPrefix */ + build_append_int_noprefix(var->buf, bswap32(id), sizeof(id)); + return var; +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 58bbbfead1..bf94155316 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -96,6 +96,7 @@ Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_equal(Aml *arg1, Aml *arg2); Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, const char *name_format, ...) GCC_FMT_ATTR(4, 5); +Aml *aml_eisaid(const char *str); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From e2ea299b01fbd9d4d0262cfbfcbdbd02ada984a5 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:45 +0000 Subject: acpi: add aml_reserved_field() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 10 ++++++++++ include/hw/acpi/aml-build.h | 6 ++++++ 2 files changed, 16 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index eeb006f416..9d9b978e13 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -598,6 +598,16 @@ Aml *aml_named_field(const char *name, unsigned length) return var; } +/* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: ReservedField */ +Aml *aml_reserved_field(unsigned length) +{ + Aml *var = aml_alloc(); + /* ReservedField := 0x00 PkgLength */ + build_append_byte(var->buf, 0x00); + build_append_pkg_length(var->buf, length, false); + return var; +} + /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefField */ Aml *aml_field(const char *name, AmlFieldFlags flags) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index bf94155316..ea3ece316b 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -29,7 +29,12 @@ typedef enum { } AmlIODecode; typedef enum { + aml_any_acc = 0, aml_byte_acc = 1, + aml_word_acc = 2, + aml_dword_acc = 3, + aml_qword_acc = 4, + aml_buffer_acc = 5, } AmlFieldFlags; typedef enum { @@ -91,6 +96,7 @@ Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, Aml *aml_operation_region(const char *name, AmlRegionSpace rs, uint32_t offset, uint32_t len); Aml *aml_named_field(const char *name, unsigned length); +Aml *aml_reserved_field(unsigned length); Aml *aml_local(int num); Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2); Aml *aml_equal(Aml *arg1, Aml *arg2); -- cgit 1.4.1 From 6ece7053d6a4a502d2ea5d24ecf512caaa1437c7 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Wed, 18 Feb 2015 19:14:48 +0000 Subject: acpi: add aml_word_bus_number(), aml_word_io(), aml_dword_memory(), aml_qword_memory() terms Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 161 ++++++++++++++++++++++++++++++++++++++++++++ include/hw/acpi/aml-build.h | 72 ++++++++++++++++++++ 2 files changed, 233 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 9d9b978e13..d793775d93 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -704,3 +704,164 @@ Aml *aml_eisaid(const char *str) build_append_int_noprefix(var->buf, bswap32(id), sizeof(id)); return var; } + +/* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor: bytes 3-5 */ +static Aml *aml_as_desc_header(AmlResourceType type, AmlMinFixed min_fixed, + AmlMaxFixed max_fixed, AmlDecode dec, + uint8_t type_flags) +{ + uint8_t flags = max_fixed | min_fixed | dec; + Aml *var = aml_alloc(); + + build_append_byte(var->buf, type); + build_append_byte(var->buf, flags); + build_append_byte(var->buf, type_flags); /* Type Specific Flags */ + return var; +} + +/* ACPI 1.0b: 6.4.3.5.5 Word Address Space Descriptor */ +static Aml *aml_word_as_desc(AmlResourceType type, AmlMinFixed min_fixed, + AmlMaxFixed max_fixed, AmlDecode dec, + uint16_t addr_gran, uint16_t addr_min, + uint16_t addr_max, uint16_t addr_trans, + uint16_t len, uint8_t type_flags) +{ + Aml *var = aml_alloc(); + + build_append_byte(var->buf, 0x88); /* Word Address Space Descriptor */ + /* minimum length since we do not encode optional fields */ + build_append_byte(var->buf, 0x0D); + build_append_byte(var->buf, 0x0); + + aml_append(var, + aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags)); + build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran)); + build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min)); + build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max)); + build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans)); + build_append_int_noprefix(var->buf, len, sizeof(len)); + return var; +} + +/* ACPI 1.0b: 6.4.3.5.3 DWord Address Space Descriptor */ +static Aml *aml_dword_as_desc(AmlResourceType type, AmlMinFixed min_fixed, + AmlMaxFixed max_fixed, AmlDecode dec, + uint32_t addr_gran, uint32_t addr_min, + uint32_t addr_max, uint32_t addr_trans, + uint32_t len, uint8_t type_flags) +{ + Aml *var = aml_alloc(); + + build_append_byte(var->buf, 0x87); /* DWord Address Space Descriptor */ + /* minimum length since we do not encode optional fields */ + build_append_byte(var->buf, 23); + build_append_byte(var->buf, 0x0); + + + aml_append(var, + aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags)); + build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran)); + build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min)); + build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max)); + build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans)); + build_append_int_noprefix(var->buf, len, sizeof(len)); + return var; +} + +/* ACPI 1.0b: 6.4.3.5.1 QWord Address Space Descriptor */ +static Aml *aml_qword_as_desc(AmlResourceType type, AmlMinFixed min_fixed, + AmlMaxFixed max_fixed, AmlDecode dec, + uint64_t addr_gran, uint64_t addr_min, + uint64_t addr_max, uint64_t addr_trans, + uint64_t len, uint8_t type_flags) +{ + Aml *var = aml_alloc(); + + build_append_byte(var->buf, 0x8A); /* QWord Address Space Descriptor */ + /* minimum length since we do not encode optional fields */ + build_append_byte(var->buf, 0x2B); + build_append_byte(var->buf, 0x0); + + aml_append(var, + aml_as_desc_header(type, min_fixed, max_fixed, dec, type_flags)); + build_append_int_noprefix(var->buf, addr_gran, sizeof(addr_gran)); + build_append_int_noprefix(var->buf, addr_min, sizeof(addr_min)); + build_append_int_noprefix(var->buf, addr_max, sizeof(addr_max)); + build_append_int_noprefix(var->buf, addr_trans, sizeof(addr_trans)); + build_append_int_noprefix(var->buf, len, sizeof(len)); + return var; +} + +/* + * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor + * + * More verbose description at: + * ACPI 5.0: 19.5.141 WordBusNumber (Word Bus Number Resource Descriptor Macro) + */ +Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, + AmlDecode dec, uint16_t addr_gran, + uint16_t addr_min, uint16_t addr_max, + uint16_t addr_trans, uint16_t len) + +{ + return aml_word_as_desc(aml_bus_number_range, min_fixed, max_fixed, dec, + addr_gran, addr_min, addr_max, addr_trans, len, 0); +} + +/* + * ACPI 1.0b: 6.4.3.5.6 ASL Macros for WORD Address Descriptor + * + * More verbose description at: + * ACPI 5.0: 19.5.142 WordIO (Word IO Resource Descriptor Macro) + */ +Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, + AmlDecode dec, AmlISARanges isa_ranges, + uint16_t addr_gran, uint16_t addr_min, + uint16_t addr_max, uint16_t addr_trans, + uint16_t len) + +{ + return aml_word_as_desc(aml_io_range, min_fixed, max_fixed, dec, + addr_gran, addr_min, addr_max, addr_trans, len, + isa_ranges); +} + +/* + * ACPI 1.0b: 6.4.3.5.4 ASL Macros for DWORD Address Space Descriptor + * + * More verbose description at: + * ACPI 5.0: 19.5.34 DWordMemory (DWord Memory Resource Descriptor Macro) + */ +Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, + AmlMaxFixed max_fixed, AmlCacheble cacheable, + AmlReadAndWrite read_and_write, + uint32_t addr_gran, uint32_t addr_min, + uint32_t addr_max, uint32_t addr_trans, + uint32_t len) +{ + uint8_t flags = read_and_write | (cacheable << 1); + + return aml_dword_as_desc(aml_memory_range, min_fixed, max_fixed, + dec, addr_gran, addr_min, addr_max, + addr_trans, len, flags); +} + +/* + * ACPI 1.0b: 6.4.3.5.2 ASL Macros for QWORD Address Space Descriptor + * + * More verbose description at: + * ACPI 5.0: 19.5.102 QWordMemory (QWord Memory Resource Descriptor Macro) + */ +Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, + AmlMaxFixed max_fixed, AmlCacheble cacheable, + AmlReadAndWrite read_and_write, + uint64_t addr_gran, uint64_t addr_min, + uint64_t addr_max, uint64_t addr_trans, + uint64_t len) +{ + uint8_t flags = read_and_write | (cacheable << 1); + + return aml_qword_as_desc(aml_memory_range, min_fixed, max_fixed, + dec, addr_gran, addr_min, addr_max, + addr_trans, len, flags); +} diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index ea3ece316b..d2b2c354fa 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -42,6 +42,57 @@ typedef enum { aml_system_io = 0x01, } AmlRegionSpace; +typedef enum { + aml_memory_range = 0, + aml_io_range = 1, + aml_bus_number_range = 2, +} AmlResourceType; + +typedef enum { + aml_sub_decode = 1 << 1, + aml_pos_decode = 0 +} AmlDecode; + +typedef enum { + aml_max_fixed = 1 << 3, + aml_max_not_fixed = 0, +} AmlMaxFixed; + +typedef enum { + aml_min_fixed = 1 << 2, + aml_min_not_fixed = 0 +} AmlMinFixed; + +/* + * ACPI 1.0b: Table 6-26 I/O Resource Flag (Resource Type = 1) Definitions + * _RNG field definition + */ +typedef enum { + aml_isa_only = 1, + aml_non_isa_only = 2, + aml_entire_range = 3, +} AmlISARanges; + +/* + * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions + * _MEM field definition + */ +typedef enum { + aml_non_cacheable = 0, + aml_cacheable = 1, + aml_write_combining = 2, + aml_prefetchable = 3, +} AmlCacheble; + +/* + * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions + * _RW field definition + */ +typedef enum { + aml_ReadOnly = 0, + aml_ReadWrite = 1, +} AmlReadAndWrite; + /** * init_aml_allocator: * @@ -103,6 +154,27 @@ Aml *aml_equal(Aml *arg1, Aml *arg2); Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len, const char *name_format, ...) GCC_FMT_ATTR(4, 5); Aml *aml_eisaid(const char *str); +Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, + AmlDecode dec, uint16_t addr_gran, + uint16_t addr_min, uint16_t addr_max, + uint16_t addr_trans, uint16_t len); +Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, + AmlDecode dec, AmlISARanges isa_ranges, + uint16_t addr_gran, uint16_t addr_min, + uint16_t addr_max, uint16_t addr_trans, + uint16_t len); +Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed, + AmlMaxFixed max_fixed, AmlCacheble cacheable, + AmlReadAndWrite read_and_write, + uint32_t addr_gran, uint32_t addr_min, + uint32_t addr_max, uint32_t addr_trans, + uint32_t len); +Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed, + AmlMaxFixed max_fixed, AmlCacheble cacheable, + AmlReadAndWrite read_and_write, + uint64_t addr_gran, uint64_t addr_min, + uint64_t addr_max, uint64_t addr_trans, + uint64_t len); /* Block AML object primitives */ Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); -- cgit 1.4.1 From 7056045332b997a8cd298e3bf231ecf690accdf4 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Fri, 20 Feb 2015 18:22:10 +0000 Subject: acpi: add acpi_irq_no_flags() term Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 21 +++++++++++++++++++++ include/hw/acpi/aml-build.h | 1 + 2 files changed, 22 insertions(+) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index d793775d93..60245e7ded 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -514,6 +514,27 @@ Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, return var; } +/* + * ACPI 1.0b: 6.4.2.1.1 ASL Macro for IRQ Descriptor + * + * More verbose description at: + * ACPI 5.0: 19.5.64 IRQNoFlags (Interrupt Resource Descriptor Macro) + * 6.4.2.1 IRQ Descriptor + */ +Aml *aml_irq_no_flags(uint8_t irq) +{ + uint16_t irq_mask; + Aml *var = aml_alloc(); + + assert(irq < 16); + build_append_byte(var->buf, 0x22); /* IRQ descriptor 2 byte form */ + + irq_mask = 1U << irq; + build_append_byte(var->buf, irq_mask & 0xFF); /* IRQ mask bits[7:0] */ + build_append_byte(var->buf, irq_mask >> 8); /* IRQ mask bits[15:8] */ + return var; +} + /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefLEqual */ Aml *aml_equal(Aml *arg1, Aml *arg2) { diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index d2b2c354fa..11871970ca 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -146,6 +146,7 @@ Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base, uint8_t aln, uint8_t len); Aml *aml_operation_region(const char *name, AmlRegionSpace rs, uint32_t offset, uint32_t len); +Aml *aml_irq_no_flags(uint8_t irq); Aml *aml_named_field(const char *name, unsigned length); Aml *aml_reserved_field(unsigned length); Aml *aml_local(int num); -- cgit 1.4.1 From af59b35ce1aa3e69488e7308b632d5af2ac54948 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Fri, 20 Feb 2015 18:22:20 +0000 Subject: acpi: make build_*() routines static to aml-build.c build_*() routines were used for composing AML structures manually in acpi-build.c but after conversion to AML API they are not used outside of aml-build.c anymore, so hide them from external users. Signed-off-by: Igor Mammedov Reviewed-by: Michael S. Tsirkin Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 20 ++++++++++---------- include/hw/acpi/aml-build.h | 16 ---------------- 2 files changed, 10 insertions(+), 26 deletions(-) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 60245e7ded..3e5949be10 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -27,27 +27,27 @@ #include "hw/acpi/aml-build.h" #include "qemu/bswap.h" -GArray *build_alloc_array(void) +static GArray *build_alloc_array(void) { return g_array_new(false, true /* clear */, 1); } -void build_free_array(GArray *array) +static void build_free_array(GArray *array) { g_array_free(array, true); } -void build_prepend_byte(GArray *array, uint8_t val) +static void build_prepend_byte(GArray *array, uint8_t val) { g_array_prepend_val(array, val); } -void build_append_byte(GArray *array, uint8_t val) +static void build_append_byte(GArray *array, uint8_t val) { g_array_append_val(array, val); } -void build_append_array(GArray *array, GArray *val) +static void build_append_array(GArray *array, GArray *val) { g_array_append_vals(array, val->data, val->len); } @@ -141,7 +141,7 @@ build_append_namestringv(GArray *array, const char *format, va_list ap) g_strfreev(segs); } -void build_append_namestring(GArray *array, const char *format, ...) +static void build_append_namestring(GArray *array, const char *format, ...) { va_list ap; @@ -158,7 +158,7 @@ enum { PACKAGE_LENGTH_4BYTE_SHIFT = 20, }; -void +static void build_prepend_package_length(GArray *package, unsigned length, bool incl_self) { uint8_t byte; @@ -226,13 +226,13 @@ build_append_pkg_length(GArray *array, unsigned length, bool incl_self) build_free_array(tmp); } -void build_package(GArray *package, uint8_t op) +static void build_package(GArray *package, uint8_t op) { build_prepend_package_length(package, package->len, true); build_prepend_byte(package, op); } -void build_extop_package(GArray *package, uint8_t op) +static void build_extop_package(GArray *package, uint8_t op) { build_package(package, op); build_prepend_byte(package, 0x5B); /* ExtOpPrefix */ @@ -248,7 +248,7 @@ static void build_append_int_noprefix(GArray *table, uint64_t value, int size) } } -void build_append_int(GArray *table, uint64_t value) +static void build_append_int(GArray *table, uint64_t value) { if (value == 0x00) { build_append_byte(table, 0x00); /* ZeroOp */ diff --git a/include/hw/acpi/aml-build.h b/include/hw/acpi/aml-build.h index 11871970ca..f6735eaac2 100644 --- a/include/hw/acpi/aml-build.h +++ b/include/hw/acpi/aml-build.h @@ -188,20 +188,4 @@ Aml *aml_resource_template(void); Aml *aml_field(const char *name, AmlFieldFlags flags); Aml *aml_varpackage(uint32_t num_elements); -/* other helpers */ -GArray *build_alloc_array(void); -void build_free_array(GArray *array); -void build_prepend_byte(GArray *array, uint8_t val); -void build_append_byte(GArray *array, uint8_t val); -void build_append_array(GArray *array, GArray *val); - -void GCC_FMT_ATTR(2, 3) -build_append_namestring(GArray *array, const char *format, ...); - -void -build_prepend_package_length(GArray *package, unsigned length, bool incl_self); -void build_package(GArray *package, uint8_t op); -void build_append_int(GArray *table, uint64_t value); -void build_extop_package(GArray *package, uint8_t op); - #endif -- cgit 1.4.1 From 2e5feadbf8133b9b108919a2f576943b448a1a06 Mon Sep 17 00:00:00 2001 From: "Michael S. Tsirkin" Date: Tue, 3 Mar 2015 17:40:45 +0100 Subject: aml-build: fix build for glib < 2.22 g_ptr_array_new_with_free_func is there since glib 2.22, use the older g_ptr_array_foreach instead. Reported-by: Peter Maydell Signed-off-by: Michael S. Tsirkin --- hw/acpi/aml-build.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'hw/acpi/aml-build.c') diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 3e5949be10..876cada4b2 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -299,7 +299,7 @@ static Aml *aml_bundle(uint8_t op, AmlBlockFlags flags) return var; } -static void aml_free(gpointer data) +static void aml_free(gpointer data, gpointer user_data) { Aml *var = data; build_free_array(var->buf); @@ -310,13 +310,14 @@ Aml *init_aml_allocator(void) Aml *var; assert(!alloc_list); - alloc_list = g_ptr_array_new_with_free_func(aml_free); + alloc_list = g_ptr_array_new(); var = aml_alloc(); return var; } void free_aml_allocator(void) { + g_ptr_array_foreach(alloc_list, aml_free, NULL); g_ptr_array_free(alloc_list, true); alloc_list = 0; } -- cgit 1.4.1