]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg/CpuDxe: implement Mp Protocol:StartupThisAP()
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
index ea403e8e124310853343f212825fe911fc223e75..552bac3c6dc2c6268a419e4e7a4fcece5e2cba6f 100644 (file)
 #include "CpuDxe.h"\r
 #include "CpuMp.h"\r
 \r
+UINTN gMaxLogicalProcessorNumber;\r
+UINTN gApStackSize;\r
+UINTN gPollInterval = 100; // 100 microseconds\r
+\r
+MP_SYSTEM_DATA mMpSystemData;\r
+\r
 VOID *mCommonStack = 0;\r
 VOID *mTopOfApCommonStack = 0;\r
+VOID *mApStackStart = 0;\r
+\r
+EFI_MP_SERVICES_PROTOCOL  mMpServicesTemplate = {\r
+  GetNumberOfProcessors,\r
+  GetProcessorInfo,\r
+  NULL, // StartupAllAPs,\r
+  StartupThisAP,\r
+  NULL, // SwitchBSP,\r
+  EnableDisableAP,\r
+  WhoAmI\r
+};\r
+\r
+/**\r
+  Check whether caller processor is BSP.\r
+\r
+  @retval  TRUE       the caller is BSP\r
+  @retval  FALSE      the caller is AP\r
+\r
+**/\r
+BOOLEAN\r
+IsBSP (\r
+  VOID\r
+  )\r
+{\r
+  UINTN           CpuIndex;\r
+  CPU_DATA_BLOCK  *CpuData;\r
+\r
+  CpuData = NULL;\r
+\r
+  WhoAmI (&mMpServicesTemplate, &CpuIndex);\r
+  CpuData = &mMpSystemData.CpuDatas[CpuIndex];\r
+\r
+  return CpuData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT ? TRUE : FALSE;\r
+}\r
+\r
+/**\r
+  Get the Application Processors state.\r
+\r
+  @param   CpuData    the pointer to CPU_DATA_BLOCK of specified AP\r
+\r
+  @retval  CPU_STATE  the AP status\r
+\r
+**/\r
+CPU_STATE\r
+GetApState (\r
+  IN  CPU_DATA_BLOCK  *CpuData\r
+  )\r
+{\r
+  CPU_STATE State;\r
+\r
+  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+    CpuPause ();\r
+  }\r
+\r
+  State = CpuData->State;\r
+  ReleaseSpinLock (&CpuData->CpuDataLock);\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
+  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+    CpuPause ();\r
+  }\r
+\r
+  CpuData->State = State;\r
+  ReleaseSpinLock (&CpuData->CpuDataLock);\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
+  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+    CpuPause ();\r
+  }\r
+\r
+  CpuData->Parameter  = ProcedureArgument;\r
+  CpuData->Procedure  = Procedure;\r
+  ReleaseSpinLock (&CpuData->CpuDataLock);\r
+}\r
+\r
+/**\r
+  Check the Application Processors Status whether contains the Flags.\r
+\r
+  @param     CpuData  the pointer to CPU_DATA_BLOCK of specified AP\r
+  @param     Flags    the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
+\r
+  @retval    TRUE     the AP status includes the StatusFlag\r
+  @retval    FALSE    the AP status excludes the StatusFlag\r
+\r
+**/\r
+BOOLEAN\r
+TestCpuStatusFlag (\r
+  IN  CPU_DATA_BLOCK  *CpuData,\r
+  IN  UINT32          Flags\r
+  )\r
+{\r
+  UINT32 Ret;\r
+\r
+  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+    CpuPause ();\r
+  }\r
+\r
+  Ret = CpuData->Info.StatusFlag & Flags;\r
+  ReleaseSpinLock (&CpuData->CpuDataLock);\r
+\r
+  return !!(Ret);\r
+}\r
+\r
+/**\r
+  Bitwise-Or of the Application Processors Status with the Flags.\r
+\r
+  @param     CpuData  the pointer to CPU_DATA_BLOCK of specified AP\r
+  @param     Flags    the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
+\r
+**/\r
+VOID\r
+CpuStatusFlagOr (\r
+  IN  CPU_DATA_BLOCK  *CpuData,\r
+  IN  UINT32          Flags\r
+  )\r
+{\r
+  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+    CpuPause ();\r
+  }\r
+\r
+  CpuData->Info.StatusFlag |= Flags;\r
+  ReleaseSpinLock (&CpuData->CpuDataLock);\r
+}\r
+\r
+/**\r
+  Bitwise-AndNot of the Application Processors Status with the Flags.\r
+\r
+  @param     CpuData  the pointer to CPU_DATA_BLOCK of specified AP\r
+  @param     Flags    the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
+\r
+**/\r
+VOID\r
+CpuStatusFlagAndNot (\r
+  IN  CPU_DATA_BLOCK  *CpuData,\r
+  IN  UINT32          Flags\r
+  )\r
+{\r
+  while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+    CpuPause ();\r
+  }\r
+\r
+  CpuData->Info.StatusFlag &= ~Flags;\r
+  ReleaseSpinLock (&CpuData->CpuDataLock);\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
+  return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+  This service retrieves the number of logical processor in the platform\r
+  and the number of those logical processors that are enabled on this boot.\r
+  This service may only be called from the BSP.\r
+\r
+  This function is used to retrieve the following information:\r
+    - The number of logical processors that are present in the system.\r
+    - The number of enabled logical processors in the system at the instant\r
+      this call is made.\r
+\r
+  Because MP Service Protocol provides services to enable and disable processors\r
+  dynamically, the number of enabled logical processors may vary during the\r
+  course of a boot session.\r
+\r
+  If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
+  If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
+  EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
+  is returned in NumberOfProcessors, the number of currently enabled processor\r
+  is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
+\r
+  @param[in]  This                        A pointer to the EFI_MP_SERVICES_PROTOCOL\r
+                                          instance.\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.\r
+  @retval EFI_INVALID_PARAMETER   NumberOfEnabledProcessors is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetNumberOfProcessors (\r
+  IN  EFI_MP_SERVICES_PROTOCOL  *This,\r
+  OUT UINTN                     *NumberOfProcessors,\r
+  OUT UINTN                     *NumberOfEnabledProcessors\r
+  )\r
+{\r
+  if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (!IsBSP ()) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  *NumberOfProcessors        = mMpSystemData.NumberOfProcessors;\r
+  *NumberOfEnabledProcessors = mMpSystemData.NumberOfEnabledProcessors;\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
+  This service retrieves detailed MP-related information about any processor\r
+  on the platform. Note the following:\r
+    - The processor information may change during the course of a boot session.\r
+    - The information presented here is entirely MP related.\r
+\r
+  Information regarding the number of caches and their sizes, frequency of operation,\r
+  slot numbers is all considered platform-related information and is not provided\r
+  by this service.\r
+\r
+  @param[in]  This                  A pointer to the EFI_MP_SERVICES_PROTOCOL\r
+                                    instance.\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
+\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
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetProcessorInfo (\r
+  IN  EFI_MP_SERVICES_PROTOCOL   *This,\r
+  IN  UINTN                      ProcessorNumber,\r
+  OUT EFI_PROCESSOR_INFORMATION  *ProcessorInfoBuffer\r
+  )\r
+{\r
+  if (ProcessorInfoBuffer == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (!IsBSP ()) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  CopyMem (ProcessorInfoBuffer, &mMpSystemData.CpuDatas[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));\r
+  return EFI_SUCCESS;\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
+  EFI_STATUS            Status;\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
+  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
+    return EFI_NOT_READY;\r
+  }\r
+\r
+  SetApState (CpuData, CpuStateReady);\r
+\r
+  SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
+\r
+  CpuData->Timeout = TimeoutInMicroseconds;\r
+  CpuData->WaitEvent = WaitEvent;\r
+  CpuData->TimeoutActive = !!(TimeoutInMicroseconds);\r
+  CpuData->Finished = Finished;\r
+\r
+  if (WaitEvent != NULL) {\r
+    //\r
+    // Non Blocking\r
+    //\r
+    Status = gBS->SetTimer (\r
+                    CpuData->CheckThisAPEvent,\r
+                    TimerPeriodic,\r
+                    EFI_TIMER_PERIOD_MICROSECONDS (100)\r
+                    );\r
+    return Status;\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 lets the caller enable or disable an AP from this point onward.\r
+  This service may only be called from the BSP.\r
+\r
+  This service allows the caller enable or disable an AP from this point onward.\r
+  The caller can optionally specify the health status of the AP by Health. If\r
+  an AP is being disabled, then the state of the disabled AP is implementation\r
+  dependent. If an AP is enabled, then the implementation must guarantee that a\r
+  complete initialization sequence is performed on the AP, so the AP is in a state\r
+  that is compatible with an MP operating system. This service may not be supported\r
+  after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
+\r
+  If the enable or disable AP operation cannot be completed prior to the return\r
+  from this service, then 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] 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. This flag\r
+                               corresponds to StatusFlag defined in\r
+                               EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
+                               the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
+                               bits are ignored.  If it is NULL, this parameter\r
+                               is ignored.\r
+\r
+  @retval EFI_SUCCESS             The specified AP was enabled or disabled successfully.\r
+  @retval EFI_UNSUPPORTED         Enabling or disabling an AP cannot be completed\r
+                                  prior to this service returning.\r
+  @retval EFI_UNSUPPORTED         Enabling or disabling an AP is not supported.\r
+  @retval EFI_DEVICE_ERROR        The calling processor is an AP.\r
+  @retval EFI_NOT_FOUND           Processor with the handle specified by ProcessorNumber\r
+                                  does not exist.\r
+  @retval EFI_INVALID_PARAMETER   ProcessorNumber specifies the BSP.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EnableDisableAP (\r
+  IN  EFI_MP_SERVICES_PROTOCOL  *This,\r
+  IN  UINTN                     ProcessorNumber,\r
+  IN  BOOLEAN                   EnableAP,\r
+  IN  UINT32                    *HealthFlag OPTIONAL\r
+  )\r
+{\r
+  CPU_DATA_BLOCK *CpuData;\r
+\r
+  if (!IsBSP ()) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
+    return EFI_NOT_FOUND;\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
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  if (EnableAP) {\r
+    if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {\r
+      mMpSystemData.NumberOfEnabledProcessors++;\r
+    }\r
+    CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);\r
+  } else {\r
+    if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
+      mMpSystemData.NumberOfEnabledProcessors--;\r
+    }\r
+    CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);\r
+  }\r
+\r
+  if (HealthFlag != NULL) {\r
+    CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);\r
+    CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));\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
+  This service returns the processor handle number for the calling processor.\r
+  The returned value is in the range from 0 to the total number of logical\r
+  processors minus 1. The total number of logical processors can be retrieved\r
+  with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
+  called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
+  is returned. Otherwise, the current processors handle number is returned in\r
+  ProcessorNumber, and EFI_SUCCESS is returned.\r
+\r
+  @param[in]  This             A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
+  @param[out] 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
+\r
+  @retval EFI_SUCCESS             The current processor handle number was returned\r
+                                  in ProcessorNumber.\r
+  @retval EFI_INVALID_PARAMETER   ProcessorNumber is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+WhoAmI (\r
+  IN EFI_MP_SERVICES_PROTOCOL  *This,\r
+  OUT UINTN                    *ProcessorNumber\r
+  )\r
+{\r
+  UINTN   Index;\r
+  UINT32  ProcessorId;\r
+\r
+  if (ProcessorNumber == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  ProcessorId = GetApicId ();\r
+  for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {\r
+    if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {\r
+      break;\r
+    }\r
+  }\r
+\r
+  *ProcessorNumber = Index;\r
+  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
+}\r
+\r
+/**\r
+  Application Processors do loop routine\r
+  after switch to its own stack.\r
+\r
+  @param  Context1    A pointer to the context to pass into the function.\r
+  @param  Context2    A pointer to the context to pass into the function.\r
+\r
+**/\r
+VOID\r
+ProcessorToIdleState (\r
+  IN      VOID                      *Context1,  OPTIONAL\r
+  IN      VOID                      *Context2   OPTIONAL\r
+  )\r
+{\r
+  DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));\r
+\r
+  AsmApDoneWithCommonStack ();\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
+  gBS->SetTimer (CpuData->CheckThisAPEvent, TimerCancel, 0);\r
+  if (CpuData->WaitEvent) {\r
+    gBS->SignalEvent (CpuData->WaitEvent);\r
+    CpuData->WaitEvent = NULL;\r
+  }\r
+}\r
 \r
 /**\r
   Application Processor C code entry point.\r
@@ -29,8 +756,100 @@ 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
+\r
+  SwitchStack (\r
+    (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
+    NULL,\r
+    NULL,\r
+    TopOfApStack);\r
+}\r
+\r
+/**\r
+  This function is called by all processors (both BSP and AP) once and collects MP related data.\r
+\r
+  @param Bsp             TRUE if the CPU is BSP\r
+  @param ProcessorNumber The specific processor number\r
+\r
+  @retval EFI_SUCCESS    Data for the processor collected and filled in\r
+\r
+**/\r
+EFI_STATUS\r
+FillInProcessorInformation (\r
+  IN     BOOLEAN              Bsp,\r
+  IN     UINTN                ProcessorNumber\r
+  )\r
+{\r
+  CPU_DATA_BLOCK  *CpuData;\r
+  UINT32          ProcessorId;\r
+\r
+  CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
+  ProcessorId  = GetApicId ();\r
+  CpuData->Info.ProcessorId  = ProcessorId;\r
+  CpuData->Info.StatusFlag   = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
+  if (Bsp) {\r
+    CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
+  }\r
+  CpuData->Info.Location.Package = ProcessorId;\r
+  CpuData->Info.Location.Core    = 0;\r
+  CpuData->Info.Location.Thread  = 0;\r
+  CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
+\r
+  CpuData->Procedure        = NULL;\r
+  CpuData->Parameter        = NULL;\r
+  InitializeSpinLock (&CpuData->CpuDataLock);\r
+\r
+  return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Prepare the System Data.\r
+\r
+  @retval EFI_SUCCESS     the System Data finished initilization.\r
+\r
+**/\r
+EFI_STATUS\r
+InitMpSystemData (\r
+  VOID\r
+  )\r
+{\r
+  UINTN          ProcessorNumber;\r
+  CPU_DATA_BLOCK *CpuData;\r
+  EFI_STATUS     Status;\r
+\r
+  ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
+\r
+  mMpSystemData.NumberOfProcessors = 1;\r
+  mMpSystemData.NumberOfEnabledProcessors = 1;\r
+\r
+  mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
+  ASSERT(mMpSystemData.CpuDatas != NULL);\r
+\r
+  for (ProcessorNumber = 0; ProcessorNumber < gMaxLogicalProcessorNumber; ProcessorNumber++) {\r
+    CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
+    Status = gBS->CreateEvent (\r
+                    EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+                    TPL_CALLBACK,\r
+                    CheckThisAPStatus,\r
+                    (VOID *) CpuData,\r
+                    &CpuData->CheckThisAPEvent\r
+                    );\r
+    ASSERT_EFI_ERROR (Status);\r
+  }\r
+\r
+  //\r
+  // BSP\r
+  //\r
+  FillInProcessorInformation (TRUE, 0);\r
+\r
+  return EFI_SUCCESS;\r
+}\r
 \r
 /**\r
   Initialize Multi-processor support.\r
@@ -41,5 +860,40 @@ InitializeMpSupport (
   VOID\r
   )\r
 {\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
 \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
+  InitMpSystemData ();\r
+\r
+  if (mMpSystemData.NumberOfProcessors == 1) {\r
+    FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
+    return;\r
+  }\r
+\r
+  if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
+    FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
+                                (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
+                                gApStackSize));\r
+  }\r
+}\r