2 Internal library implementation for PCI Bus module.
4 Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 GLOBAL_REMOVE_IF_UNREFERENCED
18 CHAR16
*mBarTypeStr
[] = {
32 Retrieve the PCI Card device BAR information via PciIo interface.
34 @param PciIoDevice PCI Card device instance.
39 IN PCI_IO_DEVICE
*PciIoDevice
44 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
49 // Read PciBar information from the bar register
51 if (!gFullEnumeration
) {
53 PciIoDevice
->PciIo
.Pci
.Read (
54 &(PciIoDevice
->PciIo
),
56 PCI_CARD_MEMORY_BASE_0
,
61 (PciIoDevice
->PciBar
)[P2C_MEM_1
].BaseAddress
= (UINT64
) (Address
);
62 (PciIoDevice
->PciBar
)[P2C_MEM_1
].Length
= 0x2000000;
63 (PciIoDevice
->PciBar
)[P2C_MEM_1
].BarType
= PciBarTypeMem32
;
66 PciIoDevice
->PciIo
.Pci
.Read (
67 &(PciIoDevice
->PciIo
),
69 PCI_CARD_MEMORY_BASE_1
,
73 (PciIoDevice
->PciBar
)[P2C_MEM_2
].BaseAddress
= (UINT64
) (Address
);
74 (PciIoDevice
->PciBar
)[P2C_MEM_2
].Length
= 0x2000000;
75 (PciIoDevice
->PciBar
)[P2C_MEM_2
].BarType
= PciBarTypePMem32
;
78 PciIoDevice
->PciIo
.Pci
.Read (
79 &(PciIoDevice
->PciIo
),
81 PCI_CARD_IO_BASE_0_LOWER
,
85 (PciIoDevice
->PciBar
)[P2C_IO_1
].BaseAddress
= (UINT64
) (Address
);
86 (PciIoDevice
->PciBar
)[P2C_IO_1
].Length
= 0x100;
87 (PciIoDevice
->PciBar
)[P2C_IO_1
].BarType
= PciBarTypeIo16
;
90 PciIoDevice
->PciIo
.Pci
.Read (
91 &(PciIoDevice
->PciIo
),
93 PCI_CARD_IO_BASE_1_LOWER
,
97 (PciIoDevice
->PciBar
)[P2C_IO_2
].BaseAddress
= (UINT64
) (Address
);
98 (PciIoDevice
->PciBar
)[P2C_IO_2
].Length
= 0x100;
99 (PciIoDevice
->PciBar
)[P2C_IO_2
].BarType
= PciBarTypeIo16
;
103 if (gPciHotPlugInit
!= NULL
&& FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
104 GetResourcePaddingForHpb (PciIoDevice
);
109 Remove rejected pci device from specific root bridge
112 @param RootBridgeHandle Specific parent root bridge handle.
113 @param Bridge Bridge device instance.
117 RemoveRejectedPciDevices (
118 IN EFI_HANDLE RootBridgeHandle
,
119 IN PCI_IO_DEVICE
*Bridge
123 LIST_ENTRY
*CurrentLink
;
124 LIST_ENTRY
*LastLink
;
126 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
130 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
132 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
134 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
136 if (IS_PCI_BRIDGE (&Temp
->Pci
)) {
138 // Remove rejected devices recusively
140 RemoveRejectedPciDevices (RootBridgeHandle
, Temp
);
143 // Skip rejection for all PPBs, while detect rejection for others
145 if (IsPciDeviceRejected (Temp
)) {
148 // For P2C, remove all devices on it
150 if (!IsListEmpty (&Temp
->ChildList
)) {
151 RemoveAllPciDeviceOnBridge (RootBridgeHandle
, Temp
);
155 // Finally remove itself
157 LastLink
= CurrentLink
->BackLink
;
158 RemoveEntryList (CurrentLink
);
159 FreePciDevice (Temp
);
161 CurrentLink
= LastLink
;
165 CurrentLink
= CurrentLink
->ForwardLink
;
170 Dump the resourc map of the bridge device.
172 @param[in] BridgeResource Resource descriptor of the bridge device.
176 IN PCI_RESOURCE_NODE
*BridgeResource
180 PCI_RESOURCE_NODE
*Resource
;
183 if ((BridgeResource
!= NULL
) && (BridgeResource
->Length
!= 0)) {
185 EFI_D_INFO
, "Type = %s; Base = 0x%x;\tLength = 0x%x;\tAlignment = 0x%x\n",
186 mBarTypeStr
[MIN (BridgeResource
->ResType
, PciBarTypeMaxType
)],
187 BridgeResource
->PciDev
->PciBar
[BridgeResource
->Bar
].BaseAddress
,
188 BridgeResource
->Length
, BridgeResource
->Alignment
190 for ( Link
= BridgeResource
->ChildList
.ForwardLink
191 ; Link
!= &BridgeResource
->ChildList
192 ; Link
= Link
->ForwardLink
194 Resource
= RESOURCE_NODE_FROM_LINK (Link
);
195 if (Resource
->ResourceUsage
== PciResUsageTypical
) {
196 Bar
= Resource
->Virtual
? Resource
->PciDev
->VfPciBar
: Resource
->PciDev
->PciBar
;
198 EFI_D_INFO
, " Base = 0x%x;\tLength = 0x%x;\tAlignment = 0x%x;\tOwner = %s ",
199 Bar
[Resource
->Bar
].BaseAddress
, Resource
->Length
, Resource
->Alignment
,
200 IS_PCI_BRIDGE (&Resource
->PciDev
->Pci
) ? L
"PPB" :
201 IS_CARDBUS_BRIDGE (&Resource
->PciDev
->Pci
) ? L
"P2C" :
205 if ((!IS_PCI_BRIDGE (&Resource
->PciDev
->Pci
) && !IS_CARDBUS_BRIDGE (&Resource
->PciDev
->Pci
)) ||
206 (IS_PCI_BRIDGE (&Resource
->PciDev
->Pci
) && (Resource
->Bar
< PPB_IO_RANGE
)) ||
207 (IS_CARDBUS_BRIDGE (&Resource
->PciDev
->Pci
) && (Resource
->Bar
< P2C_MEM_1
))
210 // The resource requirement comes from the device itself.
213 EFI_D_INFO
, " [%02x|%02x|%02x:%02x]\n",
214 Resource
->PciDev
->BusNumber
, Resource
->PciDev
->DeviceNumber
,
215 Resource
->PciDev
->FunctionNumber
, Bar
[Resource
->Bar
].Offset
219 // The resource requirement comes from the subordinate devices.
222 EFI_D_INFO
, " [%02x|%02x|%02x:**]\n",
223 Resource
->PciDev
->BusNumber
, Resource
->PciDev
->DeviceNumber
,
224 Resource
->PciDev
->FunctionNumber
228 DEBUG ((EFI_D_INFO
, " Padding:Length = 0x%x;\tAlignment = 0x%x\n", Resource
->Length
, Resource
->Alignment
));
235 Find the corresponding resource node for the Device in child list of BridgeResource.
237 @param[in] Device Pointer to PCI_IO_DEVICE.
238 @param[in] BridgeResource Pointer to PCI_RESOURCE_NODE.
240 @return !NULL The corresponding resource node for the Device.
241 @return NULL No corresponding resource node for the Device.
245 IN PCI_IO_DEVICE
*Device
,
246 IN PCI_RESOURCE_NODE
*BridgeResource
250 PCI_RESOURCE_NODE
*Resource
;
252 for ( Link
= BridgeResource
->ChildList
.ForwardLink
253 ; Link
!= &BridgeResource
->ChildList
254 ; Link
= Link
->ForwardLink
256 Resource
= RESOURCE_NODE_FROM_LINK (Link
);
257 if (Resource
->PciDev
== Device
) {
266 Dump the resource map of all the devices under Bridge.
268 @param[in] Bridge Bridge device instance.
269 @param[in] IoNode IO resource descriptor for the bridge device.
270 @param[in] Mem32Node Mem32 resource descriptor for the bridge device.
271 @param[in] PMem32Node PMem32 resource descriptor for the bridge device.
272 @param[in] Mem64Node Mem64 resource descriptor for the bridge device.
273 @param[in] PMem64Node PMem64 resource descriptor for the bridge device.
277 IN PCI_IO_DEVICE
*Bridge
,
278 IN PCI_RESOURCE_NODE
*IoNode
,
279 IN PCI_RESOURCE_NODE
*Mem32Node
,
280 IN PCI_RESOURCE_NODE
*PMem32Node
,
281 IN PCI_RESOURCE_NODE
*Mem64Node
,
282 IN PCI_RESOURCE_NODE
*PMem64Node
287 PCI_IO_DEVICE
*Device
;
288 PCI_RESOURCE_NODE
*ChildIoNode
;
289 PCI_RESOURCE_NODE
*ChildMem32Node
;
290 PCI_RESOURCE_NODE
*ChildPMem32Node
;
291 PCI_RESOURCE_NODE
*ChildMem64Node
;
292 PCI_RESOURCE_NODE
*ChildPMem64Node
;
293 EFI_DEVICE_PATH_TO_TEXT_PROTOCOL
*ToText
;
296 DEBUG ((EFI_D_INFO
, "PciBus: Resource Map for "));
298 Status
= gBS
->OpenProtocol (
300 &gEfiPciRootBridgeIoProtocolGuid
,
304 EFI_OPEN_PROTOCOL_TEST_PROTOCOL
306 if (EFI_ERROR (Status
)) {
308 EFI_D_INFO
, "Bridge [%02x|%02x|%02x]\n",
309 Bridge
->BusNumber
, Bridge
->DeviceNumber
, Bridge
->FunctionNumber
312 Status
= gBS
->LocateProtocol (
313 &gEfiDevicePathToTextProtocolGuid
,
318 if (!EFI_ERROR (Status
)) {
319 Str
= ToText
->ConvertDevicePathToText (
320 DevicePathFromHandle (Bridge
->Handle
),
325 DEBUG ((EFI_D_INFO
, "Root Bridge %s\n", Str
!= NULL
? Str
: L
""));
331 DumpBridgeResource (IoNode
);
332 DumpBridgeResource (Mem32Node
);
333 DumpBridgeResource (PMem32Node
);
334 DumpBridgeResource (Mem64Node
);
335 DumpBridgeResource (PMem64Node
);
336 DEBUG ((EFI_D_INFO
, "\n"));
338 for ( Link
= Bridge
->ChildList
.ForwardLink
339 ; Link
!= &Bridge
->ChildList
340 ; Link
= Link
->ForwardLink
342 Device
= PCI_IO_DEVICE_FROM_LINK (Link
);
343 if (IS_PCI_BRIDGE (&Device
->Pci
)) {
345 ChildIoNode
= (IoNode
== NULL
? NULL
: FindResourceNode (Device
, IoNode
));
346 ChildMem32Node
= (Mem32Node
== NULL
? NULL
: FindResourceNode (Device
, Mem32Node
));
347 ChildPMem32Node
= (PMem32Node
== NULL
? NULL
: FindResourceNode (Device
, PMem32Node
));
348 ChildMem64Node
= (Mem64Node
== NULL
? NULL
: FindResourceNode (Device
, Mem64Node
));
349 ChildPMem64Node
= (PMem64Node
== NULL
? NULL
: FindResourceNode (Device
, PMem64Node
));
364 Submits the I/O and memory resource requirements for the specified PCI Host Bridge.
366 @param PciResAlloc Point to protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
368 @retval EFI_SUCCESS Successfully finished resource allocation.
369 @retval EFI_NOT_FOUND Cannot get root bridge instance.
370 @retval EFI_OUT_OF_RESOURCES Platform failed to program the resources if no hot plug supported.
371 @retval other Some error occurred when allocating resources for the PCI Host Bridge.
373 @note Feature flag PcdPciBusHotplugDeviceSupport determine whether need support hotplug.
377 PciHostBridgeResourceAllocator (
378 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
381 PCI_IO_DEVICE
*RootBridgeDev
;
382 EFI_HANDLE RootBridgeHandle
;
391 UINT64 Mem32ResStatus
;
392 UINT64 PMem32ResStatus
;
393 UINT64 Mem64ResStatus
;
394 UINT64 PMem64ResStatus
;
395 UINT64 MaxOptionRomSize
;
396 PCI_RESOURCE_NODE
*IoBridge
;
397 PCI_RESOURCE_NODE
*Mem32Bridge
;
398 PCI_RESOURCE_NODE
*PMem32Bridge
;
399 PCI_RESOURCE_NODE
*Mem64Bridge
;
400 PCI_RESOURCE_NODE
*PMem64Bridge
;
401 PCI_RESOURCE_NODE IoPool
;
402 PCI_RESOURCE_NODE Mem32Pool
;
403 PCI_RESOURCE_NODE PMem32Pool
;
404 PCI_RESOURCE_NODE Mem64Pool
;
405 PCI_RESOURCE_NODE PMem64Pool
;
407 EFI_DEVICE_HANDLE_EXTENDED_DATA_PAYLOAD HandleExtendedData
;
408 EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA_PAYLOAD AllocFailExtendedData
;
416 // It may try several times if the resource allocation fails
420 // Initialize resource pool
422 InitializeResourcePool (&IoPool
, PciBarTypeIo16
);
423 InitializeResourcePool (&Mem32Pool
, PciBarTypeMem32
);
424 InitializeResourcePool (&PMem32Pool
, PciBarTypePMem32
);
425 InitializeResourcePool (&Mem64Pool
, PciBarTypeMem64
);
426 InitializeResourcePool (&PMem64Pool
, PciBarTypePMem64
);
428 RootBridgeDev
= NULL
;
429 RootBridgeHandle
= 0;
431 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
433 // Get Root Bridge Device by handle
435 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
437 if (RootBridgeDev
== NULL
) {
438 return EFI_NOT_FOUND
;
442 // Create the entire system resource map from the information collected by
443 // enumerator. Several resource tree was created
447 // If non-stardard PCI Bridge I/O window alignment is supported,
448 // set I/O aligment to minimum possible alignment for root bridge.
450 IoBridge
= CreateResourceNode (
453 FeaturePcdGet (PcdPciBridgeIoAlignmentProbe
) ? 0x1FF: 0xFFF,
459 Mem32Bridge
= CreateResourceNode (
468 PMem32Bridge
= CreateResourceNode (
477 Mem64Bridge
= CreateResourceNode (
486 PMem64Bridge
= CreateResourceNode (
496 // Create resourcemap by going through all the devices subject to this root bridge
508 // Get the max ROM size that the root bridge can process
510 RootBridgeDev
->RomSize
= Mem32Bridge
->Length
;
513 // Skip to enlarge the resource request during realloction
517 // Get Max Option Rom size for current root bridge
519 MaxOptionRomSize
= GetMaxOptionRomSize (RootBridgeDev
);
522 // Enlarger the mem32 resource to accomdate the option rom
523 // if the mem32 resource is not enough to hold the rom
525 if (MaxOptionRomSize
> Mem32Bridge
->Length
) {
527 Mem32Bridge
->Length
= MaxOptionRomSize
;
528 RootBridgeDev
->RomSize
= MaxOptionRomSize
;
531 // Alignment should be adjusted as well
533 if (Mem32Bridge
->Alignment
< MaxOptionRomSize
- 1) {
534 Mem32Bridge
->Alignment
= MaxOptionRomSize
- 1;
540 // Based on the all the resource tree, contruct ACPI resource node to
541 // submit the resource aperture to pci host bridge protocol
543 Status
= ConstructAcpiResourceRequestor (
554 // Insert these resource nodes into the database
556 InsertResourceNode (&IoPool
, IoBridge
);
557 InsertResourceNode (&Mem32Pool
, Mem32Bridge
);
558 InsertResourceNode (&PMem32Pool
, PMem32Bridge
);
559 InsertResourceNode (&Mem64Pool
, Mem64Bridge
);
560 InsertResourceNode (&PMem64Pool
, PMem64Bridge
);
562 if (Status
== EFI_SUCCESS
) {
564 // Submit the resource requirement
566 Status
= PciResAlloc
->SubmitResources (
568 RootBridgeDev
->Handle
,
574 // Free acpi resource node
576 if (AcpiConfig
!= NULL
) {
577 FreePool (AcpiConfig
);
580 if (EFI_ERROR (Status
)) {
582 // Destroy all the resource tree
584 DestroyResourceTree (&IoPool
);
585 DestroyResourceTree (&Mem32Pool
);
586 DestroyResourceTree (&PMem32Pool
);
587 DestroyResourceTree (&Mem64Pool
);
588 DestroyResourceTree (&PMem64Pool
);
593 // End while, at least one Root Bridge should be found.
595 ASSERT (RootBridgeDev
!= NULL
);
598 // Notify platform to start to program the resource
600 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeAllocateResources
);
601 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
603 // If Hot Plug is not supported
605 if (EFI_ERROR (Status
)) {
607 // Allocation failed, then return
609 return EFI_OUT_OF_RESOURCES
;
612 // Allocation succeed.
613 // Get host bridge handle for status report, and then skip the main while
615 HandleExtendedData
.Handle
= RootBridgeDev
->PciRootBridgeIo
->ParentHandle
;
621 // If Hot Plug is supported
623 if (!EFI_ERROR (Status
)) {
625 // Allocation succeed, then continue the following
631 // If the resource allocation is unsuccessful, free resources on bridge
634 RootBridgeDev
= NULL
;
635 RootBridgeHandle
= 0;
637 IoResStatus
= EFI_RESOURCE_SATISFIED
;
638 Mem32ResStatus
= EFI_RESOURCE_SATISFIED
;
639 PMem32ResStatus
= EFI_RESOURCE_SATISFIED
;
640 Mem64ResStatus
= EFI_RESOURCE_SATISFIED
;
641 PMem64ResStatus
= EFI_RESOURCE_SATISFIED
;
643 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
645 // Get RootBridg Device by handle
647 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
648 if (RootBridgeDev
== NULL
) {
649 return EFI_NOT_FOUND
;
653 // Get host bridge handle for status report
655 HandleExtendedData
.Handle
= RootBridgeDev
->PciRootBridgeIo
->ParentHandle
;
658 // Get acpi resource node for all the resource types
662 Status
= PciResAlloc
->GetProposedResources (
664 RootBridgeDev
->Handle
,
668 if (EFI_ERROR (Status
)) {
672 if (AcpiConfig
!= NULL
) {
674 // Adjust resource allocation policy for each RB
676 GetResourceAllocationStatus (
684 FreePool (AcpiConfig
);
692 // Raise the EFI_IOB_EC_RESOURCE_CONFLICT status code
695 // It is very difficult to follow the spec here
696 // Device path , Bar index can not be get here
698 ZeroMem (&AllocFailExtendedData
, sizeof (AllocFailExtendedData
));
700 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
702 EFI_IO_BUS_PCI
| EFI_IOB_EC_RESOURCE_CONFLICT
,
703 (VOID
*) &AllocFailExtendedData
,
704 sizeof (AllocFailExtendedData
)
707 Status
= PciHostBridgeAdjustAllocation (
721 // Destroy all the resource tree
723 DestroyResourceTree (&IoPool
);
724 DestroyResourceTree (&Mem32Pool
);
725 DestroyResourceTree (&PMem32Pool
);
726 DestroyResourceTree (&Mem64Pool
);
727 DestroyResourceTree (&PMem64Pool
);
729 NotifyPhase (PciResAlloc
, EfiPciHostBridgeFreeResources
);
731 if (EFI_ERROR (Status
)) {
743 // Raise the EFI_IOB_PCI_RES_ALLOC status code
745 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
747 EFI_IO_BUS_PCI
| EFI_IOB_PCI_RES_ALLOC
,
748 (VOID
*) &HandleExtendedData
,
749 sizeof (HandleExtendedData
)
753 // Notify pci bus driver starts to program the resource
755 NotifyPhase (PciResAlloc
, EfiPciHostBridgeSetResources
);
757 RootBridgeDev
= NULL
;
759 RootBridgeHandle
= 0;
761 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
763 // Get RootBridg Device by handle
765 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
767 if (RootBridgeDev
== NULL
) {
768 return EFI_NOT_FOUND
;
772 // Get acpi resource node for all the resource types
775 Status
= PciResAlloc
->GetProposedResources (
777 RootBridgeDev
->Handle
,
781 if (EFI_ERROR (Status
)) {
786 // Get the resource base by interpreting acpi resource node
799 // Process option rom for this root bridge
801 ProcessOptionRom (RootBridgeDev
, Mem32Base
, RootBridgeDev
->RomSize
);
804 // Create the entire system resource map from the information collected by
805 // enumerator. Several resource tree was created
807 IoBridge
= FindResourceNode (RootBridgeDev
, &IoPool
);
808 Mem32Bridge
= FindResourceNode (RootBridgeDev
, &Mem32Pool
);
809 PMem32Bridge
= FindResourceNode (RootBridgeDev
, &PMem32Pool
);
810 Mem64Bridge
= FindResourceNode (RootBridgeDev
, &Mem64Pool
);
811 PMem64Bridge
= FindResourceNode (RootBridgeDev
, &PMem64Pool
);
814 // Program IO resources
822 // Program Mem32 resources
830 // Program PMem32 resources
838 // Program Mem64 resources
846 // Program PMem64 resources
853 IoBridge
->PciDev
->PciBar
[IoBridge
->Bar
].BaseAddress
= IoBase
;
854 Mem32Bridge
->PciDev
->PciBar
[Mem32Bridge
->Bar
].BaseAddress
= Mem32Base
;
855 PMem32Bridge
->PciDev
->PciBar
[PMem32Bridge
->Bar
].BaseAddress
= PMem32Base
;
856 Mem64Bridge
->PciDev
->PciBar
[Mem64Bridge
->Bar
].BaseAddress
= Mem64Base
;
857 PMem64Bridge
->PciDev
->PciBar
[PMem64Bridge
->Bar
].BaseAddress
= PMem64Base
;
860 // Dump the resource map for current root bridge
873 FreePool (AcpiConfig
);
877 // Destroy all the resource tree
879 DestroyResourceTree (&IoPool
);
880 DestroyResourceTree (&Mem32Pool
);
881 DestroyResourceTree (&PMem32Pool
);
882 DestroyResourceTree (&Mem64Pool
);
883 DestroyResourceTree (&PMem64Pool
);
886 // Notify the resource allocation phase is to end
888 NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndResourceAllocation
);
894 Scan pci bus and assign bus number to the given PCI bus system.
896 @param Bridge Bridge device instance.
897 @param StartBusNumber start point.
898 @param SubBusNumber Point to sub bus number.
899 @param PaddedBusRange Customized bus number.
901 @retval EFI_SUCCESS Successfully scanned and assigned bus number.
902 @retval other Some error occurred when scanning pci bus.
904 @note Feature flag PcdPciBusHotplugDeviceSupport determine whether need support hotplug.
909 IN PCI_IO_DEVICE
*Bridge
,
910 IN UINT8 StartBusNumber
,
911 OUT UINT8
*SubBusNumber
,
912 OUT UINT8
*PaddedBusRange
923 PCI_IO_DEVICE
*PciDevice
;
927 EFI_HPC_PADDING_ATTRIBUTES Attributes
;
928 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
930 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
932 UINT32 TempReservedBusNum
;
934 PciRootBridgeIo
= Bridge
->PciRootBridgeIo
;
938 Attributes
= (EFI_HPC_PADDING_ATTRIBUTES
) 0;
944 for (Device
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
945 TempReservedBusNum
= 0;
946 for (Func
= 0; Func
<= PCI_MAX_FUNC
; Func
++) {
949 // Check to see whether a pci device is present
951 Status
= PciDevicePresent (
959 if (EFI_ERROR (Status
)) {
964 // Get the PCI device information
966 Status
= PciSearchDevice (
975 ASSERT (!EFI_ERROR (Status
));
977 PciAddress
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0);
979 if (!IS_PCI_BRIDGE (&Pci
)) {
981 // PCI bridges will be called later
982 // Here just need for PCI device or PCI to cardbus controller
983 // EfiPciBeforeChildBusEnumeration for PCI Device Node
985 PreprocessController (
987 PciDevice
->BusNumber
,
988 PciDevice
->DeviceNumber
,
989 PciDevice
->FunctionNumber
,
990 EfiPciBeforeChildBusEnumeration
994 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
996 // For Pci Hotplug controller devcie only
998 if (gPciHotPlugInit
!= NULL
) {
1000 // Check if it is a Hotplug PCI controller
1002 if (IsRootPciHotPlugController (PciDevice
->DevicePath
, &HpIndex
)) {
1003 gPciRootHpcData
[HpIndex
].Found
= TRUE
;
1005 if (!gPciRootHpcData
[HpIndex
].Initialized
) {
1007 Status
= CreateEventForHpc (HpIndex
, &Event
);
1009 ASSERT (!EFI_ERROR (Status
));
1011 Status
= gPciHotPlugInit
->InitializeRootHpc (
1013 gPciRootHpcPool
[HpIndex
].HpcDevicePath
,
1019 PreprocessController (
1021 PciDevice
->BusNumber
,
1022 PciDevice
->DeviceNumber
,
1023 PciDevice
->FunctionNumber
,
1024 EfiPciBeforeChildBusEnumeration
1031 if (IS_PCI_BRIDGE (&Pci
) || IS_CARDBUS_BRIDGE (&Pci
)) {
1035 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1037 // If Hot Plug is not supported,
1038 // get the bridge information
1040 Status
= PciSearchDevice (
1049 if (EFI_ERROR (Status
)) {
1054 // If Hot Plug is supported,
1055 // Get the bridge information
1058 if (gPciHotPlugInit
!= NULL
) {
1060 if (IsRootPciHotPlugBus (PciDevice
->DevicePath
, &HpIndex
)) {
1063 // If it is initialized, get the padded bus range
1065 Status
= gPciHotPlugInit
->GetResourcePadding (
1067 gPciRootHpcPool
[HpIndex
].HpbDevicePath
,
1070 (VOID
**) &Descriptors
,
1074 if (EFI_ERROR (Status
)) {
1079 Status
= PciGetBusRange (
1086 FreePool (Descriptors
);
1088 if (EFI_ERROR (Status
)) {
1098 // Add feature to support customized secondary bus number
1100 if (*SubBusNumber
== 0) {
1101 *SubBusNumber
= *PaddedBusRange
;
1102 *PaddedBusRange
= 0;
1106 SecondBus
= *SubBusNumber
;
1108 Register
= (UINT16
) ((SecondBus
<< 8) | (UINT16
) StartBusNumber
);
1109 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, PCI_BRIDGE_PRIMARY_BUS_REGISTER_OFFSET
);
1111 Status
= PciRootBridgeIo
->Pci
.Write (
1121 // If it is PPB, resursively search down this bridge
1123 if (IS_PCI_BRIDGE (&Pci
)) {
1126 // Temporarily initialize SubBusNumber to maximum bus number to ensure the
1127 // PCI configuration transaction to go through any PPB
1130 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, PCI_BRIDGE_SUBORDINATE_BUS_REGISTER_OFFSET
);
1131 Status
= PciRootBridgeIo
->Pci
.Write (
1140 // Nofify EfiPciBeforeChildBusEnumeration for PCI Brige
1142 PreprocessController (
1144 PciDevice
->BusNumber
,
1145 PciDevice
->DeviceNumber
,
1146 PciDevice
->FunctionNumber
,
1147 EfiPciBeforeChildBusEnumeration
1150 Status
= PciScanBus (
1152 (UINT8
) (SecondBus
),
1156 if (EFI_ERROR (Status
)) {
1161 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
) && BusPadding
) {
1163 // Ensure the device is enabled and initialized
1165 if ((Attributes
== EfiPaddingPciRootBridge
) &&
1166 (State
& EFI_HPC_STATE_ENABLED
) != 0 &&
1167 (State
& EFI_HPC_STATE_INITIALIZED
) != 0) {
1168 *PaddedBusRange
= (UINT8
) ((UINT8
) (BusRange
) +*PaddedBusRange
);
1170 *SubBusNumber
= (UINT8
) ((UINT8
) (BusRange
) +*SubBusNumber
);
1175 // Set the current maximum bus number under the PPB
1177 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, PCI_BRIDGE_SUBORDINATE_BUS_REGISTER_OFFSET
);
1179 Status
= PciRootBridgeIo
->Pci
.Write (
1188 // It is device. Check PCI IOV for Bus reservation
1189 // Go through each function, just reserve the MAX ReservedBusNum for one device
1191 if (PcdGetBool (PcdSrIovSupport
) && PciDevice
->SrIovCapabilityOffset
!= 0) {
1192 if (TempReservedBusNum
< PciDevice
->ReservedBusNum
) {
1194 (*SubBusNumber
) = (UINT8
)((*SubBusNumber
) + PciDevice
->ReservedBusNum
- TempReservedBusNum
);
1195 TempReservedBusNum
= PciDevice
->ReservedBusNum
;
1198 DEBUG ((EFI_D_INFO
, "PCI-IOV ScanBus - SubBusNumber - 0x%x\n", *SubBusNumber
));
1200 DEBUG ((EFI_D_INFO
, "PCI-IOV ScanBus - SubBusNumber - 0x%x (Update)\n", *SubBusNumber
));
1206 if (Func
== 0 && !IS_PCI_MULTI_FUNC (&Pci
)) {
1209 // Skip sub functions, this is not a multi function device
1212 Func
= PCI_MAX_FUNC
;
1221 Process Option Rom on the specified root bridge.
1223 @param Bridge Pci root bridge device instance.
1225 @retval EFI_SUCCESS Success process.
1226 @retval other Some error occurred when processing Option Rom on the root bridge.
1230 PciRootBridgeP2CProcess (
1231 IN PCI_IO_DEVICE
*Bridge
1234 LIST_ENTRY
*CurrentLink
;
1235 PCI_IO_DEVICE
*Temp
;
1236 EFI_HPC_STATE State
;
1240 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
1242 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
1244 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
1246 if (IS_CARDBUS_BRIDGE (&Temp
->Pci
)) {
1248 if (gPciHotPlugInit
!= NULL
&& Temp
->Allocated
&& FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1251 // Raise the EFI_IOB_PCI_HPC_INIT status code
1253 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
1255 EFI_IO_BUS_PCI
| EFI_IOB_PCI_HPC_INIT
,
1259 PciAddress
= EFI_PCI_ADDRESS (Temp
->BusNumber
, Temp
->DeviceNumber
, Temp
->FunctionNumber
, 0);
1260 Status
= gPciHotPlugInit
->InitializeRootHpc (
1268 if (!EFI_ERROR (Status
)) {
1269 Status
= PciBridgeEnumerator (Temp
);
1271 if (EFI_ERROR (Status
)) {
1276 CurrentLink
= CurrentLink
->ForwardLink
;
1282 if (!IsListEmpty (&Temp
->ChildList
)) {
1283 Status
= PciRootBridgeP2CProcess (Temp
);
1286 CurrentLink
= CurrentLink
->ForwardLink
;
1293 Process Option Rom on the specified host bridge.
1295 @param PciResAlloc Pointer to instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
1297 @retval EFI_SUCCESS Success process.
1298 @retval EFI_NOT_FOUND Can not find the root bridge instance.
1299 @retval other Some error occurred when processing Option Rom on the host bridge.
1303 PciHostBridgeP2CProcess (
1304 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
1307 EFI_HANDLE RootBridgeHandle
;
1308 PCI_IO_DEVICE
*RootBridgeDev
;
1311 if (!FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1315 RootBridgeHandle
= NULL
;
1317 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1320 // Get RootBridg Device by handle
1322 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
1324 if (RootBridgeDev
== NULL
) {
1325 return EFI_NOT_FOUND
;
1328 Status
= PciRootBridgeP2CProcess (RootBridgeDev
);
1329 if (EFI_ERROR (Status
)) {
1339 This function is used to enumerate the entire host bridge
1340 in a given platform.
1342 @param PciResAlloc A pointer to the PCI Host Resource Allocation protocol.
1344 @retval EFI_SUCCESS Successfully enumerated the host bridge.
1345 @retval EFI_OUT_OF_RESOURCES No enough memory available.
1346 @retval other Some error occurred when enumerating the host bridge.
1350 PciHostBridgeEnumerator (
1351 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
1354 EFI_HANDLE RootBridgeHandle
;
1355 PCI_IO_DEVICE
*RootBridgeDev
;
1357 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
1359 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
1360 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration
;
1361 UINT8 StartBusNumber
;
1362 LIST_ENTRY RootBridgeList
;
1365 if (FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1366 InitializeHotPlugSupport ();
1369 InitializeListHead (&RootBridgeList
);
1372 // Notify the bus allocation phase is about to start
1374 NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginBusAllocation
);
1376 DEBUG((EFI_D_INFO
, "PCI Bus First Scanning\n"));
1377 RootBridgeHandle
= NULL
;
1378 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1381 // if a root bridge instance is found, create root bridge device for it
1384 RootBridgeDev
= CreateRootBridge (RootBridgeHandle
);
1386 if (RootBridgeDev
== NULL
) {
1387 return EFI_OUT_OF_RESOURCES
;
1391 // Enumerate all the buses under this root bridge
1393 Status
= PciRootBridgeEnumerator (
1398 if (gPciHotPlugInit
!= NULL
&& FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1399 InsertTailList (&RootBridgeList
, &(RootBridgeDev
->Link
));
1401 DestroyRootBridge (RootBridgeDev
);
1403 if (EFI_ERROR (Status
)) {
1409 // Notify the bus allocation phase is finished for the first time
1411 NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndBusAllocation
);
1413 if (gPciHotPlugInit
!= NULL
&& FeaturePcdGet (PcdPciBusHotplugDeviceSupport
)) {
1415 // Reset all assigned PCI bus number in all PPB
1417 RootBridgeHandle
= NULL
;
1418 Link
= GetFirstNode (&RootBridgeList
);
1419 while ((PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) &&
1420 (!IsNull (&RootBridgeList
, Link
))) {
1421 RootBridgeDev
= PCI_IO_DEVICE_FROM_LINK (Link
);
1423 // Get the Bus information
1425 Status
= PciResAlloc
->StartBusEnumeration (
1428 (VOID
**) &Configuration
1430 if (EFI_ERROR (Status
)) {
1435 // Get the bus number to start with
1437 StartBusNumber
= (UINT8
) (Configuration
->AddrRangeMin
);
1439 ResetAllPpbBusNumber (
1444 FreePool (Configuration
);
1445 Link
= RemoveEntryList (Link
);
1446 DestroyRootBridge (RootBridgeDev
);
1450 // Wait for all HPC initialized
1452 Status
= AllRootHPCInitialized (STALL_1_SECOND
* 15);
1454 if (EFI_ERROR (Status
)) {
1455 DEBUG ((EFI_D_ERROR
, "Some root HPC failed to initialize\n"));
1460 // Notify the bus allocation phase is about to start for the 2nd time
1462 NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginBusAllocation
);
1464 DEBUG((EFI_D_INFO
, "PCI Bus Second Scanning\n"));
1465 RootBridgeHandle
= NULL
;
1466 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1469 // if a root bridge instance is found, create root bridge device for it
1471 RootBridgeDev
= CreateRootBridge (RootBridgeHandle
);
1473 if (RootBridgeDev
== NULL
) {
1474 return EFI_OUT_OF_RESOURCES
;
1478 // Enumerate all the buses under this root bridge
1480 Status
= PciRootBridgeEnumerator (
1485 DestroyRootBridge (RootBridgeDev
);
1486 if (EFI_ERROR (Status
)) {
1492 // Notify the bus allocation phase is to end for the 2nd time
1494 NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndBusAllocation
);
1498 // Notify the resource allocation phase is to start
1500 NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginResourceAllocation
);
1502 RootBridgeHandle
= NULL
;
1503 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
1506 // if a root bridge instance is found, create root bridge device for it
1508 RootBridgeDev
= CreateRootBridge (RootBridgeHandle
);
1510 if (RootBridgeDev
== NULL
) {
1511 return EFI_OUT_OF_RESOURCES
;
1514 Status
= StartManagingRootBridge (RootBridgeDev
);
1516 if (EFI_ERROR (Status
)) {
1520 PciRootBridgeIo
= RootBridgeDev
->PciRootBridgeIo
;
1521 Status
= PciRootBridgeIo
->Configuration (PciRootBridgeIo
, (VOID
**) &Descriptors
);
1523 if (EFI_ERROR (Status
)) {
1527 Status
= PciGetBusRange (&Descriptors
, &MinBus
, NULL
, NULL
);
1529 if (EFI_ERROR (Status
)) {
1534 // Determine root bridge attribute by calling interface of Pcihostbridge
1537 DetermineRootBridgeAttributes (
1543 // Collect all the resource information under this root bridge
1544 // A database that records all the information about pci device subject to this
1545 // root bridge will then be created
1547 Status
= PciPciDeviceInfoCollector (
1552 if (EFI_ERROR (Status
)) {
1556 InsertRootBridge (RootBridgeDev
);
1559 // Record the hostbridge handle
1561 AddHostBridgeEnumerator (RootBridgeDev
->PciRootBridgeIo
->ParentHandle
);