/* 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 "qemu/osdep.h" #include #include "hw/acpi/aml-build.h" #include "qemu/bswap.h" #include "qemu/bitops.h" #include "hw/acpi/bios-linker-loader.h" static GArray *build_alloc_array(void) { return g_array_new(false, true /* clear */, 1); } static void build_free_array(GArray *array) { g_array_free(array, true); } static void build_prepend_byte(GArray *array, uint8_t val) { g_array_prepend_val(array, val); } static void build_append_byte(GArray *array, uint8_t val) { g_array_append_val(array, val); } static void build_append_array(GArray *array, GArray *val) { g_array_append_vals(array, val->data, val->len); } #define ACPI_NAMESEG_LEN 4 static void build_append_nameseg(GArray *array, const char *seg) { int len; len = strlen(seg); assert(len <= ACPI_NAMESEG_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 GCC_FMT_ATTR(2, 0) build_append_namestringv(GArray *array, const char *format, va_list ap) { char *s; char **segs; char **segs_iter; int seg_count = 0; s = g_strdup_vprintf(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, 0x00); /* 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); } GCC_FMT_ATTR(2, 3) static 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. */ PACKAGE_LENGTH_2BYTE_SHIFT = 4, PACKAGE_LENGTH_3BYTE_SHIFT = 12, PACKAGE_LENGTH_4BYTE_SHIFT = 20, }; static void build_prepend_package_length(GArray *package, unsigned length, bool incl_self) { uint8_t byte; 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; } /* * 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: 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); } 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); } static void build_package(GArray *package, uint8_t op) { build_prepend_package_length(package, package->len, true); build_prepend_byte(package, op); } static void build_extop_package(GArray *package, uint8_t op) { build_package(package, op); build_prepend_byte(package, 0x5B); /* ExtOpPrefix */ } static void build_append_int_noprefix(GArray *table, uint64_t value, int size) { int i; for (i = 0; i < size; ++i) { build_append_byte(table, value & 0xFF); value = value >> 8; } } static 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_byte(table, 0x0A); /* BytePrefix */ build_append_int_noprefix(table, value, 1); } else if (value <= 0xFFFF) { 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_byte(table, 0x0E); /* QWordPrefix */ build_append_int_noprefix(table, value, 8); } } /* * Build NAME(XXXX, 0x00000000) where 0x00000000 is encoded as a dword, * and return the offset to 0x00000000 for runtime patching. * * Warning: runtime patching is best avoided. Only use this as * a replacement for DataTableRegion (for guests that don't * support it). */ int build_append_named_dword(GArray *array, const char *name_format, ...) { int offset; va_list ap; build_append_byte(array, 0x08); /* NameOp */ va_start(ap, name_format); build_append_namestringv(array, name_format, ap); va_end(ap); build_append_byte(array, 0x0C); /* DWordPrefix */ offset = array->len; build_append_int_noprefix(array, 0x00000000, 4); assert(array->len == offset + 4); return offset; } 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 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(
# makefile to manage dpdk package
#

# Copyright 2015-2016 OPNFV
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Contributors:
#   Aihua Li, Huawei Technologies.
#   Martin Klozik, Intel Corporation.
#   Christian Trautman, Red Hat Inc.

include ../mk/master.mk
include ../package-list.mk
include /etc/os-release

.PHONY: install force_make

ifndef VHOST_USER
    VHOST_USER = n
endif
WORK_DIR = dpdk
TAG_DONE_FLAG = $(WORK_DIR)/.$(DPDK_TAG).tag.done

