]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg: Update CPU MP drivers to support single CPU configuration
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
index 1d3d16bcc06bfa776e1df2f45699dad51aaa205d..da3686e2788e8c50f0652bd743a3a010c42e5a2e 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
 \r
 UINTN gMaxLogicalProcessorNumber;\r
 UINTN gApStackSize;\r
+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
   GetNumberOfProcessors,\r
   GetProcessorInfo,\r
-  NULL, // StartupAllAPs,\r
-  NULL, // StartupThisAP,\r
-  NULL, // SwitchBSP,\r
+  StartupAllAPs,\r
+  StartupThisAP,\r
+  SwitchBSP,\r
   EnableDisableAP,\r
   WhoAmI\r
 };\r
 \r
+/**\r
+   Get Mp Service Lock.\r
+\r
+  @param   CpuData    the pointer to CPU_DATA_BLOCK of specified processor\r
+\r
+**/\r
+VOID\r
+GetMpSpinLock (\r
+  IN  CPU_DATA_BLOCK  *CpuData\r
+  )\r
+{\r
+  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+    CpuPause ();\r
+  }\r
+  CpuData->LockSelf = GetApicId ();\r
+}\r
+\r
+/**\r
+   Release Mp Service Lock.\r
+\r
+  @param   CpuData    the pointer to CPU_DATA_BLOCK of specified processor\r
+\r
+**/\r
+VOID\r
+ReleaseMpSpinLock (\r
+  IN  CPU_DATA_BLOCK  *CpuData\r
+  )\r
+{\r
+  ReleaseSpinLock (&CpuData->CpuDataLock);\r
+}\r
+\r
 /**\r
   Check whether caller processor is BSP.\r
 \r
@@ -72,16 +109,53 @@ GetApState (
 {\r
   CPU_STATE State;\r
 \r
-  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
-    CpuPause ();\r
-  }\r
-\r
+  GetMpSpinLock (CpuData);\r
   State = CpuData->State;\r
-  ReleaseSpinLock (&CpuData->CpuDataLock);\r
+  ReleaseMpSpinLock (CpuData);\r
 \r
   return State;\r
 }\r
 \r
+/**\r
+  Set the Application Processors state.\r
+\r
+  @param   CpuData    The pointer to CPU_DATA_BLOCK of specified AP\r
+  @param   State      The AP status\r
+\r
+**/\r
+VOID\r
+SetApState (\r
+  IN  CPU_DATA_BLOCK   *CpuData,\r
+  IN  CPU_STATE        State\r
+  )\r
+{\r
+  GetMpSpinLock (CpuData);\r
+  CpuData->State = State;\r
+  ReleaseMpSpinLock (CpuData);\r
+}\r
+\r
+/**\r
+  Set the Application Processor prepare to run a function specified\r
+  by Params.\r
+\r
+  @param CpuData           the pointer to CPU_DATA_BLOCK of specified AP\r
+  @param Procedure         A pointer to the function to be run on enabled APs of the system\r
+  @param ProcedureArgument Pointer to the optional parameter of the assigned function\r
+\r
+**/\r
+VOID\r
+SetApProcedure (\r
+  IN   CPU_DATA_BLOCK        *CpuData,\r
+  IN   EFI_AP_PROCEDURE      Procedure,\r
+  IN   VOID                  *ProcedureArgument\r
+  )\r
+{\r
+  GetMpSpinLock (CpuData);\r
+  CpuData->Parameter  = ProcedureArgument;\r
+  CpuData->Procedure  = Procedure;\r
+  ReleaseMpSpinLock (CpuData);\r
+}\r
+\r
 /**\r
   Check the Application Processors Status whether contains the Flags.\r
 \r
@@ -100,14 +174,11 @@ TestCpuStatusFlag (
 {\r
   UINT32 Ret;\r
 \r
-  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
-    CpuPause ();\r
-  }\r
-\r
+  GetMpSpinLock (CpuData);\r
   Ret = CpuData->Info.StatusFlag & Flags;\r
-  ReleaseSpinLock (&CpuData->CpuDataLock);\r
+  ReleaseMpSpinLock (CpuData);\r
 \r
-  return !!(Ret);\r
+  return (BOOLEAN) (Ret != 0);\r
 }\r
 \r
 /**\r
@@ -123,12 +194,9 @@ CpuStatusFlagOr (
   IN  UINT32          Flags\r
   )\r
 {\r
-  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
-    CpuPause ();\r
-  }\r
-\r
+  GetMpSpinLock (CpuData);\r
   CpuData->Info.StatusFlag |= Flags;\r
-  ReleaseSpinLock (&CpuData->CpuDataLock);\r
+  ReleaseMpSpinLock (CpuData);\r
 }\r
 \r
 /**\r
@@ -144,12 +212,155 @@ CpuStatusFlagAndNot (
   IN  UINT32          Flags\r
   )\r
 {\r
-  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
-    CpuPause ();\r
+  GetMpSpinLock (CpuData);\r
+  CpuData->Info.StatusFlag &= ~Flags;\r
+  ReleaseMpSpinLock (CpuData);\r
+}\r
+\r
+/**\r
+  Searches for the next blocking AP.\r
+\r
+  Search for the next AP that is put in blocking state by single-threaded StartupAllAPs().\r
+\r
+  @param  NextNumber           Pointer to the processor number of the next blocking AP.\r
+\r
+  @retval EFI_SUCCESS          The next blocking AP has been found.\r
+  @retval EFI_NOT_FOUND        No blocking AP exists.\r
+\r
+**/\r
+EFI_STATUS\r
+GetNextBlockedNumber (\r
+  OUT UINTN  *NextNumber\r
+  )\r
+{\r
+  UINTN                 Number;\r
+  CPU_STATE             CpuState;\r
+  CPU_DATA_BLOCK        *CpuData;\r
+\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
+    CpuState = GetApState (CpuData);\r
+    if (CpuState == CpuStateBlocked) {\r
+      *NextNumber = Number;\r
+      return EFI_SUCCESS;\r
+    }\r
   }\r
 \r
