2 PCI Hot Plug support functions implementation for PCI Bus module..
4 Copyright (c) 2006 - 2010, 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.
17 EFI_PCI_HOT_PLUG_INIT_PROTOCOL
*gPciHotPlugInit
= NULL
;
18 EFI_HPC_LOCATION
*gPciRootHpcPool
= NULL
;
19 UINTN gPciRootHpcCount
= 0;
20 ROOT_HPC_DATA
*gPciRootHpcData
= NULL
;
24 Event notification function to set Hot Plug controller status.
26 @param Event The event that invoke this function.
27 @param Context The calling context, pointer to ROOT_HPC_DATA.
37 ROOT_HPC_DATA
*HpcData
;
39 HpcData
= (ROOT_HPC_DATA
*) Context
;
40 HpcData
->Initialized
= TRUE
;
44 Compare two device pathes to check if they are exactly same.
46 @param DevicePath1 A pointer to the first device path data structure.
47 @param DevicePath2 A pointer to the second device path data structure.
49 @retval TRUE They are same.
50 @retval FALSE They are not same.
54 EfiCompareDevicePath (
55 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath1
,
56 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath2
62 Size1
= GetDevicePathSize (DevicePath1
);
63 Size2
= GetDevicePathSize (DevicePath2
);
69 if (CompareMem (DevicePath1
, DevicePath2
, Size1
) != 0) {
77 Check hot plug support and initialize root hot plug private data.
79 If Hot Plug is supported by the platform, call PCI Hot Plug Init protocol
80 to get PCI Hot Plug controller's information and constructor the root hot plug
81 private data structure.
83 @retval EFI_SUCCESS They are same.
84 @retval EFI_UNSUPPORTED No PCI Hot Plug controler on the platform.
85 @retval EFI_OUT_OF_RESOURCES No memory to constructor root hot plug private
90 InitializeHotPlugSupport (
95 EFI_HPC_LOCATION
*HpcList
;
99 // Locate the PciHotPlugInit Protocol
100 // If it doesn't exist, that means there is no
101 // hot plug controller supported on the platform
102 // the PCI Bus driver is running on. HotPlug Support
103 // is an optional feature, so absence of the protocol
104 // won't incur the penalty.
106 Status
= gBS
->LocateProtocol (
107 &gEfiPciHotPlugInitProtocolGuid
,
109 (VOID
**) &gPciHotPlugInit
112 if (EFI_ERROR (Status
)) {
113 return EFI_UNSUPPORTED
;
116 Status
= gPciHotPlugInit
->GetRootHpcList (
122 if (!EFI_ERROR (Status
)) {
124 gPciRootHpcPool
= HpcList
;
125 gPciRootHpcCount
= HpcCount
;
126 gPciRootHpcData
= AllocateZeroPool (sizeof (ROOT_HPC_DATA
) * gPciRootHpcCount
);
127 if (gPciRootHpcData
== NULL
) {
128 return EFI_OUT_OF_RESOURCES
;
136 Test whether device path is for root pci hot plug bus.
138 @param HpbDevicePath A pointer to device path data structure to be tested.
139 @param HpIndex If HpIndex is not NULL, return the index of root hot
140 plug in global array when TRUE is retuned.
142 @retval TRUE The device path is for root pci hot plug bus.
143 @retval FALSE The device path is not for root pci hot plug bus.
147 IsRootPciHotPlugBus (
148 IN EFI_DEVICE_PATH_PROTOCOL
*HpbDevicePath
,
149 OUT UINTN
*HpIndex OPTIONAL
154 for (Index
= 0; Index
< gPciRootHpcCount
; Index
++) {
156 if (EfiCompareDevicePath (gPciRootHpcPool
[Index
].HpbDevicePath
, HpbDevicePath
)) {
158 if (HpIndex
!= NULL
) {
170 Test whether device path is for root pci hot plug controller.
172 @param HpcDevicePath A pointer to device path data structure to be tested.
173 @param HpIndex If HpIndex is not NULL, return the index of root hot
174 plug in global array when TRUE is retuned.
176 @retval TRUE The device path is for root pci hot plug controller.
177 @retval FALSE The device path is not for root pci hot plug controller.
181 IsRootPciHotPlugController (
182 IN EFI_DEVICE_PATH_PROTOCOL
*HpcDevicePath
,
188 for (Index
= 0; Index
< gPciRootHpcCount
; Index
++) {
190 if (EfiCompareDevicePath (gPciRootHpcPool
[Index
].HpcDevicePath
, HpcDevicePath
)) {
192 if (HpIndex
!= NULL
) {
204 Creating event object for PCI Hot Plug controller.
206 @param HpIndex Index of hot plug device in global array.
207 @param Event The retuned event that invoke this function.
209 @return Status of create event invoken.
220 Status
= gBS
->CreateEvent (
224 gPciRootHpcData
+ HpIndex
,
225 &((gPciRootHpcData
+ HpIndex
)->Event
)
228 if (!EFI_ERROR (Status
)) {
229 *Event
= (gPciRootHpcData
+ HpIndex
)->Event
;
236 Wait for all root PCI Hot Plug controller finished initializing.
238 @param TimeoutInMicroSeconds Microseconds to wait for all root HPCs' initialization.
240 @retval EFI_SUCCESS All HPCs initialization finished.
241 @retval EFI_TIMEOUT Not ALL HPCs initialization finished in Microseconds.
245 AllRootHPCInitialized (
246 IN UINTN TimeoutInMicroSeconds
252 Delay
= (UINT32
) ((TimeoutInMicroSeconds
/ 30) + 1);
255 for (Index
= 0; Index
< gPciRootHpcCount
; Index
++) {
257 if (gPciRootHpcData
[Index
].Found
&& !gPciRootHpcData
[Index
].Initialized
) {
262 if (Index
== gPciRootHpcCount
) {
267 // Stall for 30 microseconds..
279 Check whether PCI-PCI bridge has PCI Hot Plug capability register block.
281 @param PciIoDevice A Pointer to the PCI-PCI bridge.
283 @retval TRUE PCI device is HPC.
284 @retval FALSE PCI device is not HPC.
289 IN PCI_IO_DEVICE
*PciIoDevice
296 if (PciIoDevice
== NULL
) {
301 Status
= LocateCapabilityRegBlock (
303 EFI_PCI_CAPABILITY_ID_SHPC
,
309 // If the PCI-PCI bridge has the hot plug controller build-in,
312 if (!EFI_ERROR (Status
)) {
320 Check whether PciIoDevice supports PCIe hotplug.
322 This is equivalent to the following condition:
323 - the device is either a PCIe switch downstream port or a root port,
324 - and the device has the SlotImplemented bit set in its PCIe capability
326 - and the device has the HotPlugCapable bit set in its slot capabilities
329 @param[in] PciIoDevice The device being checked.
331 @retval TRUE PciIoDevice is a PCIe port that accepts a hotplugged device.
332 @retval FALSE Otherwise.
336 SupportsPcieHotplug (
337 IN PCI_IO_DEVICE
*PciIoDevice
342 PCI_REG_PCIE_CAPABILITY Capability
;
343 PCI_REG_PCIE_SLOT_CAPABILITY SlotCapability
;
345 if (PciIoDevice
== NULL
) {
350 // Read the PCI Express Capabilities Register
352 if (!PciIoDevice
->IsPciExp
) {
355 Offset
= PciIoDevice
->PciExpressCapabilityOffset
+
356 OFFSET_OF (PCI_CAPABILITY_PCIEXP
, Capability
);
357 Status
= PciIoDevice
->PciIo
.Pci
.Read (
364 if (EFI_ERROR (Status
)) {
369 // Check the contents of the register
371 switch (Capability
.Bits
.DevicePortType
) {
372 case PCIE_DEVICE_PORT_TYPE_ROOT_PORT
:
373 case PCIE_DEVICE_PORT_TYPE_DOWNSTREAM_PORT
:
378 if (!Capability
.Bits
.SlotImplemented
) {
383 // Read the Slot Capabilities Register
385 Offset
= PciIoDevice
->PciExpressCapabilityOffset
+
386 OFFSET_OF (PCI_CAPABILITY_PCIEXP
, SlotCapability
);
387 Status
= PciIoDevice
->PciIo
.Pci
.Read (
394 if (EFI_ERROR (Status
)) {
399 // Check the contents of the register
401 if (SlotCapability
.Bits
.HotPlugCapable
) {
408 Get resource padding if the specified PCI bridge is a hot plug bus.
410 @param PciIoDevice PCI bridge instance.
414 GetResourcePaddingForHpb (
415 IN PCI_IO_DEVICE
*PciIoDevice
421 EFI_HPC_PADDING_ATTRIBUTES Attributes
;
422 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR
*Descriptors
;
424 if (IsPciHotPlugBus (PciIoDevice
)) {
426 // If PCI-PCI bridge device is PCI Hot Plug bus.
428 PciAddress
= EFI_PCI_ADDRESS (PciIoDevice
->BusNumber
, PciIoDevice
->DeviceNumber
, PciIoDevice
->FunctionNumber
, 0);
429 Status
= gPciHotPlugInit
->GetResourcePadding (
431 PciIoDevice
->DevicePath
,
434 (VOID
**) &Descriptors
,
438 if (EFI_ERROR (Status
)) {
442 if ((State
& EFI_HPC_STATE_ENABLED
) != 0 && (State
& EFI_HPC_STATE_INITIALIZED
) != 0) {
443 PciIoDevice
->ResourcePaddingDescriptors
= Descriptors
;
444 PciIoDevice
->PaddingAttributes
= Attributes
;
452 Test whether PCI device is hot plug bus.
454 @param PciIoDevice PCI device instance.
456 @retval TRUE PCI device is a hot plug bus.
457 @retval FALSE PCI device is not a hot plug bus.
462 PCI_IO_DEVICE
*PciIoDevice
465 if (IsSHPC (PciIoDevice
)) {
467 // If the PPB has the hot plug controller build-in,
473 if (SupportsPcieHotplug (PciIoDevice
)) {
475 // If the PPB is a PCIe root complex port or a switch downstream port, and
476 // implements a hot-plug capable slot, then also return TRUE.
482 // Otherwise, see if it is a Root HPC
484 if(IsRootPciHotPlugBus (PciIoDevice
->DevicePath
, NULL
)) {