public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Ojeda Leon, Nicolas" <ncoleon@amazon.com>
To: <devel@edk2.groups.io>
Cc: <atugup@amazon.com>, Nicolas Ojeda Leon <ncoleon@amazon.com>,
	Alexander Graf <graf@amazon.de>,
	Gerd Hoffmann <kraxel@redhat.com>
Subject: [PATCH v3 4/8] Ovmf/PlatformPei: Use host-provided GPA end if available
Date: Tue, 25 Jan 2022 15:30:11 +0100	[thread overview]
Message-ID: <fbdaf49254bc8853ebd5056ea2383d3ba7dc034d.1643120206.git.ncoleon@amazon.com> (raw)
In-Reply-To: <cover.1643120206.git.ncoleon@amazon.com>

Read the "hardware-info" item from fw-cfg to extract specifications
of PCI host bridges and analyze the 64-bit apertures of them to
find out the highest 64-bit MMIO address required which determines
the address space required by the guest, and, consequently, the
FirstNonAddress used to calculate size of physical addresses.

Using the static PeiHardwareInfoLib, read the fw-cfg file of
hardware information to extract, one by one, all the host
bridges. Find the last 64-bit MMIO address of each host bridge,
using the HardwareInfoPciHostBridgeLib API, and compare it to an
accumulate value to discover the highest address used, which
corresponds to the highest value that must be included in the
guest's physical address space.

Given that platforms with multiple host bridges may provide the PCI
apertures' addresses, the memory detection logic must take into
account that, if the host provided the MMIO windows that can and must
be used, the guest needs to take those values. Therefore, if the
MMIO windows are found in the host-provided fw-cfg file, skip all the
logic calculating the physical address size and just use the value
provided. Since each PCI host bridge corresponds to an element in
the information provided by the host, each of these must be analyzed
looking for the highest address used.

Cc: Alexander Graf <graf@amazon.de>
Cc: Gerd Hoffmann <kraxel@redhat.com>

Signed-off-by: Nicolas Ojeda Leon <ncoleon@amazon.com>
---
 OvmfPkg/PlatformPei/MemDetect.c     | 146 ++++++++++++++++++++++++++--
 OvmfPkg/PlatformPei/PlatformPei.inf |   1 +
 2 files changed, 141 insertions(+), 6 deletions(-)

diff --git a/OvmfPkg/PlatformPei/MemDetect.c b/OvmfPkg/PlatformPei/MemDetect.c
index 1bcb5a08bc..26f6df7e7e 100644
--- a/OvmfPkg/PlatformPei/MemDetect.c
+++ b/OvmfPkg/PlatformPei/MemDetect.c
@@ -36,6 +36,7 @@ Module Name:
 #include <Library/MtrrLib.h>
 #include <Library/QemuFwCfgLib.h>
 #include <Library/QemuFwCfgSimpleParserLib.h>
+#include <Library/HardwareInfoLib.h>
 
 #include "Platform.h"
 #include "Cmos.h"
@@ -369,6 +370,126 @@ GetSystemMemorySizeAbove4gb (
   return LShiftU64 (Size, 16);
 }
 
+/**
+  Iterate over the PCI host bridges resources information optionally provided
+  in fw-cfg and find the highest address contained in the PCI MMIO windows. If
+  the information is found, return the exclusive end; one past the last usable
+  address.
+
+  @param[out] PciMmioAddressEnd Pointer to one-after End Address updated with
+                                information extracted from host-provided data
+                                or zero if no information available or an
+                                error happened
+
+  @retval EFI_SUCCESS               PCI information was read and the output
+                                    parameter updated with the last valid
+                                    address in the 64-bit MMIO range.
+  @retval EFI_INVALID_PARAMETER     Pointer parameter is invalid
+  @retval EFI_INCOMPATIBLE_VERSION  Hardware information found in fw-cfg
+                                    has an incompatible format
+  @retval EFI_UNSUPPORTED           Fw-cfg is not supported, thus host
+                                    provided information, if any, cannot be
+                                    read
+  @retval EFI_NOT_FOUND             No PCI host bridge information provided
+                                    by the host.
+**/
+STATIC
+EFI_STATUS
+ScanHostProvided64BitPciMmioEnd (
+  OUT UINT64  *PciMmioAddressEnd
+  )
+{
+  EFI_STATUS            Status;
+  HOST_BRIDGE_INFO      HostBridge;
+  FIRMWARE_CONFIG_ITEM  FwCfgItem;
+  UINTN                 FwCfgSize;
+  UINTN                 FwCfgReadIndex;
+  UINTN                 ReadDataSize;
+  UINT64                Above4GMmioEnd;
+
+  if (PciMmioAddressEnd == NULL) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  *PciMmioAddressEnd = 0;
+  Above4GMmioEnd     = 0;
+
+  Status = QemuFwCfgFindFile ("etc/hardware-info", &FwCfgItem, &FwCfgSize);
+  if (EFI_ERROR (Status)) {
+    return Status;
+  }
+
+  QemuFwCfgSelectItem (FwCfgItem);
+
+  FwCfgReadIndex = 0;
+  while (FwCfgReadIndex < FwCfgSize) {
+    Status = QemuFwCfgReadNextHardwareInfoByType (
+               HardwareInfoTypeHostBridge,
+               sizeof (HostBridge),
+               FwCfgSize,
+               &HostBridge,
+               &ReadDataSize,
+               &FwCfgReadIndex
+               );
+
+    if (Status != EFI_SUCCESS) {
+      //
+      // No more data available to read in the file, break
+      // loop and finish process
+      //
+      break;
+    }
+
+    Status = HardwareInfoPciHostBridgeLastMmioAddress (
+               &HostBridge,
+               ReadDataSize,
+               TRUE,
+               &Above4GMmioEnd
+               );
+
+    if (Status != EFI_SUCCESS) {
+      //
+      // Error parsing MMIO apertures and extracting last MMIO
+      // address, reset PciMmioAddressEnd as if no information was
+      // found, to avoid moving forward with incomplete data, and
+      // bail out
+      //
+      DEBUG ((
+        DEBUG_ERROR,
+        "%a: ignoring malformed hardware information from fw_cfg\n",
+        __FUNCTION__
+        ));
+      *PciMmioAddressEnd = 0;
+      return Status;
+    }
+
+    if (Above4GMmioEnd > *PciMmioAddressEnd) {
+      *PciMmioAddressEnd = Above4GMmioEnd;
+    }
+  }
+
+  if (*PciMmioAddressEnd > 0) {
+    //
+    // Host-provided PCI information was found and a MMIO window end
+    // derived from it.
+    // Increase the End address by one to have the output pointing to
+    // one after the address in use (exclusive end).
+    //
+    *PciMmioAddressEnd += 1;
+
+    DEBUG ((
+      DEBUG_INFO,
+      "%a: Pci64End=0x%Lx\n",
+      __FUNCTION__,
+      *PciMmioAddressEnd
+      ));
+
+    return EFI_SUCCESS;
+  }
+
+  return EFI_NOT_FOUND;
+}
+
 /**
   Return the highest address that DXE could possibly use, plus one.
 **/
