Skip to contentSkip to conversation

MdeModulePkg, ArmVirtPkg, OvmfPkg: fixed PCI BAR placement

PATCH In review Tushar Dave 3 participants 7 messages Sep 16 – Sep 23

lore
Share & tools
lore
#0 lore raw

cover MdeModulePkg, ArmVirtPkg, OvmfPkg: fixed PCI BAR placement

This is the firmware companion to the QEMU series [1], which validates fixed BAR configuration and exports it to firmware through the "etc/fixed-bars" fw_cfg file.

The existing IncompatiblePciDeviceSupportDxe driver is extended to read the fw_cfg blob and supply fixed BAR addresses to PciBusDxe. The PciBusDxe changes honor these addresses both when programming a BAR directly and when sizing and programming the bridge windows above it, so a fixed BAR behind one or more bridges is placed correctly and covered by the appropriate bridge windows.

Testing:

The series was tested together with the QEMU companion patches on the AArch64 virt machine across a range of PCIe topologies: emulated devices, VFIO passthrough devices, PCIe switches, single and multiple fixed-BAR endpoints under a shared root port, and fixed BARs behind nested bridges.

Changes since RFC v2:

- Integrate fixed-BAR handling into the existing IncompatiblePciDeviceSupportDxe driver instead of adding a new QemuFixedBarsDxe driver.
- Size and program PCI bridge windows for fixed BARs as part of PciBusDxe's own normal resource calculation and programming (CalculateResourceAperture()/ProgramPpbApperture()), instead of a separate post-enumeration reprogramming pass.

RFC v2:
https://lore.kernel.org/qemu-devel/[email protected]/

[1] QEMU companion series:
https://lore.kernel.org/qemu-arm/[email protected]/

A git branch with this series applied is available at:
https://github.com/tdavenvidia/edk2/tree/Upstream-fixed-bar-Sep-15-V1

The related QEMU change is available at:
https://github.com/tdavenvidia/upstream-qemu/tree/Upstream-fixed-bar-Sep-15-V1

Tushar Dave (3):
MdeModulePkg/PciBusDxe: honor fixed BAR addresses from IncompatiblePci ArmVirtPkg,OvmfPkg: fold fixed BAR placement into IncompatiblePci MdeModulePkg/PciBusDxe: size and program bridge windows for fixed BARs

 ArmVirtPkg/ArmVirtQemu.dsc                    |   2 +
 ArmVirtPkg/ArmVirtQemuFvMain.fdf.inc          |   1 +
 MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h       |   2 +
 .../Bus/Pci/PciBusDxe/PciEnumeratorSupport.c  |  12 +
 .../Bus/Pci/PciBusDxe/PciResourceSupport.c    | 121 +++++
 .../Bus/Pci/PciBusDxe/PciResourceSupport.h    |   7 +
 .../FixedBars.c                               | 413 ++++++++++++++++++
 .../FixedBars.h                               |  63 +++
 .../IncompatiblePciDeviceSupport.c            | 114 +++--
 .../IncompatiblePciDeviceSupport.inf          |   8 +-
 10 files changed, 712 insertions(+), 31 deletions(-)
 create mode 100644 OvmfPkg/IncompatiblePciDeviceSupportDxe/FixedBars.c
 create mode 100644 OvmfPkg/IncompatiblePciDeviceSupportDxe/FixedBars.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/3 MdeModulePkg/PciBusDxe: honor fixed BAR addresses from IncompatiblePci

EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL returns ACPI address descriptors describing PCI resource requirements. When both _MIF (BIT2) and _MAF (BIT3) are set in the descriptor's GenFlag, the descriptor represents a fixed address range.

Extend PCI_BAR with FixedBaseAddress and update the PCI bus driver to preserve and use this information:

* UpdatePciInfo() detects _MIF|_MAF, records AddrRangeMin as FixedBaseAddress, and marks the BAR as fixed.

