2 Internal library implementation for PCI Bus module.
4 Copyright (c) 2006 - 2022, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
12 GLOBAL_REMOVE_IF_UNREFERENCED
13 CHAR16
*mBarTypeStr
[] = {
28 Retrieve the max bus number that is assigned to the Root Bridge hierarchy.
29 It can support the case that there are multiple bus ranges.
31 @param Bridge Bridge device instance.
33 @retval The max bus number that is assigned to this Root Bridge hierarchy.
38 IN PCI_IO_DEVICE
*Bridge
41 PCI_IO_DEVICE
*RootBridge
;
42 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BusNumberRanges
;
43 UINT64 MaxNumberInRange
;
46 // Get PCI Root Bridge device
49 while (RootBridge
->Parent
!= NULL
) {
50 RootBridge
= RootBridge
->Parent
;
55 // Iterate the bus number ranges to get max PCI bus number
57 BusNumberRanges
= RootBridge
->BusNumberRanges
;
58 while (BusNumberRanges
->Desc
!= ACPI_END_TAG_DESCRIPTOR
) {
59 MaxNumberInRange
= BusNumberRanges
->AddrRangeMin
+ BusNumberRanges
->AddrLen
- 1;
63 return (UINT16
)MaxNumberInRange
;
67 Retrieve the PCI Card device BAR information via PciIo interface.
69 @param PciIoDevice PCI Card device instance.
74 IN PCI_IO_DEVICE
*PciIoDevice
79 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
84 // Read PciBar information from the bar register
86 if (!gFullEnumeration
) {
88 PciIoDevice
->PciIo
.Pci
.Read (
89 &(PciIoDevice
->PciIo
),
91 PCI_CARD_MEMORY_BASE_0
,
96 (PciIoDevice
->PciBar
)[P2C_MEM_1
].BaseAddress
= (UINT64
)(Address
);
97 (PciIoDevice
->PciBar
)[P2C_MEM_1
].Length
= 0x2000000;
98 (PciIoDevice
->PciBar
)[P2C_MEM_1
].BarType
= PciBarTypeMem32
;
101 PciIoDevice
->PciIo
.Pci
.Read (
102 &(PciIoDevice
->PciIo
),
104 PCI_CARD_MEMORY_BASE_1
,
108 (PciIoDevice
->PciBar
)[P2C_MEM_2
].BaseAddress
= (UINT64
)(Address
);
109 (PciIoDevice
->PciBar
)[P2C_MEM_2
].Length
= 0x2000000;
110 (PciIoDevice
->PciBar
)[P2C_MEM_2
].BarType
= PciBarTypePMem32
;
113 PciIoDevice
->PciIo
.Pci
.Read (
114 &(PciIoDevice
->PciIo
),
116 PCI_CARD_IO_BASE_0_LOWER
,
120 (PciIoDevice
->PciBar
)[P2C_IO_1
].BaseAddress
= (UINT64
)(Address
);
121 (PciIoDevice
->PciBar
)[P2C_IO_1
].Length
= 0x100;
122 (PciIoDevice
->PciBar
)[P2C_IO_1
].BarType
= PciBarTypeIo16
;
125 PciIoDevice
->PciIo
.Pci
.Read (
126 &(PciIoDevice
->PciIo
),
128 PCI_CARD_IO_BASE_1_LOWER
,
132 (PciIoDevice
->PciBar
)[P2C_IO_2
].BaseAddress
= (UINT64
)(Address
);
133 (PciIoDevice
->PciBar
)[P2C_IO_2
].Length
= 0x100;
134 (PciIoDevice
->PciBar
)[P2C_IO_2
].BarType
= PciBarTypeIo16
;
137 if ((gPciHotPlugInit
!= NULL
) && FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
138 GetResourcePaddingForHpb (PciIoDevice
);
143 Remove rejected pci device from specific root bridge
146 @param RootBridgeHandle Specific parent root bridge handle.
147 @param Bridge Bridge device instance.
151 RemoveRejectedPciDevices (
152 IN EFI_HANDLE RootBridgeHandle
,
153 IN PCI_IO_DEVICE
*Bridge
157 LIST_ENTRY
*CurrentLink
;
158 LIST_ENTRY
*LastLink
;
160 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
164 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
166 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
167 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
169 if (IS_PCI_BRIDGE (&Temp
->Pci
)) {
171 // Remove rejected devices recusively
173 RemoveRejectedPciDevices (RootBridgeHandle
, Temp
);
176 // Skip rejection for all PPBs, while detect rejection for others
178 if (IsPciDeviceRejected (Temp
)) {
180 // For P2C, remove all devices on it
182 if (!IsListEmpty (&Temp
->ChildList
)) {
183 RemoveAllPciDeviceOnBridge (RootBridgeHandle
, Temp
);
187 // Finally remove itself
189 LastLink
= CurrentLink
->BackLink
;
190 RemoveEntryList (CurrentLink
);
191 FreePciDevice (Temp
);
193 CurrentLink
= LastLink
;
197 CurrentLink
= CurrentLink
->ForwardLink
;
202 Dump the resourc map of the bridge device.
204 @param[in] BridgeResource Resource descriptor of the bridge device.
208 IN PCI_RESOURCE_NODE
*BridgeResource
212 PCI_RESOURCE_NODE
*Resource
;
215 if ((BridgeResource
!= NULL
) && (BridgeResource
->Length
!= 0)) {
218 "Type = %s; Base = 0x%lx;\tLength = 0x%lx;\tAlignment = 0x%lx\n",
219 mBarTypeStr
[MIN (BridgeResource
->ResType
, PciBarTypeMaxType
)],
220 BridgeResource
->PciDev
->PciBar
[BridgeResource
->Bar
].BaseAddress
,
221 BridgeResource
->Length
,
222 BridgeResource
->Alignment
224 for ( Link
= GetFirstNode (&BridgeResource
->ChildList
)
225 ; !IsNull (&BridgeResource
->ChildList
, Link
)
226 ; Link
= GetNextNode (&BridgeResource
->ChildList
, Link
)
229 Resource
= RESOURCE_NODE_FROM_LINK (Link
);
230 if (Resource
->ResourceUsage
== PciResUsageTypical
) {
231 Bar
= Resource
->Virtual
? Resource
->PciDev
->VfPciBar
: Resource
->PciDev
->PciBar
;
234 " Base = 0x%lx;\tLength = 0x%lx;\tAlignment = 0x%lx;\tOwner = %s [%02x|%02x|%02x:",
235 Bar
[Resource
->Bar
].BaseAddress
,
238 IS_PCI_BRIDGE (&Resource
->PciDev
->Pci
) ? L
"PPB" :
239 IS_CARDBUS_BRIDGE (&Resource
->PciDev
->Pci
) ? L
"P2C" :
241 Resource
->PciDev
->BusNumber
,
242 Resource
->PciDev
->DeviceNumber
,
243 Resource
->PciDev
->FunctionNumber
246 if ((!IS_PCI_BRIDGE (&Resource
->PciDev
->Pci
) && !IS_CARDBUS_BRIDGE (&Resource
->PciDev
->Pci
)) ||
247 (IS_PCI_BRIDGE (&Resource
->PciDev
->Pci
) && (Resource
->Bar
< PPB_IO_RANGE
)) ||
248 (IS_CARDBUS_BRIDGE (&Resource
->PciDev
->Pci
) && (Resource
->Bar
< P2C_MEM_1
))
252 // The resource requirement comes from the device itself.
254 DEBUG ((DEBUG_INFO
, "%02x]", Bar
[Resource
->Bar
].Offset
));
257 // The resource requirement comes from the subordinate devices.
259 DEBUG ((DEBUG_INFO
, "**]"));
262 DEBUG ((DEBUG_INFO
, " Base = Padding;\tLength = 0x%lx;\tAlignment = 0x%lx", Resource
->Length
, Resource
->Alignment
));
265 if (BridgeResource
->ResType
!= Resource
->ResType
) {
266 DEBUG ((DEBUG_INFO
, "; Type = %s", mBarTypeStr
[MIN (Resource
->ResType
, PciBarTypeMaxType
)]));
269 DEBUG ((DEBUG_INFO
, "\n"));
275 Find the corresponding resource node for the Device in child list of BridgeResource.
277 @param[in] Device Pointer to PCI_IO_DEVICE.
278 @param[in] BridgeResource Pointer to PCI_RESOURCE_NODE.
279 @param[out] DeviceResources Pointer to a buffer to receive resources for the Device.
281 @return Count of the resource descriptors returned.
285 IN PCI_IO_DEVICE
*Device
,
286 IN PCI_RESOURCE_NODE
*BridgeResource
,
287 OUT PCI_RESOURCE_NODE
**DeviceResources OPTIONAL
291 PCI_RESOURCE_NODE
*Resource
;
295 for ( Link
= BridgeResource
->ChildList
.ForwardLink
296 ; Link
!= &BridgeResource
->ChildList
297 ; Link
= Link
->ForwardLink
300 Resource
= RESOURCE_NODE_FROM_LINK (Link
);
301 if (Resource
->PciDev
== Device
) {
302 if (DeviceResources
!= NULL
) {
303 DeviceResources
[Count
] = Resource
;
314 Dump the resource map of all the devices under Bridge.
316 @param[in] Bridge Bridge device instance.
317 @param[in] Resources Resource descriptors for the bridge device.
318 @param[in] ResourceCount Count of resource descriptors.
322 IN PCI_IO_DEVICE
*Bridge
,
323 IN PCI_RESOURCE_NODE
**Resources
,
324 IN UINTN ResourceCount
329 PCI_IO_DEVICE
*Device
;
332 PCI_RESOURCE_NODE
**ChildResources
;
333 UINTN ChildResourceCount
;
335 DEBUG ((DEBUG_INFO
, "PciBus: Resource Map for "));
337 Status
= gBS
->OpenProtocol (
339 &gEfiPciRootBridgeIoProtocolGuid
,
343 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
345 if (EFI_ERROR (Status
)) {
348 "Bridge [%02x|%02x|%02x]\n",
350 Bridge
->DeviceNumber
,
351 Bridge
->FunctionNumber
354 Str
= ConvertDevicePathToText (
355 DevicePathFromHandle (Bridge
->Handle
),
359 DEBUG ((DEBUG_INFO
, "Root Bridge %s\n", Str
!= NULL
? Str
: L
""));
365 for (Index
= 0; Index
< ResourceCount
; Index
++) {
366 DumpBridgeResource (Resources
[Index
]);
369 DEBUG ((DEBUG_INFO
, "\n"));
371 for ( Link
= Bridge
->ChildList
.ForwardLink
372 ; Link
!= &Bridge
->ChildList
373 ; Link
= Link
->ForwardLink
376 Device
= PCI_IO_DEVICE_FROM_LINK (Link
);
377 if (IS_PCI_BRIDGE (&Device
->Pci
)) {
378 ChildResourceCount
= 0;
379 for (Index
= 0; Index
< ResourceCount
; Index
++) {
380 ChildResourceCount
+= FindResourceNode (Device
, Resources
[Index
], NULL
);
383 ChildResources
= AllocatePool (sizeof (PCI_RESOURCE_NODE
*) * ChildResourceCount
);
384 ASSERT (ChildResources
!= NULL
);
385 ChildResourceCount
= 0;
386 for (Index
= 0; Index
< ResourceCount
; Index
++) {
387 ChildResourceCount
+= FindResourceNode (Device
, Resources
[Index
], &ChildResources
[ChildResourceCount
]);
390 DumpResourceMap (Device
, ChildResources
, ChildResourceCount
);
391 FreePool (ChildResources
);
397 Adjust the Devices' BAR size to minimum value if it support Resizeable BAR capability.
399 @param RootBridgeDev Pointer to instance of PCI_IO_DEVICE..
401 @return TRUE if BAR size is adjusted.
405 AdjustPciDeviceBarSize (
406 IN PCI_IO_DEVICE
*RootBridgeDev
409 PCI_IO_DEVICE
*PciIoDevice
;
410 LIST_ENTRY
*CurrentLink
;
416 CurrentLink
= RootBridgeDev
->ChildList
.ForwardLink
;
418 while (CurrentLink
!= NULL
&& CurrentLink
!= &RootBridgeDev
->ChildList
) {
419 PciIoDevice
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
421 if (IS_PCI_BRIDGE (&PciIoDevice
->Pci
)) {
422 if (AdjustPciDeviceBarSize (PciIoDevice
)) {
426 if (PciIoDevice
->ResizableBarOffset
!= 0) {
429 "PciBus: [%02x|%02x|%02x] Adjust Pci Device Bar Size\n",
430 PciIoDevice
->BusNumber
,
431 PciIoDevice
->DeviceNumber
,
432 PciIoDevice
->FunctionNumber
434 PciProgramResizableBar (PciIoDevice
, PciResizableBarMin
);
436 // Start to parse the bars
438 for (Offset
= 0x10, BarIndex
= 0; Offset
<= 0x24 && BarIndex
< PCI_MAX_BAR
; BarIndex
++) {
439 Offset
= PciParseBar (PciIoDevice
, Offset
, BarIndex
);
444 DumpPciBars (PciIoDevice
);
449 CurrentLink
= CurrentLink
->ForwardLink
;
456 Submits the I/O and memory resource requirements for the specified PCI Host Bridge.
458 @param PciResAlloc Point to protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
460 @retval EFI_SUCCESS Successfully finished resource allocation.
461 @retval EFI_NOT_FOUND Cannot get root bridge instance.
462 @retval EFI_OUT_OF_RESOURCES Platform failed to program the resources if no hot plug supported.
463 @retval other Some error occurred when allocating resources for the PCI Host Bridge.
465 @note Feature flag PcdPciBusHotplugDeviceSupport determine whether need support hotplug.
469 PciHostBridgeResourceAllocator (
470 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
473 PCI_IO_DEVICE
*RootBridgeDev
;
474 EFI_HANDLE RootBridgeHandle
;
483 UINT64 Mem32ResStatus
;
484 UINT64 PMem32ResStatus
;
485 UINT64 Mem64ResStatus
;
486 UINT64 PMem64ResStatus
;
487 UINT32 MaxOptionRomSize
;
488 PCI_RESOURCE_NODE
*IoBridge
;
489 PCI_RESOURCE_NODE
*Mem32Bridge
;
490 PCI_RESOURCE_NODE
*PMem32Bridge
;
491 PCI_RESOURCE_NODE
*Mem64Bridge
;
492 PCI_RESOURCE_NODE
*PMem64Bridge
;
493 PCI_RESOURCE_NODE IoPool
;
494 PCI_RESOURCE_NODE Mem32Pool
;
495 PCI_RESOURCE_NODE PMem32Pool
;
496 PCI_RESOURCE_NODE Mem64Pool
;
497 PCI_RESOURCE_NODE PMem64Pool
;
498 EFI_DEVICE_HANDLE_EXTENDED_DATA_PAYLOAD HandleExtendedData
;
499 EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA_PAYLOAD AllocFailExtendedData
;
500 BOOLEAN ResizableBarNeedAdjust
;
501 BOOLEAN ResizableBarAdjusted
;
503 ResizableBarNeedAdjust
= PcdGetBool (PcdPcieResizableBarSupport
);
506 // It may try several times if the resource allocation fails
510 // Initialize resource pool
512 InitializeResourcePool (&IoPool
, PciBarTypeIo16
);
513 InitializeResourcePool (&Mem32Pool
, PciBarTypeMem32
);
514 InitializeResourcePool (&PMem32Pool
, PciBarTypePMem32
);
515 InitializeResourcePool (&Mem64Pool
, PciBarTypeMem64
);
516 InitializeResourcePool (&PMem64Pool
, PciBarTypePMem64
);
518 RootBridgeDev
= NULL
;
519 RootBridgeHandle
= 0;
521 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
523 // Get Root Bridge Device by handle
525 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
527 if (RootBridgeDev
== NULL
) {
528 return EFI_NOT_FOUND
;
532 // Create the entire system resource map from the information collected by
533 // enumerator. Several resource tree was created
537 // If non-standard PCI Bridge I/O window alignment is supported,
538 // set I/O aligment to minimum possible alignment for root bridge.
540 IoBridge
= CreateResourceNode (
543 FeaturePcdGet (PcdPciBridgeIoAlignmentProbe
) ? 0x1FF : 0xFFF,
549 Mem32Bridge
= CreateResourceNode (
558 PMem32Bridge
= CreateResourceNode (
567 Mem64Bridge
= CreateResourceNode (
576 PMem64Bridge
= CreateResourceNode (
586 // Get the max ROM size that the root bridge can process
587 // Insert to resource map so that there will be dedicate MEM32 resource range for Option ROM.
588 // All devices' Option ROM share the same MEM32 resource.
590 MaxOptionRomSize
= GetMaxOptionRomSize (RootBridgeDev
);
591 if (MaxOptionRomSize
!= 0) {
592 RootBridgeDev
->PciBar
[0].BarType
= PciBarTypeOpRom
;
593 RootBridgeDev
->PciBar
[0].Length
= MaxOptionRomSize
;
594 RootBridgeDev
->PciBar
[0].Alignment
= MaxOptionRomSize
- 1;
595 GetResourceFromDevice (RootBridgeDev
, IoBridge
, Mem32Bridge
, PMem32Bridge
, Mem64Bridge
, PMem64Bridge
);
599 // Create resourcemap by going through all the devices subject to this root bridge
611 // Based on the all the resource tree, construct ACPI resource node to
612 // submit the resource aperture to pci host bridge protocol
614 Status
= ConstructAcpiResourceRequestor (
625 // Insert these resource nodes into the database
627 InsertResourceNode (&IoPool
, IoBridge
);
628 InsertResourceNode (&Mem32Pool
, Mem32Bridge
);
629 InsertResourceNode (&PMem32Pool
, PMem32Bridge
);
630 InsertResourceNode (&Mem64Pool
, Mem64Bridge
);
631 InsertResourceNode (&PMem64Pool
, PMem64Bridge
);
633 if (Status
== EFI_SUCCESS
) {
635 // Submit the resource requirement
637 Status
= PciResAlloc
->SubmitResources (
639 RootBridgeDev
->Handle
,
643 // If SubmitResources returns error, PciBus isn't able to start.
644 // It's a fatal error so assertion is added.
646 DEBUG ((DEBUG_INFO
, "PciBus: HostBridge->SubmitResources() - %r\n", Status
));
647 ASSERT_EFI_ERROR (Status
);
651 // Free acpi resource node
653 if (AcpiConfig
!= NULL
) {
654 FreePool (AcpiConfig
);
657 if (EFI_ERROR (Status
)) {
659 // Destroy all the resource tree
661 DestroyResourceTree (&IoPool
);
662 DestroyResourceTree (&Mem32Pool
);
663 DestroyResourceTree (&PMem32Pool
);
664 DestroyResourceTree (&Mem64Pool
);
665 DestroyResourceTree (&PMem64Pool
);
671 // End while, at least one Root Bridge should be found.
673 ASSERT (RootBridgeDev
!= NULL
);
676 // Notify platform to start to program the resource
678 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeAllocateResources
);
679 DEBUG ((DEBUG_INFO
, "PciBus: HostBridge->NotifyPhase(AllocateResources) - %r\n", Status
));
680 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
682 // If Hot Plug is not supported
684 if (EFI_ERROR (Status
)) {
686 // Allocation failed, then return
688 return EFI_OUT_OF_RESOURCES
;
692 // Allocation succeed.
693 // Get host bridge handle for status report, and then skip the main while
695 HandleExtendedData
.Handle
= RootBridgeDev
->PciRootBridgeIo
->ParentHandle
;
700 // If Hot Plug is supported
702 if (!EFI_ERROR (Status
)) {
704 // Allocation succeed, then continue the following
710 // If the resource allocation is unsuccessful, free resources on bridge
713 RootBridgeDev
= NULL
;
714 RootBridgeHandle
= 0;
716 IoResStatus
= EFI_RESOURCE_SATISFIED
;
717 Mem32ResStatus
= EFI_RESOURCE_SATISFIED
;
718 PMem32ResStatus
= EFI_RESOURCE_SATISFIED
;
719 Mem64ResStatus
= EFI_RESOURCE_SATISFIED
;
720 PMem64ResStatus
= EFI_RESOURCE_SATISFIED
;
722 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
724 // Get RootBridg Device by handle
726 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
727 if (RootBridgeDev
== NULL
) {
728 return EFI_NOT_FOUND
;
732 // Get host bridge handle for status report
734 HandleExtendedData
.Handle
= RootBridgeDev
->PciRootBridgeIo
->ParentHandle
;
737 // Get acpi resource node for all the resource types
741 Status
= PciResAlloc
->GetProposedResources (
743 RootBridgeDev
->Handle
,
747 if (EFI_ERROR (Status
)) {
751 if (AcpiConfig
!= NULL
) {
753 // Adjust resource allocation policy for each RB
755 GetResourceAllocationStatus (
763 FreePool (AcpiConfig
);
772 // Raise the EFI_IOB_EC_RESOURCE_CONFLICT status code
775 // It is very difficult to follow the spec here
776 // Device path , Bar index can not be get here
778 ZeroMem (&AllocFailExtendedData
, sizeof (AllocFailExtendedData
));
780 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
782 EFI_IO_BUS_PCI
| EFI_IOB_EC_RESOURCE_CONFLICT
,
783 (VOID
*)&AllocFailExtendedData
,
784 sizeof (AllocFailExtendedData
)
788 // When resource conflict happens, adjust the BAR size first.
789 // Only when adjusting BAR size doesn't help or BAR size cannot be adjusted,
790 // reject the device who requests largest resource that causes conflict.
792 ResizableBarAdjusted
= FALSE
;
793 if (ResizableBarNeedAdjust
) {
794 ResizableBarAdjusted
= AdjustPciDeviceBarSize (RootBridgeDev
);
795 ResizableBarNeedAdjust
= FALSE
;
798 if (!ResizableBarAdjusted
) {
799 Status
= PciHostBridgeAdjustAllocation (
814 // Destroy all the resource tree
816 DestroyResourceTree (&IoPool
);
817 DestroyResourceTree (&Mem32Pool
);
818 DestroyResourceTree (&PMem32Pool
);
819 DestroyResourceTree (&Mem64Pool
);
820 DestroyResourceTree (&PMem64Pool
);
822 NotifyPhase (PciResAlloc
, EfiPciHostBridgeFreeResources
);
824 if (EFI_ERROR (Status
)) {
835 // Raise the EFI_IOB_PCI_RES_ALLOC status code
837 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
839 EFI_IO_BUS_PCI
| EFI_IOB_PCI_RES_ALLOC
,
840 (VOID
*)&HandleExtendedData
,
841 sizeof (HandleExtendedData
)
845 // Notify pci bus driver starts to program the resource
847 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeSetResources
);
849 if (EFI_ERROR (Status
)) {
853 RootBridgeDev
= NULL
;
855 RootBridgeHandle
= 0;
857 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
859 // Get RootBridg Device by handle
861 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
863 if (RootBridgeDev
== NULL
) {
864 return EFI_NOT_FOUND
;
868 // Get acpi resource node for all the resource types
871 Status
= PciResAlloc
->GetProposedResources (
873 RootBridgeDev
->Handle
,
877 if (EFI_ERROR (Status
)) {
882 // Get the resource base by interpreting acpi resource node
895 // Create the entire system resource map from the information collected by
896 // enumerator. Several resource tree was created
898 FindResourceNode (RootBridgeDev
, &IoPool
, &IoBridge
);
899 FindResourceNode (RootBridgeDev
, &Mem32Pool
, &Mem32Bridge
);
900 FindResourceNode (RootBridgeDev
, &PMem32Pool
, &PMem32Bridge
);
901 FindResourceNode (RootBridgeDev
, &Mem64Pool
, &Mem64Bridge
);
902 FindResourceNode (RootBridgeDev
, &PMem64Pool
, &PMem64Bridge
);
904 ASSERT (IoBridge
!= NULL
);
905 ASSERT (Mem32Bridge
!= NULL
);
906 ASSERT (PMem32Bridge
!= NULL
);
907 ASSERT (Mem64Bridge
!= NULL
);
908 ASSERT (PMem64Bridge
!= NULL
);
911 // Program IO resources
919 // Program Mem32 resources
927 // Program PMem32 resources
935 // Program Mem64 resources
943 // Program PMem64 resources
951 // Process Option ROM for this root bridge after all BARs are programmed.
952 // The PPB's MEM32 RANGE BAR is re-programmed to the Option ROM BAR Base in order to
953 // shadow the Option ROM of the devices under the PPB.
954 // After the shadow, Option ROM BAR decoding is turned off and the PPB's MEM32 RANGE
955 // BAR is restored back to the original value.
956 // The original value is programmed by ProgramResource() above.
960 "Process Option ROM: BAR Base/Length = %lx/%lx\n",
961 RootBridgeDev
->PciBar
[0].BaseAddress
,
962 RootBridgeDev
->PciBar
[0].Length
964 ProcessOptionRom (RootBridgeDev
, RootBridgeDev
->PciBar
[0].BaseAddress
, RootBridgeDev
->PciBar
[0].Length
);
966 IoBridge
->PciDev
->PciBar
[IoBridge
->Bar
].BaseAddress
= IoBase
;
967 Mem32Bridge
->PciDev
->PciBar
[Mem32Bridge
->Bar
].BaseAddress
= Mem32Base
;
968 PMem32Bridge
->PciDev
->PciBar
[PMem32Bridge
->Bar
].BaseAddress
= PMem32Base
;
969 Mem64Bridge
->PciDev
->PciBar
[Mem64Bridge
->Bar
].BaseAddress
= Mem64Base
;
970 PMem64Bridge
->PciDev
->PciBar
[PMem64Bridge
->Bar
].BaseAddress
= PMem64Base
;
973 // Dump the resource map for current root bridge
976 PCI_RESOURCE_NODE
*Resources
[5];
977 Resources
[0] = IoBridge
;
978 Resources
[1] = Mem32Bridge
;
979 Resources
[2] = PMem32Bridge
;
980 Resources
[3] = Mem64Bridge
;
981 Resources
[4] = PMem64Bridge
;
982 DumpResourceMap (RootBridgeDev
, Resources
, ARRAY_SIZE (Resources
));
985 FreePool (AcpiConfig
);
989 // Destroy all the resource tree
991 DestroyResourceTree (&IoPool
);
992 DestroyResourceTree (&Mem32Pool
);
993 DestroyResourceTree (&PMem32Pool
);
994 DestroyResourceTree (&Mem64Pool
);
995 DestroyResourceTree (&PMem64Pool
);
998 // Notify the resource allocation phase is to end
1000 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndResourceAllocation
);
1006 Allocate NumberOfBuses buses and return the next available PCI bus number.
1008 @param Bridge Bridge device instance.
1009 @param StartBusNumber Current available PCI bus number.
1010 @param NumberOfBuses Number of buses enumerated below the StartBusNumber.
1011 @param NextBusNumber Next available PCI bus number.
1013 @retval EFI_SUCCESS Available bus number resource is enough. Next available PCI bus number
1014 is returned in NextBusNumber.
1015 @retval EFI_OUT_OF_RESOURCES Available bus number resource is not enough for allocation.
1019 PciAllocateBusNumber (
1020 IN PCI_IO_DEVICE
*Bridge
,
1021 IN UINT8 StartBusNumber
,
1022 IN UINT8 NumberOfBuses
,
1023 OUT UINT8
*NextBusNumber
1026 PCI_IO_DEVICE
*RootBridge
;
1027 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BusNumberRanges
;
1029 UINT64 MaxNumberInRange
;
1032 // Get PCI Root Bridge device
1034 RootBridge
= Bridge
;
1035 while (RootBridge
->Parent
!= NULL
) {
1036 RootBridge
= RootBridge
->Parent
;
1040 // Get next available PCI bus number
1042 BusNumberRanges
= RootBridge
->BusNumberRanges
;
1043 while (BusNumberRanges
->Desc
!= ACPI_END_TAG_DESCRIPTOR
) {
1044 MaxNumberInRange
= BusNumberRanges
->AddrRangeMin
+ BusNumberRanges
->AddrLen
- 1;
1045 if ((StartBusNumber
>= BusNumberRanges
->AddrRangeMin
) && (StartBusNumber
<= MaxNumberInRange
)) {
1046 NextNumber
= (UINT8
)(StartBusNumber
+ NumberOfBuses
);
1047 while (NextNumber
> MaxNumberInRange
) {
1049 if (BusNumberRanges
->Desc
== ACPI_END_TAG_DESCRIPTOR
) {
1050 return EFI_OUT_OF_RESOURCES
;
1053 NextNumber
= (UINT8
)(NextNumber
+ (BusNumberRanges
->AddrRangeMin
- (MaxNumberInRange
+ 1)));
1054 MaxNumberInRange
= BusNumberRanges
->AddrRangeMin
+ BusNumberRanges
->AddrLen
- 1;
1057 *NextBusNumber
= NextNumber
;
1064 return EFI_OUT_OF_RESOURCES
;
1068 Scan pci bus and assign bus number to the given PCI bus system.
1070 @param Bridge Bridge device instance.
1071 @param StartBusNumber start point.
1072 @param SubBusNumber Point to sub bus number.
1073 @param PaddedBusRange Customized bus number.
1075 @retval EFI_SUCCESS Successfully scanned and assigned bus number.
1076 @retval other Some error occurred when scanning pci bus.
1078 @note Feature flag PcdPciBusHotplugDeviceSupport determine whether need support hotplug.
1083 IN PCI_IO_DEVICE
*Bridge
,
1084 IN UINT8 StartBusNumber
,
1085 OUT UINT8
*SubBusNumber
,
1086 OUT UINT8
*PaddedBusRange
1098 PCI_IO_DEVICE
*PciDevice
;
1100 EFI_HPC_STATE State
;
1102 EFI_HPC_PADDING_ATTRIBUTES Attributes
;
1103 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
1104 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*NextDescriptors
;
1106 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
1108 UINT32 TempReservedBusNum
;
1110 PciRootBridgeIo
= Bridge
->PciRootBridgeIo
;
1114 Attributes
= (EFI_HPC_PADDING_ATTRIBUTES
)0;
1120 for (Device
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
1121 TempReservedBusNum
= 0;
1122 for (Func
= 0; Func
<= PCI_MAX_FUNC
; Func
++) {
1124 // Check to see whether a pci device is present
1126 Status
= PciDevicePresent (
1134 if (EFI_ERROR (Status
) && (Func
== 0)) {
1136 // go to next device if there is no Function 0
1141 if (EFI_ERROR (Status
)) {
1146 // Get the PCI device information
1148 Status
= PciSearchDevice (
1157 if (EFI_ERROR (Status
)) {
1161 PciAddress
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0);
1163 if (!IS_PCI_BRIDGE (&Pci
)) {
1165 // PCI bridges will be called later
1166 // Here just need for PCI device or PCI to cardbus controller
1167 // EfiPciBeforeChildBusEnumeration for PCI Device Node
1169 PreprocessController (
1171 PciDevice
->BusNumber
,
1172 PciDevice
->DeviceNumber
,
1173 PciDevice
->FunctionNumber
,
1174 EfiPciBeforeChildBusEnumeration
1178 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1180 // For Pci Hotplug controller devcie only
1182 if (gPciHotPlugInit
!= NULL
) {
1184 // Check if it is a Hotplug PCI controller
1186 if (IsRootPciHotPlugController (PciDevice
->DevicePath
, &HpIndex
)) {
1187 gPciRootHpcData
[HpIndex
].Found
= TRUE
;
1189 if (!gPciRootHpcData
[HpIndex
].Initialized
) {
1190 Status
= CreateEventForHpc (HpIndex
, &Event
);
1192 ASSERT (!EFI_ERROR (Status
));
1194 Status
= gPciHotPlugInit
->InitializeRootHpc (
1196 gPciRootHpcPool
[HpIndex
].HpcDevicePath
,
1202 PreprocessController (
1204 PciDevice
->BusNumber
,
1205 PciDevice
->DeviceNumber
,
1206 PciDevice
->FunctionNumber
,
1207 EfiPciBeforeChildBusEnumeration
1214 if (IS_PCI_BRIDGE (&Pci
) || IS_CARDBUS_BRIDGE (&Pci
)) {
1218 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1220 // If Hot Plug is supported,
1221 // Get the bridge information
1224 if (gPciHotPlugInit
!= NULL
) {
1225 if (IsPciHotPlugBus (PciDevice
)) {
1227 // If it is initialized, get the padded bus range
1229 Status
= gPciHotPlugInit
->GetResourcePadding (
1231 PciDevice
->DevicePath
,
1234 (VOID
**)&Descriptors
,
1238 if (EFI_ERROR (Status
)) {
1243 NextDescriptors
= Descriptors
;
1244 Status
= PciGetBusRange (
1251 FreePool (Descriptors
);
1253 if (!EFI_ERROR (Status
)) {
1255 } else if (Status
!= EFI_NOT_FOUND
) {
1257 // EFI_NOT_FOUND is not a real error. It indicates no bus number padding requested.
1265 Status
= PciAllocateBusNumber (Bridge
, *SubBusNumber
, 1, SubBusNumber
);
1266 if (EFI_ERROR (Status
)) {
1270 SecondBus
= *SubBusNumber
;
1272 Register
= (UINT16
)((SecondBus
<< 8) | (UINT16
)StartBusNumber
);
1273 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, PCI_BRIDGE_PRIMARY_BUS_REGISTER_OFFSET
);
1275 Status
= PciRootBridgeIo
->Pci
.Write (
1284 // If it is PPB, resursively search down this bridge
1286 if (IS_PCI_BRIDGE (&Pci
)) {
1288 // Temporarily initialize SubBusNumber to maximum bus number to ensure the
1289 // PCI configuration transaction to go through any PPB
1291 Register
= PciGetMaxBusNumber (Bridge
);
1292 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, PCI_BRIDGE_SUBORDINATE_BUS_REGISTER_OFFSET
);
1293 Status
= PciRootBridgeIo
->Pci
.Write (
1302 // Nofify EfiPciBeforeChildBusEnumeration for PCI Brige
1304 PreprocessController (
1306 PciDevice
->BusNumber
,
1307 PciDevice
->DeviceNumber
,
1308 PciDevice
->FunctionNumber
,
1309 EfiPciBeforeChildBusEnumeration
1312 Status
= PciScanBus (
1318 if (EFI_ERROR (Status
)) {
1323 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
) && BusPadding
) {
1325 // Ensure the device is enabled and initialized
1327 if ((Attributes
== EfiPaddingPciRootBridge
) &&
1328 ((State
& EFI_HPC_STATE_ENABLED
) != 0) &&
1329 ((State
& EFI_HPC_STATE_INITIALIZED
) != 0))
1331 *PaddedBusRange
= (UINT8
)((UINT8
)(BusRange
) + *PaddedBusRange
);
1334 // Reserve the larger one between the actual occupied bus number and padded bus number
1336 Status
= PciAllocateBusNumber (PciDevice
, SecondBus
, (UINT8
)(BusRange
), &PaddedSubBus
);
1337 if (EFI_ERROR (Status
)) {
1341 *SubBusNumber
= MAX (PaddedSubBus
, *SubBusNumber
);
1346 // Set the current maximum bus number under the PPB
1348 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, PCI_BRIDGE_SUBORDINATE_BUS_REGISTER_OFFSET
);
1350 Status
= PciRootBridgeIo
->Pci
.Write (
1359 // It is device. Check PCI IOV for Bus reservation
1360 // Go through each function, just reserve the MAX ReservedBusNum for one device
1362 if (PcdGetBool (PcdSrIovSupport
) && (PciDevice
->SrIovCapabilityOffset
!= 0)) {
1363 if (TempReservedBusNum
< PciDevice
->ReservedBusNum
) {
1364 Status
= PciAllocateBusNumber (PciDevice
, *SubBusNumber
, (UINT8
)(PciDevice
->ReservedBusNum
- TempReservedBusNum
), SubBusNumber
);
1365 if (EFI_ERROR (Status
)) {
1369 TempReservedBusNum
= PciDevice
->ReservedBusNum
;
1372 DEBUG ((DEBUG_INFO
, "PCI-IOV ScanBus - SubBusNumber - 0x%x\n", *SubBusNumber
));
1374 DEBUG ((DEBUG_INFO
, "PCI-IOV ScanBus - SubBusNumber - 0x%x (Update)\n", *SubBusNumber
));
1380 if ((Func
== 0) && !IS_PCI_MULTI_FUNC (&Pci
)) {
1382 // Skip sub functions, this is not a multi function device
1385 Func
= PCI_MAX_FUNC
;
1394 Process Option Rom on the specified root bridge.
1396 @param Bridge Pci root bridge device instance.
1398 @retval EFI_SUCCESS Success process.
1399 @retval other Some error occurred when processing Option Rom on the root bridge.
1403 PciRootBridgeP2CProcess (
1404 IN PCI_IO_DEVICE
*Bridge
1407 LIST_ENTRY
*CurrentLink
;
1408 PCI_IO_DEVICE
*Temp
;
1409 EFI_HPC_STATE State
;
1413 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
1415 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
1416 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
1418 if (IS_CARDBUS_BRIDGE (&Temp
->Pci
)) {
1419 if ((gPciHotPlugInit
!= NULL
) && Temp
->Allocated
&& FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1421 // Raise the EFI_IOB_PCI_HPC_INIT status code
1423 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1425 EFI_IO_BUS_PCI
| EFI_IOB_PCI_HPC_INIT
,
1429 PciAddress
= EFI_PCI_ADDRESS (Temp
->BusNumber
, Temp
->DeviceNumber
, Temp
->FunctionNumber
, 0);
1430 Status
= gPciHotPlugInit
->InitializeRootHpc (
1438 if (!EFI_ERROR (Status
)) {
1439 Status
= PciBridgeEnumerator (Temp
);
1441 if (EFI_ERROR (Status
)) {
1446 CurrentLink
= CurrentLink
->ForwardLink
;
1451 if (!IsListEmpty (&Temp
->ChildList
)) {
1452 Status
= PciRootBridgeP2CProcess (Temp
);
1455 CurrentLink
= CurrentLink
->ForwardLink
;
1462 Process Option Rom on the specified host bridge.
1464 @param PciResAlloc Pointer to instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
1466 @retval EFI_SUCCESS Success process.
1467 @retval EFI_NOT_FOUND Can not find the root bridge instance.
1468 @retval other Some error occurred when processing Option Rom on the host bridge.
1472 PciHostBridgeP2CProcess (
1473 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
1476 EFI_HANDLE RootBridgeHandle
;
1477 PCI_IO_DEVICE
*RootBridgeDev
;
1480 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1484 RootBridgeHandle
= NULL
;
1486 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1488 // Get RootBridg Device by handle
1490 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
1492 if (RootBridgeDev
== NULL
) {
1493 return EFI_NOT_FOUND
;
1496 Status
= PciRootBridgeP2CProcess (RootBridgeDev
);
1497 if (EFI_ERROR (Status
)) {
1506 This function is used to enumerate the entire host bridge
1507 in a given platform.
1509 @param PciResAlloc A pointer to the PCI Host Resource Allocation protocol.
1511 @retval EFI_SUCCESS Successfully enumerated the host bridge.
1512 @retval EFI_OUT_OF_RESOURCES No enough memory available.
1513 @retval other Some error occurred when enumerating the host bridge.
1517 PciHostBridgeEnumerator (
1518 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
1521 EFI_HANDLE RootBridgeHandle
;
1522 PCI_IO_DEVICE
*RootBridgeDev
;
1524 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
1526 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
1527 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration
;
1528 UINT8 StartBusNumber
;
1529 LIST_ENTRY RootBridgeList
;
1531 EFI_STATUS RootBridgeEnumerationStatus
;
1533 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1534 InitializeHotPlugSupport ();
1537 InitializeListHead (&RootBridgeList
);
1540 // Notify the bus allocation phase is about to start
1542 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginBusAllocation
);
1544 if (EFI_ERROR (Status
)) {
1548 DEBUG ((DEBUG_INFO
, "PCI Bus First Scanning\n"));
1549 RootBridgeHandle
= NULL
;
1550 RootBridgeEnumerationStatus
= EFI_SUCCESS
;
1551 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1553 // if a root bridge instance is found, create root bridge device for it
1556 RootBridgeDev
= CreateRootBridge (RootBridgeHandle
);
1558 if (RootBridgeDev
== NULL
) {
1559 return EFI_OUT_OF_RESOURCES
;
1563 // Enumerate all the buses under this root bridge
1565 Status
= PciRootBridgeEnumerator (
1570 if ((gPciHotPlugInit
!= NULL
) && FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1571 InsertTailList (&RootBridgeList
, &(RootBridgeDev
->Link
));
1573 DestroyRootBridge (RootBridgeDev
);
1576 if (EFI_ERROR (Status
)) {
1577 RootBridgeEnumerationStatus
= Status
;
1582 // Notify the bus allocation phase is finished for the first time
1584 NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndBusAllocation
);
1586 if (EFI_ERROR (RootBridgeEnumerationStatus
)) {
1587 return RootBridgeEnumerationStatus
;
1590 if ((gPciHotPlugInit
!= NULL
) && FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1592 // Reset all assigned PCI bus number in all PPB
1594 RootBridgeHandle
= NULL
;
1595 Link
= GetFirstNode (&RootBridgeList
);
1596 while ((PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) &&
1597 (!IsNull (&RootBridgeList
, Link
)))
1599 RootBridgeDev
= PCI_IO_DEVICE_FROM_LINK (Link
);
1601 // Get the Bus information
1603 Status
= PciResAlloc
->StartBusEnumeration (
1606 (VOID
**)&Configuration
1608 if (EFI_ERROR (Status
)) {
1613 // Get the bus number to start with
1615 StartBusNumber
= (UINT8
)(Configuration
->AddrRangeMin
);
1617 ResetAllPpbBusNumber (
1622 FreePool (Configuration
);
1623 Link
= RemoveEntryList (Link
);
1624 DestroyRootBridge (RootBridgeDev
);
1628 // Wait for all HPC initialized
1630 Status
= AllRootHPCInitialized (STALL_1_SECOND
* 15);
1632 if (EFI_ERROR (Status
)) {
1633 DEBUG ((DEBUG_ERROR
, "Some root HPC failed to initialize\n"));
1638 // Notify the bus allocation phase is about to start for the 2nd time
1640 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginBusAllocation
);
1642 if (EFI_ERROR (Status
)) {
1646 DEBUG ((DEBUG_INFO
, "PCI Bus Second Scanning\n"));
1647 RootBridgeHandle
= NULL
;
1648 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1650 // if a root bridge instance is found, create root bridge device for it
1652 RootBridgeDev
= CreateRootBridge (RootBridgeHandle
);
1654 if (RootBridgeDev
== NULL
) {
1655 return EFI_OUT_OF_RESOURCES
;
1659 // Enumerate all the buses under this root bridge
1661 Status
= PciRootBridgeEnumerator (
1666 DestroyRootBridge (RootBridgeDev
);
1667 if (EFI_ERROR (Status
)) {
1668 RootBridgeEnumerationStatus
= Status
;
1673 // Notify the bus allocation phase is to end for the 2nd time
1675 NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndBusAllocation
);
1677 if (EFI_ERROR (RootBridgeEnumerationStatus
)) {
1678 return RootBridgeEnumerationStatus
;
1683 // Notify the resource allocation phase is to start
1685 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginResourceAllocation
);
1687 if (EFI_ERROR (Status
)) {
1691 RootBridgeHandle
= NULL
;
1692 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1694 // if a root bridge instance is found, create root bridge device for it
1696 RootBridgeDev
= CreateRootBridge (RootBridgeHandle
);
1698 if (RootBridgeDev
== NULL
) {
1699 return EFI_OUT_OF_RESOURCES
;
1702 Status
= StartManagingRootBridge (RootBridgeDev
);
1704 if (EFI_ERROR (Status
)) {
1708 PciRootBridgeIo
= RootBridgeDev
->PciRootBridgeIo
;
1709 Status
= PciRootBridgeIo
->Configuration (PciRootBridgeIo
, (VOID
**)&Descriptors
);
1711 if (EFI_ERROR (Status
)) {
1715 Status
= PciGetBusRange (&Descriptors
, &MinBus
, NULL
, NULL
);
1717 if (EFI_ERROR (Status
)) {
1722 // Determine root bridge attribute by calling interface of Pcihostbridge
1725 DetermineRootBridgeAttributes (
1731 // Collect all the resource information under this root bridge
1732 // A database that records all the information about pci device subject to this
1733 // root bridge will then be created
1735 Status
= PciPciDeviceInfoCollector (
1740 if (EFI_ERROR (Status
)) {
1744 InsertRootBridge (RootBridgeDev
);
1747 // Record the hostbridge handle
1749 AddHostBridgeEnumerator (RootBridgeDev
->PciRootBridgeIo
->ParentHandle
);
1756 This function is used to program the Resizable BAR Register.
1758 @param PciIoDevice A pointer to the PCI_IO_DEVICE.
1759 @param ResizableBarOp PciResizableBarMax: Set BAR to max size
1760 PciResizableBarMin: set BAR to min size.
1762 @retval EFI_SUCCESS Successfully enumerated the host bridge.
1763 @retval other Some error occurred when enumerating the host bridge.
1767 PciProgramResizableBar (
1768 IN PCI_IO_DEVICE
*PciIoDevice
,
1769 IN PCI_RESIZABLE_BAR_OPERATION ResizableBarOp
1772 EFI_PCI_IO_PROTOCOL
*PciIo
;
1773 UINT64 Capabilities
;
1777 UINTN ResizableBarNumber
;
1779 PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY Entries
[PCI_MAX_BAR
];
1781 ASSERT (PciIoDevice
->ResizableBarOffset
!= 0);
1785 " Programs Resizable BAR register, offset: 0x%08x, number: %d\n",
1786 PciIoDevice
->ResizableBarOffset
,
1787 PciIoDevice
->ResizableBarNumber
1790 ResizableBarNumber
= MIN (PciIoDevice
->ResizableBarNumber
, PCI_MAX_BAR
);
1791 PciIo
= &PciIoDevice
->PciIo
;
1792 Status
= PciIo
->Pci
.Read (
1795 PciIoDevice
->ResizableBarOffset
+ sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_HEADER
),
1796 sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY
) * ResizableBarNumber
,
1799 ASSERT_EFI_ERROR (Status
);
1801 for (Index
= 0; Index
< ResizableBarNumber
; Index
++) {
1803 // When the bit of Capabilities Set, indicates that the Function supports
1804 // operating with the BAR sized to (2^Bit) MB.
1806 // Bit 0 is set: supports operating with the BAR sized to 1 MB
1807 // Bit 1 is set: supports operating with the BAR sized to 2 MB
1808 // Bit n is set: supports operating with the BAR sized to (2^n) MB
1810 Capabilities
= LShiftU64 (Entries
[Index
].ResizableBarControl
.Bits
.BarSizeCapability
, 28)
1811 | Entries
[Index
].ResizableBarCapability
.Bits
.BarSizeCapability
;
1813 if (ResizableBarOp
== PciResizableBarMax
) {
1814 Bit
= HighBitSet64 (Capabilities
);
1816 ASSERT (ResizableBarOp
== PciResizableBarMin
);
1817 Bit
= LowBitSet64 (Capabilities
);
1822 Offset
= PciIoDevice
->ResizableBarOffset
+ sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_HEADER
)
1823 + Index
* sizeof (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY
)
1824 + OFFSET_OF (PCI_EXPRESS_EXTENDED_CAPABILITIES_RESIZABLE_BAR_ENTRY
, ResizableBarControl
);
1826 Entries
[Index
].ResizableBarControl
.Bits
.BarSize
= (UINT32
)Bit
;
1829 " Resizable Bar: Offset = 0x%x, Bar Size Capability = 0x%016lx, New Bar Size = 0x%lx\n",
1830 OFFSET_OF (PCI_TYPE00
, Device
.Bar
[Entries
[Index
].ResizableBarControl
.Bits
.BarIndex
]),
1832 LShiftU64 (SIZE_1MB
, Bit
)
1836 EfiPciIoWidthUint32
,
1839 &Entries
[Index
].ResizableBarControl
.Uint32