From: minyard@acm.org
To: qemu-devel@nongnu.org
Cc: Corey Minyard <cminyard@mvista.com>,
openipmi-developer@lists.sourceforge.net
Subject: [Qemu-devel] [PATCH 16/20] acpi: Add table construction tools
Date: Wed, 29 May 2013 17:08:12 -0500 [thread overview]
Message-ID: <1369865296-19584-17-git-send-email-minyard@acm.org> (raw)
In-Reply-To: <1369865296-19584-1-git-send-email-minyard@acm.org>
From: Corey Minyard <cminyard@mvista.com>
Add a set of functions to allow construction of ACPI elements
dynamically.
Signed-off-by: Corey Minyard <cminyard@mvista.com>
---
hw/acpi/Makefile.objs | 2 +-
hw/acpi/acpi-elements.c | 351 +++++++++++++++++++++++++++++++++++++++
include/hw/acpi/acpi-elements.h | 77 +++++++++
3 files changed, 429 insertions(+), 1 deletion(-)
create mode 100644 hw/acpi/acpi-elements.c
create mode 100644 include/hw/acpi/acpi-elements.h
diff --git a/hw/acpi/Makefile.objs b/hw/acpi/Makefile.objs
index a0b63b5..abfdc31 100644
--- a/hw/acpi/Makefile.objs
+++ b/hw/acpi/Makefile.objs
@@ -1,2 +1,2 @@
-common-obj-$(CONFIG_ACPI) += core.o piix4.o ich9.o
+common-obj-$(CONFIG_ACPI) += core.o piix4.o ich9.o acpi-elements.o
diff --git a/hw/acpi/acpi-elements.c b/hw/acpi/acpi-elements.c
new file mode 100644
index 0000000..08d3f40
--- /dev/null
+++ b/hw/acpi/acpi-elements.c
@@ -0,0 +1,351 @@
+/*
+ * Dynamically construct ACPI elements
+ *
+ * Copyright (C) 2012 Corey Minyard <cminyard@mvista.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2 as published by the Free Software Foundation.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>
+ *
+ * Contributions after 2012-01-13 are licensed under the terms of the
+ * GNU GPL, version 2 or (at your option) any later version.
+ */
+
+#include "hw/acpi/acpi-elements.h"
+#include <string.h>
+
+#define acpi_add_byte(data, val) do { **data = val; (*data)++; } while(0)
+
+static int
+acpi_add_Pkglen(char **data, int dlen, int length)
+{
+ int pkglen;
+
+ /*
+ * The funny length values following are because we include the length
+ * bytes in the full length.
+ */
+ if (length <= 0x3e) {
+ if (dlen > 0) {
+ acpi_add_byte(data, length + 1);
+ }
+ return 1;
+ } else if (length <= 0xffd) {
+ pkglen = 2;
+ } else if (length <= 0xffffc) {
+ pkglen = 3;
+ } else if (length <= 0xffffffb) {
+ pkglen = 4;
+ } else {
+ return -1;
+ }
+ length += pkglen;
+ if (pkglen <= dlen) {
+ acpi_add_byte(data, ((pkglen - 1) << 6) | (length & 0xf));
+ length >>= 4;
+ pkglen--;
+ while (pkglen > 0) {
+ acpi_add_byte(data, length & 0xff);
+ length >>= 8;
+ pkglen--;
+ }
+ }
+ return pkglen;
+}
+
+static int
+acpi_add_NameString(char **data, int dlen, const char *name)
+{
+ int length = strlen(name);
+ int i;
+
+ if (dlen >= 4) {
+ i = 0;
+ while ((i < 4) && (i < length)) {
+ acpi_add_byte(data, *name++);
+ i++;
+ }
+ while (i < 4) {
+ acpi_add_byte(data, '_');
+ i++;
+ }
+ }
+ return 4;
+}
+
+int
+acpi_add_Device(char **data, int dlen, const char *name,
+ contained_acpi_elem e, void *opaque)
+{
+ int length, plen, totlen;
+
+ length = e(NULL, 0, opaque);
+ if (length < 0)
+ return length;
+
+ plen = acpi_add_Pkglen(NULL, 0, length);
+ if (plen < 0)
+ return plen;
+ totlen = length + plen + 6;
+ if (dlen >= totlen) {
+ acpi_add_byte(data, 0x5b);
+ acpi_add_byte(data, 0x82);
+ dlen -= 2;
+ dlen -= acpi_add_Pkglen(data, dlen, length);
+ dlen -= acpi_add_NameString(data, dlen, name);
+ dlen -= e(data, dlen, opaque);
+ }
+
+ return totlen;
+}
+
+int
+acpi_add_Name(char **data, int dlen, const char *name,
+ contained_acpi_elem e, void *opaque)
+{
+ int rv;
+
+ if (dlen >= 5) {
+ acpi_add_byte(data, 0x8);
+ dlen -= 1;
+ dlen -= acpi_add_NameString(data, dlen, name);
+ }
+ rv = e(data, dlen, opaque);
+ if (rv < 0)
+ return rv;
+ return rv + 5;
+}
+
+int
+acpi_add_Method(char **data, int dlen, const char *name, uint8_t flags,
+ contained_acpi_elem e, void *opaque)
+{
+ int elen, plen;
+
+ elen = e(NULL, 0, opaque);
+ if (elen < 0)
+ return elen;
+
+ plen = acpi_add_Pkglen(NULL, 0, elen + 5);
+ if (plen < 0)
+ return plen;
+ if (plen + elen + 6 <= dlen) {
+ acpi_add_byte(data, 0x14);
+ dlen -= 1;
+ dlen -= acpi_add_Pkglen(data, dlen, elen + 5);
+ dlen -= acpi_add_NameString(data, dlen, name);
+ acpi_add_byte(data, flags);
+ dlen -= 1;
+ dlen -= e(data, dlen, opaque);
+ }
+ return plen + elen + 6;
+}
+
+int
+acpi_add_Integer(char **data, int dlen, void *vval)
+{
+ uint64_t val = *((uint64_t *) vval);
+ int length, i;
+ unsigned char op;
+
+ if ((val == 0) || (val == 1)) {
+ /* ZeroOp or OneOp */
+ if (dlen > 0) {
+ acpi_add_byte(data, val);
+ }
+ return 1;
+ } if (val <= 0xff) {
+ length = 1;
+ op = 0x0a;
+ } else if (val <= 0xffff) {
+ length = 2;
+ op = 0x0b;
+ } else if (val <= 0xffffffff) {
+ length = 4;
+ op = 0x0c;
+ } else {
+ length = 8;
+ op = 0x0e;
+ }
+
+ if (dlen >= length + 1) {
+ acpi_add_byte(data, op);
+ for (i = 0; i < length; i++) {
+ acpi_add_byte(data, val & 0xff);
+ val >>= 8;
+ }
+ }
+ return length + 1;
+}
+
+/*
+ * A compressed EISA ID has the top bit reserved, the next 15 bits as
+ * compressed ASCII upper case letters, and the bottom 16 bits as four
+ * hex digits.
+ */
+int
+acpi_add_EISAID(char **data, int dlen, void *val)
+{
+ char *str = val;
+ uint32_t ival = 0;
+ int i;
+
+ if (dlen >= 5) {
+ acpi_add_byte(data, 0xc); /* dword */
+ if (strlen(val) != 7)
+ return -1;
+ for (i = 0; i < 3; i++) {
+ if (str[i] < 'A' || str[i] > 'Z')
+ return -1;
+ ival = (ival << 5) | (str[i] - 0x40);
+ }
+ for (; i < 7; i++) {
+ int v;
+ if (str[i] >= '0' && str[i] <= '9')
+ v = str[i] - '0';
+ else if (str[i] >= 'A' && str[i] <= 'F')
+ v = str[i] - 'A' + 10;
+ else
+ return -1;
+ ival = (ival << 4) | v;
+ }
+ /* Note that for some reason this is big endian */
+ for (i = 0; i < 4; i++) {
+ acpi_add_byte(data, (ival >> 24) & 0xff);
+ ival <<= 8;
+ }
+ }
+
+ return 5;
+}
+
+int
+acpi_add_BufferOp(char **data, int dlen,
+ contained_acpi_elem e, void *opaque)
+{
+ int blen, slen, plen, tlen;
+ uint64_t val;
+
+ blen = e(NULL, 0, opaque);
+ if (blen < 0)
+ return blen;
+
+ val = blen;
+ slen = acpi_add_Integer(NULL, 0, &val);
+ if (slen < 0)
+ return slen;
+ plen = acpi_add_Pkglen(NULL, 0, slen + blen);
+ if (plen < 0)
+ return plen;
+ tlen = blen + slen + plen + 1;
+ if (tlen <= dlen) {
+ acpi_add_byte(data, 0x11);
+ dlen--;
+ dlen -= acpi_add_Pkglen(data, dlen, slen + blen);
+ dlen -= acpi_add_Integer(data, dlen, &val);
+ dlen -= e(data, dlen, opaque);
+ }
+ return tlen;
+}
+
+int
+acpi_add_Return(char **data, int dlen, void *val)
+{
+ int blen;
+
+ blen = acpi_add_Integer(NULL, 0, val);
+ if (blen + 1 <= dlen) {
+ acpi_add_byte(data, 0xa4);
+ dlen -= 1;
+ dlen -= acpi_add_Integer(data, dlen, val);
+ }
+ return blen + 1;
+}
+
+/*
+ * Note that str is void*, not char*, so it can be passed as a
+ * contained element.
+ */
+static int
+unicode_helper(char **data, int dlen, void *vstr)
+{
+ char *str = vstr;
+ int len = strlen(str) + 1;
+
+ if (len * 2 <= dlen) {
+ while (*str) {
+ acpi_add_byte(data, *str++);
+ acpi_add_byte(data, 0);
+ }
+ acpi_add_byte(data, 0);
+ acpi_add_byte(data, 0);
+ }
+ return len * 2;
+}
+int
+acpi_add_Unicode(char **data, int dlen, void *vstr)
+{
+ int len;
+
+ len = acpi_add_BufferOp(NULL, 0, unicode_helper, vstr);
+ if (len < 0)
+ return len;
+ if (len <= dlen) {
+ acpi_add_BufferOp(data, dlen, unicode_helper, vstr);
+ }
+ return len;
+}
+
+int
+acpi_add_IO16(char **data, int dlen,
+ uint16_t minaddr, uint16_t maxaddr,
+ uint8_t align, uint8_t range)
+{
+ if (dlen >= 8) {
+ acpi_add_byte(data, 0x47);
+ acpi_add_byte(data, 1);
+ acpi_add_byte(data, minaddr & 0xff);
+ acpi_add_byte(data, minaddr >> 8);
+ acpi_add_byte(data, maxaddr & 0xff);
+ acpi_add_byte(data, maxaddr >> 8);
+ acpi_add_byte(data, align);
+ acpi_add_byte(data, range);
+ }
+ return 8;
+}
+
+int
+acpi_add_Interrupt(char **data, int dlen, int irq,
+ int consumer, int mode, int polarity, int sharing)
+{
+ if (dlen >= 9) {
+ acpi_add_byte(data, 0x89);
+ acpi_add_byte(data, 6);
+ acpi_add_byte(data, 0);
+ acpi_add_byte(data, consumer | (mode << 1) | (polarity << 2) | (sharing << 3));
+ acpi_add_byte(data, 1); /* Only 1 irq */
+ acpi_add_byte(data, irq);
+ acpi_add_byte(data, 0);
+ acpi_add_byte(data, 0);
+ acpi_add_byte(data, 0);
+ }
+ return 9;
+}
+
+int
+acpi_add_EndResource(char **data, int dlen)
+{
+ if (dlen >= 2) {
+ acpi_add_byte(data, 0x79);
+ acpi_add_byte(data, 0);
+ }
+ return 2;
+}
diff --git a/include/hw/acpi/acpi-elements.h b/include/hw/acpi/acpi-elements.h
new file mode 100644
index 0000000..68ce151
--- /dev/null
+++ b/include/hw/acpi/acpi-elements.h
@@ -0,0 +1,77 @@
+/*
+ * Dynamically construct ACPI elements
+ *
+ * Copyright (C) 2012 Corey Minyard <cminyard@mvista.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2 as published by the Free Software Foundation.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>
+ *
+ * Contributions after 2012-01-13 are licensed under the terms of the
+ * GNU GPL, version 2 or (at your option) any later version.
+ */
+
+#ifndef __ACPI_ELEMENTS_H
+#define __ACPI_ELEMENTS_H
+
+#include <stdint.h>
+
+typedef int (*contained_acpi_elem)(char **data, int length,
+ void *opaque);
+
+int acpi_add_Device(char **data, int dlen, const char *name,
+ contained_acpi_elem e, void *opaque);
+
+int acpi_add_Name(char **data, int dlen, const char *name,
+ contained_acpi_elem e, void *opaque);
+
+int acpi_add_Method(char **data, int dlen, const char *name, uint8_t flags,
+ contained_acpi_elem e, void *opaque);
+
+/* Pass in a pointer to a u64 */
+int acpi_add_Integer(char **data, int dlen, void *val);
+
+/* Pass in a pointer to a string */
+int acpi_add_EISAID(char **data, int dlen, void *val);
+
+int acpi_add_BufferOp(char **data, int dlen,
+ contained_acpi_elem e, void *opaque);
+
+/* Pass in a pointer to a u64 */
+int acpi_add_Return(char **data, int dlen, void *);
+
+/*
+ * Note that str is void*, not char*, so it can be passed as a
+ * contained element.
+ */
+int acpi_add_Unicode(char **data, int dlen, void *vstr);
+
+int acpi_add_IO16(char **data, int dlen,
+ uint16_t minaddr, uint16_t maxaddr,
+ uint8_t align, uint8_t range);
+
+#define ACPI_RESOURCE_PRODUCER 0
+#define ACPI_RESOURCE_CONSUMER 1
+#define ACPI_INTERRUPT_MODE_LEVEL 0
+#define ACPI_INTERRUPT_MODE_EDGE 1
+#define ACPI_INTERRUPT_POLARITY_ACTIVE_HIGH 0
+#define ACPI_INTERRUPT_POLARITY_ACTIVE_LOW 1
+#define ACPI_INTERRUPT_EXCLUSIVE 0
+#define ACPI_INTERRUPT_SHARED 1
+#define ACPI_INTERRUPT_EXCLUSIVE_WAKE 2
+#define ACPI_INTERRUPT_SHARED_WAKE 3
+
+int acpi_add_Interrupt(char **data, int dlen, int irq,
+ int consumer, int mode, int polarity, int sharing);
+
+int acpi_add_EndResource(char **data, int dlen);
+
+#endif
--
1.7.9.5
next prev parent reply other threads:[~2013-05-29 22:18 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-05-29 22:07 [Qemu-devel] [PATCH 00/20] Add an IPMI device to QEMU minyard
2013-05-29 22:07 ` [Qemu-devel] [PATCH 01/20] qemu-char: Allocate CharDriverState in qemu_chr_new_from_opts minyard
2013-05-29 22:07 ` [Qemu-devel] [PATCH 02/20] qemu-char: Allow a chardev to reconnect if disconnected minyard
2013-05-29 22:07 ` [Qemu-devel] [PATCH 03/20] qemu-char: Fix a race reporting opens and closes minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 04/20] qemu-char: remove free of chr from win_stdio_close minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 05/20] qemu-char: Close fd at end of file minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 06/20] Add a base IPMI interface minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 07/20] ipmi: Add a PC ISA type structure minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 08/20] ipmi: Add a KCS low-level interface minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 09/20] ipmi: Add a BT " minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 10/20] ipmi: Add a local BMC simulation minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 11/20] ipmi: Add an external connection simulation interface minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 12/20] ipmi: Add tests minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 13/20] ipmi: Add documentation minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 14/20] ipmi: Add migration capability to the IPMI device minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 15/20] acpi: Add a way to extend tables minyard
2013-05-29 22:08 ` minyard [this message]
2013-05-29 22:08 ` [Qemu-devel] [PATCH 17/20] pc: Postpone adding ACPI and SMBIOS to fw_cfg minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 18/20] ipmi: Add ACPI table entries for BMCs minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 19/20] smbios: Add a function to directly add an entry minyard
2013-05-29 22:08 ` [Qemu-devel] [PATCH 20/20] ipmi: Add SMBIOS table entry minyard
2013-11-05 13:56 ` [Qemu-devel] [PATCH 00/20] Add an IPMI device to QEMU Michael S. Tsirkin
2013-11-05 14:05 ` Corey Minyard
2013-11-05 16:09 ` Andreas Färber
2013-11-05 16:48 ` Corey Minyard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1369865296-19584-17-git-send-email-minyard@acm.org \
--to=minyard@acm.org \
--cc=cminyard@mvista.com \
--cc=openipmi-developer@lists.sourceforge.net \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).