Create a new PCD boolean token in MdeModulePkg for global use.
We use this token to indicate if the configuration, parsed from
fw-cfg, requires pre-populated BARs to be preserved.

During creation of root bridges configurations, the flag is set
according to the "pre-populated-bars" item in fw-cfg. The Pcd token
is created as a dynamic item so it can be modified at runtime and
it is consumed in both Pei and Dxe PCI modules.

In virtualized environments, the hypervisor provides a layer of
isolation between the VMs and the hardware. This isolation may
include address translations and shadowing of configurations that
prevent guests directly modifying hardware registers. The hypervisor
then takes care of emulating the hardware accesses requested by
the guest. In some cases, the host may want the guest to use a
specific value for some or all PCI BARs so that transactions in
GPA level (particularly DMA) can make use of the controlled set
of addresses. The host then indicates the guest to preserve
those BARs that are pre-populated, that is, BARs that already
report a value even before guest's firmware BAR placement.

The token provides a globally accessible configuration flag to
determine, during PCI BAR allocation, if pre-populated BARs must
be respected. The pre-allocated PCI BARs are used in platforms in
which MMIO resources are configured with the same host physical
addresses in the guest so that DMA transactions can happen between
PCI devices without packets going through the IOMMU. Performance is
improved due to PCI packets travelling shorter distances and
avoiding links reaching the Root Complex, which can get busy during
I/O intensive periods.

Cc: Alexander Graf <g...@amazon.de>
Cc: Gerd Hoffmann <kra...@redhat.com>

Signed-off-by: Nicolas Ojeda Leon <ncol...@amazon.com>
---
 MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf    |  1 +
 MdeModulePkg/MdeModulePkg.dec                   |  6 ++++++
 .../PciHostBridgeUtilityLib.c                   | 17 +++++++++++++++++
 .../PciHostBridgeUtilityLib.inf                 |  4 ++++
 OvmfPkg/OvmfPkgX64.dsc                          |  1 +
 OvmfPkg/PlatformPei/PlatformPei.inf             |  1 +
 6 files changed, 30 insertions(+)

diff --git a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf 
b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
index e317169d9c..046876bb3b 100644
--- a/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
+++ b/MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
@@ -107,6 +107,7 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdMrIovSupport                ## CONSUMES
   gEfiMdeModulePkgTokenSpaceGuid.PcdPciDisableBusEnumeration    ## 
SOMETIMES_CONSUMES
   gEfiMdeModulePkgTokenSpaceGuid.PcdPcieResizableBarSupport     ## CONSUMES
+  gEfiMdeModulePkgTokenSpaceGuid.PcdPciPreservePopulatedMappings## CONSUMES
 
 [UserExtensions.TianoCore."ExtraFiles"]
   PciBusDxeExtra.uni
diff --git a/MdeModulePkg/MdeModulePkg.dec b/MdeModulePkg/MdeModulePkg.dec
index 463e889e9a..078877ba7f 100644
--- a/MdeModulePkg/MdeModulePkg.dec
+++ b/MdeModulePkg/MdeModulePkg.dec
@@ -1902,6 +1902,12 @@
   # @Prompt Disable full PCI enumeration.
   
gEfiMdeModulePkgTokenSpaceGuid.PcdPciDisableBusEnumeration|FALSE|BOOLEAN|0x10000048
 
+  ## The flag to control preservation of pre-populated PCI BARs
+  #   TRUE  - Respect pre-populated PCI BARs
+  #   FALSE - No pre-populated BARs, place all BARs
+  # @Prompt Enable preservsation of pre-populated PCI BARs
+  
gEfiMdeModulePkgTokenSpaceGuid.PcdPciPreservePopulatedMappings|FALSE|BOOLEAN|0x10000050
+
   ## Disk I/O - Number of Data Buffer block.
   # Define the size in block of the pre-allocated buffer. It provide better
   # performance for large Disk I/O requests.
