\r
UINTN gMaxLogicalProcessorNumber;\r
UINTN gApStackSize;\r
+UINTN gPollInterval = 100; // 100 microseconds\r
\r
MP_SYSTEM_DATA mMpSystemData;\r
\r
GetNumberOfProcessors,\r
GetProcessorInfo,\r
NULL, // StartupAllAPs,\r
- NULL, // StartupThisAP,\r
+ StartupThisAP,\r
NULL, // SwitchBSP,\r
EnableDisableAP,\r
WhoAmI\r
return State;\r
}\r
\r
+/**\r
+ Set the Application Processors state.\r
+\r
+ @param CpuData The pointer to CPU_DATA_BLOCK of specified AP\r
+ @param State The AP status\r
+\r
+**/\r
+VOID\r
+SetApState (\r
+ IN CPU_DATA_BLOCK *CpuData,\r
+ IN CPU_STATE State\r
+ )\r
+{\r
+ while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+ CpuPause ();\r
+ }\r
+\r
+ CpuData->State = State;\r
+ ReleaseSpinLock (&CpuData->CpuDataLock);\r
+}\r
+\r
+/**\r
+ Set the Application Processor prepare to run a function specified\r
+ by Params.\r
+\r
+ @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
+ @param Procedure A pointer to the function to be run on enabled APs of the system\r
+ @param ProcedureArgument Pointer to the optional parameter of the assigned function\r
+\r
+**/\r
+VOID\r
+SetApProcedure (\r
+ IN CPU_DATA_BLOCK *CpuData,\r
+ IN EFI_AP_PROCEDURE Procedure,\r
+ IN VOID *ProcedureArgument\r
+ )\r
+{\r
+ while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+ CpuPause ();\r
+ }\r
+\r
+ CpuData->Parameter = ProcedureArgument;\r
+ CpuData->Procedure = Procedure;\r
+ ReleaseSpinLock (&CpuData->CpuDataLock);\r
+}\r
+\r
/**\r
Check the Application Processors Status whether contains the Flags.\r
\r
ReleaseSpinLock (&CpuData->CpuDataLock);\r
}\r
\r
+/**\r
+ Searches for the next blocking AP.\r
+\r
+ Search for the next AP that is put in blocking state by single-threaded StartupAllAPs().\r
+\r
+ @param NextNumber Pointer to the processor number of the next blocking AP.\r
+\r
+ @retval EFI_SUCCESS The next blocking AP has been found.\r
+ @retval EFI_NOT_FOUND No blocking AP exists.\r
+\r
+**/\r
+EFI_STATUS\r
+GetNextBlockedNumber (\r
+ OUT UINTN *NextNumber\r
+ )\r
+{\r
+ UINTN Number;\r
+ CPU_STATE CpuState;\r
+ CPU_DATA_BLOCK *CpuData;\r
+\r
+ for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) {\r
+ CpuData = &mMpSystemData.CpuDatas[Number];\r
+ if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) {\r
+ //\r
+ // Skip BSP\r
+ //\r
+ continue;\r
+ }\r
+\r
+ CpuState = GetApState (CpuData);\r
+ if (CpuState == CpuStateBlocked) {\r
+ *NextNumber = Number;\r
+ return EFI_SUCCESS;\r
+ }\r
+ }\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
/**\r
This service retrieves the number of logical processor in the platform\r
and the number of those logical processors that are enabled on this boot.\r
return EFI_SUCCESS;\r
}\r
\r
+/**\r
+ This service lets the caller get one enabled AP to execute a caller-provided\r
+ function. The caller can request the BSP to either wait for the completion\r
+ of the AP or just proceed with the next task by using the EFI event mechanism.\r
+ See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking\r
+ execution support. This service may only be called from the BSP.\r
+\r
+ This function is used to dispatch one enabled AP to the function specified by\r
+ Procedure passing in the argument specified by ProcedureArgument. If WaitEvent\r
+ is NULL, execution is in blocking mode. The BSP waits until the AP finishes or\r
+ TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.\r
+ BSP proceeds to the next task without waiting for the AP. If a non-blocking mode\r
+ is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,\r
+ then EFI_UNSUPPORTED must be returned.\r
+\r
+ If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
+ from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
+ available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and\r
+ EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
+\r
+ @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
+ instance.\r
+ @param[in] Procedure A pointer to the function to be run on\r
+ enabled APs of the system. See type\r
+ EFI_AP_PROCEDURE.\r
+ @param[in] ProcessorNumber The handle number of the AP. The range is\r
+ from 0 to the total number of logical\r
+ processors minus 1. The total number of\r
+ logical processors can be retrieved by\r
+ EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
+ @param[in] WaitEvent The event created by the caller with CreateEvent()\r
+ service. If it is NULL, then execute in\r
+ blocking mode. BSP waits until all APs finish\r
+ or TimeoutInMicroseconds expires. If it's\r
+ not NULL, then execute in non-blocking mode.\r
+ BSP requests the function specified by\r
+ Procedure to be started on all the enabled\r
+ APs, and go on executing immediately. If\r
+ all return from Procedure or TimeoutInMicroseconds\r
+ expires, this event is signaled. The BSP\r
+ can use the CheckEvent() or WaitForEvent()\r
+ services to check the state of event. Type\r
+ EFI_EVENT is defined in CreateEvent() in\r
+ the Unified Extensible Firmware Interface\r
+ Specification.\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. Zero means\r
+ infinity. If the timeout expires before\r
+ all APs return from Procedure, then Procedure\r
+ on the failed APs is terminated. All enabled\r
+ APs are available for next function assigned\r
+ by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
+ or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
+ If the timeout expires in blocking mode,\r
+ BSP returns EFI_TIMEOUT. If the timeout\r
+ expires in non-blocking mode, WaitEvent\r
+ is signaled with SignalEvent().\r
+ @param[in] ProcedureArgument The parameter passed into Procedure for\r
+ all APs.\r
+ @param[out] Finished If NULL, this parameter is ignored. In\r
+ blocking mode, this parameter is ignored.\r
+ In non-blocking mode, if AP returns from\r
+ Procedure before the timeout expires, its\r
+ content is set to TRUE. Otherwise, the\r
+ value is set to FALSE. The caller can\r
+ determine if the AP returned from Procedure\r
+ by evaluating this value.\r
+\r
+ @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
+ the timeout expires.\r
+ @retval EFI_SUCCESS In non-blocking mode, the function has been\r
+ dispatched to specified AP.\r
+ @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
+ UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
+ signaled.\r
+ @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
+ @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
+ the specified AP has finished.\r
+ @retval EFI_NOT_READY The specified AP is busy.\r
+ @retval EFI_NOT_FOUND The processor with the handle specified by\r
+ ProcessorNumber does not exist.\r
+ @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
+ @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+StartupThisAP (\r
+ IN EFI_MP_SERVICES_PROTOCOL *This,\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
+ CPU_DATA_BLOCK *CpuData;\r
+ EFI_STATUS Status;\r
+\r
+ CpuData = NULL;\r
+\r
+ if (Finished != NULL) {\r
+ *Finished = FALSE;\r
+ }\r
+\r
+ if (!IsBSP ()) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ if (Procedure == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
+ if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) ||\r
+ !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (GetApState (CpuData) != CpuStateIdle) {\r
+ return EFI_NOT_READY;\r
+ }\r
+\r
+ SetApState (CpuData, CpuStateReady);\r
+\r
+ SetApProcedure (CpuData, Procedure, ProcedureArgument);\r
+\r
+ CpuData->Timeout = TimeoutInMicroseconds;\r
+ CpuData->WaitEvent = WaitEvent;\r
+ CpuData->TimeoutActive = !!(TimeoutInMicroseconds);\r
+ CpuData->Finished = Finished;\r
+\r
+ if (WaitEvent != NULL) {\r
+ //\r
+ // Non Blocking\r
+ //\r
+ Status = gBS->SetTimer (\r
+ CpuData->CheckThisAPEvent,\r
+ TimerPeriodic,\r
+ EFI_TIMER_PERIOD_MICROSECONDS (100)\r
+ );\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Blocking\r
+ //\r
+ while (TRUE) {\r
+ if (GetApState (CpuData) == CpuStateFinished) {\r
+ SetApState (CpuData, CpuStateIdle);\r
+ break;\r
+ }\r
+\r
+ if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
+ ResetProcessorToIdleState (CpuData);\r
+ return EFI_TIMEOUT;\r
+ }\r
+\r
+ gBS->Stall (gPollInterval);\r
+ CpuData->Timeout -= gPollInterval;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
/**\r
This service lets the caller enable or disable an AP from this point onward.\r
This service may only be called from the BSP.\r
return EFI_SUCCESS;\r
}\r
\r
+/**\r
+ Terminate AP's task and set it to idle state.\r
+\r
+ This function terminates AP's task due to timeout by sending INIT-SIPI,\r
+ and sends it to idle state.\r
+\r
+ @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
+\r
+**/\r
+VOID\r
+ResetProcessorToIdleState (\r
+ IN CPU_DATA_BLOCK *CpuData\r
+ )\r
+{\r
+}\r
+\r
/**\r
Application Processors do loop routine\r
after switch to its own stack.\r
CpuDeadLoop ();\r
}\r
\r
+/**\r
+ Checks AP' status periodically.\r
+\r
+ This function is triggerred by timer perodically to check the\r
+ state of AP forStartupThisAP() executed in non-blocking mode.\r
+\r
+ @param Event Event triggered.\r
+ @param Context Parameter passed with the event.\r
+\r
+**/\r
+VOID\r
+EFIAPI\r
+CheckThisAPStatus (\r
+ IN EFI_EVENT Event,\r
+ IN VOID *Context\r
+ )\r
+{\r
+ CPU_DATA_BLOCK *CpuData;\r
+ CPU_STATE CpuState;\r
+\r
+ CpuData = (CPU_DATA_BLOCK *) Context;\r
+ if (CpuData->TimeoutActive) {\r
+ CpuData->Timeout -= gPollInterval;\r
+ }\r
+\r
+ CpuState = GetApState (CpuData);\r
+\r
+ if (CpuState == CpuStateFinished) {\r
+ if (CpuData->Finished) {\r
+ *CpuData->Finished = TRUE;\r
+ }\r
+ SetApState (CpuData, CpuStateIdle);\r
+ goto out;\r
+ }\r
+\r
+ if (CpuData->TimeoutActive && CpuData->Timeout < 0) {\r
+ if (CpuState != CpuStateIdle &&\r
+ CpuData->Finished) {\r
+ *CpuData->Finished = FALSE;\r
+ }\r
+ ResetProcessorToIdleState (CpuData);\r
+ goto out;\r
+ }\r
+\r
+ return;\r
+\r
+out:\r
+ gBS->SetTimer (CpuData->CheckThisAPEvent, TimerCancel, 0);\r
+ if (CpuData->WaitEvent) {\r
+ gBS->SignalEvent (CpuData->WaitEvent);\r
+ CpuData->WaitEvent = NULL;\r
+ }\r
+}\r
+\r
/**\r
Application Processor C code entry point.\r
\r
VOID\r
)\r
{\r
+ UINTN ProcessorNumber;\r
+ CPU_DATA_BLOCK *CpuData;\r
+ EFI_STATUS Status;\r
+\r
ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
\r
mMpSystemData.NumberOfProcessors = 1;\r
mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
ASSERT(mMpSystemData.CpuDatas != NULL);\r
\r
+ for (ProcessorNumber = 0; ProcessorNumber < gMaxLogicalProcessorNumber; ProcessorNumber++) {\r
+ CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
+ Status = gBS->CreateEvent (\r
+ EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
+ TPL_CALLBACK,\r
+ CheckThisAPStatus,\r
+ (VOID *) CpuData,\r
+ &CpuData->CheckThisAPEvent\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+ }\r
+\r
//\r
// BSP\r
//\r
\r
EFI_AP_PROCEDURE Procedure;\r
VOID *Parameter;\r
+ BOOLEAN *Finished;\r
+ INTN Timeout;\r
+ EFI_EVENT WaitEvent;\r
+ BOOLEAN TimeoutActive;\r
+ EFI_EVENT CheckThisAPEvent;\r
} CPU_DATA_BLOCK;\r
\r
/**\r
OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
);\r
\r
+/**\r
+ This service lets the caller get one enabled AP to execute a caller-provided\r
+ function. The caller can request the BSP to either wait for the completion\r
+ of the AP or just proceed with the next task by using the EFI event mechanism.\r
+ See EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() for more details on non-blocking\r
+ execution support. This service may only be called from the BSP.\r
+\r
+ This function is used to dispatch one enabled AP to the function specified by\r
+ Procedure passing in the argument specified by ProcedureArgument. If WaitEvent\r
+ is NULL, execution is in blocking mode. The BSP waits until the AP finishes or\r
+ TimeoutInMicroSecondss expires. Otherwise, execution is in non-blocking mode.\r
+ BSP proceeds to the next task without waiting for the AP. If a non-blocking mode\r
+ is requested after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled,\r
+ then EFI_UNSUPPORTED must be returned.\r
+\r
+ If the timeout specified by TimeoutInMicroseconds expires before the AP returns\r
+ from Procedure, then execution of Procedure by the AP is terminated. The AP is\r
+ available for subsequent calls to EFI_MP_SERVICES_PROTOCOL.StartupAllAPs() and\r
+ EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
+\r
+ @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
+ instance.\r
+ @param[in] Procedure A pointer to the function to be run on\r
+ enabled APs of the system. See type\r
+ EFI_AP_PROCEDURE.\r
+ @param[in] ProcessorNumber The handle number of the AP. The range is\r
+ from 0 to the total number of logical\r
+ processors minus 1. The total number of\r
+ logical processors can be retrieved by\r
+ EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
+ @param[in] WaitEvent The event created by the caller with CreateEvent()\r
+ service. If it is NULL, then execute in\r
+ blocking mode. BSP waits until all APs finish\r
+ or TimeoutInMicroseconds expires. If it's\r
+ not NULL, then execute in non-blocking mode.\r
+ BSP requests the function specified by\r
+ Procedure to be started on all the enabled\r
+ APs, and go on executing immediately. If\r
+ all return from Procedure or TimeoutInMicroseconds\r
+ expires, this event is signaled. The BSP\r
+ can use the CheckEvent() or WaitForEvent()\r
+ services to check the state of event. Type\r
+ EFI_EVENT is defined in CreateEvent() in\r
+ the Unified Extensible Firmware Interface\r
+ Specification.\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. Zero means\r
+ infinity. If the timeout expires before\r
+ all APs return from Procedure, then Procedure\r
+ on the failed APs is terminated. All enabled\r
+ APs are available for next function assigned\r
+ by EFI_MP_SERVICES_PROTOCOL.StartupAllAPs()\r
+ or EFI_MP_SERVICES_PROTOCOL.StartupThisAP().\r
+ If the timeout expires in blocking mode,\r
+ BSP returns EFI_TIMEOUT. If the timeout\r
+ expires in non-blocking mode, WaitEvent\r
+ is signaled with SignalEvent().\r
+ @param[in] ProcedureArgument The parameter passed into Procedure for\r
+ all APs.\r
+ @param[out] Finished If NULL, this parameter is ignored. In\r
+ blocking mode, this parameter is ignored.\r
+ In non-blocking mode, if AP returns from\r
+ Procedure before the timeout expires, its\r
+ content is set to TRUE. Otherwise, the\r
+ value is set to FALSE. The caller can\r
+ determine if the AP returned from Procedure\r
+ by evaluating this value.\r
+\r
+ @retval EFI_SUCCESS In blocking mode, specified AP finished before\r
+ the timeout expires.\r
+ @retval EFI_SUCCESS In non-blocking mode, the function has been\r
+ dispatched to specified AP.\r
+ @retval EFI_UNSUPPORTED A non-blocking mode request was made after the\r
+ UEFI event EFI_EVENT_GROUP_READY_TO_BOOT was\r
+ signaled.\r
+ @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
+ @retval EFI_TIMEOUT In blocking mode, the timeout expired before\r
+ the specified AP has finished.\r
+ @retval EFI_NOT_READY The specified AP is busy.\r
+ @retval EFI_NOT_FOUND The processor with the handle specified by\r
+ ProcessorNumber does not exist.\r
+ @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP or disabled AP.\r
+ @retval EFI_INVALID_PARAMETER Procedure is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+StartupThisAP (\r
+ IN EFI_MP_SERVICES_PROTOCOL *This,\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
/**\r
This service lets the caller enable or disable an AP from this point onward.\r
This service may only be called from the BSP.\r
OUT UINTN *ProcessorNumber\r
);\r
\r
+/**\r
+ Terminate AP's task and set it to idle state.\r
+\r
+ This function terminates AP's task due to timeout by sending INIT-SIPI,\r
+ and sends it to idle state.\r
+\r
+ @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
+\r
+**/\r
+VOID\r
+ResetProcessorToIdleState (\r
+ IN CPU_DATA_BLOCK *CpuData\r
+ );\r
+\r
#endif // _CPU_MP_H_\r
\r