git: 3f7acfe0730c - stable/13 - bhyve: introduce acpi_device_emul struct

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Fri, 28 Apr 2023 10:40:17 UTC
The branch stable/13 has been updated by corvink:

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

commit 3f7acfe0730cbe6ffa3550adc30ae4eaa1eaa2ac
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2022-07-22 08:00:10 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-04-28 07:28:32 +0000

    bhyve: introduce acpi_device_emul struct
    
    It'll be easier to add new properties to the ACPI device emulation if we
    have a struct which holds all device specific properties. In some future
    commits the acpi_device_emul struct will be expanded to include some
    device specific functions to build ACPI tables.
    
    Reviewed by:            markj
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D39319
    
    (cherry picked from commit acd0088c44f9ccaff571e041dc56d01fcc47d64b)
---
 usr.sbin/bhyve/acpi_device.c | 30 +++++++++++-------------------
 usr.sbin/bhyve/acpi_device.h | 14 +++++++++-----
 usr.sbin/bhyve/qemu_fwcfg.c  |  7 ++++++-
 3 files changed, 26 insertions(+), 25 deletions(-)

diff --git a/usr.sbin/bhyve/acpi_device.c b/usr.sbin/bhyve/acpi_device.c
index 047d411deec2..a1155ecb44b6 100644
--- a/usr.sbin/bhyve/acpi_device.c
+++ b/usr.sbin/bhyve/acpi_device.c
@@ -35,24 +35,23 @@ struct acpi_resource_list_entry {
  * 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 emul   Device emulation struct. It contains some information like the
+                 name of the ACPI device and some device specific functions.
  * @param crs    Current resources used by the ACPI device.
  */
 struct acpi_device {
 	struct vmctx *vm_ctx;
-	const char *name;
-	const char *hid;
+	const struct acpi_device_emul *emul;
 	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)
+    struct vmctx *const vm_ctx, const struct acpi_device_emul *const emul)
 {
-	if (new_dev == NULL || vm_ctx == NULL || name == NULL || hid == NULL) {
-		return (EINVAL);
-	}
+	assert(new_dev != NULL);
+	assert(vm_ctx != NULL);
+	assert(emul != NULL);
 
 	struct acpi_device *const dev = calloc(1, sizeof(*dev));
 	if (dev == NULL) {
@@ -60,13 +59,8 @@ acpi_device_create(struct acpi_device **const new_dev,
 	}
 
 	dev->vm_ctx = vm_ctx;
-	dev->name = strdup(name);
-	dev->hid = strdup(hid);
+	dev->emul = emul;
 	SLIST_INIT(&dev->crs);
-	if (dev->name == NULL || dev->hid == NULL) {
-		acpi_device_destroy(dev);
-		return (ENOMEM);
-	}
 
 	const int error = acpi_tables_add_device(dev);
 	if (error) {
@@ -92,9 +86,7 @@ acpi_device_destroy(struct acpi_device *const dev)
 		SLIST_REMOVE_HEAD(&dev->crs, chain);
 		free(res);
 	}
-	
-	free(__DECONST(void *, dev->hid));
-	free(__DECONST(void *, dev->name));
+
 	free(dev);
 }
 
@@ -174,9 +166,9 @@ acpi_device_write_dsdt(const struct acpi_device *const dev)
 	dsdt_line("");
 	dsdt_line("  Scope (\\_SB)");
 	dsdt_line("  {");
-	dsdt_line("    Device (%s)", dev->name);
+	dsdt_line("    Device (%s)", dev->emul->name);
 	dsdt_line("    {");
-	dsdt_line("      Name (_HID, \"%s\")", dev->hid);
+	dsdt_line("      Name (_HID, \"%s\")", dev->emul->hid);
 	dsdt_line("      Name (_STA, 0x0F)");
 	dsdt_line("      Name (_CRS, ResourceTemplate ()");
 	dsdt_line("      {");
diff --git a/usr.sbin/bhyve/acpi_device.h b/usr.sbin/bhyve/acpi_device.h
index 49e55c7d4f14..45e36a8ae771 100644
--- a/usr.sbin/bhyve/acpi_device.h
+++ b/usr.sbin/bhyve/acpi_device.h
@@ -16,18 +16,22 @@ struct vmctx;
 
 struct acpi_device;
 
+struct acpi_device_emul {
+	const char *name;
+	const char *hid;
+};
+
 /**
  * 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.
+ * @param[in]  emul    Device emulation struct. It contains some information
+ *                     like the name of the ACPI device and some device specific
+ *                     functions.
  */
 int acpi_device_create(struct acpi_device **new_dev, struct vmctx *vm_ctx,
-    const char *name, const char *hid);
+    const struct acpi_device_emul *emul);
 void acpi_device_destroy(struct acpi_device *dev);
 
 int acpi_device_add_res_fixed_ioport(struct acpi_device *dev, UINT16 port,
diff --git a/usr.sbin/bhyve/qemu_fwcfg.c b/usr.sbin/bhyve/qemu_fwcfg.c
index a93b37027142..e35d63d09795 100644
--- a/usr.sbin/bhyve/qemu_fwcfg.c
+++ b/usr.sbin/bhyve/qemu_fwcfg.c
@@ -363,6 +363,11 @@ qemu_fwcfg_add_file(const uint8_t name[QEMU_FWCFG_MAX_NAME],
 	return (0);
 }
 
+static const struct acpi_device_emul qemu_fwcfg_acpi_device_emul = {
+	.name = QEMU_FWCFG_ACPI_DEVICE_NAME,
+	.hid = QEMU_FWCFG_ACPI_HARDWARE_ID,
+};
+
 int
 qemu_fwcfg_init(struct vmctx *const ctx)
 {
@@ -376,7 +381,7 @@ qemu_fwcfg_init(struct vmctx *const ctx)
 	 */
 	if (strcmp(lpc_fwcfg(), "qemu") == 0) {
 		error = acpi_device_create(&fwcfg_sc.acpi_dev, ctx,
-		    QEMU_FWCFG_ACPI_DEVICE_NAME, QEMU_FWCFG_ACPI_HARDWARE_ID);
+		    &qemu_fwcfg_acpi_device_emul);
 		if (error) {
 			warnx("%s: failed to create ACPI device for QEMU FwCfg",
 			    __func__);