diff --git a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c 
b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c
index b0e3b5e3cf..5c86f67f76 100644
--- a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c
+++ b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c
@@ -18,6 +18,7 @@
 #include <Library/DevicePathLib.h>
 #include <Library/HardwareInfoLib.h>
 #include <Library/MemoryAllocationLib.h>
+#include <Library/PcdLib.h>
 #include <Library/PciHostBridgeUtilityLib.h>
 #include <Library/PciLib.h>
 #include <Library/QemuFwCfgLib.h>
@@ -260,6 +261,7 @@ PciHostBridgeUtilityGetRootBridges (
   PCI_ROOT_BRIDGE_APERTURE  HwInfoMemAbove4G;
   PCI_ROOT_BRIDGE_APERTURE  HwInfoPMem;
   PCI_ROOT_BRIDGE_APERTURE  HwInfoPMemAbove4G;
+  UINT64                    PrePopulatedBars;
 
   *Count = 0;
 
@@ -312,6 +314,21 @@ PciHostBridgeUtilityGetRootBridges (
       ));
   }
 
+  //
+  // Find file for pre-populated bars and set Pcd token if enabled
+  //
+  Status           = QemuFwCfgFindFile ("etc/pre-populated-bars", &FwCfgItem, 
&FwCfgSize);
+  PrePopulatedBars = 0;
+  if (!EFI_ERROR (Status) && (FwCfgSize == sizeof (PrePopulatedBars))) {
+    QemuFwCfgSelectItem (FwCfgItem);
+    QemuFwCfgReadBytes (FwCfgSize, &PrePopulatedBars);
+
+    if (PrePopulatedBars) {
+      PcdSetBoolS (PcdPciPreservePopulatedMappings, TRUE);
+      DEBUG ((DEBUG_INFO, "%a: Pre-populated BARs present\n", __FUNCTION__));
+    }
+  }
+
   //
   // Initialize the Hardware Info list head to start with an empty but valid
   // list head.
diff --git 
a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf 
b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf
index e4fc903121..570bf7e97c 100644
--- a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf
+++ b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf
@@ -40,5 +40,9 @@
   DevicePathLib
   DxeHardwareInfoLib
   MemoryAllocationLib
+  PcdLib
   PciLib
   QemuFwCfgLib
+
+[Pcd]
+  gEfiMdeModulePkgTokenSpaceGuid.PcdPciPreservePopulatedMappings
diff --git a/OvmfPkg/OvmfPkgX64.dsc b/OvmfPkg/OvmfPkgX64.dsc
index b376387b7c..1656509f03 100644
--- a/OvmfPkg/OvmfPkgX64.dsc
+++ b/OvmfPkg/OvmfPkgX64.dsc
@@ -594,6 +594,7 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwWorkingBase|0
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareBase|0
 !endif
+  gEfiMdeModulePkgTokenSpaceGuid.PcdPciPreservePopulatedMappings|FALSE
   gEfiMdeModulePkgTokenSpaceGuid.PcdVideoHorizontalResolution|800
   gEfiMdeModulePkgTokenSpaceGuid.PcdVideoVerticalResolution|600
   gEfiMdeModulePkgTokenSpaceGuid.PcdAcpiS3Enable|FALSE
diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf 
b/OvmfPkg/PlatformPei/PlatformPei.inf
index e840f960d3..b58f79c6e3 100644
--- a/OvmfPkg/PlatformPei/PlatformPei.inf
+++ b/OvmfPkg/PlatformPei/PlatformPei.inf
@@ -96,6 +96,7 @@
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageFtwSpareSize
   gEfiMdeModulePkgTokenSpaceGuid.PcdFlashNvStorageVariableSize
   gEfiMdeModulePkgTokenSpaceGuid.PcdEmuVariableNvStoreReserved
+  gEfiMdeModulePkgTokenSpaceGuid.PcdPciPreservePopulatedMappings
   gEfiMdeModulePkgTokenSpaceGuid.PcdDxeIplSwitchToLongMode
   gEfiMdeModulePkgTokenSpaceGuid.PcdUse1GPageTable
   gEfiMdeModulePkgTokenSpaceGuid.PcdSetNxForStack
-- 
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





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


Reply via email to