]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/CpuDxe/CpuMp.c
MdePkg: Add definition for new warning code EFI_WARN_FILE_SYSTEM.
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
index a37e45aebdeb0959be07cd69f8c50ad1c61ae071..98fdfdf5e0e7b9bdc2c96cf47a9811bff44e53ce 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   CPU DXE Module.\r
 \r
-  Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2008 - 2015, 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
@@ -20,11 +20,14 @@ UINTN gApStackSize;
 UINTN gPollInterval = 100; // 100 microseconds\r
 \r
 MP_SYSTEM_DATA mMpSystemData;\r
+EFI_HANDLE     mMpServiceHandle       = NULL;\r
+EFI_EVENT      mExitBootServicesEvent = (EFI_EVENT)NULL;\r
 \r
 VOID *mCommonStack = 0;\r
 VOID *mTopOfApCommonStack = 0;\r
 VOID *mApStackStart = 0;\r
 \r
+volatile BOOLEAN mAPsAlreadyInitFinished = FALSE;\r
 volatile BOOLEAN mStopCheckAllAPsStatus = TRUE;\r
 \r
 EFI_MP_SERVICES_PROTOCOL  mMpServicesTemplate = {\r
@@ -51,6 +54,7 @@ GetMpSpinLock (
   while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
     CpuPause ();\r
   }\r
+  CpuData->LockSelf = GetApicId ();\r
 }\r
 \r
 /**\r
@@ -174,7 +178,7 @@ TestCpuStatusFlag (
   Ret = CpuData->Info.StatusFlag & Flags;\r
   ReleaseMpSpinLock (CpuData);\r
 \r
-  return !!(Ret);\r
+  return (BOOLEAN) (Ret != 0);\r
 }\r
 \r
 /**\r
@@ -294,14 +298,61 @@ CheckAndUpdateAllAPsToIdleState (
           SetApProcedure (&mMpSystemData.CpuDatas[NextNumber],\r
                           mMpSystemData.Procedure,\r
                           mMpSystemData.ProcedureArgument);\r
+          //\r
+          // If this AP previous state is blocked, we should\r
+          // wake up this AP by sent a SIPI. and avoid\r
+          // re-involve the sleeping state. we must call\r
+          // SetApProcedure() first.\r
+          //\r
+          ResetProcessorToIdleState (&mMpSystemData.CpuDatas[NextNumber]);\r
         }\r
       }\r
-\r
       SetApState (CpuData, CpuStateIdle);\r
     }\r
   }\r
 }\r
 \r
+/**\r
+  Check if all APs are in state CpuStateSleeping.\r
+\r
+  Return TRUE if all APs are in the CpuStateSleeping state.  Do not\r
+  check the state of the BSP or any disabled APs.\r
+\r
+  @retval TRUE   All APs are in CpuStateSleeping state.\r
+  @retval FALSE  One or more APs are not in CpuStateSleeping state.\r
+\r
+**/\r
+BOOLEAN\r
+CheckAllAPsSleeping (\r
+  VOID\r
+  )\r
+{\r
+  UINTN           ProcessorNumber;\r
+  CPU_DATA_BLOCK  *CpuData;\r
+\r
+  for (ProcessorNumber = 0; ProcessorNumber < mMpSystemData.NumberOfProcessors; ProcessorNumber++) {\r
+    CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
+    if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
+      //\r
+      // Skip BSP\r
+      //\r
+      continue;\r
+    }\r
+\r
+    if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
+      //\r
+      // Skip Disabled processors\r
+      //\r
+      continue;\r
+    }\r
+\r
+    if (GetApState (CpuData) != CpuStateSleeping) {\r
+      return FALSE;\r
+    }\r
+  }\r
+  return TRUE;\r
+}\r
+\r
 /**\r
   If the timeout expires before all APs returns from Procedure,\r
   we should forcibly terminate the executing AP and fill FailedList back\r
@@ -339,7 +390,8 @@ ResetAllFailedAPs (
     }\r
 \r
     CpuState = GetApState (CpuData);\r
-    if (CpuState != CpuStateIdle) {\r
+    if (CpuState != CpuStateIdle &&\r
+        CpuState != CpuStateSleeping) {\r
       if (mMpSystemData.FailedList != NULL) {\r
         (*mMpSystemData.FailedList)[mMpSystemData.FailedListIndex++] = Number;\r
       }\r
@@ -611,6 +663,7 @@ StartupAllAPs (
   CPU_DATA_BLOCK        *CpuData;\r
   UINTN                 Number;\r
   CPU_STATE             APInitialState;\r
+  CPU_STATE             CpuState;\r
 \r
   CpuData = NULL;\r
 \r
@@ -630,6 +683,11 @@ StartupAllAPs (
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
+  //\r
+  // temporarily stop checkAllAPsStatus for avoid resource dead-lock.\r
+  //\r
+  mStopCheckAllAPsStatus = TRUE;\r
+\r
   for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
     CpuData = &mMpSystemData.CpuDatas[Number];\r
     if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
@@ -646,21 +704,18 @@ StartupAllAPs (
       continue;\r
     }\r
 \r
-    if (GetApState (CpuData) != CpuStateIdle) {\r
+    CpuState = GetApState (CpuData);\r
+    if (CpuState != CpuStateIdle &&\r
+        CpuState != CpuStateSleeping) {\r
       return EFI_NOT_READY;\r
     }\r
   }\r
 \r
-  //\r
-  // temporarily stop checkAllAPsStatus for initialize parameters.\r
-  //\r
-  mStopCheckAllAPsStatus = TRUE;\r
-\r
   mMpSystemData.Procedure         = Procedure;\r
   mMpSystemData.ProcedureArgument = ProcedureArgument;\r
   mMpSystemData.WaitEvent         = WaitEvent;\r
   mMpSystemData.Timeout           = TimeoutInMicroseconds;\r
-  mMpSystemData.TimeoutActive     = !!(TimeoutInMicroseconds);\r
+  mMpSystemData.TimeoutActive     = (BOOLEAN) (TimeoutInMicroseconds != 0);\r
   mMpSystemData.FinishCount       = 0;\r
   mMpSystemData.StartCount        = 0;\r
   mMpSystemData.SingleThread      = SingleThread;\r
@@ -690,13 +745,24 @@ StartupAllAPs (
     // state 1 by 1, until the previous 1 finished its task\r
     // if not "SingleThread", all APs are put to ready state from the beginning\r
     //\r
-    if (GetApState (CpuData) == CpuStateIdle) {\r
+    CpuState = GetApState (CpuData);\r
+    if (CpuState == CpuStateIdle ||\r
+        CpuState == CpuStateSleeping) {\r
       mMpSystemData.StartCount++;\r
 \r
       SetApState (CpuData, APInitialState);\r
 \r
       if (APInitialState == CpuStateReady) {\r
         SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
+        //\r
+        // If this AP previous state is Sleeping, we should\r
+        // wake up this AP by sent a SIPI. and avoid\r
+        // re-involve the sleeping state. we must call\r
+        // SetApProcedure() first.\r
+        //\r
+        if (CpuState == CpuStateSleeping) {\r
+          ResetProcessorToIdleState (CpuData);\r
+        }\r
       }\r
 \r
       if (SingleThread) {\r
@@ -714,6 +780,11 @@ StartupAllAPs (
     return EFI_SUCCESS;\r
   }\r
 \r
+  //\r
+  // Blocking temporarily stop CheckAllAPsStatus()\r
+  //\r
+  mStopCheckAllAPsStatus = TRUE;\r
+\r
   while (TRUE) {\r
     CheckAndUpdateAllAPsToIdleState ();\r
     if (mMpSystemData.FinishCount == mMpSystemData.StartCount) {\r
@@ -730,7 +801,7 @@ StartupAllAPs (
       goto Done;\r
     }\r
 \r
-    gBS->Stall (gPollInterval);\r
+    MicroSecondDelay (gPollInterval);\r
     mMpSystemData.Timeout -= gPollInterval;\r
   }\r
 \r
@@ -838,6 +909,7 @@ StartupThisAP (
   )\r
 {\r
   CPU_DATA_BLOCK        *CpuData;\r
+  CPU_STATE             CpuState;\r
 \r
   CpuData = NULL;\r
 \r
@@ -857,28 +929,39 @@ StartupThisAP (
     return EFI_NOT_FOUND;\r
   }\r
 \r
+  //\r
+  // temporarily stop checkAllAPsStatus for avoid resource dead-lock.\r
+  //\r
+  mStopCheckAllAPsStatus = TRUE;\r
+\r
   CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
   if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) ||\r
       !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (GetApState (CpuData) != CpuStateIdle) {\r
+  CpuState = GetApState (CpuData);\r
+  if (CpuState != CpuStateIdle &&\r
+      CpuState != CpuStateSleeping) {\r
     return EFI_NOT_READY;\r
   }\r
 \r
-  //\r
-  // temporarily stop checkAllAPsStatus for initialize parameters.\r
-  //\r
-  mStopCheckAllAPsStatus = TRUE;\r
-\r
   SetApState (CpuData, CpuStateReady);\r
 \r
   SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
+  //\r
+  // If this AP previous state is Sleeping, we should\r
+  // wake up this AP by sent a SIPI. and avoid\r
+  // re-involve the sleeping state. we must call\r
+  // SetApProcedure() first.\r
+  //\r
+  if (CpuState == CpuStateSleeping) {\r
+    ResetProcessorToIdleState (CpuData);\r
+  }\r
 \r
   CpuData->Timeout = TimeoutInMicroseconds;\r
   CpuData->WaitEvent = WaitEvent;\r
-  CpuData->TimeoutActive = !!(TimeoutInMicroseconds);\r
+  CpuData->TimeoutActive = (BOOLEAN) (TimeoutInMicroseconds != 0);\r
   CpuData->Finished = Finished;\r
 \r
   mStopCheckAllAPsStatus = FALSE;\r
@@ -904,7 +987,7 @@ StartupThisAP (
       return EFI_TIMEOUT;\r
     }\r
 \r
-    gBS->Stall (gPollInterval);\r
+    MicroSecondDelay (gPollInterval);\r
     CpuData->Timeout -= gPollInterval;\r
   }\r
 \r
@@ -1011,6 +1094,11 @@ EnableDisableAP (
   )\r
 {\r
   CPU_DATA_BLOCK *CpuData;\r
+  BOOLEAN        TempStopCheckState;\r
+  CPU_STATE      CpuState;\r
+\r
+  CpuData = NULL;\r
+  TempStopCheckState = FALSE;\r
 \r
   if (!IsBSP ()) {\r
     return EFI_DEVICE_ERROR;\r
@@ -1020,12 +1108,22 @@ EnableDisableAP (
     return EFI_NOT_FOUND;\r
   }\r
 \r
+  //\r
+  // temporarily stop checkAllAPsStatus for initialize parameters.\r
+  //\r
+  if (!mStopCheckAllAPsStatus) {\r
+    mStopCheckAllAPsStatus = TRUE;\r
+    TempStopCheckState = TRUE;\r
+  }\r
+\r
   CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
   if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  if (GetApState (CpuData) != CpuStateIdle) {\r
+  CpuState = GetApState (CpuData);\r
+  if (CpuState != CpuStateIdle &&\r
+      CpuState != CpuStateSleeping) {\r
     return EFI_UNSUPPORTED;\r
   }\r
 \r
@@ -1046,6 +1144,10 @@ EnableDisableAP (
     CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));\r
   }\r
 \r
+  if (TempStopCheckState) {\r
+    mStopCheckAllAPsStatus = FALSE;\r
+  }\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -1112,6 +1214,7 @@ ResetProcessorToIdleState (
   IN CPU_DATA_BLOCK  *CpuData\r
   )\r
 {\r
+  ResetApStackless ((UINT32)CpuData->Info.ProcessorId);\r
 }\r
 \r
 /**\r
@@ -1131,12 +1234,34 @@ ProcessorToIdleState (
   UINTN                 ProcessorNumber;\r
   CPU_DATA_BLOCK        *CpuData;\r
   EFI_AP_PROCEDURE      Procedure;\r
-  VOID                  *ProcedureArgument;\r
+  volatile VOID         *ProcedureArgument;\r
+\r
+  AsmApDoneWithCommonStack ();\r
+\r
+  while (!mAPsAlreadyInitFinished) {\r
+    CpuPause ();\r
+  }\r
 \r
   WhoAmI (&mMpServicesTemplate, &ProcessorNumber);\r
   CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
 \r
-  AsmApDoneWithCommonStack ();\r
+  //\r
+  // Avoid forcibly reset AP caused the AP got lock not release.\r
+  //\r
+  if (CpuData->LockSelf == (INTN) GetApicId ()) {\r
+    ReleaseSpinLock (&CpuData->CpuDataLock);\r
+  }\r
+\r
+  //\r
+  // Avoid forcibly reset AP caused the timeout AP State is not\r
+  // updated.\r
+  //\r
+  GetMpSpinLock (CpuData);\r
+  if (CpuData->State == CpuStateBusy) {\r
+    CpuData->Procedure = NULL;\r
+  }\r
+  CpuData->State = CpuStateIdle;\r
+  ReleaseMpSpinLock (CpuData);\r
 \r
   while (TRUE) {\r
     GetMpSpinLock (CpuData);\r
@@ -1145,12 +1270,28 @@ ProcessorToIdleState (
     ReleaseMpSpinLock (CpuData);\r
 \r
     if (Procedure != NULL) {\r
-      Procedure (ProcedureArgument);\r
+      SetApState (CpuData, CpuStateBusy);\r
+\r
+      Procedure ((VOID*) ProcedureArgument);\r
 \r
       GetMpSpinLock (CpuData);\r
       CpuData->Procedure = NULL;\r
       CpuData->State = CpuStateFinished;\r
       ReleaseMpSpinLock (CpuData);\r
+    } else {\r
+      //\r
+      // if no procedure to execution, we simply put AP\r
+      // into sleeping state, and waiting BSP sent SIPI.\r
+      //\r
+      GetMpSpinLock (CpuData);\r
+      if (CpuData->State == CpuStateIdle) {\r
+          CpuData->State = CpuStateSleeping;\r
+      }\r
+      ReleaseMpSpinLock (CpuData);\r
+    }\r
+\r
+    if (GetApState (CpuData) == CpuStateSleeping) {\r
+      CpuSleep ();\r
     }\r
 \r
     CpuPause ();\r
@@ -1231,6 +1372,7 @@ CheckAllAPsStatus (
 {\r
   CPU_DATA_BLOCK *CpuData;\r
   UINTN          Number;\r
+  EFI_STATUS     Status;\r
 \r
   if (mMpSystemData.TimeoutActive) {\r
     mMpSystemData.Timeout -= gPollInterval;\r
@@ -1240,6 +1382,16 @@ CheckAllAPsStatus (
     return;\r
   }\r
 \r
+  //\r
+  // avoid next timer enter.\r
+  //\r
+  Status = gBS->SetTimer (\r
+                  mMpSystemData.CheckAllAPsEvent,\r
+                  TimerCancel,\r
+                  0\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   if (mMpSystemData.WaitEvent != NULL) {\r
     CheckAndUpdateAllAPsToIdleState ();\r
     //\r
@@ -1254,13 +1406,15 @@ CheckAllAPsStatus (
     }\r
 \r
     if (mMpSystemData.FinishCount != mMpSystemData.StartCount) {\r
-      return;\r
+      goto EXIT;\r
     }\r
 \r
     mMpSystemData.TimeoutActive = FALSE;\r
     gBS->SignalEvent (mMpSystemData.WaitEvent);\r
     mMpSystemData.WaitEvent = NULL;\r
     mStopCheckAllAPsStatus = TRUE;\r
+\r
+    goto EXIT;\r
   }\r
 \r
   //\r
@@ -1268,24 +1422,18 @@ CheckAllAPsStatus (
   //\r
   for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
     CpuData = &mMpSystemData.CpuDatas[Number];\r
-    if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
-      //\r
-      // Skip BSP\r
-      //\r
-      continue;\r
-    }\r
-\r
-    if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
-      //\r
-      // Skip Disabled processors\r
-      //\r
-      continue;\r
-    }\r
-\r
     if (CpuData->WaitEvent) {\r
       CheckThisAPStatus (NULL, (VOID *)CpuData);\r
     }\r
   }\r
+\r
+EXIT:\r
+  Status = gBS->SetTimer (\r
+                  mMpSystemData.CheckAllAPsEvent,\r
+                  TimerPeriodic,\r
+                  EFI_TIMER_PERIOD_MICROSECONDS (100)\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r
 \r
 /**\r
@@ -1298,13 +1446,27 @@ ApEntryPointInC (
   VOID\r
   )\r
 {\r
-  VOID* TopOfApStack;\r
+  VOID*           TopOfApStack;\r
+  UINTN           ProcessorNumber;\r
 \r
-  FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
-  TopOfApStack  = (UINT8*)mApStackStart + gApStackSize;\r
-  mApStackStart = TopOfApStack;\r
+  if (!mAPsAlreadyInitFinished) {\r
+    FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
+    TopOfApStack  = (UINT8*)mApStackStart + gApStackSize;\r
+    mApStackStart = TopOfApStack;\r
 \r
-  mMpSystemData.NumberOfProcessors++;\r
+    //\r
+    // Store the Stack address, when reset the AP, We can found the original address.\r
+    //\r
+    mMpSystemData.CpuDatas[mMpSystemData.NumberOfProcessors].TopOfStack = TopOfApStack;\r
+    mMpSystemData.NumberOfProcessors++;\r
+    mMpSystemData.NumberOfEnabledProcessors++;\r
+  } else {\r
+    WhoAmI (&mMpServicesTemplate, &ProcessorNumber);\r
+    //\r
+    // Get the original stack address.\r
+    //\r
+    TopOfApStack = mMpSystemData.CpuDatas[ProcessorNumber].TopOfStack;\r
+  }\r
 \r
   SwitchStack (\r
     (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
@@ -1341,11 +1503,12 @@ FillInProcessorInformation (
   CpuData->Info.Location.Package = ProcessorId;\r
   CpuData->Info.Location.Core    = 0;\r
   CpuData->Info.Location.Thread  = 0;\r
-  CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
+  CpuData->State = Bsp ? CpuStateBusy : CpuStateIdle;\r
 \r
   CpuData->Procedure        = NULL;\r
   CpuData->Parameter        = NULL;\r
   InitializeSpinLock (&CpuData->CpuDataLock);\r
+  CpuData->LockSelf         = -1;\r
 \r
   return EFI_SUCCESS;\r
 }\r
@@ -1398,6 +1561,127 @@ InitMpSystemData (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Collects BIST data from HOB.\r
+\r
+  This function collects BIST data from HOB built from Sec Platform Information\r
+  PPI or SEC Platform Information2 PPI.\r
+\r
+**/\r
+VOID\r
+CollectBistDataFromHob (\r
+  VOID\r
+  )\r
+{\r
+  EFI_HOB_GUID_TYPE                     *GuidHob;\r
+  EFI_SEC_PLATFORM_INFORMATION_RECORD2  *SecPlatformInformation2;\r
+  EFI_SEC_PLATFORM_INFORMATION_RECORD   *SecPlatformInformation;\r
+  UINTN                                 NumberOfData;\r
+  EFI_SEC_PLATFORM_INFORMATION_CPU      *CpuInstance;\r
+  EFI_SEC_PLATFORM_INFORMATION_CPU      BspCpuInstance;\r
+  UINTN                                 ProcessorNumber;\r
+  UINT32                                InitialLocalApicId;\r
+  CPU_DATA_BLOCK                        *CpuData;\r
+\r
+  SecPlatformInformation2 = NULL;\r
+  SecPlatformInformation  = NULL;\r
+\r
+  //\r
+  // Get gEfiSecPlatformInformation2PpiGuid Guided HOB firstly\r
+  //\r
+  GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformation2PpiGuid);\r
+  if (GuidHob != NULL) {\r
+    //\r
+    // Sec Platform Information2 PPI includes BSP/APs' BIST information\r
+    //\r
+    SecPlatformInformation2 = GET_GUID_HOB_DATA (GuidHob);\r
+    NumberOfData = SecPlatformInformation2->NumberOfCpus;\r
+    CpuInstance  = SecPlatformInformation2->CpuInstance;\r
+  } else {\r
+    //\r
+    // Otherwise, get gEfiSecPlatformInformationPpiGuid Guided HOB\r
+    //\r
+    GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformationPpiGuid);\r
+    if (GuidHob != NULL) {\r
+      SecPlatformInformation = GET_GUID_HOB_DATA (GuidHob);\r
+      NumberOfData = 1;\r
+      //\r
+      // SEC Platform Information only includes BSP's BIST information\r
+      // does not have BSP's APIC ID\r
+      //\r
+      BspCpuInstance.CpuLocation = GetApicId ();\r
+      BspCpuInstance.InfoRecord.IA32HealthFlags.Uint32  = SecPlatformInformation->IA32HealthFlags.Uint32;\r
+      CpuInstance = &BspCpuInstance;\r
+    } else {\r
+      DEBUG ((EFI_D_INFO, "Does not find any HOB stored CPU BIST information!\n"));\r
+      //\r
+      // Does not find any HOB stored BIST information\r
+      //\r
+      return;\r
+    }\r
+  }\r
+\r
+  while ((NumberOfData--) > 0) {\r
+    for (ProcessorNumber = 0; ProcessorNumber < mMpSystemData.NumberOfProcessors; ProcessorNumber++) {\r
+      CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
+      InitialLocalApicId = (UINT32) CpuData->Info.ProcessorId;\r
+      if (InitialLocalApicId == CpuInstance[NumberOfData].CpuLocation) {\r
+        //\r
+        // Update CPU health status for MP Services Protocol according to BIST data.\r
+        //\r
+        if (CpuInstance[NumberOfData].InfoRecord.IA32HealthFlags.Uint32 != 0) {\r
+          CpuData->Info.StatusFlag &= ~PROCESSOR_HEALTH_STATUS_BIT;\r
+          //\r
+          // Report Status Code that self test is failed\r
+          //\r
+          REPORT_STATUS_CODE (\r
+            EFI_ERROR_CODE | EFI_ERROR_MAJOR,\r
+            (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_SELF_TEST)\r
+            );\r
+        }\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Callback function for ExitBootServices.\r
+\r
+  @param  Event                 Event whose notification function is being invoked.\r
+  @param  Context               The pointer to the notification function's context,\r
+                                which is implementation-dependent.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+ExitBootServicesCallback (\r
+  IN EFI_EVENT                Event,\r
+  IN VOID                     *Context\r
+  )\r
+{\r
+  //\r
+  // Avoid APs access invalid buff datas which allocated by BootServices,\r
+  // so we send INIT IPI to APs to let them wait for SIPI state.\r
+  //\r
+  SendInitIpiAllExcludingSelf ();\r
+}\r
+\r
+/**\r
+  A minimal wrapper function that allows MtrrSetAllMtrrs() to be passed to\r
+  EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() as Procedure.\r
+\r
+  @param[in] Buffer  Pointer to an MTRR_SETTINGS object, to be passed to\r
+                     MtrrSetAllMtrrs().\r
+**/\r
+VOID\r
+EFIAPI\r
+SetMtrrsFromBuffer (\r
+  IN VOID *Buffer\r
+  )\r
+{\r
+  MtrrSetAllMtrrs (Buffer);\r
+}\r
+\r
 /**\r
   Initialize Multi-processor support.\r
 \r
@@ -1407,40 +1691,116 @@ InitializeMpSupport (
   VOID\r
   )\r
 {\r
+  EFI_STATUS     Status;\r
+  MTRR_SETTINGS  MtrrSettings;\r
+  UINTN          Timeout;\r
+\r
   gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
   if (gMaxLogicalProcessorNumber < 1) {\r
     DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
     return;\r
   }\r
 \r
-  if (gMaxLogicalProcessorNumber == 1) {\r
-    return;\r
-  }\r
 \r
-  gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
-  ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
 \r
-  mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
-  ASSERT (mApStackStart != NULL);\r
+  InitMpSystemData ();\r
 \r
   //\r
-  // the first buffer of stack size used for common stack, when the amount of AP\r
-  // more than 1, we should never free the common stack which maybe used for AP reset.\r
+  // Only perform AP detection if PcdCpuMaxLogicalProcessorNumber is greater than 1\r
   //\r
-  mCommonStack = mApStackStart;\r
-  mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
-  mApStackStart = mTopOfApCommonStack;\r
+  if (gMaxLogicalProcessorNumber > 1) {\r
 \r
-  InitMpSystemData ();\r
+    gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
+    ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
+\r
+    mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
+    ASSERT (mApStackStart != NULL);\r
+\r
+    //\r
+    // the first buffer of stack size used for common stack, when the amount of AP\r
+    // more than 1, we should never free the common stack which maybe used for AP reset.\r
+    //\r
+    mCommonStack = mApStackStart;\r
+    mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
+    mApStackStart = mTopOfApCommonStack;\r
+\r
+    PrepareAPStartupCode ();\r
 \r
+    StartApsStackless ();\r
+  }\r
+\r
+  DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mMpSystemData.NumberOfProcessors));\r
   if (mMpSystemData.NumberOfProcessors == 1) {\r
-    FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
-    return;\r
+    FreeApStartupCode ();\r
+    if (mCommonStack != NULL) {\r
+      FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
+    }\r
   }\r
 \r
-  if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
-    FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
-                                (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
-                                gApStackSize));\r
+  mMpSystemData.CpuDatas = ReallocatePool (\r
+                             sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber,\r
+                             sizeof (CPU_DATA_BLOCK) * mMpSystemData.NumberOfProcessors,\r
+                             mMpSystemData.CpuDatas);\r
+\r
+  //\r
+  // Release all APs to complete initialization and enter idle loop\r
+  //\r
+  mAPsAlreadyInitFinished = TRUE;\r
+\r
+  //\r
+  // Wait for all APs to enter idle loop.\r
+  //\r
+  Timeout = 0;\r
+  do {\r
+    if (CheckAllAPsSleeping ()) {\r
+      break;\r
+    }\r
+    MicroSecondDelay (gPollInterval);\r
+    Timeout += gPollInterval;\r
+  } while (Timeout <= PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds));\r
+  ASSERT (Timeout <= PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds));\r
+\r
+  //\r
+  // Update CPU healthy information from Guided HOB\r
+  //\r
+  CollectBistDataFromHob ();\r
+\r
+  //\r
+  // Synchronize MTRR settings to APs.\r
+  //\r
+  MtrrGetAllMtrrs (&MtrrSettings);\r
+  Status = mMpServicesTemplate.StartupAllAPs (\r
+                                 &mMpServicesTemplate, // This\r
+                                 SetMtrrsFromBuffer,   // Procedure\r
+                                 TRUE,                 // SingleThread\r
+                                 NULL,                 // WaitEvent\r
+                                 0,                    // TimeoutInMicrosecsond\r
+                                 &MtrrSettings,        // ProcedureArgument\r
+                                 NULL                  // FailedCpuList\r
+                                 );\r
+  ASSERT (Status == EFI_SUCCESS || Status == EFI_NOT_STARTED);\r
+\r
+  Status = gBS->InstallMultipleProtocolInterfaces (\r
+                  &mMpServiceHandle,\r
+                  &gEfiMpServiceProtocolGuid,  &mMpServicesTemplate,\r
+                  NULL\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  if (mMpSystemData.NumberOfProcessors > 1 && mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
+    if (mApStackStart != NULL) {\r
+      FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
+                                  (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
+                                  gApStackSize));\r
+    }\r
   }\r
+\r
+  Status = gBS->CreateEvent (\r
+                  EVT_SIGNAL_EXIT_BOOT_SERVICES,\r
+                  TPL_CALLBACK,\r
+                  ExitBootServicesCallback,\r
+                  NULL,\r
+                  &mExitBootServicesEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
 }\r