]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/CpuDxe/CpuMp.c
MdeModulePkg BaseSerialPortLib16550: Fix typo in SerialPortWrite()
[mirror_edk2.git] / UefiCpuPkg / CpuDxe / CpuMp.c
index e5d2f5f7e28ece9b0796dd759830c1bd77c6dc43..04c2f1f0ac12ff3511ddfd444bf8000ee302bebc 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
@@ -312,6 +312,47 @@ CheckAndUpdateAllAPsToIdleState (
   }\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
@@ -1193,7 +1234,7 @@ ProcessorToIdleState (
   UINTN                 ProcessorNumber;\r
   CPU_DATA_BLOCK        *CpuData;\r
   EFI_AP_PROCEDURE      Procedure;\r
-  VOID                  *ProcedureArgument;\r
+  volatile VOID         *ProcedureArgument;\r
 \r
   AsmApDoneWithCommonStack ();\r
 \r
@@ -1231,7 +1272,7 @@ ProcessorToIdleState (
     if (Procedure != NULL) {\r
       SetApState (CpuData, CpuStateBusy);\r
 \r
-      Procedure (ProcedureArgument);\r
+      Procedure ((VOID*) ProcedureArgument);\r
 \r
       GetMpSpinLock (CpuData);\r
       CpuData->Procedure = NULL;\r
@@ -1520,6 +1561,89 @@ 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
@@ -1542,6 +1666,22 @@ ExitBootServicesCallback (
   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
@@ -1551,7 +1691,9 @@ InitializeMpSupport (
   VOID\r
   )\r
 {\r
-  EFI_STATUS Status;\r
+  EFI_STATUS     Status;\r
+  MTRR_SETTINGS  MtrrSettings;\r
+  UINTN          Timeout;\r
 \r
   gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
   if (gMaxLogicalProcessorNumber < 1) {\r
@@ -1559,35 +1701,40 @@ InitializeMpSupport (
     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
-  PrepareAPStartupCode ();\r
+    mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
+    ASSERT (mApStackStart != NULL);\r
 \r
-  StartApsStackless ();\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
     FreeApStartupCode ();\r
-    FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
-    return;\r
+    if (mCommonStack != NULL) {\r
+      FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
+    }\r
   }\r
 \r
   mMpSystemData.CpuDatas = ReallocatePool (\r
@@ -1595,8 +1742,44 @@ InitializeMpSupport (
                              sizeof (CPU_DATA_BLOCK) * mMpSystemData.NumberOfProcessors,\r
                              mMpSystemData.CpuDatas);\r
 \r
+  //\r
+  // Release all APs to complete initialization and enter idle loop\r
+  //\r
   mAPsAlreadyInitFinished = TRUE;\r
 \r
+  //\r
+  // Wait for all APs to enter idle loop.\r
+  //\r
+  Timeout = 0;\r
+  do {\r
+    if (CheckAllAPsSleeping ()) {\r
+      break;\r
+    }\r
+    gBS->Stall (gPollInterval);\r
+    Timeout += gPollInterval;\r
+  } while (Timeout <= PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds));\r
+  ASSERT (Timeout <= PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds));\r
+\r
+  //\r
+  // Update CPU healthy information from Guided HOB\r
+  //\r
+  CollectBistDataFromHob ();\r
+\r
+  //\r
+  // Synchronize MTRR settings to APs.\r
+  //\r
+  MtrrGetAllMtrrs (&MtrrSettings);\r
+  Status = mMpServicesTemplate.StartupAllAPs (\r
+                                 &mMpServicesTemplate, // This\r
+                                 SetMtrrsFromBuffer,   // Procedure\r
+                                 TRUE,                 // SingleThread\r
+                                 NULL,                 // WaitEvent\r
+                                 0,                    // TimeoutInMicrosecsond\r
+                                 &MtrrSettings,        // ProcedureArgument\r
+                                 NULL                  // FailedCpuList\r
+                                 );\r
+  ASSERT (Status == EFI_SUCCESS || Status == EFI_NOT_STARTED);\r
+\r
   Status = gBS->InstallMultipleProtocolInterfaces (\r
                   &mMpServiceHandle,\r
                   &gEfiMpServiceProtocolGuid,  &mMpServicesTemplate,\r
@@ -1604,10 +1787,12 @@ InitializeMpSupport (
                   );\r
   ASSERT_EFI_ERROR (Status);\r
 \r
-  if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
-    FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
-                                (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
-                                gApStackSize));\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