--- /dev/null
+/*++\r
+\r
+Copyright (c) 2006 - 2007, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution. The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
+\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+Module Name:\r
+\r
+ PciHotPlugSupport.c\r
+\r
+Abstract:\r
+\r
+\r
+\r
+Revision History\r
+\r
+--*/\r
+\r
+#include "Pcibus.h"\r
+#include "PciHotPlugSupport.h"\r
+\r
+EFI_PCI_HOT_PLUG_INIT_PROTOCOL *gPciHotPlugInit;\r
+EFI_HPC_LOCATION *gPciRootHpcPool;\r
+UINTN gPciRootHpcCount;\r
+ROOT_HPC_DATA *gPciRootHpcData;\r
+\r
+VOID\r
+EFIAPI\r
+PciHPCInitialized (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+// TODO: Event - add argument and description to function comment\r
+// TODO: Context - add argument and description to function comment\r
+{\r
+ ROOT_HPC_DATA *HpcData;\r
+\r
+ HpcData = (ROOT_HPC_DATA *) Context;\r
+ HpcData->Initialized = TRUE;\r
+\r
+}\r
+\r
+BOOLEAN\r
+EfiCompareDevicePath (\r
+ IN EFI_DEVICE_PATH_PROTOCOL *DevicePath1,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *DevicePath2\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+// TODO: DevicePath1 - add argument and description to function comment\r
+// TODO: DevicePath2 - add argument and description to function comment\r
+{\r
+ UINTN Size1;\r
+ UINTN Size2;\r
+\r
+ Size1 = GetDevicePathSize (DevicePath1);\r
+ Size2 = GetDevicePathSize (DevicePath2);\r
+\r
+ if (Size1 != Size2) {\r
+ return FALSE;\r
+ }\r
+\r
+ if (CompareMem (DevicePath1, DevicePath2, Size1)) {\r
+ return FALSE;\r
+ }\r
+\r
+ return TRUE;\r
+}\r
+\r
+EFI_STATUS\r
+InitializeHotPlugSupport (\r
+ VOID\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+// TODO: EFI_UNSUPPORTED - add return value to function comment\r
+// TODO: EFI_OUT_OF_RESOURCES - add return value to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_HPC_LOCATION *HpcList;\r
+ UINTN HpcCount;\r
+\r
+ //\r
+ // Locate the PciHotPlugInit Protocol\r
+ // If it doesn't exist, that means there is no\r
+ // hot plug controller supported on the platform\r
+ // the PCI Bus driver is running on. HotPlug Support\r
+ // is an optional feature, so absence of the protocol\r
+ // won't incur the penalty\r
+ //\r
+ gPciHotPlugInit = NULL;\r
+ gPciRootHpcPool = NULL;\r
+ gPciRootHpcCount = 0;\r
+ gPciRootHpcData = NULL;\r
+\r
+ Status = gBS->LocateProtocol (\r
+ &gEfiPciHotPlugInitProtocolGuid,\r
+ NULL,\r
+ (VOID **) &gPciHotPlugInit\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ Status = gPciHotPlugInit->GetRootHpcList (\r
+ gPciHotPlugInit,\r
+ &HpcCount,\r
+ &HpcList\r
+ );\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+\r
+ gPciRootHpcPool = HpcList;\r
+ gPciRootHpcCount = HpcCount;\r
+ gPciRootHpcData = AllocateZeroPool (sizeof (ROOT_HPC_DATA) * gPciRootHpcCount);\r
+ if (gPciRootHpcData == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+BOOLEAN\r
+IsRootPciHotPlugBus (\r
+ IN EFI_DEVICE_PATH_PROTOCOL *HpbDevicePath,\r
+ OUT UINTN *HpIndex\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+ HpcDevicePath - A pointer to the EFI_DEVICE_PATH_PROTOCOL.\r
+ HpIndex - A pointer to the Index.\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+// TODO: HpbDevicePath - add argument and description to function comment\r
+{\r
+ UINTN Index;\r
+\r
+ for (Index = 0; Index < gPciRootHpcCount; Index++) {\r
+\r
+ if (EfiCompareDevicePath (gPciRootHpcPool[Index].HpbDevicePath, HpbDevicePath)) {\r
+\r
+ if (HpIndex != NULL) {\r
+ *HpIndex = Index;\r
+ }\r
+\r
+ return TRUE;\r
+ }\r
+ }\r
+\r
+ return FALSE;\r
+}\r
+\r
+BOOLEAN\r
+IsRootPciHotPlugController (\r
+ IN EFI_DEVICE_PATH_PROTOCOL *HpcDevicePath,\r
+ OUT UINTN *HpIndex\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+ HpcDevicePath - A pointer to the EFI_DEVICE_PATH_PROTOCOL.\r
+ HpIndex - A pointer to the Index.\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+{\r
+ UINTN Index;\r
+\r
+ for (Index = 0; Index < gPciRootHpcCount; Index++) {\r
+\r
+ if (EfiCompareDevicePath (gPciRootHpcPool[Index].HpcDevicePath, HpcDevicePath)) {\r
+\r
+ if (HpIndex != NULL) {\r
+ *HpIndex = Index;\r
+ }\r
+\r
+ return TRUE;\r
+ }\r
+ }\r
+\r
+ return FALSE;\r
+}\r
+\r
+EFI_STATUS\r
+CreateEventForHpc (\r
+ IN UINTN HpIndex,\r
+ OUT EFI_EVENT *Event\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+// TODO: HpIndex - add argument and description to function comment\r
+// TODO: Event - add argument and description to function comment\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ Status = gBS->CreateEvent (\r
+ EVT_NOTIFY_SIGNAL,\r
+ TPL_CALLBACK,\r
+ PciHPCInitialized,\r
+ gPciRootHpcData + HpIndex,\r
+ &((gPciRootHpcData + HpIndex)->Event)\r
+ );\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ *Event = (gPciRootHpcData + HpIndex)->Event;\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+AllRootHPCInitialized (\r
+ IN UINTN TimeoutInMicroSeconds\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+ TimeoutInMicroSeconds - microseconds to wait for all root hpc's initialization\r
+\r
+Returns:\r
+ EFI_SUCCESS - All root hpc's initialization is finished before the timeout\r
+ EFI_TIMEOUT - Time out\r
+\r
+--*/\r
+// TODO: TimeoutInMilliSeconds - add argument and description to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+// TODO: EFI_TIMEOUT - add return value to function comment\r
+{\r
+ UINT32 Delay;\r
+ UINTN Index;\r
+\r
+ Delay = (UINT32) ((TimeoutInMicroSeconds / 30) + 1);\r
+ do {\r
+\r
+ for (Index = 0; Index < gPciRootHpcCount; Index++) {\r
+\r
+ if (!gPciRootHpcData[Index].Initialized) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (Index == gPciRootHpcCount) {\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ //\r
+ // Stall for 30 us\r
+ //\r
+ gBS->Stall (30);\r
+\r
+ Delay--;\r
+\r
+ } while (Delay);\r
+\r
+ return EFI_TIMEOUT;\r
+}\r
+\r
+EFI_STATUS\r
+IsSHPC (\r
+ PCI_IO_DEVICE *PciIoDevice\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+// TODO: PciIoDevice - add argument and description to function comment\r
+// TODO: EFI_NOT_FOUND - add return value to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+// TODO: EFI_NOT_FOUND - add return value to function comment\r
+{\r
+\r
+ EFI_STATUS Status;\r
+ UINT8 Offset;\r
+\r
+ if (!PciIoDevice) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ Offset = 0;\r
+ Status = LocateCapabilityRegBlock (\r
+ PciIoDevice,\r
+ EFI_PCI_CAPABILITY_ID_HOTPLUG,\r
+ &Offset,\r
+ NULL\r
+ );\r
+\r
+ //\r
+ // If the PPB has the hot plug controller build-in,\r
+ // then return TRUE;\r
+ //\r
+ if (!EFI_ERROR (Status)) {\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+EFI_STATUS\r
+GetResourcePaddingForHpb (\r
+ IN PCI_IO_DEVICE *PciIoDevice\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+// TODO: PciIoDevice - add argument and description to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+// TODO: EFI_NOT_FOUND - add return value to function comment\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_HPC_STATE State;\r
+ UINT64 PciAddress;\r
+ EFI_HPC_PADDING_ATTRIBUTES Attributes;\r
+ EFI_ACPI_ADDRESS_SPACE_DESCRIPTOR *Descriptors;\r
+\r
+ Status = IsPciHotPlugBus (PciIoDevice);\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ PciAddress = EFI_PCI_ADDRESS (PciIoDevice->BusNumber, PciIoDevice->DeviceNumber, PciIoDevice->FunctionNumber, 0);\r
+ Status = gPciHotPlugInit->GetResourcePadding (\r
+ gPciHotPlugInit,\r
+ PciIoDevice->DevicePath,\r
+ PciAddress,\r
+ &State,\r
+ (VOID **) &Descriptors,\r
+ &Attributes\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+\r
+ if ((State & EFI_HPC_STATE_ENABLED) && (State & EFI_HPC_STATE_INITIALIZED)) {\r
+ PciIoDevice->ResourcePaddingDescriptors = Descriptors;\r
+ PciIoDevice->PaddingAttributes = Attributes;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+EFI_STATUS\r
+IsPciHotPlugBus (\r
+ PCI_IO_DEVICE *PciIoDevice\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+Arguments:\r
+\r
+Returns:\r
+\r
+ None\r
+\r
+--*/\r
+// TODO: PciIoDevice - add argument and description to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+// TODO: EFI_NOT_FOUND - add return value to function comment\r
+{\r
+ BOOLEAN Result;\r
+ EFI_STATUS Status;\r
+\r
+ Status = IsSHPC (PciIoDevice);\r
+\r
+ //\r
+ // If the PPB has the hot plug controller build-in,\r
+ // then return TRUE;\r
+ //\r
+ if (!EFI_ERROR (Status)) {\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ //\r
+ // Otherwise, see if it is a Root HPC\r
+ //\r
+ Result = IsRootPciHotPlugBus (PciIoDevice->DevicePath, NULL);\r
+\r
+ if (Result) {\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r