]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UefiCpuPkg/CpuMpPei/CpuMpPei.c
UefiCpuPkg: Update CPU MP drivers to support single CPU configuration
[mirror_edk2.git] / UefiCpuPkg / CpuMpPei / CpuMpPei.c
index bfcf8168298e656bdb2cb98ae77a8868be4f3b03..8e35f288fe3c64fd235a84658a851671f6317029 100644 (file)
-/** @file
-  CPU PEI Module installs CPU Multiple Processor PPI.
-
-  Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
-  This program and the accompanying materials
-  are licensed and made available under the terms and conditions of the BSD License
-  which accompanies this distribution.  The full text of the license may be found at
-  http://opensource.org/licenses/bsd-license.php
-
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-
-**/
-
-#include "CpuMpPei.h"
-
-//
-// Global Descriptor Table (GDT)
-//
-GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT mGdtEntries[] = {
-/* selector { Global Segment Descriptor                              } */
-/* 0x00 */  {{0,      0,  0,  0,    0,  0,  0,  0,    0,  0, 0,  0,  0}}, //null descriptor
-/* 0x08 */  {{0xffff, 0,  0,  0x2,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //linear data segment descriptor
-/* 0x10 */  {{0xffff, 0,  0,  0xf,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //linear code segment descriptor
-/* 0x18 */  {{0xffff, 0,  0,  0x3,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //system data segment descriptor
-/* 0x20 */  {{0xffff, 0,  0,  0xa,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //system code segment descriptor
-/* 0x28 */  {{0,      0,  0,  0,    0,  0,  0,  0,    0,  0, 0,  0,  0}}, //spare segment descriptor
-/* 0x30 */  {{0xffff, 0,  0,  0x2,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //system data segment descriptor
-/* 0x38 */  {{0xffff, 0,  0,  0xa,  1,  0,  1,  0xf,  0,  1, 0,  1,  0}}, //system code segment descriptor
-/* 0x40 */  {{0,      0,  0,  0,    0,  0,  0,  0,    0,  0, 0,  0,  0}}, //spare segment descriptor
-};
-
-//
-// IA32 Gdt register
-//
-GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR mGdt = {
-  sizeof (mGdtEntries) - 1,
-  (UINTN) mGdtEntries
-  };
-
-/**
-  Sort the APIC ID of all processors.
-
-  This function sorts the APIC ID of all processors so that processor number is
-  assigned in the ascending order of APIC ID which eases MP debugging.
-
-  @param PeiCpuMpData        Pointer to PEI CPU MP Data
-**/
-VOID
-SortApicId (
-  IN PEI_CPU_MP_DATA   *PeiCpuMpData
-  )
-{
-  UINTN             Index1;
-  UINTN             Index2;
-  UINTN             Index3;
-  UINT32            ApicId;
-  EFI_HEALTH_FLAGS  Health;
-  UINT32            ApCount;
-
-  ApCount = PeiCpuMpData->CpuCount - 1;
-
-  if (ApCount != 0) {
-    for (Index1 = 0; Index1 < ApCount; Index1++) {
-      Index3 = Index1;
-      //
-      // Sort key is the hardware default APIC ID
-      //
-      ApicId = PeiCpuMpData->CpuData[Index1].ApicId;
-      for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {
-        if (ApicId > PeiCpuMpData->CpuData[Index2].ApicId) {
-          Index3 = Index2;
-          ApicId = PeiCpuMpData->CpuData[Index2].ApicId;
-        }
-      }
-      if (Index3 != Index1) {
-        PeiCpuMpData->CpuData[Index3].ApicId = PeiCpuMpData->CpuData[Index1].ApicId;
-        PeiCpuMpData->CpuData[Index1].ApicId = ApicId;
-        Health = PeiCpuMpData->CpuData[Index3].Health;
-        PeiCpuMpData->CpuData[Index3].Health = PeiCpuMpData->CpuData[Index1].Health;
-        PeiCpuMpData->CpuData[Index1].Health = Health;
-      }
-    }
-
-    //
-    // Get the processor number for the BSP
-    //
-    ApicId = GetInitialApicId ();
-    for (Index1 = 0; Index1 < PeiCpuMpData->CpuCount; Index1++) {
-      if (PeiCpuMpData->CpuData[Index1].ApicId == ApicId) {
-        PeiCpuMpData->BspNumber = (UINT32) Index1;
-        break;
-      }
-    }
-  }
-}
-
-/**
-  Get CPU MP Data pointer from the Guided HOB.
-
-  @return  Pointer to Pointer to PEI CPU MP Data
-**/
-PEI_CPU_MP_DATA *
-GetMpHobData (
-  VOID
-  )
-{
-  EFI_HOB_GUID_TYPE       *GuidHob;
-  VOID                    *DataInHob;
-  PEI_CPU_MP_DATA         *CpuMpData;
-
-  CpuMpData = NULL;
-  GuidHob = GetFirstGuidHob (&gEfiCallerIdGuid);
-  if (GuidHob != NULL) {
-    DataInHob = GET_GUID_HOB_DATA (GuidHob);
-    CpuMpData = (PEI_CPU_MP_DATA *)(*(UINTN *)DataInHob);
-  }
-  ASSERT (CpuMpData != NULL);
-  return CpuMpData;
-}
-
-/**
-  This function will be called from AP reset code if BSP uses WakeUpAP.
-
-  @param ExchangeInfo     Pointer to the MP exchange info buffer
-  @param NumApsExecuting  Number of curret executing AP
-**/
-VOID
-EFIAPI
-ApCFunction (
-  IN MP_CPU_EXCHANGE_INFO      *ExchangeInfo,
-  IN UINTN                     NumApsExecuting
-  )
-{
-  PEI_CPU_MP_DATA            *PeiCpuMpData;
-  UINTN                      ProcessorNumber;
-  EFI_AP_PROCEDURE           Procedure;
-  UINTN                      BistData;
-
-  PeiCpuMpData = ExchangeInfo->PeiCpuMpData;
-  if (PeiCpuMpData->InitFlag) {
-    //
-    // This is first time AP wakeup, get BIST inforamtion from AP stack
-    //
-    BistData = *(UINTN *) (PeiCpuMpData->Buffer + NumApsExecuting * PeiCpuMpData->CpuApStackSize - sizeof (UINTN));
-    PeiCpuMpData->CpuData[NumApsExecuting].ApicId        = GetInitialApicId ();
-    PeiCpuMpData->CpuData[NumApsExecuting].Health.Uint32 = (UINT32) BistData;
-    //
-    // Sync BSP's Mtrr table to all wakeup APs and load microcode on APs.
-    //
-    MtrrSetAllMtrrs (&PeiCpuMpData->MtrrTable);
-    MicrocodeDetect ();
-  } else {
-    //
-    // Execute AP function if AP is not disabled
-    //
-    GetProcessorNumber (PeiCpuMpData, &ProcessorNumber);
-    if ((PeiCpuMpData->CpuData[ProcessorNumber].State != CpuStateDisabled) &&
-        (PeiCpuMpData->ApFunction != 0)) {
-      PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateBusy;
-      Procedure = (EFI_AP_PROCEDURE)(UINTN)PeiCpuMpData->ApFunction;
-      Procedure ((VOID *)(UINTN)PeiCpuMpData->ApFunctionArgument);
-      PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateIdle;
-    }
-  }
-
-  //
-  // AP finished executing C code
-  //
-  InterlockedIncrement ((UINT32 *)&PeiCpuMpData->FinishedCount);
-
-}
-
-/**
-  This function will be called by BSP to wakeup AP.
-
-  @param PeiCpuMpData       Pointer to PEI CPU MP Data
-  @param Broadcast          TRUE:  Send broadcast IPI to all APs
-                            FALSE: Send IPI to AP by ApicId
-  @param ApicId             Apic ID for the processor to be waked
-  @param Procedure          The function to be invoked by AP
-  @param ProcedureArgument  The argument to be passed into AP function
-**/
-VOID
-WakeUpAP (
-  IN PEI_CPU_MP_DATA           *PeiCpuMpData,
-  IN BOOLEAN                   Broadcast,
-  IN UINT32                    ApicId,
-  IN EFI_AP_PROCEDURE          Procedure,              OPTIONAL
-  IN VOID                      *ProcedureArgument      OPTIONAL
-  )
-{
-  volatile MP_CPU_EXCHANGE_INFO    *ExchangeInfo;
-
-  PeiCpuMpData->ApFunction         = (UINTN) Procedure;
-  PeiCpuMpData->ApFunctionArgument = (UINTN) ProcedureArgument;
-  PeiCpuMpData->FinishedCount      = 0;
-
-  ExchangeInfo                     = PeiCpuMpData->MpCpuExchangeInfo;
-  ExchangeInfo->Lock               = 0;
-  ExchangeInfo->StackStart         = PeiCpuMpData->Buffer;
-  ExchangeInfo->StackSize          = PeiCpuMpData->CpuApStackSize;
-  ExchangeInfo->BufferStart        = PeiCpuMpData->WakeupBuffer;
-  ExchangeInfo->PmodeOffset        = PeiCpuMpData->AddressMap.PModeEntryOffset;
-  ExchangeInfo->LmodeOffset        = PeiCpuMpData->AddressMap.LModeEntryOffset;
-  ExchangeInfo->Cr3                = AsmReadCr3 ();
-  ExchangeInfo->CFunction          = (UINTN) ApCFunction;
-  ExchangeInfo->NumApsExecuting    = 0;
-  ExchangeInfo->PeiCpuMpData       = PeiCpuMpData;
-
-  //
-  // Get the BSP's data of GDT and IDT
-  //
-  CopyMem ((VOID *)&ExchangeInfo->GdtrProfile, &mGdt, sizeof(mGdt));
-  AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);
-
-  if (Broadcast) {
-    SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);
-  } else {
-    SendInitSipiSipi (ApicId, (UINT32) ExchangeInfo->BufferStart);
-  }
-
-  return ;
-}
-
-/**
-  Get available system memory below 1MB by specified size.
-
-  @param  WakeupBufferSize   Wakeup buffer size required
-
-  @retval other   Return wakeup buffer address below 1MB.
-  @retval -1      Cannot find free memory below 1MB.
-**/
-UINTN
-GetWakeupBuffer (
-  IN UINTN                WakeupBufferSize
-  )
-{
-  EFI_PEI_HOB_POINTERS    Hob;
-  UINTN                   WakeupBufferStart;
-  UINTN                   WakeupBufferEnd;
-
-  //
-  // Get the HOB list for processing
-  //
-  Hob.Raw = GetHobList ();
-
-  //
-  // Collect memory ranges
-  //
-  while (!END_OF_HOB_LIST (Hob)) {
-    if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {
-      if ((Hob.ResourceDescriptor->PhysicalStart < BASE_1MB) &&
-          (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&
-          ((Hob.ResourceDescriptor->ResourceAttribute &
-            (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED |
-             EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED |
-             EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED
-             )) == 0)
-           ) {
-        //
-        // Need memory under 1MB to be collected here
-        //
-        WakeupBufferEnd = (UINTN) (Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength);
-        if (WakeupBufferEnd > BASE_1MB) {
-          //
-          // Wakeup buffer should be under 1MB
-          //
-          WakeupBufferEnd = BASE_1MB;
-        }
-        //
-        // Wakeup buffer should be aligned on 4KB
-        //
-        WakeupBufferStart = (WakeupBufferEnd - WakeupBufferSize) & ~(SIZE_4KB - 1);
-        if (WakeupBufferStart < Hob.ResourceDescriptor->PhysicalStart) {
-          continue;
-        }
-        //
-        // Create a memory allocation HOB.
-        //
-        BuildMemoryAllocationHob (
-          WakeupBufferStart,
-          WakeupBufferSize,
-          EfiBootServicesData
-          );
-        return WakeupBufferStart;
-      }
-    }
-    //
-    // Find the next HOB
-    //
-    Hob.Raw = GET_NEXT_HOB (Hob);
-  }
-
-  return (UINTN) -1;
-}
-
-/**
-  Get available system memory below 1MB by specified size.
-
-  @param PeiCpuMpData        Pointer to PEI CPU MP Data
-**/
-VOID
-BackupAndPrepareWakeupBuffer(
-  IN PEI_CPU_MP_DATA         *PeiCpuMpData
-  )
-{
-  CopyMem (
-    (VOID *) PeiCpuMpData->BackupBuffer,
-    (VOID *) PeiCpuMpData->WakeupBuffer,
-    PeiCpuMpData->BackupBufferSize
-    );
-  CopyMem (
-    (VOID *) PeiCpuMpData->WakeupBuffer,
-    (VOID *) PeiCpuMpData->AddressMap.RendezvousFunnelAddress,
-    PeiCpuMpData->AddressMap.RendezvousFunnelSize
-    );
-}
-/**
-  This function will get CPU count in the system.
-
-  @param PeiCpuMpData        Pointer to PEI CPU MP Data
-
-  @return  AP processor count
-**/
-UINT32
-CountProcessorNumber (
-  IN PEI_CPU_MP_DATA            *PeiCpuMpData
-  )
-{
-  //
-  // Load Microcode on BSP
-  //
-  MicrocodeDetect ();
-  //
-  // Store BSP's MTRR setting
-  //
-  MtrrGetAllMtrrs (&PeiCpuMpData->MtrrTable);
-  //
-  // Send broadcast IPI to APs to wakeup APs
-  //
-  PeiCpuMpData->InitFlag = 1;
-  WakeUpAP (PeiCpuMpData, TRUE, 0, NULL, NULL);
-  //
-  // Wait for AP task to complete and then exit.
-  //
-  MicroSecondDelay (PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds));
-  PeiCpuMpData->InitFlag  = 0;
-  PeiCpuMpData->CpuCount += (UINT32) PeiCpuMpData->MpCpuExchangeInfo->NumApsExecuting;
-  //
-  // Sort BSP/Aps by CPU APIC ID in ascending order
-  //
-  SortApicId (PeiCpuMpData);
-
-  DEBUG ((EFI_D_INFO, "CpuMpPei: Find %d processors in system.\n", PeiCpuMpData->CpuCount));
-  return PeiCpuMpData->CpuCount;
-}
-
-/**
-  Prepare for AP wakeup buffer and copy AP reset code into it.
-
-  Get wakeup buffer below 1MB. Allocate memory for CPU MP Data and APs Stack.
-
-  @return   Pointer to PEI CPU MP Data
-**/
-PEI_CPU_MP_DATA *
-PrepareAPStartupVector (
-  VOID
-  )
-{
-  EFI_STATUS                    Status;
-  UINT32                        MaxCpuCount;
-  PEI_CPU_MP_DATA               *PeiCpuMpData;
-  EFI_PHYSICAL_ADDRESS          Buffer;
-  UINTN                         BufferSize;
-  UINTN                         WakeupBuffer;
-  UINTN                         WakeupBufferSize;
-  MP_ASSEMBLY_ADDRESS_MAP       AddressMap;
-
-  AsmGetAddressMap (&AddressMap);
-  WakeupBufferSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);
-  WakeupBuffer     = GetWakeupBuffer ((WakeupBufferSize + SIZE_4KB - 1) & ~(SIZE_4KB - 1));
-  DEBUG ((EFI_D_INFO, "CpuMpPei: WakeupBuffer = 0x%x\n", WakeupBuffer));
-
-  //
-  // Allocate Pages for APs stack, CPU MP Data and backup buffer for wakeup buffer
-  //
-  MaxCpuCount = PcdGet32(PcdCpuMaxLogicalProcessorNumber);
-  BufferSize  = PcdGet32 (PcdCpuApStackSize) * MaxCpuCount + sizeof (PEI_CPU_MP_DATA)
-                  + WakeupBufferSize + sizeof (PEI_CPU_DATA) * MaxCpuCount;
-  Status = PeiServicesAllocatePages (
-             EfiBootServicesData,
-             EFI_SIZE_TO_PAGES (BufferSize),
-             &Buffer
-             );
-  ASSERT_EFI_ERROR (Status);
-
-  PeiCpuMpData = (PEI_CPU_MP_DATA *) (UINTN) (Buffer + PcdGet32 (PcdCpuApStackSize) * MaxCpuCount);
-  PeiCpuMpData->Buffer            = (UINTN) Buffer;
-  PeiCpuMpData->CpuApStackSize    = PcdGet32 (PcdCpuApStackSize);
-  PeiCpuMpData->WakeupBuffer      = WakeupBuffer;
-  PeiCpuMpData->BackupBuffer      = (UINTN)PeiCpuMpData + sizeof (PEI_CPU_MP_DATA);
-  PeiCpuMpData->BackupBufferSize  = WakeupBufferSize;
-  PeiCpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN) (WakeupBuffer + AddressMap.RendezvousFunnelSize);
-
-  PeiCpuMpData->CpuCount                 = 1;
-  PeiCpuMpData->BspNumber                = 0;
-  PeiCpuMpData->CpuData                  = (PEI_CPU_DATA *) (PeiCpuMpData->MpCpuExchangeInfo + 1);
-  PeiCpuMpData->CpuData[0].ApicId        = GetInitialApicId ();
-  PeiCpuMpData->CpuData[0].Health.Uint32 = 0;
-  CopyMem (&PeiCpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));
-
-  //
-  // Backup original data and copy AP reset code in it
-  //
-  BackupAndPrepareWakeupBuffer(PeiCpuMpData);
-
-  return PeiCpuMpData;
-}
-/**
-  The Entry point of the MP CPU PEIM.
-
-  This function will wakeup APs and collect CPU AP count and install the
-  Mp Service Ppi.
-
-  @param  FileHandle    Handle of the file being invoked.
-  @param  PeiServices   Describes the list of possible PEI Services.
-
-  @retval EFI_SUCCESS   MpServicePpi is installed successfully.
-
-**/
-EFI_STATUS
-EFIAPI
-CpuMpPeimInit (
-  IN       EFI_PEI_FILE_HANDLE  FileHandle,
-  IN CONST EFI_PEI_SERVICES     **PeiServices
-  )
-{
-
-  PEI_CPU_MP_DATA      *PeiCpuMpData;
-  UINT32               ProcessorCount;
-
-  //
-  // Load new GDT table on BSP
-  //
-  AsmInitializeGdt (&mGdt);
-  //
-  // Get wakeup buffer and copy AP reset code in it
-  //
-  PeiCpuMpData = PrepareAPStartupVector ();
-  //
-  // Count processor number and collect processor information
-  //
-  ProcessorCount = CountProcessorNumber (PeiCpuMpData);
-  //
-  // Build location of PEI CPU MP DATA buffer in HOB
-  //
-  BuildGuidDataHob (
-    &gEfiCallerIdGuid,
-    (VOID *)&PeiCpuMpData,
-    sizeof(UINT64)
-    );
-  //
-  // Update and publish CPU BIST information
-  //
-  CollectBistDataFromPpi (PeiServices, PeiCpuMpData);
-
-  return EFI_SUCCESS;
-}
+/** @file\r
+  CPU PEI Module installs CPU Multiple Processor PPI.\r
+\r
+  Copyright (c) 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
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "CpuMpPei.h"\r
+\r
+//\r
+// Global Descriptor Table (GDT)\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED IA32_GDT mGdtEntries[] = {\r
+/* selector { Global Segment Descriptor                              } */\r
+/* 0x00 */  {{0,      0,  0,  0,    0,  0,  0,  0,    0,  0, 0,  0,  0}}, //null descriptor\r
+/* 0x08 */  {{0xffff, 0,  0,  0x2,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //linear data segment descriptor\r
+/* 0x10 */  {{0xffff, 0,  0,  0xf,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //linear code segment descriptor\r
+/* 0x18 */  {{0xffff, 0,  0,  0x3,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //system data segment descriptor\r
+/* 0x20 */  {{0xffff, 0,  0,  0xa,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //system code segment descriptor\r
+/* 0x28 */  {{0,      0,  0,  0,    0,  0,  0,  0,    0,  0, 0,  0,  0}}, //spare segment descriptor\r
+/* 0x30 */  {{0xffff, 0,  0,  0x2,  1,  0,  1,  0xf,  0,  0, 1,  1,  0}}, //system data segment descriptor\r
+/* 0x38 */  {{0xffff, 0,  0,  0xa,  1,  0,  1,  0xf,  0,  1, 0,  1,  0}}, //system code segment descriptor\r
+/* 0x40 */  {{0,      0,  0,  0,    0,  0,  0,  0,    0,  0, 0,  0,  0}}, //spare segment descriptor\r
+};\r
+\r
+//\r
+// IA32 Gdt register\r
+//\r
+GLOBAL_REMOVE_IF_UNREFERENCED IA32_DESCRIPTOR mGdt = {\r
+  sizeof (mGdtEntries) - 1,\r
+  (UINTN) mGdtEntries\r
+  };\r
+\r
+GLOBAL_REMOVE_IF_UNREFERENCED EFI_PEI_NOTIFY_DESCRIPTOR mNotifyList = {\r
+  (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+  &gEfiEndOfPeiSignalPpiGuid,\r
+  CpuMpEndOfPeiCallback\r
+};\r
+\r
+/**\r
+  Sort the APIC ID of all processors.\r
+\r
+  This function sorts the APIC ID of all processors so that processor number is\r
+  assigned in the ascending order of APIC ID which eases MP debugging.\r
+\r
+  @param PeiCpuMpData        Pointer to PEI CPU MP Data\r
+**/\r
+VOID\r
+SortApicId (\r
+  IN PEI_CPU_MP_DATA   *PeiCpuMpData\r
+  )\r
+{\r
+  UINTN             Index1;\r
+  UINTN             Index2;\r
+  UINTN             Index3;\r
+  UINT32            ApicId;\r
+  EFI_HEALTH_FLAGS  Health;\r
+  UINT32            ApCount;\r
+\r
+  ApCount = PeiCpuMpData->CpuCount - 1;\r
+\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 = PeiCpuMpData->CpuData[Index1].ApicId;\r
+      for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {\r
+        if (ApicId > PeiCpuMpData->CpuData[Index2].ApicId) {\r
+          Index3 = Index2;\r
+          ApicId = PeiCpuMpData->CpuData[Index2].ApicId;\r
+        }\r
+      }\r
+      if (Index3 != Index1) {\r
+        PeiCpuMpData->CpuData[Index3].ApicId = PeiCpuMpData->CpuData[Index1].ApicId;\r
+        PeiCpuMpData->CpuData[Index1].ApicId = ApicId;\r
+        Health = PeiCpuMpData->CpuData[Index3].Health;\r
+        PeiCpuMpData->CpuData[Index3].Health = PeiCpuMpData->CpuData[Index1].Health;\r
+        PeiCpuMpData->CpuData[Index1].Health = Health;\r
+      }\r
+    }\r
+\r
+    //\r
+    // Get the processor number for the BSP\r
+    //\r
+    ApicId = GetInitialApicId ();\r
+    for (Index1 = 0; Index1 < PeiCpuMpData->CpuCount; Index1++) {\r
+      if (PeiCpuMpData->CpuData[Index1].ApicId == ApicId) {\r
+        PeiCpuMpData->BspNumber = (UINT32) Index1;\r
+        break;\r
+      }\r
+    }\r
+  }\r
+}\r
+\r
+/**\r
+  Get CPU MP Data pointer from the Guided HOB.\r
+\r
+  @return  Pointer to Pointer to PEI CPU MP Data\r
+**/\r
+PEI_CPU_MP_DATA *\r
+GetMpHobData (\r
+  VOID\r
+  )\r
+{\r
+  EFI_HOB_GUID_TYPE       *GuidHob;\r
+  VOID                    *DataInHob;\r
+  PEI_CPU_MP_DATA         *CpuMpData;\r
+\r
+  CpuMpData = NULL;\r
+  GuidHob = GetFirstGuidHob (&gEfiCallerIdGuid);\r
+  if (GuidHob != NULL) {\r
+    DataInHob = GET_GUID_HOB_DATA (GuidHob);\r
+    CpuMpData = (PEI_CPU_MP_DATA *)(*(UINTN *)DataInHob);\r
+  }\r
+  ASSERT (CpuMpData != NULL);\r
+  return CpuMpData;\r
+}\r
+\r
+/**\r
+  This function will be called from AP reset code if BSP uses WakeUpAP.\r
+\r
+  @param ExchangeInfo     Pointer to the MP exchange info buffer\r
+  @param NumApsExecuting  Number of curret executing AP\r
+**/\r
+VOID\r
+EFIAPI\r
+ApCFunction (\r
+  IN MP_CPU_EXCHANGE_INFO      *ExchangeInfo,\r
+  IN UINTN                     NumApsExecuting\r
+  )\r
+{\r
+  PEI_CPU_MP_DATA            *PeiCpuMpData;\r
+  UINTN                      ProcessorNumber;\r
+  EFI_AP_PROCEDURE           Procedure;\r
+  UINTN                      BistData;\r
+\r
+  PeiCpuMpData = ExchangeInfo->PeiCpuMpData;\r
+  if (PeiCpuMpData->InitFlag) {\r
+    //\r
+    // This is first time AP wakeup, get BIST inforamtion from AP stack\r
+    //\r
+    BistData = *(UINTN *) (PeiCpuMpData->Buffer + NumApsExecuting * PeiCpuMpData->CpuApStackSize - sizeof (UINTN));\r
+    PeiCpuMpData->CpuData[NumApsExecuting].ApicId        = GetInitialApicId ();\r
+    PeiCpuMpData->CpuData[NumApsExecuting].Health.Uint32 = (UINT32) BistData;\r
+    //\r
+    // Sync BSP's Mtrr table to all wakeup APs and load microcode on APs.\r
+    //\r
+    MtrrSetAllMtrrs (&PeiCpuMpData->MtrrTable);\r
+    MicrocodeDetect ();\r
+  } else {\r
+    //\r
+    // Execute AP function if AP is not disabled\r
+    //\r
+    GetProcessorNumber (PeiCpuMpData, &ProcessorNumber);\r
+    if ((PeiCpuMpData->CpuData[ProcessorNumber].State != CpuStateDisabled) &&\r
+        (PeiCpuMpData->ApFunction != 0)) {\r
+      PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateBusy;\r
+      Procedure = (EFI_AP_PROCEDURE)(UINTN)PeiCpuMpData->ApFunction;\r
+      Procedure ((VOID *)(UINTN)PeiCpuMpData->ApFunctionArgument);\r
+      PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateIdle;\r
+    }\r
+  }\r
+\r
+  //\r
+  // AP finished executing C code\r
+  //\r
+  InterlockedIncrement ((UINT32 *)&PeiCpuMpData->FinishedCount);\r
+\r
+  AsmCliHltLoop ();\r
+}\r
+\r
+/**\r
+  This function will be called by BSP to wakeup AP.\r
+\r
+  @param PeiCpuMpData       Pointer to PEI CPU MP Data\r
+  @param Broadcast          TRUE:  Send broadcast IPI to all APs\r
+                            FALSE: Send IPI to AP by ApicId\r
+  @param ApicId             Apic ID for the processor to be waked\r
+  @param Procedure          The function to be invoked by AP\r
+  @param ProcedureArgument  The argument to be passed into AP function\r
+**/\r
+VOID\r
+WakeUpAP (\r
+  IN PEI_CPU_MP_DATA           *PeiCpuMpData,\r
+  IN BOOLEAN                   Broadcast,\r
+  IN UINT32                    ApicId,\r
+  IN EFI_AP_PROCEDURE          Procedure,              OPTIONAL\r
+  IN VOID                      *ProcedureArgument      OPTIONAL\r
+  )\r
+{\r
+  volatile MP_CPU_EXCHANGE_INFO    *ExchangeInfo;\r
+\r
+  PeiCpuMpData->ApFunction         = (UINTN) Procedure;\r
+  PeiCpuMpData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
+  PeiCpuMpData->FinishedCount      = 0;\r
+\r
+  ExchangeInfo                     = PeiCpuMpData->MpCpuExchangeInfo;\r
+  ExchangeInfo->Lock               = 0;\r
+  ExchangeInfo->StackStart         = PeiCpuMpData->Buffer;\r
+  ExchangeInfo->StackSize          = PeiCpuMpData->CpuApStackSize;\r
+  ExchangeInfo->BufferStart        = PeiCpuMpData->WakeupBuffer;\r
+  ExchangeInfo->PmodeOffset        = PeiCpuMpData->AddressMap.PModeEntryOffset;\r
+  ExchangeInfo->LmodeOffset        = PeiCpuMpData->AddressMap.LModeEntryOffset;\r
+  ExchangeInfo->Cr3                = AsmReadCr3 ();\r
+  ExchangeInfo->CFunction          = (UINTN) ApCFunction;\r
+  ExchangeInfo->NumApsExecuting    = 0;\r
+  ExchangeInfo->PeiCpuMpData       = PeiCpuMpData;\r
+\r
+  //\r
+  // Get the BSP's data of GDT and IDT\r
+  //\r
+  CopyMem ((VOID *)&ExchangeInfo->GdtrProfile, &mGdt, sizeof(mGdt));\r
+  AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);\r
+\r
+  if (Broadcast) {\r
+    SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);\r
+  } else {\r
+    SendInitSipiSipi (ApicId, (UINT32) ExchangeInfo->BufferStart);\r
+  }\r
+\r
+  return ;\r
+}\r
+\r
+/**\r
+  Get available system memory below 1MB by specified size.\r
+\r
+  @param  WakeupBufferSize   Wakeup buffer size required\r
+\r
+  @retval other   Return wakeup buffer address below 1MB.\r
+  @retval -1      Cannot find free memory below 1MB.\r
+**/\r
+UINTN\r
+GetWakeupBuffer (\r
+  IN UINTN                WakeupBufferSize\r
+  )\r
+{\r
+  EFI_PEI_HOB_POINTERS    Hob;\r
+  UINTN                   WakeupBufferStart;\r
+  UINTN                   WakeupBufferEnd;\r
+\r
+  //\r
+  // Get the HOB list for processing\r
+  //\r
+  Hob.Raw = GetHobList ();\r
+\r
+  //\r
+  // Collect memory ranges\r
+  //\r
+  while (!END_OF_HOB_LIST (Hob)) {\r
+    if (Hob.Header->HobType == EFI_HOB_TYPE_RESOURCE_DESCRIPTOR) {\r
+      if ((Hob.ResourceDescriptor->PhysicalStart < BASE_1MB) &&\r
+          (Hob.ResourceDescriptor->ResourceType == EFI_RESOURCE_SYSTEM_MEMORY) &&\r
+          ((Hob.ResourceDescriptor->ResourceAttribute &\r
+            (EFI_RESOURCE_ATTRIBUTE_READ_PROTECTED |\r
+             EFI_RESOURCE_ATTRIBUTE_WRITE_PROTECTED |\r
+             EFI_RESOURCE_ATTRIBUTE_EXECUTION_PROTECTED\r
+             )) == 0)\r
+           ) {\r
+        //\r
+        // Need memory under 1MB to be collected here\r
+        //\r
+        WakeupBufferEnd = (UINTN) (Hob.ResourceDescriptor->PhysicalStart + Hob.ResourceDescriptor->ResourceLength);\r
+        if (WakeupBufferEnd > BASE_1MB) {\r
+          //\r
+          // Wakeup buffer should be under 1MB\r
+          //\r
+          WakeupBufferEnd = BASE_1MB;\r
+        }\r
+        //\r
+        // Wakeup buffer should be aligned on 4KB\r
+        //\r
+        WakeupBufferStart = (WakeupBufferEnd - WakeupBufferSize) & ~(SIZE_4KB - 1);\r
+        if (WakeupBufferStart < Hob.ResourceDescriptor->PhysicalStart) {\r
+          continue;\r
+        }\r
+        //\r
+        // Create a memory allocation HOB.\r
+        //\r
+        BuildMemoryAllocationHob (\r
+          WakeupBufferStart,\r
+          WakeupBufferSize,\r
+          EfiBootServicesData\r
+          );\r
+        return WakeupBufferStart;\r
+      }\r
+    }\r
+    //\r
+    // Find the next HOB\r
+    //\r
+    Hob.Raw = GET_NEXT_HOB (Hob);\r
+  }\r
+\r
+  return (UINTN) -1;\r
+}\r
+\r
+/**\r
+  Get available system memory below 1MB by specified size.\r
+\r
+  @param PeiCpuMpData        Pointer to PEI CPU MP Data\r
+**/\r
+VOID\r
+BackupAndPrepareWakeupBuffer(\r
+  IN PEI_CPU_MP_DATA         *PeiCpuMpData\r
+  )\r
+{\r
+  CopyMem (\r
+    (VOID *) PeiCpuMpData->BackupBuffer,\r
+    (VOID *) PeiCpuMpData->WakeupBuffer,\r
+    PeiCpuMpData->BackupBufferSize\r
+    );\r
+  CopyMem (\r
+    (VOID *) PeiCpuMpData->WakeupBuffer,\r
+    (VOID *) PeiCpuMpData->AddressMap.RendezvousFunnelAddress,\r
+    PeiCpuMpData->AddressMap.RendezvousFunnelSize\r
+    );\r
+}\r
+\r
+/**\r
+  Restore wakeup buffer data.\r
+\r
+  @param PeiCpuMpData        Pointer to PEI CPU MP Data\r
+**/\r
+VOID\r
+RestoreWakeupBuffer(\r
+  IN PEI_CPU_MP_DATA         *PeiCpuMpData\r
+  )\r
+{\r
+  CopyMem ((VOID *) PeiCpuMpData->WakeupBuffer, (VOID *) PeiCpuMpData->BackupBuffer, PeiCpuMpData->BackupBufferSize);\r
+}\r
+\r
+/**\r
+  This function will get CPU count in the system.\r
+\r
+  @param PeiCpuMpData        Pointer to PEI CPU MP Data\r
+\r
+  @return  AP processor count\r
+**/\r
+UINT32\r
+CountProcessorNumber (\r
+  IN PEI_CPU_MP_DATA            *PeiCpuMpData\r
+  )\r
+{\r
+  //\r
+  // Load Microcode on BSP\r
+  //\r
+  MicrocodeDetect ();\r
+  //\r
+  // Store BSP's MTRR setting\r
+  //\r
+  MtrrGetAllMtrrs (&PeiCpuMpData->MtrrTable);\r
+\r
+  //\r
+  // Only perform AP detection if PcdCpuMaxLogicalProcessorNumber is greater than 1\r
+  //\r
+  if (PcdGet32 (PcdCpuMaxLogicalProcessorNumber) > 1) {\r
+    //\r
+    // Send broadcast IPI to APs to wakeup APs\r
+    //\r
+    PeiCpuMpData->InitFlag = 1;\r
+    WakeUpAP (PeiCpuMpData, TRUE, 0, NULL, NULL);\r
+    //\r
+    // Wait for AP task to complete and then exit.\r
+    //\r
+    MicroSecondDelay (PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds));\r
+    PeiCpuMpData->InitFlag = 0;\r
+    PeiCpuMpData->CpuCount += (UINT32)PeiCpuMpData->MpCpuExchangeInfo->NumApsExecuting;\r
+    ASSERT (PeiCpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
+    //\r
+    // Sort BSP/Aps by CPU APIC ID in ascending order\r
+    //\r
+    SortApicId (PeiCpuMpData);\r
+  }\r
+\r
+  DEBUG ((EFI_D_INFO, "CpuMpPei: Find %d processors in system.\n", PeiCpuMpData->CpuCount));\r
+  return PeiCpuMpData->CpuCount;\r
+}\r
+\r
+/**\r
+  Prepare for AP wakeup buffer and copy AP reset code into it.\r
+\r
+  Get wakeup buffer below 1MB. Allocate memory for CPU MP Data and APs Stack.\r
+\r
+  @return   Pointer to PEI CPU MP Data\r
+**/\r
+PEI_CPU_MP_DATA *\r
+PrepareAPStartupVector (\r
+  VOID\r
+  )\r
+{\r
+  EFI_STATUS                    Status;\r
+  UINT32                        MaxCpuCount;\r
+  PEI_CPU_MP_DATA               *PeiCpuMpData;\r
+  EFI_PHYSICAL_ADDRESS          Buffer;\r
+  UINTN                         BufferSize;\r
+  UINTN                         WakeupBuffer;\r
+  UINTN                         WakeupBufferSize;\r
+  MP_ASSEMBLY_ADDRESS_MAP       AddressMap;\r
+\r
+  AsmGetAddressMap (&AddressMap);\r
+  WakeupBufferSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);\r
+  WakeupBuffer     = GetWakeupBuffer ((WakeupBufferSize + SIZE_4KB - 1) & ~(SIZE_4KB - 1));\r
+  ASSERT (WakeupBuffer != (UINTN) -1);\r
+  DEBUG ((EFI_D_INFO, "CpuMpPei: WakeupBuffer = 0x%x\n", WakeupBuffer));\r
+\r
+  //\r
+  // Allocate Pages for APs stack, CPU MP Data and backup buffer for wakeup buffer\r
+  //\r
+  MaxCpuCount = PcdGet32(PcdCpuMaxLogicalProcessorNumber);\r
+  BufferSize  = PcdGet32 (PcdCpuApStackSize) * MaxCpuCount + sizeof (PEI_CPU_MP_DATA)\r
+                  + WakeupBufferSize + sizeof (PEI_CPU_DATA) * MaxCpuCount;\r
+  Status = PeiServicesAllocatePages (\r
+             EfiBootServicesData,\r
+             EFI_SIZE_TO_PAGES (BufferSize),\r
+             &Buffer\r
+             );\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  PeiCpuMpData = (PEI_CPU_MP_DATA *) (UINTN) (Buffer + PcdGet32 (PcdCpuApStackSize) * MaxCpuCount);\r
+  PeiCpuMpData->Buffer            = (UINTN) Buffer;\r
+  PeiCpuMpData->CpuApStackSize    = PcdGet32 (PcdCpuApStackSize);\r
+  PeiCpuMpData->WakeupBuffer      = WakeupBuffer;\r
+  PeiCpuMpData->BackupBuffer      = (UINTN)PeiCpuMpData + sizeof (PEI_CPU_MP_DATA);\r
+  PeiCpuMpData->BackupBufferSize  = WakeupBufferSize;\r
+  PeiCpuMpData->MpCpuExchangeInfo = (MP_CPU_EXCHANGE_INFO *) (UINTN) (WakeupBuffer + AddressMap.RendezvousFunnelSize);\r
+\r
+  PeiCpuMpData->CpuCount                 = 1;\r
+  PeiCpuMpData->BspNumber                = 0;\r
+  PeiCpuMpData->CpuData                  = (PEI_CPU_DATA *) (PeiCpuMpData->BackupBuffer +\r
+                                                             PeiCpuMpData->BackupBufferSize);\r
+  PeiCpuMpData->CpuData[0].ApicId        = GetInitialApicId ();\r
+  PeiCpuMpData->CpuData[0].Health.Uint32 = 0;\r
+  PeiCpuMpData->EndOfPeiFlag             = FALSE;\r
+  CopyMem (&PeiCpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));\r
+\r
+  //\r
+  // Backup original data and copy AP reset code in it\r
+  //\r
+  BackupAndPrepareWakeupBuffer(PeiCpuMpData);\r
+\r
+  return PeiCpuMpData;\r
+}\r
+\r
+/**\r
+  Notify function on End Of Pei PPI.\r
+\r
+  On S3 boot, this function will restore wakeup buffer data.\r
+  On normal boot, this function will flag wakeup buffer to be un-used type.\r
+\r
+  @param  PeiServices        The pointer to the PEI Services Table.\r
+  @param  NotifyDescriptor   Address of the notification descriptor data structure.\r
+  @param  Ppi                Address of the PPI that was installed.\r
+\r
+  @retval EFI_SUCCESS        When everything is OK.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CpuMpEndOfPeiCallback (\r
+  IN      EFI_PEI_SERVICES        **PeiServices,\r
+  IN EFI_PEI_NOTIFY_DESCRIPTOR    *NotifyDescriptor,\r
+  IN VOID                         *Ppi\r
+  )\r
+{\r
+  EFI_STATUS                Status;\r
+  EFI_BOOT_MODE             BootMode;\r
+  PEI_CPU_MP_DATA           *PeiCpuMpData;\r
+  EFI_PEI_HOB_POINTERS      Hob;\r
+  EFI_HOB_MEMORY_ALLOCATION *MemoryHob;\r
+\r
+  DEBUG ((EFI_D_INFO, "CpuMpPei: CpuMpEndOfPeiCallback () invokded\n"));\r
+\r
+  Status = PeiServicesGetBootMode (&BootMode);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  PeiCpuMpData = GetMpHobData ();\r
+  ASSERT (PeiCpuMpData != NULL);\r
+\r
+  if (BootMode != BOOT_ON_S3_RESUME) {\r
+    //\r
+    // Get the HOB list for processing\r
+    //\r
+    Hob.Raw = GetHobList ();\r
+    //\r
+    // Collect memory ranges\r
+    //\r
+    while (!END_OF_HOB_LIST (Hob)) {\r
+      if (Hob.Header->HobType == EFI_HOB_TYPE_MEMORY_ALLOCATION) {\r
+        MemoryHob = Hob.MemoryAllocation;\r
+        if(MemoryHob->AllocDescriptor.MemoryBaseAddress == PeiCpuMpData->WakeupBuffer) {\r
+          //\r
+          // Flag this HOB type to un-used\r
+          //\r
+          GET_HOB_TYPE (Hob) = EFI_HOB_TYPE_UNUSED;\r
+          break;\r
+        }\r
+      }\r
+      Hob.Raw = GET_NEXT_HOB (Hob);\r
+    }\r
+  } else {\r
+    RestoreWakeupBuffer (PeiCpuMpData);\r
+    PeiCpuMpData->EndOfPeiFlag = TRUE;\r
+  }\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  The Entry point of the MP CPU PEIM.\r
+\r
+  This function will wakeup APs and collect CPU AP count and install the\r
+  Mp Service Ppi.\r
+\r
+  @param  FileHandle    Handle of the file being invoked.\r
+  @param  PeiServices   Describes the list of possible PEI Services.\r
+\r
+  @retval EFI_SUCCESS   MpServicePpi is installed successfully.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+CpuMpPeimInit (\r
+  IN       EFI_PEI_FILE_HANDLE  FileHandle,\r
+  IN CONST EFI_PEI_SERVICES     **PeiServices\r
+  )\r
+{\r
+  EFI_STATUS           Status;\r
+  PEI_CPU_MP_DATA      *PeiCpuMpData;\r
+  UINT32               ProcessorCount;\r
+\r
+  //\r
+  // Load new GDT table on BSP\r
+  //\r
+  AsmInitializeGdt (&mGdt);\r
+  //\r
+  // Get wakeup buffer and copy AP reset code in it\r
+  //\r
+  PeiCpuMpData = PrepareAPStartupVector ();\r
+  //\r
+  // Count processor number and collect processor information\r
+  //\r
+  ProcessorCount = CountProcessorNumber (PeiCpuMpData);\r
+  //\r
+  // Build location of PEI CPU MP DATA buffer in HOB\r
+  //\r
+  BuildGuidDataHob (\r
+    &gEfiCallerIdGuid,\r
+    (VOID *)&PeiCpuMpData,\r
+    sizeof(UINT64)\r
+    );\r
+  //\r
+  // Update and publish CPU BIST information\r
+  //\r
+  CollectBistDataFromPpi (PeiServices, PeiCpuMpData);\r
+  //\r
+  // register an event for EndOfPei\r
+  //\r
+  Status  = PeiServicesNotifyPpi (&mNotifyList);\r
+  ASSERT_EFI_ERROR (Status);\r
+  //\r
+  // Install CPU MP PPI\r
+  //\r
+  Status = PeiServicesInstallPpi(&mPeiCpuMpPpiDesc);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+  return Status;\r
+}\r