1 /** @file 2 PCI Hot Plug support functions implementation for PCI Bus module.. 3 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 9 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. 12 13 **/ 14 15 #include "PciBus.h" 16 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; 21 22 23 /** 24 Event notification function to set Hot Plug controller status. 25 26 @param Event The event that invoke this function. 27 @param Context The calling context, pointer to ROOT_HPC_DATA. 28 29 **/ 30 VOID 31 EFIAPI 32 PciHPCInitialized ( 33 IN EFI_EVENT Event, 34 IN VOID *Context 35 ) 36 { 37 ROOT_HPC_DATA *HpcData; 38 39 HpcData = (ROOT_HPC_DATA *) Context; 40 HpcData->Initialized = TRUE; 41 } 42 43 /** 44 Compare two device pathes to check if they are exactly same. 45 46 @param DevicePath1 A pointer to the first device path data structure. 47 @param DevicePath2 A pointer to the second device path data structure. 48 49 @retval TRUE They are same. 50 @retval FALSE They are not same. 51 52 **/ 53 BOOLEAN 54 EfiCompareDevicePath ( 55 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath1, 56 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath2 57 ) 58 { 59 UINTN Size1; 60 UINTN Size2; 61 62 Size1 = GetDevicePathSize (DevicePath1); 63 Size2 = GetDevicePathSize (DevicePath2); 64 65 if (Size1 != Size2) { 66 return FALSE; 67 } 68 69 if (CompareMem (DevicePath1, DevicePath2, Size1) != 0) { 70 return FALSE; 71 } 72 73 return TRUE; 74 } 75 76 /** 77 Check hot plug support and initialize root hot plug private data. 78 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. 82 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 86 data structure. 87 88 **/ 89 EFI_STATUS 90 InitializeHotPlugSupport ( 91 VOID 92 ) 93 { 94 EFI_STATUS Status; 95 EFI_HPC_LOCATION *HpcList; 96 UINTN HpcCount; 97 98 // 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. 105 // 106 Status = gBS->LocateProtocol ( 107 &gEfiPciHotPlugInitProtocolGuid, 108 NULL, 109 (VOID **) &gPciHotPlugInit 110 ); 111 112 if (EFI_ERROR (Status)) { 113 return EFI_UNSUPPORTED; 114 } 115 116 Status = gPciHotPlugInit->GetRootHpcList ( 117 gPciHotPlugInit, 118 &HpcCount, 119 &HpcList 120 ); 121 122 if (!EFI_ERROR (Status)) { 123 124 gPciRootHpcPool = HpcList; 125 gPciRootHpcCount = HpcCount; 126 gPciRootHpcData = AllocateZeroPool (sizeof (ROOT_HPC_DATA) * gPciRootHpcCount); 127 if (gPciRootHpcData == NULL) { 128 return EFI_OUT_OF_RESOURCES; 129 } 130 } 131 132 return EFI_SUCCESS; 133 } 134 135 /** 136 Test whether device path is for root pci hot plug bus. 137 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. 141 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. 144 145 **/ 146 BOOLEAN 147 IsRootPciHotPlugBus ( 148 IN EFI_DEVICE_PATH_PROTOCOL *HpbDevicePath, 149 OUT UINTN *HpIndex OPTIONAL 150 ) 151 { 152 UINTN Index; 153 154 for (Index = 0; Index < gPciRootHpcCount; Index++) { 155 156 if (EfiCompareDevicePath (gPciRootHpcPool[Index].HpbDevicePath, HpbDevicePath)) { 157 158 if (HpIndex != NULL) { 159 *HpIndex = Index; 160 } 161 162 return TRUE; 163 } 164 } 165 166 return FALSE; 167 } 168 169 /** 170 Test whether device path is for root pci hot plug controller. 171 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. 175 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. 178 179 **/ 180 BOOLEAN 181 IsRootPciHotPlugController ( 182 IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath, 183 OUT UINTN *HpIndex 184 ) 185 { 186 UINTN Index; 187 188 for (Index = 0; Index < gPciRootHpcCount; Index++) { 189 190 if (EfiCompareDevicePath (gPciRootHpcPool[Index].HpcDevicePath, HpcDevicePath)) { 191 192 if (HpIndex != NULL) { 193 *HpIndex = Index; 194 } 195 196 return TRUE; 197 } 198 } 199 200 return FALSE; 201 } 202 203 /** 204 Creating event object for PCI Hot Plug controller. 205 206 @param HpIndex Index of hot plug device in global array. 207 @param Event The retuned event that invoke this function. 208 209 @return Status of create event invoken. 210 211 **/ 212 EFI_STATUS 213 CreateEventForHpc ( 214 IN UINTN HpIndex, 215 OUT EFI_EVENT *Event 216 ) 217 { 218 EFI_STATUS Status; 219 220 Status = gBS->CreateEvent ( 221 EVT_NOTIFY_SIGNAL, 222 TPL_CALLBACK, 223 PciHPCInitialized, 224 gPciRootHpcData + HpIndex, 225 &((gPciRootHpcData + HpIndex)->Event) 226 ); 227 228 if (!EFI_ERROR (Status)) { 229 *Event = (gPciRootHpcData + HpIndex)->Event; 230 } 231 232 return Status; 233 } 234 235 /** 236 Wait for all root PCI Hot Plug controller finished initializing. 237 238 @param TimeoutInMicroSeconds Microseconds to wait for all root HPCs' initialization. 239 240 @retval EFI_SUCCESS All HPCs initialization finished. 241 @retval EFI_TIMEOUT Not ALL HPCs initialization finished in Microseconds. 242 243 **/ 244 EFI_STATUS 245 AllRootHPCInitialized ( 246 IN UINTN TimeoutInMicroSeconds 247 ) 248 { 249 UINT32 Delay; 250 UINTN Index; 251 252 Delay = (UINT32) ((TimeoutInMicroSeconds / 30) + 1); 253 254 do { 255 for (Index = 0; Index < gPciRootHpcCount; Index++) { 256 257 if (gPciRootHpcData[Index].Found && !gPciRootHpcData[Index].Initialized) { 258 break; 259 } 260 } 261 262 if (Index == gPciRootHpcCount) { 263 return EFI_SUCCESS; 264 } 265 266 // 267 // Stall for 30 microseconds.. 268 // 269 gBS->Stall (30); 270 271 Delay--; 272 273 } while (Delay > 0); 274 275 return EFI_TIMEOUT; 276 } 277 278 /** 279 Check whether PCI-PCI bridge has PCI Hot Plug capability register block. 280 281 @param PciIoDevice A Pointer to the PCI-PCI bridge. 282 283 @retval TRUE PCI device is HPC. 284 @retval FALSE PCI device is not HPC. 285 286 **/ 287 BOOLEAN 288 IsSHPC ( 289 IN PCI_IO_DEVICE *PciIoDevice 290 ) 291 { 292 293 EFI_STATUS Status; 294 UINT8 Offset; 295 296 if (PciIoDevice == NULL) { 297 return FALSE; 298 } 299 300 Offset = 0; 301 Status = LocateCapabilityRegBlock ( 302 PciIoDevice, 303 EFI_PCI_CAPABILITY_ID_SHPC, 304 &Offset, 305 NULL 306 ); 307 308 // 309 // If the PCI-PCI bridge has the hot plug controller build-in, 310 // then return TRUE; 311 // 312 if (!EFI_ERROR (Status)) { 313 return TRUE; 314 } 315 316 return FALSE; 317 } 318 319 /** 320 Check whether PciIoDevice supports PCIe hotplug. 321 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 325 register, 326 - and the device has the HotPlugCapable bit set in its slot capabilities 327 register. 328 329 @param[in] PciIoDevice The device being checked. 330 331 @retval TRUE PciIoDevice is a PCIe port that accepts a hotplugged device. 332 @retval FALSE Otherwise. 333 334 **/ 335 BOOLEAN 336 SupportsPcieHotplug ( 337 IN PCI_IO_DEVICE *PciIoDevice 338 ) 339 { 340 UINT32 Offset; 341 EFI_STATUS Status; 342 PCI_REG_PCIE_CAPABILITY Capability; 343 PCI_REG_PCIE_SLOT_CAPABILITY SlotCapability; 344 345 if (PciIoDevice == NULL) { 346 return FALSE; 347 } 348 349 // 350 // Read the PCI Express Capabilities Register 351 // 352 if (!PciIoDevice->IsPciExp) { 353 return FALSE; 354 } 355 Offset = PciIoDevice->PciExpressCapabilityOffset + 356 OFFSET_OF (PCI_CAPABILITY_PCIEXP, Capability); 357 Status = PciIoDevice->PciIo.Pci.Read ( 358 &PciIoDevice->PciIo, 359 EfiPciIoWidthUint16, 360 Offset, 361 1, 362 &Capability 363 ); 364 if (EFI_ERROR (Status)) { 365 return FALSE; 366 } 367 368 // 369 // Check the contents of the register 370 // 371 switch (Capability.Bits.DevicePortType) { 372 case PCIE_DEVICE_PORT_TYPE_ROOT_PORT: 373 case PCIE_DEVICE_PORT_TYPE_DOWNSTREAM_PORT: 374 break; 375 default: 376 return FALSE; 377 } 378 if (!Capability.Bits.SlotImplemented) { 379 return FALSE; 380 } 381 382 // 383 // Read the Slot Capabilities Register 384 // 385 Offset = PciIoDevice->PciExpressCapabilityOffset + 386 OFFSET_OF (PCI_CAPABILITY_PCIEXP, SlotCapability); 387 Status = PciIoDevice->PciIo.Pci.Read ( 388 &PciIoDevice->PciIo, 389 EfiPciIoWidthUint32, 390 Offset, 391 1, 392 &SlotCapability 393 ); 394 if (EFI_ERROR (Status)) { 395 return FALSE; 396 } 397 398 // 399 // Check the contents of the register 400 // 401 if (SlotCapability.Bits.HotPlugCapable) { 402 return TRUE; 403 } 404 return FALSE; 405 } 406 407 /** 408 Get resource padding if the specified PCI bridge is a hot plug bus. 409 410 @param PciIoDevice PCI bridge instance. 411 412 **/ 413 VOID 414 GetResourcePaddingForHpb ( 415 IN PCI_IO_DEVICE *PciIoDevice 416 ) 417 { 418 EFI_STATUS Status; 419 EFI_HPC_STATE State; 420 UINT64 PciAddress; 421 EFI_HPC_PADDING_ATTRIBUTES Attributes; 422 EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptors; 423 424 if (IsPciHotPlugBus (PciIoDevice)) { 425 // 426 // If PCI-PCI bridge device is PCI Hot Plug bus. 427 // 428 PciAddress = EFI_PCI_ADDRESS (PciIoDevice->BusNumber, PciIoDevice->DeviceNumber, PciIoDevice->FunctionNumber, 0); 429 Status = gPciHotPlugInit->GetResourcePadding ( 430 gPciHotPlugInit, 431 PciIoDevice->DevicePath, 432 PciAddress, 433 &State, 434 (VOID **) &Descriptors, 435 &Attributes 436 ); 437 438 if (EFI_ERROR (Status)) { 439 return; 440 } 441 442 if ((State & EFI_HPC_STATE_ENABLED) != 0 && (State & EFI_HPC_STATE_INITIALIZED) != 0) { 443 PciIoDevice->ResourcePaddingDescriptors = Descriptors; 444 PciIoDevice->PaddingAttributes = Attributes; 445 } 446 447 return; 448 } 449 } 450 451 /** 452 Test whether PCI device is hot plug bus. 453 454 @param PciIoDevice PCI device instance. 455 456 @retval TRUE PCI device is a hot plug bus. 457 @retval FALSE PCI device is not a hot plug bus. 458 459 **/ 460 BOOLEAN 461 IsPciHotPlugBus ( 462 PCI_IO_DEVICE *PciIoDevice 463 ) 464 { 465 if (IsSHPC (PciIoDevice)) { 466 // 467 // If the PPB has the hot plug controller build-in, 468 // then return TRUE; 469 // 470 return TRUE; 471 } 472 473 if (SupportsPcieHotplug (PciIoDevice)) { 474 // 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. 477 // 478 return TRUE; 479 } 480 481 // 482 // Otherwise, see if it is a Root HPC 483 // 484 if(IsRootPciHotPlugBus (PciIoDevice->DevicePath, NULL)) { 485 return TRUE; 486 } 487 488 return FALSE; 489 } 490 491