Added the HOB methods to load and store the QEMU firmware configure
address, data address and DMA address, which are not enabled during the
DXE stage.

Build-tested only (with "ArmVirtQemu.dsc and RiscVVirtQemu.dsc").

BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4755

Cc: Ard Biesheuvel <ardb+tianoc...@kernel.org>
Cc: Jiewen Yao <jiewen....@intel.com>
Cc: Gerd Hoffmann <kra...@redhat.com>
Cc: Leif Lindholm <quic_llind...@quicinc.com>
Cc: Sami Mujawar <sami.muja...@arm.com>
Cc: Sunil V L <suni...@ventanamicro.com>
Cc: Andrei Warkentin <andrei.warken...@intel.com>
Signed-off-by: Chao Li <lic...@loongson.cn>
---
 .../Library/QemuFwCfgLib/QemuFwCfgLibMmio.c   | 176 +++++++++++++++++-
 .../Library/QemuFwCfgLib/QemuFwCfgLibMmio.inf |   1 +
 .../QemuFwCfgLib/QemuFwCfgLibMmioInternal.h   |  51 +++++
 3 files changed, 218 insertions(+), 10 deletions(-)

diff --git a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmio.c 
b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmio.c
index dc949c8e26..c7cf5719e2 100644
--- a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmio.c
+++ b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmio.c
@@ -8,11 +8,16 @@
   SPDX-License-Identifier: BSD-2-Clause-Patent
 **/
 
+#include <Base.h>
 #include <Uefi.h>
 
+#include <Pi/PiBootMode.h>
+#include <Pi/PiHob.h>
+
 #include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
+#include <Library/HobLib.h>
 #include <Library/IoLib.h>
 #include <Library/QemuFwCfgLib.h>
 #include <Library/UefiBootServicesTableLib.h>
@@ -21,6 +26,157 @@
 
 #include "QemuFwCfgLibMmioInternal.h"
 
