]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/CpuDxe/CpuMp.c
UefiCpuPkg: Make the comments align with the functions
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
index 38603f9867cebd635350ddfb7217691cf884589e..9659bd280725c56a5a6ed325a191a4ff3f7bd83a 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
 EFI_HANDLE     mMpServiceHandle       = NULL;\r
 UINTN          mNumberOfProcessors    = 1;\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
 \r
 EFI_MP_SERVICES_PROTOCOL  mMpServicesTemplate = {\r
   GetNumberOfProcessors,\r
@@ -40,372 +28,6 @@ EFI_MP_SERVICES_PROTOCOL  mMpServicesTemplate = {
   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
-  @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
-STATIC\r
-CPU_STATE\r
-GetApState (\r
-  IN  CPU_DATA_BLOCK  *CpuData\r
-  )\r
-{\r
-  CPU_STATE State;\r
-\r
-  GetMpSpinLock (CpuData);\r
-  State = CpuData->State;\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
-STATIC\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
-  @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
-  GetMpSpinLock (CpuData);\r
-  Ret = CpuData->Info.StatusFlag & Flags;\r
-  ReleaseMpSpinLock (CpuData);\r
-\r
-  return (BOOLEAN) (Ret != 0);\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
-  GetMpSpinLock (CpuData);\r
-  CpuData->Info.StatusFlag |= Flags;\r
-  ReleaseMpSpinLock (CpuData);\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
-  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
-  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
-  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
-  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
   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
@@ -677,8 +299,8 @@ StartupAllAPs (
 \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
+  @param[in]  Procedure               A pointer to the function to be run on the\r
+                                      designated AP 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
@@ -687,13 +309,13 @@ StartupAllAPs (
                                       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
+                                      blocking mode. BSP waits until this AP 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
+                                      Procedure to be started on this AP,\r
+                                      and go on executing immediately. If this AP\r
+                                      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
@@ -701,20 +323,20 @@ StartupAllAPs (
                                       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
+                                      this AP to finish this 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
+                                      this AP returns from Procedure, then Procedure\r
+                                      on the AP is terminated. The\r
+                                      AP is 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[in]  ProcedureArgument       The parameter passed into Procedure on the\r
+                                      specified AP.\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
@@ -825,8 +447,8 @@ SwitchBSP (
   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
+  @param[in] ProcessorNumber   The handle number of AP.\r
+                               The range is from 0 to the total number of\r
                                logical processors minus 1. The total number of\r
                                logical processors can be retrieved by\r
                                EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
@@ -875,8 +497,8 @@ EnableDisableAP (
   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
+  @param[out] ProcessorNumber  Pointer to the handle number of AP.\r
+                               The range is from 0 to the total number of\r
                                logical processors minus 1. The total number of\r
                                logical processors can be retrieved by\r
                                EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
@@ -917,6 +539,7 @@ CollectBistDataFromHob (
   UINTN                                 ProcessorNumber;\r
   EFI_PROCESSOR_INFORMATION             ProcessorInfo;\r
   EFI_HEALTH_FLAGS                      BistData;\r
+  UINTN                                 CpuInstanceNumber;\r
 \r
   SecPlatformInformation2 = NULL;\r
   SecPlatformInformation  = NULL;\r
@@ -956,66 +579,28 @@ CollectBistDataFromHob (
     }\r
   }\r
 \r
-  while ((NumberOfData--) > 0) {\r
-    for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
-      MpInitLibGetProcessorInfo (ProcessorNumber, &ProcessorInfo, &BistData);\r
-      if (ProcessorInfo.ProcessorId == CpuInstance[NumberOfData].CpuLocation) {\r
+  for (ProcessorNumber = 0; ProcessorNumber < mNumberOfProcessors; ProcessorNumber++) {\r
+    MpInitLibGetProcessorInfo (ProcessorNumber, &ProcessorInfo, &BistData);\r
+    for (CpuInstanceNumber = 0; CpuInstanceNumber < NumberOfData; CpuInstanceNumber++) {\r
+      if (ProcessorInfo.ProcessorId == CpuInstance[CpuInstanceNumber].CpuLocation) {\r
         //\r
         // Update CPU health status for MP Services Protocol according to BIST data.\r
         //\r
-        BistData = CpuInstance[NumberOfData].InfoRecord.IA32HealthFlags;\r
-      }\r
-      if (BistData.Uint32 != 0) {\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
+        BistData = CpuInstance[CpuInstanceNumber].InfoRecord.IA32HealthFlags;\r
       }\r
     }\r
+    if (BistData.Uint32 != 0) {\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
-  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
@@ -1029,22 +614,14 @@ InitializeMpSupport (
   UINTN          NumberOfProcessors;\r
   UINTN          NumberOfEnabledProcessors;\r
 \r
-  NumberOfProcessors = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
-  if (NumberOfProcessors < 1) {\r
-    DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
-    return;\r
-  }\r
-\r
   //\r
-  // Only perform AP detection if PcdCpuMaxLogicalProcessorNumber is greater than 1\r
+  // Wakeup APs to do initialization\r
   //\r
-  if (NumberOfProcessors > 1) {\r
-    Status = MpInitLibInitialize ();\r
-    ASSERT_EFI_ERROR (Status);\r
+  Status = MpInitLibInitialize ();\r
+  ASSERT_EFI_ERROR (Status);\r
 \r
-    MpInitLibGetNumberOfProcessors (&NumberOfProcessors, &NumberOfEnabledProcessors);\r
-    mNumberOfProcessors = NumberOfProcessors;\r
-  }\r
+  MpInitLibGetNumberOfProcessors (&NumberOfProcessors, &NumberOfEnabledProcessors);\r
+  mNumberOfProcessors = NumberOfProcessors;\r
   DEBUG ((EFI_D_ERROR, "Detect CPU count: %d\n", mNumberOfProcessors));\r
 \r
   //\r