]> git.proxmox.com Git - mirror_edk2.git/commitdiff
OvmfPkg: PciHostBridgeLib: convert main loop from PciHostBridgeDxe
authorLaszlo Ersek <lersek@redhat.com>
Mon, 25 Jan 2016 21:47:00 +0000 (22:47 +0100)
committerLaszlo Ersek <lersek@redhat.com>
Thu, 3 Mar 2016 17:18:35 +0000 (18:18 +0100)
In this patch we import the scan for extra root buses from the
InitializePciHostBridge() function, in file
"OvmfPkg/PciHostBridgeDxe/PciHostBridge.c".

For the time being, the InitRootBridge() and UninitRootBridge() functions
are just placeholders.

The PciHostBridgeGetRootBridges() API expects us to return the
PCI_ROOT_BRIDGE structures in a contiguous array, instead of a linked
list. Therefore the following bits have to be converted manually:

(1) The array is allocated in advance, in a single step.

(2) The calculation of the array size depends on an explicit
    multiplication, which we must check against overflow. Since more than
    255 extra root bridges make no sense anyway, we use (1 + 255) as the
    limit on the main plus all extra root bridges. This also ensures that
    the UINTN multiplication doesn't overflow.

(3) The PciHostBridgeDxe code decrements "ExtraRootBridgesLeft" to
    terminate the scanning early. Here we need track the increasing count
    of used array elements as well, so we employ "ExtraRootBridges" as a
    constant limit, and increment the new local variable "Initialized".

(4) The prototypes of InitRootBridge() and UninitRootBridge() reflect that
    the PCI_ROOT_BRIDGE structure is allocated by the caller; only
    in-place initialization is necessary.

Additionally, macros are employed for standard PCI quantities, from
"MdePkg/Include/IndustryStandard/Pci22.h":

- MAX_PCI_DEVICE_NUMBER (31) is replaced with PCI_MAX_DEVICE (same),
- the constant 255 is replaced with PCI_MAX_BUS,
- the (RootBridgeNumber < 256) condition is replaced with
  (RootBridgeNumber <= PCI_MAX_BUS).

Cc: Jordan Justen <jordan.l.justen@intel.com>
Cc: Ruiyu Ni <ruiyu.ni@intel.com>
Cc: Marcel Apfelbaum <marcel@redhat.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
OvmfPkg/Library/PciHostBridgeLib/PciHostBridgeLib.c
OvmfPkg/Library/PciHostBridgeLib/PciHostBridgeLib.inf

index 3752993f65aa2fe32671a638a3e12bb5460e3988..2f22d637ae262f7d42b23bf67ff944d7b615c220 100644 (file)
 \r
 **/\r
 #include <PiDxe.h>\r
-#include <Library/PciHostBridgeLib.h>\r
+\r
+#include <IndustryStandard/Pci.h>\r
+\r
 #include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+#include <Library/PciHostBridgeLib.h>\r
+#include <Library/PciLib.h>\r
+#include <Library/QemuFwCfgLib.h>\r
+\r
 \r
 GLOBAL_REMOVE_IF_UNREFERENCED\r
 CHAR16 *mPciHostBridgeLibAcpiAddressSpaceTypeStr[] = {\r
   L"Mem", L"I/O", L"Bus"\r
 };\r
 \r
