X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=UefiCpuPkg%2FCpuDxe%2FCpuMp.c;h=da3686e2788e8c50f0652bd743a3a010c42e5a2e;hp=3e30d7417d4f0ea88739911544cfb2984e30ba41;hb=944f45ae2f7ecbff2c66622d15d52ffbc3455bfb;hpb=e4aaf764281d12cb8bfe605393a5520e00715838 diff --git a/UefiCpuPkg/CpuDxe/CpuMp.c b/UefiCpuPkg/CpuDxe/CpuMp.c index 3e30d7417d..da3686e278 100644 --- a/UefiCpuPkg/CpuDxe/CpuMp.c +++ b/UefiCpuPkg/CpuDxe/CpuMp.c @@ -1,7 +1,7 @@ /** @file CPU DXE Module. - Copyright (c) 2008 - 2014, Intel Corporation. All rights reserved.
+ Copyright (c) 2008 - 2015, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -20,11 +20,14 @@ UINTN gApStackSize; UINTN gPollInterval = 100; // 100 microseconds MP_SYSTEM_DATA mMpSystemData; +EFI_HANDLE mMpServiceHandle = NULL; +EFI_EVENT mExitBootServicesEvent = (EFI_EVENT)NULL; VOID *mCommonStack = 0; VOID *mTopOfApCommonStack = 0; VOID *mApStackStart = 0; +volatile BOOLEAN mAPsAlreadyInitFinished = FALSE; volatile BOOLEAN mStopCheckAllAPsStatus = TRUE; EFI_MP_SERVICES_PROTOCOL mMpServicesTemplate = { @@ -51,6 +54,7 @@ GetMpSpinLock ( while (!AcquireSpinLockOrFail (&CpuData->CpuDataLock)) { CpuPause (); } + CpuData->LockSelf = GetApicId (); } /** @@ -174,7 +178,7 @@ TestCpuStatusFlag ( Ret = CpuData->Info.StatusFlag & Flags; ReleaseMpSpinLock (CpuData); - return !!(Ret); + return (BOOLEAN) (Ret != 0); } /** @@ -294,9 +298,15 @@ CheckAndUpdateAllAPsToIdleState ( SetApProcedure (&mMpSystemData.CpuDatas[NextNumber], mMpSystemData.Procedure, mMpSystemData.ProcedureArgument); + // + // If this AP previous state is blocked, we should + // wake up this AP by sent a SIPI. and avoid + // re-involve the sleeping state. we must call + // SetApProcedure() first. + // + ResetProcessorToIdleState (&mMpSystemData.CpuDatas[NextNumber]); } } - SetApState (CpuData, CpuStateIdle); } } @@ -339,7 +349,8 @@ ResetAllFailedAPs ( } CpuState = GetApState (CpuData); - if (CpuState != CpuStateIdle) { + if (CpuState != CpuStateIdle && + CpuState != CpuStateSleeping) { if (mMpSystemData.FailedList != NULL) { (*mMpSystemData.FailedList)[mMpSystemData.FailedListIndex++] = Number; } @@ -611,6 +622,7 @@ StartupAllAPs ( CPU_DATA_BLOCK *CpuData; UINTN Number; CPU_STATE APInitialState; + CPU_STATE CpuState; CpuData = NULL; @@ -630,6 +642,11 @@ StartupAllAPs ( return EFI_INVALID_PARAMETER; } + // + // temporarily stop checkAllAPsStatus for avoid resource dead-lock. + // + mStopCheckAllAPsStatus = TRUE; + for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) { CpuData = &mMpSystemData.CpuDatas[Number]; if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) { @@ -646,21 +663,18 @@ StartupAllAPs ( continue; } - if (GetApState (CpuData) != CpuStateIdle) { + CpuState = GetApState (CpuData); + if (CpuState != CpuStateIdle && + CpuState != CpuStateSleeping) { return EFI_NOT_READY; } } - // - // temporarily stop checkAllAPsStatus for initialize parameters. - // - mStopCheckAllAPsStatus = TRUE; - mMpSystemData.Procedure = Procedure; mMpSystemData.ProcedureArgument = ProcedureArgument; mMpSystemData.WaitEvent = WaitEvent; mMpSystemData.Timeout = TimeoutInMicroseconds; - mMpSystemData.TimeoutActive = !!(TimeoutInMicroseconds); + mMpSystemData.TimeoutActive = (BOOLEAN) (TimeoutInMicroseconds != 0); mMpSystemData.FinishCount = 0; mMpSystemData.StartCount = 0; mMpSystemData.SingleThread = SingleThread; @@ -690,13 +704,24 @@ StartupAllAPs ( // state 1 by 1, until the previous 1 finished its task // if not "SingleThread", all APs are put to ready state from the beginning // - if (GetApState (CpuData) == CpuStateIdle) { + CpuState = GetApState (CpuData); + if (CpuState == CpuStateIdle || + CpuState == CpuStateSleeping) { mMpSystemData.StartCount++; SetApState (CpuData, APInitialState); if (APInitialState == CpuStateReady) { SetApProcedure (CpuData, Procedure, ProcedureArgument); + // + // If this AP previous state is Sleeping, we should + // wake up this AP by sent a SIPI. and avoid + // re-involve the sleeping state. we must call + // SetApProcedure() first. + // + if (CpuState == CpuStateSleeping) { + ResetProcessorToIdleState (CpuData); + } } if (SingleThread) { @@ -714,6 +739,11 @@ StartupAllAPs ( return EFI_SUCCESS; } + // + // Blocking temporarily stop CheckAllAPsStatus() + // + mStopCheckAllAPsStatus = TRUE; + while (TRUE) { CheckAndUpdateAllAPsToIdleState (); if (mMpSystemData.FinishCount == mMpSystemData.StartCount) { @@ -838,6 +868,7 @@ StartupThisAP ( ) { CPU_DATA_BLOCK *CpuData; + CPU_STATE CpuState; CpuData = NULL; @@ -857,28 +888,39 @@ StartupThisAP ( return EFI_NOT_FOUND; } + // + // temporarily stop checkAllAPsStatus for avoid resource dead-lock. + // + mStopCheckAllAPsStatus = TRUE; + CpuData = &mMpSystemData.CpuDatas[ProcessorNumber]; if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT) || !TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) { return EFI_INVALID_PARAMETER; } - if (GetApState (CpuData) != CpuStateIdle) { + CpuState = GetApState (CpuData); + if (CpuState != CpuStateIdle && + CpuState != CpuStateSleeping) { return EFI_NOT_READY; } - // - // temporarily stop checkAllAPsStatus for initialize parameters. - // - mStopCheckAllAPsStatus = TRUE; - SetApState (CpuData, CpuStateReady); SetApProcedure (CpuData, Procedure, ProcedureArgument); + // + // If this AP previous state is Sleeping, we should + // wake up this AP by sent a SIPI. and avoid + // re-involve the sleeping state. we must call + // SetApProcedure() first. + // + if (CpuState == CpuStateSleeping) { + ResetProcessorToIdleState (CpuData); + } CpuData->Timeout = TimeoutInMicroseconds; CpuData->WaitEvent = WaitEvent; - CpuData->TimeoutActive = !!(TimeoutInMicroseconds); + CpuData->TimeoutActive = (BOOLEAN) (TimeoutInMicroseconds != 0); CpuData->Finished = Finished; mStopCheckAllAPsStatus = FALSE; @@ -1011,6 +1053,11 @@ EnableDisableAP ( ) { CPU_DATA_BLOCK *CpuData; + BOOLEAN TempStopCheckState; + CPU_STATE CpuState; + + CpuData = NULL; + TempStopCheckState = FALSE; if (!IsBSP ()) { return EFI_DEVICE_ERROR; @@ -1020,12 +1067,22 @@ EnableDisableAP ( return EFI_NOT_FOUND; } + // + // temporarily stop checkAllAPsStatus for initialize parameters. + // + if (!mStopCheckAllAPsStatus) { + mStopCheckAllAPsStatus = TRUE; + TempStopCheckState = TRUE; + } + CpuData = &mMpSystemData.CpuDatas[ProcessorNumber]; if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) { return EFI_INVALID_PARAMETER; } - if (GetApState (CpuData) != CpuStateIdle) { + CpuState = GetApState (CpuData); + if (CpuState != CpuStateIdle && + CpuState != CpuStateSleeping) { return EFI_UNSUPPORTED; } @@ -1046,6 +1103,10 @@ EnableDisableAP ( CpuStatusFlagOr (CpuData, (*HealthFlag & PROCESSOR_HEALTH_STATUS_BIT)); } + if (TempStopCheckState) { + mStopCheckAllAPsStatus = FALSE; + } + return EFI_SUCCESS; } @@ -1112,6 +1173,7 @@ ResetProcessorToIdleState ( IN CPU_DATA_BLOCK *CpuData ) { + ResetApStackless ((UINT32)CpuData->Info.ProcessorId); } /** @@ -1131,12 +1193,34 @@ ProcessorToIdleState ( UINTN ProcessorNumber; CPU_DATA_BLOCK *CpuData; EFI_AP_PROCEDURE Procedure; - VOID *ProcedureArgument; + volatile VOID *ProcedureArgument; + + AsmApDoneWithCommonStack (); + + while (!mAPsAlreadyInitFinished) { + CpuPause (); + } WhoAmI (&mMpServicesTemplate, &ProcessorNumber); CpuData = &mMpSystemData.CpuDatas[ProcessorNumber]; - AsmApDoneWithCommonStack (); + // + // Avoid forcibly reset AP caused the AP got lock not release. + // + if (CpuData->LockSelf == (INTN) GetApicId ()) { + ReleaseSpinLock (&CpuData->CpuDataLock); + } + + // + // Avoid forcibly reset AP caused the timeout AP State is not + // updated. + // + GetMpSpinLock (CpuData); + if (CpuData->State == CpuStateBusy) { + CpuData->Procedure = NULL; + } + CpuData->State = CpuStateIdle; + ReleaseMpSpinLock (CpuData); while (TRUE) { GetMpSpinLock (CpuData); @@ -1145,12 +1229,28 @@ ProcessorToIdleState ( ReleaseMpSpinLock (CpuData); if (Procedure != NULL) { - Procedure (ProcedureArgument); + SetApState (CpuData, CpuStateBusy); + + Procedure ((VOID*) ProcedureArgument); GetMpSpinLock (CpuData); CpuData->Procedure = NULL; CpuData->State = CpuStateFinished; ReleaseMpSpinLock (CpuData); + } else { + // + // if no procedure to execution, we simply put AP + // into sleeping state, and waiting BSP sent SIPI. + // + GetMpSpinLock (CpuData); + if (CpuData->State == CpuStateIdle) { + CpuData->State = CpuStateSleeping; + } + ReleaseMpSpinLock (CpuData); + } + + if (GetApState (CpuData) == CpuStateSleeping) { + CpuSleep (); } CpuPause (); @@ -1281,20 +1381,6 @@ CheckAllAPsStatus ( // for (Number = 0; Number < mMpSystemData.NumberOfProcessors; Number++) { CpuData = &mMpSystemData.CpuDatas[Number]; - if (TestCpuStatusFlag (CpuData, PROCESSOR_AS_BSP_BIT)) { - // - // Skip BSP - // - continue; - } - - if (!TestCpuStatusFlag (CpuData, PROCESSOR_ENABLED_BIT)) { - // - // Skip Disabled processors - // - continue; - } - if (CpuData->WaitEvent) { CheckThisAPStatus (NULL, (VOID *)CpuData); } @@ -1319,13 +1405,27 @@ ApEntryPointInC ( VOID ) { - VOID* TopOfApStack; + VOID* TopOfApStack; + UINTN ProcessorNumber; - FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors); - TopOfApStack = (UINT8*)mApStackStart + gApStackSize; - mApStackStart = TopOfApStack; + if (!mAPsAlreadyInitFinished) { + FillInProcessorInformation (FALSE, mMpSystemData.NumberOfProcessors); + TopOfApStack = (UINT8*)mApStackStart + gApStackSize; + mApStackStart = TopOfApStack; - mMpSystemData.NumberOfProcessors++; + // + // Store the Stack address, when reset the AP, We can found the original address. + // + mMpSystemData.CpuDatas[mMpSystemData.NumberOfProcessors].TopOfStack = TopOfApStack; + mMpSystemData.NumberOfProcessors++; + mMpSystemData.NumberOfEnabledProcessors++; + } else { + WhoAmI (&mMpServicesTemplate, &ProcessorNumber); + // + // Get the original stack address. + // + TopOfApStack = mMpSystemData.CpuDatas[ProcessorNumber].TopOfStack; + } SwitchStack ( (SWITCH_STACK_ENTRY_POINT)(UINTN)ProcessorToIdleState, @@ -1362,11 +1462,12 @@ FillInProcessorInformation ( CpuData->Info.Location.Package = ProcessorId; CpuData->Info.Location.Core = 0; CpuData->Info.Location.Thread = 0; - CpuData->State = Bsp ? CpuStateBuzy : CpuStateIdle; + CpuData->State = Bsp ? CpuStateBusy : CpuStateIdle; CpuData->Procedure = NULL; CpuData->Parameter = NULL; InitializeSpinLock (&CpuData->CpuDataLock); + CpuData->LockSelf = -1; return EFI_SUCCESS; } @@ -1419,6 +1520,111 @@ InitMpSystemData ( return EFI_SUCCESS; } +/** + Collects BIST data from HOB. + + This function collects BIST data from HOB built from Sec Platform Information + PPI or SEC Platform Information2 PPI. + +**/ +VOID +CollectBistDataFromHob ( + VOID + ) +{ + EFI_HOB_GUID_TYPE *GuidHob; + EFI_SEC_PLATFORM_INFORMATION_RECORD2 *SecPlatformInformation2; + EFI_SEC_PLATFORM_INFORMATION_RECORD *SecPlatformInformation; + UINTN NumberOfData; + EFI_SEC_PLATFORM_INFORMATION_CPU *CpuInstance; + EFI_SEC_PLATFORM_INFORMATION_CPU BspCpuInstance; + UINTN ProcessorNumber; + UINT32 InitialLocalApicId; + CPU_DATA_BLOCK *CpuData; + + SecPlatformInformation2 = NULL; + SecPlatformInformation = NULL; + + // + // Get gEfiSecPlatformInformation2PpiGuid Guided HOB firstly + // + GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformation2PpiGuid); + if (GuidHob != NULL) { + // + // Sec Platform Information2 PPI includes BSP/APs' BIST information + // + SecPlatformInformation2 = GET_GUID_HOB_DATA (GuidHob); + NumberOfData = SecPlatformInformation2->NumberOfCpus; + CpuInstance = SecPlatformInformation2->CpuInstance; + } else { + // + // Otherwise, get gEfiSecPlatformInformationPpiGuid Guided HOB + // + GuidHob = GetFirstGuidHob (&gEfiSecPlatformInformationPpiGuid); + if (GuidHob != NULL) { + SecPlatformInformation = GET_GUID_HOB_DATA (GuidHob); + NumberOfData = 1; + // + // SEC Platform Information only includes BSP's BIST information + // does not have BSP's APIC ID + // + BspCpuInstance.CpuLocation = GetApicId (); + BspCpuInstance.InfoRecord.IA32HealthFlags.Uint32 = SecPlatformInformation->IA32HealthFlags.Uint32; + CpuInstance = &BspCpuInstance; + } else { + DEBUG ((EFI_D_INFO, "Does not find any HOB stored CPU BIST information!\n")); + // + // Does not find any HOB stored BIST information + // + return; + } + } + + while ((NumberOfData--) > 0) { + for (ProcessorNumber = 0; ProcessorNumber < mMpSystemData.NumberOfProcessors; ProcessorNumber++) { + CpuData = &mMpSystemData.CpuDatas[ProcessorNumber]; + InitialLocalApicId = (UINT32) CpuData->Info.ProcessorId; + if (InitialLocalApicId == CpuInstance[NumberOfData].CpuLocation) { + // + // Update CPU health status for MP Services Protocol according to BIST data. + // + if (CpuInstance[NumberOfData].InfoRecord.IA32HealthFlags.Uint32 != 0) { + CpuData->Info.StatusFlag &= ~PROCESSOR_HEALTH_STATUS_BIT; + // + // Report Status Code that self test is failed + // + REPORT_STATUS_CODE ( + EFI_ERROR_CODE | EFI_ERROR_MAJOR, + (EFI_COMPUTING_UNIT_HOST_PROCESSOR | EFI_CU_HP_EC_SELF_TEST) + ); + } + } + } + } +} + +/** + Callback function for ExitBootServices. + + @param Event Event whose notification function is being invoked. + @param Context The pointer to the notification function's context, + which is implementation-dependent. + +**/ +VOID +EFIAPI +ExitBootServicesCallback ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + // + // Avoid APs access invalid buff datas which allocated by BootServices, + // so we send INIT IPI to APs to let them wait for SIPI state. + // + SendInitIpiAllExcludingSelf (); +} + /** Initialize Multi-processor support. @@ -1428,40 +1634,83 @@ InitializeMpSupport ( VOID ) { + EFI_STATUS Status; + gMaxLogicalProcessorNumber = (UINTN) PcdGet32 (PcdCpuMaxLogicalProcessorNumber); if (gMaxLogicalProcessorNumber < 1) { DEBUG ((DEBUG_ERROR, "Setting PcdCpuMaxLogicalProcessorNumber should be more than zero.\n")); return; } - if (gMaxLogicalProcessorNumber == 1) { - return; - } - gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize); - ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0); - mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize)); - ASSERT (mApStackStart != NULL); + InitMpSystemData (); // - // the first buffer of stack size used for common stack, when the amount of AP - // more than 1, we should never free the common stack which maybe used for AP reset. + // Only perform AP detection if PcdCpuMaxLogicalProcessorNumber is greater than 1 // - mCommonStack = mApStackStart; - mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize; - mApStackStart = mTopOfApCommonStack; + if (gMaxLogicalProcessorNumber > 1) { - InitMpSystemData (); + gApStackSize = (UINTN) PcdGet32 (PcdCpuApStackSize); + ASSERT ((gApStackSize & (SIZE_4KB - 1)) == 0); + + mApStackStart = AllocatePages (EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize)); + ASSERT (mApStackStart != NULL); + + // + // the first buffer of stack size used for common stack, when the amount of AP + // more than 1, we should never free the common stack which maybe used for AP reset. + // + mCommonStack = mApStackStart; + mTopOfApCommonStack = (UINT8*) mApStackStart + gApStackSize; + mApStackStart = mTopOfApCommonStack; + + PrepareAPStartupCode (); + StartApsStackless (); + } + + DEBUG ((DEBUG_INFO, "Detect CPU count: %d\n", mMpSystemData.NumberOfProcessors)); if (mMpSystemData.NumberOfProcessors == 1) { - FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize)); - return; + FreeApStartupCode (); + if (mCommonStack != NULL) { + FreePages (mCommonStack, EFI_SIZE_TO_PAGES (gMaxLogicalProcessorNumber * gApStackSize)); + } } - if (mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) { - FreePages (mApStackStart, EFI_SIZE_TO_PAGES ( - (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) * - gApStackSize)); + mMpSystemData.CpuDatas = ReallocatePool ( + sizeof (CPU_DATA_BLOCK) * gMaxLogicalProcessorNumber, + sizeof (CPU_DATA_BLOCK) * mMpSystemData.NumberOfProcessors, + mMpSystemData.CpuDatas); + + mAPsAlreadyInitFinished = TRUE; + + // + // Update CPU healthy information from Guided HOB + // + CollectBistDataFromHob (); + + Status = gBS->InstallMultipleProtocolInterfaces ( + &mMpServiceHandle, + &gEfiMpServiceProtocolGuid, &mMpServicesTemplate, + NULL + ); + ASSERT_EFI_ERROR (Status); + + if (mMpSystemData.NumberOfProcessors > 1 && mMpSystemData.NumberOfProcessors < gMaxLogicalProcessorNumber) { + if (mApStackStart != NULL) { + FreePages (mApStackStart, EFI_SIZE_TO_PAGES ( + (gMaxLogicalProcessorNumber - mMpSystemData.NumberOfProcessors) * + gApStackSize)); + } } + + Status = gBS->CreateEvent ( + EVT_SIGNAL_EXIT_BOOT_SERVICES, + TPL_CALLBACK, + ExitBootServicesCallback, + NULL, + &mExitBootServicesEvent + ); + ASSERT_EFI_ERROR (Status); }