]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/MpInitLib: Set X2ApicEnable flag from BSP
authorRay Ni <ray.ni@intel.com>
Wed, 23 Oct 2019 06:23:38 +0000 (14:23 +0800)
committerRay Ni <ray.ni@intel.com>
Tue, 5 Nov 2019 02:03:43 +0000 (10:03 +0800)
Today's logic sets X2ApicEnable flag in each AP's initialization
path when InitFlag == ApInitConfig.
Since all CPUs update the same global data, a spin-lock is used
to avoid modifications from multiple CPUs happen at the same time.
The spin-lock causes two problems:
1. Potential performance downgrade.
2. Undefined behavior when improper timer lib is used.
   For example we saw certain platforms used AcpiTimerLib from
   PcAtChipsetPkg and that library depends on retrieving PeiServices
   from idtr. But in fact AP's (idtr - 4) doesn't point to
   PeiServices.

The patch simplifies the code to let BSP set the X2ApicEnable flag so
the spin-lock acquisition from AP is not needed any more.

Signed-off-by: Ray Ni <ray.ni@intel.com>
Reviewed-by: Eric Dong <eric.dong@intel.com>
Acked-by: Laszlo Ersek <lersek@redhat.com>
UefiCpuPkg/Library/MpInitLib/MpLib.c

index 622b70ca3c4ecd60c031c3afd2c86edb149356cd..8f62a8d965594065bd574486f02b1ede33810034 100644 (file)
@@ -458,6 +458,7 @@ CollectProcessorCount (
   )\r
 {\r
   UINTN                  Index;\r
+  CPU_INFO_IN_HOB        *CpuInfoInHob;\r
 \r
   //\r
   // Send 1st broadcast IPI to APs to wakeup APs\r
@@ -474,12 +475,27 @@ 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
   if (CpuMpData->CpuCount > 255) {\r
     //\r
     // If there are more than 255 processor found, force to enable X2APIC\r
     //\r
     CpuMpData->X2ApicEnable = 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
+        CpuMpData->X2ApicEnable = TRUE;\r
+        break;\r
+      }\r
+    }\r
   }\r
+\r
   if (CpuMpData->X2ApicEnable) {\r
     DEBUG ((DEBUG_INFO, "Force x2APIC mode!\n"));\r
     //\r
@@ -541,15 +557,6 @@ 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
   InitializeSpinLock(&CpuMpData->CpuData[ProcessorNumber].ApLock);\r
   SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r