661875e948
Signed-off-by: Igor Mammedov <imammedo@redhat.com> Reviewed-by: Claudio Fontana <claudio.fontana@huawei.com> Reviewed-by: Marcel Apfelbaum <marcel@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
260 lines
6.6 KiB
C
260 lines
6.6 KiB
C
/* Support for generating ACPI tables and passing them to Guests
|
|
*
|
|
* Copyright (C) 2015 Red Hat Inc
|
|
*
|
|
* Author: Michael S. Tsirkin <mst@redhat.com>
|
|
* Author: Igor Mammedov <imammedo@redhat.com>
|
|
*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
#include <assert.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#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
|
|
|
|
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 */
|
|
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
|
|
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. */
|
|
PACKAGE_LENGTH_2BYTE_SHIFT = 4,
|
|
PACKAGE_LENGTH_3BYTE_SHIFT = 12,
|
|
PACKAGE_LENGTH_4BYTE_SHIFT = 20,
|
|
};
|
|
|
|
void build_prepend_package_length(GArray *package)
|
|
{
|
|
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;
|
|
}
|
|
|
|
/* 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)
|
|
{
|
|
build_prepend_package_length(package);
|
|
build_prepend_byte(package, op);
|
|
}
|
|
|
|
void build_extop_package(GArray *package, uint8_t op)
|
|
{
|
|
build_package(package, op);
|
|
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);
|
|
}
|
|
}
|