]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg: Fix a PciBusDxe hot plug bug
authorRuiyu Ni <ruiyu.ni@intel.com>
Fri, 23 Oct 2015 08:29:38 +0000 (08:29 +0000)
committerniruiyu <niruiyu@Edk2>
Fri, 23 Oct 2015 08:29:38 +0000 (08:29 +0000)
For a hot plug bridge with device attached, PciBusDxe driver reserves
the resources which equal to the total amount of padding resource
returned from HotPlug->GetResourcePadding() and the actual occupied
resource by the attached device. The behavior is incorrect.
Correct behavior is to reserve the bigger one between the padding
resource and the actual occupied resource.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Feng Tian <feng.tian@intel.com>
git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@18658 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Bus/Pci/PciBusDxe/PciEnumeratorSupport.c
MdeModulePkg/Bus/Pci/PciBusDxe/PciEnumeratorSupport.h
MdeModulePkg/Bus/Pci/PciBusDxe/PciLib.c
MdeModulePkg/Bus/Pci/PciBusDxe/PciResourceSupport.c

index f7aea4fd80c8423723e4c3db7bf54f5442f1bdfd..030ef42320b85ffaee2c9c0f64c89c9dae43a9ad 100644 (file)
@@ -324,6 +324,77 @@ PciSearchDevice (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Dump the PPB padding resource information.\r
+\r
+  @param PciIoDevice     PCI IO instance.\r
+  @param ResourceType    The desired resource type to dump.\r
+                         PciBarTypeUnknown means to dump all types of resources.\r
+**/\r
+VOID\r
+DumpPpbPaddingResource (\r
+  IN PCI_IO_DEVICE                    *PciIoDevice,\r
+  IN PCI_BAR_TYPE                     ResourceType\r
+  )\r
+{\r
+  EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptor;\r
+  PCI_BAR_TYPE                      Type;\r
+\r
+  if (ResourceType == PciBarTypeIo16 || ResourceType == PciBarTypeIo32) {\r
+    ResourceType = PciBarTypeIo;\r
+  }\r
+\r
+  for (Descriptor = PciIoDevice->ResourcePaddingDescriptors; Descriptor->Desc != ACPI_END_TAG_DESCRIPTOR; Descriptor++) {\r
+\r
+    Type = PciBarTypeUnknown;\r
+    if (Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR && Descriptor->ResType == ACPI_ADDRESS_SPACE_TYPE_IO) {\r
+      Type = PciBarTypeIo;\r
+    } else if (Descriptor->Desc == ACPI_ADDRESS_SPACE_DESCRIPTOR && Descriptor->ResType == ACPI_ADDRESS_SPACE_TYPE_MEM) {\r
+\r
+      if (Descriptor->AddrSpaceGranularity == 32) {\r
+        //\r
+        // prefechable\r
+        //\r
+        if (Descriptor->SpecificFlag == EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE) {\r
+          Type = PciBarTypePMem32;\r
+        }\r
+\r
+        //\r
+        // Non-prefechable\r
+        //\r
+        if (Descriptor->SpecificFlag == 0) {\r
+          Type = PciBarTypeMem32;\r
+        }\r
+      }\r
+\r
+      if (Descriptor->AddrSpaceGranularity == 64) {\r
+        //\r
+        // prefechable\r
+        //\r
+        if (Descriptor->SpecificFlag == EFI_ACPI_MEMORY_RESOURCE_SPECIFIC_FLAG_CACHEABLE_PREFETCHABLE) {\r
+          Type = PciBarTypePMem64;\r
+        }\r
+\r
+        //\r
+        // Non-prefechable\r
+        //\r
+        if (Descriptor->SpecificFlag == 0) {\r
+          Type = PciBarTypeMem64;\r
+        }\r
+      }\r
+    }\r
+\r
+    if ((Type != PciBarTypeUnknown) && ((ResourceType == PciBarTypeUnknown) || (ResourceType == Type))) {\r
+      DEBUG ((\r
+        EFI_D_INFO,\r
+        "   Padding: Type = %s; Alignment = 0x%lx;\tLength = 0x%lx\n",\r
+        mBarTypeStr[Type], Descriptor->AddrRangeMax, Descriptor->AddrLen\r
+        ));\r
+    }\r
+  }\r
+\r
+}\r
+\r
 /**\r
   Dump the PCI BAR information.\r
 \r
@@ -586,7 +657,10 @@ GatherPpbInfo (
 \r
   GetResourcePaddingPpb (PciIoDevice);\r
 \r
-  DEBUG_CODE (DumpPciBars (PciIoDevice););\r
+  DEBUG_CODE (\r
+    DumpPpbPaddingResource (PciIoDevice, PciBarTypeUnknown);\r
+    DumpPciBars (PciIoDevice);\r
+  );\r
 \r
   return PciIoDevice;\r
 }\r
index a4489b895fc49dd478710b4df7ddf907ce7554df..4d7b3b754a2a25aae5a4658bc560f0f5f4a7e420 100644 (file)
@@ -460,4 +460,17 @@ ResetAllPpbBusNumber (
   IN UINT8                              StartBusNumber\r
   );\r
 \r
+/**\r
+  Dump the PPB padding resource information.\r
+\r
+  @param PciIoDevice     PCI IO instance.\r
+  @param ResourceType    The desired resource type to dump.\r
+                         PciBarTypeUnknown means to dump all types of resources.\r
+**/\r
+VOID\r
+DumpPpbPaddingResource (\r
+  IN PCI_IO_DEVICE                    *PciIoDevice,\r
+  IN PCI_BAR_TYPE                     ResourceType\r
+  );\r
+\r
 #endif\r
index 3e275e34ecebe4c998d18cf05184e373120494c6..f4b6ebfc0d30ffe8ada9712e846a726045353808 100644 (file)
@@ -188,19 +188,21 @@ DumpBridgeResource (
       BridgeResource->PciDev->PciBar[BridgeResource->Bar].BaseAddress,\r
       BridgeResource->Length, BridgeResource->Alignment\r
       ));\r
