2 PCI eunmeration implementation on entire PCI bus system for PCI Bus module.
4 Copyright (c) 2006 - 2012, 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.
18 This routine is used to enumerate entire pci bus system
21 @param Controller Parent controller handle.
23 @retval EFI_SUCCESS PCI enumeration finished successfully.
24 @retval other Some error occurred when enumerating the pci bus system.
29 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 NotifyPhase (PciResAlloc
, EfiPciHostBridgeBeginEnumeration
);
88 // Start the bus allocation phase
90 Status
= PciHostBridgeEnumerator (PciResAlloc
);
92 if (EFI_ERROR (Status
)) {
97 // Submit the resource request
99 Status
= PciHostBridgeResourceAllocator (PciResAlloc
);
101 if (EFI_ERROR (Status
)) {
106 // Notify the pci bus enumeration is about to complete
108 NotifyPhase (PciResAlloc
, EfiPciHostBridgeEndEnumeration
);
113 Status
= PciHostBridgeP2CProcess (PciResAlloc
);
115 if (EFI_ERROR (Status
)) {
120 // Process attributes for devices on this host bridge
122 Status
= PciHostBridgeDeviceAttribute (PciResAlloc
);
123 if (EFI_ERROR (Status
)) {
127 gFullEnumeration
= FALSE
;
130 Status
= gBS
->InstallProtocolInterface (
132 &gEfiPciEnumerationCompleteProtocolGuid
,
133 EFI_NATIVE_INTERFACE
,
136 if (EFI_ERROR (Status
)) {
144 Enumerate PCI root bridge.
146 @param PciResAlloc Pointer to protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
147 @param RootBridgeDev Instance of root bridge device.
149 @retval EFI_SUCCESS Successfully enumerated root bridge.
150 @retval other Failed to enumerate root bridge.
154 PciRootBridgeEnumerator (
155 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
,
156 IN PCI_IO_DEVICE
*RootBridgeDev
160 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration
;
161 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration1
;
162 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration2
;
163 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Configuration3
;
165 UINT8 StartBusNumber
;
166 UINT8 PaddedBusRange
;
167 EFI_HANDLE RootBridgeHandle
;
177 // Get the root bridge handle
179 RootBridgeHandle
= RootBridgeDev
->Handle
;
181 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
183 EFI_IO_BUS_PCI
| EFI_IOB_PCI_BUS_ENUM
,
184 RootBridgeDev
->DevicePath
188 // Get the Bus information
190 Status
= PciResAlloc
->StartBusEnumeration (
193 (VOID
**) &Configuration
196 if (EFI_ERROR (Status
)) {
200 if (Configuration
== NULL
|| Configuration
->Desc
== ACPI_END_TAG_DESCRIPTOR
) {
201 return EFI_INVALID_PARAMETER
;
203 RootBridgeDev
->BusNumberRanges
= Configuration
;
206 // Sort the descriptors in ascending order
208 for (Configuration1
= Configuration
; Configuration1
->Desc
!= ACPI_END_TAG_DESCRIPTOR
; Configuration1
++) {
209 Configuration2
= Configuration1
;
210 for (Configuration3
= Configuration1
+ 1; Configuration3
->Desc
!= ACPI_END_TAG_DESCRIPTOR
; Configuration3
++) {
211 if (Configuration2
->AddrRangeMin
> Configuration3
->AddrRangeMin
) {
212 Configuration2
= Configuration3
;
216 // All other fields other than AddrRangeMin and AddrLen are ignored in a descriptor,
217 // so only need to swap these two fields.
219 if (Configuration2
!= Configuration1
) {
220 AddrRangeMin
= Configuration1
->AddrRangeMin
;
221 Configuration1
->AddrRangeMin
= Configuration2
->AddrRangeMin
;
222 Configuration2
->AddrRangeMin
= AddrRangeMin
;
224 AddrLen
= Configuration1
->AddrLen
;
225 Configuration1
->AddrLen
= Configuration2
->AddrLen
;
226 Configuration2
->AddrLen
= AddrLen
;
231 // Get the bus number to start with
233 StartBusNumber
= (UINT8
) (Configuration
->AddrRangeMin
);
236 // Initialize the subordinate bus number
238 SubBusNumber
= StartBusNumber
;
241 // Reset all assigned PCI bus number
243 ResetAllPpbBusNumber (
251 Status
= PciScanBus (
258 if (EFI_ERROR (Status
)) {
264 // Assign max bus number scanned
267 Status
= PciAllocateBusNumber (RootBridgeDev
, SubBusNumber
, PaddedBusRange
, &SubBusNumber
);
268 if (EFI_ERROR (Status
)) {
273 // Find the bus range which contains the higest bus number, then returns the number of buses
274 // that should be decoded.
276 while (Configuration
->AddrRangeMin
+ Configuration
->AddrLen
- 1 < SubBusNumber
) {
279 AddrLen
= Configuration
->AddrLen
;
280 Configuration
->AddrLen
= SubBusNumber
- Configuration
->AddrRangeMin
+ 1;
283 // Save the Desc field of the next descriptor. Mark the next descriptor as an END descriptor.
286 Desc
= Configuration
->Desc
;
287 Configuration
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
292 Status
= PciResAlloc
->SetBusNumbers (
295 RootBridgeDev
->BusNumberRanges
299 // Restore changed fields
301 Configuration
->Desc
= Desc
;
302 (Configuration
- 1)->AddrLen
= AddrLen
;
308 This routine is used to process all PCI devices' Option Rom
309 on a certain root bridge.
311 @param Bridge Given parent's root bridge.
312 @param RomBase Base address of ROM driver loaded from.
313 @param MaxLength Maximum rom size.
318 IN PCI_IO_DEVICE
*Bridge
,
323 LIST_ENTRY
*CurrentLink
;
327 // Go through bridges to reach all devices
329 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
330 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
331 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
332 if (!IsListEmpty (&Temp
->ChildList
)) {
335 // Go further to process the option rom under this bridge
337 ProcessOptionRom (Temp
, RomBase
, MaxLength
);
340 if (Temp
->RomSize
!= 0 && Temp
->RomSize
<= MaxLength
) {
343 // Load and process the option rom
345 LoadOpRomImage (Temp
, RomBase
);
348 CurrentLink
= CurrentLink
->ForwardLink
;
353 This routine is used to assign bus number to the given PCI bus system
355 @param Bridge Parent root bridge instance.
356 @param StartBusNumber Number of beginning.
357 @param SubBusNumber The number of sub bus.
359 @retval EFI_SUCCESS Successfully assigned bus number.
360 @retval EFI_DEVICE_ERROR Failed to assign bus number.
365 IN PCI_IO_DEVICE
*Bridge
,
366 IN UINT8 StartBusNumber
,
367 OUT UINT8
*SubBusNumber
378 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
380 PciRootBridgeIo
= Bridge
->PciRootBridgeIo
;
385 *SubBusNumber
= StartBusNumber
;
388 // First check to see whether the parent is ppb
390 for (Device
= 0; Device
<= PCI_MAX_DEVICE
; Device
++) {
391 for (Func
= 0; Func
<= PCI_MAX_FUNC
; Func
++) {
394 // Check to see whether a pci device is present
396 Status
= PciDevicePresent (
404 if (!EFI_ERROR (Status
) &&
405 (IS_PCI_BRIDGE (&Pci
) || IS_CARDBUS_BRIDGE (&Pci
))) {
408 // Reserved one bus for cardbus bridge
410 Status
= PciAllocateBusNumber (Bridge
, *SubBusNumber
, 1, SubBusNumber
);
411 if (EFI_ERROR (Status
)) {
414 SecondBus
= *SubBusNumber
;
416 Register
= (UINT16
) ((SecondBus
<< 8) | (UINT16
) StartBusNumber
);
418 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0x18);
420 Status
= PciRootBridgeIo
->Pci
.Write (
429 // Initialize SubBusNumber to SecondBus
431 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0x1A);
432 Status
= PciRootBridgeIo
->Pci
.Write (
440 // If it is PPB, resursively search down this bridge
442 if (IS_PCI_BRIDGE (&Pci
)) {
445 Status
= PciRootBridgeIo
->Pci
.Write (
453 Status
= PciAssignBusNumber (
459 if (EFI_ERROR (Status
)) {
460 return EFI_DEVICE_ERROR
;
465 // Set the current maximum bus number under the PPB
467 Address
= EFI_PCI_ADDRESS (StartBusNumber
, Device
, Func
, 0x1A);
469 Status
= PciRootBridgeIo
->Pci
.Write (
479 if (Func
== 0 && !IS_PCI_MULTI_FUNC (&Pci
)) {
482 // Skip sub functions, this is not a multi function device
493 This routine is used to determine the root bridge attribute by interfacing
494 the host bridge resource allocation protocol.
496 @param PciResAlloc Protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
497 @param RootBridgeDev Root bridge instance
499 @retval EFI_SUCCESS Successfully got root bridge's attribute.
500 @retval other Failed to get attribute.
504 DetermineRootBridgeAttributes (
505 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
,
506 IN PCI_IO_DEVICE
*RootBridgeDev
511 EFI_HANDLE RootBridgeHandle
;
514 RootBridgeHandle
= RootBridgeDev
->Handle
;
517 // Get root bridge attribute by calling into pci host bridge resource allocation protocol
519 Status
= PciResAlloc
->GetAllocAttributes (
525 if (EFI_ERROR (Status
)) {
530 // Here is the point where PCI bus driver calls HOST bridge allocation protocol
531 // Currently we hardcoded for ea815
533 if ((Attributes
& EFI_PCI_HOST_BRIDGE_COMBINE_MEM_PMEM
) != 0) {
534 RootBridgeDev
->Decodes
|= EFI_BRIDGE_PMEM_MEM_COMBINE_SUPPORTED
;
537 if ((Attributes
& EFI_PCI_HOST_BRIDGE_MEM64_DECODE
) != 0) {
538 RootBridgeDev
->Decodes
|= EFI_BRIDGE_MEM64_DECODE_SUPPORTED
;
539 RootBridgeDev
->Decodes
|= EFI_BRIDGE_PMEM64_DECODE_SUPPORTED
;
542 RootBridgeDev
->Decodes
|= EFI_BRIDGE_MEM32_DECODE_SUPPORTED
;
543 RootBridgeDev
->Decodes
|= EFI_BRIDGE_PMEM32_DECODE_SUPPORTED
;
544 RootBridgeDev
->Decodes
|= EFI_BRIDGE_IO16_DECODE_SUPPORTED
;
550 Get Max Option Rom size on specified bridge.
552 @param Bridge Given bridge device instance.
554 @return Max size of option rom needed.
558 GetMaxOptionRomSize (
559 IN PCI_IO_DEVICE
*Bridge
562 LIST_ENTRY
*CurrentLink
;
564 UINT64 MaxOptionRomSize
;
565 UINT64 TempOptionRomSize
;
567 MaxOptionRomSize
= 0;
570 // Go through bridges to reach all devices
572 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
573 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
574 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
575 if (!IsListEmpty (&Temp
->ChildList
)) {
578 // Get max option rom size under this bridge
580 TempOptionRomSize
= GetMaxOptionRomSize (Temp
);
583 // Compare with the option rom size of the bridge
584 // Get the larger one
586 if (Temp
->RomSize
> TempOptionRomSize
) {
587 TempOptionRomSize
= Temp
->RomSize
;
593 // For devices get the rom size directly
595 TempOptionRomSize
= Temp
->RomSize
;
599 // Get the largest rom size on this bridge
601 if (TempOptionRomSize
> MaxOptionRomSize
) {
602 MaxOptionRomSize
= TempOptionRomSize
;
605 CurrentLink
= CurrentLink
->ForwardLink
;
608 return MaxOptionRomSize
;
612 Process attributes of devices on this host bridge
614 @param PciResAlloc Protocol instance of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL.
616 @retval EFI_SUCCESS Successfully process attribute.
617 @retval EFI_NOT_FOUND Can not find the specific root bridge device.
618 @retval other Failed to determine the root bridge device's attribute.
622 PciHostBridgeDeviceAttribute (
623 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
626 EFI_HANDLE RootBridgeHandle
;
627 PCI_IO_DEVICE
*RootBridgeDev
;
630 RootBridgeHandle
= NULL
;
632 while (PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
) == EFI_SUCCESS
) {
635 // Get RootBridg Device by handle
637 RootBridgeDev
= GetRootBridgeByHandle (RootBridgeHandle
);
639 if (RootBridgeDev
== NULL
) {
640 return EFI_NOT_FOUND
;
644 // Set the attributes for devcies behind the Root Bridge
646 Status
= DetermineDeviceAttribute (RootBridgeDev
);
647 if (EFI_ERROR (Status
)) {
657 Get resource allocation status from the ACPI resource descriptor.
659 @param AcpiConfig Point to Acpi configuration table.
660 @param IoResStatus Return the status of I/O resource.
661 @param Mem32ResStatus Return the status of 32-bit Memory resource.
662 @param PMem32ResStatus Return the status of 32-bit Prefetchable Memory resource.
663 @param Mem64ResStatus Return the status of 64-bit Memory resource.
664 @param PMem64ResStatus Return the status of 64-bit Prefetchable Memory resource.
668 GetResourceAllocationStatus (
670 OUT UINT64
*IoResStatus
,
671 OUT UINT64
*Mem32ResStatus
,
672 OUT UINT64
*PMem32ResStatus
,
673 OUT UINT64
*Mem64ResStatus
,
674 OUT UINT64
*PMem64ResStatus
679 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*ACPIAddressDesc
;
681 Temp
= (UINT8
*) AcpiConfig
;
683 while (*Temp
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
685 ACPIAddressDesc
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Temp
;
686 ResStatus
= ACPIAddressDesc
->AddrTranslationOffset
;
688 switch (ACPIAddressDesc
->ResType
) {
690 if (ACPIAddressDesc
->AddrSpaceGranularity
== 32) {
691 if (ACPIAddressDesc
->SpecificFlag
== 0x06) {
695 *PMem32ResStatus
= ResStatus
;
700 *Mem32ResStatus
= ResStatus
;
704 if (ACPIAddressDesc
->AddrSpaceGranularity
== 64) {
705 if (ACPIAddressDesc
->SpecificFlag
== 0x06) {
709 *PMem64ResStatus
= ResStatus
;
714 *Mem64ResStatus
= ResStatus
;
724 *IoResStatus
= ResStatus
;
731 Temp
+= sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
);
736 Remove a PCI device from device pool and mark its bar.
738 @param PciDevice Instance of Pci device.
740 @retval EFI_SUCCESS Successfully remove the PCI device.
741 @retval EFI_ABORTED Pci device is a root bridge or a PCI-PCI bridge.
746 IN PCI_IO_DEVICE
*PciDevice
749 PCI_IO_DEVICE
*Bridge
;
751 LIST_ENTRY
*CurrentLink
;
754 // Remove the padding resource from a bridge
756 if ( IS_PCI_BRIDGE(&PciDevice
->Pci
) &&
757 PciDevice
->ResourcePaddingDescriptors
!= NULL
) {
758 FreePool (PciDevice
->ResourcePaddingDescriptors
);
759 PciDevice
->ResourcePaddingDescriptors
= NULL
;
766 if (IS_PCI_BRIDGE (&PciDevice
->Pci
) || (PciDevice
->Parent
== NULL
)) {
770 if (IS_CARDBUS_BRIDGE (&PciDevice
->Pci
)) {
772 // Get the root bridge device
775 while (Bridge
->Parent
!= NULL
) {
776 Bridge
= Bridge
->Parent
;
779 RemoveAllPciDeviceOnBridge (Bridge
->Handle
, PciDevice
);
784 InitializeP2C (PciDevice
);
790 Bridge
= PciDevice
->Parent
;
791 CurrentLink
= Bridge
->ChildList
.ForwardLink
;
792 while (CurrentLink
!= NULL
&& CurrentLink
!= &Bridge
->ChildList
) {
793 Temp
= PCI_IO_DEVICE_FROM_LINK (CurrentLink
);
794 if (Temp
== PciDevice
) {
795 InitializePciDevice (Temp
);
796 RemoveEntryList (CurrentLink
);
797 FreePciDevice (Temp
);
801 CurrentLink
= CurrentLink
->ForwardLink
;
808 Determine whethter a PCI device can be rejected.
810 @param PciResNode Pointer to Pci resource node instance.
812 @retval TRUE The PCI device can be rejected.
813 @retval TRUE The PCI device cannot be rejected.
818 IN PCI_RESOURCE_NODE
*PciResNode
823 Temp
= PciResNode
->PciDev
;
826 // Ensure the device is present
833 // PPB and RB should go ahead
835 if (IS_PCI_BRIDGE (&Temp
->Pci
) || (Temp
->Parent
== NULL
)) {
840 // Skip device on Bus0
842 if ((Temp
->Parent
!= NULL
) && (Temp
->BusNumber
== 0)) {
849 if (IS_PCI_VGA (&Temp
->Pci
)) {
857 Compare two resource nodes and get the larger resource consumer.
859 @param PciResNode1 resource node 1 want to be compared
860 @param PciResNode2 resource node 2 want to be compared
862 @return Larger resource node.
866 GetLargerConsumerDevice (
867 IN PCI_RESOURCE_NODE
*PciResNode1
,
868 IN PCI_RESOURCE_NODE
*PciResNode2
871 if (PciResNode2
== NULL
) {
875 if ((IS_PCI_BRIDGE(&(PciResNode2
->PciDev
->Pci
)) || (PciResNode2
->PciDev
->Parent
== NULL
)) \
876 && (PciResNode2
->ResourceUsage
!= PciResUsagePadding
) )
881 if (PciResNode1
== NULL
) {
885 if ((PciResNode1
->Length
) > (PciResNode2
->Length
)) {
894 Get the max resource consumer in the host resource pool.
896 @param ResPool Pointer to resource pool node.
898 @return The max resource consumer in the host resource pool.
902 GetMaxResourceConsumerDevice (
903 IN PCI_RESOURCE_NODE
*ResPool
906 PCI_RESOURCE_NODE
*Temp
;
907 LIST_ENTRY
*CurrentLink
;
908 PCI_RESOURCE_NODE
*PciResNode
;
909 PCI_RESOURCE_NODE
*PPBResNode
;
913 CurrentLink
= ResPool
->ChildList
.ForwardLink
;
914 while (CurrentLink
!= NULL
&& CurrentLink
!= &ResPool
->ChildList
) {
916 Temp
= RESOURCE_NODE_FROM_LINK (CurrentLink
);
918 if (!IsRejectiveDevice (Temp
)) {
919 CurrentLink
= CurrentLink
->ForwardLink
;
923 if ((IS_PCI_BRIDGE (&(Temp
->PciDev
->Pci
)) || (Temp
->PciDev
->Parent
== NULL
)) \
924 && (Temp
->ResourceUsage
!= PciResUsagePadding
))
926 PPBResNode
= GetMaxResourceConsumerDevice (Temp
);
927 PciResNode
= GetLargerConsumerDevice (PciResNode
, PPBResNode
);
929 PciResNode
= GetLargerConsumerDevice (PciResNode
, Temp
);
932 CurrentLink
= CurrentLink
->ForwardLink
;
939 Adjust host bridge allocation so as to reduce resource requirement
941 @param IoPool Pointer to instance of I/O resource Node.
942 @param Mem32Pool Pointer to instance of 32-bit memory resource Node.
943 @param PMem32Pool Pointer to instance of 32-bit Prefetchable memory resource node.
944 @param Mem64Pool Pointer to instance of 64-bit memory resource node.
945 @param PMem64Pool Pointer to instance of 64-bit Prefetchable memory resource node.
946 @param IoResStatus Status of I/O resource Node.
947 @param Mem32ResStatus Status of 32-bit memory resource Node.
948 @param PMem32ResStatus Status of 32-bit Prefetchable memory resource node.
949 @param Mem64ResStatus Status of 64-bit memory resource node.
950 @param PMem64ResStatus Status of 64-bit Prefetchable memory resource node.
952 @retval EFI_SUCCESS Successfully adjusted resoruce on host bridge.
953 @retval EFI_ABORTED Host bridge hasn't this resource type or no resource be adjusted.
957 PciHostBridgeAdjustAllocation (
958 IN PCI_RESOURCE_NODE
*IoPool
,
959 IN PCI_RESOURCE_NODE
*Mem32Pool
,
960 IN PCI_RESOURCE_NODE
*PMem32Pool
,
961 IN PCI_RESOURCE_NODE
*Mem64Pool
,
962 IN PCI_RESOURCE_NODE
*PMem64Pool
,
963 IN UINT64 IoResStatus
,
964 IN UINT64 Mem32ResStatus
,
965 IN UINT64 PMem32ResStatus
,
966 IN UINT64 Mem64ResStatus
,
967 IN UINT64 PMem64ResStatus
970 BOOLEAN AllocationAjusted
;
971 PCI_RESOURCE_NODE
*PciResNode
;
972 PCI_RESOURCE_NODE
*ResPool
[5];
973 PCI_IO_DEVICE
*RemovedPciDev
[5];
975 UINTN RemovedPciDevNum
;
979 EFI_RESOURCE_ALLOC_FAILURE_ERROR_DATA_PAYLOAD AllocFailExtendedData
;
982 ZeroMem (RemovedPciDev
, 5 * sizeof (PCI_IO_DEVICE
*));
983 RemovedPciDevNum
= 0;
986 ResPool
[1] = Mem32Pool
;
987 ResPool
[2] = PMem32Pool
;
988 ResPool
[3] = Mem64Pool
;
989 ResPool
[4] = PMem64Pool
;
991 ResStatus
[0] = IoResStatus
;
992 ResStatus
[1] = Mem32ResStatus
;
993 ResStatus
[2] = PMem32ResStatus
;
994 ResStatus
[3] = Mem64ResStatus
;
995 ResStatus
[4] = PMem64ResStatus
;
997 AllocationAjusted
= FALSE
;
999 for (ResType
= 0; ResType
< 5; ResType
++) {
1001 if (ResStatus
[ResType
] == EFI_RESOURCE_SATISFIED
) {
1005 if (ResStatus
[ResType
] == EFI_RESOURCE_NOT_SATISFIED
) {
1007 // Host bridge hasn't this resource type
1013 // Hostbridge hasn't enough resource
1015 PciResNode
= GetMaxResourceConsumerDevice (ResPool
[ResType
]);
1016 if (PciResNode
== NULL
) {
1021 // Check if the device has been removed before
1023 for (DevIndex
= 0; DevIndex
< RemovedPciDevNum
; DevIndex
++) {
1024 if (PciResNode
->PciDev
== RemovedPciDev
[DevIndex
]) {
1029 if (DevIndex
!= RemovedPciDevNum
) {
1034 // Remove the device if it isn't in the array
1036 Status
= RejectPciDevice (PciResNode
->PciDev
);
1037 if (Status
== EFI_SUCCESS
) {
1040 // Raise the EFI_IOB_EC_RESOURCE_CONFLICT status code
1043 // Have no way to get ReqRes, AllocRes & Bar here
1045 ZeroMem (&AllocFailExtendedData
, sizeof (AllocFailExtendedData
));
1046 AllocFailExtendedData
.DevicePathSize
= (UINT16
) sizeof (EFI_DEVICE_PATH_PROTOCOL
);
1047 AllocFailExtendedData
.DevicePath
= (UINT8
*) PciResNode
->PciDev
->DevicePath
;
1048 AllocFailExtendedData
.Bar
= PciResNode
->Bar
;
1050 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
1052 EFI_IO_BUS_PCI
| EFI_IOB_EC_RESOURCE_CONFLICT
,
1053 (VOID
*) &AllocFailExtendedData
,
1054 sizeof (AllocFailExtendedData
)
1058 // Add it to the array and indicate at least a device has been rejected
1060 RemovedPciDev
[RemovedPciDevNum
++] = PciResNode
->PciDev
;
1061 AllocationAjusted
= TRUE
;
1068 if (AllocationAjusted
) {
1076 Summary requests for all resource type, and contruct ACPI resource
1079 @param Bridge detecting bridge
1080 @param IoNode Pointer to instance of I/O resource Node
1081 @param Mem32Node Pointer to instance of 32-bit memory resource Node
1082 @param PMem32Node Pointer to instance of 32-bit Pmemory resource node
1083 @param Mem64Node Pointer to instance of 64-bit memory resource node
1084 @param PMem64Node Pointer to instance of 64-bit Pmemory resource node
1085 @param Config Output buffer holding new constructed APCI resource requestor
1087 @retval EFI_SUCCESS Successfully constructed ACPI resource.
1088 @retval EFI_OUT_OF_RESOURCES No memory availabe.
1092 ConstructAcpiResourceRequestor (
1093 IN PCI_IO_DEVICE
*Bridge
,
1094 IN PCI_RESOURCE_NODE
*IoNode
,
1095 IN PCI_RESOURCE_NODE
*Mem32Node
,
1096 IN PCI_RESOURCE_NODE
*PMem32Node
,
1097 IN PCI_RESOURCE_NODE
*Mem64Node
,
1098 IN PCI_RESOURCE_NODE
*PMem64Node
,
1104 UINT8
*Configuration
;
1105 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Ptr
;
1106 EFI_ACPI_END_TAG_DESCRIPTOR
*PtrEnd
;
1114 // if there is io request, add to the io aperture
1116 if (ResourceRequestExisted (IoNode
)) {
1122 // if there is mem32 request, add to the mem32 aperture
1124 if (ResourceRequestExisted (Mem32Node
)) {
1130 // if there is pmem32 request, add to the pmem32 aperture
1132 if (ResourceRequestExisted (PMem32Node
)) {
1138 // if there is mem64 request, add to the mem64 aperture
1140 if (ResourceRequestExisted (Mem64Node
)) {
1146 // if there is pmem64 request, add to the pmem64 aperture
1148 if (ResourceRequestExisted (PMem64Node
)) {
1153 if (NumConfig
!= 0) {
1156 // If there is at least one type of resource request,
1157 // allocate a acpi resource node
1159 Configuration
= AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) * NumConfig
+ sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
));
1160 if (Configuration
== NULL
) {
1161 return EFI_OUT_OF_RESOURCES
;
1164 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
1167 // Deal with io aperture
1169 if ((Aperture
& 0x01) != 0) {
1170 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1171 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1175 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_IO
;
1179 Ptr
->SpecificFlag
= 1;
1180 Ptr
->AddrLen
= IoNode
->Length
;
1181 Ptr
->AddrRangeMax
= IoNode
->Alignment
;
1186 // Deal with mem32 aperture
1188 if ((Aperture
& 0x02) != 0) {
1189 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1190 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1194 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1198 Ptr
->SpecificFlag
= 0;
1202 Ptr
->AddrSpaceGranularity
= 32;
1203 Ptr
->AddrLen
= Mem32Node
->Length
;
1204 Ptr
->AddrRangeMax
= Mem32Node
->Alignment
;
1210 // Deal with Pmem32 aperture
1212 if ((Aperture
& 0x04) != 0) {
1213 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1214 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1218 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1222 Ptr
->SpecificFlag
= 0x6;
1226 Ptr
->AddrSpaceGranularity
= 32;
1227 Ptr
->AddrLen
= PMem32Node
->Length
;
1228 Ptr
->AddrRangeMax
= PMem32Node
->Alignment
;
1233 // Deal with mem64 aperture
1235 if ((Aperture
& 0x08) != 0) {
1236 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1237 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1241 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1245 Ptr
->SpecificFlag
= 0;
1249 Ptr
->AddrSpaceGranularity
= 64;
1250 Ptr
->AddrLen
= Mem64Node
->Length
;
1251 Ptr
->AddrRangeMax
= Mem64Node
->Alignment
;
1256 // Deal with Pmem64 aperture
1258 if ((Aperture
& 0x10) != 0) {
1259 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1260 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1264 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1268 Ptr
->SpecificFlag
= 0x06;
1272 Ptr
->AddrSpaceGranularity
= 64;
1273 Ptr
->AddrLen
= PMem64Node
->Length
;
1274 Ptr
->AddrRangeMax
= PMem64Node
->Alignment
;
1282 PtrEnd
= (EFI_ACPI_END_TAG_DESCRIPTOR
*) Ptr
;
1284 PtrEnd
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
1285 PtrEnd
->Checksum
= 0;
1290 // If there is no resource request
1292 Configuration
= AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
));
1293 if (Configuration
== NULL
) {
1294 return EFI_OUT_OF_RESOURCES
;
1297 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) (Configuration
);
1298 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1300 PtrEnd
= (EFI_ACPI_END_TAG_DESCRIPTOR
*) (Ptr
+ 1);
1301 PtrEnd
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
1302 PtrEnd
->Checksum
= 0;
1305 *Config
= Configuration
;
1311 Get resource base from an acpi configuration descriptor.
1313 @param Config An acpi configuration descriptor.
1314 @param IoBase Output of I/O resource base address.
1315 @param Mem32Base Output of 32-bit memory base address.
1316 @param PMem32Base Output of 32-bit prefetchable memory base address.
1317 @param Mem64Base Output of 64-bit memory base address.
1318 @param PMem64Base Output of 64-bit prefetchable memory base address.
1325 OUT UINT64
*Mem32Base
,
1326 OUT UINT64
*PMem32Base
,
1327 OUT UINT64
*Mem64Base
,
1328 OUT UINT64
*PMem64Base
1332 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Ptr
;
1335 ASSERT (Config
!= NULL
);
1337 *IoBase
= 0xFFFFFFFFFFFFFFFFULL
;
1338 *Mem32Base
= 0xFFFFFFFFFFFFFFFFULL
;
1339 *PMem32Base
= 0xFFFFFFFFFFFFFFFFULL
;
1340 *Mem64Base
= 0xFFFFFFFFFFFFFFFFULL
;
1341 *PMem64Base
= 0xFFFFFFFFFFFFFFFFULL
;
1343 Temp
= (UINT8
*) Config
;
1345 while (*Temp
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
1347 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Temp
;
1348 ResStatus
= Ptr
->AddrTranslationOffset
;
1350 if (ResStatus
== EFI_RESOURCE_SATISFIED
) {
1352 switch (Ptr
->ResType
) {
1355 // Memory type aperture
1360 // Check to see the granularity
1362 if (Ptr
->AddrSpaceGranularity
== 32) {
1363 if ((Ptr
->SpecificFlag
& 0x06) != 0) {
1364 *PMem32Base
= Ptr
->AddrRangeMin
;
1366 *Mem32Base
= Ptr
->AddrRangeMin
;
1370 if (Ptr
->AddrSpaceGranularity
== 64) {
1371 if ((Ptr
->SpecificFlag
& 0x06) != 0) {
1372 *PMem64Base
= Ptr
->AddrRangeMin
;
1374 *Mem64Base
= Ptr
->AddrRangeMin
;
1384 *IoBase
= Ptr
->AddrRangeMin
;
1398 Temp
+= sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
);
1403 Enumerate pci bridge, allocate resource and determine attribute
1404 for devices on this bridge.
1406 @param BridgeDev Pointer to instance of bridge device.
1408 @retval EFI_SUCCESS Successfully enumerated PCI bridge.
1409 @retval other Failed to enumerate.
1413 PciBridgeEnumerator (
1414 IN PCI_IO_DEVICE
*BridgeDev
1418 UINT8 StartBusNumber
;
1419 EFI_PCI_IO_PROTOCOL
*PciIo
;
1424 PciIo
= &(BridgeDev
->PciIo
);
1425 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x19, 1, &StartBusNumber
);
1427 if (EFI_ERROR (Status
)) {
1431 Status
= PciAssignBusNumber (
1437 if (EFI_ERROR (Status
)) {
1441 Status
= PciPciDeviceInfoCollector (BridgeDev
, StartBusNumber
);
1443 if (EFI_ERROR (Status
)) {
1447 Status
= PciBridgeResourceAllocator (BridgeDev
);
1449 if (EFI_ERROR (Status
)) {
1453 Status
= DetermineDeviceAttribute (BridgeDev
);
1455 if (EFI_ERROR (Status
)) {
1464 Allocate all kinds of resource for PCI bridge.
1466 @param Bridge Pointer to bridge instance.
1468 @retval EFI_SUCCESS Successfully allocated resource for PCI bridge.
1469 @retval other Failed to allocate resource for bridge.
1473 PciBridgeResourceAllocator (
1474 IN PCI_IO_DEVICE
*Bridge
1477 PCI_RESOURCE_NODE
*IoBridge
;
1478 PCI_RESOURCE_NODE
*Mem32Bridge
;
1479 PCI_RESOURCE_NODE
*PMem32Bridge
;
1480 PCI_RESOURCE_NODE
*Mem64Bridge
;
1481 PCI_RESOURCE_NODE
*PMem64Bridge
;
1489 IoBridge
= CreateResourceNode (
1492 Bridge
->BridgeIoAlignment
,
1498 Mem32Bridge
= CreateResourceNode (
1507 PMem32Bridge
= CreateResourceNode (
1516 Mem64Bridge
= CreateResourceNode (
1525 PMem64Bridge
= CreateResourceNode (
1535 // Create resourcemap by going through all the devices subject to this root bridge
1546 Status
= GetResourceBaseFromBridge (
1555 if (EFI_ERROR (Status
)) {
1560 // Program IO resources
1568 // Program Mem32 resources
1576 // Program PMem32 resources
1584 // Program Mem64 resources
1592 // Program PMem64 resources
1599 DestroyResourceTree (IoBridge
);
1600 DestroyResourceTree (Mem32Bridge
);
1601 DestroyResourceTree (PMem32Bridge
);
1602 DestroyResourceTree (PMem64Bridge
);
1603 DestroyResourceTree (Mem64Bridge
);
1605 gBS
->FreePool (IoBridge
);
1606 gBS
->FreePool (Mem32Bridge
);
1607 gBS
->FreePool (PMem32Bridge
);
1608 gBS
->FreePool (PMem64Bridge
);
1609 gBS
->FreePool (Mem64Bridge
);
1615 Get resource base address for a pci bridge device.
1617 @param Bridge Given Pci driver instance.
1618 @param IoBase Output for base address of I/O type resource.
1619 @param Mem32Base Output for base address of 32-bit memory type resource.
1620 @param PMem32Base Ooutput for base address of 32-bit Pmemory type resource.
1621 @param Mem64Base Output for base address of 64-bit memory type resource.
1622 @param PMem64Base Output for base address of 64-bit Pmemory type resource.
1624 @retval EFI_SUCCESS Successfully got resource base address.
1625 @retval EFI_OUT_OF_RESOURCES PCI bridge is not available.
1629 GetResourceBaseFromBridge (
1630 IN PCI_IO_DEVICE
*Bridge
,
1632 OUT UINT64
*Mem32Base
,
1633 OUT UINT64
*PMem32Base
,
1634 OUT UINT64
*Mem64Base
,
1635 OUT UINT64
*PMem64Base
1638 if (!Bridge
->Allocated
) {
1639 return EFI_OUT_OF_RESOURCES
;
1643 *Mem32Base
= gAllOne
;
1644 *PMem32Base
= gAllOne
;
1645 *Mem64Base
= gAllOne
;
1646 *PMem64Base
= gAllOne
;
1648 if (IS_PCI_BRIDGE (&Bridge
->Pci
)) {
1650 if (Bridge
->PciBar
[PPB_IO_RANGE
].Length
> 0) {
1651 *IoBase
= Bridge
->PciBar
[PPB_IO_RANGE
].BaseAddress
;
1654 if (Bridge
->PciBar
[PPB_MEM32_RANGE
].Length
> 0) {
1655 *Mem32Base
= Bridge
->PciBar
[PPB_MEM32_RANGE
].BaseAddress
;
1658 if (Bridge
->PciBar
[PPB_PMEM32_RANGE
].Length
> 0) {
1659 *PMem32Base
= Bridge
->PciBar
[PPB_PMEM32_RANGE
].BaseAddress
;
1662 if (Bridge
->PciBar
[PPB_PMEM64_RANGE
].Length
> 0) {
1663 *PMem64Base
= Bridge
->PciBar
[PPB_PMEM64_RANGE
].BaseAddress
;
1665 *PMem64Base
= gAllOne
;
1670 if (IS_CARDBUS_BRIDGE (&Bridge
->Pci
)) {
1671 if (Bridge
->PciBar
[P2C_IO_1
].Length
> 0) {
1672 *IoBase
= Bridge
->PciBar
[P2C_IO_1
].BaseAddress
;
1674 if (Bridge
->PciBar
[P2C_IO_2
].Length
> 0) {
1675 *IoBase
= Bridge
->PciBar
[P2C_IO_2
].BaseAddress
;
1679 if (Bridge
->PciBar
[P2C_MEM_1
].Length
> 0) {
1680 if (Bridge
->PciBar
[P2C_MEM_1
].BarType
== PciBarTypePMem32
) {
1681 *PMem32Base
= Bridge
->PciBar
[P2C_MEM_1
].BaseAddress
;
1684 if (Bridge
->PciBar
[P2C_MEM_1
].BarType
== PciBarTypeMem32
) {
1685 *Mem32Base
= Bridge
->PciBar
[P2C_MEM_1
].BaseAddress
;
1689 if (Bridge
->PciBar
[P2C_MEM_2
].Length
> 0) {
1690 if (Bridge
->PciBar
[P2C_MEM_2
].BarType
== PciBarTypePMem32
) {
1691 *PMem32Base
= Bridge
->PciBar
[P2C_MEM_2
].BaseAddress
;
1694 if (Bridge
->PciBar
[P2C_MEM_2
].BarType
== PciBarTypeMem32
) {
1695 *Mem32Base
= Bridge
->PciBar
[P2C_MEM_2
].BaseAddress
;
1704 These are the notifications from the PCI bus driver that it is about to enter a certain
1705 phase of the PCI enumeration process.
1707 This member function can be used to notify the host bridge driver to perform specific actions,
1708 including any chipset-specific initialization, so that the chipset is ready to enter the next phase.
1709 Eight notification points are defined at this time. See belows:
1710 EfiPciHostBridgeBeginEnumeration Resets the host bridge PCI apertures and internal data
1711 structures. The PCI enumerator should issue this notification
1712 before starting a fresh enumeration process. Enumeration cannot
1713 be restarted after sending any other notification such as
1714 EfiPciHostBridgeBeginBusAllocation.
1715 EfiPciHostBridgeBeginBusAllocation The bus allocation phase is about to begin. No specific action is
1716 required here. This notification can be used to perform any
1717 chipset-specific programming.
1718 EfiPciHostBridgeEndBusAllocation The bus allocation and bus programming phase is complete. No
1719 specific action is required here. This notification can be used to
1720 perform any chipset-specific programming.
1721 EfiPciHostBridgeBeginResourceAllocation
1722 The resource allocation phase is about to begin. No specific
1723 action is required here. This notification can be used to perform
1724 any chipset-specific programming.
1725 EfiPciHostBridgeAllocateResources Allocates resources per previously submitted requests for all the PCI
1726 root bridges. These resource settings are returned on the next call to
1727 GetProposedResources(). Before calling NotifyPhase() with a Phase of
1728 EfiPciHostBridgeAllocateResource, the PCI bus enumerator is responsible
1729 for gathering I/O and memory requests for
1730 all the PCI root bridges and submitting these requests using
1731 SubmitResources(). This function pads the resource amount
1732 to suit the root bridge hardware, takes care of dependencies between
1733 the PCI root bridges, and calls the Global Coherency Domain (GCD)
1734 with the allocation request. In the case of padding, the allocated range
1735 could be bigger than what was requested.
1736 EfiPciHostBridgeSetResources Programs the host bridge hardware to decode previously allocated
1737 resources (proposed resources) for all the PCI root bridges. After the
1738 hardware is programmed, reassigning resources will not be supported.
1739 The bus settings are not affected.
1740 EfiPciHostBridgeFreeResources Deallocates resources that were previously allocated for all the PCI
1741 root bridges and resets the I/O and memory apertures to their initial
1742 state. The bus settings are not affected. If the request to allocate
1743 resources fails, the PCI enumerator can use this notification to
1744 deallocate previous resources, adjust the requests, and retry
1746 EfiPciHostBridgeEndResourceAllocation The resource allocation phase is completed. No specific action is
1747 required here. This notification can be used to perform any chipsetspecific
1750 @param[in] PciResAlloc The instance pointer of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
1751 @param[in] Phase The phase during enumeration
1753 @retval EFI_NOT_READY This phase cannot be entered at this time. For example, this error
1754 is valid for a Phase of EfiPciHostBridgeAllocateResources if
1755 SubmitResources() has not been called for one or more
1756 PCI root bridges before this call
1757 @retval EFI_DEVICE_ERROR Programming failed due to a hardware error. This error is valid
1758 for a Phase of EfiPciHostBridgeSetResources.
1759 @retval EFI_INVALID_PARAMETER Invalid phase parameter
1760 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1761 This error is valid for a Phase of EfiPciHostBridgeAllocateResources if the
1762 previously submitted resource requests cannot be fulfilled or
1763 were only partially fulfilled.
1764 @retval EFI_SUCCESS The notification was accepted without any errors.
1769 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
,
1770 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE Phase
1773 EFI_HANDLE HostBridgeHandle
;
1774 EFI_HANDLE RootBridgeHandle
;
1775 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
1778 HostBridgeHandle
= NULL
;
1779 RootBridgeHandle
= NULL
;
1780 if (gPciPlatformProtocol
!= NULL
) {
1782 // Get Host Bridge Handle.
1784 PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
);
1787 // Get the rootbridge Io protocol to find the host bridge handle
1789 Status
= gBS
->HandleProtocol (
1791 &gEfiPciRootBridgeIoProtocolGuid
,
1792 (VOID
**) &PciRootBridgeIo
1795 if (EFI_ERROR (Status
)) {
1796 return EFI_NOT_FOUND
;
1799 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
1802 // Call PlatformPci::PlatformNotify() if the protocol is present.
1804 gPciPlatformProtocol
->PlatformNotify (
1805 gPciPlatformProtocol
,
1810 } else if (gPciOverrideProtocol
!= NULL
){
1812 // Get Host Bridge Handle.
1814 PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
);
1817 // Get the rootbridge Io protocol to find the host bridge handle
1819 Status
= gBS
->HandleProtocol (
1821 &gEfiPciRootBridgeIoProtocolGuid
,
1822 (VOID
**) &PciRootBridgeIo
1825 if (EFI_ERROR (Status
)) {
1826 return EFI_NOT_FOUND
;
1829 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
1832 // Call PlatformPci::PhaseNotify() if the protocol is present.
1834 gPciOverrideProtocol
->PlatformNotify (
1835 gPciOverrideProtocol
,
1842 Status
= PciResAlloc
->NotifyPhase (
1847 if (gPciPlatformProtocol
!= NULL
) {
1849 // Call PlatformPci::PlatformNotify() if the protocol is present.
1851 gPciPlatformProtocol
->PlatformNotify (
1852 gPciPlatformProtocol
,
1858 } else if (gPciOverrideProtocol
!= NULL
) {
1860 // Call PlatformPci::PhaseNotify() if the protocol is present.
1862 gPciOverrideProtocol
->PlatformNotify (
1863 gPciOverrideProtocol
,
1874 Provides the hooks from the PCI bus driver to every PCI controller (device/function) at various
1875 stages of the PCI enumeration process that allow the host bridge driver to preinitialize individual
1876 PCI controllers before enumeration.
1878 This function is called during the PCI enumeration process. No specific action is expected from this
1879 member function. It allows the host bridge driver to preinitialize individual PCI controllers before
1882 @param Bridge Pointer to the EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL instance.
1883 @param Bus The bus number of the pci device.
1884 @param Device The device number of the pci device.
1885 @param Func The function number of the pci device.
1886 @param Phase The phase of the PCI device enumeration.
1888 @retval EFI_SUCCESS The requested parameters were returned.
1889 @retval EFI_INVALID_PARAMETER RootBridgeHandle is not a valid root bridge handle.
1890 @retval EFI_INVALID_PARAMETER Phase is not a valid phase that is defined in
1891 EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE.
1892 @retval EFI_DEVICE_ERROR Programming failed due to a hardware error. The PCI enumerator should
1893 not enumerate this device, including its child devices if it is a PCI-to-PCI
1898 PreprocessController (
1899 IN PCI_IO_DEVICE
*Bridge
,
1903 IN EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE Phase
1906 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS RootBridgePciAddress
;
1907 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
;
1908 EFI_HANDLE RootBridgeHandle
;
1909 EFI_HANDLE HostBridgeHandle
;
1913 // Get the host bridge handle
1915 HostBridgeHandle
= Bridge
->PciRootBridgeIo
->ParentHandle
;
1918 // Get the pci host bridge resource allocation protocol
1920 Status
= gBS
->OpenProtocol (
1922 &gEfiPciHostBridgeResourceAllocationProtocolGuid
,
1923 (VOID
**) &PciResAlloc
,
1926 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1929 if (EFI_ERROR (Status
)) {
1930 return EFI_UNSUPPORTED
;
1934 // Get Root Brige Handle
1936 while (Bridge
->Parent
!= NULL
) {
1937 Bridge
= Bridge
->Parent
;
1940 RootBridgeHandle
= Bridge
->Handle
;
1942 RootBridgePciAddress
.Register
= 0;
1943 RootBridgePciAddress
.Function
= Func
;
1944 RootBridgePciAddress
.Device
= Device
;
1945 RootBridgePciAddress
.Bus
= Bus
;
1946 RootBridgePciAddress
.ExtendedRegister
= 0;
1948 if (gPciPlatformProtocol
!= NULL
) {
1950 // Call PlatformPci::PrepController() if the protocol is present.
1952 gPciPlatformProtocol
->PlatformPrepController (
1953 gPciPlatformProtocol
,
1956 RootBridgePciAddress
,
1960 } else if (gPciOverrideProtocol
!= NULL
) {
1962 // Call PlatformPci::PrepController() if the protocol is present.
1964 gPciOverrideProtocol
->PlatformPrepController (
1965 gPciOverrideProtocol
,
1968 RootBridgePciAddress
,
1974 Status
= PciResAlloc
->PreprocessController (
1977 RootBridgePciAddress
,
1981 if (gPciPlatformProtocol
!= NULL
) {
1983 // Call PlatformPci::PrepController() if the protocol is present.
1985 gPciPlatformProtocol
->PlatformPrepController (
1986 gPciPlatformProtocol
,
1989 RootBridgePciAddress
,
1993 } else if (gPciOverrideProtocol
!= NULL
) {
1995 // Call PlatformPci::PrepController() if the protocol is present.
1997 gPciOverrideProtocol
->PlatformPrepController (
1998 gPciOverrideProtocol
,
2001 RootBridgePciAddress
,
2011 This function allows the PCI bus driver to be notified to act as requested when a hot-plug event has
2012 happened on the hot-plug controller. Currently, the operations include add operation and remove operation..
2014 @param This A pointer to the hot plug request protocol.
2015 @param Operation The operation the PCI bus driver is requested to make.
2016 @param Controller The handle of the hot-plug controller.
2017 @param RemainingDevicePath The remaining device path for the PCI-like hot-plug device.
2018 @param NumberOfChildren The number of child handles.
2019 For a add operation, it is an output parameter.
2020 For a remove operation, it's an input parameter.
2021 @param ChildHandleBuffer The buffer which contains the child handles.
2023 @retval EFI_INVALID_PARAMETER Operation is not a legal value.
2024 Controller is NULL or not a valid handle.
2025 NumberOfChildren is NULL.
2026 ChildHandleBuffer is NULL while Operation is add.
2027 @retval EFI_OUT_OF_RESOURCES There are no enough resources to start the devices.
2028 @retval EFI_NOT_FOUND Can not find bridge according to controller handle.
2029 @retval EFI_SUCCESS The handles for the specified device have been created or destroyed
2030 as requested, and for an add operation, the new handles are
2031 returned in ChildHandleBuffer.
2035 PciHotPlugRequestNotify (
2036 IN EFI_PCI_HOTPLUG_REQUEST_PROTOCOL
* This
,
2037 IN EFI_PCI_HOTPLUG_OPERATION Operation
,
2038 IN EFI_HANDLE Controller
,
2039 IN EFI_DEVICE_PATH_PROTOCOL
* RemainingDevicePath OPTIONAL
,
2040 IN OUT UINT8
*NumberOfChildren
,
2041 IN OUT EFI_HANDLE
* ChildHandleBuffer
2044 PCI_IO_DEVICE
*Bridge
;
2045 PCI_IO_DEVICE
*Temp
;
2046 EFI_PCI_IO_PROTOCOL
*PciIo
;
2048 EFI_HANDLE RootBridgeHandle
;
2052 // Check input parameter validity
2054 if ((Controller
== NULL
) || (NumberOfChildren
== NULL
)){
2055 return EFI_INVALID_PARAMETER
;
2058 if ((Operation
!= EfiPciHotPlugRequestAdd
) && (Operation
!= EfiPciHotplugRequestRemove
)) {
2059 return EFI_INVALID_PARAMETER
;
2062 if (Operation
== EfiPciHotPlugRequestAdd
){
2063 if (ChildHandleBuffer
== NULL
) {
2064 return EFI_INVALID_PARAMETER
;
2066 } else if ((Operation
== EfiPciHotplugRequestRemove
) && (*NumberOfChildren
!= 0)) {
2067 if (ChildHandleBuffer
== NULL
) {
2068 return EFI_INVALID_PARAMETER
;
2072 Status
= gBS
->OpenProtocol (
2074 &gEfiPciIoProtocolGuid
,
2076 gPciBusDriverBinding
.DriverBindingHandle
,
2078 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2081 if (EFI_ERROR (Status
)) {
2082 return EFI_NOT_FOUND
;
2085 Bridge
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (PciIo
);
2088 // Get root bridge handle
2091 while (Temp
->Parent
!= NULL
) {
2092 Temp
= Temp
->Parent
;
2095 RootBridgeHandle
= Temp
->Handle
;
2097 if (Operation
== EfiPciHotPlugRequestAdd
) {
2099 // Report Status Code to indicate hot plug happens
2101 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
2103 (EFI_IO_BUS_PCI
| EFI_IOB_PC_HOTPLUG
),
2107 if (NumberOfChildren
!= NULL
) {
2108 *NumberOfChildren
= 0;
2111 if (IsListEmpty (&Bridge
->ChildList
)) {
2113 Status
= PciBridgeEnumerator (Bridge
);
2115 if (EFI_ERROR (Status
)) {
2120 Status
= StartPciDevicesOnBridge (
2123 RemainingDevicePath
,
2131 if (Operation
== EfiPciHotplugRequestRemove
) {
2133 if (*NumberOfChildren
== 0) {
2135 // Remove all devices on the bridge
2137 RemoveAllPciDeviceOnBridge (RootBridgeHandle
, Bridge
);
2142 for (Index
= 0; Index
< *NumberOfChildren
; Index
++) {
2144 // De register all the pci device
2146 Status
= DeRegisterPciDevice (RootBridgeHandle
, ChildHandleBuffer
[Index
]);
2148 if (EFI_ERROR (Status
)) {
2163 Search hostbridge according to given handle
2165 @param RootBridgeHandle Host bridge handle.
2167 @retval TRUE Found host bridge handle.
2168 @retval FALSE Not found hot bridge handle.
2172 SearchHostBridgeHandle (
2173 IN EFI_HANDLE RootBridgeHandle
2176 EFI_HANDLE HostBridgeHandle
;
2177 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
2182 // Get the rootbridge Io protocol to find the host bridge handle
2184 Status
= gBS
->OpenProtocol (
2186 &gEfiPciRootBridgeIoProtocolGuid
,
2187 (VOID
**) &PciRootBridgeIo
,
2188 gPciBusDriverBinding
.DriverBindingHandle
,
2190 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2193 if (EFI_ERROR (Status
)) {
2197 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
2198 for (Index
= 0; Index
< gPciHostBridgeNumber
; Index
++) {
2199 if (HostBridgeHandle
== gPciHostBrigeHandles
[Index
]) {
2208 Add host bridge handle to global variable for enumerating.
2210 @param HostBridgeHandle Host bridge handle.
2212 @retval EFI_SUCCESS Successfully added host bridge.
2213 @retval EFI_ABORTED Host bridge is NULL, or given host bridge
2214 has been in host bridge list.
2218 AddHostBridgeEnumerator (
2219 IN EFI_HANDLE HostBridgeHandle
2224 if (HostBridgeHandle
== NULL
) {
2228 for (Index
= 0; Index
< gPciHostBridgeNumber
; Index
++) {
2229 if (HostBridgeHandle
== gPciHostBrigeHandles
[Index
]) {
2234 if (Index
< PCI_MAX_HOST_BRIDGE_NUM
) {
2235 gPciHostBrigeHandles
[Index
] = HostBridgeHandle
;
2236 gPciHostBridgeNumber
++;