]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/Bus/Pci/PciBusDxe: Support PCIe Resizable BAR Capability
authorLuo, Heng <heng.luo@intel.com>
Mon, 4 Jan 2021 06:59:54 +0000 (14:59 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 4 Jan 2021 08:25:28 +0000 (08:25 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=313

Add PcdPcieResizableBarSupport to enable/disable PCIe Resizable
BAR Capability fearture.
Program the Resizable BAR Register if the device suports PCIe Resizable
BAR Capability and PcdPcieResizableBarSupport is TRUE.

Cc: Ray Ni <ray.ni@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Signed-off-by: Heng Luo <heng.luo@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
MdeModulePkg/Bus/Pci/PciBusDxe/PciBus.h
MdeModulePkg/Bus/Pci/PciBusDxe/PciBusDxe.inf
MdeModulePkg/Bus/Pci/PciBusDxe/PciEnumeratorSupport.c
MdeModulePkg/Bus/Pci/PciBusDxe/PciEnumeratorSupport.h
MdeModulePkg/Bus/Pci/PciBusDxe/PciLib.c
MdeModulePkg/Bus/Pci/PciBusDxe/PciLib.h
MdeModulePkg/MdeModulePkg.dec

index d4113993c8af090d08d84e3465eb9b164354dbf6..a619a68526682fe5780ed550640cbd333551381b 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Header files and data structures needed by PCI Bus module.\r
 \r
-Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
@@ -280,6 +280,8 @@ struct _PCI_IO_DEVICE {
   // This field is used to support this case.\r
   //\r
   UINT16                                    BridgeIoAlignment;\r
+  UINT32                                    ResizableBarOffset;\r
+  UINT32                                    ResizableBarNumber;\r
 };\r
 \r
 #define PCI_IO_DEVICE_FROM_PCI_IO_THIS(a) \\r
index 9284998f36153248230ffdd78c6068688cc111de..e317169d9c57f99023319d05cf1e301fced20254 100644 (file)
@@ -2,7 +2,7 @@
 #  The PCI bus driver will probe all PCI devices and allocate MMIO and IO space for these devices.\r
 #  Please use PCD feature flag PcdPciBusHotplugDeviceSupport to enable hot plug supporting.\r
 #\r
-#  Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+#  Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
 #\r
 #  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdAriSupport                  ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdMrIovSupport                ## CONSUMES\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdPciDisableBusEnumeration    ## SOMETIMES_CONSUMES\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdPcieResizableBarSupport     ## CONSUMES\r
 \r
 [UserExtensions.TianoCore."ExtraFiles"]\r
   PciBusDxeExtra.uni\r
index 6c68a97d4e467e4498aea5296d28e8932c3abbc3..1b64924b7b32cac5bbd28c6ef40cc2d027ff7b66 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   PCI emumeration support functions implementation for PCI Bus module.\r
 \r
-Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
@@ -2426,6 +2426,31 @@ CreatePciIoDevice (
     }\r
   }\r
 \r
+  PciIoDevice->ResizableBarOffset = 0;\r
+  if (PcdGetBool (PcdPcieResizableBarSupport)) {\r
+    Status = LocatePciExpressCapabilityRegBlock (\r
+               PciIoDevice,\r
+               PCI_EXPRESS_EXTENDED_CAPABILITY_RESIZABLE_BAR_ID,\r
+               &PciIoDevice->ResizableBarOffset,\r
+               NULL\r
+               );\r
+    if (!EFI_ERROR (Status)) {\r
+      PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_CONTROL ResizableBarControl;\r
+      UINT32                                                  Offset;\r
+      Offset = PciIoDevice->ResizableBarOffset + sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_HEADER)\r
+                + sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_CAPABILITY),\r
+      PciIo->Pci.Read (\r
+              PciIo,\r
+              EfiPciIoWidthUint8,\r
+              Offset,\r
+              sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_CONTROL),\r
+              &ResizableBarControl\r
+              );\r
+      PciIoDevice->ResizableBarNumber = ResizableBarControl.Bits.ResizableBarNumber;\r
+      PciProgramResizableBar (PciIoDevice, PciResizableBarMax);\r
+    }\r
+  }\r
+\r
   //\r
   // Initialize the reserved resource list\r
   //\r
index d76606c7df32b14cf8e79807b0639a13a568cb25..4581b270c93ef9cc4e59a44fff8572820dab747c 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   PCI enumeration support functions declaration for PCI Bus module.\r
 \r
-Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
@@ -467,4 +467,14 @@ DumpPpbPaddingResource (
   IN PCI_BAR_TYPE                     ResourceType\r
   );\r
 \r
+/**\r
+  Dump the PCI BAR information.\r
+\r
+  @param PciIoDevice     PCI IO instance.\r
+**/\r
+VOID\r
+DumpPciBars (\r
+  IN PCI_IO_DEVICE                    *PciIoDevice\r
+  );\r
+\r
 #endif\r
index 72690ab6476acc46408e12552c0bc891bd751b9c..6bba2836716520f8f1f55d30b08cff9f002d1030 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Internal library implementation for PCI Bus module.\r
 \r
-Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>\r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
@@ -377,6 +377,60 @@ DumpResourceMap (
   }\r
 }\r
 \r
+/**\r
+  Adjust the Devices' BAR size to minimum value if it support Resizeable BAR capability.\r
+\r
+  @param RootBridgeDev  Pointer to instance of PCI_IO_DEVICE..\r
+\r
+  @return TRUE if BAR size is adjusted.\r
+\r
+**/\r
+BOOLEAN\r
+AdjustPciDeviceBarSize (\r
+  IN PCI_IO_DEVICE *RootBridgeDev\r
+  )\r
+{\r
+  PCI_IO_DEVICE     *PciIoDevice;\r
+  LIST_ENTRY        *CurrentLink;\r
+  BOOLEAN           Adjusted;\r
+  UINTN             Offset;\r
+  UINTN             BarIndex;\r
+\r
+  Adjusted    = FALSE;\r
+  CurrentLink = RootBridgeDev->ChildList.ForwardLink;\r
+\r
+  while (CurrentLink != NULL && CurrentLink != &RootBridgeDev->ChildList) {\r
+    PciIoDevice = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
+\r
+    if (IS_PCI_BRIDGE (&PciIoDevice->Pci)) {\r
+      if (AdjustPciDeviceBarSize (PciIoDevice)) {\r
+        Adjusted = TRUE;\r
+      }\r
+    } else {\r
+      if (PciIoDevice->ResizableBarOffset != 0) {\r
+        DEBUG ((\r
+          DEBUG_ERROR,\r
+          "PciBus: [%02x|%02x|%02x] Adjust Pci Device Bar Size\n",\r
+          PciIoDevice->BusNumber, PciIoDevice->DeviceNumber, PciIoDevice->FunctionNumber\r
+          ));\r
+        PciProgramResizableBar (PciIoDevice, PciResizableBarMin);\r
+        //\r
+        // Start to parse the bars\r
+        //\r
+        for (Offset = 0x10, BarIndex = 0; Offset <= 0x24 && BarIndex < PCI_MAX_BAR; BarIndex++) {\r
+          Offset = PciParseBar (PciIoDevice, Offset, BarIndex);\r
+        }\r
+        Adjusted = TRUE;\r
+        DEBUG_CODE (DumpPciBars (PciIoDevice););\r
+      }\r
+    }\r
+\r
+    CurrentLink = CurrentLink->ForwardLink;\r
+  }\r
+\r
+  return Adjusted;\r
+}\r
+\r
 /**\r
   Submits the I/O and memory resource requirements for the specified PCI Host Bridge.\r
 \r
@@ -422,6 +476,10 @@ PciHostBridgeResourceAllocator (
   PCI_RESOURCE_NODE                              PMem64Pool;\r
   EFI_DEVICE_HANDLE_EXTENDED_DATA_PAYLOAD        HandleExtendedData;\r
   EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA_PAYLOAD  AllocFailExtendedData;\r
+  BOOLEAN                                        ResizableBarNeedAdjust;\r
+  BOOLEAN                                        ResizableBarAdjusted;\r
+\r
+  ResizableBarNeedAdjust = PcdGetBool (PcdPcieResizableBarSupport);\r
 \r
   //\r
   // It may try several times if the resource allocation fails\r
@@ -703,19 +761,30 @@ PciHostBridgeResourceAllocator (
             sizeof (AllocFailExtendedData)\r
             );\r
 \r
-      Status = PciHostBridgeAdjustAllocation (\r
-                 &IoPool,\r
-                 &Mem32Pool,\r
-                 &PMem32Pool,\r
-                 &Mem64Pool,\r
-                 &PMem64Pool,\r
-                 IoResStatus,\r
-                 Mem32ResStatus,\r
-                 PMem32ResStatus,\r
-                 Mem64ResStatus,\r
-                 PMem64ResStatus\r
-                 );\r
-\r
+     //\r
+     // When resource conflict happens, adjust the BAR size first.\r
+     // Only when adjusting BAR size doesn't help or BAR size cannot be adjusted,\r
+     // reject the device who requests largest resource that causes conflict.\r
+     //\r
+      ResizableBarAdjusted = FALSE;\r
+      if (ResizableBarNeedAdjust) {\r
+        ResizableBarAdjusted = AdjustPciDeviceBarSize (RootBridgeDev);\r
+        ResizableBarNeedAdjust = FALSE;\r
+      }\r
+      if (!ResizableBarAdjusted) {\r
+        Status = PciHostBridgeAdjustAllocation (\r
+                  &IoPool,\r
+                  &Mem32Pool,\r
+                  &PMem32Pool,\r
+                  &Mem64Pool,\r
+                  &PMem64Pool,\r
+                  IoResStatus,\r
+                  Mem32ResStatus,\r
+                  PMem32ResStatus,\r
+                  Mem64ResStatus,\r
+                  PMem64ResStatus\r
+                  );\r
+      }\r
       //\r
       // Destroy all the resource tree\r
       //\r
@@ -1651,3 +1720,91 @@ PciHostBridgeEnumerator (
 \r
   return EFI_SUCCESS;\r
 }\r
+\r
+/**\r
+  This function is used to program the Resizable BAR Register.\r
+\r
+  @param PciIoDevice            A pointer to the PCI_IO_DEVICE.\r
+  @param ResizableBarOp         PciResizableBarMax: Set BAR to max size\r
+                                PciResizableBarMin: set BAR to min size.\r
+\r
+  @retval EFI_SUCCESS           Successfully enumerated the host bridge.\r
+  @retval other                 Some error occurred when enumerating the host bridge.\r
+\r
+**/\r
+EFI_STATUS\r
+PciProgramResizableBar (\r
+  IN PCI_IO_DEVICE                *PciIoDevice,\r
+  IN PCI_RESIZABLE_BAR_OPERATION  ResizableBarOp\r
+  )\r
+{\r
+  EFI_PCI_IO_PROTOCOL  *PciIo;\r
+  UINT64                Capabilities;\r
+  UINT32                Index;\r
+  UINT32                Offset;\r
+  INTN                  Bit;\r
+  UINTN                 ResizableBarNumber;\r
+  EFI_STATUS            Status;\r
+  PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY   Entries[PCI_MAX_BAR];\r
+\r
+  ASSERT (PciIoDevice->ResizableBarOffset != 0);\r
+\r
+  DEBUG ((DEBUG_INFO, "   Programs Resizable BAR register, offset: 0x%08x, number: %d\n",\r
+        PciIoDevice->ResizableBarOffset, PciIoDevice->ResizableBarNumber));\r
+\r
+  ResizableBarNumber = MIN (PciIoDevice->ResizableBarNumber, PCI_MAX_BAR);\r
+  PciIo = &PciIoDevice->PciIo;\r
+  Status = PciIo->Pci.Read (\r
+          PciIo,\r
+          EfiPciIoWidthUint8,\r
+          PciIoDevice->ResizableBarOffset + sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_HEADER),\r
+          sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY) * ResizableBarNumber,\r
+          (VOID *)(&Entries)\r
+          );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  for (Index = 0; Index < ResizableBarNumber; Index++) {\r
+\r
+    //\r
+    // When the bit of Capabilities Set, indicates that the Function supports\r
+    // operating with the BAR sized to (2^Bit) MB.\r
+    // Example:\r
+    // Bit 0 is set: supports operating with the BAR sized to 1 MB\r
+    // Bit 1 is set: supports operating with the BAR sized to 2 MB\r
+    // Bit n is set: supports operating with the BAR sized to (2^n) MB\r
+    //\r
+    Capabilities = LShiftU64(Entries[Index].ResizableBarControl.Bits.BarSizeCapability, 28)\r
+                  | Entries[Index].ResizableBarCapability.Bits.BarSizeCapability;\r
+\r
+    if (ResizableBarOp == PciResizableBarMax) {\r
+      Bit = HighBitSet64(Capabilities);\r
+    } else if (ResizableBarOp == PciResizableBarMin) {\r
+      Bit = LowBitSet64(Capabilities);\r
+    } else {\r
+      ASSERT ((ResizableBarOp == PciResizableBarMax) || (ResizableBarOp == PciResizableBarMin));\r
+    }\r
+\r
+    ASSERT (Bit >= 0);\r
+\r
+    Offset = PciIoDevice->ResizableBarOffset + sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_HEADER)\r
+            + Index * sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY)\r
+            + OFFSET_OF (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY, ResizableBarControl);\r
+\r
+    Entries[Index].ResizableBarControl.Bits.BarSize = (UINT32) Bit;\r
+    DEBUG ((\r
+      DEBUG_INFO,\r
+      "   Resizable Bar: Offset = 0x%x, Bar Size Capability = 0x%016lx, New Bar Size = 0x%lx\n",\r
+      OFFSET_OF (PCI_TYPE00, Device.Bar[Entries[Index].ResizableBarControl.Bits.BarIndex]),\r
+      Capabilities, LShiftU64 (SIZE_1MB, Bit)\r
+      ));\r
+    PciIo->Pci.Write (\r
+            PciIo,\r
+            EfiPciIoWidthUint32,\r
+            Offset,\r
+            1,\r
+            &Entries[Index].ResizableBarControl.Uint32\r
+            );\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
index 10b435d146117894abaf4d8130484d520ec49b8c..aeec6d6b6dcdabe4fddf6feff7bd161f34324610 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Internal library declaration for PCI Bus module.\r
 \r
-Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2021, Intel Corporation. All rights reserved.<BR>\r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
@@ -24,6 +24,10 @@ typedef struct {
   UINT8                              *AllocRes;\r
 } EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA_PAYLOAD;\r
 \r
+typedef enum {\r
+  PciResizableBarMin = 0x00,\r
+  PciResizableBarMax = 0xFF\r
+} PCI_RESIZABLE_BAR_OPERATION;\r
 \r
 /**\r
   Retrieve the PCI Card device BAR information via PciIo interface.\r
@@ -156,4 +160,20 @@ PciHostBridgeEnumerator (
   IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL  *PciResAlloc\r
   );\r
 \r
+/**\r
+  This function is used to program the Resizable BAR Register.\r
+\r
+  @param PciIoDevice            A pointer to the PCI_IO_DEVICE.\r
+  @param ResizableBarOp         PciResizableBarMax: Set BAR to max size\r
+                                PciResizableBarMin: set BAR to min size.\r
+\r
+  @retval EFI_SUCCESS           Successfully enumerated the host bridge.\r
+  @retval other                 Some error occurred when enumerating the host bridge.\r
+\r
+**/\r
+EFI_STATUS\r
+PciProgramResizableBar (\r
+  IN PCI_IO_DEVICE                *PciIoDevice,\r
+  IN PCI_RESIZABLE_BAR_OPERATION  ResizableBarOp\r
+  );\r
 #endif\r
