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 5/8] OvmfPkg/PciHostBridgeUtilityLib: Initialize RootBridges apertures with spec
Date: Tue, 25 Jan 2022 15:35:55 +0100	[thread overview]
Message-ID: <b6957669bf5803b3c8e8004558c208e44a63864f.1643120206.git.ncoleon@amazon.com> (raw)
In-Reply-To: <cover.1643120206.git.ncoleon@amazon.com>

Consume the host-provided specification of PCI host bridges if
available. Using the DxeHardwareInfoLib, populate a list of
hardware descriptors based on the content of the "hardware-info"
fw-cfg file, if provided. In the affirmative case, use the
resources and attributes specified by the hypervisor for each
Host Bridge to create the RootBridge elements.

In Ovmf platforms, the host can provide the specification of
non-discoverable hardware resources like PCI host bridges. If the
proper fw-cfg file is found, parse the contents provided by the
host into a linked list by using the Hardware Info library. Then,
using the list of PCI host bridges' descriptions, populate the
PCI_ROOT_BRIDGES array with the resources and attributes specified
by the host. If the file is not provided or no Host Bridge is found
in it, fold back to the legacy method based on pre-defined
apertures and rules.

In some use cases, the host requires additional control over the
hardware resources' configurations in the guest for performance and
discoverability reasons. For instance, to disclose information about
the PCI hierarchy to the guest so that this can profit from
optimized accesses. In this case, the host can decide to describe
multiple PCI Host Bridges and provide a specific set of resources
(e.g. MMIO apertures) so that the guest uses the values provided.
Using the provided values may entitle the guest to added performance,
for example by using specific MMIO mappings that can enable peer-to-peer
communication across the PCI hierarchy or by allocating memory closer
to a device for faster DMA transactions.

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

Signed-off-by: Nicolas Ojeda Leon <ncoleon@amazon.com>
---
 .../PciHostBridgeUtilityLib.c                 | 345 ++++++++++++++----
 .../PciHostBridgeUtilityLib.inf               |   1 +
 2 files changed, 268 insertions(+), 78 deletions(-)

diff --git a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c
index 92e1ea812f..b0e3b5e3cf 100644
--- a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c
+++ b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c
@@ -12,13 +12,16 @@
 
 #include <IndustryStandard/Acpi10.h>
 #include <IndustryStandard/Pci.h>
+#include <Library/BaseLib.h>
 #include <Library/BaseMemoryLib.h>
 #include <Library/DebugLib.h>
 #include <Library/DevicePathLib.h>
+#include <Library/HardwareInfoLib.h>
 #include <Library/MemoryAllocationLib.h>
 #include <Library/PciHostBridgeUtilityLib.h>
 #include <Library/PciLib.h>
 #include <Library/QemuFwCfgLib.h>
+#include <Protocol/PciHostBridgeResourceAllocation.h>
 
 #pragma pack(1)
 typedef struct {
@@ -234,14 +237,29 @@ PciHostBridgeUtilityGetRootBridges (
   IN  PCI_ROOT_BRIDGE_APERTURE  *PMemAbove4G
   )
 {
-  EFI_STATUS            Status;
-  FIRMWARE_CONFIG_ITEM  FwCfgItem;
-  UINTN                 FwCfgSize;
-  UINT64                ExtraRootBridges;
-  PCI_ROOT_BRIDGE       *Bridges;
-  UINTN                 Initialized;
-  UINTN                 LastRootBridgeNumber;
-  UINTN                 RootBridgeNumber;
+  EFI_STATUS                Status;
+  FIRMWARE_CONFIG_ITEM      FwCfgItem;
+  UINTN                     FwCfgSize;
+  UINT64                    ExtraRootBridges;
+  PCI_ROOT_BRIDGE           *Bridges;
+  UINTN                     Initialized;
+  UINTN                     LastRootBridgeNumber;
+  UINTN                     RootBridgeNumber;
+  UINTN                     PciHostBridgeCount;
+  UINT8                     *HardwareInfoBlob;
+  LIST_ENTRY                HwInfoList;
+  LIST_ENTRY                *HwLink;
+  HARDWARE_INFO             *HwInfo;
+  UINT64                    HwInfoAttributes;
+  UINT64                    HwInfoAllocationAttributes;
+  BOOLEAN                   HwInfoDmaAbove4G;
+  BOOLEAN                   HwInfoNoExtendedConfigSpace;
+  BOOLEAN                   HwInfoCombineMemPMem;
+  PCI_ROOT_BRIDGE_APERTURE  HwInfoIo;
+  PCI_ROOT_BRIDGE_APERTURE  HwInfoMem;
+  PCI_ROOT_BRIDGE_APERTURE  HwInfoMemAbove4G;
+  PCI_ROOT_BRIDGE_APERTURE  HwInfoPMem;
+  PCI_ROOT_BRIDGE_APERTURE  HwInfoPMemAbove4G;
 
   *Count = 0;
 
@@ -294,103 +312,266 @@ PciHostBridgeUtilityGetRootBridges (
       ));
   }
 
