+/**\r
+ Free AP reset vector buffer.\r
+\r
+ @param[in] CpuMpData The pointer to CPU MP Data structure.\r
+**/\r
+VOID\r
+FreeResetVector (\r
+ IN CPU_MP_DATA *CpuMpData\r
+ )\r
+{\r
+ RestoreWakeupBuffer (CpuMpData);\r
+}\r
+\r
+/**\r
+ This function will be called by BSP to wakeup AP.\r
+\r
+ @param[in] CpuMpData Pointer to CPU MP Data\r
+ @param[in] Broadcast TRUE: Send broadcast IPI to all APs\r
+ FALSE: Send IPI to AP by ApicId\r
+ @param[in] ProcessorNumber The handle number of specified processor\r
+ @param[in] Procedure The function to be invoked by AP\r
+ @param[in] ProcedureArgument The argument to be passed into AP function\r
+**/\r
+VOID\r
+WakeUpAP (\r
+ IN CPU_MP_DATA *CpuMpData,\r
+ IN BOOLEAN Broadcast,\r
+ IN UINTN ProcessorNumber,\r
+ IN EFI_AP_PROCEDURE Procedure, OPTIONAL\r
+ IN VOID *ProcedureArgument OPTIONAL\r
+ )\r
+{\r
+ volatile MP_CPU_EXCHANGE_INFO *ExchangeInfo;\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
+\r
+ if (CpuMpData->ApLoopMode == ApInHltLoop ||\r
+ CpuMpData->InitFlag != ApInitDone) {\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
+ // for it maybe updated by platform again\r
+ //\r
+ CpuMpData->ApTargetCState = PcdGet8 (PcdCpuApTargetCstate);\r
+ }\r
+\r
+ ExchangeInfo = CpuMpData->MpCpuExchangeInfo;\r
+\r
+ if (Broadcast) {\r
+ for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+ if (Index != CpuMpData->BspNumber) {\r
+ CpuData = &CpuMpData->CpuData[Index];\r
+ CpuData->ApFunction = (UINTN) Procedure;\r
+ CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
+ SetApState (CpuData, CpuStateReady);\r
+ if (CpuMpData->InitFlag != ApInitConfig) {\r
+ *(UINT32 *) CpuData->StartupApSignal = WAKEUP_AP_SIGNAL;\r
+ }\r
+ }\r
+ }\r
+ if (ResetVectorRequired) {\r
+ //\r
+ // Wakeup all APs\r
+ //\r
+ 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
+\r
+ while (CpuMpData->MpCpuExchangeInfo->NumApsExecuting != 0) {\r
+ CpuPause();\r
+ }\r
+ } else {\r
+ //\r
+ // Wait all APs waken up if this is not the 1st broadcast of SIPI\r
+ //\r
+ for (Index = 0; Index < CpuMpData->CpuCount; Index++) {\r
+ CpuData = &CpuMpData->CpuData[Index];\r
+ if (Index != CpuMpData->BspNumber) {\r
+ WaitApWakeup (CpuData->StartupApSignal);\r
+ }\r
+ }\r
+ }\r
+ } else {\r
+ CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+ CpuData->ApFunction = (UINTN) Procedure;\r
+ CpuData->ApFunctionArgument = (UINTN) ProcedureArgument;\r
+ SetApState (CpuData, CpuStateReady);\r
+ //\r
+ // Wakeup specified AP\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
+ CpuInfoInHob[ProcessorNumber].ApicId,\r
+ (UINT32) ExchangeInfo->BufferStart\r
+ );\r
+ }\r
+ //\r
+ // Wait specified AP waken up\r
+ //\r
+ WaitApWakeup (CpuData->StartupApSignal);\r
+ }\r
+\r
+ if (ResetVectorRequired) {\r
+ FreeResetVector (CpuMpData);\r
+ }\r
+}\r
+\r
+/**\r
+ Calculate timeout value and return the current performance counter value.\r
+\r
+ Calculate the number of performance counter ticks required for a timeout.\r
+ If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
+ as infinity.\r
+\r
+ @param[in] TimeoutInMicroseconds Timeout value in microseconds.\r
+ @param[out] CurrentTime Returns the current value of the performance counter.\r
+\r
+ @return Expected time stamp counter for timeout.\r
+ If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
+ as infinity.\r
+\r
+**/\r
+UINT64\r
+CalculateTimeout (\r
+ IN UINTN TimeoutInMicroseconds,\r
+ OUT UINT64 *CurrentTime\r
+ )\r
+{\r
+ UINT64 TimeoutInSeconds;\r
+ UINT64 TimestampCounterFreq;\r
+\r
+ //\r
+ // Read the current value of the performance counter\r
+ //\r
+ *CurrentTime = GetPerformanceCounter ();\r
+\r
+ //\r
+ // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized\r
+ // as infinity.\r
+ //\r
+ if (TimeoutInMicroseconds == 0) {\r
+ return 0;\r
+ }\r
+\r
+ //\r
+ // GetPerformanceCounterProperties () returns the timestamp counter's frequency\r
+ // in Hz. \r
+ //\r
+ TimestampCounterFreq = GetPerformanceCounterProperties (NULL, NULL);\r
+\r
+ //\r
+ // Check the potential overflow before calculate the number of ticks for the timeout value.\r
+ //\r
+ if (DivU64x64Remainder (MAX_UINT64, TimeoutInMicroseconds, NULL) < TimestampCounterFreq) {\r
+ //\r
+ // Convert microseconds into seconds if direct multiplication overflows\r
+ //\r
+ TimeoutInSeconds = DivU64x32 (TimeoutInMicroseconds, 1000000);\r
+ //\r
+ // Assertion if the final tick count exceeds MAX_UINT64\r
+ //\r
+ ASSERT (DivU64x64Remainder (MAX_UINT64, TimeoutInSeconds, NULL) >= TimestampCounterFreq);\r
+ return MultU64x64 (TimestampCounterFreq, TimeoutInSeconds);\r
+ } else {\r
+ //\r
+ // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide\r
+ // it by 1,000,000, to get the number of ticks for the timeout value.\r
+ //\r
+ return DivU64x32 (\r
+ MultU64x64 (\r
+ TimestampCounterFreq,\r
+ TimeoutInMicroseconds\r
+ ),\r
+ 1000000\r
+ );\r
+ }\r
+}\r
+\r
+/**\r
+ Checks whether timeout expires.\r
+\r
+ Check whether the number of elapsed performance counter ticks required for\r
+ a timeout condition has been reached.\r
+ If Timeout is zero, which means infinity, return value is always FALSE.\r
+\r
+ @param[in, out] PreviousTime On input, the value of the performance counter\r
+ when it was last read.\r
+ On output, the current value of the performance\r
+ counter\r
+ @param[in] TotalTime The total amount of elapsed time in performance\r
+ counter ticks.\r
+ @param[in] Timeout The number of performance counter ticks required\r
+ to reach a timeout condition.\r
+\r
+ @retval TRUE A timeout condition has been reached.\r
+ @retval FALSE A timeout condition has not been reached.\r
+\r
+**/\r
+BOOLEAN\r
+CheckTimeout (\r
+ IN OUT UINT64 *PreviousTime,\r
+ IN UINT64 *TotalTime,\r
+ IN UINT64 Timeout\r
+ )\r
+{\r
+ UINT64 Start;\r
+ UINT64 End;\r
+ UINT64 CurrentTime;\r
+ INT64 Delta;\r
+ INT64 Cycle;\r
+\r
+ if (Timeout == 0) {\r
+ return FALSE;\r
+ }\r
+ GetPerformanceCounterProperties (&Start, &End);\r
+ Cycle = End - Start;\r
+ if (Cycle < 0) {\r
+ Cycle = -Cycle;\r
+ }\r
+ Cycle++;\r
+ CurrentTime = GetPerformanceCounter();\r
+ Delta = (INT64) (CurrentTime - *PreviousTime);\r
+ if (Start > End) {\r
+ Delta = -Delta;\r
+ }\r
+ if (Delta < 0) {\r
+ Delta += Cycle;\r
+ }\r
+ *TotalTime += Delta;\r
+ *PreviousTime = CurrentTime;\r
+ if (*TotalTime > Timeout) {\r
+ return TRUE;\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
+ Any task being executed by the AP will be aborted and the AP\r
+ will be waiting for a new task in Wait-For-SIPI state.\r
+\r
+ @param[in] ProcessorNumber The handle number of processor.\r
+**/\r
+VOID\r
+ResetProcessorToIdleState (\r
+ IN UINTN ProcessorNumber\r
+ )\r
+{\r
+ CPU_MP_DATA *CpuMpData;\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
+\r
+/**\r
+ Searches for the next waiting AP.\r
+\r
+ Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().\r
+\r
+ @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.\r
+\r
+ @retval EFI_SUCCESS The next waiting AP has been found.\r
+ @retval EFI_NOT_FOUND No waiting AP exists.\r
+\r
+**/\r
+EFI_STATUS\r
+GetNextWaitingProcessorNumber (\r
+ OUT UINTN *NextProcessorNumber\r
+ )\r
+{\r
+ UINTN ProcessorNumber;\r
+ CPU_MP_DATA *CpuMpData;\r
+\r
+ CpuMpData = GetCpuMpData ();\r
+\r
+ for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
+ if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
+ *NextProcessorNumber = ProcessorNumber;\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+/** Checks status of specified AP.\r
+\r
+ This function checks whether the specified AP has finished the task assigned\r
+ by StartupThisAP(), and whether timeout expires.\r
+\r
+ @param[in] ProcessorNumber The handle number of processor.\r
+\r
+ @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().\r
+ @retval EFI_TIMEOUT The timeout expires.\r
+ @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.\r
+**/\r
+EFI_STATUS\r
+CheckThisAP (\r
+ IN UINTN ProcessorNumber\r
+ )\r
+{\r
+ CPU_MP_DATA *CpuMpData;\r
+ CPU_AP_DATA *CpuData;\r
+\r
+ CpuMpData = GetCpuMpData ();\r
+ CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+\r
+ //\r
+ // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
+ // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
+ // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
+ //\r
+ //\r
+ // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.\r
+ //\r
+ if (GetApState(CpuData) == CpuStateFinished) {\r
+ if (CpuData->Finished != NULL) {\r
+ *(CpuData->Finished) = TRUE;\r
+ }\r
+ SetApState (CpuData, CpuStateIdle);\r
+ return EFI_SUCCESS;\r
+ } else {\r
+ //\r
+ // If timeout expires for StartupThisAP(), report timeout.\r
+ //\r
+ if (CheckTimeout (&CpuData->CurrentTime, &CpuData->TotalTime, CpuData->ExpectedTime)) {\r
+ if (CpuData->Finished != NULL) {\r
+ *(CpuData->Finished) = FALSE;\r
+ }\r
+ //\r
+ // Reset failed AP to idle state\r
+ //\r
+ ResetProcessorToIdleState (ProcessorNumber);\r
+\r
+ return EFI_TIMEOUT;\r
+ }\r
+ }\r
+ return EFI_NOT_READY;\r
+}\r
+\r
+/**\r
+ Checks status of all APs.\r
+\r
+ This function checks whether all APs have finished task assigned by StartupAllAPs(),\r
+ and whether timeout expires.\r
+\r
+ @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().\r
+ @retval EFI_TIMEOUT The timeout expires.\r
+ @retval EFI_NOT_READY APs have not finished task and timeout has not expired.\r
+**/\r
+EFI_STATUS\r
+CheckAllAPs (\r
+ VOID\r
+ )\r
+{\r
+ UINTN ProcessorNumber;\r
+ UINTN NextProcessorNumber;\r
+ UINTN ListIndex;\r
+ EFI_STATUS Status;\r
+ CPU_MP_DATA *CpuMpData;\r
+ CPU_AP_DATA *CpuData;\r
+\r
+ CpuMpData = GetCpuMpData ();\r
+\r
+ NextProcessorNumber = 0;\r
+\r
+ //\r
+ // Go through all APs that are responsible for the StartupAllAPs().\r
+ //\r
+ for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
+ if (!CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
+ continue;\r
+ }\r
+\r
+ CpuData = &CpuMpData->CpuData[ProcessorNumber];\r
+ //\r
+ // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.\r
+ // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the\r
+ // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.\r
+ //\r
+ if (GetApState(CpuData) == CpuStateFinished) {\r
+ CpuMpData->RunningCount ++;\r
+ CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
+ SetApState(CpuData, CpuStateIdle);\r
+\r
+ //\r
+ // If in Single Thread mode, then search for the next waiting AP for execution.\r
+ //\r
+ if (CpuMpData->SingleThread) {\r
+ Status = GetNextWaitingProcessorNumber (&NextProcessorNumber);\r
+\r
+ if (!EFI_ERROR (Status)) {\r
+ WakeUpAP (\r
+ CpuMpData,\r
+ FALSE,\r
+ (UINT32) NextProcessorNumber,\r
+ CpuMpData->Procedure,\r
+ CpuMpData->ProcArguments\r
+ );\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ //\r
+ // If all APs finish, return EFI_SUCCESS.\r
+ //\r
+ if (CpuMpData->RunningCount == CpuMpData->StartCount) {\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ //\r
+ // If timeout expires, report timeout.\r
+ //\r
+ if (CheckTimeout (\r
+ &CpuMpData->CurrentTime,\r
+ &CpuMpData->TotalTime,\r
+ CpuMpData->ExpectedTime)\r
+ ) {\r
+ //\r
+ // If FailedCpuList is not NULL, record all failed APs in it.\r
+ //\r
+ if (CpuMpData->FailedCpuList != NULL) {\r
+ *CpuMpData->FailedCpuList =\r
+ AllocatePool ((CpuMpData->StartCount - CpuMpData->FinishedCount + 1) * sizeof (UINTN));\r
+ ASSERT (*CpuMpData->FailedCpuList != NULL);\r
+ }\r
+ ListIndex = 0;\r
+\r
+ for (ProcessorNumber = 0; ProcessorNumber < CpuMpData->CpuCount; ProcessorNumber++) {\r
+ //\r
+ // Check whether this processor is responsible for StartupAllAPs().\r
+ //\r
+ if (CpuMpData->CpuData[ProcessorNumber].Waiting) {\r
+ //\r
+ // Reset failed APs to idle state\r
+ //\r
+ ResetProcessorToIdleState (ProcessorNumber);\r
+ CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\r
+ if (CpuMpData->FailedCpuList != NULL) {\r
+ (*CpuMpData->FailedCpuList)[ListIndex++] = ProcessorNumber;\r
+ }\r
+ }\r
+ }\r
+ if (CpuMpData->FailedCpuList != NULL) {\r
+ (*CpuMpData->FailedCpuList)[ListIndex] = END_OF_CPU_LIST;\r
+ }\r
+ return EFI_TIMEOUT;\r
+ }\r
+ return EFI_NOT_READY;\r
+}\r
+\r
+/**\r
+ MP Initialize Library initialization.\r
+\r
+ This service will allocate AP reset vector and wakeup all APs to do APs\r
+ initialization.\r
+\r
+ This service must be invoked before all other MP Initialize Library\r
+ service are invoked.\r
+\r
+ @retval EFI_SUCCESS MP initialization succeeds.\r
+ @retval Others MP initialization fails.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+MpInitLibInitialize (\r
+ VOID\r
+ )\r
+{\r
+ CPU_MP_DATA *OldCpuMpData;\r
+ CPU_INFO_IN_HOB *CpuInfoInHob;\r
+ UINT32 MaxLogicalProcessorNumber;\r
+ UINT32 ApStackSize;\r
+ MP_ASSEMBLY_ADDRESS_MAP AddressMap;\r
+ UINTN BufferSize;\r
+ UINT32 MonitorFilterSize;\r
+ VOID *MpBuffer;\r
+ UINTN Buffer;\r
+ CPU_MP_DATA *CpuMpData;\r
+ UINT8 ApLoopMode;\r
+ UINT8 *MonitorBuffer;\r
+ UINTN Index;\r
+ UINTN ApResetVectorSize;\r
+ UINTN BackupBufferAddr;\r
+\r
+ OldCpuMpData = GetCpuMpDataFromGuidedHob ();\r
+ if (OldCpuMpData == NULL) {\r
+ MaxLogicalProcessorNumber = PcdGet32(PcdCpuMaxLogicalProcessorNumber);\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
+ ApStackSize = PcdGet32(PcdCpuApStackSize);\r
+ ApLoopMode = GetApLoopMode (&MonitorFilterSize);\r
+\r
+ BufferSize = ApStackSize * MaxLogicalProcessorNumber;\r
+ BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;\r
+ BufferSize += sizeof (CPU_MP_DATA);\r
+ BufferSize += ApResetVectorSize;\r
+ BufferSize += (sizeof (CPU_AP_DATA) + sizeof (CPU_INFO_IN_HOB))* MaxLogicalProcessorNumber;\r
+ MpBuffer = AllocatePages (EFI_SIZE_TO_PAGES (BufferSize));\r
+ ASSERT (MpBuffer != NULL);\r
+ ZeroMem (MpBuffer, BufferSize);\r
+ Buffer = (UINTN) MpBuffer;\r
+\r
+ MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);\r
+ BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;\r
+ CpuMpData = (CPU_MP_DATA *) (BackupBufferAddr + ApResetVectorSize);\r
+ CpuMpData->Buffer = Buffer;\r
+ CpuMpData->CpuApStackSize = ApStackSize;\r
+ CpuMpData->BackupBuffer = BackupBufferAddr;\r
+ CpuMpData->BackupBufferSize = ApResetVectorSize;\r
+ CpuMpData->WakeupBuffer = (UINTN) -1;\r
+ CpuMpData->CpuCount = 1;\r
+ CpuMpData->BspNumber = 0;\r
+ CpuMpData->WaitEvent = NULL;\r
+ CpuMpData->SwitchBspFlag = FALSE;\r
+ CpuMpData->CpuData = (CPU_AP_DATA *) (CpuMpData + 1);\r
+ CpuMpData->CpuInfoInHob = (UINT64) (UINTN) (CpuMpData->CpuData + MaxLogicalProcessorNumber);\r
+ CpuMpData->MicrocodePatchAddress = PcdGet64 (PcdCpuMicrocodePatchAddress);\r
+ CpuMpData->MicrocodePatchRegionSize = PcdGet64 (PcdCpuMicrocodePatchRegionSize);\r
+ InitializeSpinLock(&CpuMpData->MpLock);\r
+ //\r
+ // Save BSP's Control registers to APs\r
+ //\r
+ SaveVolatileRegisters (&CpuMpData->CpuData[0].VolatileRegisters);\r
+ //\r
+ // Set BSP basic information\r
+ //\r
+ InitializeApData (CpuMpData, 0, 0, CpuMpData->Buffer + ApStackSize);\r
+ //\r
+ // Save assembly code information\r
+ //\r
+ CopyMem (&CpuMpData->AddressMap, &AddressMap, sizeof (MP_ASSEMBLY_ADDRESS_MAP));\r
+ //\r
+ // Finally set AP loop mode\r
+ //\r
+ CpuMpData->ApLoopMode = ApLoopMode;\r
+ DEBUG ((DEBUG_INFO, "AP Loop Mode is %d\n", CpuMpData->ApLoopMode));\r
+ //\r
+ // Set up APs wakeup signal buffer\r
+ //\r
+ for (Index = 0; Index < MaxLogicalProcessorNumber; Index++) {\r
+ CpuMpData->CpuData[Index].StartupApSignal =\r
+ (UINT32 *)(MonitorBuffer + MonitorFilterSize * Index);\r
+ }\r
+ //\r
+ // Load Microcode on BSP\r
+ //\r
+ MicrocodeDetect (CpuMpData);\r
+ //\r
+ // Store BSP's MTRR setting\r
+ //\r
+ MtrrGetAllMtrrs (&CpuMpData->MtrrTable);\r
+ //\r
+ // Enable the local APIC for Virtual Wire Mode.\r
+ //\r
+ ProgramVirtualWireMode ();\r
+\r
+ if (OldCpuMpData == NULL) {\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
+ // from HOB\r
+ //\r
+ CpuMpData->CpuCount = OldCpuMpData->CpuCount;\r
+ CpuMpData->BspNumber = OldCpuMpData->BspNumber;\r
+ CpuMpData->InitFlag = ApInitReconfig;\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
+ 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 (\r
+ &CpuMpData->CpuData[Index].VolatileRegisters,\r
+ &CpuMpData->CpuData[0].VolatileRegisters,\r
+ sizeof (CPU_VOLATILE_REGISTERS)\r
+ );\r
+ }\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