index 9b52b34494433757ba0ef1ff9a065d8cf4ae6742..9173fdef8393bc535cf7ea0468f5d72d8c341231 100644 (file)
@@ -4,7 +4,7 @@
 # and libraries instances, which are used for those modules.\r
 #\r
 # Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.\r
-# Copyright (c) 2007 - 2020, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>\r
 # Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>\r
 # (C) Copyright 2016 - 2019 Hewlett Packard Enterprise Development LP<BR>\r
 # Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>\r
   # @Prompt Enable StatusCode via memory.\r
   gEfiMdeModulePkgTokenSpaceGuid.PcdStatusCodeUseMemory|FALSE|BOOLEAN|0x00010023\r
 \r
+  ## Indicates if the PCIe Resizable BAR Capability Supported.<BR><BR>\r
+  #   TRUE  - PCIe Resizable BAR Capability is supported.<BR>\r
+  #   FALSE - PCIe Resizable BAR Capability is not supported.<BR>\r
+  # @Prompt Enable PCIe Resizable BAR Capability support.\r
+  gEfiMdeModulePkgTokenSpaceGuid.PcdPcieResizableBarSupport|TRUE|BOOLEAN|0x10000024\r
+\r
 [PcdsPatchableInModule]\r
   ## Specify memory size with page number for PEI code when\r
   #  Loading Module at Fixed Address feature is enabled.\r