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 v4 5/8] OvmfPkg/PciHostBridgeUtilityLib: Initialize RootBridges apertures with spec
Date: Thu, 3 Feb 2022 21:56:25 +0100	[thread overview]
Message-ID: <048b039ecfad8765487246c4b1c5d928a91c57aa.1643919691.git.ncoleon@amazon.com> (raw)
In-Reply-To: <cover.1643919691.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>
---
Notes:
   v4:
   - Splitted PciHostBridgeUtilityGetRootBridges into 2 smaller functions,
     one that performs same BusScan population and a new one that uses
     HostProvided values to populate Root Bridges.
   - PciHostBridgeUtilityGetRootBridges is left as a thin wrapper that calls
     the 2 functions depending on availability of host-provided data.

 .../PciHostBridgeUtilityLib.c                 | 316 +++++++++++++++++-
 .../PciHostBridgeUtilityLib.inf               |   1 +
 2 files changed, 312 insertions(+), 5 deletions(-)

diff --git a/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c b/OvmfPkg/Library/PciHostBridgeUtilityLib/PciHostBridgeUtilityLib.c
index 92e1ea812f..4bd4671f10 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 {
@@ -189,7 +192,9 @@ PciHostBridgeUtilityUninitRootBridge (
 }
 
 /**
-  Utility function to return all the root bridge instances in an array.
+  Utility function to scan PCI root bridges and create instances for those
+  that are found not empty. Populate their resources from the default
+  provided parameters and return all the root bridge instances in an array.
 
   @param[out] Count                  The number of root bridge instances.
 
@@ -217,9 +222,9 @@ PciHostBridgeUtilityUninitRootBridge (
 
   @return                            All the root bridge instances in an array.
 **/
+STATIC
 PCI_ROOT_BRIDGE *
-EFIAPI
-PciHostBridgeUtilityGetRootBridges (
+PciHostBridgeUtilityGetRootBridgesBusScan (
   OUT UINTN                     *Count,
   IN  UINT64                    Attributes,
   IN  UINT64                    AllocationAttributes,
@@ -243,8 +248,6 @@ PciHostBridgeUtilityGetRootBridges (
   UINTN                 LastRootBridgeNumber;
   UINTN                 RootBridgeNumber;
 
-  *Count = 0;
-
   if ((BusMin > BusMax) || (BusMax > PCI_MAX_BUS)) {
     DEBUG ((
       DEBUG_ERROR,
@@ -403,6 +406,309 @@ FreeBridges:
   return NULL;
 }
 
+/**
+  Utility function to read root bridges information from host-provided fw-cfg
+  file and return them in an array.
+
+  @param[out] Count   The number of root bridge instances.
+
+  @return             All the root bridge instances in an array parsed from
+                      host-provided fw-cfg file (hardware-info).
+**/
+STATIC
+PCI_ROOT_BRIDGE *
+PciHostBridgeUtilityGetRootBridgesHostProvided (
+  OUT UINTN  *Count
+  )
+{
+  EFI_STATUS                Status;
+  FIRMWARE_CONFIG_ITEM      FwCfgItem;
+  UINTN                     FwCfgSize;
+  PCI_ROOT_BRIDGE           *Bridges;
+  UINTN                     Initialized;
+  UINTN                     LastRootBridgeNumber;
+  UINTN                     RootBridgeNumber;
+  UINTN                     PciHostBridgeCount;
+  UINT8                     *HardwareInfoBlob;
+  LIST_ENTRY                HwInfoList;
+  LIST_ENTRY                *HwLink;
+  HARDWARE_INFO             *HwInfo;
+  UINT64                    Attributes;
+  UINT64                    AllocationAttributes;
+  BOOLEAN                   DmaAbove4G;
+  BOOLEAN                   NoExtendedConfigSpace;
+  BOOLEAN                   CombineMemPMem;
+  PCI_ROOT_BRIDGE_APERTURE  Io;
+  PCI_ROOT_BRIDGE_APERTURE  Mem;
+  PCI_ROOT_BRIDGE_APERTURE  MemAbove4G;
+  PCI_ROOT_BRIDGE_APERTURE  PMem;
+  PCI_ROOT_BRIDGE_APERTURE  PMemAbove4G;
+
+  //
+  // 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)) {
+    return NULL;
+  }
+
+  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 from fw-cfg\n",
+      __FUNCTION__
+      ));
+
+    goto FreeBridges;
+  }
+
+  PciHostBridgeCount = GetHardwareInfoCountByType (
+                         &HwInfoList,
+                         HardwareInfoTypeHostBridge,
+                         sizeof (HOST_BRIDGE_INFO)
+                         );
+
+  if (PciHostBridgeCount == 0) {
+    goto FreeBridges;
+  }
+
+  DEBUG ((
+    DEBUG_INFO,
+    "%a: Host provided description for %Lu root bridges\n",
+    __FUNCTION__,
+    PciHostBridgeCount
+    ));
+
+  //
+  // Allocate the root bridges
+  //
+  Bridges = AllocatePool (((UINTN)PciHostBridgeCount) * sizeof *Bridges);
+  if (Bridges == NULL) {
+    DEBUG ((DEBUG_ERROR, "%a: %r\n", __FUNCTION__, EFI_OUT_OF_RESOURCES));
+    goto FreeBridges;
+  }
+
+  //
+  // 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,
+      &Attributes,
+      &DmaAbove4G,
+      &NoExtendedConfigSpace,
+      &CombineMemPMem,
+      &Io,
+      &Mem,
+      &MemAbove4G,
+      &PMem,
+      &PMemAbove4G,
+      NULL
+      );
+
+    AllocationAttributes = 0;
+    if (CombineMemPMem) {
+      AllocationAttributes |= EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM;
+    }
+
+    if ((MemAbove4G.Limit > MemAbove4G.Base) ||
+        (PMemAbove4G.Limit > PMemAbove4G.Base))
+    {
+      AllocationAttributes |= EFI_PCI_HOST_BRIDGE_MEM64_DECODE;
+    }
+
+    Status = PciHostBridgeUtilityInitRootBridge (
+               Attributes,
+               Attributes,
+               AllocationAttributes,
+               DmaAbove4G,
+               NoExtendedConfigSpace,
+               RootBridgeNumber,
+               LastRootBridgeNumber,
+               &Io,
+               &Mem,
+               &MemAbove4G,
+               &PMem,
+               &PMemAbove4G,
+               &Bridges[Initialized]
+               );
+
+    if (EFI_ERROR (Status)) {
+      goto FreeBridges;
+    }
+
+    ++Initialized;
+
+    HwLink = GetNextHardwareInfoByType (
+               &HwInfoList,
+               HwLink,
+               HardwareInfoTypeHostBridge,
+               sizeof (HOST_BRIDGE_INFO)
+               );
+  }
+
+  *Count = Initialized;
+
+  //
+  // If resources were allocated for host bridges info, release them
+  //
+  if (HardwareInfoBlob) {
+    FreePool (HardwareInfoBlob);
+  }
+
+  FreeHardwareInfoList (&HwInfoList);
+  return Bridges;
+
+FreeBridges:
+  while (Initialized > 0) {
+    --Initialized;
+    PciHostBridgeUtilityUninitRootBridge (&Bridges[Initialized]);
+  }
+
+  if (Bridges) {
+    FreePool (Bridges);
+  }
+
+  if (HardwareInfoBlob) {
+    FreePool (HardwareInfoBlob);
+  }
+
+  FreeHardwareInfoList (&HwInfoList);
+  return NULL;
+}
+
+/**
+  Utility function to return all the root bridge instances in an array.
+
+  @param[out] Count                  The number of root bridge instances.
+
+  @param[in]  Attributes             Initial attributes.
+
+  @param[in]  AllocAttributes        Allocation attributes.
+
+  @param[in]  DmaAbove4G             DMA above 4GB memory.
+
+  @param[in]  NoExtendedConfigSpace  No Extended Config Space.
+
+  @param[in]  BusMin                 Minimum Bus number, inclusive.
+
+  @param[in]  BusMax                 Maximum Bus number, inclusive.
+
+  @param[in]  Io                     IO aperture.
+
+  @param[in]  Mem                    MMIO aperture.
+
+  @param[in]  MemAbove4G             MMIO aperture above 4G.
+
+  @param[in]  PMem                   Prefetchable MMIO aperture.
+
+  @param[in]  PMemAbove4G            Prefetchable MMIO aperture above 4G.
+
+  @return                            All the root bridge instances in an array.
+**/
+PCI_ROOT_BRIDGE *
+EFIAPI
+PciHostBridgeUtilityGetRootBridges (
+  OUT UINTN                     *Count,
+  IN  UINT64                    Attributes,
+  IN  UINT64                    AllocationAttributes,
+  IN  BOOLEAN                   DmaAbove4G,
+  IN  BOOLEAN                   NoExtendedConfigSpace,
+  IN  UINTN                     BusMin,
+  IN  UINTN                     BusMax,
+  IN  PCI_ROOT_BRIDGE_APERTURE  *Io,
+  IN  PCI_ROOT_BRIDGE_APERTURE  *Mem,
+  IN  PCI_ROOT_BRIDGE_APERTURE  *MemAbove4G,
+  IN  PCI_ROOT_BRIDGE_APERTURE  *PMem,
+  IN  PCI_ROOT_BRIDGE_APERTURE  *PMemAbove4G
+  )
+{
+  PCI_ROOT_BRIDGE  *Bridges;
+
+  *Count = 0;
+
+  //
+  // First attempt to get the host provided descriptions of the Root Bridges
+  // if available.
+  //
+  Bridges = PciHostBridgeUtilityGetRootBridgesHostProvided (Count);
+
+  //
+  // If host did not provide Root Bridge information, scan the buses and
+  // auto populate them with default resources.
+  //
+  if (Bridges == NULL) {
+    Bridges = PciHostBridgeUtilityGetRootBridgesBusScan (
+                Count,
+                Attributes,
+                AllocationAttributes,
+                DmaAbove4G,
+                NoExtendedConfigSpace,
+                BusMin,
+                BusMax,
+                Io,
+                Mem,
+                MemAbove4G,
+                PMem,
+                PMemAbove4G
+                );
+  }
+
+  return Bridges;
+}
+
 /**
   Utility function to free root bridge instances array from
   PciHostBridgeUtilityGetRootBridges().
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-02-03 21:00 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-03 20:56 [PATCH v4 0/8] Handling of multiple PCI host bridges specified Ojeda Leon, Nicolas
2022-02-03 20:56 ` [PATCH v4 1/8] OvmfPkg/Library: Create base HardwareInfoLib for PCI Host Bridges Ojeda Leon, Nicolas
2022-02-03 20:56 ` [PATCH v4 2/8] Ovmf/HardwareInfoLib: Create Pei lib to parse directly from fw-cfg Ojeda Leon, Nicolas
2022-02-03 20:56 ` [PATCH v4 3/8] Ovmf/HardwareInfoLib: Add Dxe lib to dynamically parse heterogenous data Ojeda Leon, Nicolas
2022-02-03 20:56 ` [PATCH v4 4/8] Ovmf/PlatformPei: Use host-provided GPA end if available Ojeda Leon, Nicolas
2022-02-03 20:56 ` Ojeda Leon, Nicolas [this message]
2022-02-04  7:39   ` [PATCH v4 5/8] OvmfPkg/PciHostBridgeUtilityLib: Initialize RootBridges apertures with spec Gerd Hoffmann
2022-02-03 20:56 ` [PATCH v4 6/8] MdeModulePkg, OvmfPkg: Add Pcd token for PCI pre-populated BARs Ojeda Leon, Nicolas
2022-02-03 20:56 ` [PATCH v4 7/8] PciHostBridgeDxe: Extend service to get base addresses before allocation Ojeda Leon, Nicolas
2022-02-04  8:36   ` Gerd Hoffmann
2022-02-04  9:17     ` Ojeda Leon, Nicolas
2022-02-14 10:18       ` Gerd Hoffmann
2022-02-14 10:28         ` [edk2-devel] " Ojeda Leon, Nicolas
2022-02-04  9:42     ` Ojeda Leon, Nicolas
2022-02-03 20:56 ` [PATCH v4 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=048b039ecfad8765487246c4b1c5d928a91c57aa.1643919691.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