]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/DxeMpInitLib: Support source debugging on AP function
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.c
index 9641e5e82c200f8f14154f17d258805730e9fcf7..a21a980b58b1f48f5642608237c32fceb1f936c5 100644 (file)
@@ -253,33 +253,33 @@ SortApicId (
   UINTN             Index2;\r
   UINTN             Index3;\r
   UINT32            ApicId;\r
-  CPU_AP_DATA       CpuData;\r
+  CPU_INFO_IN_HOB   CpuInfo;\r
   UINT32            ApCount;\r
   CPU_INFO_IN_HOB   *CpuInfoInHob;\r
 \r
   ApCount = CpuMpData->CpuCount - 1;\r
-\r
+  CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
   if (ApCount != 0) {\r
     for (Index1 = 0; Index1 < ApCount; Index1++) {\r
       Index3 = Index1;\r
       //\r
       // Sort key is the hardware default APIC ID\r
       //\r
-      ApicId = CpuMpData->CpuData[Index1].ApicId;\r
+      ApicId = CpuInfoInHob[Index1].ApicId;\r
       for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {\r
-        if (ApicId > CpuMpData->CpuData[Index2].ApicId) {\r
+        if (ApicId > CpuInfoInHob[Index2].ApicId) {\r
           Index3 = Index2;\r
-          ApicId = CpuMpData->CpuData[Index2].ApicId;\r
+          ApicId = CpuInfoInHob[Index2].ApicId;\r
         }\r
       }\r
       if (Index3 != Index1) {\r
-        CopyMem (&CpuData, &CpuMpData->CpuData[Index3], sizeof (CPU_AP_DATA));\r
+        CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));\r
         CopyMem (\r
-          &CpuMpData->CpuData[Index3],\r
-          &CpuMpData->CpuData[Index1],\r
-          sizeof (CPU_AP_DATA)\r
+          &CpuInfoInHob[Index3],\r
+          &CpuInfoInHob[Index1],\r
+          sizeof (CPU_INFO_IN_HOB)\r
           );\r
-        CopyMem (&CpuMpData->CpuData[Index1], &CpuData, sizeof (CPU_AP_DATA));\r
+        CopyMem (&CpuInfoInHob[Index1], &CpuInfo, sizeof (CPU_INFO_IN_HOB));\r
       }\r
     }\r
 \r
@@ -288,18 +288,11 @@ SortApicId (
     //\r
     ApicId = GetInitialApicId ();\r
     for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {\r
-      if (CpuMpData->CpuData[Index1].ApicId == ApicId) {\r
+      if (CpuInfoInHob[Index1].ApicId == ApicId) {\r
         CpuMpData->BspNumber = (UINT32) Index1;\r
         break;\r
       }\r
     }\r
-\r
-    CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
-    for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {\r
-      CpuInfoInHob[Index1].InitialApicId = CpuMpData->CpuData[Index1].InitialApicId;\r
-      CpuInfoInHob[Index1].ApicId        = CpuMpData->CpuData[Index1].ApicId;\r
-      CpuInfoInHob[Index1].Health        = CpuMpData->CpuData[Index1].Health;\r
-    }\r
   }\r
 }\r
 \r
@@ -344,8 +337,8 @@ ApInitializeSync (
 /**\r
   Find the current Processor number by APIC ID.\r
 \r
-  @param[in] CpuMpData         Pointer to PEI CPU MP Data\r
-  @param[in] ProcessorNumber   Return the pocessor number found\r
+  @param[in]  CpuMpData         Pointer to PEI CPU MP Data\r
+  @param[out] ProcessorNumber   Return the pocessor number found\r
 \r
   @retval EFI_SUCCESS          ProcessorNumber is found and returned.\r
   @retval EFI_NOT_FOUND        ProcessorNumber is not found.\r
@@ -358,10 +351,13 @@ GetProcessorNumber (
 {\r
   UINTN                   TotalProcessorNumber;\r
   UINTN                   Index;\r
+  CPU_INFO_IN_HOB         *CpuInfoInHob;\r
+\r
+  CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
 \r
   TotalProcessorNumber = CpuMpData->CpuCount;\r
   for (Index = 0; Index < TotalProcessorNumber; Index ++) {\r
-    if (CpuMpData->CpuData[Index].ApicId == GetApicId ()) {\r
+    if (CpuInfoInHob[Index].ApicId == GetApicId ()) {\r
       *ProcessorNumber = Index;\r
       return EFI_SUCCESS;\r
     }\r
@@ -424,27 +420,34 @@ CollectProcessorCount (
   return CpuMpData->CpuCount;\r
 }\r
 \r
-/*\r
+/**\r
   Initialize CPU AP Data when AP is wakeup at the first time.\r
 \r
   @param[in, out] CpuMpData        Pointer to PEI CPU MP Data\r
   @param[in]      ProcessorNumber  The handle number of processor\r
   @param[in]      BistData         Processor BIST data\r
+  @param[in]      ApTopOfStack     Top of AP stack\r
 \r
 **/\r
 VOID\r
 InitializeApData (\r
   IN OUT CPU_MP_DATA      *CpuMpData,\r
   IN     UINTN            ProcessorNumber,\r
-  IN     UINT32           BistData\r
+  IN     UINT32           BistData,\r
+  IN     UINT64           ApTopOfStack\r
   )\r
 {\r
+  CPU_INFO_IN_HOB          *CpuInfoInHob;\r
+\r
+  CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+  CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
+  CpuInfoInHob[ProcessorNumber].ApicId        = GetApicId ();\r
+  CpuInfoInHob[ProcessorNumber].Health        = BistData;\r
+  CpuInfoInHob[ProcessorNumber].ApTopOfStack  = ApTopOfStack;\r
+\r
   CpuMpData->CpuData[ProcessorNumber].Waiting    = FALSE;\r
-  CpuMpData->CpuData[ProcessorNumber].Health     = BistData;\r
   CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;\r
-  CpuMpData->CpuData[ProcessorNumber].ApicId     = GetApicId ();\r
-  CpuMpData->CpuData[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
-  if (CpuMpData->CpuData[ProcessorNumber].InitialApicId >= 0xFF) {\r
+  if (CpuInfoInHob[ProcessorNumber].InitialApicId >= 0xFF) {\r
     //\r
     // Set x2APIC mode if there are any logical processor reporting\r
     // an Initial APIC ID of 255 or greater.\r
@@ -477,6 +480,8 @@ ApWakeupFunction (
   VOID                       *Parameter;\r
   UINT32                     BistData;\r
   volatile UINT32            *ApStartupSignalBuffer;\r
+  CPU_INFO_IN_HOB            *CpuInfoInHob;\r
+  UINT64                     ApTopOfStack;\r
 \r
   //\r
   // AP finished assembly code and begin to execute C code\r
@@ -495,7 +500,8 @@ ApWakeupFunction (
       //\r
       // This is first time AP wakeup, get BIST information from AP stack\r
       //\r
-      BistData = *(UINT32 *) (CpuMpData->Buffer + ProcessorNumber * CpuMpData->CpuApStackSize - sizeof (UINTN));\r
+      ApTopOfStack  = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize;\r
+      BistData = *(UINT32 *) ((UINTN) ApTopOfStack - sizeof (UINTN));\r
       //\r
       // Do some AP initialize sync\r
       //\r
@@ -504,7 +510,7 @@ ApWakeupFunction (
       // Sync BSP's Control registers to APs\r
       //\r
       RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);\r
-      InitializeApData (CpuMpData, ProcessorNumber, BistData);\r
+      InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack);\r
       ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
     } else {\r
       //\r
@@ -533,9 +539,14 @@ ApWakeupFunction (
         if (Procedure != NULL) {\r
           SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy);\r
           //\r
+          // Enable source debugging on AP function\r
+          //         \r
+          EnableDebugAgent ();\r
+          //\r
           // Invoke AP function here\r
           //\r
           Procedure (Parameter);\r
+          CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
           if (CpuMpData->SwitchBspFlag) {\r
             //\r
             // Re-get the processor number due to BSP/AP maybe exchange in AP function\r
@@ -543,12 +554,14 @@ ApWakeupFunction (
             GetProcessorNumber (CpuMpData, &ProcessorNumber);\r
             CpuMpData->CpuData[ProcessorNumber].ApFunction = 0;\r
             CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0;\r
+            ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
+            CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack;\r
           } else {\r
             //\r
             // Re-get the CPU APICID and Initial APICID\r
             //\r
-            CpuMpData->CpuData[ProcessorNumber].ApicId        = GetApicId ();\r
-            CpuMpData->CpuData[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
+            CpuInfoInHob[ProcessorNumber].ApicId        = GetApicId ();\r
+            CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
           }\r
         }\r
         SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);\r
@@ -662,6 +675,8 @@ FillExchangeInfoData (
 \r
   ExchangeInfo->CFunction       = (UINTN) ApWakeupFunction;\r
   ExchangeInfo->NumApsExecuting = 0;\r
+  ExchangeInfo->InitFlag        = (UINTN) CpuMpData->InitFlag;\r
+  ExchangeInfo->CpuInfo         = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
   ExchangeInfo->CpuMpData       = CpuMpData;\r
 \r
   ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();\r
@@ -673,6 +688,21 @@ FillExchangeInfoData (
   AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);\r
 }\r
 \r
+/**\r
+  Helper function that waits until the finished AP count reaches the specified\r
+  limit, or the specified timeout elapses (whichever comes first).\r
+\r
+  @param[in] CpuMpData        Pointer to CPU MP Data.\r
+  @param[in] FinishedApLimit  The number of finished APs to wait for.\r
+  @param[in] TimeLimit        The number of microseconds to wait for.\r
+**/\r
+VOID\r
+TimedWaitForApFinish (\r
+  IN CPU_MP_DATA               *CpuMpData,\r
+  IN UINT32                    FinishedApLimit,\r
+  IN UINT32                    TimeLimit\r
+  );\r
+\r
 /**\r
   This function will be called by BSP to wakeup AP.\r
 \r
@@ -696,6 +726,7 @@ WakeUpAP (
   UINTN                            Index;\r
   CPU_AP_DATA                      *CpuData;\r
   BOOLEAN                          ResetVectorRequired;\r
+  CPU_INFO_IN_HOB                  *CpuInfoInHob;\r
 \r
   CpuMpData->FinishedCount = 0;\r
   ResetVectorRequired = FALSE;\r
@@ -737,7 +768,11 @@ WakeUpAP (
       //\r
       // Wait for all potential APs waken up in one specified period\r
       //\r
-      MicroSecondDelay (PcdGet32(PcdCpuApInitTimeOutInMicroSeconds));\r
+      TimedWaitForApFinish (\r
+        CpuMpData,\r
+        PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,\r
+        PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)\r
+        );\r
     } else {\r
       //\r
       // Wait all APs waken up if this is not the 1st broadcast of SIPI\r
@@ -760,8 +795,9 @@ WakeUpAP (
     ASSERT (CpuMpData->InitFlag != ApInitConfig);\r
     *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
     if (ResetVectorRequired) {\r
+      CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
       SendInitSipiSipi (\r
-        CpuData->ApicId,\r
+        CpuInfoInHob[ProcessorNumber].ApicId,\r
         (UINT32) ExchangeInfo->BufferStart\r
         );\r
     }\r
@@ -882,6 +918,58 @@ CheckTimeout (
   return FALSE;\r
 }\r
 \r
+/**\r
+  Helper function that waits until the finished AP count reaches the specified\r
+  limit, or the specified timeout elapses (whichever comes first).\r
+\r
+  @param[in] CpuMpData        Pointer to CPU MP Data.\r
+  @param[in] FinishedApLimit  The number of finished APs to wait for.\r
+  @param[in] TimeLimit        The number of microseconds to wait for.\r
+**/\r
+VOID\r
+TimedWaitForApFinish (\r
+  IN CPU_MP_DATA               *CpuMpData,\r
+  IN UINT32                    FinishedApLimit,\r
+  IN UINT32                    TimeLimit\r
+  )\r
+{\r
+  //\r
+  // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0\r
+  // "infinity", so check for (TimeLimit == 0) explicitly.\r
+  //\r
+  if (TimeLimit == 0) {\r
+    return;\r
+  }\r
+\r
+  CpuMpData->TotalTime = 0;\r
+  CpuMpData->ExpectedTime = CalculateTimeout (\r
+                              TimeLimit,\r
+                              &CpuMpData->CurrentTime\r
+                              );\r
+  while (CpuMpData->FinishedCount < FinishedApLimit &&\r
+         !CheckTimeout (\r
+            &CpuMpData->CurrentTime,\r
+            &CpuMpData->TotalTime,\r
+            CpuMpData->ExpectedTime\r
+            )) {\r
+    CpuPause ();\r
+  }\r
+\r
+  if (CpuMpData->FinishedCount >= FinishedApLimit) {\r
+    DEBUG ((\r
+      DEBUG_VERBOSE,\r
+      "%a: reached FinishedApLimit=%u in %Lu microseconds\n",\r
+      __FUNCTION__,\r
+      FinishedApLimit,\r
+      DivU64x64Remainder (\r
+        MultU64x32 (CpuMpData->TotalTime, 1000000),\r
+        GetPerformanceCounterProperties (NULL, NULL),\r
+        NULL\r
+        )\r
+      ));\r
+  }\r
+}\r
+\r
 /**\r
   Reset an AP to Idle state.\r
 \r
@@ -1188,7 +1276,7 @@ MpInitLibInitialize (
   //\r
   // Set BSP basic information\r
   //\r
-  InitializeApData (CpuMpData, 0, 0);\r
+  InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer);\r
   //\r
   // Save assembly code information\r
   //\r
@@ -1229,16 +1317,14 @@ MpInitLibInitialize (
     CpuMpData->CpuCount  = OldCpuMpData->CpuCount;\r
     CpuMpData->BspNumber = OldCpuMpData->BspNumber;\r
     CpuMpData->InitFlag  = ApInitReconfig;\r
-    CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) OldCpuMpData->CpuInfoInHob;\r
+    CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;\r
+    CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
     for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
       InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);\r
-      CpuMpData->CpuData[Index].ApicId        = CpuInfoInHob[Index].ApicId;\r
-      CpuMpData->CpuData[Index].InitialApicId = CpuInfoInHob[Index].InitialApicId;\r
-      if (CpuMpData->CpuData[Index].InitialApicId >= 255) {\r
+      if (CpuInfoInHob[Index].InitialApicId >= 255) {\r
         CpuMpData->X2ApicEnable = TRUE;\r
       }\r
-      CpuMpData->CpuData[Index].Health     = CpuInfoInHob[Index].Health;\r
-      CpuMpData->CpuData[Index].CpuHealthy = (CpuMpData->CpuData[Index].Health == 0)? TRUE:FALSE;\r
+      CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;\r
       CpuMpData->CpuData[Index].ApFunction = 0;\r
       CopyMem (\r
         &CpuMpData->CpuData[Index].VolatileRegisters,\r
@@ -1299,8 +1385,10 @@ MpInitLibGetProcessorInfo (
 {\r
   CPU_MP_DATA            *CpuMpData;\r
   UINTN                  CallerNumber;\r
+  CPU_INFO_IN_HOB        *CpuInfoInHob;\r
 \r
   CpuMpData = GetCpuMpData ();\r
+  CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
 \r
   //\r
   // Check whether caller processor is BSP\r
@@ -1318,7 +1406,7 @@ MpInitLibGetProcessorInfo (
     return EFI_NOT_FOUND;\r
   }\r
 \r
-  ProcessorInfoBuffer->ProcessorId = (UINT64) CpuMpData->CpuData[ProcessorNumber].ApicId;\r
+  ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;\r
   ProcessorInfoBuffer->StatusFlag  = 0;\r
   if (ProcessorNumber == CpuMpData->BspNumber) {\r
     ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
@@ -1336,14 +1424,14 @@ MpInitLibGetProcessorInfo (
   // Get processor location information\r
   //\r
   GetProcessorLocationByApicId (\r
-    CpuMpData->CpuData[ProcessorNumber].ApicId,\r
+    CpuInfoInHob[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
+    HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -1410,6 +1498,7 @@ SwitchBSPWorker (
   CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
   CpuMpData->APInfo.State  = CPU_SWITCH_STATE_IDLE;\r
   CpuMpData->SwitchBspFlag = TRUE;\r
+  CpuMpData->NewBspNumber  = ProcessorNumber;\r
 \r
   //\r
   // Clear the BSP bit of MSR_IA32_APIC_BASE\r
@@ -1622,7 +1711,7 @@ MpInitLibGetNumberOfProcessors (
                                       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
+  @param[in]  TimeoutInMicroseconds   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
@@ -1776,7 +1865,7 @@ StartupAllAPsWorker (
   @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
+  @param[in]  TimeoutInMicroseconds   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