]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UefiCpuPkg/MpInitLib: Produce EDKII microcode patch HOB
authorHao A Wu <hao.a.wu@intel.com>
Mon, 23 Dec 2019 06:32:49 +0000 (14:32 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Thu, 2 Jan 2020 03:10:36 +0000 (03:10 +0000)
REF:https://bugzilla.tianocore.org/show_bug.cgi?id=2430

This commit will update the MpInitLib to:

A. Collect the base address and size information after microcode patches
   being loaded into memory;
B. Collect the detected microcode patch for each processor within system;
C. Based on the collected information, produce the EDKII microcode patch
   HOB.

Cc: Eric Dong <eric.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Laszlo Ersek <lersek@redhat.com>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Siyuan Fu <siyuan.fu@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: Hao A Wu <hao.a.wu@intel.com>
Reviewed-by: Eric Dong <eric.dong@intel.com>
Reviewed-by: Ray Ni <ray.ni@intel.com>
UefiCpuPkg/Library/MpInitLib/Microcode.c
UefiCpuPkg/Library/MpInitLib/MpLib.c
UefiCpuPkg/Library/MpInitLib/MpLib.h
UefiCpuPkg/Library/MpInitLib/PeiMpInitLib.inf
UefiCpuPkg/Library/MpInitLib/PeiMpLib.c

index 330fd99623347ef172542b9bceaa708cd29ef424..4162b4a8dcfe5d22cd2eed08b79fe420606abdb0 100644 (file)
@@ -65,13 +65,15 @@ GetCurrentMicrocodeSignature (
          It does not guarantee that the data has not been modified.\r
          CPU has its own mechanism to verify Microcode Binary part.\r
 \r
-  @param[in]  CpuMpData    The pointer to CPU MP Data structure.\r
-  @param[in]  IsBspCallIn  Indicate whether the caller is BSP or not.\r
+  @param[in]  CpuMpData        The pointer to CPU MP Data structure.\r
+  @param[in]  ProcessorNumber  The handle number of the processor. The range is\r
+                               from 0 to the total number of logical processors\r
+                               minus 1.\r
 **/\r
 VOID\r
 MicrocodeDetect (\r
   IN CPU_MP_DATA             *CpuMpData,\r
-  IN BOOLEAN                 IsBspCallIn\r
+  IN UINTN                   ProcessorNumber\r
   )\r
 {\r
   UINT32                                  ExtendedTableLength;\r
@@ -93,6 +95,7 @@ MicrocodeDetect (
   MSR_IA32_PLATFORM_ID_REGISTER           PlatformIdMsr;\r
   UINT32                                  ProcessorFlags;\r
   UINT32                                  ThreadId;\r
+  BOOLEAN                                 IsBspCallIn;\r
 \r
   //\r
   // set ProcessorFlags to suppress incorrect compiler/analyzer warnings\r
@@ -107,6 +110,7 @@ MicrocodeDetect (
   }\r
 \r
   CurrentRevision = GetCurrentMicrocodeSignature ();\r
+  IsBspCallIn     = (ProcessorNumber == (UINTN)CpuMpData->BspNumber) ? TRUE : FALSE;\r
   if (CurrentRevision != 0 && !IsBspCallIn) {\r
     //\r
     // Skip loading microcode if it has been loaded successfully\r
@@ -295,6 +299,16 @@ MicrocodeDetect (
   } while (((UINTN) MicrocodeEntryPoint < MicrocodeEnd));\r
 \r
 Done:\r
+  if (LatestRevision != 0) {\r
+    //\r
+    // Save the detected microcode patch entry address (including the\r
+    // microcode patch header) for each processor.\r
+    // It will be used when building the microcode patch cache HOB.\r
+    //\r
+    CpuMpData->CpuData[ProcessorNumber].MicrocodeEntryAddr =\r
+      (UINTN) MicrocodeData -  sizeof (CPU_MICROCODE_HEADER);\r
+  }\r
+\r
   if (LatestRevision > CurrentRevision) {\r
     //\r
     // BIOS only authenticate updates that contain a numerically larger revision\r
index c72bf3c9eece135f09227942e290f48ad5af172a..e611a8ca40637815054414ae98178203418d643f 100644 (file)
@@ -399,12 +399,16 @@ ApInitializeSync (
   )\r
 {\r
   CPU_MP_DATA  *CpuMpData;\r
+  UINTN        ProcessorNumber;\r
+  EFI_STATUS   Status;\r
 \r
   CpuMpData = (CPU_MP_DATA *) Buffer;\r
+  Status = GetProcessorNumber (CpuMpData, &ProcessorNumber);\r
+  ASSERT_EFI_ERROR (Status);\r
   //\r
   // Load microcode on AP\r
   //\r
-  MicrocodeDetect (CpuMpData, FALSE);\r
+  MicrocodeDetect (CpuMpData, ProcessorNumber);\r
   //\r
   // Sync BSP's MTRR table to AP\r
   //\r
@@ -1761,7 +1765,7 @@ MpInitLibInitialize (
   //\r
   // Detect and apply Microcode on BSP\r
   //\r
-  MicrocodeDetect (CpuMpData, TRUE);\r
+  MicrocodeDetect (CpuMpData, CpuMpData->BspNumber);\r
   //\r
   // Store BSP's MTRR setting\r
   //\r
index 56b0df664a7956d13e19e7ab4e154e88d26d9a22..885656900c5586287b292b347adf3d26819437c6 100644 (file)
@@ -138,6 +138,7 @@ typedef struct {
   EFI_EVENT                      WaitEvent;\r
   UINT32                         ProcessorSignature;\r
   UINT8                          PlatformId;\r
+  UINT64                         MicrocodeEntryAddr;\r
 } CPU_AP_DATA;\r
 \r
 //\r
@@ -580,13 +581,15 @@ CheckAndUpdateApsStatus (
 /**\r
   Detect whether specified processor can find matching microcode patch and load it.\r
 \r
-  @param[in]  CpuMpData    The pointer to CPU MP Data structure.\r
-  @param[in]  IsBspCallIn  Indicate whether the caller is BSP or not.\r
+  @param[in]  CpuMpData        The pointer to CPU MP Data structure.\r
+  @param[in]  ProcessorNumber  The handle number of the processor. The range is\r
+                               from 0 to the total number of logical processors\r
+                               minus 1.\r
 **/\r
 VOID\r
 MicrocodeDetect (\r
   IN CPU_MP_DATA             *CpuMpData,\r
-  IN BOOLEAN                 IsBspCallIn\r
+  IN UINTN                   ProcessorNumber\r
   );\r
 \r
 /**\r
@@ -619,5 +622,20 @@ EnableDebugAgent (
   VOID\r
   );\r
 \r
+/**\r
+  Find the current Processor number by APIC ID.\r
+\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
+**/\r
+EFI_STATUS\r
+GetProcessorNumber (\r
+  IN CPU_MP_DATA               *CpuMpData,\r
+  OUT UINTN                    *ProcessorNumber\r
+  );\r
+\r
 #endif\r
 \r
index 1538185ef99a52d123c03999c87d2d5a76d6e082..326703cc9ad336dea6dbdee9f127547195deb2b9 100644 (file)
@@ -63,3 +63,4 @@
 \r
 [Guids]\r
   gEdkiiS3SmmInitDoneGuid\r
+  gEdkiiMicrocodePatchHobGuid\r
index 3999603c3efc8117a2613bdf688f1a3fc8625f7a..06e3f5d0d3da53374a086fe2d2434808bddadd68 100644 (file)
@@ -9,6 +9,7 @@
 #include "MpLib.h"\r
 #include <Library/PeiServicesLib.h>\r
 #include <Guid/S3SmmInitDone.h>\r
+#include <Guid/MicrocodePatchHob.h>\r
 \r
 /**\r
   S3 SMM Init Done notification function.\r
@@ -290,6 +291,59 @@ CheckAndUpdateApsStatus (
 {\r
 }\r
 \r
+/**\r
+  Build the microcode patch HOB that contains the base address and size of the\r
+  microcode patch stored in the memory.\r
+\r
+  @param[in]  CpuMpData    Pointer to the CPU_MP_DATA structure.\r
+\r
+**/\r
+VOID\r
+BuildMicrocodeCacheHob (\r
+  IN CPU_MP_DATA    *CpuMpData\r
+  )\r
+{\r
+  EDKII_MICROCODE_PATCH_HOB    *MicrocodeHob;\r
+  UINTN                        HobDataLength;\r
+  UINT32                       Index;\r
+\r
+  HobDataLength = sizeof (EDKII_MICROCODE_PATCH_HOB) +\r
+                  sizeof (UINT64) * CpuMpData->CpuCount;\r
+\r
+  MicrocodeHob  = AllocatePool (HobDataLength);\r
+  if (MicrocodeHob == NULL) {\r
+    ASSERT (FALSE);\r
+    return;\r
+  }\r
+\r
+  //\r
+  // Store the information of the memory region that holds the microcode patches.\r
+  //\r
+  MicrocodeHob->MicrocodePatchAddress    = CpuMpData->MicrocodePatchAddress;\r
+  MicrocodeHob->MicrocodePatchRegionSize = CpuMpData->MicrocodePatchRegionSize;\r
+\r
+  //\r
+  // Store the detected microcode patch for each processor as well.\r
+  //\r
+  MicrocodeHob->ProcessorCount = CpuMpData->CpuCount;\r
+  for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+    if (CpuMpData->CpuData[Index].MicrocodeEntryAddr != 0) {\r
+      MicrocodeHob->ProcessorSpecificPatchOffset[Index] =\r
+        CpuMpData->CpuData[Index].MicrocodeEntryAddr - CpuMpData->MicrocodePatchAddress;\r
+    } else {\r
+      MicrocodeHob->ProcessorSpecificPatchOffset[Index] = MAX_UINT64;\r
+    }\r
+  }\r
+\r
+  BuildGuidDataHob (\r
+    &gEdkiiMicrocodePatchHobGuid,\r
+    MicrocodeHob,\r
+    HobDataLength\r
+    );\r
+\r
+  return;\r
+}\r
+\r
 /**\r
   Initialize global data for MP support.\r
 \r
@@ -302,6 +356,7 @@ InitMpGlobalData (
 {\r
   EFI_STATUS  Status;\r
 \r
+  BuildMicrocodeCacheHob (CpuMpData);\r
   SaveCpuMpData (CpuMpData);\r
 \r
   ///\r