]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/MpLib: Add GDTR, IDTR and TR in saved AP data
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.c
index f05db7c2a5dc7420b5e63f4a4db4b9e370fb8b86..0c2058a7b0db7e340e2e23a07e844d6fafe444a6 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   CPU MP Initialize Library common functions.\r
 \r
-  Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
   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
 \r
 #include "MpLib.h"\r
 \r
+EFI_GUID mCpuInitMpLibHobGuid = CPU_INIT_MP_LIB_HOB_GUID;\r
+\r
+/**\r
+  The function will check if BSP Execute Disable is enabled.\r
+\r
+  DxeIpl may have enabled Execute Disable for BSP, APs need to\r
+  get the status and sync up the settings.\r
+  If BSP's CR0.Paging is not set, BSP execute Disble feature is\r
+  not working actually.\r
+\r
+  @retval TRUE      BSP Execute Disable is enabled.\r
+  @retval FALSE     BSP Execute Disable is not enabled.\r
+**/\r
+BOOLEAN\r
+IsBspExecuteDisableEnabled (\r
+  VOID\r
+  )\r
+{\r
+  UINT32                      Eax;\r
+  CPUID_EXTENDED_CPU_SIG_EDX  Edx;\r
+  MSR_IA32_EFER_REGISTER      EferMsr;\r
+  BOOLEAN                     Enabled;\r
+  IA32_CR0                    Cr0;\r
+\r
+  Enabled = FALSE;\r
+  Cr0.UintN = AsmReadCr0 ();\r
+  if (Cr0.Bits.PG != 0) {\r
+    //\r
+    // If CR0 Paging bit is set\r
+    //\r
+    AsmCpuid (CPUID_EXTENDED_FUNCTION, &Eax, NULL, NULL, NULL);\r
+    if (Eax >= CPUID_EXTENDED_CPU_SIG) {\r
+      AsmCpuid (CPUID_EXTENDED_CPU_SIG, NULL, NULL, NULL, &Edx.Uint32);\r
+      //\r
+      // CPUID 0x80000001\r
+      // Bit 20: Execute Disable Bit available.\r
+      //\r
+      if (Edx.Bits.NX != 0) {\r
+        EferMsr.Uint64 = AsmReadMsr64 (MSR_IA32_EFER);\r
+        //\r
+        // MSR 0xC0000080\r
+        // Bit 11: Execute Disable Bit enable.\r
+        //\r
+        if (EferMsr.Bits.NXE != 0) {\r
+          Enabled = TRUE;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  return Enabled;\r
+}\r
+\r
+/**\r
+  Worker function for SwitchBSP().\r
+\r
+  Worker function for SwitchBSP(), assigned to the AP which is intended\r
+  to become BSP.\r
+\r
+  @param[in] Buffer   Pointer to CPU MP Data\r
+**/\r
+VOID\r
+EFIAPI\r
+FutureBSPProc (\r
+  IN  VOID            *Buffer\r
+  )\r
+{\r
+  CPU_MP_DATA         *DataInHob;\r
+\r
+  DataInHob = (CPU_MP_DATA *) Buffer;\r
+  AsmExchangeRole (&DataInHob->APInfo, &DataInHob->BSPInfo);\r
+}\r
+\r
+/**\r
+  Get the Application Processors state.\r
+\r
+  @param[in]  CpuData    The pointer to CPU_AP_DATA of specified AP\r
+\r
+  @return  The AP status\r
+**/\r
+CPU_STATE\r
+GetApState (\r
+  IN  CPU_AP_DATA     *CpuData\r
+  )\r
+{\r
+  return CpuData->State;\r
+}\r
+\r
+/**\r
+  Set the Application Processors state.\r
+\r
+  @param[in]   CpuData    The pointer to CPU_AP_DATA of specified AP\r
+  @param[in]   State      The AP status\r
+**/\r
+VOID\r
+SetApState (\r
+  IN  CPU_AP_DATA     *CpuData,\r
+  IN  CPU_STATE       State\r
+  )\r
+{\r
+  AcquireSpinLock (&CpuData->ApLock);\r
+  CpuData->State = State;\r
+  ReleaseSpinLock (&CpuData->ApLock);\r
+}\r
+\r
+/**\r
+  Save BSP's local APIC timer setting.\r
+\r
+  @param[in] CpuMpData          Pointer to CPU MP Data\r
+**/\r
+VOID\r
+SaveLocalApicTimerSetting (\r
+  IN CPU_MP_DATA   *CpuMpData\r
+  )\r
+{\r
+  //\r
+  // Record the current local APIC timer setting of BSP\r
+  //\r
+  GetApicTimerState (\r
+    &CpuMpData->DivideValue,\r
+    &CpuMpData->PeriodicMode,\r
+    &CpuMpData->Vector\r
+    );\r
+  CpuMpData->CurrentTimerCount   = GetApicTimerCurrentCount ();\r
+  CpuMpData->TimerInterruptState = GetApicTimerInterruptState ();\r
+}\r
+\r
+/**\r
+  Sync local APIC timer setting from BSP to AP.\r
+\r
+  @param[in] CpuMpData          Pointer to CPU MP Data\r
+**/\r
+VOID\r
+SyncLocalApicTimerSetting (\r
+  IN CPU_MP_DATA   *CpuMpData\r
+  )\r
+{\r
+  //\r
+  // Sync local APIC timer setting from BSP to AP\r
+  //\r
+  InitializeApicTimer (\r
+    CpuMpData->DivideValue,\r
+    CpuMpData->CurrentTimerCount,\r
+    CpuMpData->PeriodicMode,\r
+    CpuMpData->Vector\r
+    );\r
+  //\r
+  // Disable AP's local APIC timer interrupt\r
+  //\r
+  DisableApicTimerInterrupt ();\r
+}\r
+\r
+/**\r
+  Save the volatile registers required to be restored following INIT IPI.\r
+\r
+  @param[out]  VolatileRegisters    Returns buffer saved the volatile resisters\r
+**/\r
+VOID\r
+SaveVolatileRegisters (\r
+  OUT CPU_VOLATILE_REGISTERS    *VolatileRegisters\r
+  )\r
+{\r
+  CPUID_VERSION_INFO_EDX        VersionInfoEdx;\r
+\r
+  VolatileRegisters->Cr0 = AsmReadCr0 ();\r
+  VolatileRegisters->Cr3 = AsmReadCr3 ();\r
+  VolatileRegisters->Cr4 = AsmReadCr4 ();\r
+\r
+  AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
+  if (VersionInfoEdx.Bits.DE != 0) {\r
+    //\r
+    // If processor supports Debugging Extensions feature\r
+    // by CPUID.[EAX=01H]:EDX.BIT2\r
+    //\r
+    VolatileRegisters->Dr0 = AsmReadDr0 ();\r
+    VolatileRegisters->Dr1 = AsmReadDr1 ();\r
+    VolatileRegisters->Dr2 = AsmReadDr2 ();\r
+    VolatileRegisters->Dr3 = AsmReadDr3 ();\r
+    VolatileRegisters->Dr6 = AsmReadDr6 ();\r
+    VolatileRegisters->Dr7 = AsmReadDr7 ();\r
+  }\r
+\r
+  AsmReadGdtr (&VolatileRegisters->Gdtr);\r
+  AsmReadIdtr (&VolatileRegisters->Idtr);\r
+  VolatileRegisters->Tr = AsmReadTr ();\r
+}\r
+\r
+/**\r
+  Restore the volatile registers following INIT IPI.\r
+\r
+  @param[in]  VolatileRegisters   Pointer to volatile resisters\r
+  @param[in]  IsRestoreDr         TRUE:  Restore DRx if supported\r
+                                  FALSE: Do not restore DRx\r
+**/\r
+VOID\r
+RestoreVolatileRegisters (\r
+  IN CPU_VOLATILE_REGISTERS    *VolatileRegisters,\r
+  IN BOOLEAN                   IsRestoreDr\r
+  )\r
+{\r
+  CPUID_VERSION_INFO_EDX        VersionInfoEdx;\r
+  IA32_TSS_DESCRIPTOR           *Tss;\r
+\r
+  AsmWriteCr0 (VolatileRegisters->Cr0);\r
+  AsmWriteCr3 (VolatileRegisters->Cr3);\r
+  AsmWriteCr4 (VolatileRegisters->Cr4);\r
+\r
+  if (IsRestoreDr) {\r
+    AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
+    if (VersionInfoEdx.Bits.DE != 0) {\r
+      //\r
+      // If processor supports Debugging Extensions feature\r
+      // by CPUID.[EAX=01H]:EDX.BIT2\r
+      //\r
+      AsmWriteDr0 (VolatileRegisters->Dr0);\r
+      AsmWriteDr1 (VolatileRegisters->Dr1);\r
+      AsmWriteDr2 (VolatileRegisters->Dr2);\r
+      AsmWriteDr3 (VolatileRegisters->Dr3);\r
+      AsmWriteDr6 (VolatileRegisters->Dr6);\r
+      AsmWriteDr7 (VolatileRegisters->Dr7);\r
+    }\r
+  }\r
+\r
+  AsmWriteGdtr (&VolatileRegisters->Gdtr);\r
+  AsmWriteIdtr (&VolatileRegisters->Idtr);\r
+  if (VolatileRegisters->Tr != 0 &&\r
+      VolatileRegisters->Tr < VolatileRegisters->Gdtr.Limit) {\r
+    Tss = (IA32_TSS_DESCRIPTOR *)(VolatileRegisters->Gdtr.Base +\r
+                                  VolatileRegisters->Tr);\r
+    if (Tss->Bits.P == 1) {\r
+      Tss->Bits.Type &= 0xD;  // 1101 - Clear busy bit just in case\r
+      AsmWriteTr (VolatileRegisters->Tr);\r
+    }\r
+  }\r
+}\r
 \r
 /**\r
   Detect whether Mwait-monitor feature is supported.\r
@@ -74,163 +309,1943 @@ GetApLoopMode (
 \r
   return ApLoopMode;\r
 }\r
+\r
 /**\r
-  MP Initialize Library initialization.\r
+  Sort the APIC ID of all processors.\r
 \r
-  This service will allocate AP reset vector and wakeup all APs to do APs\r
-  initialization.\r
+  This function sorts the APIC ID of all processors so that processor number is\r
+  assigned in the ascending order of APIC ID which eases MP debugging.\r
 \r
-  This service must be invoked before all other MP Initialize Library\r
-  service are invoked.\r
+  @param[in] CpuMpData        Pointer to PEI CPU MP Data\r
+**/\r
+VOID\r
+SortApicId (\r
+  IN CPU_MP_DATA   *CpuMpData\r
+  )\r
+{\r
+  UINTN             Index1;\r
+  UINTN             Index2;\r
+  UINTN             Index3;\r
+  UINT32            ApicId;\r
+  CPU_INFO_IN_HOB   CpuInfo;\r
+  UINT32            ApCount;\r
+  CPU_INFO_IN_HOB   *CpuInfoInHob;\r
 \r
-  @retval  EFI_SUCCESS           MP initialization succeeds.\r
-  @retval  Others                MP initialization fails.\r
+  ApCount = CpuMpData->CpuCount - 1;\r
+  CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+  if (ApCount != 0) {\r
+    for (Index1 = 0; Index1 < ApCount; Index1++) {\r
+      Index3 = Index1;\r
+      //\r
+      // Sort key is the hardware default APIC ID\r
+      //\r
+      ApicId = CpuInfoInHob[Index1].ApicId;\r
+      for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {\r
+        if (ApicId > CpuInfoInHob[Index2].ApicId) {\r
+          Index3 = Index2;\r
+          ApicId = CpuInfoInHob[Index2].ApicId;\r
+        }\r
+      }\r
+      if (Index3 != Index1) {\r
+        CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));\r
+        CopyMem (\r
+          &CpuInfoInHob[Index3],\r
+          &CpuInfoInHob[Index1],\r
+          sizeof (CPU_INFO_IN_HOB)\r
+          );\r
+        CopyMem (&CpuInfoInHob[Index1], &CpuInfo, sizeof (CPU_INFO_IN_HOB));\r
+      }\r
+    }\r
+\r
+    //\r
+    // Get the processor number for the BSP\r
+    //\r
+    ApicId = GetInitialApicId ();\r
+    for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {\r
+      if (CpuInfoInHob[Index1].ApicId == ApicId) {\r
+        CpuMpData->BspNumber = (UINT32) Index1;\r
+        break;\r
+      }\r
+    }\r
+  }\r
+}\r
 \r
+/**\r
+  Enable x2APIC mode on APs.\r
+\r
+  @param[in, out] Buffer  Pointer to private data buffer.\r
 **/\r
-EFI_STATUS\r
+VOID\r
 EFIAPI\r
-MpInitLibInitialize (\r
-  VOID\r
+ApFuncEnableX2Apic (\r
+  IN OUT VOID  *Buffer\r
   )\r
 {\r
-  UINT32                   MaxLogicalProcessorNumber;\r
-  UINT32                   ApStackSize;\r
-  MP_ASSEMBLY_ADDRESS_MAP  AddressMap;\r
-  UINTN                    BufferSize;\r
-  UINT32                   MonitorFilterSize;\r
-  VOID                     *MpBuffer;\r
-  UINTN                    Buffer;\r
-  CPU_MP_DATA              *CpuMpData;\r
-  UINT8                    ApLoopMode;\r
-  UINT8                    *MonitorBuffer;\r
-  UINTN                    ApResetVectorSize;\r
-  UINTN                    BackupBufferAddr;\r
-  MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);\r
+  SetApicMode (LOCAL_APIC_MODE_X2APIC);\r
+}\r
 \r
-  AsmGetAddressMap (&AddressMap);\r
-  ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);\r
-  ApStackSize = PcdGet32(PcdCpuApStackSize);\r
-  ApLoopMode  = GetApLoopMode (&MonitorFilterSize);\r
+/**\r
+  Do sync on APs.\r
 \r
-  BufferSize  = ApStackSize * MaxLogicalProcessorNumber;\r
-  BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;\r
-  BufferSize += sizeof (CPU_MP_DATA);\r
-  BufferSize += ApResetVectorSize;\r
-  BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;\r
-  MpBuffer    = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
-  ASSERT (MpBuffer != NULL);\r
-  ZeroMem (MpBuffer, BufferSize);\r
-  Buffer = (UINTN) MpBuffer;\r
+  @param[in, out] Buffer  Pointer to private data buffer.\r
+**/\r
+VOID\r
+EFIAPI\r
+ApInitializeSync (\r
+  IN OUT VOID  *Buffer\r
+  )\r
+{\r
+  CPU_MP_DATA  *CpuMpData;\r
 \r
-  MonitorBuffer    = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);\r
-  BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;\r
-  CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);\r
-  CpuMpData->Buffer           = Buffer;\r
-  CpuMpData->CpuApStackSize   = ApStackSize;\r
-  CpuMpData->BackupBuffer     = BackupBufferAddr;\r
-  CpuMpData->BackupBufferSize = ApResetVectorSize;\r
-  CpuMpData->EndOfPeiFlag     = FALSE;\r
-  CpuMpData->WakeupBuffer     = (UINTN) -1;\r
-  CpuMpData->CpuCount         = 1;\r
-  CpuMpData->BspNumber        = 0;\r
-  CpuMpData->WaitEvent        = NULL;\r
-  CpuMpData->CpuData          = (CPU_AP_DATA *) (CpuMpData + 1);\r
-  CpuMpData->CpuInfoInHob     = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);\r
-  InitializeSpinLock(&CpuMpData->MpLock);\r
-  //\r
-  // Save assembly code information\r
+  CpuMpData = (CPU_MP_DATA *) Buffer;\r
   //\r
-  CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));\r
-  //\r
-  // Finally set AP loop mode\r
+  // Load microcode on AP\r
   //\r
-  CpuMpData->ApLoopMode = ApLoopMode;\r
-  DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));\r
+  MicrocodeDetect (CpuMpData);\r
   //\r
-  // Store BSP's MTRR setting\r
+  // Sync BSP's MTRR table to AP\r
   //\r
-  MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
-\r
-  return EFI_SUCCESS;\r
+  MtrrSetAllMtrrs (&CpuMpData->MtrrTable);\r
 }\r
 \r
 /**\r
-  Gets detailed MP-related information on the requested processor at the\r
-  instant this call is made. This service may only be called from the BSP.\r
-\r
-  @param[in]  ProcessorNumber       The handle number of processor.\r
-  @param[out] ProcessorInfoBuffer   A pointer to the buffer where information for\r
-                                    the requested processor is deposited.\r
-  @param[out]  HealthData            Return processor health data.\r
+  Find the current Processor number by APIC ID.\r
 \r
-  @retval EFI_SUCCESS             Processor information was returned.\r
-  @retval EFI_DEVICE_ERROR        The calling processor is an AP.\r
-  @retval EFI_INVALID_PARAMETER   ProcessorInfoBuffer is NULL.\r
-  @retval EFI_NOT_FOUND           The processor with the handle specified by\r
-                                  ProcessorNumber does not exist in the platform.\r
-  @retval EFI_NOT_READY           MP Initialize Library is not initialized.\r
+  @param[in]  CpuMpData         Pointer to PEI CPU MP Data\r
+  @param[out] ProcessorNumber   Return the pocessor number found\r
 \r
+  @retval EFI_SUCCESS          ProcessorNumber is found and returned.\r
+  @retval EFI_NOT_FOUND        ProcessorNumber is not found.\r
 **/\r
 EFI_STATUS\r
-EFIAPI\r
-MpInitLibGetProcessorInfo (\r
-  IN  UINTN                      ProcessorNumber,\r
-  OUT EFI_PROCESSOR_INFORMATION  *ProcessorInfoBuffer,\r
-  OUT EFI_HEALTH_FLAGS           *HealthData  OPTIONAL\r
+GetProcessorNumber (\r
+  IN CPU_MP_DATA               *CpuMpData,\r
+  OUT UINTN                    *ProcessorNumber\r
   )\r
 {\r
-  return EFI_UNSUPPORTED;\r
+  UINTN                   TotalProcessorNumber;\r
+  UINTN                   Index;\r
+  CPU_INFO_IN_HOB         *CpuInfoInHob;\r
+\r
+  CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+\r
+  TotalProcessorNumber = CpuMpData->CpuCount;\r
+  for (Index = 0; Index < TotalProcessorNumber; Index ++) {\r
+    if (CpuInfoInHob[Index].ApicId == GetApicId ()) {\r
+      *ProcessorNumber = Index;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+  return EFI_NOT_FOUND;\r
 }\r
-/**\r
-  This return the handle number for the calling processor.  This service may be\r
-  called from the BSP and APs.\r
 \r
-  @param[out] ProcessorNumber  Pointer to the handle number of AP.\r
-                               The range is from 0 to the total number of\r
-                               logical processors minus 1. The total number of\r
-                               logical processors can be retrieved by\r
-                               MpInitLibGetNumberOfProcessors().\r
+/**\r
+  This function will get CPU count in the system.\r
 \r
-  @retval EFI_SUCCESS             The current processor handle number was returned\r
-                                  in ProcessorNumber.\r
-  @retval EFI_INVALID_PARAMETER   ProcessorNumber is NULL.\r
-  @retval EFI_NOT_READY           MP Initialize Library is not initialized.\r
+  @param[in] CpuMpData        Pointer to PEI CPU MP Data\r
 \r
+  @return  CPU count detected\r
 **/\r
-EFI_STATUS\r
-EFIAPI\r
-MpInitLibWhoAmI (\r
-  OUT UINTN                    *ProcessorNumber\r
+UINTN\r
+CollectProcessorCount (\r
+  IN CPU_MP_DATA         *CpuMpData\r
   )\r
 {\r
-  return EFI_UNSUPPORTED;\r
+  UINTN                  Index;\r
+\r
+  //\r
+  // Send 1st broadcast IPI to APs to wakeup APs\r
+  //\r
+  CpuMpData->InitFlag     = ApInitConfig;\r
+  CpuMpData->X2ApicEnable = FALSE;\r
+  WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL);\r
+  CpuMpData->InitFlag = ApInitDone;\r
+  ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
+  //\r
+  // Wait for all APs finished the initialization\r
+  //\r
+  while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
+    CpuPause ();\r
+  }\r
+\r
+  if (CpuMpData->CpuCount > 255) {\r
+    //\r
+    // If there are more than 255 processor found, force to enable X2APIC\r
+    //\r
+    CpuMpData->X2ApicEnable = TRUE;\r
+  }\r
+  if (CpuMpData->X2ApicEnable) {\r
+    DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n"));\r
+    //\r
+    // Wakeup all APs to enable x2APIC mode\r
+    //\r
+    WakeUpAP (CpuMpData, TRUE, 0, ApFuncEnableX2Apic, NULL);\r
+    //\r
+    // Wait for all known APs finished\r
+    //\r
+    while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
+      CpuPause ();\r
+    }\r
+    //\r
+    // Enable x2APIC on BSP\r
+    //\r
+    SetApicMode (LOCAL_APIC_MODE_X2APIC);\r
+    //\r
+    // Set BSP/Aps state to IDLE\r
+    //\r
+    for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+      SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
+    }\r
+  }\r
+  DEBUG ((DEBUG_INFO, "APIC MODE is %d\n", GetApicMode ()));\r
+  //\r
+  // Sort BSP/Aps by CPU APIC ID in ascending order\r
+  //\r
+  SortApicId (CpuMpData);\r
+\r
+  DEBUG ((DEBUG_INFO, "MpInitLib: Find %d processors in system.\n", CpuMpData->CpuCount));\r
+\r
+  return CpuMpData->CpuCount;\r
 }\r
