2 PCI Hot Plug support functions implementation for PCI Bus module..
4 Copyright (c) 2006 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*gPciHotPlugInit
= NULL
;
12 EFI_HPC_LOCATION
*gPciRootHpcPool
= NULL
;
13 UINTN gPciRootHpcCount
= 0;
14 ROOT_HPC_DATA
*gPciRootHpcData
= NULL
;
17 Event notification function to set Hot Plug controller status.
19 @param Event The event that invoke this function.
20 @param Context The calling context, pointer to ROOT_HPC_DATA.
30 ROOT_HPC_DATA
*HpcData
;
32 HpcData
= (ROOT_HPC_DATA
*)Context
;
33 HpcData
->Initialized
= TRUE
;
37 Compare two device paths to check if they are exactly same.
39 @param DevicePath1 A pointer to the first device path data structure.
40 @param DevicePath2 A pointer to the second device path data structure.
42 @retval TRUE They are same.
43 @retval FALSE They are not same.
47 EfiCompareDevicePath (
48 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath1
,
49 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
55 Size1
= GetDevicePathSize (DevicePath1
);
56 Size2
= GetDevicePathSize (DevicePath2
);
62 if (CompareMem (DevicePath1
, DevicePath2
, Size1
) != 0) {
70 Check hot plug support and initialize root hot plug private data.
72 If Hot Plug is supported by the platform, call PCI Hot Plug Init protocol
73 to get PCI Hot Plug controller's information and constructor the root hot plug
74 private data structure.
76 @retval EFI_SUCCESS They are same.
77 @retval EFI_UNSUPPORTED No PCI Hot Plug controller on the platform.
78 @retval EFI_OUT_OF_RESOURCES No memory to constructor root hot plug private
83 InitializeHotPlugSupport (
88 EFI_HPC_LOCATION
*HpcList
;
92 // Locate the PciHotPlugInit Protocol
93 // If it doesn't exist, that means there is no
94 // hot plug controller supported on the platform
95 // the PCI Bus driver is running on. HotPlug Support
96 // is an optional feature, so absence of the protocol
97 // won't incur the penalty.
99 Status
= gBS
->LocateProtocol (
100 &gEfiPciHotPlugInitProtocolGuid
,
102 (VOID
**)&gPciHotPlugInit
105 if (EFI_ERROR (Status
)) {
106 return EFI_UNSUPPORTED
;
109 Status
= gPciHotPlugInit
->GetRootHpcList (
115 if (!EFI_ERROR (Status
)) {
116 gPciRootHpcPool
= HpcList
;
117 gPciRootHpcCount
= HpcCount
;
118 gPciRootHpcData
= AllocateZeroPool (sizeof (ROOT_HPC_DATA
) * gPciRootHpcCount
);
119 if (gPciRootHpcData
== NULL
) {
120 return EFI_OUT_OF_RESOURCES
;
128 Test whether device path is for root pci hot plug bus.
130 @param HpbDevicePath A pointer to device path data structure to be tested.
131 @param HpIndex If HpIndex is not NULL, return the index of root hot
132 plug in global array when TRUE is returned.
134 @retval TRUE The device path is for root pci hot plug bus.
135 @retval FALSE The device path is not for root pci hot plug bus.
139 IsRootPciHotPlugBus (
140 IN EFI_DEVICE_PATH_PROTOCOL
*HpbDevicePath
,
141 OUT UINTN
*HpIndex OPTIONAL
146 for (Index
= 0; Index
< gPciRootHpcCount
; Index
++) {
147 if (EfiCompareDevicePath (gPciRootHpcPool
[Index
].HpbDevicePath
, HpbDevicePath
)) {
148 if (HpIndex
!= NULL
) {
160 Test whether device path is for root pci hot plug controller.
162 @param HpcDevicePath A pointer to device path data structure to be tested.
163 @param HpIndex If HpIndex is not NULL, return the index of root hot
164 plug in global array when TRUE is returned.
166 @retval TRUE The device path is for root pci hot plug controller.
167 @retval FALSE The device path is not for root pci hot plug controller.
171 IsRootPciHotPlugController (
172 IN EFI_DEVICE_PATH_PROTOCOL
*HpcDevicePath
,
178 for (Index
= 0; Index
< gPciRootHpcCount
; Index
++) {
179 if (EfiCompareDevicePath (gPciRootHpcPool
[Index
].HpcDevicePath
, HpcDevicePath
)) {
180 if (HpIndex
!= NULL
) {
192 Creating event object for PCI Hot Plug controller.
194 @param HpIndex Index of hot plug device in global array.
195 @param Event The returned event that invoke this function.
197 @return Status of create event.
208 Status
= gBS
->CreateEvent (
212 gPciRootHpcData
+ HpIndex
,
213 &((gPciRootHpcData
+ HpIndex
)->Event
)
216 if (!EFI_ERROR (Status
)) {
217 *Event
= (gPciRootHpcData
+ HpIndex
)->Event
;
224 Wait for all root PCI Hot Plug controller finished initializing.
226 @param TimeoutInMicroSeconds Microseconds to wait for all root HPCs' initialization.
228 @retval EFI_SUCCESS All HPCs initialization finished.
229 @retval EFI_TIMEOUT Not ALL HPCs initialization finished in Microseconds.
233 AllRootHPCInitialized (
234 IN UINTN TimeoutInMicroSeconds
240 Delay
= (UINT32
)((TimeoutInMicroSeconds
/ 30) + 1);
243 for (Index
= 0; Index
< gPciRootHpcCount
; Index
++) {
244 if (gPciRootHpcData
[Index
].Found
&& !gPciRootHpcData
[Index
].Initialized
) {
249 if (Index
== gPciRootHpcCount
) {
254 // Stall for 30 microseconds..
265 Check whether PCI-PCI bridge has PCI Hot Plug capability register block.
267 @param PciIoDevice A Pointer to the PCI-PCI bridge.
269 @retval TRUE PCI device is HPC.
270 @retval FALSE PCI device is not HPC.
275 IN PCI_IO_DEVICE
*PciIoDevice
281 if (PciIoDevice
== NULL
) {
286 Status
= LocateCapabilityRegBlock (
288 EFI_PCI_CAPABILITY_ID_SHPC
,
294 // If the PCI-PCI bridge has the hot plug controller build-in,
297 if (!EFI_ERROR (Status
)) {
305 Check whether PciIoDevice supports PCIe hotplug.
307 This is equivalent to the following condition:
308 - the device is either a PCIe switch downstream port or a root port,
309 - and the device has the SlotImplemented bit set in its PCIe capability
311 - and the device has the HotPlugCapable bit set in its slot capabilities
314 @param[in] PciIoDevice The device being checked.
316 @retval TRUE PciIoDevice is a PCIe port that accepts a hot-plugged device.
317 @retval FALSE Otherwise.
321 SupportsPcieHotplug (
322 IN PCI_IO_DEVICE
*PciIoDevice
327 PCI_REG_PCIE_CAPABILITY Capability
;
328 PCI_REG_PCIE_SLOT_CAPABILITY SlotCapability
;
330 if (PciIoDevice
== NULL
) {
335 // Read the PCI Express Capabilities Register
337 if (!PciIoDevice
->IsPciExp
) {
341 Offset
= PciIoDevice
->PciExpressCapabilityOffset
+
342 OFFSET_OF (PCI_CAPABILITY_PCIEXP
, Capability
);
343 Status
= PciIoDevice
->PciIo
.Pci
.Read (
350 if (EFI_ERROR (Status
)) {
355 // Check the contents of the register
357 switch (Capability
.Bits
.DevicePortType
) {
358 case PCIE_DEVICE_PORT_TYPE_ROOT_PORT
:
359 case PCIE_DEVICE_PORT_TYPE_DOWNSTREAM_PORT
:
365 if (!Capability
.Bits
.SlotImplemented
) {
370 // Read the Slot Capabilities Register
372 Offset
= PciIoDevice
->PciExpressCapabilityOffset
+
373 OFFSET_OF (PCI_CAPABILITY_PCIEXP
, SlotCapability
);
374 Status
= PciIoDevice
->PciIo
.Pci
.Read (
381 if (EFI_ERROR (Status
)) {
386 // Check the contents of the register
388 if (SlotCapability
.Bits
.HotPlugCapable
) {
396 Get resource padding if the specified PCI bridge is a hot plug bus.
398 @param PciIoDevice PCI bridge instance.
402 GetResourcePaddingForHpb (
403 IN PCI_IO_DEVICE
*PciIoDevice
409 EFI_HPC_PADDING_ATTRIBUTES Attributes
;
410 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
412 if (IsPciHotPlugBus (PciIoDevice
)) {
414 // If PCI-PCI bridge device is PCI Hot Plug bus.
416 PciAddress
= EFI_PCI_ADDRESS (PciIoDevice
->BusNumber
, PciIoDevice
->DeviceNumber
, PciIoDevice
->FunctionNumber
, 0);
417 Status
= gPciHotPlugInit
->GetResourcePadding (
419 PciIoDevice
->DevicePath
,
422 (VOID
**)&Descriptors
,
426 if (EFI_ERROR (Status
)) {
430 if (((State
& EFI_HPC_STATE_ENABLED
) != 0) && ((State
& EFI_HPC_STATE_INITIALIZED
) != 0)) {
431 PciIoDevice
->ResourcePaddingDescriptors
= Descriptors
;
432 PciIoDevice
->PaddingAttributes
= Attributes
;
440 Test whether PCI device is hot plug bus.
442 @param PciIoDevice PCI device instance.
444 @retval TRUE PCI device is a hot plug bus.
445 @retval FALSE PCI device is not a hot plug bus.
450 PCI_IO_DEVICE
*PciIoDevice
453 if (IsSHPC (PciIoDevice
)) {
455 // If the PPB has the hot plug controller build-in,
461 if (SupportsPcieHotplug (PciIoDevice
)) {
463 // If the PPB is a PCIe root complex port or a switch downstream port, and
464 // implements a hot-plug capable slot, then also return TRUE.
470 // Otherwise, see if it is a Root HPC
472 if (IsRootPciHotPlugBus (PciIoDevice
->DevicePath
, NULL
)) {