+EFI_GUID  mFwCfgSelectorAddressGuid = FW_CONFIG_SELECTOR_ADDRESS_HOB_GUID;
+EFI_GUID  mFwCfgDataAddressGuid     = FW_CONFIG_DATA_ADDRESS_HOB_GUID;
+EFI_GUID  mFwCfgDmaAddressGuid      = FW_CONFIG_DMA_ADDRESS_HOB_GUID;
+
+/**
+  Build firmware configure selector address HOB.
+
+  @param[in]   FwCfgSelectorAddress  Firmware configure selector address
+
+  @retval  NULL
+**/
+VOID
+QemuBuildFwCfgSelectorHob (
+  IN UINT64  FwCfgSelectorAddress
+  )
+{
+  BuildGuidDataHob (
+    &mFwCfgSelectorAddressGuid,
+    (VOID *)&FwCfgSelectorAddress,
+    sizeof (UINT64)
+    );
+}
+
+/**
+  Build firmware configure data address HOB.
+
+  @param[in]   FwCfgDataAddress  Firmware configure data address.
+
+  @retval  NULL
+**/
+VOID
+QemuBuildFwCfgDataHob (
+  IN UINT64  FwCfgDataAddress
+  )
+{
+  BuildGuidDataHob (
+    &mFwCfgDataAddressGuid,
+    (VOID *)&FwCfgDataAddress,
+    sizeof (UINT64)
+    );
+}
+
+/**
+  Build firmware configure DMA address HOB.
+
+  @param[in]   FwCfgDmaAddress  Firmware configure DMA address.
+
+  @retval  NULL
+**/
+VOID
+QemuBuildFwCfgDmaHob (
+  IN UINT64  FwCfgDmaAddress
+  )
+{
+  BuildGuidDataHob (
+    &mFwCfgDmaAddressGuid,
+    (VOID *)&FwCfgDmaAddress,
+    sizeof (UINT64)
+    );
+}
+
+/**
+  To get firmware configure selector address.
+
+  @param VOID
+
+  @retval  firmware configure selector address
+**/
+UINTN
+EFIAPI
+QemuGetFwCfgSelectorAddress (
+  VOID
+  )
+{
+  UINTN              FwCfgSelectorAddress;
+  EFI_HOB_GUID_TYPE  *GuidHob;
+  VOID               *DataInHob;
+
+  FwCfgSelectorAddress = mFwCfgSelectorAddress;
+  GuidHob              = NULL;
+  DataInHob            = NULL;
+
+  if (FwCfgSelectorAddress == 0) {
+    GuidHob              = GetFirstGuidHob (&mFwCfgSelectorAddressGuid);
+    DataInHob            = GET_GUID_HOB_DATA (GuidHob);
+    FwCfgSelectorAddress = (UINT64)(*(UINTN *)DataInHob);
+  }
+
+  return FwCfgSelectorAddress;
+}
+
+/**
+  To get firmware configure Data address.
+
+  @param VOID
+
+  @retval  firmware configure data address
+**/
+UINTN
+EFIAPI
+QemuGetFwCfgDataAddress (
+  VOID
+  )
+{
+  UINTN              FwCfgDataAddress;
+  EFI_HOB_GUID_TYPE  *GuidHob;
+  VOID               *DataInHob;
+
+  FwCfgDataAddress = mFwCfgDataAddress;
+  GuidHob          = NULL;
+  DataInHob        = NULL;
+
+  if (FwCfgDataAddress == 0) {
+    GuidHob          = GetFirstGuidHob (&mFwCfgDataAddressGuid);
+    DataInHob        = GET_GUID_HOB_DATA (GuidHob);
+    FwCfgDataAddress = (UINT64)(*(UINTN *)DataInHob);
+  }
+
+  return FwCfgDataAddress;
+}
+
+/**
+  To get firmware DMA address.
+
+  @param VOID
+
+  @retval  firmware DMA address
+**/
+UINTN
+EFIAPI
+QemuGetFwCfgDmaAddress (
+  VOID
+  )
+{
+  UINTN              FwCfgDmaAddress;
+  EFI_HOB_GUID_TYPE  *GuidHob;
+  VOID               *DataInHob;
+
+  FwCfgDmaAddress = mFwCfgDmaAddress;
+  GuidHob          = NULL;
+  DataInHob        = NULL;
+
+  if (FwCfgDmaAddress == 0) {
+    GuidHob          = GetFirstGuidHob (&mFwCfgDmaAddressGuid);
+    DataInHob        = GET_GUID_HOB_DATA (GuidHob);
+    FwCfgDmaAddress = (UINT64)(*(UINTN *)DataInHob);
+  }
+
+  return FwCfgDmaAddress;
+}
+
 /**
   Returns a boolean indicating if the firmware configuration interface
   is available or not.
@@ -37,7 +193,7 @@ QemuFwCfgIsAvailable (
   VOID
   )
 {
-  return (BOOLEAN)(mFwCfgSelectorAddress != 0 && mFwCfgDataAddress != 0);
+  return (BOOLEAN)(QemuGetFwCfgSelectorAddress () != 0 && 
QemuGetFwCfgDataAddress () != 0);
 }
 
 /**
@@ -56,7 +212,7 @@ QemuFwCfgSelectItem (
   )
 {
   if (QemuFwCfgIsAvailable ()) {
-    MmioWrite16 (mFwCfgSelectorAddress, SwapBytes16 ((UINT16)QemuFwCfgItem));
+    MmioWrite16 (QemuGetFwCfgSelectorAddress (), SwapBytes16 
((UINT16)QemuFwCfgItem));
   }
 }
 
@@ -86,30 +242,30 @@ MmioReadBytes (
 
  #if defined (MDE_CPU_AARCH64) || defined (MDE_CPU_RISCV64) || defined 
(MDE_CPU_LOONGARCH64)
   while (Ptr < End) {
-    *(UINT64 *)Ptr = MmioRead64 (mFwCfgDataAddress);
+    *(UINT64 *)Ptr = MmioRead64 (QemuGetFwCfgDataAddress ());
     Ptr           += 8;
   }
 
   if (Left & 4) {
-    *(UINT32 *)Ptr = MmioRead32 (mFwCfgDataAddress);
+    *(UINT32 *)Ptr = MmioRead32 (QemuGetFwCfgDataAddress ());
     Ptr           += 4;
   }
 
  #else
   while (Ptr < End) {
-    *(UINT32 *)Ptr = MmioRead32 (mFwCfgDataAddress);
+    *(UINT32 *)Ptr = MmioRead32 (QemuGetFwCfgDataAddress ());
     Ptr           += 4;
   }
 
  #endif
 
   if (Left & 2) {
-    *(UINT16 *)Ptr = MmioRead16 (mFwCfgDataAddress);
+    *(UINT16 *)Ptr = MmioRead16 (QemuGetFwCfgDataAddress ());
     Ptr           += 2;
   }
 
   if (Left & 1) {
-    *Ptr = MmioRead8 (mFwCfgDataAddress);
+    *Ptr = MmioRead8 (QemuGetFwCfgDataAddress ());
   }
 }
 
@@ -162,9 +318,9 @@ DmaTransferBytes (
   // This will fire off the transfer.
   //
  #if defined (MDE_CPU_AARCH64) || defined (MDE_CPU_RISCV64) || defined 
(MDE_CPU_LOONGARCH64)
-  MmioWrite64 (mFwCfgDmaAddress, SwapBytes64 ((UINT64)&Access));
+  MmioWrite64 (QemuGetFwCfgDmaAddress (), SwapBytes64 ((UINT64)&Access));
  #else
-  MmioWrite32 ((UINT32)(mFwCfgDmaAddress + 4), SwapBytes32 ((UINT32)&Access));
+  MmioWrite32 ((UINT32)(QemuGetFwCfgDmaAddress () + 4), SwapBytes32 
((UINT32)&Access));
  #endif
 
   //
@@ -233,7 +389,7 @@ MmioWriteBytes (
   UINTN  Idx;
 
   for (Idx = 0; Idx < Size; ++Idx) {
-    MmioWrite8 (mFwCfgDataAddress, ((UINT8 *)Buffer)[Idx]);
+    MmioWrite8 (QemuGetFwCfgDataAddress (), ((UINT8 *)Buffer)[Idx]);
   }
 }
 
diff --git a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmio.inf 
b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmio.inf
index f2596f270e..8e191f2d22 100644
--- a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmio.inf
+++ b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmio.inf
@@ -40,6 +40,7 @@ [LibraryClasses]
   BaseLib
   BaseMemoryLib
   DebugLib
+  HobLib
   IoLib
   UefiBootServicesTableLib
 
diff --git a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmioInternal.h 
b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmioInternal.h
index d7d645f700..18148f9997 100644
--- a/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmioInternal.h
+++ b/OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgLibMmioInternal.h
@@ -16,6 +16,21 @@ extern UINTN  mFwCfgSelectorAddress;
 extern UINTN  mFwCfgDataAddress;
 extern UINTN  mFwCfgDmaAddress;
 
+#define FW_CONFIG_SELECTOR_ADDRESS_HOB_GUID \
+  { \
+    0x3cc47b04, 0x0d3e, 0xaa64, { 0x06, 0xa6, 0x4b, 0xdc, 0x9a, 0x2c, 0x61, 
0x19 } \
+  }
+
+#define FW_CONFIG_DATA_ADDRESS_HOB_GUID \
+  { \
+    0xef854788, 0x10f3, 0x8e7a, { 0x3e, 0xd0, 0x4d, 0x16, 0xc1, 0x79, 0x55, 
0x2f } \
+  }
+
+#define FW_CONFIG_DMA_ADDRESS_HOB_GUID \
+  { \
+    0x547cfaee, 0xa056, 0x3183, { 0x97, 0x47, 0x19, 0x83, 0x77, 0xd2, 0xa1, 
0x07 } \
+  }
+
 /**
   Reads firmware configuration bytes into a buffer
 
@@ -96,6 +111,42 @@ EFIAPI
   IN UINTN  Size
   );
 
+/**
+  Build firmware configure selector address HOB.
+
+  @param[in]   FwCfgSelectorAddress  Firmware configure selector address
+
+  @retval  NULL
+**/
+VOID
+QemuBuildFwCfgSelectorHob (
+  IN UINT64  FwCfgSelectorAddress
+  );
+
+/**
+  Build firmware configure data address HOB.
+
+  @param[in]   FwCfgDataAddress  Firmware configure data address.
+
+  @retval  NULL
+**/
+VOID
+QemuBuildFwCfgDataHob (
+  IN UINT64  FwCfgDataAddress
+  );
+
+/**
+  Build firmware configure DMA address HOB.
+
+  @param[in]   FwCfgDmaAddress  Firmware configure DMA address.
+
+  @retval  NULL
+**/
+VOID
+QemuBuildFwCfgDmaHob (
+  IN UINT64  FwCfgDmaAddress
+  );
+
 /**
   Slow READ_BYTES_FUNCTION.
 **/
-- 
2.27.0



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118257): https://edk2.groups.io/g/devel/message/118257
Mute This Topic: https://groups.io/mt/105724969/21656
Group Owner: devel+ow...@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [arch...@mail-archive.com]
-=-=-=-=-=-=-=-=-=-=-=-


Reply via email to