+ // 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 CpuStateIdle, 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 CpuStateIdle, 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) == CpuStateIdle) {\r
+ if (CpuData->Finished != NULL) {\r
+ *(CpuData->Finished) = TRUE;\r
+ }\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 CpuStateIdle, 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 CpuStateIdle, so BSP can safely make use of its value.\r
+ //\r
+ if (GetApState(CpuData) == CpuStateIdle) {\r
+ CpuMpData->RunningCount --;\r
+ CpuMpData->CpuData[ProcessorNumber].Waiting = FALSE;\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 == 0) {\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->RunningCount + 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
+ CPU_VOLATILE_REGISTERS VolatileRegisters;\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
+ UINTN ApIdtBase;\r
+ VOID *MicrocodePatchInRam;\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
+ //\r
+ // Save BSP's Control registers for APs\r
+ //\r
+ SaveVolatileRegisters (&VolatileRegisters);\r
+\r
+ BufferSize = ApStackSize * MaxLogicalProcessorNumber;\r
+ BufferSize += MonitorFilterSize * MaxLogicalProcessorNumber;\r
+ BufferSize += ApResetVectorSize;\r
+ BufferSize = ALIGN_VALUE (BufferSize, 8);\r
+ BufferSize += VolatileRegisters.Idtr.Limit + 1;\r
+ BufferSize += sizeof (CPU_MP_DATA);\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
+ //\r
+ // The layout of the Buffer is as below:\r
+ //\r
+ // +--------------------+ <-- Buffer\r
+ // AP Stacks (N)\r
+ // +--------------------+ <-- MonitorBuffer\r
+ // AP Monitor Filters (N)\r
+ // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)\r
+ // Backup Buffer\r
+ // +--------------------+\r
+ // Padding\r
+ // +--------------------+ <-- ApIdtBase (8-byte boundary)\r
+ // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.\r
+ // +--------------------+ <-- CpuMpData\r
+ // CPU_MP_DATA\r
+ // +--------------------+ <-- CpuMpData->CpuData\r
+ // CPU_AP_DATA (N)\r
+ // +--------------------+ <-- CpuMpData->CpuInfoInHob\r
+ // CPU_INFO_IN_HOB (N)\r
+ // +--------------------+\r
+ //\r
+ MonitorBuffer = (UINT8 *) (Buffer + ApStackSize * MaxLogicalProcessorNumber);\r
+ BackupBufferAddr = (UINTN) MonitorBuffer + MonitorFilterSize * MaxLogicalProcessorNumber;\r
+ ApIdtBase = ALIGN_VALUE (BackupBufferAddr + ApResetVectorSize, 8);\r
+ CpuMpData = (CPU_MP_DATA *) (ApIdtBase + VolatileRegisters.Idtr.Limit + 1);\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->MicrocodePatchRegionSize = PcdGet64 (PcdCpuMicrocodePatchRegionSize);\r
+ //\r
+ // If platform has more than one CPU, relocate microcode to memory to reduce\r
+ // loading microcode time.\r
+ //\r
+ MicrocodePatchInRam = NULL;\r
+ if (MaxLogicalProcessorNumber > 1) {\r
+ MicrocodePatchInRam = AllocatePages (\r
+ EFI_SIZE_TO_PAGES (\r
+ (UINTN)CpuMpData->MicrocodePatchRegionSize\r
+ )\r
+ );\r
+ }\r
+ if (MicrocodePatchInRam == NULL) {\r
+ //\r
+ // there is only one processor, or no microcode patch is available, or\r
+ // memory allocation failed\r
+ //\r
+ CpuMpData->MicrocodePatchAddress = PcdGet64 (PcdCpuMicrocodePatchAddress);\r
+ } else {\r
+ //\r
+ // there are multiple processors, and a microcode patch is available, and\r
+ // memory allocation succeeded\r
+ //\r
+ CopyMem (\r
+ MicrocodePatchInRam,\r
+ (VOID *)(UINTN)PcdGet64 (PcdCpuMicrocodePatchAddress),\r
+ (UINTN)CpuMpData->MicrocodePatchRegionSize\r
+ );\r
+ CpuMpData->MicrocodePatchAddress = (UINTN)MicrocodePatchInRam;\r
+ }\r
+\r
+ InitializeSpinLock(&CpuMpData->MpLock);\r
+\r
+ //\r
+ // Make sure no memory usage outside of the allocated buffer.\r
+ //\r
+ ASSERT ((CpuMpData->CpuInfoInHob + sizeof (CPU_INFO_IN_HOB) * MaxLogicalProcessorNumber) ==\r
+ Buffer + BufferSize);\r
+\r
+ //\r
+ // Duplicate BSP's IDT to APs.\r
+ // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1\r
+ //\r
+ CopyMem ((VOID *)ApIdtBase, (VOID *)VolatileRegisters.Idtr.Base, VolatileRegisters.Idtr.Limit + 1);\r
+ VolatileRegisters.Idtr.Base = ApIdtBase;\r
+ CopyMem (&CpuMpData->CpuData[0].VolatileRegisters, &VolatileRegisters, sizeof (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