-    for ( Link = BridgeResource->ChildList.ForwardLink\r
-        ; Link != &BridgeResource->ChildList\r
-        ; Link = Link->ForwardLink\r
+    for ( Link = GetFirstNode (&BridgeResource->ChildList)\r
+        ; !IsNull (&BridgeResource->ChildList, Link)\r
+        ; Link = GetNextNode (&BridgeResource->ChildList, Link)\r
         ) {\r
       Resource = RESOURCE_NODE_FROM_LINK (Link);\r
       if (Resource->ResourceUsage == PciResUsageTypical) {\r
         Bar = Resource->Virtual ? Resource->PciDev->VfPciBar : Resource->PciDev->PciBar;\r
         DEBUG ((\r
-          EFI_D_INFO, " Base = 0x%lx;\tLength = 0x%lx;\tAlignment = 0x%lx;\tOwner = %s ",\r
+          EFI_D_INFO, "   Base = 0x%lx;\tLength = 0x%lx;\tAlignment = 0x%lx;\tOwner = %s [%02x|%02x|%02x:",\r
           Bar[Resource->Bar].BaseAddress, Resource->Length, Resource->Alignment,\r
           IS_PCI_BRIDGE (&Resource->PciDev->Pci)     ? L"PPB" :\r
           IS_CARDBUS_BRIDGE (&Resource->PciDev->Pci) ? L"P2C" :\r
-                                                       L"PCI"\r
+                                                       L"PCI",\r
+          Resource->PciDev->BusNumber, Resource->PciDev->DeviceNumber,\r
+          Resource->PciDev->FunctionNumber\r
           ));\r
 \r
         if ((!IS_PCI_BRIDGE (&Resource->PciDev->Pci) && !IS_CARDBUS_BRIDGE (&Resource->PciDev->Pci)) ||\r
@@ -210,24 +212,20 @@ DumpBridgeResource (
           //\r
           // The resource requirement comes from the device itself.\r
           //\r
-          DEBUG ((\r
-            EFI_D_INFO, " [%02x|%02x|%02x:%02x]\n",\r
-            Resource->PciDev->BusNumber, Resource->PciDev->DeviceNumber,\r
-            Resource->PciDev->FunctionNumber, Bar[Resource->Bar].Offset\r
-            ));\r
+          DEBUG ((EFI_D_INFO, "%02x]", Bar[Resource->Bar].Offset));\r
         } else {\r
           //\r
           // The resource requirement comes from the subordinate devices.\r
           //\r
-          DEBUG ((\r
-            EFI_D_INFO, " [%02x|%02x|%02x:**]\n",\r
-            Resource->PciDev->BusNumber, Resource->PciDev->DeviceNumber,\r
-            Resource->PciDev->FunctionNumber\r
-            ));\r
+          DEBUG ((EFI_D_INFO, "**]"));\r
         }\r
       } else {\r
-        DEBUG ((EFI_D_INFO, " Padding:Length = 0x%lx;\tAlignment = 0x%lx\n", Resource->Length, Resource->Alignment));\r
+        DEBUG ((EFI_D_INFO, "   Base = Padding;\tLength = 0x%lx;\tAlignment = 0x%lx", Resource->Length, Resource->Alignment));\r
       }\r
+      if (BridgeResource->ResType != Resource->ResType) {\r
+        DEBUG ((EFI_D_INFO, "; Type = %s", mBarTypeStr[MIN (Resource->ResType, PciBarTypeMaxType)]));\r
+      }\r
+      DEBUG ((EFI_D_INFO, "\n"));\r
     }\r
   }\r
 }\r
