From 4bd0849d81b48233196092868081315ec4d9233d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Corvin=20K=C3=B6hne?= <corvink@freebsd.org>
Date: Tue, 6 Jun 2023 11:21:39 +0200
Subject: [PATCH] OvmfPkg/Bhyve: install ACPI tables from memory
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

It's much easier to create configuration dependent ACPI tables for bhyve
than for OVMF. For this reason, don't use the statically created ACPI
tables provided by OVMF. Instead, prefer the dynamically created ACPI
tables of bhyve. If bhyve provides no ACPI tables or we are unable to
detect those, fall back to OVMF tables.

Ideally, we use the qemu fwcfg interface to pass the ACPI tables from
bhyve to OVMF. bhyve will support this in the future. However, current
bhyve executables don't support passing ACPI tables by the qemu fwcfg
interface. They just copy the ACPI into main memory. For that reason,
pick up the ACPI tables from main memory.

Signed-off-by: Corvin Köhne <corvink@FreeBSD.org>
Reviewed-by: Rebecca Cran <rebecca@bsdio.com>
Acked-by: Peter Grehan <grehan@freebsd.org>
Acked-by: Gerd Hoffmann <kraxel@redhat.com>
---
 OvmfPkg/Bhyve/AcpiPlatformDxe/AcpiPlatform.c  | 37 ++++++++++++++++++-
 .../Bhyve/AcpiPlatformDxe/AcpiPlatformDxe.inf |  1 +
 OvmfPkg/Bhyve/BhyveX64.dsc                    |  1 +
 3 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/OvmfPkg/Bhyve/AcpiPlatformDxe/AcpiPlatform.c b/OvmfPkg/Bhyve/AcpiPlatformDxe/AcpiPlatform.c
index 999e9f151e..1e1c90614e 100644
--- a/OvmfPkg/Bhyve/AcpiPlatformDxe/AcpiPlatform.c
+++ b/OvmfPkg/Bhyve/AcpiPlatformDxe/AcpiPlatform.c
@@ -10,6 +10,11 @@
 
 #include "AcpiPlatform.h"
 
+#include <Library/AcpiPlatformLib.h> // InstallAcpiTablesFromMemory()
+
+#define BHYVE_ACPI_PHYSICAL_ADDRESS  ((UINTN)0x000F2400)
+#define BHYVE_BIOS_PHYSICAL_END      ((UINTN)0x00100000)
+
 EFI_STATUS
 EFIAPI
 InstallAcpiTable (
@@ -241,7 +246,37 @@ InstallAcpiTables (
   IN   EFI_ACPI_TABLE_PROTOCOL  *AcpiTable
   )
 {
-  EFI_STATUS  Status;
+  EFI_ACPI_2_0_ROOT_SYSTEM_DESCRIPTION_POINTER  *Rsdp;
+  EFI_STATUS                                    Status;
+
+  Status = GetAcpiRsdpFromMemory (
+             BHYVE_ACPI_PHYSICAL_ADDRESS,
+             BHYVE_BIOS_PHYSICAL_END,
+             &Rsdp
+             );
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  Status = InstallAcpiTablesFromRsdp (
+             AcpiTable,
+             Rsdp
+             );
+  if (!EFI_ERROR (Status)) {
+    return EFI_SUCCESS;
+  }
+
+  if (Status != EFI_NOT_FOUND) {
+    DEBUG (
+      (
+       DEBUG_WARN,
+       "%a: unable to install bhyve's ACPI tables (%r)\n",
+       __func__,
+       Status
+      )
+      );
+    return Status;
+  }
 
   Status = InstallOvmfFvTables (AcpiTable);
 
diff --git a/OvmfPkg/Bhyve/AcpiPlatformDxe/AcpiPlatformDxe.inf b/OvmfPkg/Bhyve/AcpiPlatformDxe/AcpiPlatformDxe.inf
index 94c65f32dc..75ed8e4a7d 100644
--- a/OvmfPkg/Bhyve/AcpiPlatformDxe/AcpiPlatformDxe.inf
+++ b/OvmfPkg/Bhyve/AcpiPlatformDxe/AcpiPlatformDxe.inf
@@ -35,6 +35,7 @@
   UefiCpuPkg/UefiCpuPkg.dec
 
 [LibraryClasses]
+  AcpiPlatformLib
   BaseLib
   BaseMemoryLib
   BhyveFwCtlLib
diff --git a/OvmfPkg/Bhyve/BhyveX64.dsc b/OvmfPkg/Bhyve/BhyveX64.dsc
index bb317a50e6..7b974706f9 100644
--- a/OvmfPkg/Bhyve/BhyveX64.dsc
+++ b/OvmfPkg/Bhyve/BhyveX64.dsc
@@ -341,6 +341,7 @@
   PciLib|OvmfPkg/Library/DxePciLibI440FxQ35/DxePciLibI440FxQ35.inf
 
 [LibraryClasses.common.DXE_DRIVER]
+  AcpiPlatformLib|OvmfPkg/Library/AcpiPlatformLib/DxeAcpiPlatformLib.inf
   PcdLib|MdePkg/Library/DxePcdLib/DxePcdLib.inf
   HobLib|MdePkg/Library/DxeHobLib/DxeHobLib.inf
   MemoryAllocationLib|MdePkg/Library/UefiMemoryAllocationLib/UefiMemoryAllocationLib.inf