# VHOST configuration options are stored in different files based on DPDK version
# v1.2.3r0-v1.6.0r2 - configuration inside config/defconfig_x86_64-default-linuxapp-gcc
# v1.7.0-rc1-v2.2.0 - configuration inside config/common_linuxapp
# v16 and newer - configuration split between config/common_linuxapp and config/common_base
DPDK_TAG_MAJOR = $(shell echo $(DPDK_TAG) | cut -d. -f1)
DPDK_TAG_MINOR = $(shell echo $(DPDK_TAG) | cut -d. -f2)
ifeq ($(DPDK_TAG_MAJOR),v1)
ifeq ($(DPDK_TAG_MINOR), $(filter $(DPDK_TAG_MINOR), 7 8))
    DPDK_TARGET = x86_64-native-linuxapp-gcc
    CONFIG_FILE_LINUXAPP = $(WORK_DIR)/config/common_linuxapp
else
    DPDK_TARGET = x86_64-default-linuxapp-gcc
    CONFIG_FILE_LINUXAPP = $(WORK_DIR)/config/defconfig_x86_64-default-linuxapp-gcc
endif
else
ifeq ($(DPDK_TAG_MAJOR),v2)
    DPDK_TARGET = x86_64-native-linuxapp-gcc
    CONFIG_FILE_LINUXAPP = $(WORK_DIR)/config/common_linuxapp
else
    DPDK_TARGET = x86_64-native-linuxapp-gcc
    CONFIG_FILE_BASE = $(WORK_DIR)/config/common_base
    CONFIG_FILE_LINUXAPP = $(WORK_DIR)/config/common_linuxapp
endif
endif

all: force_make
	@echo "Finished making $(WORK_DIR) "

INSTALL_TARGET = force_make

# modify CONFIG_FILE to enable VHOST_USER build and restore original CONFIG_FILE after the build
# DPDK v16 comments:
## CONFIG_RTE_BUILD_COMBINE_LIBS has been obsoleted
## CONFIG_RTE_LIBRTE_VHOST and CONFIG_RTE_LIBRTE_KNI are listed in both config_base and config_linuxapp,
## values from config_linuxapp will be used, but options are modified at both places to avoid confusion.
force_make: $(TAG_DONE_FLAG)
	$(AT)cd $(WORK_DIR) && git pull $(DPDK_URL) $(DPDK_TAG)
ifdef CONFIG_FILE_BASE
	$(AT)sed -i -e 's/CONFIG_RTE_LIBRTE_VHOST_USER=.\+/CONFIG_RTE_LIBRTE_VHOST_USER=$(VHOST_USER)/g' $(CONFIG_FILE_BASE)
	$(AT)sed -i -e 's/CONFIG_RTE_LIBRTE_VHOST=./CONFIG_RTE_LIBRTE_VHOST=y/g' $(CONFIG_FILE_BASE)
	$(AT)sed -i -e 's/CONFIG_RTE_LIBRTE_KNI=./CONFIG_RTE_LIBRTE_KNI=n/g' $(CONFIG_FILE_BASE)
	$(AT)sed -i -e 's/CONFIG_RTE_LIBRTE_VHOST_NUMA=./CONFIG_RTE_LIBRTE_VHOST_NUMA=y/g' $(CONFIG_FILE_BASE)
else
	$(AT)sed -i -e 's/CONFIG_RTE_LIBRTE_VHOST_USER=.\+/CONFIG_RTE_LIBRTE_VHOST_USER=$(VHOST_USER)/g' $(CONFIG_FILE_LINUXAPP)
	$(AT)sed -i -e 's/CONFIG_RTE_BUILD_COMBINE_LIBS=./CONFIG_RTE_BUILD_COMBINE_LIBS=y/g' $(CONFIG_FILE_LINUXAPP)
endif
# CentOS 7.3 specific config changes to compile
ifeq ($(ID),"centos")
ifeq ($(VERSION_ID),"7")
	$(AT)sed -i.bak s@'SRCS-y += ethtool/igb/igb_main.c'@'#SRCS-y += ethtool/igb/igb_main.c'@g $(WORK_DIR)/lib/librte_eal/linuxapp/kni/Makefile