* ProgramBar() programs FixedBaseAddress into the BAR register instead of the resource allocator selected address for fixed BARs.

No functional change occurs unless a platform driver supplies fixed BAR descriptors through EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL.

---
 MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h              |  2 ++
 .../Bus/Pci/PciBusDxe/PciEnumeratorSupport.c         | 12 ++++++++++++
 MdeModulePkg/Bus/Pci/PciBusDxe/PciResourceSupport.c  | 11 +++++++++++
 3 files changed, 25 insertions(+)
MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h +2 -0
index 01bce28b1b..dbec57bd54 100644
@@ -96,6 +96,8 @@ struct _PCI_BAR {
   UINT64          Alignment;
   PCI_BAR_TYPE    BarType;
   BOOLEAN         BarTypeFixed;
+  UINT64          FixedBaseAddress;
+  BOOLEAN         HasFixedBaseAddress;
   UINT16          Offset;
 };
 
MdeModulePkg/Bus/Pci/PciBusDxe/PciEnumeratorSupport.c +12 -0
index ae94462f60..b7c132cc10 100644
@@ -1638,6 +1638,18 @@ UpdatePciInfo (
         if (Ptr->AddrLen != 0) {
           PciIoDevice->PciBar[BarIndex].Length = Ptr->AddrLen;
         }
+
+        //
+        // When both _MIF (BIT2) and _MAF (BIT3) are set in GenFlag the
+        // descriptor carries a fixed base address in AddrRangeMin.
+        //
+        if ((Ptr->GenFlag & (BIT2 | BIT3)) == (BIT2 | BIT3)) {
+          PciIoDevice->PciBar[BarIndex].HasFixedBaseAddress = TRUE;
+          PciIoDevice->PciBar[BarIndex].FixedBaseAddress    = Ptr->AddrRangeMin;
+          DEBUG ((DEBUG_INFO,
+                  "PciBus: UpdatePciInfo BAR[%u] fixed address=0x%016Lx\n",
+                  (UINT32)BarIndex, Ptr->AddrRangeMin));
+        }
       }
     }
 