@@ -235,63 +233,61 @@ DumpBridgeResource (
 /**\r
   Find the corresponding resource node for the Device in child list of BridgeResource.\r
   \r
-  @param[in] Device         Pointer to PCI_IO_DEVICE.\r
-  @param[in] BridgeResource Pointer to PCI_RESOURCE_NODE.\r
+  @param[in]  Device          Pointer to PCI_IO_DEVICE.\r
+  @param[in]  BridgeResource  Pointer to PCI_RESOURCE_NODE.\r
+  @param[out] DeviceResources Pointer to a buffer to receive resources for the Device.\r
   \r
-  @return !NULL  The corresponding resource node for the Device.\r
-  @return NULL   No corresponding resource node for the Device.\r
+  @return Count of the resource descriptors returned.\r
 **/\r
-PCI_RESOURCE_NODE *\r
+UINTN\r
 FindResourceNode (\r
-  IN PCI_IO_DEVICE     *Device,\r
-  IN PCI_RESOURCE_NODE *BridgeResource\r
+  IN  PCI_IO_DEVICE     *Device,\r
+  IN  PCI_RESOURCE_NODE *BridgeResource,\r
+  OUT PCI_RESOURCE_NODE **DeviceResources OPTIONAL\r
   )\r
 {\r
   LIST_ENTRY               *Link;\r
   PCI_RESOURCE_NODE        *Resource;\r
+  UINTN                    Count;\r
 \r
+  Count = 0;\r
   for ( Link = BridgeResource->ChildList.ForwardLink\r
       ; Link != &BridgeResource->ChildList\r
       ; Link = Link->ForwardLink\r
       ) {\r
     Resource = RESOURCE_NODE_FROM_LINK (Link);\r
     if (Resource->PciDev == Device) {\r
-      return Resource;\r
+      if (DeviceResources != NULL) {\r
+        DeviceResources[Count] = Resource;\r
+      }\r
+      Count++;\r
     }\r
   }\r
 \r
-  return NULL;\r
+  return Count;\r
 }\r
 \r
 /**\r
   Dump the resource map of all the devices under Bridge.\r
   \r
-  @param[in] Bridge     Bridge device instance.\r
-  @param[in] IoNode     IO resource descriptor for the bridge device.\r
-  @param[in] Mem32Node  Mem32 resource descriptor for the bridge device.\r
-  @param[in] PMem32Node PMem32 resource descriptor for the bridge device.\r
-  @param[in] Mem64Node  Mem64 resource descriptor for the bridge device.\r
-  @param[in] PMem64Node PMem64 resource descriptor for the bridge device.\r
+  @param[in] Bridge        Bridge device instance.\r
+  @param[in] Resources     Resource descriptors for the bridge device.\r
+  @param[in] ResourceCount Count of resource descriptors.\r
 **/\r
 VOID\r
 DumpResourceMap (\r
   IN PCI_IO_DEVICE     *Bridge,\r
-  IN PCI_RESOURCE_NODE *IoNode,\r
-  IN PCI_RESOURCE_NODE *Mem32Node,\r
-  IN PCI_RESOURCE_NODE *PMem32Node,\r
-  IN PCI_RESOURCE_NODE *Mem64Node,\r
-  IN PCI_RESOURCE_NODE *PMem64Node\r
+  IN PCI_RESOURCE_NODE **Resources,\r
+  IN UINTN             ResourceCount\r
   )\r
 {\r
-  EFI_STATUS                       Status;\r
-  LIST_ENTRY                       *Link;\r
-  PCI_IO_DEVICE                    *Device;\r
-  PCI_RESOURCE_NODE                *ChildIoNode;\r
-  PCI_RESOURCE_NODE                *ChildMem32Node;\r
-  PCI_RESOURCE_NODE                *ChildPMem32Node;\r
-  PCI_RESOURCE_NODE                *ChildMem64Node;\r
-  PCI_RESOURCE_NODE                *ChildPMem64Node;\r
-  CHAR16                           *Str;\r
+  EFI_STATUS           Status;\r
+  LIST_ENTRY           *Link;\r
+  PCI_IO_DEVICE        *Device;\r
+  UINTN                Index;\r
+  CHAR16               *Str;\r
+  PCI_RESOURCE_NODE    **ChildResources;\r
+  UINTN                ChildResourceCount;\r
 \r
   DEBUG ((EFI_D_INFO, "PciBus: Resource Map for "));\r
 \r
@@ -320,11 +316,9 @@ DumpResourceMap (
     }\r
   }\r
 \r
-  DumpBridgeResource (IoNode);\r
-  DumpBridgeResource (Mem32Node);\r
-  DumpBridgeResource (PMem32Node);\r
-  DumpBridgeResource (Mem64Node);\r
-  DumpBridgeResource (PMem64Node);\r
+  for (Index = 0; Index < ResourceCount; Index++) {\r
+    DumpBridgeResource (Resources[Index]);\r
+  }\r
   DEBUG ((EFI_D_INFO, "\n"));\r
 \r
   for ( Link = Bridge->ChildList.ForwardLink\r
@@ -334,20 +328,19 @@ DumpResourceMap (
     Device = PCI_IO_DEVICE_FROM_LINK (Link);\r
     if (IS_PCI_BRIDGE (&Device->Pci)) {\r
 \r
-      ChildIoNode     = (IoNode     == NULL ? NULL : FindResourceNode (Device, IoNode));\r
-      ChildMem32Node  = (Mem32Node  == NULL ? NULL : FindResourceNode (Device, Mem32Node));\r
-      ChildPMem32Node = (PMem32Node == NULL ? NULL : FindResourceNode (Device, PMem32Node));\r
-      ChildMem64Node  = (Mem64Node  == NULL ? NULL : FindResourceNode (Device, Mem64Node));\r
-      ChildPMem64Node = (PMem64Node == NULL ? NULL : FindResourceNode (Device, PMem64Node));\r
-\r
-      DumpResourceMap (\r
-        Device,\r
-        ChildIoNode,\r
-        ChildMem32Node,\r
-        ChildPMem32Node,\r
-        ChildMem64Node,\r
-        ChildPMem64Node\r
-        );\r
+      ChildResourceCount = 0;\r
+      for (Index = 0; Index < ResourceCount; Index++) {\r
+        ChildResourceCount += FindResourceNode (Device, Resources[Index], NULL);\r
+      }\r
+      ChildResources = AllocatePool (sizeof (PCI_RESOURCE_NODE *) * ChildResourceCount);\r
+      ASSERT (ChildResources != NULL);\r
+      ChildResourceCount = 0;\r
+      for (Index = 0; Index < ResourceCount; Index++) {\r
+        ChildResourceCount += FindResourceNode (Device, Resources[Index], &ChildResources[ChildResourceCount]);\r
+      }\r
+\r
+      DumpResourceMap (Device, ChildResources, ChildResourceCount);\r
+      FreePool (ChildResources);\r
     }\r
   }\r
 }\r
@@ -807,11 +800,11 @@ PciHostBridgeResourceAllocator (
     // Create the entire system resource map from the information collected by\r
     // enumerator. Several resource tree was created\r
     //\r
-    IoBridge     = FindResourceNode (RootBridgeDev, &IoPool);\r
-    Mem32Bridge  = FindResourceNode (RootBridgeDev, &Mem32Pool);\r
-    PMem32Bridge = FindResourceNode (RootBridgeDev, &PMem32Pool);\r
-    Mem64Bridge  = FindResourceNode (RootBridgeDev, &Mem64Pool);\r
-    PMem64Bridge = FindResourceNode (RootBridgeDev, &PMem64Pool);\r
+    FindResourceNode (RootBridgeDev, &IoPool, &IoBridge);\r
+    FindResourceNode (RootBridgeDev, &Mem32Pool, &Mem32Bridge);\r
+    FindResourceNode (RootBridgeDev, &PMem32Pool, &PMem32Bridge);\r
+    FindResourceNode (RootBridgeDev, &Mem64Pool, &Mem64Bridge);\r
+    FindResourceNode (RootBridgeDev, &PMem64Pool, &PMem64Bridge);\r
 \r
     ASSERT (IoBridge     != NULL);\r
     ASSERT (Mem32Bridge  != NULL);\r
@@ -869,14 +862,13 @@ PciHostBridgeResourceAllocator (
     // Dump the resource map for current root bridge\r
     //\r
     DEBUG_CODE (\r
-      DumpResourceMap (\r
-        RootBridgeDev,\r
-        IoBridge,\r
-        Mem32Bridge,\r
-        PMem32Bridge,\r
-        Mem64Bridge,\r
-        PMem64Bridge\r
-        );\r
+      PCI_RESOURCE_NODE *Resources[5];\r
+      Resources[0] = IoBridge;\r
+      Resources[1] = Mem32Bridge;\r
+      Resources[2] = PMem32Bridge;\r
+      Resources[3] = Mem64Bridge;\r
+      Resources[4] = PMem64Bridge;\r
+      DumpResourceMap (RootBridgeDev, Resources, sizeof (Resources) / sizeof (Resources[0]));\r
     );\r
 \r
     FreePool (AcpiConfig);\r
@@ -984,7 +976,8 @@ PciScanBus (
   UINT8                             Device;\r
   UINT8                             Func;\r
   UINT64                            Address;\r
-  UINTN                             SecondBus;\r
+  UINT8                             SecondBus;\r
+  UINT8                             PaddedSubBus;\r
   UINT16                            Register;\r
   UINTN                             HpIndex;\r
   PCI_IO_DEVICE                     *PciDevice;\r
@@ -1218,7 +1211,7 @@ PciScanBus (
 \r
           Status = PciScanBus (\r
                     PciDevice,\r
-                    (UINT8) (SecondBus),\r
+                    SecondBus,\r
                     SubBusNumber,\r
                     PaddedBusRange\r
                     );\r
@@ -1234,12 +1227,16 @@ PciScanBus (
           if ((Attributes == EfiPaddingPciRootBridge) &&\r
               (State & EFI_HPC_STATE_ENABLED) != 0    &&\r
               (State & EFI_HPC_STATE_INITIALIZED) != 0) {\r
-            *PaddedBusRange = (UINT8) ((UINT8) (BusRange) +*PaddedBusRange);\r
+            *PaddedBusRange = (UINT8) ((UINT8) (BusRange) + *PaddedBusRange);\r
           } else {\r
-            Status = PciAllocateBusNumber (PciDevice, *SubBusNumber, (UINT8) (BusRange), SubBusNumber);\r
+            //\r
+            // Reserve the larger one between the actual occupied bus number and padded bus number\r
+            //\r
+            Status = PciAllocateBusNumber (PciDevice, StartBusNumber, (UINT8) (BusRange), &PaddedSubBus);\r
             if (EFI_ERROR (Status)) {\r
               return Status;\r
             }\r
+            *SubBusNumber = MAX (PaddedSubBus, *SubBusNumber);\r
           }\r
         }\r
 \r
index d8d988cbfc270b223503d34aad5ef50bcb589bef..b106abe0fa8075aeb81767ad742bc99dc829807d 100644 (file)
@@ -196,6 +196,7 @@ CalculateApertureIo16 (
   PCI_RESOURCE_NODE       *Node;\r
   UINT64                  Offset;\r
   EFI_PCI_PLATFORM_POLICY PciPolicy;\r
+  UINT64                  PaddingAperture;\r
 \r
   if (!mPolicyDetermined) {\r
     //\r
@@ -228,21 +229,27 @@ CalculateApertureIo16 (
     mPolicyDetermined = TRUE;\r
   }\r
 \r
-  Aperture = 0;\r
+  Aperture        = 0;\r
+  PaddingAperture = 0;\r
 \r
   if (Bridge == NULL) {\r
     return ;\r
   }\r
 \r
-  CurrentLink = Bridge->ChildList.ForwardLink;\r
-\r
   //\r
   // Assume the bridge is aligned\r
   //\r
-  while (CurrentLink != &Bridge->ChildList) {\r
+  for ( CurrentLink = GetFirstNode (&Bridge->ChildList)\r
+      ; !IsNull (&Bridge->ChildList, CurrentLink)\r
+      ; CurrentLink = GetNextNode (&Bridge->ChildList, CurrentLink)\r
+      ) {\r
 \r
     Node = RESOURCE_NODE_FROM_LINK (CurrentLink);\r
-\r
+    if (Node->ResourceUsage == PciResUsagePadding) {\r
+      ASSERT (PaddingAperture == 0);\r
+      PaddingAperture = Node->Length;\r
+      continue;\r
+    }\r
     //\r
     // Consider the aperture alignment\r
     //\r
@@ -293,13 +300,10 @@ CalculateApertureIo16 (
     // Increment aperture by the length of node\r
     //\r
     Aperture += Node->Length;\r
-\r
-    CurrentLink = CurrentLink->ForwardLink;\r
   }\r
 \r
   //\r
-  // At last, adjust the aperture with the bridge's\r
-  // alignment\r
+  // Adjust the aperture with the bridge's alignment\r
   //\r
   Offset = Aperture & (Bridge->Alignment);\r
 \r
@@ -319,6 +323,12 @@ CalculateApertureIo16 (
       Bridge->Alignment = Node->Alignment;\r
     }\r
   }\r
+\r
+  //\r
+  // Hotplug controller needs padding resources.\r
+  // Use the larger one between the padding resource and actual occupied resource.\r
+  //\r
+  Bridge->Length = MAX (Bridge->Length, PaddingAperture);\r
 }\r
 \r
 /**\r
@@ -336,10 +346,11 @@ CalculateResourceAperture (
   UINT64            Aperture;\r
   LIST_ENTRY        *CurrentLink;\r
   PCI_RESOURCE_NODE *Node;\r
-\r
+  UINT64            PaddingAperture;\r
   UINT64            Offset;\r
 \r
-  Aperture = 0;\r
+  Aperture        = 0;\r
+  PaddingAperture = 0;\r
 \r
   if (Bridge == NULL) {\r
     return ;\r
@@ -351,14 +362,20 @@ CalculateResourceAperture (
     return ;\r
   }\r
 \r
-  CurrentLink = Bridge->ChildList.ForwardLink;\r
-\r
   //\r
   // Assume the bridge is aligned\r
   //\r
-  while (CurrentLink != &Bridge->ChildList) {\r
+  for ( CurrentLink = GetFirstNode (&Bridge->ChildList)\r
+      ; !IsNull (&Bridge->ChildList, CurrentLink)\r
+      ; CurrentLink = GetNextNode (&Bridge->ChildList, CurrentLink)\r
+      ) {\r
 \r
     Node = RESOURCE_NODE_FROM_LINK (CurrentLink);\r
+    if (Node->ResourceUsage == PciResUsagePadding) {\r
+      ASSERT (PaddingAperture == 0);\r
+      PaddingAperture = Node->Length;\r
+      continue;\r
+    }\r
 \r
     //\r
     // Apply padding resource if available\r
@@ -381,11 +398,6 @@ CalculateResourceAperture (
     // Increment aperture by the length of node\r
     //\r
     Aperture += Node->Length;\r
-\r
-    //\r
-    // Consider the aperture alignment\r
-    //\r
-    CurrentLink = CurrentLink->ForwardLink;\r
   }\r
 \r
   //\r
@@ -407,7 +419,7 @@ CalculateResourceAperture (
   }\r
 \r
   //\r
-  // At last, adjust the bridge's alignment to the first child's alignment\r
+  // Adjust the bridge's alignment to the first child's alignment\r
   // if the bridge has at least one child\r
   //\r
   CurrentLink = Bridge->ChildList.ForwardLink;\r
@@ -417,6 +429,12 @@ CalculateResourceAperture (
       Bridge->Alignment = Node->Alignment;\r
     }\r
   }\r
+\r
+  //\r
+  // Hotplug controller needs padding resources.\r
+  // Use the larger one between the padding resource and actual occupied resource.\r
+  //\r
+  Bridge->Length = MAX (Bridge->Length, PaddingAperture);\r
 }\r
 \r
 /**\r