#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
+ NULL, // 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
+ 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 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
\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