git: 682a522d6122 - main - bhyve: add helper func to write a dsdt entry

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Tue, 14 Feb 2023 07:29:09 UTC
The branch main has been updated by corvink:

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

commit 682a522d61223920f23a202aa57901f38bdfd8dd
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2021-10-07 13:58:27 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-02-14 07:28:27 +0000

    bhyve: add helper func to write a dsdt entry
    
    The guest will check the dsdt to detect acpi devices. Therefore, add a
    helper function to create such a dsdt entry for an acpi device.
    
    Reviewed by:            markj
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D38329
---
 usr.sbin/bhyve/acpi_device.c | 46 ++++++++++++++++++++++++++++++++++++++++++++
 usr.sbin/bhyve/acpi_device.h |  2 ++
 2 files changed, 48 insertions(+)

diff --git a/usr.sbin/bhyve/acpi_device.c b/usr.sbin/bhyve/acpi_device.c
index 402e32faa93a..10259006c619 100644
--- a/usr.sbin/bhyve/acpi_device.c
+++ b/usr.sbin/bhyve/acpi_device.c
@@ -10,6 +10,7 @@
 
 #include <machine/vmm.h>
 
+#include <assert.h>
 #include <err.h>
 #include <errno.h>
 #include <vmmapi.h>
@@ -135,3 +136,48 @@ acpi_device_add_res_fixed_memory32(struct acpi_device *const dev,
 
 	return (0);
 }
+
+static void
+acpi_device_write_dsdt_crs(const struct acpi_device *const dev)
+{
+	const struct acpi_resource_list_entry *res;
+	SLIST_FOREACH(res, &dev->crs, chain) {
+		switch (res->type) {
+		case ACPI_RESOURCE_TYPE_FIXED_IO:
+			dsdt_fixed_ioport(res->data.FixedIo.Address,
+			    res->data.FixedIo.AddressLength);
+			break;
+		case ACPI_RESOURCE_TYPE_FIXED_MEMORY32:
+			dsdt_fixed_mem32(res->data.FixedMemory32.Address,
+			    res->data.FixedMemory32.AddressLength);
+			break;
+		default:
+			assert(0);
+			break;
+		}
+	}
+}
+
+void
+acpi_device_write_dsdt(const struct acpi_device *const dev)
+{
+	if (dev == NULL) {
+		return;
+	}
+
+	dsdt_line("");
+	dsdt_line("  Scope (\\_SB)");
+	dsdt_line("  {");
+	dsdt_line("    Device (%s)", dev->name);
+	dsdt_line("    {");
+	dsdt_line("      Name (_HID, \"%s\")", dev->hid);
+	dsdt_line("      Name (_STA, 0x0F)");
+	dsdt_line("      Name (_CRS, ResourceTemplate ()");
+	dsdt_line("      {");
+	dsdt_indent(4);
+	acpi_device_write_dsdt_crs(dev);
+	dsdt_unindent(4);
+	dsdt_line("      })");
+	dsdt_line("    }");
+	dsdt_line("  }");
+}
diff --git a/usr.sbin/bhyve/acpi_device.h b/usr.sbin/bhyve/acpi_device.h
index d9618d7e0a43..5fb3ea18e481 100644
--- a/usr.sbin/bhyve/acpi_device.h
+++ b/usr.sbin/bhyve/acpi_device.h
@@ -34,3 +34,5 @@ int acpi_device_add_res_fixed_ioport(struct acpi_device *const dev,
     const UINT16 port, UINT8 length);
 int acpi_device_add_res_fixed_memory32(struct acpi_device *const dev,
     const UINT8 write_protected, const UINT32 address, const UINT32 length);
+
+void acpi_device_write_dsdt(const struct acpi_device *const dev);