MdeModulePkg/Bus/Pci/PciBusDxe/PciResourceSupport.c +11 -0
index 8f3cff3eaa..169011e647 100644
@@ -1276,6 +1276,17 @@ ProgramBar (
 
   Address = Base + Node->Offset;
 
+  //
+  // If EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL supplied a fixed
+  // base address (_MIF|_MAF in the ACPI descriptor), use it directly.
+  //
+  if (Node->PciDev->PciBar[Node->Bar].HasFixedBaseAddress) {
+    Address = Node->PciDev->PciBar[Node->Bar].FixedBaseAddress;
+    DEBUG ((DEBUG_INFO,
+            "PciBus: BAR[%d] fixed address 0x%016Lx (GCD 0x%016Lx)\n",
+            Node->Bar, Address, Base + Node->Offset));
+  }
+
   //
   // Indicate pci bus driver has allocated
   // resource for this device
Signature · 1 line
2.34.1
#2 lore raw

PATCH 3/3 MdeModulePkg/PciBusDxe: size and program bridge windows for fixed BARs

In reply to Tushar Dave

CalculateResourceAperture() sizes a PCI bridge's resource window from its children's Length and Alignment, without accounting for a fixed base address assigned to a child BAR by EFI_INCOMPATIBLE_PCI_DEVICE_SUPPORT_PROTOCOL. As a result, the bridge window does not cover the fixed BAR, and ProgramPpbApperture() programs the resource allocator's base instead of the fixed base.

Track Fixed and FixedBase in PCI_RESOURCE_NODE and add ResourceNodeIsFixed() to identify fixed BARs and bridge windows inherited from fixed children.

When a bridge has fixed children, CalculateResourceAperture() now sizes and places the bridge window to cover the union of their fixed address ranges. Bridges without fixed children retain the existing behavior.

ProgramPpbApperture() also honors FixedBase when programming the bridge window, consistent with ProgramBar() for fixed BARs.

---
 .../Bus/Pci/PciBusDxe/PciResourceSupport.c    | 110 ++++++++++++++++++
 .../Bus/Pci/PciBusDxe/PciResourceSupport.h    |   7 ++
 2 files changed, 117 insertions(+)
MdeModulePkg/Bus/Pci/PciBusDxe/PciResourceSupport.c +110 -0
index 169011e647..0aa3465636 100644
@@ -324,6 +324,35 @@ CalculateApertureIo16 (
   Bridge->Length = MAX (Bridge->Length, PaddingAperture);
 }
 
+/**
+  Return TRUE, with the fixed base address, if Node must be placed at a
+  specific address instead of anywhere satisfying Length/Alignment. Node is
+  either a BAR marked fixed by IncompatiblePciDeviceSupport, or a
+  child bridge whose own window was already determined to be fixed.
+
+  @param Node        Resource node to check.
+  @param FixedBase   Fixed base address of Node.
+
+  @retval TRUE   Node requires a fixed base address.
+  @retval FALSE  Node has no fixed base address requirement.
+
+**/
+STATIC
+BOOLEAN
+ResourceNodeIsFixed (
+  IN  PCI_RESOURCE_NODE  *Node,
+  OUT UINT64             *FixedBase
+  )
+{
+  if (IS_PCI_BRIDGE (&Node->PciDev->Pci)) {
+    *FixedBase = Node->FixedBase;
+    return Node->Fixed;
+  }
+
+  *FixedBase = Node->PciDev->PciBar[Node->Bar].FixedBaseAddress;
+  return Node->PciDev->PciBar[Node->Bar].HasFixedBaseAddress;
+}
+
 /**
   This function is used to calculate the resource aperture
   for a given bridge device.
@@ -339,6 +368,10 @@ CalculateResourceAperture (
   UINT64             Aperture[2];
   LIST_ENTRY         *CurrentLink;
   PCI_RESOURCE_NODE  *Node;
+  UINT64             ChildFixedBase;
+  UINT64             FixedMin;
+  UINT64             FixedMax;
+  UINT64             NextFreeAddr;
 
   if (Bridge == NULL) {
     return;
@@ -349,6 +382,71 @@ CalculateResourceAperture (
     return;
   }
 
+  //
+  // If at least one child requires a fixed base address, size and place
+  // this bridge's window as the exact union of those addresses instead of
+  // packing children by size and alignment alone.
+  //
+  FixedMin = MAX_UINT64;
+  FixedMax = 0;
+  for ( CurrentLink = GetFirstNode (&Bridge->ChildList)
+        ; !IsNull (&Bridge->ChildList, CurrentLink)
+        ; CurrentLink = GetNextNode (&Bridge->ChildList, CurrentLink)
+        )
+  {
+    Node = RESOURCE_NODE_FROM_LINK (CurrentLink);
+
+    if (ResourceNodeIsFixed (Node, &ChildFixedBase)) {
+      FixedMin = MIN (FixedMin, ChildFixedBase);
+      FixedMax = MAX (FixedMax, ChildFixedBase + Node->Length);
+    }
+  }
+
+  if (FixedMin < FixedMax) {
+    Bridge->Fixed     = TRUE;
+    Bridge->FixedBase = FixedMin & ~Bridge->Alignment;
+    NextFreeAddr      = FixedMax;
+
+    for ( CurrentLink = GetFirstNode (&Bridge->ChildList)
+          ; !IsNull (&Bridge->ChildList, CurrentLink)
+          ; CurrentLink = GetNextNode (&Bridge->ChildList, CurrentLink)
+          )
+    {
+      Node = RESOURCE_NODE_FROM_LINK (CurrentLink);
+
+      if (ResourceNodeIsFixed (Node, &ChildFixedBase)) {
+        Node->Offset = ChildFixedBase - Bridge->FixedBase;
+        FixedMax     = MAX (FixedMax, Node->Offset + Node->Length + Bridge->FixedBase);
+
+        DEBUG ((DEBUG_INFO,
+                "PciBus: bridge %02x:%02x.%x child %02x:%02x.%x BAR[%d] fixed at 0x%016Lx, length 0x%Lx\n",
+                Bridge->PciDev->BusNumber, Bridge->PciDev->DeviceNumber, Bridge->PciDev->FunctionNumber,
+                Node->PciDev->BusNumber, Node->PciDev->DeviceNumber, Node->PciDev->FunctionNumber,
+                Node->Bar, ChildFixedBase, Node->Length));
+      } else {
+        Node->Offset  = ALIGN_VALUE (NextFreeAddr - Bridge->FixedBase, Node->Alignment + 1);
+        NextFreeAddr  = Bridge->FixedBase + Node->Offset + Node->Length;
+      }
+    }
+
+    Bridge->Length = ALIGN_VALUE (MAX (FixedMax, NextFreeAddr) - Bridge->FixedBase, Bridge->Alignment + 1);
+
+    DEBUG ((DEBUG_INFO,
+            "PciBus: bridge %02x:%02x.%x fixed window [0x%016Lx, 0x%016Lx]\n",
+            Bridge->PciDev->BusNumber, Bridge->PciDev->DeviceNumber, Bridge->PciDev->FunctionNumber,
+            Bridge->FixedBase, Bridge->FixedBase + Bridge->Length - 1));
+
+    CurrentLink = Bridge->ChildList.ForwardLink;
+    if (CurrentLink != &Bridge->ChildList) {
+      Node = RESOURCE_NODE_FROM_LINK (CurrentLink);
+      if (Node->Alignment > Bridge->Alignment) {
+        Bridge->Alignment = Node->Alignment;
+      }
+    }
+
+    return;
+  }
+
   Aperture[PciResUsageTypical] = 0;
   Aperture[PciResUsagePadding] = 0;
   //
@@ -1468,6 +1566,18 @@ ProgramPpbApperture (
   PciIo   = &(Node->PciDev->PciIo);
   Address = Base + Node->Offset;
 
+  //
+  // If CalculateResourceAperture() determined this window must cover a
+  // fixed BAR, use its base directly.
+  //
+  if (Node->Fixed) {
+    Address = Node->FixedBase;
+    DEBUG ((DEBUG_INFO,
+            "PciBus: bridge %02x:%02x.%x fixed window base 0x%016Lx (GCD 0x%016Lx)\n",
+            Node->PciDev->BusNumber, Node->PciDev->DeviceNumber, Node->PciDev->FunctionNumber,
+            Address, Base + Node->Offset));
+  }
+
   //
   // Indicate the PPB resource has been allocated
   //
MdeModulePkg/Bus/Pci/PciBusDxe/PciResourceSupport.h +7 -0
index 148d9503c8..0682237d74 100644
@@ -28,6 +28,13 @@ typedef struct {
   BOOLEAN               Reserved;
   PCI_RESOURCE_USAGE    ResourceUsage;
   BOOLEAN               Virtual;
+
+  //
+  // TRUE if this bridge's aggregate window must be placed at
+  // FixedBase, because it has at least one fixed descendant.
+  //
+  BOOLEAN               Fixed;
+  UINT64                FixedBase;
 } PCI_RESOURCE_NODE;
 
 #define RESOURCE_NODE_FROM_LINK(a) \
Signature · 1 line
2.34.1
#3 lore raw

PATCH 2/3 ArmVirtPkg, OvmfPkg: fold fixed BAR placement into IncompatiblePci

In reply to Tushar Dave

Extend the existing IncompatiblePciDeviceSupportDxe driver to support explicit PCI BAR placement. CheckDevice() checks for a fixed-bars entry first, then falls back to the existing 64-bit-MMIO-preference descriptor.

Add FixedBars.c/.h to encapsulate fixed-BAR handling. Parse QEMU's etc/fixed-bars fw_cfg blob, reserve the specified PCI BAR address ranges in the GCD memory space map before PCI enumeration, provide fixed BAR descriptors to PciBusDxe during device discovery.

Also, IncompatiblePciDeviceSupportDxe's CcProbe() call has no CcProbeLib resolution on ArmVirtQemu, which causes the build to fail. This patch adds a CcProbeLib resolution for ArmVirtQemu.

---
 ArmVirtPkg/ArmVirtQemu.dsc                    |   2 +
 ArmVirtPkg/ArmVirtQemuFvMain.fdf.inc          |   1 +
 .../FixedBars.c                               | 413 ++++++++++++++++++
 .../FixedBars.h                               |  63 +++
 .../IncompatiblePciDeviceSupport.c            | 114 +++--
 .../IncompatiblePciDeviceSupport.inf          |   8 +-
 6 files changed, 570 insertions(+), 31 deletions(-)
 create mode 100644 OvmfPkg/IncompatiblePciDeviceSupportDxe/FixedBars.c
 create mode 100644 OvmfPkg/IncompatiblePciDeviceSupportDxe/FixedBars.h
ArmVirtPkg/ArmVirtQemu.dsc +2 -0
index a3f8a2d0ac..d331a3a9bd 100644
@@ -67,6 +67,7 @@
 !include ArmVirtPkg/ArmVirt.dsc.inc
 
 [LibraryClasses.common]
+  CcProbeLib|MdePkg/Library/CcProbeLibNull/CcProbeLibNull.inf
   ArmTransferListLib|ArmPkg/Library/ArmTransferListLib/ArmTransferListLib.inf
 
   QemuFwCfgLib|OvmfPkg/Library/QemuFwCfgLib/QemuFwCfgMmioDxeLib.inf
@@ -392,6 +393,7 @@
   #
   # PCI support
   #
+  OvmfPkg/IncompatiblePciDeviceSupportDxe/IncompatiblePciDeviceSupport.inf
   MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridgeDxe.inf
 
   #
ArmVirtPkg/ArmVirtQemuFvMain.fdf.inc +1 -0
index 3fb6a1da9d..3f0b00b2e7 100644
@@ -159,6 +159,7 @@ READ_LOCK_STATUS   = TRUE
   # PCI support
   #
   INF UefiCpuPkg/CpuMmio2Dxe/CpuMmio2Dxe.inf
+  INF OvmfPkg/IncompatiblePciDeviceSupportDxe/IncompatiblePciDeviceSupport.inf
   INF MdeModulePkg/Bus/Pci/PciHostBridgeDxe/PciHostBridgeDxe.inf
   INF MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
   INF OvmfPkg/PciHotPlugInitDxe/PciHotPlugInit.inf
OvmfPkg/IncompatiblePciDeviceSupportDxe/FixedBars.c new file +413 -0

413 lines not on this page · raw message

OvmfPkg/IncompatiblePciDeviceSupportDxe/FixedBars.h new file +63 -0

63 lines not on this page · raw message

OvmfPkg/IncompatiblePciDeviceSupportDxe/IncompatiblePciDeviceSupport.c +84 -30

155 lines not on this page · raw message

OvmfPkg/IncompatiblePciDeviceSupportDxe/IncompatiblePciDeviceSupport.inf +7 -1
index b92662d0bb..b51f51c25e 100644
@@ -18,21 +18,27 @@
 
 [Sources]
   IncompatiblePciDeviceSupport.c
+  FixedBars.c
+  FixedBars.h
 
 [Packages]
   MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
   OvmfPkg/OvmfPkg.dec
 
 [LibraryClasses]
+  BaseMemoryLib
   CcProbeLib
   DebugLib
+  DxeServicesTableLib
   MemoryAllocationLib
   PcdLib
+  QemuFwCfgLib
   UefiBootServicesTableLib
   UefiDriverEntryPoint
 
 [Protocols]
-  gEfiIncompatiblePciDeviceSupportProtocolGuid ## SOMETIMES_PRODUCES
+  gEfiIncompatiblePciDeviceSupportProtocolGuid       ## SOMETIMES_PRODUCES
 
 [Pcd]
   gUefiOvmfPkgTokenSpaceGuid.PcdPciMmio64Size                ## CONSUMES
Signature · 1 line
2.34.1
#4 lore raw
In reply to Tushar Dave

Hi,

Quoted 4 lines

The series was tested together with the QEMU companion patches on the AArch64 virt machine across a range of PCIe topologies: emulated devices, VFIO passthrough devices, PCIe switches, single and multiple fixed-BAR endpoints under a shared root port, and fixed BARs behind nested bridges.

Given this is active for both aarch64 and x86_64: Any testing with OVMF on x86_64?

Overall this looks good to me. edk2 has switched to github PRs, so please open a PR there. edk2 has a policy that patches should not touch multiple packages, so patch #2 should be split into two for ArmVirtPkg and OvmfPkg.

take care,
Gerd

#5 lore raw
Quoted 9 lines · Gerd Hoffmann

Hi,

Quoted 4 lines

The series was tested together with the QEMU companion patches on the AArch64 virt machine across a range of PCIe topologies: emulated devices, VFIO passthrough devices, PCIe switches, single and multiple fixed-BAR endpoints under a shared root port, and fixed BARs behind nested bridges.

Given this is active for both aarch64 and x86_64: Any testing with OVMF on x86_64?

Gerd,

Thank you for the review.

Yes — tested on x86_64 OVMF (OvmfPkgX64, q35). There's no fixed-BAR placement to actually exercise on x86 since nothing on the QEMU side populates the fw_cfg blob there, so this was really a check that the fixed-BAR addition to IncompatiblePciDeviceSupportDxe is a correct no-op when that blob is absent. And it is — existing behavior is unchanged, without any regressions.

Quoted 5 lines

Overall this looks good to me. edk2 has switched to github PRs, so please open a PR there. edk2 has a policy that patches should not touch multiple packages, so patch #2 should be split into two for ArmVirtPkg and OvmfPkg.

Sure, I'll open a GitHub PR for this series and split patch #2 into ArmVirtPkg and OvmfPkg patches.

Quoted 3 lines

take care,
Gerd

Thanks.
-Tushar

#6 lore raw
Quoted 25 lines · Tushar Dave
Quoted 9 lines · Gerd Hoffmann

Hi,

Quoted 4 lines

The series was tested together with the QEMU companion patches on the AArch64 virt machine across a range of PCIe topologies: emulated devices, VFIO passthrough devices, PCIe switches, single and multiple fixed-BAR endpoints under a shared root port, and fixed BARs behind nested bridges.

Given this is active for both aarch64 and x86_64: Any testing with OVMF on x86_64?

Gerd,

Thank you for the review.

Yes — tested on x86_64 OVMF (OvmfPkgX64, q35). There's no fixed-BAR placement to actually exercise on x86 since nothing on the QEMU side populates the fw_cfg blob there, so this was really a check that the fixed-BAR addition to IncompatiblePciDeviceSupportDxe is a correct no-op when that blob is absent. And it is — existing behavior is unchanged, without any regressions.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

Overall this looks good to me. edk2 has switched to github PRs, so please open a PR there. edk2 has a policy that patches should not touch multiple packages, so patch #2 should be split into two for ArmVirtPkg and OvmfPkg.

Sure, I'll open a GitHub PR for this series and split patch #2 into ArmVirtPkg and OvmfPkg patches.

PR submitted https://github.com/tianocore/edk2/pull/13183

Thanks.
-Tushar

Quoted 7 lines
Quoted 4 lines

take care,
Gerd

Thanks.
-Tushar