]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/MpInitLib: Collect processors' CPUID & Platform ID info
[mirror_edk2.git] / UefiCpuPkg / Library / MpInitLib / MpLib.c
index d6f84c6f45c02c551d63aa30822f4a34a4d45bf3..d5077e080e495f0864682ae42e152396c9302a1e 100644 (file)
@@ -458,12 +458,13 @@ CollectProcessorCount (
   )\r
 {\r
   UINTN                  Index;\r
+  CPU_INFO_IN_HOB        *CpuInfoInHob;\r
+  BOOLEAN                X2Apic;\r
 \r
   //\r
   // Send 1st broadcast IPI to APs to wakeup APs\r
   //\r
-  CpuMpData->InitFlag     = ApInitConfig;\r
-  CpuMpData->X2ApicEnable = FALSE;\r
+  CpuMpData->InitFlag = ApInitConfig;\r
   WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL, TRUE);\r
   CpuMpData->InitFlag = ApInitDone;\r
   ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
@@ -474,13 +475,29 @@ CollectProcessorCount (
     CpuPause ();\r
   }\r
 \r
+\r
+  //\r
+  // Enable x2APIC mode if\r
+  //  1. Number of CPU is greater than 255; or\r
+  //  2. There are any logical processors reporting an Initial APIC ID of 255 or greater.\r
+  //\r
+  X2Apic = FALSE;\r
   if (CpuMpData->CpuCount > 255) {\r
     //\r
     // If there are more than 255 processor found, force to enable X2APIC\r
     //\r
-    CpuMpData->X2ApicEnable = TRUE;\r
+    X2Apic = TRUE;\r
+  } else {\r
+    CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+    for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+      if (CpuInfoInHob[Index].InitialApicId >= 0xFF) {\r
+        X2Apic = TRUE;\r
+        break;\r
+      }\r
+    }\r
   }\r
-  if (CpuMpData->X2ApicEnable) {\r
+\r
+  if (X2Apic) {\r
     DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n"));\r
     //\r
     // Wakeup all APs to enable x2APIC mode\r
@@ -531,7 +548,8 @@ InitializeApData (
   IN     UINT64           ApTopOfStack\r
   )\r
 {\r
-  CPU_INFO_IN_HOB          *CpuInfoInHob;\r
+  CPU_INFO_IN_HOB                  *CpuInfoInHob;\r
+  MSR_IA32_PLATFORM_ID_REGISTER    PlatformIdMsr;\r
 \r
   CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
   CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
@@ -541,15 +559,17 @@ InitializeApData (
 \r
   CpuMpData->CpuData[ProcessorNumber].Waiting    = FALSE;\r
   CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;\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
-    //\r
-    AcquireSpinLock(&CpuMpData->MpLock);\r
-    CpuMpData->X2ApicEnable = TRUE;\r
-    ReleaseSpinLock(&CpuMpData->MpLock);\r
-  }\r
+\r
+  PlatformIdMsr.Uint64 = AsmReadMsr64 (MSR_IA32_PLATFORM_ID);\r
+  CpuMpData->CpuData[ProcessorNumber].PlatformId = (UINT8) PlatformIdMsr.Bits.PlatformId;\r
+\r
+  AsmCpuid (\r
+    CPUID_VERSION_INFO,\r
+    &CpuMpData->CpuData[ProcessorNumber].ProcessorSignature,\r
+    NULL,\r
+    NULL,\r
+    NULL\r
+    );\r
 \r
   InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);\r
   SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
@@ -1044,24 +1064,67 @@ WakeUpAP (
       SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);\r
     }\r
     if (CpuMpData->InitFlag == ApInitConfig) {\r
-      //\r
-      // Here support two methods to collect AP count through adjust\r
-      // PcdCpuApInitTimeOutInMicroSeconds values.\r
-      //\r
-      // one way is set a value to just let the first AP to start the\r
-      // initialization, then through the later while loop to wait all Aps\r
-      // finsh the initialization.\r
-      // The other way is set a value to let all APs finished the initialzation.\r
-      // In this case, the later while loop is useless.\r
-      //\r
-      TimedWaitForApFinish (\r
-        CpuMpData,\r
-        PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,\r
-        PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)\r
-        );\r
+      if (PcdGet32 (PcdCpuBootLogicalProcessorNumber) > 0) {\r
+        //\r
+        // The AP enumeration algorithm below is suitable only when the\r
+        // platform can tell us the *exact* boot CPU count in advance.\r
+        //\r
+        // The wait below finishes only when the detected AP count reaches\r
+        // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that\r
+        // takes. If at least one AP fails to check in (meaning a platform\r
+        // hardware bug), the detection hangs forever, by design. If the actual\r
+        // boot CPU count in the system is higher than\r
+        // PcdCpuBootLogicalProcessorNumber (meaning a platform\r
+        // misconfiguration), then some APs may complete initialization after\r
+        // the wait finishes, and cause undefined behavior.\r
+        //\r
+        TimedWaitForApFinish (\r
+          CpuMpData,\r
+          PcdGet32 (PcdCpuBootLogicalProcessorNumber) - 1,\r
+          MAX_UINT32 // approx. 71 minutes\r
+          );\r
+      } else {\r
+        //\r
+        // The AP enumeration algorithm below is suitable for two use cases.\r
+        //\r
+        // (1) The check-in time for an individual AP is bounded, and APs run\r
+        //     through their initialization routines strongly concurrently. In\r
+        //     particular, the number of concurrently running APs\r
+        //     ("NumApsExecuting") is never expected to fall to zero\r
+        //     *temporarily* -- it is expected to fall to zero only when all\r
+        //     APs have checked-in.\r
+        //\r
+        //     In this case, the platform is supposed to set\r
+        //     PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long\r
+        //     enough for one AP to start initialization). The timeout will be\r
+        //     reached soon, and remaining APs are collected by watching\r
+        //     NumApsExecuting fall to zero. If NumApsExecuting falls to zero\r
+        //     mid-process, while some APs have not completed initialization,\r
+        //     the behavior is undefined.\r
+        //\r
+        // (2) The check-in time for an individual AP is unbounded, and/or APs\r
+        //     may complete their initializations widely spread out. In\r
+        //     particular, some APs may finish initialization before some APs\r
+        //     even start.\r
+        //\r
+        //     In this case, the platform is supposed to set\r
+        //     PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP\r
+        //     enumeration will always take that long (except when the boot CPU\r
+        //     count happens to be maximal, that is,\r
+        //     PcdCpuMaxLogicalProcessorNumber). All APs are expected to\r
+        //     check-in before the timeout, and NumApsExecuting is assumed zero\r
+        //     at timeout. APs that miss the time-out may cause undefined\r
+        //     behavior.\r
+        //\r
+        TimedWaitForApFinish (\r
+          CpuMpData,\r
+          PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,\r
+          PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)\r
+          );\r
 \r
-      while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) {\r
-        CpuPause();\r
+        while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) {\r
+          CpuPause();\r
+        }\r
       }\r
     } else {\r
       //\r
@@ -1730,9 +1793,6 @@ MpInitLibInitialize (
     CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
     for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
       InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);\r
-      if (CpuInfoInHob[Index].InitialApicId >= 255 || Index > 254) {\r
-        CpuMpData->X2ApicEnable = TRUE;\r
-      }\r
       CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;\r
       CpuMpData->CpuData[Index].ApFunction = 0;\r
       CopyMem (&CpuMpData->CpuData[Index].VolatileRegisters, &VolatileRegisters, sizeof (CPU_VOLATILE_REGISTERS));\r