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
;
18 Event notification function to set Hot Plug controller status.
20 @param Event The event that invoke this function.
21 @param Context The calling context, pointer to ROOT_HPC_DATA.
31 ROOT_HPC_DATA
*HpcData
;
33 HpcData
= (ROOT_HPC_DATA
*) Context
;
34 HpcData
->Initialized
= TRUE
;
38 Compare two device paths to check if they are exactly same.
40 @param DevicePath1 A pointer to the first device path data structure.
41 @param DevicePath2 A pointer to the second device path data structure.
43 @retval TRUE They are same.
44 @retval FALSE They are not same.
48 EfiCompareDevicePath (
49 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath1
,
50 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
56 Size1
= GetDevicePathSize (DevicePath1
);
57 Size2
= GetDevicePathSize (DevicePath2
);
63 if (CompareMem (DevicePath1
, DevicePath2
, Size1
) != 0) {
71 Check hot plug support and initialize root hot plug private data.
73 If Hot Plug is supported by the platform, call PCI Hot Plug Init protocol
74 to get PCI Hot Plug controller's information and constructor the root hot plug
75 private data structure.
77 @retval EFI_SUCCESS They are same.
78 @retval EFI_UNSUPPORTED No PCI Hot Plug controller on the platform.
79 @retval EFI_OUT_OF_RESOURCES No memory to constructor root hot plug private
84 InitializeHotPlugSupport (
89 EFI_HPC_LOCATION
*HpcList
;
93 // Locate the PciHotPlugInit Protocol
94 // If it doesn't exist, that means there is no
95 // hot plug controller supported on the platform
96 // the PCI Bus driver is running on. HotPlug Support
97 // is an optional feature, so absence of the protocol
98 // won't incur the penalty.
100 Status
= gBS
->LocateProtocol (
101 &gEfiPciHotPlugInitProtocolGuid
,
103 (VOID
**) &gPciHotPlugInit
106 if (EFI_ERROR (Status
)) {
107 return EFI_UNSUPPORTED
;
110 Status
= gPciHotPlugInit
->GetRootHpcList (
116 if (!EFI_ERROR (Status
)) {
118 gPciRootHpcPool
= HpcList
;
119 gPciRootHpcCount
= HpcCount
;
120 gPciRootHpcData
= AllocateZeroPool (sizeof (ROOT_HPC_DATA
) * gPciRootHpcCount
);
121 if (gPciRootHpcData
== NULL
) {
122 return EFI_OUT_OF_RESOURCES
;
130 Test whether device path is for root pci hot plug bus.
132 @param HpbDevicePath A pointer to device path data structure to be tested.
133 @param HpIndex If HpIndex is not NULL, return the index of root hot
134 plug in global array when TRUE is returned.
136 @retval TRUE The device path is for root pci hot plug bus.
137 @retval FALSE The device path is not for root pci hot plug bus.
141 IsRootPciHotPlugBus (
142 IN EFI_DEVICE_PATH_PROTOCOL
*HpbDevicePath
,
143 OUT UINTN
*HpIndex OPTIONAL
148 for (Index
= 0; Index
< gPciRootHpcCount
; Index
++) {
150 if (EfiCompareDevicePath (gPciRootHpcPool
[Index
].HpbDevicePath
, HpbDevicePath
)) {
152 if (HpIndex
!= NULL
) {
164 Test whether device path is for root pci hot plug controller.
166 @param HpcDevicePath A pointer to device path data structure to be tested.
167 @param HpIndex If HpIndex is not NULL, return the index of root hot
168 plug in global array when TRUE is returned.
170 @retval TRUE The device path is for root pci hot plug controller.
171 @retval FALSE The device path is not for root pci hot plug controller.
175 IsRootPciHotPlugController (
176 IN EFI_DEVICE_PATH_PROTOCOL
*HpcDevicePath
,
182 for (Index
= 0; Index
< gPciRootHpcCount
; Index
++) {
184 if (EfiCompareDevicePath (gPciRootHpcPool
[Index
].HpcDevicePath
, HpcDevicePath
)) {
186 if (HpIndex
!= NULL
) {
198 Creating event object for PCI Hot Plug controller.
200 @param HpIndex Index of hot plug device in global array.
201 @param Event The returned event that invoke this function.
203 @return Status of create event.
214 Status
= gBS
->CreateEvent (
218 gPciRootHpcData
+ HpIndex
,
219 &((gPciRootHpcData
+ HpIndex
)->Event
)
222 if (!EFI_ERROR (Status
)) {
223 *Event
= (gPciRootHpcData
+ HpIndex
)->Event
;
230 Wait for all root PCI Hot Plug controller finished initializing.
232 @param TimeoutInMicroSeconds Microseconds to wait for all root HPCs' initialization.
234 @retval EFI_SUCCESS All HPCs initialization finished.
235 @retval EFI_TIMEOUT Not ALL HPCs initialization finished in Microseconds.
239 AllRootHPCInitialized (
240 IN UINTN TimeoutInMicroSeconds
246 Delay
= (UINT32
) ((TimeoutInMicroSeconds
/ 30) + 1);
249 for (Index
= 0; Index
< gPciRootHpcCount
; Index
++) {
251 if (gPciRootHpcData
[Index
].Found
&& !gPciRootHpcData
[Index
].Initialized
) {
256 if (Index
== gPciRootHpcCount
) {
261 // Stall for 30 microseconds..
273 Check whether PCI-PCI bridge has PCI Hot Plug capability register block.
275 @param PciIoDevice A Pointer to the PCI-PCI bridge.
277 @retval TRUE PCI device is HPC.
278 @retval FALSE PCI device is not HPC.
283 IN PCI_IO_DEVICE
*PciIoDevice
290 if (PciIoDevice
== NULL
) {
295 Status
= LocateCapabilityRegBlock (
297 EFI_PCI_CAPABILITY_ID_SHPC
,
303 // If the PCI-PCI bridge has the hot plug controller build-in,
306 if (!EFI_ERROR (Status
)) {
314 Check whether PciIoDevice supports PCIe hotplug.
316 This is equivalent to the following condition:
317 - the device is either a PCIe switch downstream port or a root port,
318 - and the device has the SlotImplemented bit set in its PCIe capability
320 - and the device has the HotPlugCapable bit set in its slot capabilities
323 @param[in] PciIoDevice The device being checked.
325 @retval TRUE PciIoDevice is a PCIe port that accepts a hot-plugged device.
326 @retval FALSE Otherwise.
330 SupportsPcieHotplug (
331 IN PCI_IO_DEVICE
*PciIoDevice
336 PCI_REG_PCIE_CAPABILITY Capability
;
337 PCI_REG_PCIE_SLOT_CAPABILITY SlotCapability
;
339 if (PciIoDevice
== NULL
) {
344 // Read the PCI Express Capabilities Register
346 if (!PciIoDevice
->IsPciExp
) {
349 Offset
= PciIoDevice
->PciExpressCapabilityOffset
+
350 OFFSET_OF (PCI_CAPABILITY_PCIEXP
, Capability
);
351 Status
= PciIoDevice
->PciIo
.Pci
.Read (
358 if (EFI_ERROR (Status
)) {
363 // Check the contents of the register
365 switch (Capability
.Bits
.DevicePortType
) {
366 case PCIE_DEVICE_PORT_TYPE_ROOT_PORT
:
367 case PCIE_DEVICE_PORT_TYPE_DOWNSTREAM_PORT
:
372 if (!Capability
.Bits
.SlotImplemented
) {
377 // Read the Slot Capabilities Register
379 Offset
= PciIoDevice
->PciExpressCapabilityOffset
+
380 OFFSET_OF (PCI_CAPABILITY_PCIEXP
, SlotCapability
);
381 Status
= PciIoDevice
->PciIo
.Pci
.Read (
388 if (EFI_ERROR (Status
)) {
393 // Check the contents of the register
395 if (SlotCapability
.Bits
.HotPlugCapable
) {
402 Get resource padding if the specified PCI bridge is a hot plug bus.
404 @param PciIoDevice PCI bridge instance.
408 GetResourcePaddingForHpb (
409 IN PCI_IO_DEVICE
*PciIoDevice
415 EFI_HPC_PADDING_ATTRIBUTES Attributes
;
416 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
418 if (IsPciHotPlugBus (PciIoDevice
)) {
420 // If PCI-PCI bridge device is PCI Hot Plug bus.
422 PciAddress
= EFI_PCI_ADDRESS (PciIoDevice
->BusNumber
, PciIoDevice
->DeviceNumber
, PciIoDevice
->FunctionNumber
, 0);
423 Status
= gPciHotPlugInit
->GetResourcePadding (
425 PciIoDevice
->DevicePath
,
428 (VOID
**) &Descriptors
,
432 if (EFI_ERROR (Status
)) {
436 if ((State
& EFI_HPC_STATE_ENABLED
) != 0 && (State
& EFI_HPC_STATE_INITIALIZED
) != 0) {
437 PciIoDevice
->ResourcePaddingDescriptors
= Descriptors
;
438 PciIoDevice
->PaddingAttributes
= Attributes
;
446 Test whether PCI device is hot plug bus.
448 @param PciIoDevice PCI device instance.
450 @retval TRUE PCI device is a hot plug bus.
451 @retval FALSE PCI device is not a hot plug bus.
456 PCI_IO_DEVICE
*PciIoDevice
459 if (IsSHPC (PciIoDevice
)) {
461 // If the PPB has the hot plug controller build-in,
467 if (SupportsPcieHotplug (PciIoDevice
)) {
469 // If the PPB is a PCIe root complex port or a switch downstream port, and
470 // implements a hot-plug capable slot, then also return TRUE.
476 // Otherwise, see if it is a Root HPC
478 if(IsRootPciHotPlugBus (PciIoDevice
->DevicePath
, NULL
)) {