-/**\r
-  Retrieves the number of logical processor in the platform and the number of\r
-  those logical processors that are enabled on this boot. This service may only\r
-  be called from the BSP.\r
 \r
-  @param[out] NumberOfProcessors          Pointer to the total number of logical\r
-                                          processors in the system, including the BSP\r
-                                          and disabled APs.\r
-  @param[out] NumberOfEnabledProcessors   Pointer to the number of enabled logical\r
-                                          processors that exist in system, including\r
-                                          the BSP.\r
+/**\r
+  Initialize CPU AP Data when AP is wakeup at the first time.\r
 \r
-  @retval EFI_SUCCESS             The number of logical processors and enabled\r
-                                  logical processors was retrieved.\r
-  @retval EFI_DEVICE_ERROR        The calling processor is an AP.\r
-  @retval EFI_INVALID_PARAMETER   NumberOfProcessors is NULL and NumberOfEnabledProcessors\r
-                                  is NULL.\r
-  @retval EFI_NOT_READY           MP Initialize Library is not initialized.\r
+  @param[in, out] CpuMpData        Pointer to PEI CPU MP Data\r
+  @param[in]      ProcessorNumber  The handle number of processor\r
+  @param[in]      BistData         Processor BIST data\r
+  @param[in]      ApTopOfStack     Top of AP stack\r
 \r
 **/\r
