]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg: Move GetProcessorLocation() to LocalApicLib library
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.c
index 2691af289a2a573369bf0849944d6436cd3f0efd..f205b6bad4ccff9018a593f9d4c78e03d69530c8 100644 (file)
@@ -57,132 +57,6 @@ IsBspExecuteDisableEnabled (
   return Enabled;\r
 }\r
 \r
-/**\r
-  Get CPU Package/Core/Thread location information.\r
-\r
-  @param[in]  InitialApicId     CPU APIC ID\r
-  @param[out] Location          Pointer to CPU location information\r
-**/\r
-VOID\r
-ExtractProcessorLocation (\r
-  IN  UINT32                     InitialApicId,\r
-  OUT EFI_CPU_PHYSICAL_LOCATION  *Location\r
-  )\r
-{\r
-  BOOLEAN                        TopologyLeafSupported;\r
-  UINTN                          ThreadBits;\r
-  UINTN                          CoreBits;\r
-  CPUID_VERSION_INFO_EBX         VersionInfoEbx;\r
-  CPUID_VERSION_INFO_EDX         VersionInfoEdx;\r
-  CPUID_CACHE_PARAMS_EAX         CacheParamsEax;\r
-  CPUID_EXTENDED_TOPOLOGY_EAX    ExtendedTopologyEax;\r
-  CPUID_EXTENDED_TOPOLOGY_EBX    ExtendedTopologyEbx;\r
-  CPUID_EXTENDED_TOPOLOGY_ECX    ExtendedTopologyEcx;\r
-  UINT32                         MaxCpuIdIndex;\r
-  UINT32                         SubIndex;\r
-  UINTN                          LevelType;\r
-  UINT32                         MaxLogicProcessorsPerPackage;\r
-  UINT32                         MaxCoresPerPackage;\r
-\r
-  //\r
-  // Check if the processor is capable of supporting more than one logical processor.\r
-  //\r
-  AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
-  if (VersionInfoEdx.Bits.HTT == 0) {\r
-    Location->Thread  = 0;\r
-    Location->Core    = 0;\r
-    Location->Package = 0;\r
-    return;\r
-  }\r
-\r
-  ThreadBits = 0;\r
-  CoreBits = 0;\r
-\r
-  //\r
-  // Assume three-level mapping of APIC ID: Package:Core:SMT.\r
-  //\r
-\r
-  TopologyLeafSupported = FALSE;\r
-  //\r
-  // Get the max index of basic CPUID\r
-  //\r
-  AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
-\r
-  //\r
-  // If the extended topology enumeration leaf is available, it\r
-  // is the preferred mechanism for enumerating topology.\r
-  //\r
-  if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
-    AsmCpuidEx (\r
-      CPUID_EXTENDED_TOPOLOGY,\r
-      0,\r
-      &ExtendedTopologyEax.Uint32,\r
-      &ExtendedTopologyEbx.Uint32,\r
-      &ExtendedTopologyEcx.Uint32,\r
-      NULL\r
-      );\r
-    //\r
-    // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
-    // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
-    // supported on that processor.\r
-    //\r
-    if (ExtendedTopologyEbx.Uint32 != 0) {\r
-      TopologyLeafSupported = TRUE;\r
-\r
-      //\r
-      // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
-      // the SMT sub-field of x2APIC ID.\r
-      //\r
-      LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
-      ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
-      ThreadBits = ExtendedTopologyEax.Bits.ApicIdShift;\r
-\r
-      //\r
-      // Software must not assume any "level type" encoding\r
-      // value to be related to any sub-leaf index, except sub-leaf 0.\r
-      //\r
-      SubIndex = 1;\r
-      do {\r
-        AsmCpuidEx (\r
-          CPUID_EXTENDED_TOPOLOGY,\r
-          SubIndex,\r
-          &ExtendedTopologyEax.Uint32,\r
-          NULL,\r
-          &ExtendedTopologyEcx.Uint32,\r
-          NULL\r
-          );\r
-        LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
-        if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
-          CoreBits = ExtendedTopologyEax.Bits.ApicIdShift - ThreadBits;\r
-          break;\r
-        }\r
-        SubIndex++;\r
-      } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
-    }\r
-  }\r
-\r
-  if (!TopologyLeafSupported) {\r
-    AsmCpuid (CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
-    MaxLogicProcessorsPerPackage = VersionInfoEbx.Bits.MaximumAddressableIdsForLogicalProcessors;\r
-    if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
-      AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
-      MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
-    } else {\r
-      //\r
-      // Must be a single-core processor.\r
-      //\r
-      MaxCoresPerPackage = 1;\r
-    }\r
-\r
-    ThreadBits = (UINTN) (HighBitSet32 (MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
-    CoreBits = (UINTN) (HighBitSet32 (MaxCoresPerPackage - 1) + 1);\r
-  }\r
-\r
-  Location->Thread  = InitialApicId & ((1 << ThreadBits) - 1);\r
-  Location->Core    = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
-  Location->Package = (InitialApicId >> (ThreadBits + CoreBits));\r
-}\r
-\r
 /**\r
   Worker function for SwitchBSP().\r
 \r
@@ -513,10 +387,6 @@ CollectProcessorCount (
   CpuMpData->InitFlag     = ApInitConfig;\r
   CpuMpData->X2ApicEnable = FALSE;\r
   WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL);\r
-  //\r
-  // Wait for AP task to complete and then exit.\r
-  //\r
-  MicroSecondDelay (PcdGet32(PcdCpuApInitTimeOutInMicroSeconds));\r
   CpuMpData->InitFlag = ApInitDone;\r
   ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
   //\r
@@ -863,7 +733,12 @@ WakeUpAP (
       //\r
       SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);\r
     }\r
-    if (CpuMpData->InitFlag != ApInitConfig) {\r
+    if (CpuMpData->InitFlag == ApInitConfig) {\r
+      //\r
+      // Wait for all potential APs waken up in one specified period\r
+      //\r
+      MicroSecondDelay (PcdGet32(PcdCpuApInitTimeOutInMicroSeconds));\r
+    } else {\r
       //\r
       // Wait all APs waken up if this is not the 1st broadcast of SIPI\r
       //\r
@@ -1291,7 +1166,7 @@ MpInitLibInitialize (
   CpuMpData->CpuApStackSize   = ApStackSize;\r
   CpuMpData->BackupBuffer     = BackupBufferAddr;\r
   CpuMpData->BackupBufferSize = ApResetVectorSize;\r
-  CpuMpData->EndOfPeiFlag     = FALSE;\r
+  CpuMpData->SaveRestoreFlag  = FALSE;\r
   CpuMpData->WakeupBuffer     = (UINTN) -1;\r
   CpuMpData->CpuCount         = 1;\r
   CpuMpData->BspNumber        = 0;\r
@@ -1450,7 +1325,12 @@ MpInitLibGetProcessorInfo (
   //\r
   // Get processor location information\r
   //\r
-  ExtractProcessorLocation (CpuMpData->CpuData[ProcessorNumber].ApicId, &ProcessorInfoBuffer->Location);\r
+  GetProcessorLocation (\r
+    CpuMpData->CpuData[ProcessorNumber].ApicId,\r
+    &ProcessorInfoBuffer->Location.Package,\r
+    &ProcessorInfoBuffer->Location.Core,\r
+    &ProcessorInfoBuffer->Location.Thread\r
+    );\r
 \r
   if (HealthData != NULL) {\r
     HealthData->Uint32 = CpuMpData->CpuData[ProcessorNumber].Health;\r
@@ -1719,6 +1599,276 @@ MpInitLibGetNumberOfProcessors (
 }\r
 \r
 \r
+/**\r
+  Worker function to execute a caller provided function on all enabled APs.\r
+\r
+  @param[in]  Procedure               A pointer to the function to be run on\r
+                                      enabled APs of the system.\r
+  @param[in]  SingleThread            If TRUE, then all the enabled APs execute\r
+                                      the function specified by Procedure one by\r
+                                      one, in ascending order of processor handle\r
+                                      number.  If FALSE, then all the enabled APs\r
+                                      execute the function specified by Procedure\r
+                                      simultaneously.\r
+  @param[in]  WaitEvent               The event created by the caller with CreateEvent()\r
+                                      service.\r
+  @param[in]  TimeoutInMicrosecsond   Indicates the time limit in microseconds for\r
+                                      APs to return from Procedure, either for\r
+                                      blocking or non-blocking mode.\r
+  @param[in]  ProcedureArgument       The parameter passed into Procedure for\r
+                                      all APs.\r
+  @param[out] FailedCpuList           If all APs finish successfully, then its\r
+                                      content is set to NULL. If not all APs\r
+                                      finish before timeout expires, then its\r
+                                      content is set to address of the buffer\r
+                                      holding handle numbers of the failed APs.\r
+\r
+  @retval EFI_SUCCESS             In blocking mode, all APs have finished before\r
+                                  the timeout expired.\r
+  @retval EFI_SUCCESS             In non-blocking mode, function has been dispatched\r
+                                  to all enabled APs.\r
+  @retval others                  Failed to Startup all APs.\r
+\r
+**/\r
+EFI_STATUS\r
+StartupAllAPsWorker (\r
+  IN  EFI_AP_PROCEDURE          Procedure,\r
+  IN  BOOLEAN                   SingleThread,\r
+  IN  EFI_EVENT                 WaitEvent               OPTIONAL,\r
+  IN  UINTN                     TimeoutInMicroseconds,\r
+  IN  VOID                      *ProcedureArgument      OPTIONAL,\r
+  OUT UINTN                     **FailedCpuList         OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+  CPU_MP_DATA             *CpuMpData;\r
+  UINTN                   ProcessorCount;\r
+  UINTN                   ProcessorNumber;\r
+  UINTN                   CallerNumber;\r
+  CPU_AP_DATA             *CpuData;\r
+  BOOLEAN                 HasEnabledAp;\r
+  CPU_STATE               ApState;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  if (FailedCpuList != NULL) {\r
+    *FailedCpuList = NULL;\r
+  }\r
+\r
+  if (CpuMpData->CpuCount == 1) {\r
+    return EFI_NOT_STARTED;\r
+  }\r
+\r
+  if (Procedure == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Check whether caller processor is BSP\r
+  //\r
+  MpInitLibWhoAmI (&CallerNumber);\r
+  if (CallerNumber != CpuMpData->BspNumber) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  //\r
+  // Update AP state\r
+  //\r
+  CheckAndUpdateApsStatus ();\r
+\r
+  ProcessorCount = CpuMpData->CpuCount;\r
+  HasEnabledAp   = FALSE;\r
+  //\r
+  // Check whether all enabled APs are idle.\r
+  // If any enabled AP is not idle, return EFI_NOT_READY.\r
+  //\r
+  for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
+    CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+    if (ProcessorNumber != CpuMpData->BspNumber) {\r
+      ApState = GetApState (CpuData);\r
+      if (ApState != CpuStateDisabled) {\r
+        HasEnabledAp = TRUE;\r
+        if (ApState != CpuStateIdle) {\r
+          //\r
+          // If any enabled APs are busy, return EFI_NOT_READY.\r
+          //\r
+          return EFI_NOT_READY;\r
+        }\r
+      }\r
+    }\r
+  }\r
+\r
+  if (!HasEnabledAp) {\r
+    //\r
+    // If no enabled AP exists, return EFI_NOT_STARTED.\r
+    //\r
+    return EFI_NOT_STARTED;\r
+  }\r
+\r
+  CpuMpData->StartCount = 0;\r
+  for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
+    CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+    CpuData->Waiting = FALSE;\r
+    if (ProcessorNumber != CpuMpData->BspNumber) {\r
+      if (CpuData->State == CpuStateIdle) {\r
+        //\r
+        // Mark this processor as responsible for current calling.\r
+        //\r
+        CpuData->Waiting = TRUE;\r
+        CpuMpData->StartCount++;\r
+      }\r
+    }\r
+  }\r
+\r
+  CpuMpData->Procedure     = Procedure;\r
+  CpuMpData->ProcArguments = ProcedureArgument;\r
+  CpuMpData->SingleThread  = SingleThread;\r
+  CpuMpData->FinishedCount = 0;\r
+  CpuMpData->RunningCount  = 0;\r
+  CpuMpData->FailedCpuList = FailedCpuList;\r
+  CpuMpData->ExpectedTime  = CalculateTimeout (\r
+                               TimeoutInMicroseconds,\r
+                               &CpuMpData->CurrentTime\r
+                               );\r
+  CpuMpData->TotalTime     = 0;\r
+  CpuMpData->WaitEvent     = WaitEvent;\r
+\r
+  if (!SingleThread) {\r
+    WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);\r
+  } else {\r
+    for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
+      if (ProcessorNumber == CallerNumber) {\r
+        continue;\r
+      }\r
+      if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
+        WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
+        break;\r
+      }\r
+    }\r
+  }\r
+\r
+  Status = EFI_SUCCESS;\r
+  if (WaitEvent == NULL) {\r
+    do {\r
+      Status = CheckAllAPs ();\r
+    } while (Status == EFI_NOT_READY);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Worker function to let the caller get one enabled AP to execute a caller-provided\r
+  function.\r
+\r
+  @param[in]  Procedure               A pointer to the function to be run on\r
+                                      enabled APs of the system.\r
+  @param[in]  ProcessorNumber         The handle number of the AP.\r
+  @param[in]  WaitEvent               The event created by the caller with CreateEvent()\r
+                                      service.\r
+  @param[in]  TimeoutInMicrosecsond   Indicates the time limit in microseconds for\r
+                                      APs to return from Procedure, either for\r
+                                      blocking or non-blocking mode.\r
+  @param[in]  ProcedureArgument       The parameter passed into Procedure for\r
+                                      all APs.\r
+  @param[out] Finished                If AP returns from Procedure before the\r
+                                      timeout expires, its content is set to TRUE.\r
+                                      Otherwise, the value is set to FALSE.\r
+\r
+  @retval EFI_SUCCESS             In blocking mode, specified AP finished before\r
+                                  the timeout expires.\r
+  @retval others                  Failed to Startup AP.\r
+\r
+**/\r
+EFI_STATUS\r
+StartupThisAPWorker (\r
+  IN  EFI_AP_PROCEDURE          Procedure,\r
+  IN  UINTN                     ProcessorNumber,\r
+  IN  EFI_EVENT                 WaitEvent               OPTIONAL,\r
+  IN  UINTN                     TimeoutInMicroseconds,\r
+  IN  VOID                      *ProcedureArgument      OPTIONAL,\r
+  OUT BOOLEAN                   *Finished               OPTIONAL\r
+  )\r
+{\r
+  EFI_STATUS              Status;\r
+  CPU_MP_DATA             *CpuMpData;\r
+  CPU_AP_DATA             *CpuData;\r
+  UINTN                   CallerNumber;\r
+\r
+  CpuMpData = GetCpuMpData ();\r
+\r
+  if (Finished != NULL) {\r
+    *Finished = FALSE;\r
+  }\r
+\r
+  //\r
+  // Check whether caller processor is BSP\r
+  //\r
+  MpInitLibWhoAmI (&CallerNumber);\r
+  if (CallerNumber != CpuMpData->BspNumber) {\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+\r
+  //\r
+  // Check whether processor with the handle specified by ProcessorNumber exists\r
+  //\r
+  if (ProcessorNumber >= CpuMpData->CpuCount) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  //\r
+  // Check whether specified processor is BSP\r
+  //\r
+  if (ProcessorNumber == CpuMpData->BspNumber) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Check parameter Procedure\r
+  //\r
+  if (Procedure == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Update AP state\r
+  //\r
+  CheckAndUpdateApsStatus ();\r
+\r
+  //\r
+  // Check whether specified AP is disabled\r
+  //\r
+  if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // If WaitEvent is not NULL, execute in non-blocking mode.\r
+  // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
+  // CheckAPsStatus() will check completion and timeout periodically.\r
+  //\r
+  CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+  CpuData->WaitEvent    = WaitEvent;\r
+  CpuData->Finished     = Finished;\r
+  CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);\r
+  CpuData->TotalTime    = 0;\r
+\r
+  WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
+\r
+  //\r
+  // If WaitEvent is NULL, execute in blocking mode.\r
+  // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.\r
+  //\r
+  Status = EFI_SUCCESS;\r
+  if (WaitEvent == NULL) {\r
+    do {\r
+      Status = CheckThisAP (ProcessorNumber);\r
+    } while (Status == EFI_NOT_READY);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
 /**\r
   Get pointer to CPU MP Data structure from GUIDed HOB.\r
 \r
@@ -1741,3 +1891,42 @@ GetCpuMpDataFromGuidedHob (
   }\r
   return CpuMpData;\r
 }\r
+\r
+/**\r
+  Get available system memory below 1MB by specified size.\r
+\r
+  @param[in]  CpuMpData  The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+BackupAndPrepareWakeupBuffer(\r
+  IN CPU_MP_DATA              *CpuMpData\r
+  )\r
+{\r
+  CopyMem (\r
+    (VOID *) CpuMpData->BackupBuffer,\r
+    (VOID *) CpuMpData->WakeupBuffer,\r
+    CpuMpData->BackupBufferSize\r
+    );\r
+  CopyMem (\r
+    (VOID *) CpuMpData->WakeupBuffer,\r
+    (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,\r
+    CpuMpData->AddressMap.RendezvousFunnelSize\r
+    );\r
+}\r
+\r
+/**\r
+  Restore wakeup buffer data.\r
+\r
+  @param[in]  CpuMpData  The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+RestoreWakeupBuffer(\r
+  IN CPU_MP_DATA              *CpuMpData\r
+  )\r
+{\r
+  CopyMem (\r
+    (VOID *) CpuMpData->WakeupBuffer,\r
+    (VOID *) CpuMpData->BackupBuffer,\r
+    CpuMpData->BackupBufferSize\r
+    );\r
+}\r