2 PCI eunmeration implementation on entire PCI bus system for PCI Bus module.
4 Copyright (c) 2006 - 2016, 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.
19 This routine is used to enumerate entire pci bus system
22 @param Controller Parent controller handle.
24 @retval EFI_SUCCESS PCI enumeration finished successfully.
25 @retval other Some error occurred when enumerating the pci bus system.
30 IN EFI_HANDLE Controller
33 EFI_HANDLE HostBridgeHandle
;
35 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
;
36 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
39 // If PCI bus has already done the full enumeration, never do it again
41 if (!gFullEnumeration
) {
42 return PciEnumeratorLight (Controller
);
46 // Get the rootbridge Io protocol to find the host bridge handle
48 Status
= gBS
->OpenProtocol (
50 &gEfiPciRootBridgeIoProtocolGuid
,
51 (VOID
**) &PciRootBridgeIo
,
52 gPciBusDriverBinding
.DriverBindingHandle
,
54 EFI_OPEN_PROTOCOL_GET_PROTOCOL
57 if (EFI_ERROR (Status
)) {
62 // Get the host bridge handle
64 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
67 // Get the pci host bridge resource allocation protocol
69 Status
= gBS
->OpenProtocol (
71 &gEfiPciHostBridgeResourceAllocationProtocolGuid
,
72 (VOID
**) &PciResAlloc
,
73 gPciBusDriverBinding
.DriverBindingHandle
,
75 EFI_OPEN_PROTOCOL_GET_PROTOCOL
78 if (EFI_ERROR (Status
)) {
83 // Notify the pci bus enumeration is about to begin
85 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginEnumeration
);
87 if (EFI_ERROR (Status
)) {
92 // Start the bus allocation phase
94 Status
= PciHostBridgeEnumerator (PciResAlloc
);
96 if (EFI_ERROR (Status
)) {
101 // Submit the resource request
103 Status
= PciHostBridgeResourceAllocator (PciResAlloc
);
105 if (EFI_ERROR (Status
)) {
110 // Notify the pci bus enumeration is about to complete
112 Status
= NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndEnumeration
);
114 if (EFI_ERROR (Status
)) {
121 Status
= PciHostBridgeP2CProcess (PciResAlloc
);
123 if (EFI_ERROR (Status
)) {
128 // Process attributes for devices on this host bridge
130 Status
= PciHostBridgeDeviceAttribute (PciResAlloc
);
131 if (EFI_ERROR (Status
)) {
135 gFullEnumeration
= FALSE
;
137 Status
= gBS
->InstallProtocolInterface (
139 &gEfiPciEnumerationCompleteProtocolGuid
,
140 EFI_NATIVE_INTERFACE
,
143 if (EFI_ERROR (Status
)) {
151 Enumerate PCI root bridge.
153 @param PciResAlloc Pointer to protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
154 @param RootBridgeDev Instance of root bridge device.
156 @retval EFI_SUCCESS Successfully enumerated root bridge.
157 @retval other Failed to enumerate root bridge.
161 PciRootBridgeEnumerator (
162 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
,
163 IN PCI_IO_DEVICE
*RootBridgeDev
167 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration
;
168 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration1
;
169 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration2
;
170 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration3
;
172 UINT8 StartBusNumber
;
173 UINT8 PaddedBusRange
;
174 EFI_HANDLE RootBridgeHandle
;
184 // Get the root bridge handle
186 RootBridgeHandle
= RootBridgeDev
->Handle
;
188 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
190 EFI_IO_BUS_PCI
| EFI_IOB_PCI_BUS_ENUM
,
191 RootBridgeDev
->DevicePath
195 // Get the Bus information
197 Status
= PciResAlloc
->StartBusEnumeration (
200 (VOID
**) &Configuration
203 if (EFI_ERROR (Status
)) {
207 if (Configuration
== NULL
|| Configuration
->Desc
== ACPI_END_TAG_DESCRIPTOR
) {
208 return EFI_INVALID_PARAMETER
;
210 RootBridgeDev
->BusNumberRanges
= Configuration
;
213 // Sort the descriptors in ascending order
215 for (Configuration1
= Configuration
; Configuration1
->Desc
!= ACPI_END_TAG_DESCRIPTOR
; Configuration1
++) {
216 Configuration2
= Configuration1
;
217 for (Configuration3
= Configuration1
+ 1; Configuration3
->Desc
!= ACPI_END_TAG_DESCRIPTOR
; Configuration3
++) {
218 if (Configuration2
->AddrRangeMin
> Configuration3
->AddrRangeMin
) {
219 Configuration2
= Configuration3
;
223 // All other fields other than AddrRangeMin and AddrLen are ignored in a descriptor,
224 // so only need to swap these two fields.
226 if (Configuration2
!= Configuration1
) {
227 AddrRangeMin
= Configuration1
->AddrRangeMin
;
228 Configuration1
->AddrRangeMin
= Configuration2
->AddrRangeMin
;
229 Configuration2
->AddrRangeMin
= AddrRangeMin
;
231 AddrLen
= Configuration1
->AddrLen
;
232 Configuration1
->AddrLen
= Configuration2
->AddrLen
;
233 Configuration2
->AddrLen
= AddrLen
;
238 // Get the bus number to start with
240 StartBusNumber
= (UINT8
) (Configuration
->AddrRangeMin
);
243 // Initialize the subordinate bus number
245 SubBusNumber
= StartBusNumber
;
248 // Reset all assigned PCI bus number
250 ResetAllPpbBusNumber (
258 Status
= PciScanBus (
265 if (EFI_ERROR (Status
)) {
271 // Assign max bus number scanned
274 Status
= PciAllocateBusNumber (RootBridgeDev
, SubBusNumber
, PaddedBusRange
, &SubBusNumber
);
275 if (EFI_ERROR (Status
)) {
280 // Find the bus range which contains the higest bus number, then returns the number of buses
281 // that should be decoded.
283 while (Configuration
->AddrRangeMin
+ Configuration
->AddrLen
- 1 < SubBusNumber
) {
286 AddrLen
= Configuration
->AddrLen
;
287 Configuration
->AddrLen
= SubBusNumber
- Configuration
->AddrRangeMin
+ 1;
290 // Save the Desc field of the next descriptor. Mark the next descriptor as an END descriptor.
293 Desc
= Configuration
->Desc
;
294 Configuration
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
299 Status
= PciResAlloc
->SetBusNumbers (
302 RootBridgeDev
->BusNumberRanges
306 // Restore changed fields
308 Configuration
->Desc
= Desc
;
309 (Configuration
- 1)->AddrLen
= AddrLen
;
315 This routine is used to process all PCI devices' Option Rom
316 on a certain root bridge.
318 @param Bridge Given parent's root bridge.
319 @param RomBase Base address of ROM driver loaded from.
320 @param MaxLength Maximum rom size.
325 IN PCI_IO_DEVICE
*Bridge
,
330 LIST_ENTRY
*CurrentLink
;
334 // Go through bridges to reach all devices
336 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
337 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
338 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
339 if (!IsListEmpty (&Temp
->ChildList
)) {
342 // Go further to process the option rom under this bridge
344 ProcessOptionRom (Temp
, RomBase
, MaxLength
);
347 if (Temp
->RomSize
!= 0 && Temp
->RomSize
<= MaxLength
) {
350 // Load and process the option rom
352 LoadOpRomImage (Temp
, RomBase
);
355 CurrentLink
= CurrentLink
->ForwardLink
;
360 This routine is used to assign bus number to the given PCI bus system
362 @param Bridge Parent root bridge instance.
363 @param StartBusNumber Number of beginning.
364 @param SubBusNumber The number of sub bus.
366 @retval EFI_SUCCESS Successfully assigned bus number.
367 @retval EFI_DEVICE_ERROR Failed to assign bus number.
372 IN PCI_IO_DEVICE
*Bridge
,
373 IN UINT8 StartBusNumber
,
374 OUT UINT8
*SubBusNumber
385 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
387 PciRootBridgeIo
= Bridge
->PciRootBridgeIo
;
392 *SubBusNumber
= StartBusNumber
;
395 // First check to see whether the parent is ppb
397 for (Device
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
398 for (Func
= 0; Func
<= PCI_MAX_FUNC
; Func
++) {
401 // Check to see whether a pci device is present
403 Status
= PciDevicePresent (
411 if (EFI_ERROR (Status
) && Func
== 0) {
413 // go to next device if there is no Function 0
418 if (!EFI_ERROR (Status
) &&
419 (IS_PCI_BRIDGE (&Pci
) || IS_CARDBUS_BRIDGE (&Pci
))) {
422 // Reserved one bus for cardbus bridge
424 Status
= PciAllocateBusNumber (Bridge
, *SubBusNumber
, 1, SubBusNumber
);
425 if (EFI_ERROR (Status
)) {
428 SecondBus
= *SubBusNumber
;
430 Register
= (UINT16
) ((SecondBus
<< 8) | (UINT16
) StartBusNumber
);
432 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0x18);
434 Status
= PciRootBridgeIo
->Pci
.Write (
443 // Initialize SubBusNumber to SecondBus
445 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0x1A);
446 Status
= PciRootBridgeIo
->Pci
.Write (
454 // If it is PPB, resursively search down this bridge
456 if (IS_PCI_BRIDGE (&Pci
)) {
459 Status
= PciRootBridgeIo
->Pci
.Write (
467 Status
= PciAssignBusNumber (
473 if (EFI_ERROR (Status
)) {
474 return EFI_DEVICE_ERROR
;
479 // Set the current maximum bus number under the PPB
481 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0x1A);
483 Status
= PciRootBridgeIo
->Pci
.Write (
493 if (Func
== 0 && !IS_PCI_MULTI_FUNC (&Pci
)) {
496 // Skip sub functions, this is not a multi function device
507 This routine is used to determine the root bridge attribute by interfacing
508 the host bridge resource allocation protocol.
510 @param PciResAlloc Protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
511 @param RootBridgeDev Root bridge instance
513 @retval EFI_SUCCESS Successfully got root bridge's attribute.
514 @retval other Failed to get attribute.
518 DetermineRootBridgeAttributes (
519 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
,
520 IN PCI_IO_DEVICE
*RootBridgeDev
525 EFI_HANDLE RootBridgeHandle
;
528 RootBridgeHandle
= RootBridgeDev
->Handle
;
531 // Get root bridge attribute by calling into pci host bridge resource allocation protocol
533 Status
= PciResAlloc
->GetAllocAttributes (
539 if (EFI_ERROR (Status
)) {
544 // Here is the point where PCI bus driver calls HOST bridge allocation protocol
545 // Currently we hardcoded for ea815
547 if ((Attributes
& EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM
) != 0) {
548 RootBridgeDev
->Decodes
|= EFI_BRIDGE_PMEM_MEM_COMBINE_SUPPORTED
;
551 if ((Attributes
& EFI_PCI_HOST_BRIDGE_MEM64_DECODE
) != 0) {
552 RootBridgeDev
->Decodes
|= EFI_BRIDGE_MEM64_DECODE_SUPPORTED
;
553 RootBridgeDev
->Decodes
|= EFI_BRIDGE_PMEM64_DECODE_SUPPORTED
;
556 RootBridgeDev
->Decodes
|= EFI_BRIDGE_MEM32_DECODE_SUPPORTED
;
557 RootBridgeDev
->Decodes
|= EFI_BRIDGE_PMEM32_DECODE_SUPPORTED
;
558 RootBridgeDev
->Decodes
|= EFI_BRIDGE_IO16_DECODE_SUPPORTED
;
564 Get Max Option Rom size on specified bridge.
566 @param Bridge Given bridge device instance.
568 @return Max size of option rom needed.
572 GetMaxOptionRomSize (
573 IN PCI_IO_DEVICE
*Bridge
576 LIST_ENTRY
*CurrentLink
;
578 UINT64 MaxOptionRomSize
;
579 UINT64 TempOptionRomSize
;
581 MaxOptionRomSize
= 0;
584 // Go through bridges to reach all devices
586 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
587 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
588 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
589 if (!IsListEmpty (&Temp
->ChildList
)) {
592 // Get max option rom size under this bridge
594 TempOptionRomSize
= GetMaxOptionRomSize (Temp
);
597 // Compare with the option rom size of the bridge
598 // Get the larger one
600 if (Temp
->RomSize
> TempOptionRomSize
) {
601 TempOptionRomSize
= Temp
->RomSize
;
607 // For devices get the rom size directly
609 TempOptionRomSize
= Temp
->RomSize
;
613 // Get the largest rom size on this bridge
615 if (TempOptionRomSize
> MaxOptionRomSize
) {
616 MaxOptionRomSize
= TempOptionRomSize
;
619 CurrentLink
= CurrentLink
->ForwardLink
;
622 return MaxOptionRomSize
;
626 Process attributes of devices on this host bridge
628 @param PciResAlloc Protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
630 @retval EFI_SUCCESS Successfully process attribute.
631 @retval EFI_NOT_FOUND Can not find the specific root bridge device.
632 @retval other Failed to determine the root bridge device's attribute.
636 PciHostBridgeDeviceAttribute (
637 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
640 EFI_HANDLE RootBridgeHandle
;
641 PCI_IO_DEVICE
*RootBridgeDev
;
644 RootBridgeHandle
= NULL
;
646 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
649 // Get RootBridg Device by handle
651 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
653 if (RootBridgeDev
== NULL
) {
654 return EFI_NOT_FOUND
;
658 // Set the attributes for devcies behind the Root Bridge
660 Status
= DetermineDeviceAttribute (RootBridgeDev
);
661 if (EFI_ERROR (Status
)) {
671 Get resource allocation status from the ACPI resource descriptor.
673 @param AcpiConfig Point to Acpi configuration table.
674 @param IoResStatus Return the status of I/O resource.
675 @param Mem32ResStatus Return the status of 32-bit Memory resource.
676 @param PMem32ResStatus Return the status of 32-bit Prefetchable Memory resource.
677 @param Mem64ResStatus Return the status of 64-bit Memory resource.
678 @param PMem64ResStatus Return the status of 64-bit Prefetchable Memory resource.
682 GetResourceAllocationStatus (
684 OUT UINT64
*IoResStatus
,
685 OUT UINT64
*Mem32ResStatus
,
686 OUT UINT64
*PMem32ResStatus
,
687 OUT UINT64
*Mem64ResStatus
,
688 OUT UINT64
*PMem64ResStatus
693 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*ACPIAddressDesc
;
695 Temp
= (UINT8
*) AcpiConfig
;
697 while (*Temp
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
699 ACPIAddressDesc
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Temp
;
700 ResStatus
= ACPIAddressDesc
->AddrTranslationOffset
;
702 switch (ACPIAddressDesc
->ResType
) {
704 if (ACPIAddressDesc
->AddrSpaceGranularity
== 32) {
705 if (ACPIAddressDesc
->SpecificFlag
== 0x06) {
709 *PMem32ResStatus
= ResStatus
;
714 *Mem32ResStatus
= ResStatus
;
718 if (ACPIAddressDesc
->AddrSpaceGranularity
== 64) {
719 if (ACPIAddressDesc
->SpecificFlag
== 0x06) {
723 *PMem64ResStatus
= ResStatus
;
728 *Mem64ResStatus
= ResStatus
;
738 *IoResStatus
= ResStatus
;
745 Temp
+= sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
);
750 Remove a PCI device from device pool and mark its bar.
752 @param PciDevice Instance of Pci device.
754 @retval EFI_SUCCESS Successfully remove the PCI device.
755 @retval EFI_ABORTED Pci device is a root bridge or a PCI-PCI bridge.
760 IN PCI_IO_DEVICE
*PciDevice
763 PCI_IO_DEVICE
*Bridge
;
765 LIST_ENTRY
*CurrentLink
;
768 // Remove the padding resource from a bridge
770 if ( IS_PCI_BRIDGE(&PciDevice
->Pci
) &&
771 PciDevice
->ResourcePaddingDescriptors
!= NULL
) {
772 FreePool (PciDevice
->ResourcePaddingDescriptors
);
773 PciDevice
->ResourcePaddingDescriptors
= NULL
;
780 if (IS_PCI_BRIDGE (&PciDevice
->Pci
) || (PciDevice
->Parent
== NULL
)) {
784 if (IS_CARDBUS_BRIDGE (&PciDevice
->Pci
)) {
786 // Get the root bridge device
789 while (Bridge
->Parent
!= NULL
) {
790 Bridge
= Bridge
->Parent
;
793 RemoveAllPciDeviceOnBridge (Bridge
->Handle
, PciDevice
);
798 InitializeP2C (PciDevice
);
804 Bridge
= PciDevice
->Parent
;
805 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
806 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
807 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
808 if (Temp
== PciDevice
) {
809 InitializePciDevice (Temp
);
810 RemoveEntryList (CurrentLink
);
814 CurrentLink
= CurrentLink
->ForwardLink
;
821 Determine whethter a PCI device can be rejected.
823 @param PciResNode Pointer to Pci resource node instance.
825 @retval TRUE The PCI device can be rejected.
826 @retval TRUE The PCI device cannot be rejected.
831 IN PCI_RESOURCE_NODE
*PciResNode
836 Temp
= PciResNode
->PciDev
;
839 // Ensure the device is present
846 // PPB and RB should go ahead
848 if (IS_PCI_BRIDGE (&Temp
->Pci
) || (Temp
->Parent
== NULL
)) {
853 // Skip device on Bus0
855 if ((Temp
->Parent
!= NULL
) && (Temp
->BusNumber
== 0)) {
862 if (IS_PCI_VGA (&Temp
->Pci
)) {
870 Compare two resource nodes and get the larger resource consumer.
872 @param PciResNode1 resource node 1 want to be compared
873 @param PciResNode2 resource node 2 want to be compared
875 @return Larger resource node.
879 GetLargerConsumerDevice (
880 IN PCI_RESOURCE_NODE
*PciResNode1
,
881 IN PCI_RESOURCE_NODE
*PciResNode2
884 if (PciResNode2
== NULL
) {
888 if ((IS_PCI_BRIDGE(&(PciResNode2
->PciDev
->Pci
)) || (PciResNode2
->PciDev
->Parent
== NULL
)) \
889 && (PciResNode2
->ResourceUsage
!= PciResUsagePadding
) )
894 if (PciResNode1
== NULL
) {
898 if ((PciResNode1
->Length
) > (PciResNode2
->Length
)) {
907 Get the max resource consumer in the host resource pool.
909 @param ResPool Pointer to resource pool node.
911 @return The max resource consumer in the host resource pool.
915 GetMaxResourceConsumerDevice (
916 IN PCI_RESOURCE_NODE
*ResPool
919 PCI_RESOURCE_NODE
*Temp
;
920 LIST_ENTRY
*CurrentLink
;
921 PCI_RESOURCE_NODE
*PciResNode
;
922 PCI_RESOURCE_NODE
*PPBResNode
;
926 CurrentLink
= ResPool
->ChildList
.ForwardLink
;
927 while (CurrentLink
!= NULL
&& CurrentLink
!= &ResPool
->ChildList
) {
929 Temp
= RESOURCE_NODE_FROM_LINK (CurrentLink
);
931 if (!IsRejectiveDevice (Temp
)) {
932 CurrentLink
= CurrentLink
->ForwardLink
;
936 if ((IS_PCI_BRIDGE (&(Temp
->PciDev
->Pci
)) || (Temp
->PciDev
->Parent
== NULL
)) \
937 && (Temp
->ResourceUsage
!= PciResUsagePadding
))
939 PPBResNode
= GetMaxResourceConsumerDevice (Temp
);
940 PciResNode
= GetLargerConsumerDevice (PciResNode
, PPBResNode
);
942 PciResNode
= GetLargerConsumerDevice (PciResNode
, Temp
);
945 CurrentLink
= CurrentLink
->ForwardLink
;
952 Adjust host bridge allocation so as to reduce resource requirement
954 @param IoPool Pointer to instance of I/O resource Node.
955 @param Mem32Pool Pointer to instance of 32-bit memory resource Node.
956 @param PMem32Pool Pointer to instance of 32-bit Prefetchable memory resource node.
957 @param Mem64Pool Pointer to instance of 64-bit memory resource node.
958 @param PMem64Pool Pointer to instance of 64-bit Prefetchable memory resource node.
959 @param IoResStatus Status of I/O resource Node.
960 @param Mem32ResStatus Status of 32-bit memory resource Node.
961 @param PMem32ResStatus Status of 32-bit Prefetchable memory resource node.
962 @param Mem64ResStatus Status of 64-bit memory resource node.
963 @param PMem64ResStatus Status of 64-bit Prefetchable memory resource node.
965 @retval EFI_SUCCESS Successfully adjusted resource on host bridge.
966 @retval EFI_ABORTED Host bridge hasn't this resource type or no resource be adjusted.
970 PciHostBridgeAdjustAllocation (
971 IN PCI_RESOURCE_NODE
*IoPool
,
972 IN PCI_RESOURCE_NODE
*Mem32Pool
,
973 IN PCI_RESOURCE_NODE
*PMem32Pool
,
974 IN PCI_RESOURCE_NODE
*Mem64Pool
,
975 IN PCI_RESOURCE_NODE
*PMem64Pool
,
976 IN UINT64 IoResStatus
,
977 IN UINT64 Mem32ResStatus
,
978 IN UINT64 PMem32ResStatus
,
979 IN UINT64 Mem64ResStatus
,
980 IN UINT64 PMem64ResStatus
983 BOOLEAN AllocationAjusted
;
984 PCI_RESOURCE_NODE
*PciResNode
;
985 PCI_RESOURCE_NODE
*ResPool
[5];
986 PCI_IO_DEVICE
*RemovedPciDev
[5];
988 UINTN RemovedPciDevNum
;
992 EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA_PAYLOAD AllocFailExtendedData
;
995 ZeroMem (RemovedPciDev
, 5 * sizeof (PCI_IO_DEVICE
*));
996 RemovedPciDevNum
= 0;
999 ResPool
[1] = Mem32Pool
;
1000 ResPool
[2] = PMem32Pool
;
1001 ResPool
[3] = Mem64Pool
;
1002 ResPool
[4] = PMem64Pool
;
1004 ResStatus
[0] = IoResStatus
;
1005 ResStatus
[1] = Mem32ResStatus
;
1006 ResStatus
[2] = PMem32ResStatus
;
1007 ResStatus
[3] = Mem64ResStatus
;
1008 ResStatus
[4] = PMem64ResStatus
;
1010 AllocationAjusted
= FALSE
;
1012 for (ResType
= 0; ResType
< 5; ResType
++) {
1014 if (ResStatus
[ResType
] == EFI_RESOURCE_SATISFIED
) {
1018 if (ResStatus
[ResType
] == EFI_RESOURCE_NOT_SATISFIED
) {
1020 // Host bridge hasn't this resource type
1026 // Hostbridge hasn't enough resource
1028 PciResNode
= GetMaxResourceConsumerDevice (ResPool
[ResType
]);
1029 if (PciResNode
== NULL
) {
1034 // Check if the device has been removed before
1036 for (DevIndex
= 0; DevIndex
< RemovedPciDevNum
; DevIndex
++) {
1037 if (PciResNode
->PciDev
== RemovedPciDev
[DevIndex
]) {
1042 if (DevIndex
!= RemovedPciDevNum
) {
1047 // Remove the device if it isn't in the array
1049 Status
= RejectPciDevice (PciResNode
->PciDev
);
1050 if (Status
== EFI_SUCCESS
) {
1053 "PciBus: [%02x|%02x|%02x] was rejected due to resource confliction.\n",
1054 PciResNode
->PciDev
->BusNumber
, PciResNode
->PciDev
->DeviceNumber
, PciResNode
->PciDev
->FunctionNumber
1058 // Raise the EFI_IOB_EC_RESOURCE_CONFLICT status code
1061 // Have no way to get ReqRes, AllocRes & Bar here
1063 ZeroMem (&AllocFailExtendedData
, sizeof (AllocFailExtendedData
));
1064 AllocFailExtendedData
.DevicePathSize
= (UINT16
) sizeof (EFI_DEVICE_PATH_PROTOCOL
);
1065 AllocFailExtendedData
.DevicePath
= (UINT8
*) PciResNode
->PciDev
->DevicePath
;
1066 AllocFailExtendedData
.Bar
= PciResNode
->Bar
;
1068 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
1070 EFI_IO_BUS_PCI
| EFI_IOB_EC_RESOURCE_CONFLICT
,
1071 (VOID
*) &AllocFailExtendedData
,
1072 sizeof (AllocFailExtendedData
)
1076 // Add it to the array and indicate at least a device has been rejected
1078 RemovedPciDev
[RemovedPciDevNum
++] = PciResNode
->PciDev
;
1079 AllocationAjusted
= TRUE
;
1086 if (AllocationAjusted
) {
1094 Summary requests for all resource type, and construct ACPI resource
1097 @param Bridge detecting bridge
1098 @param IoNode Pointer to instance of I/O resource Node
1099 @param Mem32Node Pointer to instance of 32-bit memory resource Node
1100 @param PMem32Node Pointer to instance of 32-bit Pmemory resource node
1101 @param Mem64Node Pointer to instance of 64-bit memory resource node
1102 @param PMem64Node Pointer to instance of 64-bit Pmemory resource node
1103 @param Config Output buffer holding new constructed APCI resource requestor
1105 @retval EFI_SUCCESS Successfully constructed ACPI resource.
1106 @retval EFI_OUT_OF_RESOURCES No memory available.
1110 ConstructAcpiResourceRequestor (
1111 IN PCI_IO_DEVICE
*Bridge
,
1112 IN PCI_RESOURCE_NODE
*IoNode
,
1113 IN PCI_RESOURCE_NODE
*Mem32Node
,
1114 IN PCI_RESOURCE_NODE
*PMem32Node
,
1115 IN PCI_RESOURCE_NODE
*Mem64Node
,
1116 IN PCI_RESOURCE_NODE
*PMem64Node
,
1122 UINT8
*Configuration
;
1123 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Ptr
;
1124 EFI_ACPI_END_TAG_DESCRIPTOR
*PtrEnd
;
1132 // if there is io request, add to the io aperture
1134 if (ResourceRequestExisted (IoNode
)) {
1140 // if there is mem32 request, add to the mem32 aperture
1142 if (ResourceRequestExisted (Mem32Node
)) {
1148 // if there is pmem32 request, add to the pmem32 aperture
1150 if (ResourceRequestExisted (PMem32Node
)) {
1156 // if there is mem64 request, add to the mem64 aperture
1158 if (ResourceRequestExisted (Mem64Node
)) {
1164 // if there is pmem64 request, add to the pmem64 aperture
1166 if (ResourceRequestExisted (PMem64Node
)) {
1171 if (NumConfig
!= 0) {
1174 // If there is at least one type of resource request,
1175 // allocate a acpi resource node
1177 Configuration
= AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) * NumConfig
+ sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
));
1178 if (Configuration
== NULL
) {
1179 return EFI_OUT_OF_RESOURCES
;
1182 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
1185 // Deal with io aperture
1187 if ((Aperture
& 0x01) != 0) {
1188 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1189 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1193 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_IO
;
1197 Ptr
->SpecificFlag
= 1;
1198 Ptr
->AddrLen
= IoNode
->Length
;
1199 Ptr
->AddrRangeMax
= IoNode
->Alignment
;
1204 // Deal with mem32 aperture
1206 if ((Aperture
& 0x02) != 0) {
1207 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1208 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1212 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1216 Ptr
->SpecificFlag
= 0;
1220 Ptr
->AddrSpaceGranularity
= 32;
1221 Ptr
->AddrLen
= Mem32Node
->Length
;
1222 Ptr
->AddrRangeMax
= Mem32Node
->Alignment
;
1228 // Deal with Pmem32 aperture
1230 if ((Aperture
& 0x04) != 0) {
1231 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1232 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1236 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1240 Ptr
->SpecificFlag
= 0x6;
1244 Ptr
->AddrSpaceGranularity
= 32;
1245 Ptr
->AddrLen
= PMem32Node
->Length
;
1246 Ptr
->AddrRangeMax
= PMem32Node
->Alignment
;
1251 // Deal with mem64 aperture
1253 if ((Aperture
& 0x08) != 0) {
1254 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1255 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1259 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1263 Ptr
->SpecificFlag
= 0;
1267 Ptr
->AddrSpaceGranularity
= 64;
1268 Ptr
->AddrLen
= Mem64Node
->Length
;
1269 Ptr
->AddrRangeMax
= Mem64Node
->Alignment
;
1274 // Deal with Pmem64 aperture
1276 if ((Aperture
& 0x10) != 0) {
1277 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1278 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1282 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1286 Ptr
->SpecificFlag
= 0x06;
1290 Ptr
->AddrSpaceGranularity
= 64;
1291 Ptr
->AddrLen
= PMem64Node
->Length
;
1292 Ptr
->AddrRangeMax
= PMem64Node
->Alignment
;
1300 PtrEnd
= (EFI_ACPI_END_TAG_DESCRIPTOR
*) Ptr
;
1302 PtrEnd
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
1303 PtrEnd
->Checksum
= 0;
1308 // If there is no resource request
1310 Configuration
= AllocateZeroPool (sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
));
1311 if (Configuration
== NULL
) {
1312 return EFI_OUT_OF_RESOURCES
;
1315 PtrEnd
= (EFI_ACPI_END_TAG_DESCRIPTOR
*) (Configuration
);
1316 PtrEnd
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
1317 PtrEnd
->Checksum
= 0;
1320 *Config
= Configuration
;
1326 Get resource base from an acpi configuration descriptor.
1328 @param Config An acpi configuration descriptor.
1329 @param IoBase Output of I/O resource base address.
1330 @param Mem32Base Output of 32-bit memory base address.
1331 @param PMem32Base Output of 32-bit prefetchable memory base address.
1332 @param Mem64Base Output of 64-bit memory base address.
1333 @param PMem64Base Output of 64-bit prefetchable memory base address.
1340 OUT UINT64
*Mem32Base
,
1341 OUT UINT64
*PMem32Base
,
1342 OUT UINT64
*Mem64Base
,
1343 OUT UINT64
*PMem64Base
1347 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Ptr
;
1350 ASSERT (Config
!= NULL
);
1352 *IoBase
= 0xFFFFFFFFFFFFFFFFULL
;
1353 *Mem32Base
= 0xFFFFFFFFFFFFFFFFULL
;
1354 *PMem32Base
= 0xFFFFFFFFFFFFFFFFULL
;
1355 *Mem64Base
= 0xFFFFFFFFFFFFFFFFULL
;
1356 *PMem64Base
= 0xFFFFFFFFFFFFFFFFULL
;
1358 Temp
= (UINT8
*) Config
;
1360 while (*Temp
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
1362 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Temp
;
1363 ResStatus
= Ptr
->AddrTranslationOffset
;
1365 if (ResStatus
== EFI_RESOURCE_SATISFIED
) {
1367 switch (Ptr
->ResType
) {
1370 // Memory type aperture
1375 // Check to see the granularity
1377 if (Ptr
->AddrSpaceGranularity
== 32) {
1378 if ((Ptr
->SpecificFlag
& 0x06) != 0) {
1379 *PMem32Base
= Ptr
->AddrRangeMin
;
1381 *Mem32Base
= Ptr
->AddrRangeMin
;
1385 if (Ptr
->AddrSpaceGranularity
== 64) {
1386 if ((Ptr
->SpecificFlag
& 0x06) != 0) {
1387 *PMem64Base
= Ptr
->AddrRangeMin
;
1389 *Mem64Base
= Ptr
->AddrRangeMin
;
1399 *IoBase
= Ptr
->AddrRangeMin
;
1413 Temp
+= sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
);
1418 Enumerate pci bridge, allocate resource and determine attribute
1419 for devices on this bridge.
1421 @param BridgeDev Pointer to instance of bridge device.
1423 @retval EFI_SUCCESS Successfully enumerated PCI bridge.
1424 @retval other Failed to enumerate.
1428 PciBridgeEnumerator (
1429 IN PCI_IO_DEVICE
*BridgeDev
1433 UINT8 StartBusNumber
;
1434 EFI_PCI_IO_PROTOCOL
*PciIo
;
1439 PciIo
= &(BridgeDev
->PciIo
);
1440 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x19, 1, &StartBusNumber
);
1442 if (EFI_ERROR (Status
)) {
1446 Status
= PciAssignBusNumber (
1452 if (EFI_ERROR (Status
)) {
1456 Status
= PciPciDeviceInfoCollector (BridgeDev
, StartBusNumber
);
1458 if (EFI_ERROR (Status
)) {
1462 Status
= PciBridgeResourceAllocator (BridgeDev
);
1464 if (EFI_ERROR (Status
)) {
1468 Status
= DetermineDeviceAttribute (BridgeDev
);
1470 if (EFI_ERROR (Status
)) {
1479 Allocate all kinds of resource for PCI bridge.
1481 @param Bridge Pointer to bridge instance.
1483 @retval EFI_SUCCESS Successfully allocated resource for PCI bridge.
1484 @retval other Failed to allocate resource for bridge.
1488 PciBridgeResourceAllocator (
1489 IN PCI_IO_DEVICE
*Bridge
1492 PCI_RESOURCE_NODE
*IoBridge
;
1493 PCI_RESOURCE_NODE
*Mem32Bridge
;
1494 PCI_RESOURCE_NODE
*PMem32Bridge
;
1495 PCI_RESOURCE_NODE
*Mem64Bridge
;
1496 PCI_RESOURCE_NODE
*PMem64Bridge
;
1504 IoBridge
= CreateResourceNode (
1507 Bridge
->BridgeIoAlignment
,
1513 Mem32Bridge
= CreateResourceNode (
1522 PMem32Bridge
= CreateResourceNode (
1531 Mem64Bridge
= CreateResourceNode (
1540 PMem64Bridge
= CreateResourceNode (
1550 // Create resourcemap by going through all the devices subject to this root bridge
1561 Status
= GetResourceBaseFromBridge (
1570 if (EFI_ERROR (Status
)) {
1575 // Program IO resources
1583 // Program Mem32 resources
1591 // Program PMem32 resources
1599 // Program Mem64 resources
1607 // Program PMem64 resources
1614 DestroyResourceTree (IoBridge
);
1615 DestroyResourceTree (Mem32Bridge
);
1616 DestroyResourceTree (PMem32Bridge
);
1617 DestroyResourceTree (PMem64Bridge
);
1618 DestroyResourceTree (Mem64Bridge
);
1620 gBS
->FreePool (IoBridge
);
1621 gBS
->FreePool (Mem32Bridge
);
1622 gBS
->FreePool (PMem32Bridge
);
1623 gBS
->FreePool (PMem64Bridge
);
1624 gBS
->FreePool (Mem64Bridge
);
1630 Get resource base address for a pci bridge device.
1632 @param Bridge Given Pci driver instance.
1633 @param IoBase Output for base address of I/O type resource.
1634 @param Mem32Base Output for base address of 32-bit memory type resource.
1635 @param PMem32Base Ooutput for base address of 32-bit Pmemory type resource.
1636 @param Mem64Base Output for base address of 64-bit memory type resource.
1637 @param PMem64Base Output for base address of 64-bit Pmemory type resource.
1639 @retval EFI_SUCCESS Successfully got resource base address.
1640 @retval EFI_OUT_OF_RESOURCES PCI bridge is not available.
1644 GetResourceBaseFromBridge (
1645 IN PCI_IO_DEVICE
*Bridge
,
1647 OUT UINT64
*Mem32Base
,
1648 OUT UINT64
*PMem32Base
,
1649 OUT UINT64
*Mem64Base
,
1650 OUT UINT64
*PMem64Base
1653 if (!Bridge
->Allocated
) {
1654 return EFI_OUT_OF_RESOURCES
;
1658 *Mem32Base
= gAllOne
;
1659 *PMem32Base
= gAllOne
;
1660 *Mem64Base
= gAllOne
;
1661 *PMem64Base
= gAllOne
;
1663 if (IS_PCI_BRIDGE (&Bridge
->Pci
)) {
1665 if (Bridge
->PciBar
[PPB_IO_RANGE
].Length
> 0) {
1666 *IoBase
= Bridge
->PciBar
[PPB_IO_RANGE
].BaseAddress
;
1669 if (Bridge
->PciBar
[PPB_MEM32_RANGE
].Length
> 0) {
1670 *Mem32Base
= Bridge
->PciBar
[PPB_MEM32_RANGE
].BaseAddress
;
1673 if (Bridge
->PciBar
[PPB_PMEM32_RANGE
].Length
> 0) {
1674 *PMem32Base
= Bridge
->PciBar
[PPB_PMEM32_RANGE
].BaseAddress
;
1677 if (Bridge
->PciBar
[PPB_PMEM64_RANGE
].Length
> 0) {
1678 *PMem64Base
= Bridge
->PciBar
[PPB_PMEM64_RANGE
].BaseAddress
;
1680 *PMem64Base
= gAllOne
;
1685 if (IS_CARDBUS_BRIDGE (&Bridge
->Pci
)) {
1686 if (Bridge
->PciBar
[P2C_IO_1
].Length
> 0) {
1687 *IoBase
= Bridge
->PciBar
[P2C_IO_1
].BaseAddress
;
1689 if (Bridge
->PciBar
[P2C_IO_2
].Length
> 0) {
1690 *IoBase
= Bridge
->PciBar
[P2C_IO_2
].BaseAddress
;
1694 if (Bridge
->PciBar
[P2C_MEM_1
].Length
> 0) {
1695 if (Bridge
->PciBar
[P2C_MEM_1
].BarType
== PciBarTypePMem32
) {
1696 *PMem32Base
= Bridge
->PciBar
[P2C_MEM_1
].BaseAddress
;
1699 if (Bridge
->PciBar
[P2C_MEM_1
].BarType
== PciBarTypeMem32
) {
1700 *Mem32Base
= Bridge
->PciBar
[P2C_MEM_1
].BaseAddress
;
1704 if (Bridge
->PciBar
[P2C_MEM_2
].Length
> 0) {
1705 if (Bridge
->PciBar
[P2C_MEM_2
].BarType
== PciBarTypePMem32
) {
1706 *PMem32Base
= Bridge
->PciBar
[P2C_MEM_2
].BaseAddress
;
1709 if (Bridge
->PciBar
[P2C_MEM_2
].BarType
== PciBarTypeMem32
) {
1710 *Mem32Base
= Bridge
->PciBar
[P2C_MEM_2
].BaseAddress
;
1719 These are the notifications from the PCI bus driver that it is about to enter a certain
1720 phase of the PCI enumeration process.
1722 This member function can be used to notify the host bridge driver to perform specific actions,
1723 including any chipset-specific initialization, so that the chipset is ready to enter the next phase.
1724 Eight notification points are defined at this time. See belows:
1725 EfiPciHostBridgeBeginEnumeration Resets the host bridge PCI apertures and internal data
1726 structures. The PCI enumerator should issue this notification
1727 before starting a fresh enumeration process. Enumeration cannot
1728 be restarted after sending any other notification such as
1729 EfiPciHostBridgeBeginBusAllocation.
1730 EfiPciHostBridgeBeginBusAllocation The bus allocation phase is about to begin. No specific action is
1731 required here. This notification can be used to perform any
1732 chipset-specific programming.
1733 EfiPciHostBridgeEndBusAllocation The bus allocation and bus programming phase is complete. No
1734 specific action is required here. This notification can be used to
1735 perform any chipset-specific programming.
1736 EfiPciHostBridgeBeginResourceAllocation
1737 The resource allocation phase is about to begin. No specific
1738 action is required here. This notification can be used to perform
1739 any chipset-specific programming.
1740 EfiPciHostBridgeAllocateResources Allocates resources per previously submitted requests for all the PCI
1741 root bridges. These resource settings are returned on the next call to
1742 GetProposedResources(). Before calling NotifyPhase() with a Phase of
1743 EfiPciHostBridgeAllocateResource, the PCI bus enumerator is responsible
1744 for gathering I/O and memory requests for
1745 all the PCI root bridges and submitting these requests using
1746 SubmitResources(). This function pads the resource amount
1747 to suit the root bridge hardware, takes care of dependencies between
1748 the PCI root bridges, and calls the Global Coherency Domain (GCD)
1749 with the allocation request. In the case of padding, the allocated range
1750 could be bigger than what was requested.
1751 EfiPciHostBridgeSetResources Programs the host bridge hardware to decode previously allocated
1752 resources (proposed resources) for all the PCI root bridges. After the
1753 hardware is programmed, reassigning resources will not be supported.
1754 The bus settings are not affected.
1755 EfiPciHostBridgeFreeResources Deallocates resources that were previously allocated for all the PCI
1756 root bridges and resets the I/O and memory apertures to their initial
1757 state. The bus settings are not affected. If the request to allocate
1758 resources fails, the PCI enumerator can use this notification to
1759 deallocate previous resources, adjust the requests, and retry
1761 EfiPciHostBridgeEndResourceAllocation The resource allocation phase is completed. No specific action is
1762 required here. This notification can be used to perform any chipsetspecific
1765 @param[in] PciResAlloc The instance pointer of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
1766 @param[in] Phase The phase during enumeration
1768 @retval EFI_NOT_READY This phase cannot be entered at this time. For example, this error
1769 is valid for a Phase of EfiPciHostBridgeAllocateResources if
1770 SubmitResources() has not been called for one or more
1771 PCI root bridges before this call
1772 @retval EFI_DEVICE_ERROR Programming failed due to a hardware error. This error is valid
1773 for a Phase of EfiPciHostBridgeSetResources.
1774 @retval EFI_INVALID_PARAMETER Invalid phase parameter
1775 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1776 This error is valid for a Phase of EfiPciHostBridgeAllocateResources if the
1777 previously submitted resource requests cannot be fulfilled or
1778 were only partially fulfilled.
1779 @retval EFI_SUCCESS The notification was accepted without any errors.
1784 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
,
1785 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE Phase
1788 EFI_HANDLE HostBridgeHandle
;
1789 EFI_HANDLE RootBridgeHandle
;
1790 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
1793 HostBridgeHandle
= NULL
;
1794 RootBridgeHandle
= NULL
;
1795 if (gPciPlatformProtocol
!= NULL
) {
1797 // Get Host Bridge Handle.
1799 PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
);
1802 // Get the rootbridge Io protocol to find the host bridge handle
1804 Status
= gBS
->HandleProtocol (
1806 &gEfiPciRootBridgeIoProtocolGuid
,
1807 (VOID
**) &PciRootBridgeIo
1810 if (EFI_ERROR (Status
)) {
1811 return EFI_NOT_FOUND
;
1814 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
1817 // Call PlatformPci::PlatformNotify() if the protocol is present.
1819 gPciPlatformProtocol
->PlatformNotify (
1820 gPciPlatformProtocol
,
1825 } else if (gPciOverrideProtocol
!= NULL
){
1827 // Get Host Bridge Handle.
1829 PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
);
1832 // Get the rootbridge Io protocol to find the host bridge handle
1834 Status
= gBS
->HandleProtocol (
1836 &gEfiPciRootBridgeIoProtocolGuid
,
1837 (VOID
**) &PciRootBridgeIo
1840 if (EFI_ERROR (Status
)) {
1841 return EFI_NOT_FOUND
;
1844 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
1847 // Call PlatformPci::PhaseNotify() if the protocol is present.
1849 gPciOverrideProtocol
->PlatformNotify (
1850 gPciOverrideProtocol
,
1857 Status
= PciResAlloc
->NotifyPhase (
1862 if (gPciPlatformProtocol
!= NULL
) {
1864 // Call PlatformPci::PlatformNotify() if the protocol is present.
1866 gPciPlatformProtocol
->PlatformNotify (
1867 gPciPlatformProtocol
,
1873 } else if (gPciOverrideProtocol
!= NULL
) {
1875 // Call PlatformPci::PhaseNotify() if the protocol is present.
1877 gPciOverrideProtocol
->PlatformNotify (
1878 gPciOverrideProtocol
,
1889 Provides the hooks from the PCI bus driver to every PCI controller (device/function) at various
1890 stages of the PCI enumeration process that allow the host bridge driver to preinitialize individual
1891 PCI controllers before enumeration.
1893 This function is called during the PCI enumeration process. No specific action is expected from this
1894 member function. It allows the host bridge driver to preinitialize individual PCI controllers before
1897 @param Bridge Pointer to the EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL instance.
1898 @param Bus The bus number of the pci device.
1899 @param Device The device number of the pci device.
1900 @param Func The function number of the pci device.
1901 @param Phase The phase of the PCI device enumeration.
1903 @retval EFI_SUCCESS The requested parameters were returned.
1904 @retval EFI_INVALID_PARAMETER RootBridgeHandle is not a valid root bridge handle.
1905 @retval EFI_INVALID_PARAMETER Phase is not a valid phase that is defined in
1906 EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE.
1907 @retval EFI_DEVICE_ERROR Programming failed due to a hardware error. The PCI enumerator should
1908 not enumerate this device, including its child devices if it is a PCI-to-PCI
1913 PreprocessController (
1914 IN PCI_IO_DEVICE
*Bridge
,
1918 IN EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE Phase
1921 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS RootBridgePciAddress
;
1922 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
;
1923 EFI_HANDLE RootBridgeHandle
;
1924 EFI_HANDLE HostBridgeHandle
;
1928 // Get the host bridge handle
1930 HostBridgeHandle
= Bridge
->PciRootBridgeIo
->ParentHandle
;
1933 // Get the pci host bridge resource allocation protocol
1935 Status
= gBS
->OpenProtocol (
1937 &gEfiPciHostBridgeResourceAllocationProtocolGuid
,
1938 (VOID
**) &PciResAlloc
,
1941 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1944 if (EFI_ERROR (Status
)) {
1945 return EFI_UNSUPPORTED
;
1949 // Get Root Brige Handle
1951 while (Bridge
->Parent
!= NULL
) {
1952 Bridge
= Bridge
->Parent
;
1955 RootBridgeHandle
= Bridge
->Handle
;
1957 RootBridgePciAddress
.Register
= 0;
1958 RootBridgePciAddress
.Function
= Func
;
1959 RootBridgePciAddress
.Device
= Device
;
1960 RootBridgePciAddress
.Bus
= Bus
;
1961 RootBridgePciAddress
.ExtendedRegister
= 0;
1963 if (gPciPlatformProtocol
!= NULL
) {
1965 // Call PlatformPci::PrepController() if the protocol is present.
1967 gPciPlatformProtocol
->PlatformPrepController (
1968 gPciPlatformProtocol
,
1971 RootBridgePciAddress
,
1975 } else if (gPciOverrideProtocol
!= NULL
) {
1977 // Call PlatformPci::PrepController() if the protocol is present.
1979 gPciOverrideProtocol
->PlatformPrepController (
1980 gPciOverrideProtocol
,
1983 RootBridgePciAddress
,
1989 Status
= PciResAlloc
->PreprocessController (
1992 RootBridgePciAddress
,
1996 if (gPciPlatformProtocol
!= NULL
) {
1998 // Call PlatformPci::PrepController() if the protocol is present.
2000 gPciPlatformProtocol
->PlatformPrepController (
2001 gPciPlatformProtocol
,
2004 RootBridgePciAddress
,
2008 } else if (gPciOverrideProtocol
!= NULL
) {
2010 // Call PlatformPci::PrepController() if the protocol is present.
2012 gPciOverrideProtocol
->PlatformPrepController (
2013 gPciOverrideProtocol
,
2016 RootBridgePciAddress
,
2026 This function allows the PCI bus driver to be notified to act as requested when a hot-plug event has
2027 happened on the hot-plug controller. Currently, the operations include add operation and remove operation..
2029 @param This A pointer to the hot plug request protocol.
2030 @param Operation The operation the PCI bus driver is requested to make.
2031 @param Controller The handle of the hot-plug controller.
2032 @param RemainingDevicePath The remaining device path for the PCI-like hot-plug device.
2033 @param NumberOfChildren The number of child handles.
2034 For a add operation, it is an output parameter.
2035 For a remove operation, it's an input parameter.
2036 @param ChildHandleBuffer The buffer which contains the child handles.
2038 @retval EFI_INVALID_PARAMETER Operation is not a legal value.
2039 Controller is NULL or not a valid handle.
2040 NumberOfChildren is NULL.
2041 ChildHandleBuffer is NULL while Operation is add.
2042 @retval EFI_OUT_OF_RESOURCES There are no enough resources to start the devices.
2043 @retval EFI_NOT_FOUND Can not find bridge according to controller handle.
2044 @retval EFI_SUCCESS The handles for the specified device have been created or destroyed
2045 as requested, and for an add operation, the new handles are
2046 returned in ChildHandleBuffer.
2050 PciHotPlugRequestNotify (
2051 IN EFI_PCI_HOTPLUG_REQUEST_PROTOCOL
* This
,
2052 IN EFI_PCI_HOTPLUG_OPERATION Operation
,
2053 IN EFI_HANDLE Controller
,
2054 IN EFI_DEVICE_PATH_PROTOCOL
* RemainingDevicePath OPTIONAL
,
2055 IN OUT UINT8
*NumberOfChildren
,
2056 IN OUT EFI_HANDLE
* ChildHandleBuffer
2059 PCI_IO_DEVICE
*Bridge
;
2060 PCI_IO_DEVICE
*Temp
;
2061 EFI_PCI_IO_PROTOCOL
*PciIo
;
2063 EFI_HANDLE RootBridgeHandle
;
2067 // Check input parameter validity
2069 if ((Controller
== NULL
) || (NumberOfChildren
== NULL
)){
2070 return EFI_INVALID_PARAMETER
;
2073 if ((Operation
!= EfiPciHotPlugRequestAdd
) && (Operation
!= EfiPciHotplugRequestRemove
)) {
2074 return EFI_INVALID_PARAMETER
;
2077 if (Operation
== EfiPciHotPlugRequestAdd
){
2078 if (ChildHandleBuffer
== NULL
) {
2079 return EFI_INVALID_PARAMETER
;
2081 } else if ((Operation
== EfiPciHotplugRequestRemove
) && (*NumberOfChildren
!= 0)) {
2082 if (ChildHandleBuffer
== NULL
) {
2083 return EFI_INVALID_PARAMETER
;
2087 Status
= gBS
->OpenProtocol (
2089 &gEfiPciIoProtocolGuid
,
2091 gPciBusDriverBinding
.DriverBindingHandle
,
2093 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2096 if (EFI_ERROR (Status
)) {
2097 return EFI_NOT_FOUND
;
2100 Bridge
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (PciIo
);
2103 // Get root bridge handle
2106 while (Temp
->Parent
!= NULL
) {
2107 Temp
= Temp
->Parent
;
2110 RootBridgeHandle
= Temp
->Handle
;
2112 if (Operation
== EfiPciHotPlugRequestAdd
) {
2114 // Report Status Code to indicate hot plug happens
2116 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
2118 (EFI_IO_BUS_PCI
| EFI_IOB_PC_HOTPLUG
),
2122 if (NumberOfChildren
!= NULL
) {
2123 *NumberOfChildren
= 0;
2126 if (IsListEmpty (&Bridge
->ChildList
)) {
2128 Status
= PciBridgeEnumerator (Bridge
);
2130 if (EFI_ERROR (Status
)) {
2135 Status
= StartPciDevicesOnBridge (
2138 RemainingDevicePath
,
2146 if (Operation
== EfiPciHotplugRequestRemove
) {
2148 if (*NumberOfChildren
== 0) {
2150 // Remove all devices on the bridge
2152 RemoveAllPciDeviceOnBridge (RootBridgeHandle
, Bridge
);
2157 for (Index
= 0; Index
< *NumberOfChildren
; Index
++) {
2159 // De register all the pci device
2161 Status
= DeRegisterPciDevice (RootBridgeHandle
, ChildHandleBuffer
[Index
]);
2163 if (EFI_ERROR (Status
)) {
2178 Search hostbridge according to given handle
2180 @param RootBridgeHandle Host bridge handle.
2182 @retval TRUE Found host bridge handle.
2183 @retval FALSE Not found hot bridge handle.
2187 SearchHostBridgeHandle (
2188 IN EFI_HANDLE RootBridgeHandle
2191 EFI_HANDLE HostBridgeHandle
;
2192 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
2197 // Get the rootbridge Io protocol to find the host bridge handle
2199 Status
= gBS
->OpenProtocol (
2201 &gEfiPciRootBridgeIoProtocolGuid
,
2202 (VOID
**) &PciRootBridgeIo
,
2203 gPciBusDriverBinding
.DriverBindingHandle
,
2205 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2208 if (EFI_ERROR (Status
)) {
2212 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
2213 for (Index
= 0; Index
< gPciHostBridgeNumber
; Index
++) {
2214 if (HostBridgeHandle
== gPciHostBrigeHandles
[Index
]) {
2223 Add host bridge handle to global variable for enumerating.
2225 @param HostBridgeHandle Host bridge handle.
2227 @retval EFI_SUCCESS Successfully added host bridge.
2228 @retval EFI_ABORTED Host bridge is NULL, or given host bridge
2229 has been in host bridge list.
2233 AddHostBridgeEnumerator (
2234 IN EFI_HANDLE HostBridgeHandle
2239 if (HostBridgeHandle
== NULL
) {
2243 for (Index
= 0; Index
< gPciHostBridgeNumber
; Index
++) {
2244 if (HostBridgeHandle
== gPciHostBrigeHandles
[Index
]) {
2249 if (Index
< PCI_MAX_HOST_BRIDGE_NUM
) {
2250 gPciHostBrigeHandles
[Index
] = HostBridgeHandle
;
2251 gPciHostBridgeNumber
++;