-EFI_STATUS\r
+VOID\r
+InitializeApData (\r
+  IN OUT CPU_MP_DATA      *CpuMpData,\r
+  IN     UINTN            ProcessorNumber,\r
+  IN     UINT32           BistData,\r
+  IN     UINT64           ApTopOfStack\r
+  )\r
+{\r
+  CPU_INFO_IN_HOB          *CpuInfoInHob;\r
+\r
+  CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+  CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
+  CpuInfoInHob[ProcessorNumber].ApicId        = GetApicId ();\r
+  CpuInfoInHob[ProcessorNumber].Health        = BistData;\r
+  CpuInfoInHob[ProcessorNumber].ApTopOfStack  = ApTopOfStack;\r
+\r
+  CpuMpData->CpuData[ProcessorNumber].Waiting    = FALSE;\r
+  CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;\r
+  if (CpuInfoInHob[ProcessorNumber].InitialApicId >= 0xFF) {\r
+    //\r
+    // Set x2APIC mode if there are any logical processor reporting\r
+    // an Initial APIC ID of 255 or greater.\r
+    //\r
+    AcquireSpinLock(&CpuMpData->MpLock);\r
+    CpuMpData->X2ApicEnable = TRUE;\r
+    ReleaseSpinLock(&CpuMpData->MpLock);\r
+  }\r
+\r
+  InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);\r
+  SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
+}\r
+\r
+/**\r
+  This function will be called from AP reset code if BSP uses WakeUpAP.\r
+\r
+  @param[in] ExchangeInfo     Pointer to the MP exchange info buffer\r
+  @param[in] ApIndex          Number of current executing AP\r
+**/\r
+VOID\r
+EFIAPI\r
+ApWakeupFunction (\r
+  IN MP_CPU_EXCHANGE_INFO      *ExchangeInfo,\r
+  IN UINTN                     ApIndex\r
+  )\r
+{\r
+  CPU_MP_DATA                *CpuMpData;\r
+  UINTN                      ProcessorNumber;\r
+  EFI_AP_PROCEDURE           Procedure;\r
+  VOID                       *Parameter;\r
+  UINT32                     BistData;\r
+  volatile UINT32            *ApStartupSignalBuffer;\r
+  CPU_INFO_IN_HOB            *CpuInfoInHob;\r
+  UINT64                     ApTopOfStack;\r
+  UINTN                      CurrentApicMode;\r
+\r
+  //\r
+  // AP finished assembly code and begin to execute C code\r
+  //\r
+  CpuMpData = ExchangeInfo->CpuMpData;\r
+\r
+  //\r
+  // AP's local APIC settings will be lost after received INIT IPI\r
+  // We need to re-initialize them at here\r
+  //\r
+  ProgramVirtualWireMode ();\r
+  SyncLocalApicTimerSetting (CpuMpData);\r
+\r
+  CurrentApicMode = GetApicMode ();\r
+  while (TRUE) {\r
+    if (CpuMpData->InitFlag == ApInitConfig) {\r
+      //\r
+      // Add CPU number\r
+      //\r
+      InterlockedIncrement ((UINT32 *) &CpuMpData->CpuCount);\r
+      ProcessorNumber = ApIndex;\r
+      //\r
+      // This is first time AP wakeup, get BIST information from AP stack\r
+      //\r
+      ApTopOfStack  = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize;\r
+      BistData = *(UINT32 *) ((UINTN) ApTopOfStack - sizeof (UINTN));\r
+      //\r
+      // Do some AP initialize sync\r
+      //\r
+      ApInitializeSync (CpuMpData);\r
+      //\r
+      // Sync BSP's Control registers to APs\r
+      //\r
+      RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);\r
+      InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack);\r
+      ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
+    } else {\r
+      //\r
+      // Execute AP function if AP is ready\r
+      //\r
+      GetProcessorNumber (CpuMpData, &ProcessorNumber);\r
+      //\r
+      // Clear AP start-up signal when AP waken up\r
+      //\r
+      ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
+      InterlockedCompareExchange32 (\r
+        (UINT32 *) ApStartupSignalBuffer,\r
+        WAKEUP_AP_SIGNAL,\r
+        0\r
+        );\r
+      if (CpuMpData->ApLoopMode == ApInHltLoop) {\r
+        //\r
+        // Restore AP's volatile registers saved\r
+        //\r
+        RestoreVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE);\r
+      }\r
+\r
+      if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateReady) {\r
+        Procedure = (EFI_AP_PROCEDURE)CpuMpData->CpuData[ProcessorNumber].ApFunction;\r
+        Parameter = (VOID *) CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument;\r
+        if (Procedure != NULL) {\r
+          SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy);\r
+          //\r
+          // Enable source debugging on AP function\r
+          //         \r
+          EnableDebugAgent ();\r
+          //\r
+          // Invoke AP function here\r
+          //\r
+          Procedure (Parameter);\r
+          CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+          if (CpuMpData->SwitchBspFlag) {\r
+            //\r
+            // Re-get the processor number due to BSP/AP maybe exchange in AP function\r
+            //\r
+            GetProcessorNumber (CpuMpData, &ProcessorNumber);\r
+            CpuMpData->CpuData[ProcessorNumber].ApFunction = 0;\r
+            CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0;\r
+            ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
+            CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack;\r
+          } else {\r
+            if (CpuInfoInHob[ProcessorNumber].ApicId != GetApicId () ||\r
+                CpuInfoInHob[ProcessorNumber].InitialApicId != GetInitialApicId ()) {\r
+              if (CurrentApicMode != GetApicMode ()) {\r
+                //\r
+                // If APIC mode change happened during AP function execution,\r
+                // we do not support APIC ID value changed.\r
+                //\r
+                ASSERT (FALSE);\r
+                CpuDeadLoop ();\r
+              } else {\r
+                //\r
+                // Re-get the CPU APICID and Initial APICID if they are changed\r
+                //\r
+                CpuInfoInHob[ProcessorNumber].ApicId        = GetApicId ();\r
+                CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
+              }\r
+            }\r
+          }\r
+        }\r
+        SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);\r
+      }\r
+    }\r
+\r
+    //\r
+    // AP finished executing C code\r
+    //\r
+    InterlockedIncrement ((UINT32 *) &CpuMpData->FinishedCount);\r
+    InterlockedDecrement ((UINT32 *) &CpuMpData->MpCpuExchangeInfo->NumApsExecuting);\r
+\r
+    //\r
+    // Place AP is specified loop mode\r
+    //\r
+    if (CpuMpData->ApLoopMode == ApInHltLoop) {\r
+      //\r
+      // Save AP volatile registers\r
+      //\r
+      SaveVolatileRegisters (&CpuMpData->CpuData[ProcessorNumber].VolatileRegisters);\r
+      //\r
+      // Place AP in HLT-loop\r
+      //\r
+      while (TRUE) {\r
+        DisableInterrupts ();\r
+        CpuSleep ();\r
+        CpuPause ();\r
+      }\r
+    }\r
+    while (TRUE) {\r
+      DisableInterrupts ();\r
+      if (CpuMpData->ApLoopMode == ApInMwaitLoop) {\r
+        //\r
+        // Place AP in MWAIT-loop\r
+        //\r
+        AsmMonitor ((UINTN) ApStartupSignalBuffer, 0, 0);\r
+        if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) {\r
+          //\r
+          // Check AP start-up signal again.\r
+          // If AP start-up signal is not set, place AP into\r
+          // the specified C-state\r
+          //\r
+          AsmMwait (CpuMpData->ApTargetCState << 4, 0);\r
+        }\r
+      } else if (CpuMpData->ApLoopMode == ApInRunLoop) {\r
+        //\r
+        // Place AP in Run-loop\r
+        //\r
+        CpuPause ();\r
+      } else {\r
+        ASSERT (FALSE);\r
+      }\r
+\r
+      //\r
+      // If AP start-up signal is written, AP is waken up\r
+      // otherwise place AP in loop again\r
+      //\r
+      if (*ApStartupSignalBuffer == WAKEUP_AP_SIGNAL) {\r
+        break;\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Wait for AP wakeup and write AP start-up signal till AP is waken up.\r
+\r
+  @param[in] ApStartupSignalBuffer  Pointer to AP wakeup signal\r
+**/\r
+VOID\r
+WaitApWakeup (\r
+  IN volatile UINT32        *ApStartupSignalBuffer\r
+  )\r
+{\r
+  //\r
+  // If AP is waken up, StartupApSignal should be cleared.\r
+  // Otherwise, write StartupApSignal again till AP waken up.\r
+  //\r
+  while (InterlockedCompareExchange32 (\r
+          (UINT32 *) ApStartupSignalBuffer,\r
+          WAKEUP_AP_SIGNAL,\r
+          WAKEUP_AP_SIGNAL\r
+          ) != 0) {\r
+    CpuPause ();\r
+  }\r
+}\r
+\r
+/**\r
+  This function will fill the exchange info structure.\r
+\r
+  @param[in] CpuMpData          Pointer to CPU MP Data\r
+\r
+**/\r
+VOID\r
+FillExchangeInfoData (\r
+  IN CPU_MP_DATA               *CpuMpData\r
+  )\r
+{\r
+  volatile MP_CPU_EXCHANGE_INFO    *ExchangeInfo;\r
+\r
+  ExchangeInfo                  = CpuMpData->MpCpuExchangeInfo;\r
+  ExchangeInfo->Lock            = 0;\r
+  ExchangeInfo->StackStart      = CpuMpData->Buffer;\r
+  ExchangeInfo->StackSize       = CpuMpData->CpuApStackSize;\r
+  ExchangeInfo->BufferStart     = CpuMpData->WakeupBuffer;\r
+  ExchangeInfo->ModeOffset      = CpuMpData->AddressMap.ModeEntryOffset;\r
+\r
+  ExchangeInfo->CodeSegment     = AsmReadCs ();\r
+  ExchangeInfo->DataSegment     = AsmReadDs ();\r
+\r
+  ExchangeInfo->Cr3             = AsmReadCr3 ();\r
+\r
+  ExchangeInfo->CFunction       = (UINTN) ApWakeupFunction;\r
+  ExchangeInfo->ApIndex         = 0;\r
+  ExchangeInfo->NumApsExecuting = 0;\r
+  ExchangeInfo->InitFlag        = (UINTN) CpuMpData->InitFlag;\r
+  ExchangeInfo->CpuInfo         = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+  ExchangeInfo->CpuMpData       = CpuMpData;\r
+\r
+  ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();\r
+\r
+  ExchangeInfo->InitializeFloatingPointUnitsAddress = (UINTN)InitializeFloatingPointUnits;\r
+\r
+  //\r
+  // Get the BSP's data of GDT and IDT\r
+  //\r
+  AsmReadGdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->GdtrProfile);\r
+  AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);\r
+}\r
+\r
+/**\r
+  Helper function that waits until the finished AP count reaches the specified\r
+  limit, or the specified timeout elapses (whichever comes first).\r
+\r
+  @param[in] CpuMpData        Pointer to CPU MP Data.\r
+  @param[in] FinishedApLimit  The number of finished APs to wait for.\r
+  @param[in] TimeLimit        The number of microseconds to wait for.\r
+**/\r
+VOID\r
+TimedWaitForApFinish (\r
+  IN CPU_MP_DATA               *CpuMpData,\r
+  IN UINT32                    FinishedApLimit,\r
+  IN UINT32                    TimeLimit\r
+  );\r
+\r
+/**\r
+  Get available system memory below 1MB by specified size.\r
+\r
+  @param[in]  CpuMpData  The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+BackupAndPrepareWakeupBuffer(\r
+  IN CPU_MP_DATA              *CpuMpData\r
+  )\r
+{\r
+  CopyMem (\r
+    (VOID *) CpuMpData->BackupBuffer,\r
+    (VOID *) CpuMpData->WakeupBuffer,\r
+    CpuMpData->BackupBufferSize\r
+    );\r
+  CopyMem (\r
+    (VOID *) CpuMpData->WakeupBuffer,\r
+    (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,\r
+    CpuMpData->AddressMap.RendezvousFunnelSize\r
+    );\r
+}\r
+\r
+/**\r
+  Restore wakeup buffer data.\r
+\r
+  @param[in]  CpuMpData  The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+RestoreWakeupBuffer(\r
+  IN CPU_MP_DATA              *CpuMpData\r
+  )\r
+{\r
+  CopyMem (\r
+    (VOID *) CpuMpData->WakeupBuffer,\r
+    (VOID *) CpuMpData->BackupBuffer,\r
+    CpuMpData->BackupBufferSize\r
+    );\r
+}\r
+\r
+/**\r
+  Allocate reset vector buffer.\r
+\r
+  @param[in, out]  CpuMpData  The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+AllocateResetVector (\r
+  IN OUT CPU_MP_DATA          *CpuMpData\r
+  )\r
+{\r
+  UINTN           ApResetVectorSize;\r
+\r
+  if (CpuMpData->WakeupBuffer == (UINTN) -1) {\r
+    ApResetVectorSize = CpuMpData->AddressMap.RendezvousFunnelSize +\r
+                          sizeof (MP_CPU_EXCHANGE_INFO);\r
+\r
+    CpuMpData->WakeupBuffer      = GetWakeupBuffer (ApResetVectorSize);\r
+    CpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN)\r
+                    (CpuMpData->WakeupBuffer + CpuMpData->AddressMap.RendezvousFunnelSize);\r
+  }\r
+  BackupAndPrepareWakeupBuffer (CpuMpData);\r
+}\r
+\r
+/**\r
+  Free AP reset vector buffer.\r
+\r
+  @param[in]  CpuMpData  The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+FreeResetVector (\r
+  IN CPU_MP_DATA              *CpuMpData\r
+  )\r
+{\r
+  RestoreWakeupBuffer (CpuMpData);\r
+}\r
+\r
+/**\r
+  This function will be called by BSP to wakeup AP.\r
+\r
+  @param[in] CpuMpData          Pointer to CPU MP Data\r
+  @param[in] Broadcast          TRUE:  Send broadcast IPI to all APs\r
+                                FALSE: Send IPI to AP by ApicId\r
+  @param[in] ProcessorNumber    The handle number of specified processor\r
+  @param[in] Procedure          The function to be invoked by AP\r
+  @param[in] ProcedureArgument  The argument to be passed into AP function\r
+**/\r
+VOID\r
+WakeUpAP (\r
+  IN CPU_MP_DATA               *CpuMpData,\r
+  IN BOOLEAN                   Broadcast,\r
+  IN UINTN                     ProcessorNumber,\r
+  IN EFI_AP_PROCEDURE          Procedure,              OPTIONAL\r
+  IN VOID                      *ProcedureArgument      OPTIONAL\r
+  )\r
+{\r
+  volatile MP_CPU_EXCHANGE_INFO    *ExchangeInfo;\r
+  UINTN                            Index;\r
+  CPU_AP_DATA                      *CpuData;\r
+  BOOLEAN                          ResetVectorRequired;\r
+  CPU_INFO_IN_HOB                  *CpuInfoInHob;\r
+\r
+  CpuMpData->FinishedCount = 0;\r
+  ResetVectorRequired = FALSE;\r
+\r
+  if (CpuMpData->ApLoopMode == ApInHltLoop ||\r
+      CpuMpData->InitFlag   != ApInitDone) {\r
+    ResetVectorRequired = TRUE;\r
+    AllocateResetVector (CpuMpData);\r
+    FillExchangeInfoData (CpuMpData);\r
+    SaveLocalApicTimerSetting (CpuMpData);\r
+  } else if (CpuMpData->ApLoopMode == ApInMwaitLoop) {\r
+    //\r
+    // Get AP target C-state each time when waking up AP,\r
+    // for it maybe updated by platform again\r
+    //\r
+    CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);\r
+  }\r
+\r
+  ExchangeInfo = CpuMpData->MpCpuExchangeInfo;\r
+\r
+  if (Broadcast) {\r
+    for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+      if (Index != CpuMpData->BspNumber) {\r
+        CpuData = &CpuMpData->CpuData[Index];\r
+        CpuData->ApFunction         = (UINTN) Procedure;\r
+        CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
+        SetApState (CpuData, CpuStateReady);\r
+        if (CpuMpData->InitFlag != ApInitConfig) {\r
+          *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
+        }\r
+      }\r
+    }\r
+    if (ResetVectorRequired) {\r
+      //\r
+      // Wakeup all APs\r
+      //\r
+      SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);\r
+    }\r
+    if (CpuMpData->InitFlag == ApInitConfig) {\r
+      //\r
+      // Here support two methods to collect AP count through adjust\r
+      // PcdCpuApInitTimeOutInMicroSeconds values.\r
+      //\r
+      // one way is set a value to just let the first AP to start the\r
+      // initialization, then through the later while loop to wait all Aps\r
+      // finsh the initialization.\r
+      // The other way is set a value to let all APs finished the initialzation.\r
+      // In this case, the later while loop is useless.\r
+      //\r
+      TimedWaitForApFinish (\r
+        CpuMpData,\r
+        PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,\r
+        PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)\r
+        );\r
+\r
+      while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) {\r
+        CpuPause();\r
+      }\r
+    } else {\r
+      //\r
+      // Wait all APs waken up if this is not the 1st broadcast of SIPI\r
+      //\r
+      for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+        CpuData = &CpuMpData->CpuData[Index];\r
+        if (Index != CpuMpData->BspNumber) {\r
+          WaitApWakeup (CpuData->StartupApSignal);\r
+        }\r
+      }\r
+    }\r
+  } else {\r
+    CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+    CpuData->ApFunction         = (UINTN) Procedure;\r
+    CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
+    SetApState (CpuData, CpuStateReady);\r
+    //\r
+    // Wakeup specified AP\r
+    //\r
+    ASSERT (CpuMpData->InitFlag != ApInitConfig);\r
+    *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
+    if (ResetVectorRequired) {\r
+      CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+      SendInitSipiSipi (\r
+        CpuInfoInHob[ProcessorNumber].ApicId,\r
+        (UINT32) ExchangeInfo->BufferStart\r
+        );\r
+    }\r
+    //\r
+    // Wait specified AP waken up\r
+    //\r
+    WaitApWakeup (CpuData->StartupApSignal);\r
+  }\r
+\r
+  if (ResetVectorRequired) {\r
+    FreeResetVector (CpuMpData);\r
+  }\r
+}\r
+\r
+/**\r
+  Calculate timeout value and return the current performance counter value.\r
+\r
+  Calculate the number of performance counter ticks required for a timeout.\r
+  If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
+  as infinity.\r
+\r
+  @param[in]  TimeoutInMicroseconds   Timeout value in microseconds.\r
+  @param[out] CurrentTime             Returns the current value of the performance counter.\r
+\r
+  @return Expected time stamp counter for timeout.\r
+          If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
+          as infinity.\r
+\r
+**/\r
+UINT64\r
+CalculateTimeout (\r
+  IN  UINTN   TimeoutInMicroseconds,\r
+  OUT UINT64  *CurrentTime\r
+  )\r
+{\r
+  UINT64 TimeoutInSeconds;\r
+  UINT64 TimestampCounterFreq;\r
+\r
+  //\r
+  // Read the current value of the performance counter\r
+  //\r
+  *CurrentTime = GetPerformanceCounter ();\r
+\r
+  //\r
+  // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
+  // as infinity.\r
+  //\r
+  if (TimeoutInMicroseconds == 0) {\r
+    return 0;\r
+  }\r
+\r
+  //\r
+  // GetPerformanceCounterProperties () returns the timestamp counter's frequency\r
+  // in Hz. \r
+  //\r
+  TimestampCounterFreq = GetPerformanceCounterProperties (NULL, NULL);\r
+\r
+  //\r
+  // Check the potential overflow before calculate the number of ticks for the timeout value.\r
+  //\r
+  if (DivU64x64Remainder (MAX_UINT64, TimeoutInMicroseconds, NULL) < TimestampCounterFreq) {\r
+    //\r
+    // Convert microseconds into seconds if direct multiplication overflows\r
+    //\r
+    TimeoutInSeconds = DivU64x32 (TimeoutInMicroseconds, 1000000);\r
+    //\r
+    // Assertion if the final tick count exceeds MAX_UINT64\r
+    //\r
+    ASSERT (DivU64x64Remainder (MAX_UINT64, TimeoutInSeconds, NULL) >= TimestampCounterFreq);\r
+    return MultU64x64 (TimestampCounterFreq, TimeoutInSeconds);\r
+  } else {\r
+    //\r
+    // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide\r
+    // it by 1,000,000, to get the number of ticks for the timeout value.\r
+    //\r
+    return DivU64x32 (\r
+             MultU64x64 (\r
+               TimestampCounterFreq,\r
+               TimeoutInMicroseconds\r
+               ),\r
+             1000000\r
+             );\r
+  }\r
+}\r
+\r
+/**\r
+  Checks whether timeout expires.\r
+\r
+  Check whether the number of elapsed performance counter ticks required for\r
+  a timeout condition has been reached.\r
+  If Timeout is zero, which means infinity, return value is always FALSE.\r
+\r
+  @param[in, out]  PreviousTime   On input,  the value of the performance counter\r
+                                  when it was last read.\r
+                                  On output, the current value of the performance\r
+                                  counter\r
+  @param[in]       TotalTime      The total amount of elapsed time in performance\r
+                                  counter ticks.\r
+  @param[in]       Timeout        The number of performance counter ticks required\r
+                                  to reach a timeout condition.\r
+\r
+  @retval TRUE                    A timeout condition has been reached.\r
+  @retval FALSE                   A timeout condition has not been reached.\r
+\r
+**/\r
+BOOLEAN\r
+CheckTimeout (\r
+  IN OUT UINT64  *PreviousTime,\r
+  IN     UINT64  *TotalTime,\r
+  IN     UINT64  Timeout\r
+  )\r
+{\r
+  UINT64  Start;\r
+  UINT64  End;\r
+  UINT64  CurrentTime;\r
+  INT64   Delta;\r
+  INT64   Cycle;\r
+\r
+  if (Timeout == 0) {\r
+    return FALSE;\r
+  }\r
+  GetPerformanceCounterProperties (&Start, &End);\r
+  Cycle = End - Start;\r
+  if (Cycle < 0) {\r
+    Cycle = -Cycle;\r
+  }\r
+  Cycle++;\r
+  CurrentTime = GetPerformanceCounter();\r
+  Delta = (INT64) (CurrentTime - *PreviousTime);\r
+  if (Start > End) {\r
+    Delta = -Delta;\r
+  }\r
+  if (Delta < 0) {\r
+    Delta += Cycle;\r
+  }\r
+  *TotalTime += Delta;\r
+  *PreviousTime = CurrentTime;\r
+  if (*TotalTime > Timeout) {\r
+    return TRUE;\r
+  }\r
+  return FALSE;\r
+}\r
+\r
+/**\r
+  Helper function that waits until the finished AP count reaches the specified\r
+  limit, or the specified timeout elapses (whichever comes first).\r
+\r
+  @param[in] CpuMpData        Pointer to CPU MP Data.\r
+  @param[in] FinishedApLimit  The number of finished APs to wait for.\r
+  @param[in] TimeLimit        The number of microseconds to wait for.\r
+**/\r
+VOID\r
+TimedWaitForApFinish (\r
+  IN CPU_MP_DATA               *CpuMpData,\r
+  IN UINT32                    FinishedApLimit,\r
+  IN UINT32                    TimeLimit\r
+  )\r
+{\r
+  //\r
+  // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0\r
+  // "infinity", so check for (TimeLimit == 0) explicitly.\r
+  //\r
+  if (TimeLimit == 0) {\r
+    return;\r
+  }\r
+\r
+  CpuMpData->TotalTime = 0;\r
+  CpuMpData->ExpectedTime = CalculateTimeout (\r
+                              TimeLimit,\r
+                              &CpuMpData->CurrentTime\r
+                              );\r
+  while (CpuMpData->FinishedCount < FinishedApLimit &&\r
+         !CheckTimeout (\r
+            &CpuMpData->CurrentTime,\r
+            &CpuMpData->TotalTime,\r
+            CpuMpData->ExpectedTime\r
+            )) {\r
+    CpuPause ();\r
+  }\r
+\r
+  if (CpuMpData->FinishedCount >= FinishedApLimit) {\r
+    DEBUG ((\r
+      DEBUG_VERBOSE,\r
+      "%a: reached FinishedApLimit=%u in %Lu microseconds\n",\r
+      __FUNCTION__,\r
+      FinishedApLimit,\r
+      DivU64x64Remainder (\r
+        MultU64x32 (CpuMpData->TotalTime, 1000000),\r
+        GetPerformanceCounterProperties (NULL, NULL),\r
+        NULL\r
+        )\r
+      ));\r
+  }\r
+}\r
+\r
+/**\r
+  Reset an AP to Idle state.\r
+\r
+  Any task being executed by the AP will be aborted and the AP\r
+  will be waiting for a new task in Wait-For-SIPI state.\r
+\r
+  @param[in] ProcessorNumber  The handle number of processor.\r
+**/\r
+VOID\r
+ResetProcessorToIdleState (\r
+  IN UINTN                     ProcessorNumber\r
+  )\r
+{\r
+  CPU_MP_DATA           *CpuMpData;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  CpuMpData->InitFlag = ApInitReconfig;\r
+  WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL);\r
+  while (CpuMpData->FinishedCount < 1) {\r
+    CpuPause ();\r
+  }\r
+  CpuMpData->InitFlag = ApInitDone;\r
+\r
+  SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
+}\r
+\r
+/**\r
+  Searches for the next waiting AP.\r
+\r
+  Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().\r
+\r
+  @param[out]  NextProcessorNumber  Pointer to the processor number of the next waiting AP.\r
+\r
+  @retval EFI_SUCCESS          The next waiting AP has been found.\r
+  @retval EFI_NOT_FOUND        No waiting AP exists.\r
+\r
+**/\r
+EFI_STATUS\r
+GetNextWaitingProcessorNumber (\r
+  OUT UINTN                    *NextProcessorNumber\r
+  )\r
+{\r
+  UINTN           ProcessorNumber;\r
+  CPU_MP_DATA     *CpuMpData;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
+    if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
+      *NextProcessorNumber = ProcessorNumber;\r
+      return EFI_SUCCESS;\r
+    }\r
+  }\r
+\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/** Checks status of specified AP.\r
+\r
+  This function checks whether the specified AP has finished the task assigned\r
+  by StartupThisAP(), and whether timeout expires.\r
+\r
+  @param[in]  ProcessorNumber       The handle number of processor.\r
+\r
+  @retval EFI_SUCCESS           Specified AP has finished task assigned by StartupThisAPs().\r
+  @retval EFI_TIMEOUT           The timeout expires.\r
+  @retval EFI_NOT_READY         Specified AP has not finished task and timeout has not expired.\r
+**/\r
+EFI_STATUS\r
+CheckThisAP (\r
+  IN UINTN        ProcessorNumber\r
+  )\r
+{\r
+  CPU_MP_DATA     *CpuMpData;\r
+  CPU_AP_DATA     *CpuData;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+  CpuData   = &CpuMpData->CpuData[ProcessorNumber];\r
+\r
+  //\r
+  //  Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
+  //  Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
+  //  value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
+  //\r
+  //\r
+  // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.\r
+  //\r
+  if (GetApState(CpuData) == CpuStateFinished) {\r
+    if (CpuData->Finished != NULL) {\r
+      *(CpuData->Finished) = TRUE;\r
+    }\r
+    SetApState (CpuData, CpuStateIdle);\r
+    return EFI_SUCCESS;\r
+  } else {\r
+    //\r
+    // If timeout expires for StartupThisAP(), report timeout.\r
+    //\r
+    if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {\r
+      if (CpuData->Finished != NULL) {\r
+        *(CpuData->Finished) = FALSE;\r
+      }\r
+      //\r
+      // Reset failed AP to idle state\r
+      //\r
+      ResetProcessorToIdleState (ProcessorNumber);\r
+\r
+      return EFI_TIMEOUT;\r
+    }\r
+  }\r
+  return EFI_NOT_READY;\r
+}\r
+\r
+/**\r
+  Checks status of all APs.\r
+\r
+  This function checks whether all APs have finished task assigned by StartupAllAPs(),\r
+  and whether timeout expires.\r
+\r
+  @retval EFI_SUCCESS           All APs have finished task assigned by StartupAllAPs().\r
+  @retval EFI_TIMEOUT           The timeout expires.\r
+  @retval EFI_NOT_READY         APs have not finished task and timeout has not expired.\r
+**/\r
+EFI_STATUS\r
+CheckAllAPs (\r
+  VOID\r
+  )\r
+{\r
+  UINTN           ProcessorNumber;\r
+  UINTN           NextProcessorNumber;\r
+  UINTN           ListIndex;\r
+  EFI_STATUS      Status;\r
+  CPU_MP_DATA     *CpuMpData;\r
+  CPU_AP_DATA     *CpuData;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  NextProcessorNumber = 0;\r
+\r
+  //\r
+  // Go through all APs that are responsible for the StartupAllAPs().\r
+  //\r
+  for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
+    if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
+      continue;\r
+    }\r
+\r
+    CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+    //\r
+    // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
+    // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
+    // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
+    //\r
+    if (GetApState(CpuData) == CpuStateFinished) {\r
+      CpuMpData->RunningCount ++;\r
+      CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
+      SetApState(CpuData, CpuStateIdle);\r
+\r
+      //\r
+      // If in Single Thread mode, then search for the next waiting AP for execution.\r
+      //\r
+      if (CpuMpData->SingleThread) {\r
+        Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);\r
+\r
+        if (!EFI_ERROR (Status)) {\r
+          WakeUpAP (\r
+            CpuMpData,\r
+            FALSE,\r
+            (UINT32) NextProcessorNumber,\r
+            CpuMpData->Procedure,\r
+            CpuMpData->ProcArguments\r
+            );\r
+         }\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // If all APs finish, return EFI_SUCCESS.\r
+  //\r
+  if (CpuMpData->RunningCount == CpuMpData->StartCount) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // If timeout expires, report timeout.\r
+  //\r
+  if (CheckTimeout (\r
+       &CpuMpData->CurrentTime,\r
+       &CpuMpData->TotalTime,\r
+       CpuMpData->ExpectedTime)\r
+       ) {\r
+    //\r
+    // If FailedCpuList is not NULL, record all failed APs in it.\r
+    //\r
+    if (CpuMpData->FailedCpuList != NULL) {\r
+      *CpuMpData->FailedCpuList =\r
+         AllocatePool ((CpuMpData->StartCount - CpuMpData->FinishedCount + 1) * sizeof (UINTN));\r
+      ASSERT (*CpuMpData->FailedCpuList != NULL);\r
+    }\r
+    ListIndex = 0;\r
+\r
+    for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
+      //\r
+      // Check whether this processor is responsible for StartupAllAPs().\r
+      //\r
+      if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
+        //\r
+        // Reset failed APs to idle state\r
+        //\r
+        ResetProcessorToIdleState (ProcessorNumber);\r
+        CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
+        if (CpuMpData->FailedCpuList != NULL) {\r
+          (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;\r
+        }\r
+      }\r
+    }\r
+    if (CpuMpData->FailedCpuList != NULL) {\r
+      (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;\r
+    }\r
+    return EFI_TIMEOUT;\r
+  }\r
+  return EFI_NOT_READY;\r
+}\r
+\r
+/**\r
+  MP Initialize Library initialization.\r
+\r
+  This service will allocate AP reset vector and wakeup all APs to do APs\r
+  initialization.\r
+\r
+  This service must be invoked before all other MP Initialize Library\r
+  service are invoked.\r
+\r
+  @retval  EFI_SUCCESS           MP initialization succeeds.\r
+  @retval  Others                MP initialization fails.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MpInitLibInitialize (\r
+  VOID\r
+  )\r
+{\r
+  CPU_MP_DATA              *OldCpuMpData;\r
+  CPU_INFO_IN_HOB          *CpuInfoInHob;\r
+  UINT32                   MaxLogicalProcessorNumber;\r
+  UINT32                   ApStackSize;\r
+  MP_ASSEMBLY_ADDRESS_MAP  AddressMap;\r
+  UINTN                    BufferSize;\r
+  UINT32                   MonitorFilterSize;\r
+  VOID                     *MpBuffer;\r
+  UINTN                    Buffer;\r
+  CPU_MP_DATA              *CpuMpData;\r
+  UINT8                    ApLoopMode;\r
+  UINT8                    *MonitorBuffer;\r
+  UINTN                    Index;\r
+  UINTN                    ApResetVectorSize;\r
+  UINTN                    BackupBufferAddr;\r
+\r
+  OldCpuMpData = GetCpuMpDataFromGuidedHob ();\r
+  if (OldCpuMpData == NULL) {\r
+    MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);\r
+  } else {\r
+    MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;\r
+  }\r
+  ASSERT (MaxLogicalProcessorNumber != 0);\r
+\r
+  AsmGetAddressMap (&AddressMap);\r
+  ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);\r
+  ApStackSize = PcdGet32(PcdCpuApStackSize);\r
+  ApLoopMode  = GetApLoopMode (&MonitorFilterSize);\r
+\r
+  BufferSize  = ApStackSize * MaxLogicalProcessorNumber;\r
+  BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;\r
+  BufferSize += sizeof (CPU_MP_DATA);\r
+  BufferSize += ApResetVectorSize;\r
+  BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;\r
+  MpBuffer    = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
+  ASSERT (MpBuffer != NULL);\r
+  ZeroMem (MpBuffer, BufferSize);\r
+  Buffer = (UINTN) MpBuffer;\r
+\r
+  MonitorBuffer    = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);\r
+  BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;\r
+  CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);\r
+  CpuMpData->Buffer           = Buffer;\r
+  CpuMpData->CpuApStackSize   = ApStackSize;\r
+  CpuMpData->BackupBuffer     = BackupBufferAddr;\r
+  CpuMpData->BackupBufferSize = ApResetVectorSize;\r
+  CpuMpData->WakeupBuffer     = (UINTN) -1;\r
+  CpuMpData->CpuCount         = 1;\r
+  CpuMpData->BspNumber        = 0;\r
+  CpuMpData->WaitEvent        = NULL;\r
+  CpuMpData->SwitchBspFlag    = FALSE;\r
+  CpuMpData->CpuData          = (CPU_AP_DATA *) (CpuMpData + 1);\r
+  CpuMpData->CpuInfoInHob     = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);\r
+  CpuMpData->MicrocodePatchAddress    = PcdGet64 (PcdCpuMicrocodePatchAddress);\r
+  CpuMpData->MicrocodePatchRegionSize = PcdGet64 (PcdCpuMicrocodePatchRegionSize);\r
+  InitializeSpinLock(&CpuMpData->MpLock);\r
+  //\r
+  // Save BSP's Control registers to APs\r
+  //\r
+  SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);\r
+  //\r
+  // Set BSP basic information\r
+  //\r
+  InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer);\r
+  //\r
+  // Save assembly code information\r
+  //\r
+  CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));\r
+  //\r
+  // Finally set AP loop mode\r
+  //\r
+  CpuMpData->ApLoopMode = ApLoopMode;\r
+  DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));\r
+  //\r
+  // Set up APs wakeup signal buffer\r
+  //\r
+  for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {\r
+    CpuMpData->CpuData[Index].StartupApSignal =\r
+      (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);\r
+  }\r
+  //\r
+  // Load Microcode on BSP\r
+  //\r
+  MicrocodeDetect (CpuMpData);\r
+  //\r
+  // Store BSP's MTRR setting\r
+  //\r
+  MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
+  //\r
+  // Enable the local APIC for Virtual Wire Mode.\r
+  //\r
+  ProgramVirtualWireMode ();\r
+\r
+  if (OldCpuMpData == NULL) {\r
+    if (MaxLogicalProcessorNumber > 1) {\r
+      //\r
+      // Wakeup all APs and calculate the processor count in system\r
+      //\r
+      CollectProcessorCount (CpuMpData);\r
+    }\r
+  } else {\r
+    //\r
+    // APs have been wakeup before, just get the CPU Information\r
+    // from HOB\r
+    //\r
+    CpuMpData->CpuCount  = OldCpuMpData->CpuCount;\r
+    CpuMpData->BspNumber = OldCpuMpData->BspNumber;\r
+    CpuMpData->InitFlag  = ApInitReconfig;\r
+    CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;\r
+    CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+    for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+      InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);\r
+      if (CpuInfoInHob[Index].InitialApicId >= 255 || Index > 254) {\r
+        CpuMpData->X2ApicEnable = TRUE;\r
+      }\r
+      CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;\r
+      CpuMpData->CpuData[Index].ApFunction = 0;\r
+      CopyMem (\r
+        &CpuMpData->CpuData[Index].VolatileRegisters,\r
+        &CpuMpData->CpuData[0].VolatileRegisters,\r
+        sizeof (CPU_VOLATILE_REGISTERS)\r
+        );\r
+    }\r
+    if (MaxLogicalProcessorNumber > 1) {\r
+      //\r
+      // Wakeup APs to do some AP initialize sync\r
+      //\r
+      WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);\r
+      //\r
+      // Wait for all APs finished initialization\r
+      //\r
+      while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
+        CpuPause ();\r
+      }\r
+      CpuMpData->InitFlag = ApInitDone;\r
+      for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+        SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
+      }\r
+    }\r
+  }\r
+\r
+  //\r
+  // Initialize global data for MP support\r
+  //\r
+  InitMpGlobalData (CpuMpData);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Gets detailed MP-related information on the requested processor at the\r
+  instant this call is made. This service may only be called from the BSP.\r
+\r
+  @param[in]  ProcessorNumber       The handle number of processor.\r
+  @param[out] ProcessorInfoBuffer   A pointer to the buffer where information for\r
+                                    the requested processor is deposited.\r
+  @param[out]  HealthData            Return processor health data.\r
+\r
+  @retval EFI_SUCCESS             Processor information was returned.\r
+  @retval EFI_DEVICE_ERROR        The calling processor is an AP.\r
+  @retval EFI_INVALID_PARAMETER   ProcessorInfoBuffer is NULL.\r
+  @retval EFI_NOT_FOUND           The processor with the handle specified by\r
+                                  ProcessorNumber does not exist in the platform.\r
+  @retval EFI_NOT_READY           MP Initialize Library is not initialized.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MpInitLibGetProcessorInfo (\r
+  IN  UINTN                      ProcessorNumber,\r
+  OUT EFI_PROCESSOR_INFORMATION  *ProcessorInfoBuffer,\r
+  OUT EFI_HEALTH_FLAGS           *HealthData  OPTIONAL\r
+  )\r
+{\r
+  CPU_MP_DATA            *CpuMpData;\r
+  UINTN                  CallerNumber;\r
+  CPU_INFO_IN_HOB        *CpuInfoInHob;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+  CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+\r
+  //\r
+  // Check whether caller processor is BSP\r
+  //\r
+  MpInitLibWhoAmI (&CallerNumber);\r
+  if (CallerNumber != CpuMpData->BspNumber) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  if (ProcessorInfoBuffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (ProcessorNumber >= CpuMpData->CpuCount) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;\r
+  ProcessorInfoBuffer->StatusFlag  = 0;\r
+  if (ProcessorNumber == CpuMpData->BspNumber) {\r
+    ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
+  }\r
+  if (CpuMpData->CpuData[ProcessorNumber].CpuHealthy) {\r
+    ProcessorInfoBuffer->StatusFlag |= PROCESSOR_HEALTH_STATUS_BIT;\r
+  }\r
+  if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
+    ProcessorInfoBuffer->StatusFlag &= ~PROCESSOR_ENABLED_BIT;\r
+  } else {\r
+    ProcessorInfoBuffer->StatusFlag |= PROCESSOR_ENABLED_BIT;\r
+  }\r
+\r
+  //\r
+  // Get processor location information\r
+  //\r
+  GetProcessorLocationByApicId (\r
+    CpuInfoInHob[ProcessorNumber].ApicId,\r
+    &ProcessorInfoBuffer->Location.Package,\r
+    &ProcessorInfoBuffer->Location.Core,\r
+    &ProcessorInfoBuffer->Location.Thread\r
+    );\r
+\r
+  if (HealthData != NULL) {\r
+    HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Worker function to switch the requested AP to be the BSP from that point onward.\r
+\r
+  @param[in] ProcessorNumber   The handle number of AP that is to become the new BSP.\r
+  @param[in] EnableOldBSP      If TRUE, then the old BSP will be listed as an\r
+                               enabled AP. Otherwise, it will be disabled.\r
+\r
+  @retval EFI_SUCCESS          BSP successfully switched.\r
+  @retval others               Failed to switch BSP. \r
+\r
+**/\r
+EFI_STATUS\r
+SwitchBSPWorker (\r
+  IN UINTN                     ProcessorNumber,\r
+  IN BOOLEAN                   EnableOldBSP\r
+  )\r
+{\r
+  CPU_MP_DATA                  *CpuMpData;\r
+  UINTN                        CallerNumber;\r
+  CPU_STATE                    State;\r
+  MSR_IA32_APIC_BASE_REGISTER  ApicBaseMsr;\r
+  BOOLEAN                      OldInterruptState;\r
+  BOOLEAN                      OldTimerInterruptState;\r
+\r
+  //\r
+  // Save and Disable Local APIC timer interrupt\r
+  //\r
+  OldTimerInterruptState = GetApicTimerInterruptState ();\r
+  DisableApicTimerInterrupt ();\r
+  //\r
+  // Before send both BSP and AP to a procedure to exchange their roles,\r
+  // interrupt must be disabled. This is because during the exchange role\r
+  // process, 2 CPU may use 1 stack. If interrupt happens, the stack will\r
+  // be corrupted, since interrupt return address will be pushed to stack\r
+  // by hardware.\r
+  //\r
+  OldInterruptState = SaveAndDisableInterrupts ();\r
+\r
+  //\r
+  // Mask LINT0 & LINT1 for the old BSP\r
+  //\r
+  DisableLvtInterrupts ();\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  //\r
+  // Check whether caller processor is BSP\r
+  //\r
+  MpInitLibWhoAmI (&CallerNumber);\r
+  if (CallerNumber != CpuMpData->BspNumber) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  if (ProcessorNumber >= CpuMpData->CpuCount) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  //\r
+  // Check whether specified AP is disabled\r
+  //\r
+  State = GetApState (&CpuMpData->CpuData[ProcessorNumber]);\r
+  if (State == CpuStateDisabled) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Check whether ProcessorNumber specifies the current BSP\r
+  //\r
+  if (ProcessorNumber == CpuMpData->BspNumber) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Check whether specified AP is busy\r
+  //\r
+  if (State == CpuStateBusy) {\r
+    return EFI_NOT_READY;\r
+  }\r
+\r
+  CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
+  CpuMpData->APInfo.State  = CPU_SWITCH_STATE_IDLE;\r
+  CpuMpData->SwitchBspFlag = TRUE;\r
+  CpuMpData->NewBspNumber  = ProcessorNumber;\r
+\r
+  //\r
+  // Clear the BSP bit of MSR_IA32_APIC_BASE\r
+  //\r
+  ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
+  ApicBaseMsr.Bits.BSP = 0;\r
+  AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
+\r
+  //\r
+  // Need to wakeUp AP (future BSP).\r
+  //\r
+  WakeUpAP (CpuMpData, FALSE, ProcessorNumber, FutureBSPProc, CpuMpData);\r
+\r
+  AsmExchangeRole (&CpuMpData->BSPInfo, &CpuMpData->APInfo);\r
+\r
+  //\r
+  // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP\r
+  //\r
+  ApicBaseMsr.Uint64 = AsmReadMsr64 (MSR_IA32_APIC_BASE);\r
+  ApicBaseMsr.Bits.BSP = 1;\r
+  AsmWriteMsr64 (MSR_IA32_APIC_BASE, ApicBaseMsr.Uint64);\r
+\r
+  //\r
+  // Wait for old BSP finished AP task\r
+  //\r
+  while (GetApState (&CpuMpData->CpuData[CallerNumber]) != CpuStateFinished) {\r
+    CpuPause ();\r
+  }\r
+\r
+  CpuMpData->SwitchBspFlag = FALSE;\r
+  //\r
+  // Set old BSP enable state\r
+  //\r
+  if (!EnableOldBSP) {\r
+    SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateDisabled);\r
+  } else {\r
+    SetApState (&CpuMpData->CpuData[CallerNumber], CpuStateIdle);\r
+  }\r
+  //\r
+  // Save new BSP number\r
+  //\r
+  CpuMpData->BspNumber = (UINT32) ProcessorNumber;\r
+\r
+  //\r
+  // Restore interrupt state.\r
+  //\r
+  SetInterruptState (OldInterruptState);\r
+\r
+  if (OldTimerInterruptState) {\r
+    EnableApicTimerInterrupt ();\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Worker function to let the caller enable or disable an AP from this point onward.\r
+  This service may only be called from the BSP.\r
+\r
+  @param[in] ProcessorNumber   The handle number of AP.\r
+  @param[in] EnableAP          Specifies the new state for the processor for\r
+                               enabled, FALSE for disabled.\r
+  @param[in] HealthFlag        If not NULL, a pointer to a value that specifies\r
+                               the new health status of the AP.\r
+\r
+  @retval EFI_SUCCESS          The specified AP was enabled or disabled successfully.\r
+  @retval others               Failed to Enable/Disable AP.\r
+\r
+**/\r
+EFI_STATUS\r
+EnableDisableApWorker (\r
+  IN  UINTN                     ProcessorNumber,\r
+  IN  BOOLEAN                   EnableAP,\r
+  IN  UINT32                    *HealthFlag OPTIONAL\r
+  )\r
+{\r
+  CPU_MP_DATA               *CpuMpData;\r
+  UINTN                     CallerNumber;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  //\r
+  // Check whether caller processor is BSP\r
+  //\r
+  MpInitLibWhoAmI (&CallerNumber);\r
+  if (CallerNumber != CpuMpData->BspNumber) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  if (ProcessorNumber == CpuMpData->BspNumber) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (ProcessorNumber >= CpuMpData->CpuCount) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  if (!EnableAP) {\r
+    SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateDisabled);\r
+  } else {\r
+    ResetProcessorToIdleState (ProcessorNumber);\r
+  }\r
+\r
+  if (HealthFlag != NULL) {\r
+    CpuMpData->CpuData[ProcessorNumber].CpuHealthy =\r
+          (BOOLEAN) ((*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT) != 0);\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  This return the handle number for the calling processor.  This service may be\r
+  called from the BSP and APs.\r
+\r
+  @param[out] ProcessorNumber  Pointer to the handle number of AP.\r
+                               The range is from 0 to the total number of\r
+                               logical processors minus 1. The total number of\r
+                               logical processors can be retrieved by\r
+                               MpInitLibGetNumberOfProcessors().\r
+\r
+  @retval EFI_SUCCESS             The current processor handle number was returned\r
+                                  in ProcessorNumber.\r
+  @retval EFI_INVALID_PARAMETER   ProcessorNumber is NULL.\r
+  @retval EFI_NOT_READY           MP Initialize Library is not initialized.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MpInitLibWhoAmI (\r
+  OUT UINTN                    *ProcessorNumber\r
+  )\r
+{\r
+  CPU_MP_DATA           *CpuMpData;\r
+\r
+  if (ProcessorNumber == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  return GetProcessorNumber (CpuMpData, ProcessorNumber);\r
+}\r
+\r
+/**\r
+  Retrieves the number of logical processor in the platform and the number of\r
+  those logical processors that are enabled on this boot. This service may only\r
+  be called from the BSP.\r
+\r
+  @param[out] NumberOfProcessors          Pointer to the total number of logical\r
+                                          processors in the system, including the BSP\r
+                                          and disabled APs.\r
+  @param[out] NumberOfEnabledProcessors   Pointer to the number of enabled logical\r
+                                          processors that exist in system, including\r
+                                          the BSP.\r
+\r
+  @retval EFI_SUCCESS             The number of logical processors and enabled\r
+                                  logical processors was retrieved.\r
+  @retval EFI_DEVICE_ERROR        The calling processor is an AP.\r
+  @retval EFI_INVALID_PARAMETER   NumberOfProcessors is NULL and NumberOfEnabledProcessors\r
+                                  is NULL.\r
+  @retval EFI_NOT_READY           MP Initialize Library is not initialized.\r
+\r
+**/\r
+EFI_STATUS\r
 EFIAPI\r
 MpInitLibGetNumberOfProcessors (\r
   OUT UINTN                     *NumberOfProcessors,       OPTIONAL\r
   OUT UINTN                     *NumberOfEnabledProcessors OPTIONAL\r
   )\r
 {\r
-  return EFI_UNSUPPORTED;\r
+  CPU_MP_DATA             *CpuMpData;\r
+  UINTN                   CallerNumber;\r
+  UINTN                   ProcessorNumber;\r
+  UINTN                   EnabledProcessorNumber;\r
+  UINTN                   Index;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  if ((NumberOfProcessors == NULL) && (NumberOfEnabledProcessors == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Check whether caller processor is BSP\r
+  //\r
+  MpInitLibWhoAmI (&CallerNumber);\r
+  if (CallerNumber != CpuMpData->BspNumber) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  ProcessorNumber        = CpuMpData->CpuCount;\r
+  EnabledProcessorNumber = 0;\r
+  for (Index = 0; Index < ProcessorNumber; Index++) {\r
+    if (GetApState (&CpuMpData->CpuData[Index]) != CpuStateDisabled) {\r
+      EnabledProcessorNumber ++;\r
+    }\r
+  }\r
+\r
+  if (NumberOfProcessors != NULL) {\r
+    *NumberOfProcessors = ProcessorNumber;\r
+  }\r
+  if (NumberOfEnabledProcessors != NULL) {\r
+    *NumberOfEnabledProcessors = EnabledProcessorNumber;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+\r
+/**\r
+  Worker function to execute a caller provided function on all enabled APs.\r
+\r
+  @param[in]  Procedure               A pointer to the function to be run on\r
+                                      enabled APs of the system.\r
+  @param[in]  SingleThread            If TRUE, then all the enabled APs execute\r
+                                      the function specified by Procedure one by\r
+                                      one, in ascending order of processor handle\r
+                                      number.  If FALSE, then all the enabled APs\r
+                                      execute the function specified by Procedure\r
+                                      simultaneously.\r
+  @param[in]  WaitEvent               The event created by the caller with CreateEvent()\r
+                                      service.\r
+  @param[in]  TimeoutInMicroseconds   Indicates the time limit in microseconds for\r
+                                      APs to return from Procedure, either for\r
+                                      blocking or non-blocking mode.\r
+  @param[in]  ProcedureArgument       The parameter passed into Procedure for\r
+                                      all APs.\r
+  @param[out] FailedCpuList           If all APs finish successfully, then its\r
+                                      content is set to NULL. If not all APs\r
+                                      finish before timeout expires, then its\r
+                                      content is set to address of the buffer\r
+                                      holding handle numbers of the failed APs.\r
+\r
+  @retval EFI_SUCCESS             In blocking mode, all APs have finished before\r
+                                  the timeout expired.\r
+  @retval EFI_SUCCESS             In non-blocking mode, function has been dispatched\r
+                                  to all enabled APs.\r
+  @retval others                  Failed to Startup all APs.\r
+\r
+**/\r
+EFI_STATUS\r
+StartupAllAPsWorker (\r
+  IN  EFI_AP_PROCEDURE          Procedure,\r
+  IN  BOOLEAN                   SingleThread,\r
+  IN  EFI_EVENT                 WaitEvent               OPTIONAL,\r
+  IN  UINTN                     TimeoutInMicroseconds,\r
+  IN  VOID                      *ProcedureArgument      OPTIONAL,\r
+  OUT UINTN                     **FailedCpuList         OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+  CPU_MP_DATA             *CpuMpData;\r
+  UINTN                   ProcessorCount;\r
+  UINTN                   ProcessorNumber;\r
+  UINTN                   CallerNumber;\r
+  CPU_AP_DATA             *CpuData;\r
+  BOOLEAN                 HasEnabledAp;\r
+  CPU_STATE               ApState;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  if (FailedCpuList != NULL) {\r
+    *FailedCpuList = NULL;\r
+  }\r
+\r
+  if (CpuMpData->CpuCount == 1) {\r
+    return EFI_NOT_STARTED;\r
+  }\r
+\r
+  if (Procedure == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Check whether caller processor is BSP\r
+  //\r
+  MpInitLibWhoAmI (&CallerNumber);\r
+  if (CallerNumber != CpuMpData->BspNumber) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  //\r
+  // Update AP state\r
+  //\r
+  CheckAndUpdateApsStatus ();\r
+\r
+  ProcessorCount = CpuMpData->CpuCount;\r
+  HasEnabledAp   = FALSE;\r
+  //\r
+  // Check whether all enabled APs are idle.\r
+  // If any enabled AP is not idle, return EFI_NOT_READY.\r
+  //\r
+  for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
+    CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+    if (ProcessorNumber != CpuMpData->BspNumber) {\r
+      ApState = GetApState (CpuData);\r
+      if (ApState != CpuStateDisabled) {\r
+        HasEnabledAp = TRUE;\r
+        if (ApState != CpuStateIdle) {\r
+          //\r
+          // If any enabled APs are busy, return EFI_NOT_READY.\r
+          //\r
+          return EFI_NOT_READY;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  if (!HasEnabledAp) {\r
+    //\r
+    // If no enabled AP exists, return EFI_NOT_STARTED.\r
+    //\r
+    return EFI_NOT_STARTED;\r
+  }\r
+\r
+  CpuMpData->StartCount = 0;\r
+  for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
+    CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+    CpuData->Waiting = FALSE;\r
+    if (ProcessorNumber != CpuMpData->BspNumber) {\r
+      if (CpuData->State == CpuStateIdle) {\r
+        //\r
+        // Mark this processor as responsible for current calling.\r
+        //\r
+        CpuData->Waiting = TRUE;\r
+        CpuMpData->StartCount++;\r
+      }\r
+    }\r
+  }\r
+\r
+  CpuMpData->Procedure     = Procedure;\r
+  CpuMpData->ProcArguments = ProcedureArgument;\r
+  CpuMpData->SingleThread  = SingleThread;\r
+  CpuMpData->FinishedCount = 0;\r
+  CpuMpData->RunningCount  = 0;\r
+  CpuMpData->FailedCpuList = FailedCpuList;\r
+  CpuMpData->ExpectedTime  = CalculateTimeout (\r
+                               TimeoutInMicroseconds,\r
+                               &CpuMpData->CurrentTime\r
+                               );\r
+  CpuMpData->TotalTime     = 0;\r
+  CpuMpData->WaitEvent     = WaitEvent;\r
+\r
+  if (!SingleThread) {\r
+    WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);\r
+  } else {\r
+    for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
+      if (ProcessorNumber == CallerNumber) {\r
+        continue;\r
+      }\r
+      if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
+        WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
+        break;\r
+      }\r
+    }\r
+  }\r
+\r
+  Status = EFI_SUCCESS;\r
+  if (WaitEvent == NULL) {\r
+    do {\r
+      Status = CheckAllAPs ();\r
+    } while (Status == EFI_NOT_READY);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Worker function to let the caller get one enabled AP to execute a caller-provided\r
+  function.\r
+\r
+  @param[in]  Procedure               A pointer to the function to be run on\r
+                                      enabled APs of the system.\r
+  @param[in]  ProcessorNumber         The handle number of the AP.\r
+  @param[in]  WaitEvent               The event created by the caller with CreateEvent()\r
+                                      service.\r
+  @param[in]  TimeoutInMicroseconds   Indicates the time limit in microseconds for\r
+                                      APs to return from Procedure, either for\r
+                                      blocking or non-blocking mode.\r
+  @param[in]  ProcedureArgument       The parameter passed into Procedure for\r
+                                      all APs.\r
+  @param[out] Finished                If AP returns from Procedure before the\r
+                                      timeout expires, its content is set to TRUE.\r
+                                      Otherwise, the value is set to FALSE.\r
+\r
+  @retval EFI_SUCCESS             In blocking mode, specified AP finished before\r
+                                  the timeout expires.\r
+  @retval others                  Failed to Startup AP.\r
+\r
+**/\r
+EFI_STATUS\r
+StartupThisAPWorker (\r
+  IN  EFI_AP_PROCEDURE          Procedure,\r
+  IN  UINTN                     ProcessorNumber,\r
+  IN  EFI_EVENT                 WaitEvent               OPTIONAL,\r
+  IN  UINTN                     TimeoutInMicroseconds,\r
+  IN  VOID                      *ProcedureArgument      OPTIONAL,\r
+  OUT BOOLEAN                   *Finished               OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+  CPU_MP_DATA             *CpuMpData;\r
+  CPU_AP_DATA             *CpuData;\r
+  UINTN                   CallerNumber;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  if (Finished != NULL) {\r
+    *Finished = FALSE;\r
+  }\r
+\r
+  //\r
+  // Check whether caller processor is BSP\r
+  //\r
+  MpInitLibWhoAmI (&CallerNumber);\r
+  if (CallerNumber != CpuMpData->BspNumber) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  //\r
+  // Check whether processor with the handle specified by ProcessorNumber exists\r
+  //\r
+  if (ProcessorNumber >= CpuMpData->CpuCount) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  //\r
+  // Check whether specified processor is BSP\r
+  //\r
+  if (ProcessorNumber == CpuMpData->BspNumber) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Check parameter Procedure\r
+  //\r
+  if (Procedure == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Update AP state\r
+  //\r
+  CheckAndUpdateApsStatus ();\r
+\r
+  //\r
+  // Check whether specified AP is disabled\r
+  //\r
+  if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // If WaitEvent is not NULL, execute in non-blocking mode.\r
+  // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
+  // CheckAPsStatus() will check completion and timeout periodically.\r
+  //\r
+  CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+  CpuData->WaitEvent    = WaitEvent;\r
+  CpuData->Finished     = Finished;\r
+  CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);\r
+  CpuData->TotalTime    = 0;\r
+\r
+  WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
+\r
+  //\r
+  // If WaitEvent is NULL, execute in blocking mode.\r
+  // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.\r
+  //\r
+  Status = EFI_SUCCESS;\r
+  if (WaitEvent == NULL) {\r
+    do {\r
+      Status = CheckThisAP (ProcessorNumber);\r
+    } while (Status == EFI_NOT_READY);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Get pointer to CPU MP Data structure from GUIDed HOB.\r
+\r
+  @return  The pointer to CPU MP Data structure.\r
+**/\r
+CPU_MP_DATA *\r
+GetCpuMpDataFromGuidedHob (\r
+  VOID\r
+  )\r
+{\r
+  EFI_HOB_GUID_TYPE       *GuidHob;\r
+  VOID                    *DataInHob;\r
+  CPU_MP_DATA             *CpuMpData;\r
+\r
+  CpuMpData = NULL;\r
+  GuidHob = GetFirstGuidHob (&mCpuInitMpLibHobGuid);\r
+  if (GuidHob != NULL) {\r
+    DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
+    CpuMpData = (CPU_MP_DATA *) (*(UINTN *) DataInHob);\r
+  }\r
+  return CpuMpData;\r
 }\r
+\r