endif
endif
# RHEL 7.3 specific config changes to compile
ifeq ($(ID),"rhel")
ifeq ($(VERSION_ID),"7.3")
	$(AT)sed -i.bak s@'SRCS-y += ethtool/igb/igb_main.c'@'#SRCS-y += ethtool/igb/igb_main.c'@g $(WORK_DIR)/lib/librte_eal/linuxapp/kni/Makefile
endif
endif
	$(AT)sed -i -e 's/CONFIG_RTE_LIBRTE_VHOST=./CONFIG_RTE_LIBRTE_VHOST=y/g' $(CONFIG_FILE_LINUXAPP)
	$(AT)sed -i -e 's/CONFIG_RTE_LIBRTE_KNI=./CONFIG_RTE_LIBRTE_KNI=n/g' $(CONFIG_FILE_LINUXAPP)
	$(AT)cd $(WORK_DIR); make install T=$(DPDK_TARGET) -j DESTDIR=$(WORK_DIR)
	$(AT)cd `dirname $(CONFIG_FILE_LINUXAPP)` && git checkout `basename $(CONFIG_FILE_LINUXAPP)` && cd -
ifdef CONFIG_FILE_BASE
	$(AT)cd `dirname $(CONFIG_FILE_BASE)` && git checkout `basename $(CONFIG_FILE_BASE)` && cd -
endif
	$(AT)echo "VHOST_USER = $(VHOST_USER)"
ifeq ($(VHOST_USER),n)
	$(AT)cd $(WORK_DIR)/lib/librte_vhost/eventfd_link; make
endif
	@echo "Make done"

install: $(INSTALL_TARGET)
	$(AT)sudo cp -a $(WORK_DIR)/$(DPDK_TARGET)/kmod $(INSTALL_DIR)/lib/modules/$(KERNEL_VERSION)
	@echo "install done"

# hard way to clean and clobber
clean:
	$(AT)cd $(WORK_DIR) && git clean -xfd *.o
clobber:
	$(AT)rm -rf $(WORK_DIR)

# distclean is for developer who would like to keep the
# clone git repo, saving time to fetch again from url
distclean:
	$(AT)cd $(WORK_DIR) && git clean -xfd && git checkout -f

test:
	@echo "Make test in $(WORK_DIR) (stub) "

sanity:
	@echo "Make sanity in $(WORK_DIR) (stub) "

$(WORK_DIR):
	$(AT)git clone $(DPDK_URL)

$(TAG_DONE_FLAG): $(WORK_DIR)
	$(AT)cd $(WORK_DIR); git checkout $(DPDK_TAG)
	$(AT)touch $@
