2 Internal library implementation for PCI Bus module.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 (C) Copyright 2015 Hewlett Packard Enterprise Development LP<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 GLOBAL_REMOVE_IF_UNREFERENCED
19 CHAR16
*mBarTypeStr
[] = {
34 Retrieve the max bus number that is assigned to the Root Bridge hierarchy.
35 It can support the case that there are multiple bus ranges.
37 @param Bridge Bridge device instance.
39 @retval The max bus number that is assigned to this Root Bridge hierarchy.
44 IN PCI_IO_DEVICE
*Bridge
47 PCI_IO_DEVICE
*RootBridge
;
48 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BusNumberRanges
;
49 UINT64 MaxNumberInRange
;
52 // Get PCI Root Bridge device
55 while (RootBridge
->Parent
!= NULL
) {
56 RootBridge
= RootBridge
->Parent
;
60 // Iterate the bus number ranges to get max PCI bus number
62 BusNumberRanges
= RootBridge
->BusNumberRanges
;
63 while (BusNumberRanges
->Desc
!= ACPI_END_TAG_DESCRIPTOR
) {
64 MaxNumberInRange
= BusNumberRanges
->AddrRangeMin
+ BusNumberRanges
->AddrLen
- 1;
67 return (UINT16
) MaxNumberInRange
;
71 Retrieve the PCI Card device BAR information via PciIo interface.
73 @param PciIoDevice PCI Card device instance.
78 IN PCI_IO_DEVICE
*PciIoDevice
83 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
88 // Read PciBar information from the bar register
90 if (!gFullEnumeration
) {
92 PciIoDevice
->PciIo
.Pci
.Read (
93 &(PciIoDevice
->PciIo
),
95 PCI_CARD_MEMORY_BASE_0
,
100 (PciIoDevice
->PciBar
)[P2C_MEM_1
].BaseAddress
= (UINT64
) (Address
);
101 (PciIoDevice
->PciBar
)[P2C_MEM_1
].Length
= 0x2000000;
102 (PciIoDevice
->PciBar
)[P2C_MEM_1
].BarType
= PciBarTypeMem32
;
105 PciIoDevice
->PciIo
.Pci
.Read (
106 &(PciIoDevice
->PciIo
),
108 PCI_CARD_MEMORY_BASE_1
,
112 (PciIoDevice
->PciBar
)[P2C_MEM_2
].BaseAddress
= (UINT64
) (Address
);
113 (PciIoDevice
->PciBar
)[P2C_MEM_2
].Length
= 0x2000000;
114 (PciIoDevice
->PciBar
)[P2C_MEM_2
].BarType
= PciBarTypePMem32
;
117 PciIoDevice
->PciIo
.Pci
.Read (
118 &(PciIoDevice
->PciIo
),
120 PCI_CARD_IO_BASE_0_LOWER
,
124 (PciIoDevice
->PciBar
)[P2C_IO_1
].BaseAddress
= (UINT64
) (Address
);
125 (PciIoDevice
->PciBar
)[P2C_IO_1
].Length
= 0x100;
126 (PciIoDevice
->PciBar
)[P2C_IO_1
].BarType
= PciBarTypeIo16
;
129 PciIoDevice
->PciIo
.Pci
.Read (
130 &(PciIoDevice
->PciIo
),
132 PCI_CARD_IO_BASE_1_LOWER
,
136 (PciIoDevice
->PciBar
)[P2C_IO_2
].BaseAddress
= (UINT64
) (Address
);
137 (PciIoDevice
->PciBar
)[P2C_IO_2
].Length
= 0x100;
138 (PciIoDevice
->PciBar
)[P2C_IO_2
].BarType
= PciBarTypeIo16
;
142 if (gPciHotPlugInit
!= NULL
&& FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
143 GetResourcePaddingForHpb (PciIoDevice
);
148 Remove rejected pci device from specific root bridge
151 @param RootBridgeHandle Specific parent root bridge handle.
152 @param Bridge Bridge device instance.
156 RemoveRejectedPciDevices (
157 IN EFI_HANDLE RootBridgeHandle
,
158 IN PCI_IO_DEVICE
*Bridge
162 LIST_ENTRY
*CurrentLink
;
163 LIST_ENTRY
*LastLink
;
165 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
169 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
171 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
173 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
175 if (IS_PCI_BRIDGE (&Temp
->Pci
)) {
177 // Remove rejected devices recusively
179 RemoveRejectedPciDevices (RootBridgeHandle
, Temp
);
182 // Skip rejection for all PPBs, while detect rejection for others
184 if (IsPciDeviceRejected (Temp
)) {
187 // For P2C, remove all devices on it
189 if (!IsListEmpty (&Temp
->ChildList
)) {
190 RemoveAllPciDeviceOnBridge (RootBridgeHandle
, Temp
);
194 // Finally remove itself
196 LastLink
= CurrentLink
->BackLink
;
197 RemoveEntryList (CurrentLink
);
198 FreePciDevice (Temp
);
200 CurrentLink
= LastLink
;
204 CurrentLink
= CurrentLink
->ForwardLink
;
209 Dump the resourc map of the bridge device.
211 @param[in] BridgeResource Resource descriptor of the bridge device.
215 IN PCI_RESOURCE_NODE
*BridgeResource
219 PCI_RESOURCE_NODE
*Resource
;
222 if ((BridgeResource
!= NULL
) && (BridgeResource
->Length
!= 0)) {
224 EFI_D_INFO
, "Type = %s; Base = 0x%lx;\tLength = 0x%lx;\tAlignment = 0x%lx\n",
225 mBarTypeStr
[MIN (BridgeResource
->ResType
, PciBarTypeMaxType
)],
226 BridgeResource
->PciDev
->PciBar
[BridgeResource
->Bar
].BaseAddress
,
227 BridgeResource
->Length
, BridgeResource
->Alignment
229 for ( Link
= GetFirstNode (&BridgeResource
->ChildList
)
230 ; !IsNull (&BridgeResource
->ChildList
, Link
)
231 ; Link
= GetNextNode (&BridgeResource
->ChildList
, Link
)
233 Resource
= RESOURCE_NODE_FROM_LINK (Link
);
234 if (Resource
->ResourceUsage
== PciResUsageTypical
) {
235 Bar
= Resource
->Virtual
? Resource
->PciDev
->VfPciBar
: Resource
->PciDev
->PciBar
;
237 EFI_D_INFO
, " Base = 0x%lx;\tLength = 0x%lx;\tAlignment = 0x%lx;\tOwner = %s [%02x|%02x|%02x:",
238 Bar
[Resource
->Bar
].BaseAddress
, Resource
->Length
, Resource
->Alignment
,
239 IS_PCI_BRIDGE (&Resource
->PciDev
->Pci
) ? L
"PPB" :
240 IS_CARDBUS_BRIDGE (&Resource
->PciDev
->Pci
) ? L
"P2C" :
242 Resource
->PciDev
->BusNumber
, 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
))
251 // The resource requirement comes from the device itself.
253 DEBUG ((EFI_D_INFO
, "%02x]", Bar
[Resource
->Bar
].Offset
));
256 // The resource requirement comes from the subordinate devices.
258 DEBUG ((EFI_D_INFO
, "**]"));
261 DEBUG ((EFI_D_INFO
, " Base = Padding;\tLength = 0x%lx;\tAlignment = 0x%lx", Resource
->Length
, Resource
->Alignment
));
263 if (BridgeResource
->ResType
!= Resource
->ResType
) {
264 DEBUG ((EFI_D_INFO
, "; Type = %s", mBarTypeStr
[MIN (Resource
->ResType
, PciBarTypeMaxType
)]));
266 DEBUG ((EFI_D_INFO
, "\n"));
272 Find the corresponding resource node for the Device in child list of BridgeResource.
274 @param[in] Device Pointer to PCI_IO_DEVICE.
275 @param[in] BridgeResource Pointer to PCI_RESOURCE_NODE.
276 @param[out] DeviceResources Pointer to a buffer to receive resources for the Device.
278 @return Count of the resource descriptors returned.
282 IN PCI_IO_DEVICE
*Device
,
283 IN PCI_RESOURCE_NODE
*BridgeResource
,
284 OUT PCI_RESOURCE_NODE
**DeviceResources OPTIONAL
288 PCI_RESOURCE_NODE
*Resource
;
292 for ( Link
= BridgeResource
->ChildList
.ForwardLink
293 ; Link
!= &BridgeResource
->ChildList
294 ; Link
= Link
->ForwardLink
296 Resource
= RESOURCE_NODE_FROM_LINK (Link
);
297 if (Resource
->PciDev
== Device
) {
298 if (DeviceResources
!= NULL
) {
299 DeviceResources
[Count
] = Resource
;
309 Dump the resource map of all the devices under Bridge.
311 @param[in] Bridge Bridge device instance.
312 @param[in] Resources Resource descriptors for the bridge device.
313 @param[in] ResourceCount Count of resource descriptors.
317 IN PCI_IO_DEVICE
*Bridge
,
318 IN PCI_RESOURCE_NODE
**Resources
,
319 IN UINTN ResourceCount
324 PCI_IO_DEVICE
*Device
;
327 PCI_RESOURCE_NODE
**ChildResources
;
328 UINTN ChildResourceCount
;
330 DEBUG ((EFI_D_INFO
, "PciBus: Resource Map for "));
332 Status
= gBS
->OpenProtocol (
334 &gEfiPciRootBridgeIoProtocolGuid
,
338 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
340 if (EFI_ERROR (Status
)) {
342 EFI_D_INFO
, "Bridge [%02x|%02x|%02x]\n",
343 Bridge
->BusNumber
, Bridge
->DeviceNumber
, Bridge
->FunctionNumber
346 Str
= ConvertDevicePathToText (
347 DevicePathFromHandle (Bridge
->Handle
),
351 DEBUG ((EFI_D_INFO
, "Root Bridge %s\n", Str
!= NULL
? Str
: L
""));
357 for (Index
= 0; Index
< ResourceCount
; Index
++) {
358 DumpBridgeResource (Resources
[Index
]);
360 DEBUG ((EFI_D_INFO
, "\n"));
362 for ( Link
= Bridge
->ChildList
.ForwardLink
363 ; Link
!= &Bridge
->ChildList
364 ; Link
= Link
->ForwardLink
366 Device
= PCI_IO_DEVICE_FROM_LINK (Link
);
367 if (IS_PCI_BRIDGE (&Device
->Pci
)) {
369 ChildResourceCount
= 0;
370 for (Index
= 0; Index
< ResourceCount
; Index
++) {
371 ChildResourceCount
+= FindResourceNode (Device
, Resources
[Index
], NULL
);
373 ChildResources
= AllocatePool (sizeof (PCI_RESOURCE_NODE
*) * ChildResourceCount
);
374 ASSERT (ChildResources
!= NULL
);
375 ChildResourceCount
= 0;
376 for (Index
= 0; Index
< ResourceCount
; Index
++) {
377 ChildResourceCount
+= FindResourceNode (Device
, Resources
[Index
], &ChildResources
[ChildResourceCount
]);
380 DumpResourceMap (Device
, ChildResources
, ChildResourceCount
);
381 FreePool (ChildResources
);
387 Submits the I/O and memory resource requirements for the specified PCI Host Bridge.
389 @param PciResAlloc Point to protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
391 @retval EFI_SUCCESS Successfully finished resource allocation.
392 @retval EFI_NOT_FOUND Cannot get root bridge instance.
393 @retval EFI_OUT_OF_RESOURCES Platform failed to program the resources if no hot plug supported.
394 @retval other Some error occurred when allocating resources for the PCI Host Bridge.
396 @note Feature flag PcdPciBusHotplugDeviceSupport determine whether need support hotplug.
400 PciHostBridgeResourceAllocator (
401 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
404 PCI_IO_DEVICE
*RootBridgeDev
;
405 EFI_HANDLE RootBridgeHandle
;
414 UINT64 Mem32ResStatus
;
415 UINT64 PMem32ResStatus
;
416 UINT64 Mem64ResStatus
;
417 UINT64 PMem64ResStatus
;
418 UINT64 MaxOptionRomSize
;
419 PCI_RESOURCE_NODE
*IoBridge
;
420 PCI_RESOURCE_NODE
*Mem32Bridge
;
421 PCI_RESOURCE_NODE
*PMem32Bridge
;
422 PCI_RESOURCE_NODE
*Mem64Bridge
;
423 PCI_RESOURCE_NODE
*PMem64Bridge
;
424 PCI_RESOURCE_NODE IoPool
;
425 PCI_RESOURCE_NODE Mem32Pool
;
426 PCI_RESOURCE_NODE PMem32Pool
;
427 PCI_RESOURCE_NODE Mem64Pool
;
428 PCI_RESOURCE_NODE PMem64Pool
;
429 EFI_DEVICE_HANDLE_EXTENDED_DATA_PAYLOAD HandleExtendedData
;
430 EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA_PAYLOAD AllocFailExtendedData
;
433 // It may try several times if the resource allocation fails
437 // Initialize resource pool
439 InitializeResourcePool (&IoPool
, PciBarTypeIo16
);
440 InitializeResourcePool (&Mem32Pool
, PciBarTypeMem32
);
441 InitializeResourcePool (&PMem32Pool
, PciBarTypePMem32
);
442 InitializeResourcePool (&Mem64Pool
, PciBarTypeMem64
);
443 InitializeResourcePool (&PMem64Pool
, PciBarTypePMem64
);
445 RootBridgeDev
= NULL
;
446 RootBridgeHandle
= 0;
448 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
450 // Get Root Bridge Device by handle
452 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
454 if (RootBridgeDev
== NULL
) {
455 return EFI_NOT_FOUND
;
459 // Create the entire system resource map from the information collected by
460 // enumerator. Several resource tree was created
464 // If non-standard PCI Bridge I/O window alignment is supported,
465 // set I/O aligment to minimum possible alignment for root bridge.
467 IoBridge
= CreateResourceNode (
470 FeaturePcdGet (PcdPciBridgeIoAlignmentProbe
) ? 0x1FF: 0xFFF,
476 Mem32Bridge
= CreateResourceNode (
485 PMem32Bridge
= CreateResourceNode (
494 Mem64Bridge
= CreateResourceNode (
503 PMem64Bridge
= CreateResourceNode (
513 // Get the max ROM size that the root bridge can process
514 // Insert to resource map so that there will be dedicate MEM32 resource range for Option ROM.
515 // All devices' Option ROM share the same MEM32 resource.
517 MaxOptionRomSize
= GetMaxOptionRomSize (RootBridgeDev
);
518 RootBridgeDev
->PciBar
[0].BarType
= PciBarTypeOpRom
;
519 RootBridgeDev
->PciBar
[0].Length
= MaxOptionRomSize
;
520 RootBridgeDev
->PciBar
[0].Alignment
= MaxOptionRomSize
- 1;
521 GetResourceFromDevice (RootBridgeDev
, IoBridge
, Mem32Bridge
, PMem32Bridge
, Mem64Bridge
, PMem64Bridge
);
524 // Create resourcemap by going through all the devices subject to this root bridge
536 // Based on the all the resource tree, construct ACPI resource node to
537 // submit the resource aperture to pci host bridge protocol
539 Status
= ConstructAcpiResourceRequestor (
550 // Insert these resource nodes into the database
552 InsertResourceNode (&IoPool
, IoBridge
);
553 InsertResourceNode (&Mem32Pool
, Mem32Bridge
);
554 InsertResourceNode (&PMem32Pool
, PMem32Bridge
);
555 InsertResourceNode (&Mem64Pool
, Mem64Bridge
);
556 InsertResourceNode (&PMem64Pool
, PMem64Bridge
);
558 if (Status
== EFI_SUCCESS
) {
560 // Submit the resource requirement
562 Status
= PciResAlloc
->SubmitResources (
564 RootBridgeDev
->Handle
,
568 // If SubmitResources returns error, PciBus isn't able to start.
569 // It's a fatal error so assertion is added.
571 DEBUG ((EFI_D_INFO
, "PciBus: HostBridge->SubmitResources() - %r\n", Status
));
572 ASSERT_EFI_ERROR (Status
);
576 // Free acpi resource node
578 if (AcpiConfig
!= NULL
) {
579 FreePool (AcpiConfig
);
582 if (EFI_ERROR (Status
)) {
584 // Destroy all the resource tree
586 DestroyResourceTree (&IoPool
);
587 DestroyResourceTree (&Mem32Pool
);
588 DestroyResourceTree (&PMem32Pool
);
589 DestroyResourceTree (&Mem64Pool
);
590 DestroyResourceTree (&PMem64Pool
);
595 // End while, at least one Root Bridge should be found.
597 ASSERT (RootBridgeDev
!= NULL
);
600 // Notify platform to start to program the resource
602 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeAllocateResources
);
603 DEBUG ((EFI_D_INFO
, "PciBus: HostBridge->NotifyPhase(AllocateResources) - %r\n", Status
));
604 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
606 // If Hot Plug is not supported
608 if (EFI_ERROR (Status
)) {
610 // Allocation failed, then return
612 return EFI_OUT_OF_RESOURCES
;
615 // Allocation succeed.
616 // Get host bridge handle for status report, and then skip the main while
618 HandleExtendedData
.Handle
= RootBridgeDev
->PciRootBridgeIo
->ParentHandle
;
624 // If Hot Plug is supported
626 if (!EFI_ERROR (Status
)) {
628 // Allocation succeed, then continue the following
634 // If the resource allocation is unsuccessful, free resources on bridge
637 RootBridgeDev
= NULL
;
638 RootBridgeHandle
= 0;
640 IoResStatus
= EFI_RESOURCE_SATISFIED
;
641 Mem32ResStatus
= EFI_RESOURCE_SATISFIED
;
642 PMem32ResStatus
= EFI_RESOURCE_SATISFIED
;
643 Mem64ResStatus
= EFI_RESOURCE_SATISFIED
;
644 PMem64ResStatus
= EFI_RESOURCE_SATISFIED
;
646 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
648 // Get RootBridg Device by handle
650 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
651 if (RootBridgeDev
== NULL
) {
652 return EFI_NOT_FOUND
;
656 // Get host bridge handle for status report
658 HandleExtendedData
.Handle
= RootBridgeDev
->PciRootBridgeIo
->ParentHandle
;
661 // Get acpi resource node for all the resource types
665 Status
= PciResAlloc
->GetProposedResources (
667 RootBridgeDev
->Handle
,
671 if (EFI_ERROR (Status
)) {
675 if (AcpiConfig
!= NULL
) {
677 // Adjust resource allocation policy for each RB
679 GetResourceAllocationStatus (
687 FreePool (AcpiConfig
);
695 // Raise the EFI_IOB_EC_RESOURCE_CONFLICT status code
698 // It is very difficult to follow the spec here
699 // Device path , Bar index can not be get here
701 ZeroMem (&AllocFailExtendedData
, sizeof (AllocFailExtendedData
));
703 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
705 EFI_IO_BUS_PCI
| EFI_IOB_EC_RESOURCE_CONFLICT
,
706 (VOID
*) &AllocFailExtendedData
,
707 sizeof (AllocFailExtendedData
)
710 Status
= PciHostBridgeAdjustAllocation (
724 // Destroy all the resource tree
726 DestroyResourceTree (&IoPool
);
727 DestroyResourceTree (&Mem32Pool
);
728 DestroyResourceTree (&PMem32Pool
);
729 DestroyResourceTree (&Mem64Pool
);
730 DestroyResourceTree (&PMem64Pool
);
732 NotifyPhase (PciResAlloc
, EfiPciHostBridgeFreeResources
);
734 if (EFI_ERROR (Status
)) {
744 // Raise the EFI_IOB_PCI_RES_ALLOC status code
746 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
748 EFI_IO_BUS_PCI
| EFI_IOB_PCI_RES_ALLOC
,
749 (VOID
*) &HandleExtendedData
,
750 sizeof (HandleExtendedData
)
754 // Notify pci bus driver starts to program the resource
756 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeSetResources
);
758 if (EFI_ERROR (Status
)) {
762 RootBridgeDev
= NULL
;
764 RootBridgeHandle
= 0;
766 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
768 // Get RootBridg Device by handle
770 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
772 if (RootBridgeDev
== NULL
) {
773 return EFI_NOT_FOUND
;
777 // Get acpi resource node for all the resource types
780 Status
= PciResAlloc
->GetProposedResources (
782 RootBridgeDev
->Handle
,
786 if (EFI_ERROR (Status
)) {
791 // Get the resource base by interpreting acpi resource node
804 // Create the entire system resource map from the information collected by
805 // enumerator. Several resource tree was created
807 FindResourceNode (RootBridgeDev
, &IoPool
, &IoBridge
);
808 FindResourceNode (RootBridgeDev
, &Mem32Pool
, &Mem32Bridge
);
809 FindResourceNode (RootBridgeDev
, &PMem32Pool
, &PMem32Bridge
);
810 FindResourceNode (RootBridgeDev
, &Mem64Pool
, &Mem64Bridge
);
811 FindResourceNode (RootBridgeDev
, &PMem64Pool
, &PMem64Bridge
);
813 ASSERT (IoBridge
!= NULL
);
814 ASSERT (Mem32Bridge
!= NULL
);
815 ASSERT (PMem32Bridge
!= NULL
);
816 ASSERT (Mem64Bridge
!= NULL
);
817 ASSERT (PMem64Bridge
!= NULL
);
820 // Program IO resources
828 // Program Mem32 resources
836 // Program PMem32 resources
844 // Program Mem64 resources
852 // Program PMem64 resources
860 // Process Option ROM for this root bridge after all BARs are programmed.
861 // The PPB's MEM32 RANGE BAR is re-programmed to the Option ROM BAR Base in order to
862 // shadow the Option ROM of the devices under the PPB.
863 // After the shadow, Option ROM BAR decoding is turned off and the PPB's MEM32 RANGE
864 // BAR is restored back to the original value.
865 // The original value is programmed by ProgramResource() above.
868 DEBUG_INFO
, "Process Option ROM: BAR Base/Length = %lx/%lx\n",
869 RootBridgeDev
->PciBar
[0].BaseAddress
, RootBridgeDev
->PciBar
[0].Length
871 ProcessOptionRom (RootBridgeDev
, RootBridgeDev
->PciBar
[0].BaseAddress
, RootBridgeDev
->PciBar
[0].Length
);
873 IoBridge
->PciDev
->PciBar
[IoBridge
->Bar
].BaseAddress
= IoBase
;
874 Mem32Bridge
->PciDev
->PciBar
[Mem32Bridge
->Bar
].BaseAddress
= Mem32Base
;
875 PMem32Bridge
->PciDev
->PciBar
[PMem32Bridge
->Bar
].BaseAddress
= PMem32Base
;
876 Mem64Bridge
->PciDev
->PciBar
[Mem64Bridge
->Bar
].BaseAddress
= Mem64Base
;
877 PMem64Bridge
->PciDev
->PciBar
[PMem64Bridge
->Bar
].BaseAddress
= PMem64Base
;
880 // Dump the resource map for current root bridge
883 PCI_RESOURCE_NODE
*Resources
[5];
884 Resources
[0] = IoBridge
;
885 Resources
[1] = Mem32Bridge
;
886 Resources
[2] = PMem32Bridge
;
887 Resources
[3] = Mem64Bridge
;
888 Resources
[4] = PMem64Bridge
;
889 DumpResourceMap (RootBridgeDev
, Resources
, ARRAY_SIZE (Resources
));
892 FreePool (AcpiConfig
);
896 // Destroy all the resource tree
898 DestroyResourceTree (&IoPool
);
899 DestroyResourceTree (&Mem32Pool
);
900 DestroyResourceTree (&PMem32Pool
);
901 DestroyResourceTree (&Mem64Pool
);
902 DestroyResourceTree (&PMem64Pool
);
905 // Notify the resource allocation phase is to end
907 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndResourceAllocation
);
913 Allocate NumberOfBuses buses and return the next available PCI bus number.
915 @param Bridge Bridge device instance.
916 @param StartBusNumber Current available PCI bus number.
917 @param NumberOfBuses Number of buses enumerated below the StartBusNumber.
918 @param NextBusNumber Next available PCI bus number.
920 @retval EFI_SUCCESS Available bus number resource is enough. Next available PCI bus number
921 is returned in NextBusNumber.
922 @retval EFI_OUT_OF_RESOURCES Available bus number resource is not enough for allocation.
926 PciAllocateBusNumber (
927 IN PCI_IO_DEVICE
*Bridge
,
928 IN UINT8 StartBusNumber
,
929 IN UINT8 NumberOfBuses
,
930 OUT UINT8
*NextBusNumber
933 PCI_IO_DEVICE
*RootBridge
;
934 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*BusNumberRanges
;
936 UINT64 MaxNumberInRange
;
939 // Get PCI Root Bridge device
942 while (RootBridge
->Parent
!= NULL
) {
943 RootBridge
= RootBridge
->Parent
;
947 // Get next available PCI bus number
949 BusNumberRanges
= RootBridge
->BusNumberRanges
;
950 while (BusNumberRanges
->Desc
!= ACPI_END_TAG_DESCRIPTOR
) {
951 MaxNumberInRange
= BusNumberRanges
->AddrRangeMin
+ BusNumberRanges
->AddrLen
- 1;
952 if (StartBusNumber
>= BusNumberRanges
->AddrRangeMin
&& StartBusNumber
<= MaxNumberInRange
) {
953 NextNumber
= (UINT8
)(StartBusNumber
+ NumberOfBuses
);
954 while (NextNumber
> MaxNumberInRange
) {
956 if (BusNumberRanges
->Desc
== ACPI_END_TAG_DESCRIPTOR
) {
957 return EFI_OUT_OF_RESOURCES
;
959 NextNumber
= (UINT8
)(NextNumber
+ (BusNumberRanges
->AddrRangeMin
- (MaxNumberInRange
+ 1)));
960 MaxNumberInRange
= BusNumberRanges
->AddrRangeMin
+ BusNumberRanges
->AddrLen
- 1;
962 *NextBusNumber
= NextNumber
;
967 return EFI_OUT_OF_RESOURCES
;
971 Scan pci bus and assign bus number to the given PCI bus system.
973 @param Bridge Bridge device instance.
974 @param StartBusNumber start point.
975 @param SubBusNumber Point to sub bus number.
976 @param PaddedBusRange Customized bus number.
978 @retval EFI_SUCCESS Successfully scanned and assigned bus number.
979 @retval other Some error occurred when scanning pci bus.
981 @note Feature flag PcdPciBusHotplugDeviceSupport determine whether need support hotplug.
986 IN PCI_IO_DEVICE
*Bridge
,
987 IN UINT8 StartBusNumber
,
988 OUT UINT8
*SubBusNumber
,
989 OUT UINT8
*PaddedBusRange
1001 PCI_IO_DEVICE
*PciDevice
;
1003 EFI_HPC_STATE State
;
1005 EFI_HPC_PADDING_ATTRIBUTES Attributes
;
1006 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
1007 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*NextDescriptors
;
1009 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
1011 UINT32 TempReservedBusNum
;
1013 PciRootBridgeIo
= Bridge
->PciRootBridgeIo
;
1017 Attributes
= (EFI_HPC_PADDING_ATTRIBUTES
) 0;
1023 for (Device
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
1024 TempReservedBusNum
= 0;
1025 for (Func
= 0; Func
<= PCI_MAX_FUNC
; Func
++) {
1028 // Check to see whether a pci device is present
1030 Status
= PciDevicePresent (
1038 if (EFI_ERROR (Status
) && Func
== 0) {
1040 // go to next device if there is no Function 0
1045 if (EFI_ERROR (Status
)) {
1050 // Get the PCI device information
1052 Status
= PciSearchDevice (
1061 ASSERT (!EFI_ERROR (Status
));
1063 PciAddress
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0);
1065 if (!IS_PCI_BRIDGE (&Pci
)) {
1067 // PCI bridges will be called later
1068 // Here just need for PCI device or PCI to cardbus controller
1069 // EfiPciBeforeChildBusEnumeration for PCI Device Node
1071 PreprocessController (
1073 PciDevice
->BusNumber
,
1074 PciDevice
->DeviceNumber
,
1075 PciDevice
->FunctionNumber
,
1076 EfiPciBeforeChildBusEnumeration
1080 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1082 // For Pci Hotplug controller devcie only
1084 if (gPciHotPlugInit
!= NULL
) {
1086 // Check if it is a Hotplug PCI controller
1088 if (IsRootPciHotPlugController (PciDevice
->DevicePath
, &HpIndex
)) {
1089 gPciRootHpcData
[HpIndex
].Found
= TRUE
;
1091 if (!gPciRootHpcData
[HpIndex
].Initialized
) {
1093 Status
= CreateEventForHpc (HpIndex
, &Event
);
1095 ASSERT (!EFI_ERROR (Status
));
1097 Status
= gPciHotPlugInit
->InitializeRootHpc (
1099 gPciRootHpcPool
[HpIndex
].HpcDevicePath
,
1105 PreprocessController (
1107 PciDevice
->BusNumber
,
1108 PciDevice
->DeviceNumber
,
1109 PciDevice
->FunctionNumber
,
1110 EfiPciBeforeChildBusEnumeration
1117 if (IS_PCI_BRIDGE (&Pci
) || IS_CARDBUS_BRIDGE (&Pci
)) {
1121 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1123 // If Hot Plug is not supported,
1124 // get the bridge information
1126 Status
= PciSearchDevice (
1135 if (EFI_ERROR (Status
)) {
1140 // If Hot Plug is supported,
1141 // Get the bridge information
1144 if (gPciHotPlugInit
!= NULL
) {
1146 if (IsPciHotPlugBus (PciDevice
)) {
1149 // If it is initialized, get the padded bus range
1151 Status
= gPciHotPlugInit
->GetResourcePadding (
1153 PciDevice
->DevicePath
,
1156 (VOID
**) &Descriptors
,
1160 if (EFI_ERROR (Status
)) {
1165 NextDescriptors
= Descriptors
;
1166 Status
= PciGetBusRange (
1173 FreePool (Descriptors
);
1175 if (!EFI_ERROR (Status
)) {
1177 } else if (Status
!= EFI_NOT_FOUND
) {
1179 // EFI_NOT_FOUND is not a real error. It indicates no bus number padding requested.
1187 Status
= PciAllocateBusNumber (Bridge
, *SubBusNumber
, 1, SubBusNumber
);
1188 if (EFI_ERROR (Status
)) {
1191 SecondBus
= *SubBusNumber
;
1193 Register
= (UINT16
) ((SecondBus
<< 8) | (UINT16
) StartBusNumber
);
1194 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, PCI_BRIDGE_PRIMARY_BUS_REGISTER_OFFSET
);
1196 Status
= PciRootBridgeIo
->Pci
.Write (
1206 // If it is PPB, resursively search down this bridge
1208 if (IS_PCI_BRIDGE (&Pci
)) {
1211 // Temporarily initialize SubBusNumber to maximum bus number to ensure the
1212 // PCI configuration transaction to go through any PPB
1214 Register
= PciGetMaxBusNumber (Bridge
);
1215 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, PCI_BRIDGE_SUBORDINATE_BUS_REGISTER_OFFSET
);
1216 Status
= PciRootBridgeIo
->Pci
.Write (
1225 // Nofify EfiPciBeforeChildBusEnumeration for PCI Brige
1227 PreprocessController (
1229 PciDevice
->BusNumber
,
1230 PciDevice
->DeviceNumber
,
1231 PciDevice
->FunctionNumber
,
1232 EfiPciBeforeChildBusEnumeration
1235 Status
= PciScanBus (
1241 if (EFI_ERROR (Status
)) {
1246 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
) && BusPadding
) {
1248 // Ensure the device is enabled and initialized
1250 if ((Attributes
== EfiPaddingPciRootBridge
) &&
1251 (State
& EFI_HPC_STATE_ENABLED
) != 0 &&
1252 (State
& EFI_HPC_STATE_INITIALIZED
) != 0) {
1253 *PaddedBusRange
= (UINT8
) ((UINT8
) (BusRange
) + *PaddedBusRange
);
1256 // Reserve the larger one between the actual occupied bus number and padded bus number
1258 Status
= PciAllocateBusNumber (PciDevice
, SecondBus
, (UINT8
) (BusRange
), &PaddedSubBus
);
1259 if (EFI_ERROR (Status
)) {
1262 *SubBusNumber
= MAX (PaddedSubBus
, *SubBusNumber
);
1267 // Set the current maximum bus number under the PPB
1269 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, PCI_BRIDGE_SUBORDINATE_BUS_REGISTER_OFFSET
);
1271 Status
= PciRootBridgeIo
->Pci
.Write (
1280 // It is device. Check PCI IOV for Bus reservation
1281 // Go through each function, just reserve the MAX ReservedBusNum for one device
1283 if (PcdGetBool (PcdSrIovSupport
) && PciDevice
->SrIovCapabilityOffset
!= 0) {
1284 if (TempReservedBusNum
< PciDevice
->ReservedBusNum
) {
1286 Status
= PciAllocateBusNumber (PciDevice
, *SubBusNumber
, (UINT8
) (PciDevice
->ReservedBusNum
- TempReservedBusNum
), SubBusNumber
);
1287 if (EFI_ERROR (Status
)) {
1290 TempReservedBusNum
= PciDevice
->ReservedBusNum
;
1293 DEBUG ((EFI_D_INFO
, "PCI-IOV ScanBus - SubBusNumber - 0x%x\n", *SubBusNumber
));
1295 DEBUG ((EFI_D_INFO
, "PCI-IOV ScanBus - SubBusNumber - 0x%x (Update)\n", *SubBusNumber
));
1301 if (Func
== 0 && !IS_PCI_MULTI_FUNC (&Pci
)) {
1304 // Skip sub functions, this is not a multi function device
1307 Func
= PCI_MAX_FUNC
;
1316 Process Option Rom on the specified root bridge.
1318 @param Bridge Pci root bridge device instance.
1320 @retval EFI_SUCCESS Success process.
1321 @retval other Some error occurred when processing Option Rom on the root bridge.
1325 PciRootBridgeP2CProcess (
1326 IN PCI_IO_DEVICE
*Bridge
1329 LIST_ENTRY
*CurrentLink
;
1330 PCI_IO_DEVICE
*Temp
;
1331 EFI_HPC_STATE State
;
1335 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
1337 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
1339 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
1341 if (IS_CARDBUS_BRIDGE (&Temp
->Pci
)) {
1343 if (gPciHotPlugInit
!= NULL
&& Temp
->Allocated
&& FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1346 // Raise the EFI_IOB_PCI_HPC_INIT status code
1348 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1350 EFI_IO_BUS_PCI
| EFI_IOB_PCI_HPC_INIT
,
1354 PciAddress
= EFI_PCI_ADDRESS (Temp
->BusNumber
, Temp
->DeviceNumber
, Temp
->FunctionNumber
, 0);
1355 Status
= gPciHotPlugInit
->InitializeRootHpc (
1363 if (!EFI_ERROR (Status
)) {
1364 Status
= PciBridgeEnumerator (Temp
);
1366 if (EFI_ERROR (Status
)) {
1371 CurrentLink
= CurrentLink
->ForwardLink
;
1377 if (!IsListEmpty (&Temp
->ChildList
)) {
1378 Status
= PciRootBridgeP2CProcess (Temp
);
1381 CurrentLink
= CurrentLink
->ForwardLink
;
1388 Process Option Rom on the specified host bridge.
1390 @param PciResAlloc Pointer to instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
1392 @retval EFI_SUCCESS Success process.
1393 @retval EFI_NOT_FOUND Can not find the root bridge instance.
1394 @retval other Some error occurred when processing Option Rom on the host bridge.
1398 PciHostBridgeP2CProcess (
1399 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
1402 EFI_HANDLE RootBridgeHandle
;
1403 PCI_IO_DEVICE
*RootBridgeDev
;
1406 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1410 RootBridgeHandle
= NULL
;
1412 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1415 // Get RootBridg Device by handle
1417 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
1419 if (RootBridgeDev
== NULL
) {
1420 return EFI_NOT_FOUND
;
1423 Status
= PciRootBridgeP2CProcess (RootBridgeDev
);
1424 if (EFI_ERROR (Status
)) {
1434 This function is used to enumerate the entire host bridge
1435 in a given platform.
1437 @param PciResAlloc A pointer to the PCI Host Resource Allocation protocol.
1439 @retval EFI_SUCCESS Successfully enumerated the host bridge.
1440 @retval EFI_OUT_OF_RESOURCES No enough memory available.
1441 @retval other Some error occurred when enumerating the host bridge.
1445 PciHostBridgeEnumerator (
1446 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
1449 EFI_HANDLE RootBridgeHandle
;
1450 PCI_IO_DEVICE
*RootBridgeDev
;
1452 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
1454 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
1455 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration
;
1456 UINT8 StartBusNumber
;
1457 LIST_ENTRY RootBridgeList
;
1460 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1461 InitializeHotPlugSupport ();
1464 InitializeListHead (&RootBridgeList
);
1467 // Notify the bus allocation phase is about to start
1469 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginBusAllocation
);
1471 if (EFI_ERROR (Status
)) {
1475 DEBUG((EFI_D_INFO
, "PCI Bus First Scanning\n"));
1476 RootBridgeHandle
= NULL
;
1477 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1480 // if a root bridge instance is found, create root bridge device for it
1483 RootBridgeDev
= CreateRootBridge (RootBridgeHandle
);
1485 if (RootBridgeDev
== NULL
) {
1486 return EFI_OUT_OF_RESOURCES
;
1490 // Enumerate all the buses under this root bridge
1492 Status
= PciRootBridgeEnumerator (
1497 if (gPciHotPlugInit
!= NULL
&& FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1498 InsertTailList (&RootBridgeList
, &(RootBridgeDev
->Link
));
1500 DestroyRootBridge (RootBridgeDev
);
1502 if (EFI_ERROR (Status
)) {
1508 // Notify the bus allocation phase is finished for the first time
1510 NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndBusAllocation
);
1512 if (gPciHotPlugInit
!= NULL
&& FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1514 // Reset all assigned PCI bus number in all PPB
1516 RootBridgeHandle
= NULL
;
1517 Link
= GetFirstNode (&RootBridgeList
);
1518 while ((PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) &&
1519 (!IsNull (&RootBridgeList
, Link
))) {
1520 RootBridgeDev
= PCI_IO_DEVICE_FROM_LINK (Link
);
1522 // Get the Bus information
1524 Status
= PciResAlloc
->StartBusEnumeration (
1527 (VOID
**) &Configuration
1529 if (EFI_ERROR (Status
)) {
1534 // Get the bus number to start with
1536 StartBusNumber
= (UINT8
) (Configuration
->AddrRangeMin
);
1538 ResetAllPpbBusNumber (
1543 FreePool (Configuration
);
1544 Link
= RemoveEntryList (Link
);
1545 DestroyRootBridge (RootBridgeDev
);
1549 // Wait for all HPC initialized
1551 Status
= AllRootHPCInitialized (STALL_1_SECOND
* 15);
1553 if (EFI_ERROR (Status
)) {
1554 DEBUG ((EFI_D_ERROR
, "Some root HPC failed to initialize\n"));
1559 // Notify the bus allocation phase is about to start for the 2nd time
1561 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginBusAllocation
);
1563 if (EFI_ERROR (Status
)) {
1567 DEBUG((EFI_D_INFO
, "PCI Bus Second Scanning\n"));
1568 RootBridgeHandle
= NULL
;
1569 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1572 // if a root bridge instance is found, create root bridge device for it
1574 RootBridgeDev
= CreateRootBridge (RootBridgeHandle
);
1576 if (RootBridgeDev
== NULL
) {
1577 return EFI_OUT_OF_RESOURCES
;
1581 // Enumerate all the buses under this root bridge
1583 Status
= PciRootBridgeEnumerator (
1588 DestroyRootBridge (RootBridgeDev
);
1589 if (EFI_ERROR (Status
)) {
1595 // Notify the bus allocation phase is to end for the 2nd time
1597 NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndBusAllocation
);
1601 // Notify the resource allocation phase is to start
1603 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginResourceAllocation
);
1605 if (EFI_ERROR (Status
)) {
1609 RootBridgeHandle
= NULL
;
1610 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1613 // if a root bridge instance is found, create root bridge device for it
1615 RootBridgeDev
= CreateRootBridge (RootBridgeHandle
);
1617 if (RootBridgeDev
== NULL
) {
1618 return EFI_OUT_OF_RESOURCES
;
1621 Status
= StartManagingRootBridge (RootBridgeDev
);
1623 if (EFI_ERROR (Status
)) {
1627 PciRootBridgeIo
= RootBridgeDev
->PciRootBridgeIo
;
1628 Status
= PciRootBridgeIo
->Configuration (PciRootBridgeIo
, (VOID
**) &Descriptors
);
1630 if (EFI_ERROR (Status
)) {
1634 Status
= PciGetBusRange (&Descriptors
, &MinBus
, NULL
, NULL
);
1636 if (EFI_ERROR (Status
)) {
1641 // Determine root bridge attribute by calling interface of Pcihostbridge
1644 DetermineRootBridgeAttributes (
1650 // Collect all the resource information under this root bridge
1651 // A database that records all the information about pci device subject to this
1652 // root bridge will then be created
1654 Status
= PciPciDeviceInfoCollector (
1659 if (EFI_ERROR (Status
)) {
1663 InsertRootBridge (RootBridgeDev
);
1666 // Record the hostbridge handle
1668 AddHostBridgeEnumerator (RootBridgeDev
->PciRootBridgeIo
->ParentHandle
);