git: b5d47fa9a511 - stable/13 - bhyve: add helper struct for acpi device handling

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Tue, 28 Feb 2023 10:14:07 UTC
The branch stable/13 has been updated by corvink:

URL: https://cgit.FreeBSD.org/src/commit/?id=b5d47fa9a5112f13cac8282c18d36f40661967a0

commit b5d47fa9a5112f13cac8282c18d36f40661967a0
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2021-10-07 13:54:46 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-02-28 10:04:26 +0000

    bhyve: add helper struct for acpi device handling
    
    To simplify the handling of different acpi devices like qemu fwcfg or a
    tpm, add a helper struct. It will handle the reporting of acpi
    resources.
    
    Reviewed by:            markj
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D38327
    
    (cherry picked from commit 1231f047c3ed46ca6041df62f2b71f6e879bbb40)
---
 usr.sbin/bhyve/Makefile      |  1 +
 usr.sbin/bhyve/acpi_device.c | 92 ++++++++++++++++++++++++++++++++++++++++++++
 usr.sbin/bhyve/acpi_device.h | 31 +++++++++++++++
 3 files changed, 124 insertions(+)

diff --git a/usr.sbin/bhyve/Makefile b/usr.sbin/bhyve/Makefile
index a456d507a071..bf0a17edc59e 100644
--- a/usr.sbin/bhyve/Makefile
+++ b/usr.sbin/bhyve/Makefile
@@ -15,6 +15,7 @@ MAN=	bhyve.8 bhyve_config.5
 BHYVE_SYSDIR?=${SRCTOP}
 
 SRCS=	\
+	acpi_device.c		\
 	atkbdc.c		\
 	acpi.c			\
 	audio.c			\
diff --git a/usr.sbin/bhyve/acpi_device.c b/usr.sbin/bhyve/acpi_device.c
new file mode 100644
index 000000000000..66d50de814fd
--- /dev/null
+++ b/usr.sbin/bhyve/acpi_device.c
@@ -0,0 +1,92 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
+ * Author: Corvin Köhne <c.koehne@beckhoff.com>
+ */
+
+#include <sys/param.h>
+#include <sys/queue.h>
+
+#include <machine/vmm.h>
+
+#include <err.h>
+#include <errno.h>
+#include <vmmapi.h>
+
+#include "acpi.h"
+#include "acpi_device.h"
+
+/**
+ * List entry to enumerate all resources used by an ACPI device.
+ *
+ * @param chain Used to chain multiple elements together.
+ * @param type  Type of the ACPI resource.
+ * @param data  Data of the ACPI resource.
+ */
+struct acpi_resource_list_entry {
+	SLIST_ENTRY(acpi_resource_list_entry) chain;
+	UINT32 type;
+	ACPI_RESOURCE_DATA data;
+};
+
+/**
+ * Holds information about an ACPI device.
+ *
+ * @param vm_ctx VM context the ACPI device was created in.
+ * @param name   Name of the ACPI device.
+ * @param hid    Hardware ID of the ACPI device.
+ * @param crs    Current resources used by the ACPI device.
+ */
+struct acpi_device {
+	struct vmctx *vm_ctx;
+	const char *name;
+	const char *hid;
+	SLIST_HEAD(acpi_resource_list, acpi_resource_list_entry) crs;
+};
+
+int
+acpi_device_create(struct acpi_device **const new_dev,
+    struct vmctx *const vm_ctx, const char *const name, const char *const hid)
+{
+	if (new_dev == NULL || vm_ctx == NULL || name == NULL || hid == NULL) {
+		return (EINVAL);
+	}
+
+	struct acpi_device *const dev = calloc(1, sizeof(*dev));
+	if (dev == NULL) {
+		return (ENOMEM);
+	}
+
+	dev->vm_ctx = vm_ctx;
+	dev->name = strdup(name);
+	dev->hid = strdup(hid);
+	SLIST_INIT(&dev->crs);
+	if (dev->name == NULL || dev->hid == NULL) {
+		acpi_device_destroy(dev);
+		return (ENOMEM);
+	}
+
+	*new_dev = dev;
+
+	return (0);
+}
+
+void
+acpi_device_destroy(struct acpi_device *const dev)
+{
+	if (dev == NULL) {
+		return;
+	}
+
+	struct acpi_resource_list_entry *res;
+	while (!SLIST_EMPTY(&dev->crs)) {
+		res = SLIST_FIRST(&dev->crs);
+		SLIST_REMOVE_HEAD(&dev->crs, chain);
+		free(res);
+	}
+	
+	free(__DECONST(void *, dev->hid));
+	free(__DECONST(void *, dev->name));
+	free(dev);
+}
diff --git a/usr.sbin/bhyve/acpi_device.h b/usr.sbin/bhyve/acpi_device.h
new file mode 100644
index 000000000000..1728acd4cdcd
--- /dev/null
+++ b/usr.sbin/bhyve/acpi_device.h
@@ -0,0 +1,31 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2021 Beckhoff Automation GmbH & Co. KG
+ * Author: Corvin Köhne <c.koehne@beckhoff.com>
+ */
+
+#pragma once
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wunused-parameter"
+#include <contrib/dev/acpica/include/acpi.h>
+#pragma GCC diagnostic pop
+
+struct vmctx;
+
+struct acpi_device;
+
+/**
+ * Creates an ACPI device.
+ *
+ * @param[out] new_dev Returns the newly create ACPI device.
+ * @param[in]  vm_ctx  VM context the ACPI device is created in.
+ * @param[in]  name    Name of the ACPI device. Should always be a NULL
+ *                     terminated string.
+ * @param[in]  hid     Hardware ID of the ACPI device. Should always be a NULL
+ *                     terminated string.
+ */
+int acpi_device_create(struct acpi_device **const new_dev,
+    struct vmctx *const vm_ctx, const char *const name, const char *const hid);
+void acpi_device_destroy(struct acpi_device *const dev);