#include "CpuDxe.h"\r
#include "CpuMp.h"\r
\r
+UINTN gMaxLogicalProcessorNumber;\r
+UINTN gApStackSize;\r
+\r
+MP_SYSTEM_DATA mMpSystemData;\r
+\r
VOID *mCommonStack = 0;\r
VOID *mTopOfApCommonStack = 0;\r
+VOID *mApStackStart = 0;\r
+\r
+EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = {\r
+ GetNumberOfProcessors,\r
+ GetProcessorInfo,\r
+ NULL, // StartupAllAPs,\r
+ NULL, // StartupThisAP,\r
+ NULL, // SwitchBSP,\r
+ EnableDisableAP,\r
+ WhoAmI\r
+};\r
+\r
+/**\r
+ Check whether caller processor is BSP.\r
+\r
+ @retval TRUE the caller is BSP\r
+ @retval FALSE the caller is AP\r
+\r
+**/\r
+BOOLEAN\r
+IsBSP (\r
+ VOID\r
+ )\r
+{\r
+ UINTN CpuIndex;\r
+ CPU_DATA_BLOCK *CpuData;\r
+\r
+ CpuData = NULL;\r
+\r
+ WhoAmI (&mMpServicesTemplate, &CpuIndex);\r
+ CpuData = &mMpSystemData.CpuDatas[CpuIndex];\r
+\r
+ return CpuData->Info.StatusFlag & PROCESSOR_AS_BSP_BIT ? TRUE : FALSE;\r
+}\r
+\r
+/**\r
+ Get the Application Processors state.\r
+\r
+ @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
+\r
+ @retval CPU_STATE the AP status\r
+\r
+**/\r
+CPU_STATE\r
+GetApState (\r
+ IN CPU_DATA_BLOCK *CpuData\r
+ )\r
+{\r
+ CPU_STATE State;\r
+\r
+ while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+ CpuPause ();\r
+ }\r
+\r
+ State = CpuData->State;\r
+ ReleaseSpinLock (&CpuData->CpuDataLock);\r
+\r
+ return State;\r
+}\r
+\r
+/**\r
+ Check the Application Processors Status whether contains the Flags.\r
+\r
+ @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
+ @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
+\r
+ @retval TRUE the AP status includes the StatusFlag\r
+ @retval FALSE the AP status excludes the StatusFlag\r
+\r
+**/\r
+BOOLEAN\r
+TestCpuStatusFlag (\r
+ IN CPU_DATA_BLOCK *CpuData,\r
+ IN UINT32 Flags\r
+ )\r
+{\r
+ UINT32 Ret;\r
+\r
+ while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+ CpuPause ();\r
+ }\r
+\r
+ Ret = CpuData->Info.StatusFlag & Flags;\r
+ ReleaseSpinLock (&CpuData->CpuDataLock);\r
+\r
+ return !!(Ret);\r
+}\r
+\r
+/**\r
+ Bitwise-Or of the Application Processors Status with the Flags.\r
+\r
+ @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
+ @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
\r
+**/\r
+VOID\r
+CpuStatusFlagOr (\r
+ IN CPU_DATA_BLOCK *CpuData,\r
+ IN UINT32 Flags\r
+ )\r
+{\r
+ while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+ CpuPause ();\r
+ }\r
+\r
+ CpuData->Info.StatusFlag |= Flags;\r
+ ReleaseSpinLock (&CpuData->CpuDataLock);\r
+}\r
+\r
+/**\r
+ Bitwise-AndNot of the Application Processors Status with the Flags.\r
+\r
+ @param CpuData the pointer to CPU_DATA_BLOCK of specified AP\r
+ @param Flags the StatusFlag describing in EFI_PROCESSOR_INFORMATION\r
+\r
+**/\r
+VOID\r
+CpuStatusFlagAndNot (\r
+ IN CPU_DATA_BLOCK *CpuData,\r
+ IN UINT32 Flags\r
+ )\r
+{\r
+ while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) {\r
+ CpuPause ();\r
+ }\r
+\r
+ CpuData->Info.StatusFlag &= ~Flags;\r
+ ReleaseSpinLock (&CpuData->CpuDataLock);\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
+ This service may only be called from the BSP.\r
+\r
+ This function is used to retrieve the following information:\r
+ - The number of logical processors that are present in the system.\r
+ - The number of enabled logical processors in the system at the instant\r
+ this call is made.\r
+\r
+ Because MP Service Protocol provides services to enable and disable processors\r
+ dynamically, the number of enabled logical processors may vary during the\r
+ course of a boot session.\r
+\r
+ If this service is called from an AP, then EFI_DEVICE_ERROR is returned.\r
+ If NumberOfProcessors or NumberOfEnabledProcessors is NULL, then\r
+ EFI_INVALID_PARAMETER is returned. Otherwise, the total number of processors\r
+ is returned in NumberOfProcessors, the number of currently enabled processor\r
+ is returned in NumberOfEnabledProcessors, and EFI_SUCCESS is returned.\r
+\r
+ @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
+ instance.\r
+ @param[out] NumberOfProcessors Pointer to the total number of logical\r
+ processors in the system, including the BSP\r
+ and disabled APs.\r
+ @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical\r
+ processors that exist in system, including\r
+ the BSP.\r
+\r
+ @retval EFI_SUCCESS The number of logical processors and enabled\r
+ logical processors was retrieved.\r
+ @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
+ @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL.\r
+ @retval EFI_INVALID_PARAMETER NumberOfEnabledProcessors is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetNumberOfProcessors (\r
+ IN EFI_MP_SERVICES_PROTOCOL *This,\r
+ OUT UINTN *NumberOfProcessors,\r
+ OUT UINTN *NumberOfEnabledProcessors\r
+ )\r
+{\r
+ if ((NumberOfProcessors == NULL) || (NumberOfEnabledProcessors == NULL)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (!IsBSP ()) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ *NumberOfProcessors = mMpSystemData.NumberOfProcessors;\r
+ *NumberOfEnabledProcessors = mMpSystemData.NumberOfEnabledProcessors;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Gets detailed MP-related information on the requested processor at the\r
+ instant this call is made. This service may only be called from the BSP.\r
+\r
+ This service retrieves detailed MP-related information about any processor\r
+ on the platform. Note the following:\r
+ - The processor information may change during the course of a boot session.\r
+ - The information presented here is entirely MP related.\r
+\r
+ Information regarding the number of caches and their sizes, frequency of operation,\r
+ slot numbers is all considered platform-related information and is not provided\r
+ by this service.\r
+\r
+ @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL\r
+ instance.\r
+ @param[in] ProcessorNumber The handle number of processor.\r
+ @param[out] ProcessorInfoBuffer A pointer to the buffer where information for\r
+ the requested processor is deposited.\r
+\r
+ @retval EFI_SUCCESS Processor information was returned.\r
+ @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
+ @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.\r
+ @retval EFI_NOT_FOUND The processor with the handle specified by\r
+ ProcessorNumber does not exist in the platform.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+GetProcessorInfo (\r
+ IN EFI_MP_SERVICES_PROTOCOL *This,\r
+ IN UINTN ProcessorNumber,\r
+ OUT EFI_PROCESSOR_INFORMATION *ProcessorInfoBuffer\r
+ )\r
+{\r
+ if (ProcessorInfoBuffer == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (!IsBSP ()) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ if (ProcessorNumber >= mMpSystemData.NumberOfProcessors) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ CopyMem (ProcessorInfoBuffer, &mMpSystemData.CpuDatas[ProcessorNumber], sizeof (EFI_PROCESSOR_INFORMATION));\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
+\r
+ This service allows the caller enable or disable an AP from this point onward.\r
+ The caller can optionally specify the health status of the AP by Health. If\r
+ an AP is being disabled, then the state of the disabled AP is implementation\r
+ dependent. If an AP is enabled, then the implementation must guarantee that a\r
+ complete initialization sequence is performed on the AP, so the AP is in a state\r
+ that is compatible with an MP operating system. This service may not be supported\r
+ after the UEFI Event EFI_EVENT_GROUP_READY_TO_BOOT is signaled.\r
+\r
+ If the enable or disable AP operation cannot be completed prior to the return\r
+ from this service, then EFI_UNSUPPORTED must be returned.\r
+\r
+ @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
+ @param[in] ProcessorNumber The handle number of AP that is to become the new\r
+ BSP. The range is from 0 to the total number of\r
+ logical processors minus 1. The total number of\r
+ logical processors can be retrieved by\r
+ EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
+ @param[in] EnableAP Specifies the new state for the processor for\r
+ enabled, FALSE for disabled.\r
+ @param[in] HealthFlag If not NULL, a pointer to a value that specifies\r
+ the new health status of the AP. This flag\r
+ corresponds to StatusFlag defined in\r
+ EFI_MP_SERVICES_PROTOCOL.GetProcessorInfo(). Only\r
+ the PROCESSOR_HEALTH_STATUS_BIT is used. All other\r
+ bits are ignored. If it is NULL, this parameter\r
+ is ignored.\r
+\r
+ @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.\r
+ @retval EFI_UNSUPPORTED Enabling or disabling an AP cannot be completed\r
+ prior to this service returning.\r
+ @retval EFI_UNSUPPORTED Enabling or disabling an AP is not supported.\r
+ @retval EFI_DEVICE_ERROR The calling processor is an AP.\r
+ @retval EFI_NOT_FOUND Processor with the handle specified by ProcessorNumber\r
+ does not exist.\r
+ @retval EFI_INVALID_PARAMETER ProcessorNumber specifies the BSP.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+EnableDisableAP (\r
+ IN EFI_MP_SERVICES_PROTOCOL *This,\r
+ IN UINTN ProcessorNumber,\r
+ IN BOOLEAN EnableAP,\r
+ IN UINT32 *HealthFlag OPTIONAL\r
+ )\r
+{\r
+ CPU_DATA_BLOCK *CpuData;\r
+\r
+ if (!IsBSP ()) {\r
+ return EFI_DEVICE_ERROR;\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
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (GetApState (CpuData) != CpuStateIdle) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ if (EnableAP) {\r
+ if (!(TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT))) {\r
+ mMpSystemData.NumberOfEnabledProcessors++;\r
+ }\r
+ CpuStatusFlagOr (CpuData, PROCESSOR_ENABLED_BIT);\r
+ } else {\r
+ if (TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) {\r
+ mMpSystemData.NumberOfEnabledProcessors--;\r
+ }\r
+ CpuStatusFlagAndNot (CpuData, PROCESSOR_ENABLED_BIT);\r
+ }\r
+\r
+ if (HealthFlag != NULL) {\r
+ CpuStatusFlagAndNot (CpuData, (UINT32)~PROCESSOR_HEALTH_STATUS_BIT);\r
+ CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT));\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ This return the handle number for the calling processor. This service may be\r
+ called from the BSP and APs.\r
+\r
+ This service returns the processor handle number for the calling processor.\r
+ The returned value is in the range from 0 to the total number of logical\r
+ processors minus 1. The total number of logical processors can be retrieved\r
+ with EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors(). This service may be\r
+ called from the BSP and APs. If ProcessorNumber is NULL, then EFI_INVALID_PARAMETER\r
+ is returned. Otherwise, the current processors handle number is returned in\r
+ ProcessorNumber, and EFI_SUCCESS is returned.\r
+\r
+ @param[in] This A pointer to the EFI_MP_SERVICES_PROTOCOL instance.\r
+ @param[out] ProcessorNumber The handle number of AP that is to become the new\r
+ BSP. The range is from 0 to the total number of\r
+ logical processors minus 1. The total number of\r
+ logical processors can be retrieved by\r
+ EFI_MP_SERVICES_PROTOCOL.GetNumberOfProcessors().\r
+\r
+ @retval EFI_SUCCESS The current processor handle number was returned\r
+ in ProcessorNumber.\r
+ @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+WhoAmI (\r
+ IN EFI_MP_SERVICES_PROTOCOL *This,\r
+ OUT UINTN *ProcessorNumber\r
+ )\r
+{\r
+ UINTN Index;\r
+ UINT32 ProcessorId;\r
+\r
+ if (ProcessorNumber == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ ProcessorId = GetApicId ();\r
+ for (Index = 0; Index < mMpSystemData.NumberOfProcessors; Index++) {\r
+ if (mMpSystemData.CpuDatas[Index].Info.ProcessorId == ProcessorId) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ *ProcessorNumber = Index;\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Application Processors do loop routine\r
+ after switch to its own stack.\r
+\r
+ @param Context1 A pointer to the context to pass into the function.\r
+ @param Context2 A pointer to the context to pass into the function.\r
+\r
+**/\r
+VOID\r
+ProcessorToIdleState (\r
+ IN VOID *Context1, OPTIONAL\r
+ IN VOID *Context2 OPTIONAL\r
+ )\r
+{\r
+ DEBUG ((DEBUG_INFO, "Ap apicid is %d\n", GetApicId ()));\r
+\r
+ AsmApDoneWithCommonStack ();\r
+\r
+ CpuSleep ();\r
+ CpuDeadLoop ();\r
+}\r
\r
/**\r
Application Processor C code entry point.\r
VOID\r
)\r
{\r
+ VOID* TopOfApStack;\r
+\r
+ FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors);\r
+ TopOfApStack = (UINT8*)mApStackStart + gApStackSize;\r
+ mApStackStart = TopOfApStack;\r
+\r
+ mMpSystemData.NumberOfProcessors++;\r
+\r
+ SwitchStack (\r
+ (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState,\r
+ NULL,\r
+ NULL,\r
+ TopOfApStack);\r
}\r
\r
+/**\r
+ This function is called by all processors (both BSP and AP) once and collects MP related data.\r
+\r
+ @param Bsp TRUE if the CPU is BSP\r
+ @param ProcessorNumber The specific processor number\r
+\r
+ @retval EFI_SUCCESS Data for the processor collected and filled in\r
+\r
+**/\r
+EFI_STATUS\r
+FillInProcessorInformation (\r
+ IN BOOLEAN Bsp,\r
+ IN UINTN ProcessorNumber\r
+ )\r
+{\r
+ CPU_DATA_BLOCK *CpuData;\r
+ UINT32 ProcessorId;\r
+\r
+ CpuData = &mMpSystemData.CpuDatas[ProcessorNumber];\r
+ ProcessorId = GetApicId ();\r
+ CpuData->Info.ProcessorId = ProcessorId;\r
+ CpuData->Info.StatusFlag = PROCESSOR_ENABLED_BIT | PROCESSOR_HEALTH_STATUS_BIT;\r
+ if (Bsp) {\r
+ CpuData->Info.StatusFlag |= PROCESSOR_AS_BSP_BIT;\r
+ }\r
+ CpuData->Info.Location.Package = ProcessorId;\r
+ CpuData->Info.Location.Core = 0;\r
+ CpuData->Info.Location.Thread = 0;\r
+ CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle;\r
+\r
+ CpuData->Procedure = NULL;\r
+ CpuData->Parameter = NULL;\r
+ InitializeSpinLock (&CpuData->CpuDataLock);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Prepare the System Data.\r
+\r
+ @retval EFI_SUCCESS the System Data finished initilization.\r
+\r
+**/\r
+EFI_STATUS\r
+InitMpSystemData (\r
+ VOID\r
+ )\r
+{\r
+ ZeroMem (&mMpSystemData, sizeof (MP_SYSTEM_DATA));\r
+\r
+ mMpSystemData.NumberOfProcessors = 1;\r
+ mMpSystemData.NumberOfEnabledProcessors = 1;\r
+\r
+ mMpSystemData.CpuDatas = AllocateZeroPool (sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber);\r
+ ASSERT(mMpSystemData.CpuDatas != NULL);\r
+\r
+ //\r
+ // BSP\r
+ //\r
+ FillInProcessorInformation (TRUE, 0);\r
+\r
+ return EFI_SUCCESS;\r
+}\r
\r
/**\r
Initialize Multi-processor support.\r
VOID\r
)\r
{\r
-}\r
+ gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber);\r
+ if (gMaxLogicalProcessorNumber < 1) {\r
+ DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n"));\r
+ return;\r
+ }\r
+\r
+ if (gMaxLogicalProcessorNumber == 1) {\r
+ return;\r
+ }\r
+\r
+ gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize);\r
+ ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0);\r
\r
+ mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
+ ASSERT (mApStackStart != NULL);\r
+\r
+ //\r
+ // the first buffer of stack size used for common stack, when the amount of AP\r
+ // more than 1, we should never free the common stack which maybe used for AP reset.\r
+ //\r
+ mCommonStack = mApStackStart;\r
+ mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize;\r
+ mApStackStart = mTopOfApCommonStack;\r
+\r
+ InitMpSystemData ();\r
+\r
+ if (mMpSystemData.NumberOfProcessors == 1) {\r
+ FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize));\r
+ return;\r
+ }\r
+\r
+ if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) {\r
+ FreePages (mApStackStart, EFI_SIZE_TO_PAGES (\r
+ (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) *\r
+ gApStackSize));\r
+ }\r
+}\r