-  CpuData->Info.StatusFlag &= ~Flags;\r
-  ReleaseSpinLock (&CpuData->CpuDataLock);\r
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+  Check if the APs state are finished, and update them to idle state\r
+  by StartupAllAPs().\r
+\r
+**/\r
+VOID\r
+CheckAndUpdateAllAPsToIdleState (\r
+  VOID\r
+  )\r
+{\r
+  UINTN                 ProcessorNumber;\r
+  UINTN                 NextNumber;\r
+  CPU_DATA_BLOCK        *CpuData;\r
+  EFI_STATUS            Status;\r
+  CPU_STATE             CpuState;\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
+    CpuState = GetApState (CpuData);\r
+    if (CpuState == CpuStateFinished) {\r
+      mMpSystemData.FinishCount++;\r
+      if (mMpSystemData.SingleThread) {\r
+        Status = GetNextBlockedNumber (&NextNumber);\r
+        if (!EFI_ERROR (Status)) {\r
+          SetApState (&mMpSystemData.CpuDatas[NextNumber], CpuStateReady);\r
+          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
+      SetApState (CpuData, CpuStateIdle);\r
+    }\r
+  }\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
+  by StartupAllAPs().\r
+\r
+**/\r
+VOID\r
+ResetAllFailedAPs (\r
+  VOID\r
+  )\r
+{\r
+  CPU_DATA_BLOCK        *CpuData;\r
+  UINTN                 Number;\r
+  CPU_STATE             CpuState;\r
+\r
+  if (mMpSystemData.FailedList != NULL) {\r
+     *mMpSystemData.FailedList = AllocatePool ((mMpSystemData.StartCount - mMpSystemData.FinishCount + 1) * sizeof(UINTN));\r
+     ASSERT (*mMpSystemData.FailedList != NULL);\r
+  }\r
+\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
+    CpuState = GetApState (CpuData);\r
+    if (CpuState != CpuStateIdle &&\r
+        CpuState != CpuStateSleeping) {\r
+      if (mMpSystemData.FailedList != NULL) {\r
+        (*mMpSystemData.FailedList)[mMpSystemData.FailedListIndex++] = Number;\r
+      }\r
+      ResetProcessorToIdleState (CpuData);\r
+    }\r
+  }\r
+\r
+  if (mMpSystemData.FailedList != NULL) {\r
+    (*mMpSystemData.FailedList)[mMpSystemData.FailedListIndex] = END_OF_CPU_LIST;\r
+  }\r
 }\r
 \r
 /**\r
@@ -259,6 +470,538 @@ GetProcessorInfo (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  This service executes a caller provided function on all enabled APs. APs can\r
+  run either simultaneously or one at a time in sequence. This service supports\r
+  both blocking and non-blocking requests. The non-blocking requests use EFI\r
+  events so the BSP can detect when the APs have finished. This service may only\r
+  be called from the BSP.\r
+\r
+  This function is used to dispatch all the enabled APs to the function specified\r
+  by Procedure.  If any enabled AP is busy, then EFI_NOT_READY is returned\r
+  immediately and Procedure is not started on any AP.\r
+\r
+  If SingleThread is TRUE, all the enabled APs execute the function specified by\r
+  Procedure one by one, in ascending order of processor handle number. Otherwise,\r
+  all the enabled APs execute the function specified by Procedure simultaneously.\r
+\r
+  If WaitEvent is NULL, execution is in blocking mode. The BSP waits until all\r
+  APs finish or TimeoutInMicroseconds expires. Otherwise, execution is in non-blocking\r
+  mode, and the BSP returns from this service without waiting for APs. If a\r
+  non-blocking mode is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
+  is signaled, then EFI_UNSUPPORTED must be returned.\r
+\r
+  If the timeout specified by TimeoutInMicroseconds expires before all APs return\r
+  from Procedure, then Procedure on the failed APs is terminated. All enabled APs\r
+  are always available for further calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
+  and EFI_MP_SERVICES_PROTOCOL.StartupThisAP(). If FailedCpuList is not NULL, its\r
+  content points to the list of processor handle numbers in which Procedure was\r
+  terminated.\r
+\r
+  Note: It is the responsibility of the consumer of the EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
+  to make sure that the nature of the code that is executed on the BSP and the\r
+  dispatched APs is well controlled. The MP Services Protocol does not guarantee\r
+  that the Procedure function is MP-safe. Hence, the tasks that can be run in\r
+  parallel are limited to certain independent tasks and well-controlled exclusive\r
+  code. EFI services and protocols may not be called by APs unless otherwise\r
+  specified.\r
+\r
+  In blocking execution mode, BSP waits until all APs finish or\r
+  TimeoutInMicroseconds expires.\r
+\r
+  In non-blocking execution mode, BSP is freed to return to the caller and then\r
+  proceed to the next task without having to wait for APs. The following\r
+  sequence needs to occur in a non-blocking execution mode:\r
+\r
+    -# The caller that intends to use this MP Services Protocol in non-blocking\r
+       mode creates WaitEvent by calling the EFI CreateEvent() service.  The caller\r
+       invokes EFI_MP_SERVICES_PROTOCOL.StartupAllAPs(). If the parameter WaitEvent\r
+       is not NULL, then StartupAllAPs() executes in non-blocking mode. It requests\r
+       the function specified by Procedure to be started on all the enabled APs,\r
+       and releases the BSP to continue with other tasks.\r
+    -# The caller can use the CheckEvent() and WaitForEvent() services to check\r
+       the state of the WaitEvent created in step 1.\r
+    -# When the APs complete their task or TimeoutInMicroSecondss expires, the MP\r
+       Service signals WaitEvent by calling the EFI SignalEvent() function. If\r
+       FailedCpuList is not NULL, its content is available when WaitEvent is\r
+       signaled. If all APs returned from Procedure prior to the timeout, then\r
+       FailedCpuList is set to NULL. If not all APs return from Procedure before\r
+       the timeout, then FailedCpuList is filled in with the list of the failed\r
+       APs. The buffer is allocated by MP Service Protocol using AllocatePool().\r
+       It is the caller's responsibility to free the buffer with FreePool() service.\r
+    -# This invocation of SignalEvent() function informs the caller that invoked\r
+       EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() that either all the APs completed\r
+       the specified task or a timeout occurred. The contents of FailedCpuList\r
+       can be examined to determine which APs did not complete the specified task\r
+       prior to the timeout.\r
+\r
+  @param[in]  This                    A pointer to the EFI_MP_SERVICES_PROTOCOL\r
+                                      instance.\r
+  @param[in]  Procedure               A pointer to the function to be run on\r
+                                      enabled APs of the system. See type\r
+                                      EFI_AP_PROCEDURE.\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.  If it is NULL, then execute in\r
+                                      blocking mode. BSP waits until all APs finish\r
+                                      or TimeoutInMicroseconds expires.  If it's\r
+                                      not NULL, then execute in non-blocking mode.\r
+                                      BSP requests the function specified by\r
+                                      Procedure to be started on all the enabled\r
+                                      APs, and go on executing immediately. If\r
+                                      all return from Procedure, or TimeoutInMicroseconds\r
+                                      expires, this event is signaled. The BSP\r
+                                      can use the CheckEvent() or WaitForEvent()\r
+                                      services to check the state of event.  Type\r
+                                      EFI_EVENT is defined in CreateEvent() in\r
+                                      the Unified Extensible Firmware Interface\r
+                                      Specification.\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. Zero means\r
+                                      infinity.  If the timeout expires before\r
+                                      all APs return from Procedure, then Procedure\r
+                                      on the failed APs is terminated. All enabled\r
+                                      APs are available for next function assigned\r
+                                      by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
+                                      or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
+                                      If the timeout expires in blocking mode,\r
+                                      BSP returns EFI_TIMEOUT.  If the timeout\r
+                                      expires in non-blocking mode, WaitEvent\r
+                                      is signaled with SignalEvent().\r
+  @param[in]  ProcedureArgument       The parameter passed into Procedure for\r
+                                      all APs.\r
+  @param[out] FailedCpuList           If NULL, this parameter is ignored. Otherwise,\r
+                                      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
+                                      The buffer is allocated by MP Service Protocol,\r
+                                      and it's the caller's responsibility to\r
+                                      free the buffer with FreePool() service.\r
+                                      In blocking mode, it is ready for consumption\r
+                                      when the call returns. In non-blocking mode,\r
+                                      it is ready when WaitEvent is signaled.  The\r
+                                      list of failed CPU is terminated by\r
+                                      END_OF_CPU_LIST.\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 EFI_UNSUPPORTED         A non-blocking mode request was made after the\r
+                                  UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
+                                  signaled.\r
+  @retval EFI_DEVICE_ERROR        Caller processor is AP.\r
+  @retval EFI_NOT_STARTED         No enabled APs exist in the system.\r
+  @retval EFI_NOT_READY           Any enabled APs are busy.\r
+  @retval EFI_TIMEOUT             In blocking mode, the timeout expired before\r
+                                  all enabled APs have finished.\r
+  @retval EFI_INVALID_PARAMETER   Procedure is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+StartupAllAPs (\r
+  IN  EFI_MP_SERVICES_PROTOCOL  *This,\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_DATA_BLOCK        *CpuData;\r
+  UINTN                 Number;\r
+  CPU_STATE             APInitialState;\r
+  CPU_STATE             CpuState;\r
+\r
+  CpuData = NULL;\r
+\r
+  if (FailedCpuList != NULL) {\r
+    *FailedCpuList = NULL;\r
+  }\r
+\r
+  if (!IsBSP ()) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  if (mMpSystemData.NumberOfProcessors == 1) {\r
+    return EFI_NOT_STARTED;\r
+  }\r
+\r
+  if (Procedure == NULL) {\r
+    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
+      //\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
+    CpuState = GetApState (CpuData);\r
+    if (CpuState != CpuStateIdle &&\r
+        CpuState != CpuStateSleeping) {\r
+      return EFI_NOT_READY;\r
+    }\r
+  }\r
+\r
+  mMpSystemData.Procedure         = Procedure;\r
+  mMpSystemData.ProcedureArgument = ProcedureArgument;\r
+  mMpSystemData.WaitEvent         = WaitEvent;\r
+  mMpSystemData.Timeout           = TimeoutInMicroseconds;\r
+  mMpSystemData.TimeoutActive     = (BOOLEAN) (TimeoutInMicroseconds != 0);\r
+  mMpSystemData.FinishCount       = 0;\r
+  mMpSystemData.StartCount        = 0;\r
+  mMpSystemData.SingleThread      = SingleThread;\r
+  mMpSystemData.FailedList        = FailedCpuList;\r
+  mMpSystemData.FailedListIndex   = 0;\r
+  APInitialState                  = CpuStateReady;\r
+\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
+    //\r
+    // Get APs prepared, and put failing APs into FailedCpuList\r
+    // if "SingleThread", only 1 AP will put to ready state, other AP will be put to ready\r
+    // 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
+    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
+        APInitialState = CpuStateBlocked;\r
+      }\r
+    }\r
+  }\r
+\r
+  mStopCheckAllAPsStatus = FALSE;\r
+\r
+  if (WaitEvent != NULL) {\r
+    //\r
+    // non blocking\r
+    //\r
+    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
+      Status = EFI_SUCCESS;\r
+      goto Done;\r
+    }\r
+\r
+    //\r
+    // task timeout\r
+    //\r
+    if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {\r
+      ResetAllFailedAPs();\r
+      Status = EFI_TIMEOUT;\r
+      goto Done;\r
+    }\r
+\r
+    gBS->Stall (gPollInterval);\r
+    mMpSystemData.Timeout -= gPollInterval;\r
+  }\r
+\r
+Done:\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  This service lets the caller get one enabled AP to execute a caller-provided\r
+  function. The caller can request the BSP to either wait for the completion\r
+  of the AP or just proceed with the next task by using the EFI event mechanism.\r
+  See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking\r
+  execution support.  This service may only be called from the BSP.\r
+\r
+  This function is used to dispatch one enabled AP to the function specified by\r
+  Procedure passing in the argument specified by ProcedureArgument.  If WaitEvent\r
+  is NULL, execution is in blocking mode. The BSP waits until the AP finishes or\r
+  TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.\r
+  BSP proceeds to the next task without waiting for the AP. If a non-blocking mode\r
+  is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,\r
+  then EFI_UNSUPPORTED must be returned.\r
+\r
+  If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
+  from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
+  available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and\r
+  EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
+\r
+  @param[in]  This                    A pointer to the EFI_MP_SERVICES_PROTOCOL\r
+                                      instance.\r
+  @param[in]  Procedure               A pointer to the function to be run on\r
+                                      enabled APs of the system. See type\r
+                                      EFI_AP_PROCEDURE.\r
+  @param[in]  ProcessorNumber         The handle number of the AP. The range is\r
+                                      from 0 to the total number of logical\r
+                                      processors minus 1. The total number of\r
+                                      logical processors can be retrieved by\r
+                                      EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
+  @param[in]  WaitEvent               The event created by the caller with CreateEvent()\r
+                                      service.  If it is NULL, then execute in\r
+                                      blocking mode. BSP waits until all APs finish\r
+                                      or TimeoutInMicroseconds expires.  If it's\r
+                                      not NULL, then execute in non-blocking mode.\r
+                                      BSP requests the function specified by\r
+                                      Procedure to be started on all the enabled\r
+                                      APs, and go on executing immediately. If\r
+                                      all return from Procedure or TimeoutInMicroseconds\r
+                                      expires, this event is signaled. The BSP\r
+                                      can use the CheckEvent() or WaitForEvent()\r
+                                      services to check the state of event.  Type\r
+                                      EFI_EVENT is defined in CreateEvent() in\r
+                                      the Unified Extensible Firmware Interface\r
+                                      Specification.\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. Zero means\r
+                                      infinity.  If the timeout expires before\r
+                                      all APs return from Procedure, then Procedure\r
+                                      on the failed APs is terminated. All enabled\r
+                                      APs are available for next function assigned\r
+                                      by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
+                                      or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
+                                      If the timeout expires in blocking mode,\r
+                                      BSP returns EFI_TIMEOUT.  If the timeout\r
+                                      expires in non-blocking mode, WaitEvent\r
+                                      is signaled with SignalEvent().\r
+  @param[in]  ProcedureArgument       The parameter passed into Procedure for\r
+                                      all APs.\r
+  @param[out] Finished                If NULL, this parameter is ignored.  In\r
+                                      blocking mode, this parameter is ignored.\r
+                                      In non-blocking mode, if AP returns from\r
+                                      Procedure before the timeout expires, its\r
+                                      content is set to TRUE. Otherwise, the\r
+                                      value is set to FALSE. The caller can\r
+                                      determine if the AP returned from Procedure\r
+                                      by evaluating this value.\r
+\r
+  @retval EFI_SUCCESS             In blocking mode, specified AP finished before\r
+                                  the timeout expires.\r
+  @retval EFI_SUCCESS             In non-blocking mode, the function has been\r
+                                  dispatched to specified AP.\r
+  @retval EFI_UNSUPPORTED         A non-blocking mode request was made after the\r
+                                  UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
+                                  signaled.\r
+  @retval EFI_DEVICE_ERROR        The calling processor is an AP.\r
+  @retval EFI_TIMEOUT             In blocking mode, the timeout expired before\r
+                                  the specified AP has finished.\r
+  @retval EFI_NOT_READY           The specified AP is busy.\r
+  @retval EFI_NOT_FOUND           The processor with the handle specified by\r
+                                  ProcessorNumber does not exist.\r
+  @retval EFI_INVALID_PARAMETER   ProcessorNumber specifies the BSP or disabled AP.\r
+  @retval EFI_INVALID_PARAMETER   Procedure is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+StartupThisAP (\r
+  IN  EFI_MP_SERVICES_PROTOCOL  *This,\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
+  CPU_DATA_BLOCK        *CpuData;\r
+  CPU_STATE             CpuState;\r
+\r
+  CpuData = NULL;\r
+\r
+  if (Finished != NULL) {\r
+    *Finished = FALSE;\r
+  }\r
+\r
+  if (!IsBSP ()) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  if (Procedure == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
+    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
+  CpuState = GetApState (CpuData);\r
+  if (CpuState != CpuStateIdle &&\r
+      CpuState != CpuStateSleeping) {\r
+    return EFI_NOT_READY;\r
+  }\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 = (BOOLEAN) (TimeoutInMicroseconds != 0);\r
+  CpuData->Finished = Finished;\r
+\r
+  mStopCheckAllAPsStatus = FALSE;\r
+\r
+  if (WaitEvent != NULL) {\r
+    //\r
+    // Non Blocking\r
+    //\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
+  //\r
+  // Blocking\r
+  //\r
+  while (TRUE) {\r
+    if (GetApState (CpuData) == CpuStateFinished) {\r
+      SetApState (CpuData, CpuStateIdle);\r
+      break;\r
+    }\r
+\r
+    if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
+      ResetProcessorToIdleState (CpuData);\r
+      return EFI_TIMEOUT;\r
+    }\r
+\r
+    gBS->Stall (gPollInterval);\r
+    CpuData->Timeout -= gPollInterval;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  This service switches the requested AP to be the BSP from that point onward.\r
+  This service changes the BSP for all purposes.   This call can only be performed\r
+  by the current BSP.\r
+\r
+  This service switches the requested AP to be the BSP from that point onward.\r
+  This service changes the BSP for all purposes. The new BSP can take over the\r
+  execution of the old BSP and continue seamlessly from where the old one left\r
+  off. This service may not be supported after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT\r
+  is signaled.\r
+\r
+  If the BSP cannot be switched prior to the return from this service, then\r
+  EFI_UNSUPPORTED must be returned.\r
+\r
+  @param[in] This              A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
+  @param[in] ProcessorNumber   The handle number of AP that is to become the new\r
+                               BSP. 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
+                               EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\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 EFI_UNSUPPORTED         Switching the BSP cannot be completed prior to\r
+                                  this service returning.\r
+  @retval EFI_UNSUPPORTED         Switching the BSP is not supported.\r
+  @retval EFI_SUCCESS             The calling processor is an AP.\r
+  @retval EFI_NOT_FOUND           The processor with the handle specified by\r
+                                  ProcessorNumber does not exist.\r
+  @retval EFI_INVALID_PARAMETER   ProcessorNumber specifies the current BSP or\r
+                                  a disabled AP.\r
+  @retval EFI_NOT_READY           The specified AP is busy.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+SwitchBSP (\r
+  IN EFI_MP_SERVICES_PROTOCOL  *This,\r
+  IN  UINTN                    ProcessorNumber,\r
+  IN  BOOLEAN                  EnableOldBSP\r
+  )\r
+{\r
+   //\r
+   // Current always return unsupported.\r
+   //\r
+   return EFI_UNSUPPORTED;\r
+}\r
+\r
 /**\r
   This service lets the caller enable or disable an AP from this point onward.\r
   This service may only be called from the BSP.\r
@@ -310,6 +1053,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
@@ -319,12 +1067,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
@@ -345,6 +1103,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
@@ -397,6 +1159,23 @@ WhoAmI (
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Terminate AP's task and set it to idle state.\r
+\r
+  This function terminates AP's task due to timeout by sending INIT-SIPI,\r
+  and sends it to idle state.\r
+\r
+  @param CpuData           the pointer to CPU_DATA_BLOCK of specified AP\r
+\r
+**/\r
+VOID\r
+ResetProcessorToIdleState (\r
+  IN CPU_DATA_BLOCK  *CpuData\r
+  )\r
+{\r
+  ResetApStackless ((UINT32)CpuData->Info.ProcessorId);\r
+}\r
+\r
 /**\r
   Application Processors do loop routine\r
   after switch to its own stack.\r
@@ -411,14 +1190,211 @@ ProcessorToIdleState (
   IN      VOID                      *Context2   OPTIONAL\r
   )\r
 {\r
-  DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));\r
+  UINTN                 ProcessorNumber;\r
+  CPU_DATA_BLOCK        *CpuData;\r
+  EFI_AP_PROCEDURE      Procedure;\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
+  //\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
+    ProcedureArgument = CpuData->Parameter;\r
+    Procedure = CpuData->Procedure;\r
+    ReleaseMpSpinLock (CpuData);\r
+\r
+    if (Procedure != NULL) {\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
+  }\r
+\r
   CpuSleep ();\r
   CpuDeadLoop ();\r
 }\r
 \r
+/**\r
+  Checks AP' status periodically.\r
+\r
+  This function is triggerred by timer perodically to check the\r
+  state of AP forStartupThisAP() executed in non-blocking mode.\r
+\r
+  @param  Event    Event triggered.\r
+  @param  Context  Parameter passed with the event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+CheckThisAPStatus (\r
+  IN  EFI_EVENT        Event,\r
+  IN  VOID             *Context\r
+  )\r
+{\r
+  CPU_DATA_BLOCK  *CpuData;\r
+  CPU_STATE       CpuState;\r
+\r
+  CpuData = (CPU_DATA_BLOCK *) Context;\r
+  if (CpuData->TimeoutActive) {\r
+    CpuData->Timeout -= gPollInterval;\r
+  }\r
+\r
+  CpuState = GetApState (CpuData);\r
+\r
+  if (CpuState == CpuStateFinished) {\r
+    if (CpuData->Finished) {\r
+      *CpuData->Finished = TRUE;\r
+    }\r
+    SetApState (CpuData, CpuStateIdle);\r
+    goto out;\r
+  }\r
+\r
+  if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
+    if (CpuState != CpuStateIdle &&\r
+        CpuData->Finished) {\r
+      *CpuData->Finished = FALSE;\r
+    }\r
+    ResetProcessorToIdleState (CpuData);\r
+    goto out;\r
+  }\r
+\r
+  return;\r
+\r
+out:\r
+  CpuData->TimeoutActive = FALSE;\r
+  gBS->SignalEvent (CpuData->WaitEvent);\r
+  CpuData->WaitEvent = NULL;\r
+}\r
+\r
+/**\r
+  Checks APs' status periodically.\r
+\r
+  This function is triggerred by timer perodically to check the\r
+  state of APs for StartupAllAPs() executed in non-blocking mode.\r
+\r
+  @param  Event    Event triggered.\r
+  @param  Context  Parameter passed with the event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+CheckAllAPsStatus (\r
+  IN  EFI_EVENT        Event,\r
+  IN  VOID             *Context\r
+  )\r
+{\r
+  CPU_DATA_BLOCK *CpuData;\r
+  UINTN          Number;\r
+  EFI_STATUS     Status;\r
+\r
+  if (mMpSystemData.TimeoutActive) {\r
+    mMpSystemData.Timeout -= gPollInterval;\r
+  }\r
+\r
+  if (mStopCheckAllAPsStatus) {\r
+    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
+    // task timeout\r
+    //\r
+    if (mMpSystemData.TimeoutActive && mMpSystemData.Timeout < 0) {\r
+      ResetAllFailedAPs();\r
+      //\r
+      // force exit\r
+      //\r
+      mMpSystemData.FinishCount = mMpSystemData.StartCount;\r
+    }\r
+\r
+    if (mMpSystemData.FinishCount != mMpSystemData.StartCount) {\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
+  // check each AP status for StartupThisAP\r
+  //\r
+  for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
+    CpuData = &mMpSystemData.CpuDatas[Number];\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
   Application Processor C code entry point.\r
 \r
@@ -429,13 +1405,27 @@ ApEntryPointInC (
   VOID\r
   )\r
 {\r
-  VOID* TopOfApStack;\r
-\r
-  FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
-  TopOfApStack  = (UINT8*)mApStackStart + gApStackSize;\r
-  mApStackStart = TopOfApStack;\r
-\r
-  mMpSystemData.NumberOfProcessors++;\r
+  VOID*           TopOfApStack;\r
+  UINTN           ProcessorNumber;\r
+\r
+  if (!mAPsAlreadyInitFinished) {\r
+    FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
+    TopOfApStack  = (UINT8*)mApStackStart + gApStackSize;\r
+    mApStackStart = TopOfApStack;\r
+\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
@@ -472,11 +1462,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
@@ -492,6 +1483,8 @@ InitMpSystemData (
   VOID\r
   )\r
 {\r
+  EFI_STATUS     Status;\r
+\r
   ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
 \r
   mMpSystemData.NumberOfProcessors = 1;\r
@@ -500,6 +1493,25 @@ InitMpSystemData (
   mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
   ASSERT(mMpSystemData.CpuDatas != NULL);\r
 \r
+  Status = gBS->CreateEvent (\r
+                  EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+                  TPL_CALLBACK,\r
+                  CheckAllAPsStatus,\r
+                  NULL,\r
+                  &mMpSystemData.CheckAllAPsEvent\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  //\r
+  // Set timer to check all APs status.\r
+  //\r
+  Status = gBS->SetTimer (\r
+                  mMpSystemData.CheckAllAPsEvent,\r
+                  TimerPeriodic,\r
+                  EFI_TIMER_PERIOD_MICROSECONDS (100)\r
+                  );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
   //\r
   // BSP\r
   //\r
@@ -508,6 +1520,111 @@ 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
   Initialize Multi-processor support.\r
 \r
@@ -517,40 +1634,83 @@ InitializeMpSupport (
   VOID\r
   )\r
 {\r
+  EFI_STATUS Status;\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
+  mAPsAlreadyInitFinished = TRUE;\r
+\r
+  //\r
+  // Update CPU healthy information from Guided HOB\r
+  //\r
+  CollectBistDataFromHob ();\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