2 PCI eunmeration implementation on entire PCI bus system for PCI Bus module.
4 Copyright (c) 2006 - 2013, 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 "PciBus: [%02x|%02x|%02x] was rejected due to resource confliction.\n",
1041 PciResNode
->PciDev
->BusNumber
, PciResNode
->PciDev
->DeviceNumber
, PciResNode
->PciDev
->FunctionNumber
1045 // Raise the EFI_IOB_EC_RESOURCE_CONFLICT status code
1048 // Have no way to get ReqRes, AllocRes & Bar here
1050 ZeroMem (&AllocFailExtendedData
, sizeof (AllocFailExtendedData
));
1051 AllocFailExtendedData
.DevicePathSize
= (UINT16
) sizeof (EFI_DEVICE_PATH_PROTOCOL
);
1052 AllocFailExtendedData
.DevicePath
= (UINT8
*) PciResNode
->PciDev
->DevicePath
;
1053 AllocFailExtendedData
.Bar
= PciResNode
->Bar
;
1055 REPORT_STATUS_CODE_WITH_EXTENDED_DATA (
1057 EFI_IO_BUS_PCI
| EFI_IOB_EC_RESOURCE_CONFLICT
,
1058 (VOID
*) &AllocFailExtendedData
,
1059 sizeof (AllocFailExtendedData
)
1063 // Add it to the array and indicate at least a device has been rejected
1065 RemovedPciDev
[RemovedPciDevNum
++] = PciResNode
->PciDev
;
1066 AllocationAjusted
= TRUE
;
1073 if (AllocationAjusted
) {
1081 Summary requests for all resource type, and contruct ACPI resource
1084 @param Bridge detecting bridge
1085 @param IoNode Pointer to instance of I/O resource Node
1086 @param Mem32Node Pointer to instance of 32-bit memory resource Node
1087 @param PMem32Node Pointer to instance of 32-bit Pmemory resource node
1088 @param Mem64Node Pointer to instance of 64-bit memory resource node
1089 @param PMem64Node Pointer to instance of 64-bit Pmemory resource node
1090 @param Config Output buffer holding new constructed APCI resource requestor
1092 @retval EFI_SUCCESS Successfully constructed ACPI resource.
1093 @retval EFI_OUT_OF_RESOURCES No memory availabe.
1097 ConstructAcpiResourceRequestor (
1098 IN PCI_IO_DEVICE
*Bridge
,
1099 IN PCI_RESOURCE_NODE
*IoNode
,
1100 IN PCI_RESOURCE_NODE
*Mem32Node
,
1101 IN PCI_RESOURCE_NODE
*PMem32Node
,
1102 IN PCI_RESOURCE_NODE
*Mem64Node
,
1103 IN PCI_RESOURCE_NODE
*PMem64Node
,
1109 UINT8
*Configuration
;
1110 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Ptr
;
1111 EFI_ACPI_END_TAG_DESCRIPTOR
*PtrEnd
;
1119 // if there is io request, add to the io aperture
1121 if (ResourceRequestExisted (IoNode
)) {
1127 // if there is mem32 request, add to the mem32 aperture
1129 if (ResourceRequestExisted (Mem32Node
)) {
1135 // if there is pmem32 request, add to the pmem32 aperture
1137 if (ResourceRequestExisted (PMem32Node
)) {
1143 // if there is mem64 request, add to the mem64 aperture
1145 if (ResourceRequestExisted (Mem64Node
)) {
1151 // if there is pmem64 request, add to the pmem64 aperture
1153 if (ResourceRequestExisted (PMem64Node
)) {
1158 if (NumConfig
!= 0) {
1161 // If there is at least one type of resource request,
1162 // allocate a acpi resource node
1164 Configuration
= AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) * NumConfig
+ sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
));
1165 if (Configuration
== NULL
) {
1166 return EFI_OUT_OF_RESOURCES
;
1169 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Configuration
;
1172 // Deal with io aperture
1174 if ((Aperture
& 0x01) != 0) {
1175 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1176 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1180 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_IO
;
1184 Ptr
->SpecificFlag
= 1;
1185 Ptr
->AddrLen
= IoNode
->Length
;
1186 Ptr
->AddrRangeMax
= IoNode
->Alignment
;
1191 // Deal with mem32 aperture
1193 if ((Aperture
& 0x02) != 0) {
1194 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1195 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1199 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1203 Ptr
->SpecificFlag
= 0;
1207 Ptr
->AddrSpaceGranularity
= 32;
1208 Ptr
->AddrLen
= Mem32Node
->Length
;
1209 Ptr
->AddrRangeMax
= Mem32Node
->Alignment
;
1215 // Deal with Pmem32 aperture
1217 if ((Aperture
& 0x04) != 0) {
1218 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1219 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1223 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1227 Ptr
->SpecificFlag
= 0x6;
1231 Ptr
->AddrSpaceGranularity
= 32;
1232 Ptr
->AddrLen
= PMem32Node
->Length
;
1233 Ptr
->AddrRangeMax
= PMem32Node
->Alignment
;
1238 // Deal with mem64 aperture
1240 if ((Aperture
& 0x08) != 0) {
1241 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1242 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1246 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1250 Ptr
->SpecificFlag
= 0;
1254 Ptr
->AddrSpaceGranularity
= 64;
1255 Ptr
->AddrLen
= Mem64Node
->Length
;
1256 Ptr
->AddrRangeMax
= Mem64Node
->Alignment
;
1261 // Deal with Pmem64 aperture
1263 if ((Aperture
& 0x10) != 0) {
1264 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1265 Ptr
->Len
= (UINT16
) (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) - 3);
1269 Ptr
->ResType
= ACPI_ADDRESS_SPACE_TYPE_MEM
;
1273 Ptr
->SpecificFlag
= 0x06;
1277 Ptr
->AddrSpaceGranularity
= 64;
1278 Ptr
->AddrLen
= PMem64Node
->Length
;
1279 Ptr
->AddrRangeMax
= PMem64Node
->Alignment
;
1287 PtrEnd
= (EFI_ACPI_END_TAG_DESCRIPTOR
*) Ptr
;
1289 PtrEnd
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
1290 PtrEnd
->Checksum
= 0;
1295 // If there is no resource request
1297 Configuration
= AllocateZeroPool (sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
) + sizeof (EFI_ACPI_END_TAG_DESCRIPTOR
));
1298 if (Configuration
== NULL
) {
1299 return EFI_OUT_OF_RESOURCES
;
1302 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) (Configuration
);
1303 Ptr
->Desc
= ACPI_ADDRESS_SPACE_DESCRIPTOR
;
1305 PtrEnd
= (EFI_ACPI_END_TAG_DESCRIPTOR
*) (Ptr
+ 1);
1306 PtrEnd
->Desc
= ACPI_END_TAG_DESCRIPTOR
;
1307 PtrEnd
->Checksum
= 0;
1310 *Config
= Configuration
;
1316 Get resource base from an acpi configuration descriptor.
1318 @param Config An acpi configuration descriptor.
1319 @param IoBase Output of I/O resource base address.
1320 @param Mem32Base Output of 32-bit memory base address.
1321 @param PMem32Base Output of 32-bit prefetchable memory base address.
1322 @param Mem64Base Output of 64-bit memory base address.
1323 @param PMem64Base Output of 64-bit prefetchable memory base address.
1330 OUT UINT64
*Mem32Base
,
1331 OUT UINT64
*PMem32Base
,
1332 OUT UINT64
*Mem64Base
,
1333 OUT UINT64
*PMem64Base
1337 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Ptr
;
1340 ASSERT (Config
!= NULL
);
1342 *IoBase
= 0xFFFFFFFFFFFFFFFFULL
;
1343 *Mem32Base
= 0xFFFFFFFFFFFFFFFFULL
;
1344 *PMem32Base
= 0xFFFFFFFFFFFFFFFFULL
;
1345 *Mem64Base
= 0xFFFFFFFFFFFFFFFFULL
;
1346 *PMem64Base
= 0xFFFFFFFFFFFFFFFFULL
;
1348 Temp
= (UINT8
*) Config
;
1350 while (*Temp
== ACPI_ADDRESS_SPACE_DESCRIPTOR
) {
1352 Ptr
= (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*) Temp
;
1353 ResStatus
= Ptr
->AddrTranslationOffset
;
1355 if (ResStatus
== EFI_RESOURCE_SATISFIED
) {
1357 switch (Ptr
->ResType
) {
1360 // Memory type aperture
1365 // Check to see the granularity
1367 if (Ptr
->AddrSpaceGranularity
== 32) {
1368 if ((Ptr
->SpecificFlag
& 0x06) != 0) {
1369 *PMem32Base
= Ptr
->AddrRangeMin
;
1371 *Mem32Base
= Ptr
->AddrRangeMin
;
1375 if (Ptr
->AddrSpaceGranularity
== 64) {
1376 if ((Ptr
->SpecificFlag
& 0x06) != 0) {
1377 *PMem64Base
= Ptr
->AddrRangeMin
;
1379 *Mem64Base
= Ptr
->AddrRangeMin
;
1389 *IoBase
= Ptr
->AddrRangeMin
;
1403 Temp
+= sizeof (EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
);
1408 Enumerate pci bridge, allocate resource and determine attribute
1409 for devices on this bridge.
1411 @param BridgeDev Pointer to instance of bridge device.
1413 @retval EFI_SUCCESS Successfully enumerated PCI bridge.
1414 @retval other Failed to enumerate.
1418 PciBridgeEnumerator (
1419 IN PCI_IO_DEVICE
*BridgeDev
1423 UINT8 StartBusNumber
;
1424 EFI_PCI_IO_PROTOCOL
*PciIo
;
1429 PciIo
= &(BridgeDev
->PciIo
);
1430 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint8
, 0x19, 1, &StartBusNumber
);
1432 if (EFI_ERROR (Status
)) {
1436 Status
= PciAssignBusNumber (
1442 if (EFI_ERROR (Status
)) {
1446 Status
= PciPciDeviceInfoCollector (BridgeDev
, StartBusNumber
);
1448 if (EFI_ERROR (Status
)) {
1452 Status
= PciBridgeResourceAllocator (BridgeDev
);
1454 if (EFI_ERROR (Status
)) {
1458 Status
= DetermineDeviceAttribute (BridgeDev
);
1460 if (EFI_ERROR (Status
)) {
1469 Allocate all kinds of resource for PCI bridge.
1471 @param Bridge Pointer to bridge instance.
1473 @retval EFI_SUCCESS Successfully allocated resource for PCI bridge.
1474 @retval other Failed to allocate resource for bridge.
1478 PciBridgeResourceAllocator (
1479 IN PCI_IO_DEVICE
*Bridge
1482 PCI_RESOURCE_NODE
*IoBridge
;
1483 PCI_RESOURCE_NODE
*Mem32Bridge
;
1484 PCI_RESOURCE_NODE
*PMem32Bridge
;
1485 PCI_RESOURCE_NODE
*Mem64Bridge
;
1486 PCI_RESOURCE_NODE
*PMem64Bridge
;
1494 IoBridge
= CreateResourceNode (
1497 Bridge
->BridgeIoAlignment
,
1503 Mem32Bridge
= CreateResourceNode (
1512 PMem32Bridge
= CreateResourceNode (
1521 Mem64Bridge
= CreateResourceNode (
1530 PMem64Bridge
= CreateResourceNode (
1540 // Create resourcemap by going through all the devices subject to this root bridge
1551 Status
= GetResourceBaseFromBridge (
1560 if (EFI_ERROR (Status
)) {
1565 // Program IO resources
1573 // Program Mem32 resources
1581 // Program PMem32 resources
1589 // Program Mem64 resources
1597 // Program PMem64 resources
1604 DestroyResourceTree (IoBridge
);
1605 DestroyResourceTree (Mem32Bridge
);
1606 DestroyResourceTree (PMem32Bridge
);
1607 DestroyResourceTree (PMem64Bridge
);
1608 DestroyResourceTree (Mem64Bridge
);
1610 gBS
->FreePool (IoBridge
);
1611 gBS
->FreePool (Mem32Bridge
);
1612 gBS
->FreePool (PMem32Bridge
);
1613 gBS
->FreePool (PMem64Bridge
);
1614 gBS
->FreePool (Mem64Bridge
);
1620 Get resource base address for a pci bridge device.
1622 @param Bridge Given Pci driver instance.
1623 @param IoBase Output for base address of I/O type resource.
1624 @param Mem32Base Output for base address of 32-bit memory type resource.
1625 @param PMem32Base Ooutput for base address of 32-bit Pmemory type resource.
1626 @param Mem64Base Output for base address of 64-bit memory type resource.
1627 @param PMem64Base Output for base address of 64-bit Pmemory type resource.
1629 @retval EFI_SUCCESS Successfully got resource base address.
1630 @retval EFI_OUT_OF_RESOURCES PCI bridge is not available.
1634 GetResourceBaseFromBridge (
1635 IN PCI_IO_DEVICE
*Bridge
,
1637 OUT UINT64
*Mem32Base
,
1638 OUT UINT64
*PMem32Base
,
1639 OUT UINT64
*Mem64Base
,
1640 OUT UINT64
*PMem64Base
1643 if (!Bridge
->Allocated
) {
1644 return EFI_OUT_OF_RESOURCES
;
1648 *Mem32Base
= gAllOne
;
1649 *PMem32Base
= gAllOne
;
1650 *Mem64Base
= gAllOne
;
1651 *PMem64Base
= gAllOne
;
1653 if (IS_PCI_BRIDGE (&Bridge
->Pci
)) {
1655 if (Bridge
->PciBar
[PPB_IO_RANGE
].Length
> 0) {
1656 *IoBase
= Bridge
->PciBar
[PPB_IO_RANGE
].BaseAddress
;
1659 if (Bridge
->PciBar
[PPB_MEM32_RANGE
].Length
> 0) {
1660 *Mem32Base
= Bridge
->PciBar
[PPB_MEM32_RANGE
].BaseAddress
;
1663 if (Bridge
->PciBar
[PPB_PMEM32_RANGE
].Length
> 0) {
1664 *PMem32Base
= Bridge
->PciBar
[PPB_PMEM32_RANGE
].BaseAddress
;
1667 if (Bridge
->PciBar
[PPB_PMEM64_RANGE
].Length
> 0) {
1668 *PMem64Base
= Bridge
->PciBar
[PPB_PMEM64_RANGE
].BaseAddress
;
1670 *PMem64Base
= gAllOne
;
1675 if (IS_CARDBUS_BRIDGE (&Bridge
->Pci
)) {
1676 if (Bridge
->PciBar
[P2C_IO_1
].Length
> 0) {
1677 *IoBase
= Bridge
->PciBar
[P2C_IO_1
].BaseAddress
;
1679 if (Bridge
->PciBar
[P2C_IO_2
].Length
> 0) {
1680 *IoBase
= Bridge
->PciBar
[P2C_IO_2
].BaseAddress
;
1684 if (Bridge
->PciBar
[P2C_MEM_1
].Length
> 0) {
1685 if (Bridge
->PciBar
[P2C_MEM_1
].BarType
== PciBarTypePMem32
) {
1686 *PMem32Base
= Bridge
->PciBar
[P2C_MEM_1
].BaseAddress
;
1689 if (Bridge
->PciBar
[P2C_MEM_1
].BarType
== PciBarTypeMem32
) {
1690 *Mem32Base
= Bridge
->PciBar
[P2C_MEM_1
].BaseAddress
;
1694 if (Bridge
->PciBar
[P2C_MEM_2
].Length
> 0) {
1695 if (Bridge
->PciBar
[P2C_MEM_2
].BarType
== PciBarTypePMem32
) {
1696 *PMem32Base
= Bridge
->PciBar
[P2C_MEM_2
].BaseAddress
;
1699 if (Bridge
->PciBar
[P2C_MEM_2
].BarType
== PciBarTypeMem32
) {
1700 *Mem32Base
= Bridge
->PciBar
[P2C_MEM_2
].BaseAddress
;
1709 These are the notifications from the PCI bus driver that it is about to enter a certain
1710 phase of the PCI enumeration process.
1712 This member function can be used to notify the host bridge driver to perform specific actions,
1713 including any chipset-specific initialization, so that the chipset is ready to enter the next phase.
1714 Eight notification points are defined at this time. See belows:
1715 EfiPciHostBridgeBeginEnumeration Resets the host bridge PCI apertures and internal data
1716 structures. The PCI enumerator should issue this notification
1717 before starting a fresh enumeration process. Enumeration cannot
1718 be restarted after sending any other notification such as
1719 EfiPciHostBridgeBeginBusAllocation.
1720 EfiPciHostBridgeBeginBusAllocation The bus allocation phase is about to begin. No specific action is
1721 required here. This notification can be used to perform any
1722 chipset-specific programming.
1723 EfiPciHostBridgeEndBusAllocation The bus allocation and bus programming phase is complete. No
1724 specific action is required here. This notification can be used to
1725 perform any chipset-specific programming.
1726 EfiPciHostBridgeBeginResourceAllocation
1727 The resource allocation phase is about to begin. No specific
1728 action is required here. This notification can be used to perform
1729 any chipset-specific programming.
1730 EfiPciHostBridgeAllocateResources Allocates resources per previously submitted requests for all the PCI
1731 root bridges. These resource settings are returned on the next call to
1732 GetProposedResources(). Before calling NotifyPhase() with a Phase of
1733 EfiPciHostBridgeAllocateResource, the PCI bus enumerator is responsible
1734 for gathering I/O and memory requests for
1735 all the PCI root bridges and submitting these requests using
1736 SubmitResources(). This function pads the resource amount
1737 to suit the root bridge hardware, takes care of dependencies between
1738 the PCI root bridges, and calls the Global Coherency Domain (GCD)
1739 with the allocation request. In the case of padding, the allocated range
1740 could be bigger than what was requested.
1741 EfiPciHostBridgeSetResources Programs the host bridge hardware to decode previously allocated
1742 resources (proposed resources) for all the PCI root bridges. After the
1743 hardware is programmed, reassigning resources will not be supported.
1744 The bus settings are not affected.
1745 EfiPciHostBridgeFreeResources Deallocates resources that were previously allocated for all the PCI
1746 root bridges and resets the I/O and memory apertures to their initial
1747 state. The bus settings are not affected. If the request to allocate
1748 resources fails, the PCI enumerator can use this notification to
1749 deallocate previous resources, adjust the requests, and retry
1751 EfiPciHostBridgeEndResourceAllocation The resource allocation phase is completed. No specific action is
1752 required here. This notification can be used to perform any chipsetspecific
1755 @param[in] PciResAlloc The instance pointer of EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
1756 @param[in] Phase The phase during enumeration
1758 @retval EFI_NOT_READY This phase cannot be entered at this time. For example, this error
1759 is valid for a Phase of EfiPciHostBridgeAllocateResources if
1760 SubmitResources() has not been called for one or more
1761 PCI root bridges before this call
1762 @retval EFI_DEVICE_ERROR Programming failed due to a hardware error. This error is valid
1763 for a Phase of EfiPciHostBridgeSetResources.
1764 @retval EFI_INVALID_PARAMETER Invalid phase parameter
1765 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
1766 This error is valid for a Phase of EfiPciHostBridgeAllocateResources if the
1767 previously submitted resource requests cannot be fulfilled or
1768 were only partially fulfilled.
1769 @retval EFI_SUCCESS The notification was accepted without any errors.
1774 IN EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
,
1775 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PHASE Phase
1778 EFI_HANDLE HostBridgeHandle
;
1779 EFI_HANDLE RootBridgeHandle
;
1780 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
1783 HostBridgeHandle
= NULL
;
1784 RootBridgeHandle
= NULL
;
1785 if (gPciPlatformProtocol
!= NULL
) {
1787 // Get Host Bridge Handle.
1789 PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
);
1792 // Get the rootbridge Io protocol to find the host bridge handle
1794 Status
= gBS
->HandleProtocol (
1796 &gEfiPciRootBridgeIoProtocolGuid
,
1797 (VOID
**) &PciRootBridgeIo
1800 if (EFI_ERROR (Status
)) {
1801 return EFI_NOT_FOUND
;
1804 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
1807 // Call PlatformPci::PlatformNotify() if the protocol is present.
1809 gPciPlatformProtocol
->PlatformNotify (
1810 gPciPlatformProtocol
,
1815 } else if (gPciOverrideProtocol
!= NULL
){
1817 // Get Host Bridge Handle.
1819 PciResAlloc
->GetNextRootBridge (PciResAlloc
, &RootBridgeHandle
);
1822 // Get the rootbridge Io protocol to find the host bridge handle
1824 Status
= gBS
->HandleProtocol (
1826 &gEfiPciRootBridgeIoProtocolGuid
,
1827 (VOID
**) &PciRootBridgeIo
1830 if (EFI_ERROR (Status
)) {
1831 return EFI_NOT_FOUND
;
1834 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
1837 // Call PlatformPci::PhaseNotify() if the protocol is present.
1839 gPciOverrideProtocol
->PlatformNotify (
1840 gPciOverrideProtocol
,
1847 Status
= PciResAlloc
->NotifyPhase (
1852 if (gPciPlatformProtocol
!= NULL
) {
1854 // Call PlatformPci::PlatformNotify() if the protocol is present.
1856 gPciPlatformProtocol
->PlatformNotify (
1857 gPciPlatformProtocol
,
1863 } else if (gPciOverrideProtocol
!= NULL
) {
1865 // Call PlatformPci::PhaseNotify() if the protocol is present.
1867 gPciOverrideProtocol
->PlatformNotify (
1868 gPciOverrideProtocol
,
1879 Provides the hooks from the PCI bus driver to every PCI controller (device/function) at various
1880 stages of the PCI enumeration process that allow the host bridge driver to preinitialize individual
1881 PCI controllers before enumeration.
1883 This function is called during the PCI enumeration process. No specific action is expected from this
1884 member function. It allows the host bridge driver to preinitialize individual PCI controllers before
1887 @param Bridge Pointer to the EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL instance.
1888 @param Bus The bus number of the pci device.
1889 @param Device The device number of the pci device.
1890 @param Func The function number of the pci device.
1891 @param Phase The phase of the PCI device enumeration.
1893 @retval EFI_SUCCESS The requested parameters were returned.
1894 @retval EFI_INVALID_PARAMETER RootBridgeHandle is not a valid root bridge handle.
1895 @retval EFI_INVALID_PARAMETER Phase is not a valid phase that is defined in
1896 EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE.
1897 @retval EFI_DEVICE_ERROR Programming failed due to a hardware error. The PCI enumerator should
1898 not enumerate this device, including its child devices if it is a PCI-to-PCI
1903 PreprocessController (
1904 IN PCI_IO_DEVICE
*Bridge
,
1908 IN EFI_PCI_CONTROLLER_RESOURCE_ALLOCATION_PHASE Phase
1911 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL_PCI_ADDRESS RootBridgePciAddress
;
1912 EFI_PCI_HOST_BRIDGE_RESOURCE_ALLOCATION_PROTOCOL
*PciResAlloc
;
1913 EFI_HANDLE RootBridgeHandle
;
1914 EFI_HANDLE HostBridgeHandle
;
1918 // Get the host bridge handle
1920 HostBridgeHandle
= Bridge
->PciRootBridgeIo
->ParentHandle
;
1923 // Get the pci host bridge resource allocation protocol
1925 Status
= gBS
->OpenProtocol (
1927 &gEfiPciHostBridgeResourceAllocationProtocolGuid
,
1928 (VOID
**) &PciResAlloc
,
1931 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1934 if (EFI_ERROR (Status
)) {
1935 return EFI_UNSUPPORTED
;
1939 // Get Root Brige Handle
1941 while (Bridge
->Parent
!= NULL
) {
1942 Bridge
= Bridge
->Parent
;
1945 RootBridgeHandle
= Bridge
->Handle
;
1947 RootBridgePciAddress
.Register
= 0;
1948 RootBridgePciAddress
.Function
= Func
;
1949 RootBridgePciAddress
.Device
= Device
;
1950 RootBridgePciAddress
.Bus
= Bus
;
1951 RootBridgePciAddress
.ExtendedRegister
= 0;
1953 if (gPciPlatformProtocol
!= NULL
) {
1955 // Call PlatformPci::PrepController() if the protocol is present.
1957 gPciPlatformProtocol
->PlatformPrepController (
1958 gPciPlatformProtocol
,
1961 RootBridgePciAddress
,
1965 } else if (gPciOverrideProtocol
!= NULL
) {
1967 // Call PlatformPci::PrepController() if the protocol is present.
1969 gPciOverrideProtocol
->PlatformPrepController (
1970 gPciOverrideProtocol
,
1973 RootBridgePciAddress
,
1979 Status
= PciResAlloc
->PreprocessController (
1982 RootBridgePciAddress
,
1986 if (gPciPlatformProtocol
!= NULL
) {
1988 // Call PlatformPci::PrepController() if the protocol is present.
1990 gPciPlatformProtocol
->PlatformPrepController (
1991 gPciPlatformProtocol
,
1994 RootBridgePciAddress
,
1998 } else if (gPciOverrideProtocol
!= NULL
) {
2000 // Call PlatformPci::PrepController() if the protocol is present.
2002 gPciOverrideProtocol
->PlatformPrepController (
2003 gPciOverrideProtocol
,
2006 RootBridgePciAddress
,
2016 This function allows the PCI bus driver to be notified to act as requested when a hot-plug event has
2017 happened on the hot-plug controller. Currently, the operations include add operation and remove operation..
2019 @param This A pointer to the hot plug request protocol.
2020 @param Operation The operation the PCI bus driver is requested to make.
2021 @param Controller The handle of the hot-plug controller.
2022 @param RemainingDevicePath The remaining device path for the PCI-like hot-plug device.
2023 @param NumberOfChildren The number of child handles.
2024 For a add operation, it is an output parameter.
2025 For a remove operation, it's an input parameter.
2026 @param ChildHandleBuffer The buffer which contains the child handles.
2028 @retval EFI_INVALID_PARAMETER Operation is not a legal value.
2029 Controller is NULL or not a valid handle.
2030 NumberOfChildren is NULL.
2031 ChildHandleBuffer is NULL while Operation is add.
2032 @retval EFI_OUT_OF_RESOURCES There are no enough resources to start the devices.
2033 @retval EFI_NOT_FOUND Can not find bridge according to controller handle.
2034 @retval EFI_SUCCESS The handles for the specified device have been created or destroyed
2035 as requested, and for an add operation, the new handles are
2036 returned in ChildHandleBuffer.
2040 PciHotPlugRequestNotify (
2041 IN EFI_PCI_HOTPLUG_REQUEST_PROTOCOL
* This
,
2042 IN EFI_PCI_HOTPLUG_OPERATION Operation
,
2043 IN EFI_HANDLE Controller
,
2044 IN EFI_DEVICE_PATH_PROTOCOL
* RemainingDevicePath OPTIONAL
,
2045 IN OUT UINT8
*NumberOfChildren
,
2046 IN OUT EFI_HANDLE
* ChildHandleBuffer
2049 PCI_IO_DEVICE
*Bridge
;
2050 PCI_IO_DEVICE
*Temp
;
2051 EFI_PCI_IO_PROTOCOL
*PciIo
;
2053 EFI_HANDLE RootBridgeHandle
;
2057 // Check input parameter validity
2059 if ((Controller
== NULL
) || (NumberOfChildren
== NULL
)){
2060 return EFI_INVALID_PARAMETER
;
2063 if ((Operation
!= EfiPciHotPlugRequestAdd
) && (Operation
!= EfiPciHotplugRequestRemove
)) {
2064 return EFI_INVALID_PARAMETER
;
2067 if (Operation
== EfiPciHotPlugRequestAdd
){
2068 if (ChildHandleBuffer
== NULL
) {
2069 return EFI_INVALID_PARAMETER
;
2071 } else if ((Operation
== EfiPciHotplugRequestRemove
) && (*NumberOfChildren
!= 0)) {
2072 if (ChildHandleBuffer
== NULL
) {
2073 return EFI_INVALID_PARAMETER
;
2077 Status
= gBS
->OpenProtocol (
2079 &gEfiPciIoProtocolGuid
,
2081 gPciBusDriverBinding
.DriverBindingHandle
,
2083 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2086 if (EFI_ERROR (Status
)) {
2087 return EFI_NOT_FOUND
;
2090 Bridge
= PCI_IO_DEVICE_FROM_PCI_IO_THIS (PciIo
);
2093 // Get root bridge handle
2096 while (Temp
->Parent
!= NULL
) {
2097 Temp
= Temp
->Parent
;
2100 RootBridgeHandle
= Temp
->Handle
;
2102 if (Operation
== EfiPciHotPlugRequestAdd
) {
2104 // Report Status Code to indicate hot plug happens
2106 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
2108 (EFI_IO_BUS_PCI
| EFI_IOB_PC_HOTPLUG
),
2112 if (NumberOfChildren
!= NULL
) {
2113 *NumberOfChildren
= 0;
2116 if (IsListEmpty (&Bridge
->ChildList
)) {
2118 Status
= PciBridgeEnumerator (Bridge
);
2120 if (EFI_ERROR (Status
)) {
2125 Status
= StartPciDevicesOnBridge (
2128 RemainingDevicePath
,
2136 if (Operation
== EfiPciHotplugRequestRemove
) {
2138 if (*NumberOfChildren
== 0) {
2140 // Remove all devices on the bridge
2142 RemoveAllPciDeviceOnBridge (RootBridgeHandle
, Bridge
);
2147 for (Index
= 0; Index
< *NumberOfChildren
; Index
++) {
2149 // De register all the pci device
2151 Status
= DeRegisterPciDevice (RootBridgeHandle
, ChildHandleBuffer
[Index
]);
2153 if (EFI_ERROR (Status
)) {
2168 Search hostbridge according to given handle
2170 @param RootBridgeHandle Host bridge handle.
2172 @retval TRUE Found host bridge handle.
2173 @retval FALSE Not found hot bridge handle.
2177 SearchHostBridgeHandle (
2178 IN EFI_HANDLE RootBridgeHandle
2181 EFI_HANDLE HostBridgeHandle
;
2182 EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL
*PciRootBridgeIo
;
2187 // Get the rootbridge Io protocol to find the host bridge handle
2189 Status
= gBS
->OpenProtocol (
2191 &gEfiPciRootBridgeIoProtocolGuid
,
2192 (VOID
**) &PciRootBridgeIo
,
2193 gPciBusDriverBinding
.DriverBindingHandle
,
2195 EFI_OPEN_PROTOCOL_GET_PROTOCOL
2198 if (EFI_ERROR (Status
)) {
2202 HostBridgeHandle
= PciRootBridgeIo
->ParentHandle
;
2203 for (Index
= 0; Index
< gPciHostBridgeNumber
; Index
++) {
2204 if (HostBridgeHandle
== gPciHostBrigeHandles
[Index
]) {
2213 Add host bridge handle to global variable for enumerating.
2215 @param HostBridgeHandle Host bridge handle.
2217 @retval EFI_SUCCESS Successfully added host bridge.
2218 @retval EFI_ABORTED Host bridge is NULL, or given host bridge
2219 has been in host bridge list.
2223 AddHostBridgeEnumerator (
2224 IN EFI_HANDLE HostBridgeHandle
2229 if (HostBridgeHandle
== NULL
) {
2233 for (Index
= 0; Index
< gPciHostBridgeNumber
; Index
++) {
2234 if (HostBridgeHandle
== gPciHostBrigeHandles
[Index
]) {
2239 if (Index
< PCI_MAX_HOST_BRIDGE_NUM
) {
2240 gPciHostBrigeHandles
[Index
] = HostBridgeHandle
;
2241 gPciHostBridgeNumber
++;