+  //
+  // Initialize the Hardware Info list head to start with an empty but valid
+  // list head.
+  //
+  InitializeListHead (&HwInfoList);
+  HardwareInfoBlob   =  NULL;
+  Initialized        = 0;
+  Bridges            = NULL;
+  PciHostBridgeCount = 0;
+
+  //
+  // Hypervisor can provide the specifications (resources) for one or more
+  // PCI host bridges. Such information comes through fw-cfg as part of
+  // the hardware-info file.
+  //
+  Status = QemuFwCfgFindFile ("etc/hardware-info", &FwCfgItem, &FwCfgSize);
+
+  if (!EFI_ERROR (Status)) {
+    HardwareInfoBlob = AllocatePool (FwCfgSize);
+
+    if (HardwareInfoBlob == NULL) {
+      DEBUG ((
+        DEBUG_ERROR,
+        "%a: Failed to allocate memory for hardware resources info\n",
+        __FUNCTION__
+        ));
+      return NULL;
+    }
+
+    QemuFwCfgSelectItem (FwCfgItem);
+    QemuFwCfgReadBytes (FwCfgSize, HardwareInfoBlob);
+
+    //
+    // Create the list of hardware info devices filtering for PCI host
+    // bridges
+    //
+    Status = CreateHardwareInfoList (
+               HardwareInfoBlob,
+               FwCfgSize,
+               HardwareInfoTypeHostBridge,
+               &HwInfoList
+               );
+
+    if (EFI_ERROR (Status)) {
+      DEBUG ((
+        DEBUG_ERROR,
+        "%a: Failed to create hardware info list to retrieve host "
+        "bridges information\n",
+        __FUNCTION__
+        ));
+
+      goto FreeBridges;
+    }
+
+    PciHostBridgeCount = GetHardwareInfoCountByType (
+                           &HwInfoList,
+                           HardwareInfoTypeHostBridge,
+                           sizeof (HOST_BRIDGE_INFO)
+                           );
+
+    if ((1 + ExtraRootBridges) != PciHostBridgeCount) {
+      DEBUG ((
+        DEBUG_ERROR,
+        "%a: Invalid number of host bridges in hardware info list. "
+        "Expected %ld, got %ld\n",
+        __FUNCTION__,
+        (1 + ExtraRootBridges),
+        PciHostBridgeCount
+        ));
+      goto FreeBridges;
+    }
+
+    DEBUG ((
+      DEBUG_INFO,
+      "%a: Resources for %Lu root buses found in fw-cfg\n",
+      __FUNCTION__,
+      PciHostBridgeCount
+      ));
+  }
+
   //
   // Allocate the "main" root bridge, and any extra root bridges.
   //
   Bridges = AllocatePool ((1 + (UINTN)ExtraRootBridges) * sizeof *Bridges);
   if (Bridges == NULL) {
     DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, EFI_OUT_OF_RESOURCES));
-    return NULL;
+    goto FreeBridges;
   }
 