l *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; } 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; } /* 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 Descriptor * * More verbose description at: * ACPI 5.0: 19.5.33 DWordIO (DWord IO Resource Descriptor Macro) */ Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed, AmlDecode dec, AmlISARanges isa_ranges, uint32_t addr_gran, uint32_t addr_min, uint32_t addr_max, uint32_t addr_trans, uint32_t len) { return aml_dword_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, AmlCacheable 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, AmlCacheable 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); } /* ACPI 1.0b: 6.4.2.2 DMA Format/6.4.2.2.1 ASL Macro for DMA Descriptor */ Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz, uint8_t channel) { Aml *var = aml_alloc(); uint8_t flags = sz | bm << 2 | typ << 5; assert(channel < 8); build_append_byte(var->buf, 0x2A); /* Byte 0: DMA Descriptor */ build_append_byte(var->buf, 1U << channel); /* Byte 1: _DMA - DmaChannel */ build_append_byte(var->buf, flags); /* Byte 2 */ return var; } /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefSleep */ Aml *aml_sleep(uint64_t msec) { Aml *var = aml_alloc(); build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ build_append_byte(var->buf, 0x22); /* SleepOp */ aml_append(var, aml_int(msec)); return var; } static uint8_t Hex2Byte(const char *src) { int hi, lo; hi = Hex2Digit(src[0]); assert(hi >= 0); assert(hi <= 15); lo = Hex2Digit(src[1]); assert(lo >= 0); assert(lo <= 15); return (hi << 4) | lo; } /* * ACPI 3.0: 17.5.124 ToUUID (Convert String to UUID Macro) * e.g. UUID: aabbccdd-eeff-gghh-iijj-kkllmmnnoopp * call aml_touuid("aabbccdd-eeff-gghh-iijj-kkllmmnnoopp"); */ Aml *aml_touuid(const char *uuid) { Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER); assert(strlen(uuid) == 36); assert(uuid[8] == '-'); assert(uuid[13] == '-'); assert(uuid[18] == '-'); assert(uuid[23] == '-'); build_append_byte(var->buf, Hex2Byte(uuid + 6)); /* dd - at offset 00 */ build_append_byte(var->buf, Hex2Byte(uuid + 4)); /* cc - at offset 01 */ build_append_byte(var->buf, Hex2Byte(uuid + 2)); /* bb - at offset 02 */ build_append_byte(var->buf, Hex2Byte(uuid + 0)); /* aa - at offset 03 */ build_append_byte(var->buf, Hex2Byte(uuid + 11)); /* ff - at offset 04 */ build_append_byte(var->buf, Hex2Byte(uuid + 9)); /* ee - at offset 05 */ build_append_byte(var->buf, Hex2Byte(uuid + 16)); /* hh - at offset 06 */ build_append_byte(var->buf, Hex2Byte(uuid + 14)); /* gg - at offset 07 */ build_append_byte(var->buf, Hex2Byte(uuid + 19)); /* ii - at offset 08 */ build_append_byte(var->buf, Hex2Byte(uuid + 21)); /* jj - at offset 09 */ build_append_byte(var->buf, Hex2Byte(uuid + 24)); /* kk - at offset 10 */ build_append_byte(var->buf, Hex2Byte(uuid + 26)); /* ll - at offset 11 */ build_append_byte(var->buf, Hex2Byte(uuid + 28)); /* mm - at offset 12 */ build_append_byte(var->buf, Hex2Byte(uuid + 30)); /* nn - at offset 13 */ build_append_byte(var->buf, Hex2Byte(uuid + 32)); /* oo - at offset 14 */ build_append_byte(var->buf, Hex2Byte(uuid + 34)); /* pp - at offset 15 */ return var; } /* * ACPI 2.0b: 16.2.3.6.4.3 Unicode Macro (Convert Ascii String To Unicode) */ Aml *aml_unicode(const char *str) { int i = 0; Aml *var = aml_bundle(0x11 /* BufferOp */, AML_BUFFER); do { build_append_byte(var->buf, str[i]); build_append_byte(var->buf, 0); i++; } while (i <= strlen(str)); return var; } /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefDerefOf */ Aml *aml_derefof(Aml *arg) { Aml *var = aml_opcode(0x83 /* DerefOfOp */); aml_append(var, arg); return var; } /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefSizeOf */ Aml *aml_sizeof(Aml *arg) { Aml *var = aml_opcode(0x87 /* SizeOfOp */); aml_append(var, arg); return var; } /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: DefMutex */ Aml *aml_mutex(const char *name, uint8_t sync_level) { Aml *var = aml_alloc(); build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ build_append_byte(var->buf, 0x01); /* MutexOp */ build_append_namestring(var->buf, "%s", name); assert(!(sync_level & 0xF0)); build_append_byte(var->buf, sync_level); return var; } /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefAcquire */ Aml *aml_acquire(Aml *mutex, uint16_t timeout) { Aml *var = aml_alloc(); build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ build_append_byte(var->buf, 0x23); /* AcquireOp */ aml_append(var, mutex); build_append_int_noprefix(var->buf, timeout, sizeof(timeout)); return var; } /* ACPI 1.0b: 16.2.5.3 Type 1 Opcodes Encoding: DefRelease */ Aml *aml_release(Aml *mutex) { Aml *var = aml_alloc(); build_append_byte(var->buf, 0x5B); /* ExtOpPrefix */ build_append_byte(var->buf, 0x27); /* ReleaseOp */ aml_append(var, mutex); return var; } /* ACPI 1.0b: 16.2.5.1 Name Space Modifier Objects Encoding: DefAlias */ Aml *aml_alias(const char *source_object, const char *alias_object) { Aml *var = aml_opcode(0x06 /* AliasOp */); aml_append(var, aml_name("%s", source_object)); aml_append(var, aml_name("%s", alias_object)); return var; } /* ACPI 1.0b: 16.2.5.4 Type 2 Opcodes Encoding: DefConcat */ Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target) { return build_opcode_2arg_dst(0x73 /* ConcatOp */, source1, source2, target); } void build_header(GArray *linker, GArray *table_data, AcpiTableHeader *h, const char *sig, int len, uint8_t rev, const char *oem_id, const char *oem_table_id) { memcpy(&h->signature, sig, 4); h->length = cpu_to_le32(len); h->revision = rev; if (oem_id) { strncpy((char *)h->oem_id, oem_id, sizeof h->oem_id); } else { memcpy(h->oem_id, ACPI_BUILD_APPNAME6, 6); } if (oem_table_id) { strncpy((char *)h->oem_table_id, oem_table_id, sizeof(h->oem_table_id)); } else { memcpy(h->oem_table_id, ACPI_BUILD_APPNAME4, 4); memcpy(h->oem_table_id + 4, sig, 4); } h->oem_revision = cpu_to_le32(1); memcpy(h->asl_compiler_id, ACPI_BUILD_APPNAME4, 4); h->asl_compiler_revision = cpu_to_le32(1); h->checksum = 0; /* Checksum to be filled in by Guest linker */ bios_linker_loader_add_checksum(linker, ACPI_BUILD_TABLE_FILE, table_data, h, len, &h->checksum); } void *acpi_data_push(GArray *table_data, unsigned size) { unsigned off = table_data->len; g_array_set_size(table_data, off + size); return table_data->data + off; } unsigned acpi_data_len(GArray *table) { assert(g_array_get_element_size(table) == 1); return table->len; } void acpi_add_table(GArray *table_offsets, GArray *table_data) { uint32_t offset = cpu_to_le32(table_data->len); g_array_append_val(table_offsets, offset); } void acpi_build_tables_init(AcpiBuildTables *tables) { tables->rsdp = g_array_new(false, true /* clear */, 1); tables->table_data = g_array_new(false, true /* clear */, 1); tables->tcpalog = g_array_new(false, true /* clear */, 1); tables->linker = bios_linker_loader_init(); } void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre) { void *linker_data = bios_linker_loader_cleanup(tables->linker); g_free(linker_data); g_array_free(tables->rsdp, true); g_array_free(tables->table_data, true); g_array_free(tables->tcpalog, mfre); } /* Build rsdt table */ void build_rsdt(GArray *table_data, GArray *linker, GArray *table_offsets, const char *oem_id, const char *oem_table_id) { AcpiRsdtDescriptorRev1 *rsdt; size_t rsdt_len; int i; const int table_data_len = (sizeof(uint32_t) * table_offsets->len); rsdt_len = sizeof(*rsdt) + table_data_len; rsdt = acpi_data_push(table_data, rsdt_len); memcpy(rsdt->table_offset_entry, table_offsets->data, table_data_len); for (i = 0; i < table_offsets->len; ++i) { /* rsdt->table_offset_entry to be filled by Guest linker */ bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE, ACPI_BUILD_TABLE_FILE, table_data, &rsdt->table_offset_entry[i], sizeof(uint32_t)); } build_header(linker, table_data, (void *)rsdt, "RSDT", rsdt_len, 1, oem_id, oem_table_id); }