return Enabled;\r
}\r
\r
-/**\r
- Get CPU Package/Core/Thread location information.\r
-\r
- @param[in] InitialApicId CPU APIC ID\r
- @param[out] Location Pointer to CPU location information\r
-**/\r
-VOID\r
-ExtractProcessorLocation (\r
- IN UINT32 InitialApicId,\r
- OUT EFI_CPU_PHYSICAL_LOCATION *Location\r
- )\r
-{\r
- BOOLEAN TopologyLeafSupported;\r
- UINTN ThreadBits;\r
- UINTN CoreBits;\r
- CPUID_VERSION_INFO_EBX VersionInfoEbx;\r
- CPUID_VERSION_INFO_EDX VersionInfoEdx;\r
- CPUID_CACHE_PARAMS_EAX CacheParamsEax;\r
- CPUID_EXTENDED_TOPOLOGY_EAX ExtendedTopologyEax;\r
- CPUID_EXTENDED_TOPOLOGY_EBX ExtendedTopologyEbx;\r
- CPUID_EXTENDED_TOPOLOGY_ECX ExtendedTopologyEcx;\r
- UINT32 MaxCpuIdIndex;\r
- UINT32 SubIndex;\r
- UINTN LevelType;\r
- UINT32 MaxLogicProcessorsPerPackage;\r
- UINT32 MaxCoresPerPackage;\r
-\r
- //\r
- // Check if the processor is capable of supporting more than one logical processor.\r
- //\r
- AsmCpuid (CPUID_VERSION_INFO, NULL, NULL, NULL, &VersionInfoEdx.Uint32);\r
- if (VersionInfoEdx.Bits.HTT == 0) {\r
- Location->Thread = 0;\r
- Location->Core = 0;\r
- Location->Package = 0;\r
- return;\r
- }\r
-\r
- ThreadBits = 0;\r
- CoreBits = 0;\r
-\r
- //\r
- // Assume three-level mapping of APIC ID: Package:Core:SMT.\r
- //\r
-\r
- TopologyLeafSupported = FALSE;\r
- //\r
- // Get the max index of basic CPUID\r
- //\r
- AsmCpuid (CPUID_SIGNATURE, &MaxCpuIdIndex, NULL, NULL, NULL);\r
-\r
- //\r
- // If the extended topology enumeration leaf is available, it\r
- // is the preferred mechanism for enumerating topology.\r
- //\r
- if (MaxCpuIdIndex >= CPUID_EXTENDED_TOPOLOGY) {\r
- AsmCpuidEx (\r
- CPUID_EXTENDED_TOPOLOGY,\r
- 0,\r
- &ExtendedTopologyEax.Uint32,\r
- &ExtendedTopologyEbx.Uint32,\r
- &ExtendedTopologyEcx.Uint32,\r
- NULL\r
- );\r
- //\r
- // If CPUID.(EAX=0BH, ECX=0H):EBX returns zero and maximum input value for\r
- // basic CPUID information is greater than 0BH, then CPUID.0BH leaf is not\r
- // supported on that processor.\r
- //\r
- if (ExtendedTopologyEbx.Uint32 != 0) {\r
- TopologyLeafSupported = TRUE;\r
-\r
- //\r
- // Sub-leaf index 0 (ECX= 0 as input) provides enumeration parameters to extract\r
- // the SMT sub-field of x2APIC ID.\r
- //\r
- LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
- ASSERT (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_SMT);\r
- ThreadBits = ExtendedTopologyEax.Bits.ApicIdShift;\r
-\r
- //\r
- // Software must not assume any "level type" encoding\r
- // value to be related to any sub-leaf index, except sub-leaf 0.\r
- //\r
- SubIndex = 1;\r
- do {\r
- AsmCpuidEx (\r
- CPUID_EXTENDED_TOPOLOGY,\r
- SubIndex,\r
- &ExtendedTopologyEax.Uint32,\r
- NULL,\r
- &ExtendedTopologyEcx.Uint32,\r
- NULL\r
- );\r
- LevelType = ExtendedTopologyEcx.Bits.LevelType;\r
- if (LevelType == CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_CORE) {\r
- CoreBits = ExtendedTopologyEax.Bits.ApicIdShift - ThreadBits;\r
- break;\r
- }\r
- SubIndex++;\r
- } while (LevelType != CPUID_EXTENDED_TOPOLOGY_LEVEL_TYPE_INVALID);\r
- }\r
- }\r
-\r
- if (!TopologyLeafSupported) {\r
- AsmCpuid (CPUID_VERSION_INFO, NULL, &VersionInfoEbx.Uint32, NULL, NULL);\r
- MaxLogicProcessorsPerPackage = VersionInfoEbx.Bits.MaximumAddressableIdsForLogicalProcessors;\r
- if (MaxCpuIdIndex >= CPUID_CACHE_PARAMS) {\r
- AsmCpuidEx (CPUID_CACHE_PARAMS, 0, &CacheParamsEax.Uint32, NULL, NULL, NULL);\r
- MaxCoresPerPackage = CacheParamsEax.Bits.MaximumAddressableIdsForLogicalProcessors + 1;\r
- } else {\r
- //\r
- // Must be a single-core processor.\r
- //\r
- MaxCoresPerPackage = 1;\r
- }\r
-\r
- ThreadBits = (UINTN) (HighBitSet32 (MaxLogicProcessorsPerPackage / MaxCoresPerPackage - 1) + 1);\r
- CoreBits = (UINTN) (HighBitSet32 (MaxCoresPerPackage - 1) + 1);\r
- }\r
-\r
- Location->Thread = InitialApicId & ((1 << ThreadBits) - 1);\r
- Location->Core = (InitialApicId >> ThreadBits) & ((1 << CoreBits) - 1);\r
- Location->Package = (InitialApicId >> (ThreadBits + CoreBits));\r
-}\r
-\r
/**\r
Worker function for SwitchBSP().\r
\r
ReleaseSpinLock (&CpuData->ApLock);\r
}\r
\r
+/**\r
+ Save BSP's local APIC timer setting\r
+\r
+ @param[in] CpuMpData Pointer to CPU MP Data\r
+**/\r
+VOID\r
+SaveLocalApicTimerSetting (\r
+ IN CPU_MP_DATA *CpuMpData\r
+ )\r
+{\r
+ //\r
+ // Record the current local APIC timer setting of BSP\r
+ //\r
+ GetApicTimerState (\r
+ &CpuMpData->DivideValue,\r
+ &CpuMpData->PeriodicMode,\r
+ &CpuMpData->Vector\r
+ );\r
+ CpuMpData->CurrentTimerCount = GetApicTimerCurrentCount ();\r
+ CpuMpData->TimerInterruptState = GetApicTimerInterruptState ();\r
+}\r
+\r
+/**\r
+ Sync local APIC timer setting from BSP to AP.\r
+\r
+ @param[in] CpuMpData Pointer to CPU MP Data\r
+**/\r
+VOID\r
+SyncLocalApicTimerSetting (\r
+ IN CPU_MP_DATA *CpuMpData\r
+ )\r
+{\r
+ //\r
+ // Sync local APIC timer setting from BSP to AP\r
+ //\r
+ InitializeApicTimer (\r
+ CpuMpData->DivideValue,\r
+ CpuMpData->CurrentTimerCount,\r
+ CpuMpData->PeriodicMode,\r
+ CpuMpData->Vector\r
+ );\r
+ //\r
+ // Disable AP's local APIC timer interrupt\r
+ //\r
+ DisableApicTimerInterrupt ();\r
+}\r
+\r
/**\r
Save the volatile registers required to be restored following INIT IPI.\r
\r
UINTN Index2;\r
UINTN Index3;\r
UINT32 ApicId;\r
- CPU_AP_DATA CpuData;\r
+ CPU_INFO_IN_HOB CpuInfo;\r
UINT32 ApCount;\r
CPU_INFO_IN_HOB *CpuInfoInHob;\r
\r
ApCount = CpuMpData->CpuCount - 1;\r
-\r
+ CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\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 = CpuMpData->CpuData[Index1].ApicId;\r
+ ApicId = CpuInfoInHob[Index1].ApicId;\r
for (Index2 = Index1 + 1; Index2 <= ApCount; Index2++) {\r
- if (ApicId > CpuMpData->CpuData[Index2].ApicId) {\r
+ if (ApicId > CpuInfoInHob[Index2].ApicId) {\r
Index3 = Index2;\r
- ApicId = CpuMpData->CpuData[Index2].ApicId;\r
+ ApicId = CpuInfoInHob[Index2].ApicId;\r
}\r
}\r
if (Index3 != Index1) {\r
- CopyMem (&CpuData, &CpuMpData->CpuData[Index3], sizeof (CPU_AP_DATA));\r
+ CopyMem (&CpuInfo, &CpuInfoInHob[Index3], sizeof (CPU_INFO_IN_HOB));\r
CopyMem (\r
- &CpuMpData->CpuData[Index3],\r
- &CpuMpData->CpuData[Index1],\r
- sizeof (CPU_AP_DATA)\r
+ &CpuInfoInHob[Index3],\r
+ &CpuInfoInHob[Index1],\r
+ sizeof (CPU_INFO_IN_HOB)\r
);\r
- CopyMem (&CpuMpData->CpuData[Index1], &CpuData, sizeof (CPU_AP_DATA));\r
+ CopyMem (&CpuInfoInHob[Index1], &CpuInfo, sizeof (CPU_INFO_IN_HOB));\r
}\r
}\r
\r
//\r
ApicId = GetInitialApicId ();\r
for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {\r
- if (CpuMpData->CpuData[Index1].ApicId == ApicId) {\r
+ if (CpuInfoInHob[Index1].ApicId == ApicId) {\r
CpuMpData->BspNumber = (UINT32) Index1;\r
break;\r
}\r
}\r
-\r
- CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
- for (Index1 = 0; Index1 < CpuMpData->CpuCount; Index1++) {\r
- CpuInfoInHob[Index1].InitialApicId = CpuMpData->CpuData[Index1].InitialApicId;\r
- CpuInfoInHob[Index1].ApicId = CpuMpData->CpuData[Index1].ApicId;\r
- CpuInfoInHob[Index1].Health = CpuMpData->CpuData[Index1].Health;\r
- }\r
}\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[in] ProcessorNumber Return the pocessor number found\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
UINTN TotalProcessorNumber;\r
UINTN Index;\r
+ CPU_INFO_IN_HOB *CpuInfoInHob;\r
+\r
+ CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
\r
TotalProcessorNumber = CpuMpData->CpuCount;\r
for (Index = 0; Index < TotalProcessorNumber; Index ++) {\r
- if (CpuMpData->CpuData[Index].ApicId == GetApicId ()) {\r
+ if (CpuInfoInHob[Index].ApicId == GetApicId ()) {\r
*ProcessorNumber = Index;\r
return EFI_SUCCESS;\r
}\r
CpuMpData->InitFlag = ApInitConfig;\r
CpuMpData->X2ApicEnable = FALSE;\r
WakeUpAP (CpuMpData, TRUE, 0, NULL, NULL);\r
- //\r
- // Wait for AP task to complete and then exit.\r
- //\r
- MicroSecondDelay (PcdGet32(PcdCpuApInitTimeOutInMicroSeconds));\r
CpuMpData->InitFlag = ApInitDone;\r
ASSERT (CpuMpData->CpuCount <= PcdGet32 (PcdCpuMaxLogicalProcessorNumber));\r
//\r
return CpuMpData->CpuCount;\r
}\r
\r
-/*\r
+/**\r
Initialize CPU AP Data when AP is wakeup at the first time.\r
\r
@param[in, out] CpuMpData Pointer to PEI CPU MP Data\r
@param[in] ProcessorNumber The handle number of processor\r
@param[in] BistData Processor BIST data\r
+ @param[in] ApTopOfStack Top of AP stack\r
\r
**/\r
VOID\r
InitializeApData (\r
IN OUT CPU_MP_DATA *CpuMpData,\r
IN UINTN ProcessorNumber,\r
- IN UINT32 BistData\r
+ IN UINT32 BistData,\r
+ IN UINT64 ApTopOfStack\r
)\r
{\r
+ CPU_INFO_IN_HOB *CpuInfoInHob;\r
+\r
+ CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
+ CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
+ CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();\r
+ CpuInfoInHob[ProcessorNumber].Health = BistData;\r
+ CpuInfoInHob[ProcessorNumber].ApTopOfStack = ApTopOfStack;\r
+\r
CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
- CpuMpData->CpuData[ProcessorNumber].Health = BistData;\r
CpuMpData->CpuData[ProcessorNumber].CpuHealthy = (BistData == 0) ? TRUE : FALSE;\r
- CpuMpData->CpuData[ProcessorNumber].ApicId = GetApicId ();\r
- CpuMpData->CpuData[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
- if (CpuMpData->CpuData[ProcessorNumber].InitialApicId >= 0xFF) {\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
VOID *Parameter;\r
UINT32 BistData;\r
volatile UINT32 *ApStartupSignalBuffer;\r
+ CPU_INFO_IN_HOB *CpuInfoInHob;\r
+ UINT64 ApTopOfStack;\r
\r
//\r
// AP finished assembly code and begin to execute C code\r
//\r
CpuMpData = ExchangeInfo->CpuMpData;\r
\r
- ProgramVirtualWireMode (); \r
+ //\r
+ // AP's local APIC settings will be lost after received INIT IPI\r
+ // We need to re-initialize them at here\r
+ //\r
+ ProgramVirtualWireMode ();\r
+ SyncLocalApicTimerSetting (CpuMpData);\r
\r
while (TRUE) {\r
if (CpuMpData->InitFlag == ApInitConfig) {\r
//\r
// This is first time AP wakeup, get BIST information from AP stack\r
//\r
- BistData = *(UINT32 *) (CpuMpData->Buffer + ProcessorNumber * CpuMpData->CpuApStackSize - sizeof (UINTN));\r
+ ApTopOfStack = CpuMpData->Buffer + (ProcessorNumber + 1) * CpuMpData->CpuApStackSize;\r
+ BistData = *(UINT32 *) ((UINTN) ApTopOfStack - sizeof (UINTN));\r
//\r
// Do some AP initialize sync\r
//\r
// Sync BSP's Control registers to APs\r
//\r
RestoreVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters, FALSE);\r
- InitializeApData (CpuMpData, ProcessorNumber, BistData);\r
+ InitializeApData (CpuMpData, ProcessorNumber, BistData, ApTopOfStack);\r
ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
} else {\r
//\r
if (Procedure != NULL) {\r
SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateBusy);\r
//\r
+ // Enable source debugging on AP function\r
+ // \r
+ EnableDebugAgent ();\r
+ //\r
// Invoke AP function here\r
//\r
Procedure (Parameter);\r
+ CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
if (CpuMpData->SwitchBspFlag) {\r
//\r
// Re-get the processor number due to BSP/AP maybe exchange in AP function\r
GetProcessorNumber (CpuMpData, &ProcessorNumber);\r
CpuMpData->CpuData[ProcessorNumber].ApFunction = 0;\r
CpuMpData->CpuData[ProcessorNumber].ApFunctionArgument = 0;\r
+ ApStartupSignalBuffer = CpuMpData->CpuData[ProcessorNumber].StartupApSignal;\r
+ CpuInfoInHob[ProcessorNumber].ApTopOfStack = CpuInfoInHob[CpuMpData->NewBspNumber].ApTopOfStack;\r
} else {\r
//\r
// Re-get the CPU APICID and Initial APICID\r
//\r
- CpuMpData->CpuData[ProcessorNumber].ApicId = GetApicId ();\r
- CpuMpData->CpuData[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
+ CpuInfoInHob[ProcessorNumber].ApicId = GetApicId ();\r
+ CpuInfoInHob[ProcessorNumber].InitialApicId = GetInitialApicId ();\r
}\r
}\r
SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateFinished);\r
\r
ExchangeInfo->CFunction = (UINTN) ApWakeupFunction;\r
ExchangeInfo->NumApsExecuting = 0;\r
+ ExchangeInfo->InitFlag = (UINTN) CpuMpData->InitFlag;\r
+ ExchangeInfo->CpuInfo = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
ExchangeInfo->CpuMpData = CpuMpData;\r
\r
ExchangeInfo->EnableExecuteDisable = IsBspExecuteDisableEnabled ();\r
AsmReadIdtr ((IA32_DESCRIPTOR *) &ExchangeInfo->IdtrProfile);\r
}\r
\r
+/**\r
+ Helper function that waits until the finished AP count reaches the specified\r
+ limit, or the specified timeout elapses (whichever comes first).\r
+\r
+ @param[in] CpuMpData Pointer to CPU MP Data.\r
+ @param[in] FinishedApLimit The number of finished APs to wait for.\r
+ @param[in] TimeLimit The number of microseconds to wait for.\r
+**/\r
+VOID\r
+TimedWaitForApFinish (\r
+ IN CPU_MP_DATA *CpuMpData,\r
+ IN UINT32 FinishedApLimit,\r
+ IN UINT32 TimeLimit\r
+ );\r
+\r
/**\r
This function will be called by BSP to wakeup AP.\r
\r
UINTN Index;\r
CPU_AP_DATA *CpuData;\r
BOOLEAN ResetVectorRequired;\r
+ CPU_INFO_IN_HOB *CpuInfoInHob;\r
\r
CpuMpData->FinishedCount = 0;\r
ResetVectorRequired = FALSE;\r
ResetVectorRequired = TRUE;\r
AllocateResetVector (CpuMpData);\r
FillExchangeInfoData (CpuMpData);\r
+ SaveLocalApicTimerSetting (CpuMpData);\r
} else if (CpuMpData->ApLoopMode == ApInMwaitLoop) {\r
//\r
// Get AP target C-state each time when waking up AP,\r
//\r
SendInitSipiSipiAllExcludingSelf ((UINT32) ExchangeInfo->BufferStart);\r
}\r
- if (CpuMpData->InitFlag != ApInitConfig) {\r
+ if (CpuMpData->InitFlag == ApInitConfig) {\r
+ //\r
+ // Wait for all potential APs waken up in one specified period\r
+ //\r
+ TimedWaitForApFinish (\r
+ CpuMpData,\r
+ PcdGet32 (PcdCpuMaxLogicalProcessorNumber) - 1,\r
+ PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds)\r
+ );\r
+ } else {\r
//\r
// Wait all APs waken up if this is not the 1st broadcast of SIPI\r
//\r
ASSERT (CpuMpData->InitFlag != ApInitConfig);\r
*(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
if (ResetVectorRequired) {\r
+ CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
SendInitSipiSipi (\r
- CpuData->ApicId,\r
+ CpuInfoInHob[ProcessorNumber].ApicId,\r
(UINT32) ExchangeInfo->BufferStart\r
);\r
}\r
return FALSE;\r
}\r
\r
+/**\r
+ Helper function that waits until the finished AP count reaches the specified\r
+ limit, or the specified timeout elapses (whichever comes first).\r
+\r
+ @param[in] CpuMpData Pointer to CPU MP Data.\r
+ @param[in] FinishedApLimit The number of finished APs to wait for.\r
+ @param[in] TimeLimit The number of microseconds to wait for.\r
+**/\r
+VOID\r
+TimedWaitForApFinish (\r
+ IN CPU_MP_DATA *CpuMpData,\r
+ IN UINT32 FinishedApLimit,\r
+ IN UINT32 TimeLimit\r
+ )\r
+{\r
+ //\r
+ // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0\r
+ // "infinity", so check for (TimeLimit == 0) explicitly.\r
+ //\r
+ if (TimeLimit == 0) {\r
+ return;\r
+ }\r
+\r
+ CpuMpData->TotalTime = 0;\r
+ CpuMpData->ExpectedTime = CalculateTimeout (\r
+ TimeLimit,\r
+ &CpuMpData->CurrentTime\r
+ );\r
+ while (CpuMpData->FinishedCount < FinishedApLimit &&\r
+ !CheckTimeout (\r
+ &CpuMpData->CurrentTime,\r
+ &CpuMpData->TotalTime,\r
+ CpuMpData->ExpectedTime\r
+ )) {\r
+ CpuPause ();\r
+ }\r
+\r
+ if (CpuMpData->FinishedCount >= FinishedApLimit) {\r
+ DEBUG ((\r
+ DEBUG_VERBOSE,\r
+ "%a: reached FinishedApLimit=%u in %Lu microseconds\n",\r
+ __FUNCTION__,\r
+ FinishedApLimit,\r
+ DivU64x64Remainder (\r
+ MultU64x32 (CpuMpData->TotalTime, 1000000),\r
+ GetPerformanceCounterProperties (NULL, NULL),\r
+ NULL\r
+ )\r
+ ));\r
+ }\r
+}\r
+\r
/**\r
Reset an AP to Idle state.\r
\r
\r
CpuMpData = GetCpuMpData ();\r
\r
+ CpuMpData->InitFlag = ApInitReconfig;\r
WakeUpAP (CpuMpData, FALSE, ProcessorNumber, NULL, NULL);\r
+ while (CpuMpData->FinishedCount < 1) {\r
+ CpuPause ();\r
+ }\r
+ CpuMpData->InitFlag = ApInitDone;\r
\r
SetApState (&CpuMpData->CpuData[ProcessorNumber], CpuStateIdle);\r
}\r
} else {\r
MaxLogicalProcessorNumber = OldCpuMpData->CpuCount;\r
}\r
+ ASSERT (MaxLogicalProcessorNumber != 0);\r
\r
AsmGetAddressMap (&AddressMap);\r
ApResetVectorSize = AddressMap.RendezvousFunnelSize + sizeof (MP_CPU_EXCHANGE_INFO);\r
CpuMpData->CpuApStackSize = ApStackSize;\r
CpuMpData->BackupBuffer = BackupBufferAddr;\r
CpuMpData->BackupBufferSize = ApResetVectorSize;\r
- CpuMpData->EndOfPeiFlag = FALSE;\r
+ CpuMpData->SaveRestoreFlag = FALSE;\r
CpuMpData->WakeupBuffer = (UINTN) -1;\r
CpuMpData->CpuCount = 1;\r
CpuMpData->BspNumber = 0;\r
//\r
// Set BSP basic information\r
//\r
- InitializeApData (CpuMpData, 0, 0);\r
+ InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer);\r
//\r
// Save assembly code information\r
//\r
MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
\r
if (OldCpuMpData == NULL) {\r
- //\r
- // Wakeup all APs and calculate the processor count in system\r
- //\r
- CollectProcessorCount (CpuMpData);\r
+ if (MaxLogicalProcessorNumber > 1) {\r
+ //\r
+ // Wakeup all APs and calculate the processor count in system\r
+ //\r
+ CollectProcessorCount (CpuMpData);\r
+ }\r
} else {\r
//\r
// APs have been wakeup before, just get the CPU Information\r
CpuMpData->CpuCount = OldCpuMpData->CpuCount;\r
CpuMpData->BspNumber = OldCpuMpData->BspNumber;\r
CpuMpData->InitFlag = ApInitReconfig;\r
- CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) OldCpuMpData->CpuInfoInHob;\r
+ CpuMpData->CpuInfoInHob = OldCpuMpData->CpuInfoInHob;\r
+ CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
InitializeSpinLock(&CpuMpData->CpuData[Index].ApLock);\r
- CpuMpData->CpuData[Index].ApicId = CpuInfoInHob[Index].ApicId;\r
- CpuMpData->CpuData[Index].InitialApicId = CpuInfoInHob[Index].InitialApicId;\r
- if (CpuMpData->CpuData[Index].InitialApicId >= 255) {\r
+ if (CpuInfoInHob[Index].InitialApicId >= 255) {\r
CpuMpData->X2ApicEnable = TRUE;\r
}\r
- CpuMpData->CpuData[Index].Health = CpuInfoInHob[Index].Health;\r
- CpuMpData->CpuData[Index].CpuHealthy = (CpuMpData->CpuData[Index].Health == 0)? TRUE:FALSE;\r
+ CpuMpData->CpuData[Index].CpuHealthy = (CpuInfoInHob[Index].Health == 0)? TRUE:FALSE;\r
CpuMpData->CpuData[Index].ApFunction = 0;\r
CopyMem (\r
&CpuMpData->CpuData[Index].VolatileRegisters,\r
sizeof (CPU_VOLATILE_REGISTERS)\r
);\r
}\r
- //\r
- // Wakeup APs to do some AP initialize sync\r
- //\r
- WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);\r
- //\r
- // Wait for all APs finished initialization\r
- //\r
- while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
- CpuPause ();\r
- }\r
- CpuMpData->InitFlag = ApInitDone;\r
- for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
- SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
+ if (MaxLogicalProcessorNumber > 1) {\r
+ //\r
+ // Wakeup APs to do some AP initialize sync\r
+ //\r
+ WakeUpAP (CpuMpData, TRUE, 0, ApInitializeSync, CpuMpData);\r
+ //\r
+ // Wait for all APs finished initialization\r
+ //\r
+ while (CpuMpData->FinishedCount < (CpuMpData->CpuCount - 1)) {\r
+ CpuPause ();\r
+ }\r
+ CpuMpData->InitFlag = ApInitDone;\r
+ for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+ SetApState (&CpuMpData->CpuData[Index], CpuStateIdle);\r
+ }\r
}\r
}\r
\r
{\r
CPU_MP_DATA *CpuMpData;\r
UINTN CallerNumber;\r
+ CPU_INFO_IN_HOB *CpuInfoInHob;\r
\r
CpuMpData = GetCpuMpData ();\r
+ CpuInfoInHob = (CPU_INFO_IN_HOB *) (UINTN) CpuMpData->CpuInfoInHob;\r
\r
//\r
// Check whether caller processor is BSP\r
return EFI_NOT_FOUND;\r
}\r
\r
- ProcessorInfoBuffer->ProcessorId = (UINT64) CpuMpData->CpuData[ProcessorNumber].ApicId;\r
+ ProcessorInfoBuffer->ProcessorId = (UINT64) CpuInfoInHob[ProcessorNumber].ApicId;\r
ProcessorInfoBuffer->StatusFlag = 0;\r
if (ProcessorNumber == CpuMpData->BspNumber) {\r
ProcessorInfoBuffer->StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
//\r
// Get processor location information\r
//\r
- ExtractProcessorLocation (CpuMpData->CpuData[ProcessorNumber].ApicId, &ProcessorInfoBuffer->Location);\r
+ GetProcessorLocationByApicId (\r
+ CpuInfoInHob[ProcessorNumber].ApicId,\r
+ &ProcessorInfoBuffer->Location.Package,\r
+ &ProcessorInfoBuffer->Location.Core,\r
+ &ProcessorInfoBuffer->Location.Thread\r
+ );\r
\r
if (HealthData != NULL) {\r
- HealthData->Uint32 = CpuMpData->CpuData[ProcessorNumber].Health;\r
+ HealthData->Uint32 = CpuInfoInHob[ProcessorNumber].Health;\r
}\r
\r
return EFI_SUCCESS;\r
UINTN CallerNumber;\r
CPU_STATE State;\r
MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr;\r
+ BOOLEAN OldInterruptState;\r
+ BOOLEAN OldTimerInterruptState;\r
+\r
+ //\r
+ // Save and Disable Local APIC timer interrupt\r
+ //\r
+ OldTimerInterruptState = GetApicTimerInterruptState ();\r
+ DisableApicTimerInterrupt ();\r
+ //\r
+ // Before send both BSP and AP to a procedure to exchange their roles,\r
+ // interrupt must be disabled. This is because during the exchange role\r
+ // process, 2 CPU may use 1 stack. If interrupt happens, the stack will\r
+ // be corrupted, since interrupt return address will be pushed to stack\r
+ // by hardware.\r
+ //\r
+ OldInterruptState = SaveAndDisableInterrupts ();\r
+\r
+ //\r
+ // Mask LINT0 & LINT1 for the old BSP\r
+ //\r
+ DisableLvtInterrupts ();\r
\r
CpuMpData = GetCpuMpData ();\r
\r
CpuMpData->BSPInfo.State = CPU_SWITCH_STATE_IDLE;\r
CpuMpData->APInfo.State = CPU_SWITCH_STATE_IDLE;\r
CpuMpData->SwitchBspFlag = TRUE;\r
+ CpuMpData->NewBspNumber = ProcessorNumber;\r
\r
//\r
// Clear the BSP bit of MSR_IA32_APIC_BASE\r
//\r
CpuMpData->BspNumber = (UINT32) ProcessorNumber;\r
\r
+ //\r
+ // Restore interrupt state.\r
+ //\r
+ SetInterruptState (OldInterruptState);\r
+\r
+ if (OldTimerInterruptState) {\r
+ EnableApicTimerInterrupt ();\r
+ }\r
+\r
return EFI_SUCCESS;\r
}\r
\r
}\r
\r
\r
+/**\r
+ Worker function to execute a caller provided function on all enabled APs.\r
+\r
+ @param[in] Procedure A pointer to the function to be run on\r
+ enabled APs of the system.\r
+ @param[in] SingleThread If TRUE, then all the enabled APs execute\r
+ the function specified by Procedure one by\r
+ one, in ascending order of processor handle\r
+ number. If FALSE, then all the enabled APs\r
+ execute the function specified by Procedure\r
+ simultaneously.\r
+ @param[in] WaitEvent The event created by the caller with CreateEvent()\r
+ service.\r
+ @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
+ APs to return from Procedure, either for\r
+ blocking or non-blocking mode.\r
+ @param[in] ProcedureArgument The parameter passed into Procedure for\r
+ all APs.\r
+ @param[out] FailedCpuList If all APs finish successfully, then its\r
+ content is set to NULL. If not all APs\r
+ finish before timeout expires, then its\r
+ content is set to address of the buffer\r
+ holding handle numbers of the failed APs.\r
+\r
+ @retval EFI_SUCCESS In blocking mode, all APs have finished before\r
+ the timeout expired.\r
+ @retval EFI_SUCCESS In non-blocking mode, function has been dispatched\r
+ to all enabled APs.\r
+ @retval others Failed to Startup all APs.\r
+\r
+**/\r
+EFI_STATUS\r
+StartupAllAPsWorker (\r
+ IN EFI_AP_PROCEDURE Procedure,\r
+ IN BOOLEAN SingleThread,\r
+ IN EFI_EVENT WaitEvent OPTIONAL,\r
+ IN UINTN TimeoutInMicroseconds,\r
+ IN VOID *ProcedureArgument OPTIONAL,\r
+ OUT UINTN **FailedCpuList OPTIONAL\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ CPU_MP_DATA *CpuMpData;\r
+ UINTN ProcessorCount;\r
+ UINTN ProcessorNumber;\r
+ UINTN CallerNumber;\r
+ CPU_AP_DATA *CpuData;\r
+ BOOLEAN HasEnabledAp;\r
+ CPU_STATE ApState;\r
+\r
+ CpuMpData = GetCpuMpData ();\r
+\r
+ if (FailedCpuList != NULL) {\r
+ *FailedCpuList = NULL;\r
+ }\r
+\r
+ if (CpuMpData->CpuCount == 1) {\r
+ return EFI_NOT_STARTED;\r
+ }\r
+\r
+ if (Procedure == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Check whether caller processor is BSP\r
+ //\r
+ MpInitLibWhoAmI (&CallerNumber);\r
+ if (CallerNumber != CpuMpData->BspNumber) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ //\r
+ // Update AP state\r
+ //\r
+ CheckAndUpdateApsStatus ();\r
+\r
+ ProcessorCount = CpuMpData->CpuCount;\r
+ HasEnabledAp = FALSE;\r
+ //\r
+ // Check whether all enabled APs are idle.\r
+ // If any enabled AP is not idle, return EFI_NOT_READY.\r
+ //\r
+ for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
+ CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+ if (ProcessorNumber != CpuMpData->BspNumber) {\r
+ ApState = GetApState (CpuData);\r
+ if (ApState != CpuStateDisabled) {\r
+ HasEnabledAp = TRUE;\r
+ if (ApState != CpuStateIdle) {\r
+ //\r
+ // If any enabled APs are busy, return EFI_NOT_READY.\r
+ //\r
+ return EFI_NOT_READY;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ if (!HasEnabledAp) {\r
+ //\r
+ // If no enabled AP exists, return EFI_NOT_STARTED.\r
+ //\r
+ return EFI_NOT_STARTED;\r
+ }\r
+\r
+ CpuMpData->StartCount = 0;\r
+ for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
+ CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+ CpuData->Waiting = FALSE;\r
+ if (ProcessorNumber != CpuMpData->BspNumber) {\r
+ if (CpuData->State == CpuStateIdle) {\r
+ //\r
+ // Mark this processor as responsible for current calling.\r
+ //\r
+ CpuData->Waiting = TRUE;\r
+ CpuMpData->StartCount++;\r
+ }\r
+ }\r
+ }\r
+\r
+ CpuMpData->Procedure = Procedure;\r
+ CpuMpData->ProcArguments = ProcedureArgument;\r
+ CpuMpData->SingleThread = SingleThread;\r
+ CpuMpData->FinishedCount = 0;\r
+ CpuMpData->RunningCount = 0;\r
+ CpuMpData->FailedCpuList = FailedCpuList;\r
+ CpuMpData->ExpectedTime = CalculateTimeout (\r
+ TimeoutInMicroseconds,\r
+ &CpuMpData->CurrentTime\r
+ );\r
+ CpuMpData->TotalTime = 0;\r
+ CpuMpData->WaitEvent = WaitEvent;\r
+\r
+ if (!SingleThread) {\r
+ WakeUpAP (CpuMpData, TRUE, 0, Procedure, ProcedureArgument);\r
+ } else {\r
+ for (ProcessorNumber = 0; ProcessorNumber < ProcessorCount; ProcessorNumber++) {\r
+ if (ProcessorNumber == CallerNumber) {\r
+ continue;\r
+ }\r
+ if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
+ WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
+ break;\r
+ }\r
+ }\r
+ }\r
+\r
+ Status = EFI_SUCCESS;\r
+ if (WaitEvent == NULL) {\r
+ do {\r
+ Status = CheckAllAPs ();\r
+ } while (Status == EFI_NOT_READY);\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Worker function to let the caller get one enabled AP to execute a caller-provided\r
+ function.\r
+\r
+ @param[in] Procedure A pointer to the function to be run on\r
+ enabled APs of the system.\r
+ @param[in] ProcessorNumber The handle number of the AP.\r
+ @param[in] WaitEvent The event created by the caller with CreateEvent()\r
+ service.\r
+ @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for\r
+ APs to return from Procedure, either for\r
+ blocking or non-blocking mode.\r
+ @param[in] ProcedureArgument The parameter passed into Procedure for\r
+ all APs.\r
+ @param[out] Finished If AP returns from Procedure before the\r
+ timeout expires, its content is set to TRUE.\r
+ Otherwise, the value is set to FALSE.\r
+\r
+ @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
+ the timeout expires.\r
+ @retval others Failed to Startup AP.\r
+\r
+**/\r
+EFI_STATUS\r
+StartupThisAPWorker (\r
+ IN EFI_AP_PROCEDURE Procedure,\r
+ IN UINTN ProcessorNumber,\r
+ IN EFI_EVENT WaitEvent OPTIONAL,\r
+ IN UINTN TimeoutInMicroseconds,\r
+ IN VOID *ProcedureArgument OPTIONAL,\r
+ OUT BOOLEAN *Finished OPTIONAL\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ CPU_MP_DATA *CpuMpData;\r
+ CPU_AP_DATA *CpuData;\r
+ UINTN CallerNumber;\r
+\r
+ CpuMpData = GetCpuMpData ();\r
+\r
+ if (Finished != NULL) {\r
+ *Finished = FALSE;\r
+ }\r
+\r
+ //\r
+ // Check whether caller processor is BSP\r
+ //\r
+ MpInitLibWhoAmI (&CallerNumber);\r
+ if (CallerNumber != CpuMpData->BspNumber) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ //\r
+ // Check whether processor with the handle specified by ProcessorNumber exists\r
+ //\r
+ if (ProcessorNumber >= CpuMpData->CpuCount) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ //\r
+ // Check whether specified processor is BSP\r
+ //\r
+ if (ProcessorNumber == CpuMpData->BspNumber) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Check parameter Procedure\r
+ //\r
+ if (Procedure == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Update AP state\r
+ //\r
+ CheckAndUpdateApsStatus ();\r
+\r
+ //\r
+ // Check whether specified AP is disabled\r
+ //\r
+ if (GetApState (&CpuMpData->CpuData[ProcessorNumber]) == CpuStateDisabled) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // If WaitEvent is not NULL, execute in non-blocking mode.\r
+ // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.\r
+ // CheckAPsStatus() will check completion and timeout periodically.\r
+ //\r
+ CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+ CpuData->WaitEvent = WaitEvent;\r
+ CpuData->Finished = Finished;\r
+ CpuData->ExpectedTime = CalculateTimeout (TimeoutInMicroseconds, &CpuData->CurrentTime);\r
+ CpuData->TotalTime = 0;\r
+\r
+ WakeUpAP (CpuMpData, FALSE, ProcessorNumber, Procedure, ProcedureArgument);\r
+\r
+ //\r
+ // If WaitEvent is NULL, execute in blocking mode.\r
+ // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.\r
+ //\r
+ Status = EFI_SUCCESS;\r
+ if (WaitEvent == NULL) {\r
+ do {\r
+ Status = CheckThisAP (ProcessorNumber);\r
+ } while (Status == EFI_NOT_READY);\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
/**\r
Get pointer to CPU MP Data structure from GUIDed HOB.\r
\r
}\r
return CpuMpData;\r
}\r
+\r
+/**\r
+ Get available system memory below 1MB by specified size.\r
+\r
+ @param[in] CpuMpData The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+BackupAndPrepareWakeupBuffer(\r
+ IN CPU_MP_DATA *CpuMpData\r
+ )\r
+{\r
+ CopyMem (\r
+ (VOID *) CpuMpData->BackupBuffer,\r
+ (VOID *) CpuMpData->WakeupBuffer,\r
+ CpuMpData->BackupBufferSize\r
+ );\r
+ CopyMem (\r
+ (VOID *) CpuMpData->WakeupBuffer,\r
+ (VOID *) CpuMpData->AddressMap.RendezvousFunnelAddress,\r
+ CpuMpData->AddressMap.RendezvousFunnelSize\r
+ );\r
+}\r
+\r
+/**\r
+ Restore wakeup buffer data.\r
+\r
+ @param[in] CpuMpData The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+RestoreWakeupBuffer(\r
+ IN CPU_MP_DATA *CpuMpData\r
+ )\r
+{\r
+ CopyMem (\r
+ (VOID *) CpuMpData->WakeupBuffer,\r
+ (VOID *) CpuMpData->BackupBuffer,\r
+ CpuMpData->BackupBufferSize\r
+ );\r
+}\r