+\r
+/**\r
+  Initialize a PCI_ROOT_BRIDGE structure.\r
+\r
+  param[in]  RootBusNumber     The bus number to store in RootBus.\r
+\r
+  param[in]  MaxSubBusNumber   The inclusive maximum bus number that can be\r
+                               assigned to any subordinate bus found behind any\r
+                               PCI bridge hanging off this root bus.\r
+\r
+                               The caller is repsonsible for ensuring that\r
+                               RootBusNumber <= MaxSubBusNumber. If\r
+                               RootBusNumber equals MaxSubBusNumber, then the\r
+                               root bus has no room for subordinate buses.\r
+\r
+  param[out] RootBus           The PCI_ROOT_BRIDGE structure (allocated by the\r
+                               caller) that should be filled in by this\r
+                               function.\r
+\r
+  @retval EFI_SUCCESS           Initialization successful. A device path\r
+                                consisting of an ACPI device path node, with\r
+                                UID = RootBusNumber, has been allocated and\r
+                                linked into RootBus.\r
+\r
+  @retval EFI_OUT_OF_RESOURCES  Memory allocation failed.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+InitRootBridge (\r
+  IN  UINT8           RootBusNumber,\r
+  IN  UINT8           MaxSubBusNumber,\r
+  OUT PCI_ROOT_BRIDGE *RootBus\r
+  )\r
+{\r
+  return EFI_OUT_OF_RESOURCES;\r
+}\r
+\r
+\r
+/**\r
+  Uninitialize a PCI_ROOT_BRIDGE structure set up with InitRootBridge().\r
+\r
+  param[in] RootBus  The PCI_ROOT_BRIDGE structure, allocated by the caller and\r
+                     initialized with InitRootBridge(), that should be\r
+                     uninitialized. This function doesn't free RootBus.\r
+**/\r
+STATIC\r
+VOID\r
+UninitRootBridge (\r
+  IN PCI_ROOT_BRIDGE *RootBus\r
+  )\r
+{\r
+}\r
+\r
+\r
 /**\r
   Return all the root bridge instances in an array.\r
 \r
@@ -37,10 +98,109 @@ PciHostBridgeGetRootBridges (
   UINTN *Count\r
   )\r
 {\r
+  EFI_STATUS           Status;\r
+  FIRMWARE_CONFIG_ITEM FwCfgItem;\r
+  UINTN                FwCfgSize;\r
+  UINT64               ExtraRootBridges;\r
+  PCI_ROOT_BRIDGE      *Bridges;\r
+  UINTN                Initialized;\r
+  UINTN                LastRootBridgeNumber;\r
+  UINTN                RootBridgeNumber;\r
+\r
   *Count = 0;\r
+\r
+  //\r
+  // QEMU provides the number of extra root buses, shortening the exhaustive\r
+  // search below. If there is no hint, the feature is missing.\r
+  //\r
+  Status = QemuFwCfgFindFile ("etc/extra-pci-roots", &FwCfgItem, &FwCfgSize);\r
+  if (EFI_ERROR (Status) || FwCfgSize != sizeof ExtraRootBridges) {\r
+    ExtraRootBridges = 0;\r
+  } else {\r
+    QemuFwCfgSelectItem (FwCfgItem);\r
+    QemuFwCfgReadBytes (FwCfgSize, &ExtraRootBridges);\r
+\r
+    if (ExtraRootBridges > PCI_MAX_BUS) {\r
+      DEBUG ((EFI_D_ERROR, "%a: invalid count of extra root buses (%Lu) "\r
+        "reported by QEMU\n", __FUNCTION__, ExtraRootBridges));\r
+      return NULL;\r
+    }\r
+    DEBUG ((EFI_D_INFO, "%a: %Lu extra root buses reported by QEMU\n",\r
+      __FUNCTION__, ExtraRootBridges));\r
+  }\r
+\r
+  //\r
+  // Allocate the "main" root bridge, and any extra root bridges.\r
+  //\r
+  Bridges = AllocatePool ((1 + (UINTN)ExtraRootBridges) * sizeof *Bridges);\r
+  if (Bridges == NULL) {\r
+    DEBUG ((EFI_D_ERROR, "%a: %r\n", __FUNCTION__, EFI_OUT_OF_RESOURCES));\r
+    return NULL;\r
+  }\r
+  Initialized = 0;\r
+\r
+  //\r
+  // The "main" root bus is always there.\r
+  //\r
+  LastRootBridgeNumber = 0;\r
+\r
+  //\r
+  // Scan all other root buses. If function 0 of any device on a bus returns a\r
+  // VendorId register value different from all-bits-one, then that bus is\r
+  // alive.\r
+  //\r
+  for (RootBridgeNumber = 1;\r
+       RootBridgeNumber <= PCI_MAX_BUS && Initialized < ExtraRootBridges;\r
+       ++RootBridgeNumber) {\r
+    UINTN Device;\r
+\r
+    for (Device = 0; Device <= PCI_MAX_DEVICE; ++Device) {\r
+      if (PciRead16 (PCI_LIB_ADDRESS (RootBridgeNumber, Device, 0,\r
+                       PCI_VENDOR_ID_OFFSET)) != MAX_UINT16) {\r
+        break;\r
+      }\r
+    }\r
+    if (Device <= PCI_MAX_DEVICE) {\r
+      //\r
+      // Found the next root bus. We can now install the *previous* one,\r
+      // because now we know how big a bus number range *that* one has, for any\r
+      // subordinate buses that might exist behind PCI bridges hanging off it.\r
+      //\r
+      Status = InitRootBridge ((UINT8)LastRootBridgeNumber,\r
+                 (UINT8)(RootBridgeNumber - 1), &Bridges[Initialized]);\r
+      if (EFI_ERROR (Status)) {\r
+        goto FreeBridges;\r
+      }\r
+      ++Initialized;\r
+      LastRootBridgeNumber = RootBridgeNumber;\r
+    }\r
+  }\r
+\r
+  //\r
+  // Install the last root bus (which might be the only, ie. main, root bus, if\r
+  // we've found no extra root buses).\r
+  //\r
+  Status = InitRootBridge ((UINT8)LastRootBridgeNumber, PCI_MAX_BUS,\r
+             &Bridges[Initialized]);\r
+  if (EFI_ERROR (Status)) {\r
+    goto FreeBridges;\r
+  }\r
+  ++Initialized;\r
+\r
+  *Count = Initialized;\r
+  return Bridges;\r
+\r
+FreeBridges:\r
+  while (Initialized > 0) {\r
+    --Initialized;\r
+    UninitRootBridge (&Bridges[Initialized]);\r
+  }\r
+\r
+  FreePool (Bridges);\r
   return NULL;\r
 }\r
 \r
+\r
 /**\r
   Free the root bridge instances array returned from\r
   PciHostBridgeGetRootBridges().\r
@@ -58,6 +218,7 @@ PciHostBridgeFreeRootBridges (
   return;\r
 }\r
 \r
+\r
 /**\r
   Inform the platform that the resource conflict happens.\r
 \r
index 1f3930195a1df6b5cefbab4a6f2d0fa9ef5c9a35..b836931912611ae8b9cc42a78f1c450549255951 100644 (file)
   PciHostBridgeLib.c\r
 \r
 [Packages]\r
-  MdePkg/MdePkg.dec\r
   MdeModulePkg/MdeModulePkg.dec\r
+  MdePkg/MdePkg.dec\r
+  OvmfPkg/OvmfPkg.dec\r
+\r
+[LibraryClasses]\r
+  DebugLib\r
+  MemoryAllocationLib\r
+  PciLib\r
+  QemuFwCfgLib\r