-  Initialized = 0;
+  if (!IsListEmpty (&HwInfoList)) {
+    //
+    // If Host Bridges' specification was obtained from fw-cfg, the list
+    // contains information to populate all root bridges in the system
+    // including resources and attributes.
+    //
+    HwLink = GetFirstHardwareInfoByType (
+               &HwInfoList,
+               HardwareInfoTypeHostBridge,
+               sizeof (HOST_BRIDGE_INFO)
+               );
+
+    while (!EndOfHardwareInfoList (&HwInfoList, HwLink)) {
+      HwInfo = HARDWARE_INFO_FROM_LINK (HwLink);
+
+      HardwareInfoPciHostBridgeGet (
+        HwInfo->Data.PciHostBridge,
+        HwInfo->Header.Size,
+        &RootBridgeNumber,
+        &LastRootBridgeNumber,
+        &HwInfoAttributes,
+        &HwInfoDmaAbove4G,
+        &HwInfoNoExtendedConfigSpace,
+        &HwInfoCombineMemPMem,
+        &HwInfoIo,
+        &HwInfoMem,
+        &HwInfoMemAbove4G,
+        &HwInfoPMem,
+        &HwInfoPMemAbove4G,
+        NULL
+        );
 
-  //
-  // The "main" root bus is always there.
-  //
-  LastRootBridgeNumber = BusMin;
+      HwInfoAllocationAttributes = 0;
+      if (HwInfoCombineMemPMem) {
+        HwInfoAllocationAttributes |= EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM;
+      }
 
-  //
-  // Scan all other root buses. If function 0 of any device on a bus returns a
-  // VendorId register value different from all-bits-one, then that bus is
-  // alive.
-  //
-  for (RootBridgeNumber = BusMin + 1;
-       RootBridgeNumber <= BusMax && Initialized < ExtraRootBridges;
-       ++RootBridgeNumber)
-  {
-    UINTN  Device;
-
-    for (Device = 0; Device <= PCI_MAX_DEVICE; ++Device) {
-      if (PciRead16 (
-            PCI_LIB_ADDRESS (
-              RootBridgeNumber,
-              Device,
-              0,
-              PCI_VENDOR_ID_OFFSET
-              )
-            ) != MAX_UINT16)
+      if ((HwInfoMemAbove4G.Limit > HwInfoMemAbove4G.Base) ||
+          (HwInfoPMemAbove4G.Limit > HwInfoPMemAbove4G.Base))
       {
-        break;
+        HwInfoAllocationAttributes |= EFI_PCI_HOST_BRIDGE_MEM64_DECODE;
       }
-    }
 
-    if (Device <= PCI_MAX_DEVICE) {
-      //
-      // Found the next root bus. We can now install the *previous* one,
-      // because now we know how big a bus number range *that* one has, for any
-      // subordinate buses that might exist behind PCI bridges hanging off it.
-      //
       Status = PciHostBridgeUtilityInitRootBridge (
-                 Attributes,
-                 Attributes,
-                 AllocationAttributes,
-                 DmaAbove4G,
-                 NoExtendedConfigSpace,
-                 (UINT8)LastRootBridgeNumber,
-                 (UINT8)(RootBridgeNumber - 1),
-                 Io,
-                 Mem,
-                 MemAbove4G,
-                 PMem,
-                 PMemAbove4G,
+                 HwInfoAttributes,
+                 HwInfoAttributes,
+                 HwInfoAllocationAttributes,
+                 HwInfoDmaAbove4G,
+                 HwInfoNoExtendedConfigSpace,
+                 RootBridgeNumber,
+                 LastRootBridgeNumber,
+                 &HwInfoIo,
+                 &HwInfoMem,
+                 &HwInfoMemAbove4G,
+                 &HwInfoPMem,
+                 &HwInfoPMemAbove4G,
                  &Bridges[Initialized]
                  );
+
       if (EFI_ERROR (Status)) {
         goto FreeBridges;
       }
 
       ++Initialized;
-      LastRootBridgeNumber = RootBridgeNumber;
+
+      HwLink = GetNextHardwareInfoByType (
+                 &HwInfoList,
+                 HwLink,
+                 HardwareInfoTypeHostBridge,
+                 sizeof (HOST_BRIDGE_INFO)
+                 );
     }
+  } else {
+    Initialized = 0;
+
+    //
+    // The "main" root bus is always there.
+    //
+    LastRootBridgeNumber = BusMin;
+
+    //
+    // Scan all other root buses. If function 0 of any device on a bus returns a
+    // VendorId register value different from all-bits-one, then that bus is
+    // alive.
+    //
+    for (RootBridgeNumber = BusMin + 1;
+         RootBridgeNumber <= BusMax && Initialized < ExtraRootBridges;
+         ++RootBridgeNumber)
+    {
+      UINTN  Device;
+
+      for (Device = 0; Device <= PCI_MAX_DEVICE; ++Device) {
+        if (PciRead16 (
+              PCI_LIB_ADDRESS (
+                RootBridgeNumber,
+                Device,
+                0,
+                PCI_VENDOR_ID_OFFSET
+                )
+              ) != MAX_UINT16)
+        {
+          break;
+        }
+      }
+
+      if (Device <= PCI_MAX_DEVICE) {
+        //
+        // Found the next root bus. We can now install the *previous* one,
+        // because now we know how big a bus number range *that* one has, for any
+        // subordinate buses that might exist behind PCI bridges hanging off it.
+        //
+        Status = PciHostBridgeUtilityInitRootBridge (
+                   Attributes,
+                   Attributes,
+                   AllocationAttributes,
+                   DmaAbove4G,
+                   NoExtendedConfigSpace,
+                   (UINT8)LastRootBridgeNumber,
+                   (UINT8)(RootBridgeNumber - 1),
+                   Io,
+                   Mem,
+                   MemAbove4G,
+                   PMem,
+                   PMemAbove4G,
+                   &Bridges[Initialized]
+                   );
+        if (EFI_ERROR (Status)) {
+          goto FreeBridges;
+        }
+
+        ++Initialized;
+        LastRootBridgeNumber = RootBridgeNumber;
+      }
+    }
+
+    //
+    // Install the last root bus (which might be the only, ie. main, root bus, if
+    // we've found no extra root buses).
+    //
+    Status = PciHostBridgeUtilityInitRootBridge (
+               Attributes,
+               Attributes,
+               AllocationAttributes,
+               DmaAbove4G,
+               NoExtendedConfigSpace,
+               (UINT8)LastRootBridgeNumber,
+               (UINT8)BusMax,
+               Io,
+               Mem,
+               MemAbove4G,
+               PMem,
+               PMemAbove4G,
+               &Bridges[Initialized]
+               );
+    if (EFI_ERROR (Status)) {
+      goto FreeBridges;
+    }
+
+    ++Initialized;
   }
 
+  *Count = Initialized;
+
+  // If resources were allocated for host bridges info, release them
   //
-  // Install the last root bus (which might be the only, ie. main, root bus, if
-  // we've found no extra root buses).
-  //
-  Status = PciHostBridgeUtilityInitRootBridge (
-             Attributes,
-             Attributes,
-             AllocationAttributes,
-             DmaAbove4G,
-             NoExtendedConfigSpace,
-             (UINT8)LastRootBridgeNumber,
-             (UINT8)BusMax,
-             Io,
-             Mem,
-             MemAbove4G,
-             PMem,
-             PMemAbove4G,
-             &Bridges[Initialized]
-             );
-  if (EFI_ERROR (Status)) {
-    goto FreeBridges;
+  if (HardwareInfoBlob) {
+    FreePool (HardwareInfoBlob);
   }
 
-  ++Initialized;
+  FreeHardwareInfoList (&HwInfoList);
 
-  *Count = Initialized;
   return Bridges;
 
 FreeBridges:
@@ -399,7 +580,15 @@ FreeBridges:
     PciHostBridgeUtilityUninitRootBridge (&Bridges[Initialized]);
   }
 
-  FreePool (Bridges);
+  if (Bridges) {
+    FreePool (Bridges);
+  }
+
+  if (HardwareInfoBlob) {
+    FreePool (HardwareInfoBlob);
+  }
+
+  FreeHardwareInfoList (&HwInfoList);
   return NULL;
 }
 
diff --git a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf
index 83a734c172..e4fc903121 100644
--- a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf
+++ b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.inf
@@ -38,6 +38,7 @@
   BaseMemoryLib
   DebugLib
   DevicePathLib
+  DxeHardwareInfoLib
   MemoryAllocationLib
   PciLib
   QemuFwCfgLib
-- 
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:36 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 ` [PATCH v3 4/8] Ovmf/PlatformPei: Use host-provided GPA end if available Ojeda Leon, Nicolas
2022-01-28 10:37   ` Gerd Hoffmann
2022-01-25 14:35 ` Ojeda Leon, Nicolas [this message]
2022-01-28 10:41   ` [edk2-devel] [PATCH v3 5/8] OvmfPkg/PciHostBridgeUtilityLib: Initialize RootBridges apertures with spec 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=b6957669bf5803b3c8e8004558c208e44a63864f.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