@@ -550,15 +671,28 @@ AddressWidthInitialization (
   VOID
   )
 {
-  UINT64  FirstNonAddress;
+  UINT64      FirstNonAddress;
+  EFI_STATUS  Status;
 
   //
-  // As guest-physical memory size grows, the permanent PEI RAM requirements
-  // are dominated by the identity-mapping page tables built by the DXE IPL.
-  // The DXL IPL keys off of the physical address bits advertized in the CPU
-  // HOB. To conserve memory, we calculate the minimum address width here.
+  // First scan host-provided hardware information to assess if the address
+  // space is already known. If so, guest must use those values.
   //
-  FirstNonAddress      = GetFirstNonAddress ();
+  Status = ScanHostProvided64BitPciMmioEnd (&FirstNonAddress);
+
+  if (EFI_ERROR (Status)) {
+    //
+    // If the host did not provide valid hardware information leading to a
+    // hard-defined 64-bit MMIO end, fold back to calculating the minimum range
+    // needed.
+    // As guest-physical memory size grows, the permanent PEI RAM requirements
+    // are dominated by the identity-mapping page tables built by the DXE IPL.
+    // The DXL IPL keys off of the physical address bits advertized in the CPU
+    // HOB. To conserve memory, we calculate the minimum address width here.
+    //
+    FirstNonAddress = GetFirstNonAddress ();
+  }
+
   mPhysMemAddressWidth = (UINT8)HighBitSet64 (FirstNonAddress);
 
   //
diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf b/OvmfPkg/PlatformPei/PlatformPei.inf
index 8ef404168c..e840f960d3 100644
--- a/OvmfPkg/PlatformPei/PlatformPei.inf
+++ b/OvmfPkg/PlatformPei/PlatformPei.inf
@@ -64,6 +64,7 @@
   MemEncryptSevLib
   PcdLib
   VmgExitLib
+  PeiHardwareInfoLib
 
 [Pcd]
   gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvBase
-- 
2.17.1




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879




  parent reply	other threads:[~2022-01-25 14:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-25 14:30 [PATCH v3 0/8] Handling of multiple PCI host bridges specified Ojeda Leon, Nicolas
2022-01-25 14:30 ` [PATCH v3 1/8] OvmfPkg/Library: Create base HardwareInfoLib for PCI Host Bridges Ojeda Leon, Nicolas
2022-01-28 10:36   ` Gerd Hoffmann
2022-01-25 14:30 ` [PATCH v3 2/8] Ovmf/HardwareInfoLib: Create Pei lib to parse directly from fw-cfg Ojeda Leon, Nicolas
2022-01-28 10:36   ` Gerd Hoffmann
2022-01-25 14:30 ` [PATCH v3 3/8] Ovmf/HardwareInfoLib: Add Dxe lib to dynamically parse heterogenous data Ojeda Leon, Nicolas
2022-01-28 10:36   ` Gerd Hoffmann
2022-01-25 14:30 ` Ojeda Leon, Nicolas [this message]
2022-01-28 10:37   ` [PATCH v3 4/8] Ovmf/PlatformPei: Use host-provided GPA end if available Gerd Hoffmann
2022-01-25 14:35 ` [PATCH v3 5/8] OvmfPkg/PciHostBridgeUtilityLib: Initialize RootBridges apertures with spec Ojeda Leon, Nicolas
2022-01-28 10:41   ` [edk2-devel] " Gerd Hoffmann
2022-01-25 14:36 ` [PATCH v3 6/8] MdeModulePkg, OvmfPkg: Add Pcd token for PCI pre-populated BARs Ojeda Leon, Nicolas
2022-01-25 14:37 ` [PATCH v3 7/8] MdeModulePkg/Pci MdePkg: Create service to retrieve PCI base addresses Ojeda Leon, Nicolas
2022-01-28 10:52   ` [edk2-devel] " Gerd Hoffmann
2022-01-29  1:44     ` 回复: " gaoliming
2022-01-25 14:38 ` [PATCH v3 8/8] MdeModulePkg/PciBusDxe: Handling of pre-populated PCI BARs Ojeda Leon, Nicolas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fbdaf49254bc8853ebd5056ea2383d3ba7dc034d.1643120206.git.ncoleon@amazon.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox