2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2021, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2020, AMD Inc. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
12 #include <Library/VmgExitLib.h>
13 #include <Register/Amd/Fam17Msr.h>
14 #include <Register/Amd/Ghcb.h>
16 EFI_GUID mCpuInitMpLibHobGuid
= CPU_INIT_MP_LIB_HOB_GUID
;
19 The function will check if BSP Execute Disable is enabled.
21 DxeIpl may have enabled Execute Disable for BSP, APs need to
22 get the status and sync up the settings.
23 If BSP's CR0.Paging is not set, BSP execute Disble feature is
26 @retval TRUE BSP Execute Disable is enabled.
27 @retval FALSE BSP Execute Disable is not enabled.
30 IsBspExecuteDisableEnabled (
35 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
36 MSR_IA32_EFER_REGISTER EferMsr
;
41 Cr0
.UintN
= AsmReadCr0 ();
42 if (Cr0
.Bits
.PG
!= 0) {
44 // If CR0 Paging bit is set
46 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
47 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
48 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
51 // Bit 20: Execute Disable Bit available.
53 if (Edx
.Bits
.NX
!= 0) {
54 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
57 // Bit 11: Execute Disable Bit enable.
59 if (EferMsr
.Bits
.NXE
!= 0) {
70 Worker function for SwitchBSP().
72 Worker function for SwitchBSP(), assigned to the AP which is intended
75 @param[in] Buffer Pointer to CPU MP Data
83 CPU_MP_DATA
*DataInHob
;
85 DataInHob
= (CPU_MP_DATA
*)Buffer
;
86 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
90 Get the Application Processors state.
92 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
98 IN CPU_AP_DATA
*CpuData
101 return CpuData
->State
;
105 Set the Application Processors state.
107 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
108 @param[in] State The AP status
112 IN CPU_AP_DATA
*CpuData
,
116 AcquireSpinLock (&CpuData
->ApLock
);
117 CpuData
->State
= State
;
118 ReleaseSpinLock (&CpuData
->ApLock
);
122 Save BSP's local APIC timer setting.
124 @param[in] CpuMpData Pointer to CPU MP Data
127 SaveLocalApicTimerSetting (
128 IN CPU_MP_DATA
*CpuMpData
132 // Record the current local APIC timer setting of BSP
135 &CpuMpData
->DivideValue
,
136 &CpuMpData
->PeriodicMode
,
139 CpuMpData
->CurrentTimerCount
= GetApicTimerCurrentCount ();
140 CpuMpData
->TimerInterruptState
= GetApicTimerInterruptState ();
144 Sync local APIC timer setting from BSP to AP.
146 @param[in] CpuMpData Pointer to CPU MP Data
149 SyncLocalApicTimerSetting (
150 IN CPU_MP_DATA
*CpuMpData
154 // Sync local APIC timer setting from BSP to AP
156 InitializeApicTimer (
157 CpuMpData
->DivideValue
,
158 CpuMpData
->CurrentTimerCount
,
159 CpuMpData
->PeriodicMode
,
163 // Disable AP's local APIC timer interrupt
165 DisableApicTimerInterrupt ();
169 Save the volatile registers required to be restored following INIT IPI.
171 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
174 SaveVolatileRegisters (
175 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
178 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
180 VolatileRegisters
->Cr0
= AsmReadCr0 ();
181 VolatileRegisters
->Cr3
= AsmReadCr3 ();
182 VolatileRegisters
->Cr4
= AsmReadCr4 ();
184 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
185 if (VersionInfoEdx
.Bits
.DE
!= 0) {
187 // If processor supports Debugging Extensions feature
188 // by CPUID.[EAX=01H]:EDX.BIT2
190 VolatileRegisters
->Dr0
= AsmReadDr0 ();
191 VolatileRegisters
->Dr1
= AsmReadDr1 ();
192 VolatileRegisters
->Dr2
= AsmReadDr2 ();
193 VolatileRegisters
->Dr3
= AsmReadDr3 ();
194 VolatileRegisters
->Dr6
= AsmReadDr6 ();
195 VolatileRegisters
->Dr7
= AsmReadDr7 ();
198 AsmReadGdtr (&VolatileRegisters
->Gdtr
);
199 AsmReadIdtr (&VolatileRegisters
->Idtr
);
200 VolatileRegisters
->Tr
= AsmReadTr ();
204 Restore the volatile registers following INIT IPI.
206 @param[in] VolatileRegisters Pointer to volatile resisters
207 @param[in] IsRestoreDr TRUE: Restore DRx if supported
208 FALSE: Do not restore DRx
211 RestoreVolatileRegisters (
212 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
213 IN BOOLEAN IsRestoreDr
216 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
217 IA32_TSS_DESCRIPTOR
*Tss
;
219 AsmWriteCr3 (VolatileRegisters
->Cr3
);
220 AsmWriteCr4 (VolatileRegisters
->Cr4
);
221 AsmWriteCr0 (VolatileRegisters
->Cr0
);
224 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
225 if (VersionInfoEdx
.Bits
.DE
!= 0) {
227 // If processor supports Debugging Extensions feature
228 // by CPUID.[EAX=01H]:EDX.BIT2
230 AsmWriteDr0 (VolatileRegisters
->Dr0
);
231 AsmWriteDr1 (VolatileRegisters
->Dr1
);
232 AsmWriteDr2 (VolatileRegisters
->Dr2
);
233 AsmWriteDr3 (VolatileRegisters
->Dr3
);
234 AsmWriteDr6 (VolatileRegisters
->Dr6
);
235 AsmWriteDr7 (VolatileRegisters
->Dr7
);
239 AsmWriteGdtr (&VolatileRegisters
->Gdtr
);
240 AsmWriteIdtr (&VolatileRegisters
->Idtr
);
241 if ((VolatileRegisters
->Tr
!= 0) &&
242 (VolatileRegisters
->Tr
< VolatileRegisters
->Gdtr
.Limit
))
244 Tss
= (IA32_TSS_DESCRIPTOR
*)(VolatileRegisters
->Gdtr
.Base
+
245 VolatileRegisters
->Tr
);
246 if (Tss
->Bits
.P
== 1) {
247 Tss
->Bits
.Type
&= 0xD; // 1101 - Clear busy bit just in case
248 AsmWriteTr (VolatileRegisters
->Tr
);
254 Detect whether Mwait-monitor feature is supported.
256 @retval TRUE Mwait-monitor feature is supported.
257 @retval FALSE Mwait-monitor feature is not supported.
264 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
266 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
267 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
273 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
275 @return The AP loop mode.
279 OUT UINT32
*MonitorFilterSize
283 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
285 ASSERT (MonitorFilterSize
!= NULL
);
287 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
288 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
289 if (ApLoopMode
== ApInMwaitLoop
) {
290 if (!IsMwaitSupport ()) {
292 // If processor does not support MONITOR/MWAIT feature,
293 // force AP in Hlt-loop mode
295 ApLoopMode
= ApInHltLoop
;
298 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs
)) {
300 // For SEV-ES, force AP in Hlt-loop mode in order to use the GHCB
301 // protocol for starting APs
303 ApLoopMode
= ApInHltLoop
;
307 if (ApLoopMode
!= ApInMwaitLoop
) {
308 *MonitorFilterSize
= sizeof (UINT32
);
311 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
312 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
314 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
315 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
322 Sort the APIC ID of all processors.
324 This function sorts the APIC ID of all processors so that processor number is
325 assigned in the ascending order of APIC ID which eases MP debugging.
327 @param[in] CpuMpData Pointer to PEI CPU MP Data
331 IN CPU_MP_DATA
*CpuMpData
338 CPU_INFO_IN_HOB CpuInfo
;
340 CPU_INFO_IN_HOB
*CpuInfoInHob
;
341 volatile UINT32
*StartupApSignal
;
343 ApCount
= CpuMpData
->CpuCount
- 1;
344 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
346 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
349 // Sort key is the hardware default APIC ID
351 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
352 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
353 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
355 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
359 if (Index3
!= Index1
) {
360 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
362 &CpuInfoInHob
[Index3
],
363 &CpuInfoInHob
[Index1
],
364 sizeof (CPU_INFO_IN_HOB
)
366 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
369 // Also exchange the StartupApSignal.
371 StartupApSignal
= CpuMpData
->CpuData
[Index3
].StartupApSignal
;
372 CpuMpData
->CpuData
[Index3
].StartupApSignal
=
373 CpuMpData
->CpuData
[Index1
].StartupApSignal
;
374 CpuMpData
->CpuData
[Index1
].StartupApSignal
= StartupApSignal
;
379 // Get the processor number for the BSP
381 ApicId
= GetInitialApicId ();
382 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
383 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
384 CpuMpData
->BspNumber
= (UINT32
)Index1
;
392 Enable x2APIC mode on APs.
394 @param[in, out] Buffer Pointer to private data buffer.
402 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
408 @param[in, out] Buffer Pointer to private data buffer.
416 CPU_MP_DATA
*CpuMpData
;
417 UINTN ProcessorNumber
;
420 CpuMpData
= (CPU_MP_DATA
*)Buffer
;
421 Status
= GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
422 ASSERT_EFI_ERROR (Status
);
424 // Load microcode on AP
426 MicrocodeDetect (CpuMpData
, ProcessorNumber
);
428 // Sync BSP's MTRR table to AP
430 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
434 Find the current Processor number by APIC ID.
436 @param[in] CpuMpData Pointer to PEI CPU MP Data
437 @param[out] ProcessorNumber Return the pocessor number found
439 @retval EFI_SUCCESS ProcessorNumber is found and returned.
440 @retval EFI_NOT_FOUND ProcessorNumber is not found.
444 IN CPU_MP_DATA
*CpuMpData
,
445 OUT UINTN
*ProcessorNumber
448 UINTN TotalProcessorNumber
;
450 CPU_INFO_IN_HOB
*CpuInfoInHob
;
451 UINT32 CurrentApicId
;
453 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
455 TotalProcessorNumber
= CpuMpData
->CpuCount
;
456 CurrentApicId
= GetApicId ();
457 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
458 if (CpuInfoInHob
[Index
].ApicId
== CurrentApicId
) {
459 *ProcessorNumber
= Index
;
464 return EFI_NOT_FOUND
;
468 This function will get CPU count in the system.
470 @param[in] CpuMpData Pointer to PEI CPU MP Data
472 @return CPU count detected
475 CollectProcessorCount (
476 IN CPU_MP_DATA
*CpuMpData
480 CPU_INFO_IN_HOB
*CpuInfoInHob
;
484 // Send 1st broadcast IPI to APs to wakeup APs
486 CpuMpData
->InitFlag
= ApInitConfig
;
487 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
, TRUE
);
488 CpuMpData
->InitFlag
= ApInitDone
;
490 // When InitFlag == ApInitConfig, WakeUpAP () guarantees all APs are checked in.
491 // FinishedCount is the number of check-in APs.
493 CpuMpData
->CpuCount
= CpuMpData
->FinishedCount
+ 1;
494 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
497 // Enable x2APIC mode if
498 // 1. Number of CPU is greater than 255; or
499 // 2. There are any logical processors reporting an Initial APIC ID of 255 or greater.
502 if (CpuMpData
->CpuCount
> 255) {
504 // If there are more than 255 processor found, force to enable X2APIC
508 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
509 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
510 if (CpuInfoInHob
[Index
].InitialApicId
>= 0xFF) {
518 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
520 // Wakeup all APs to enable x2APIC mode
522 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
, TRUE
);
524 // Wait for all known APs finished
526 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
531 // Enable x2APIC on BSP
533 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
535 // Set BSP/Aps state to IDLE
537 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
538 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
542 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
544 // Sort BSP/Aps by CPU APIC ID in ascending order
546 SortApicId (CpuMpData
);
548 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
550 return CpuMpData
->CpuCount
;
554 Initialize CPU AP Data when AP is wakeup at the first time.
556 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
557 @param[in] ProcessorNumber The handle number of processor
558 @param[in] BistData Processor BIST data
559 @param[in] ApTopOfStack Top of AP stack
564 IN OUT CPU_MP_DATA
*CpuMpData
,
565 IN UINTN ProcessorNumber
,
567 IN UINT64 ApTopOfStack
570 CPU_INFO_IN_HOB
*CpuInfoInHob
;
571 MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr
;
573 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
574 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
575 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
576 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
577 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
579 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
580 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
583 // NOTE: PlatformId is not relevant on AMD platforms.
585 if (!StandardSignatureIsAuthenticAMD ()) {
586 PlatformIdMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_PLATFORM_ID
);
587 CpuMpData
->CpuData
[ProcessorNumber
].PlatformId
= (UINT8
)PlatformIdMsr
.Bits
.PlatformId
;
592 &CpuMpData
->CpuData
[ProcessorNumber
].ProcessorSignature
,
598 InitializeSpinLock (&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
599 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
603 This function will be called from AP reset code if BSP uses WakeUpAP.
605 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
606 @param[in] ApIndex Number of current executing AP
611 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
615 CPU_MP_DATA
*CpuMpData
;
616 UINTN ProcessorNumber
;
617 EFI_AP_PROCEDURE Procedure
;
620 volatile UINT32
*ApStartupSignalBuffer
;
621 CPU_INFO_IN_HOB
*CpuInfoInHob
;
623 UINTN CurrentApicMode
;
626 // AP finished assembly code and begin to execute C code
628 CpuMpData
= ExchangeInfo
->CpuMpData
;
631 // AP's local APIC settings will be lost after received INIT IPI
632 // We need to re-initialize them at here
634 ProgramVirtualWireMode ();
636 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
638 DisableLvtInterrupts ();
639 SyncLocalApicTimerSetting (CpuMpData
);
641 CurrentApicMode
= GetApicMode ();
643 if (CpuMpData
->InitFlag
== ApInitConfig
) {
644 ProcessorNumber
= ApIndex
;
646 // This is first time AP wakeup, get BIST information from AP stack
648 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
649 BistData
= *(UINT32
*)((UINTN
)ApTopOfStack
- sizeof (UINTN
));
651 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
652 // to initialize AP in InitConfig path.
653 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
655 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
656 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
657 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
660 // Execute AP function if AP is ready
662 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
664 // Clear AP start-up signal when AP waken up
666 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
667 InterlockedCompareExchange32 (
668 (UINT32
*)ApStartupSignalBuffer
,
673 if (CpuMpData
->InitFlag
== ApInitReconfig
) {
675 // ApInitReconfig happens when:
676 // 1. AP is re-enabled after it's disabled, in either PEI or DXE phase.
677 // 2. AP is initialized in DXE phase.
678 // In either case, use the volatile registers value derived from BSP.
679 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a
680 // different IDT shared by all APs.
682 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
684 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
686 // Restore AP's volatile registers saved before AP is halted
688 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
691 // The CPU driver might not flush TLB for APs on spot after updating
692 // page attributes. AP in mwait loop mode needs to take care of it when
699 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
700 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
701 Parameter
= (VOID
*)CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
702 if (Procedure
!= NULL
) {
703 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
705 // Enable source debugging on AP function
709 // Invoke AP function here
711 Procedure (Parameter
);
712 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
713 if (CpuMpData
->SwitchBspFlag
) {
715 // Re-get the processor number due to BSP/AP maybe exchange in AP function
717 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
718 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
719 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
720 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
721 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
723 if ((CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId ()) ||
724 (CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()))
726 if (CurrentApicMode
!= GetApicMode ()) {
728 // If APIC mode change happened during AP function execution,
729 // we do not support APIC ID value changed.
735 // Re-get the CPU APICID and Initial APICID if they are changed
737 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
738 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
744 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
748 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
750 // Save AP volatile registers
752 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
756 // AP finished executing C code
758 InterlockedIncrement ((UINT32
*)&CpuMpData
->FinishedCount
);
760 if (CpuMpData
->InitFlag
== ApInitConfig
) {
762 // Delay decrementing the APs executing count when SEV-ES is enabled
763 // to allow the APs to issue an AP_RESET_HOLD before the BSP possibly
764 // performs another INIT-SIPI-SIPI sequence.
766 if (!CpuMpData
->SevEsIsEnabled
) {
767 InterlockedDecrement ((UINT32
*)&CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
772 // Place AP is specified loop mode
774 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
776 // Place AP in HLT-loop
779 DisableInterrupts ();
780 if (CpuMpData
->SevEsIsEnabled
) {
781 SevEsPlaceApHlt (CpuMpData
);
791 DisableInterrupts ();
792 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
794 // Place AP in MWAIT-loop
796 AsmMonitor ((UINTN
)ApStartupSignalBuffer
, 0, 0);
797 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
799 // Check AP start-up signal again.
800 // If AP start-up signal is not set, place AP into
801 // the specified C-state
803 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
805 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
807 // Place AP in Run-loop
815 // If AP start-up signal is written, AP is waken up
816 // otherwise place AP in loop again
818 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
826 Wait for AP wakeup and write AP start-up signal till AP is waken up.
828 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
832 IN
volatile UINT32
*ApStartupSignalBuffer
836 // If AP is waken up, StartupApSignal should be cleared.
837 // Otherwise, write StartupApSignal again till AP waken up.
839 while (InterlockedCompareExchange32 (
840 (UINT32
*)ApStartupSignalBuffer
,
850 This function will fill the exchange info structure.
852 @param[in] CpuMpData Pointer to CPU MP Data
856 FillExchangeInfoData (
857 IN CPU_MP_DATA
*CpuMpData
860 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
862 IA32_SEGMENT_DESCRIPTOR
*Selector
;
865 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
866 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
867 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
868 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
869 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
871 ExchangeInfo
->CodeSegment
= AsmReadCs ();
872 ExchangeInfo
->DataSegment
= AsmReadDs ();
874 ExchangeInfo
->Cr3
= AsmReadCr3 ();
876 ExchangeInfo
->CFunction
= (UINTN
)ApWakeupFunction
;
877 ExchangeInfo
->ApIndex
= 0;
878 ExchangeInfo
->NumApsExecuting
= 0;
879 ExchangeInfo
->InitFlag
= (UINTN
)CpuMpData
->InitFlag
;
880 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
881 ExchangeInfo
->CpuMpData
= CpuMpData
;
883 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
885 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
888 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
889 // to determin whether 5-Level Paging is enabled.
890 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
891 // current system setting.
892 // Using latter way is simpler because it also eliminates the needs to
893 // check whether platform wants to enable it.
895 Cr4
.UintN
= AsmReadCr4 ();
896 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
)(Cr4
.Bits
.LA57
== 1);
897 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
899 ExchangeInfo
->SevEsIsEnabled
= CpuMpData
->SevEsIsEnabled
;
900 ExchangeInfo
->GhcbBase
= (UINTN
)CpuMpData
->GhcbBase
;
903 // Get the BSP's data of GDT and IDT
905 AsmReadGdtr ((IA32_DESCRIPTOR
*)&ExchangeInfo
->GdtrProfile
);
906 AsmReadIdtr ((IA32_DESCRIPTOR
*)&ExchangeInfo
->IdtrProfile
);
909 // Find a 32-bit code segment
911 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
912 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
914 if ((Selector
->Bits
.L
== 0) && (Selector
->Bits
.Type
>= 8)) {
915 ExchangeInfo
->ModeTransitionSegment
=
916 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
921 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
925 // Copy all 32-bit code and 64-bit code into memory with type of
926 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
928 if (CpuMpData
->WakeupBufferHigh
!= 0) {
929 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
930 CpuMpData
->AddressMap
.SwitchToRealSize
-
931 CpuMpData
->AddressMap
.ModeTransitionOffset
;
933 (VOID
*)CpuMpData
->WakeupBufferHigh
,
934 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
935 CpuMpData
->AddressMap
.ModeTransitionOffset
,
939 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
941 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
942 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
945 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
946 (UINT32
)ExchangeInfo
->ModeOffset
-
947 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
948 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
952 Helper function that waits until the finished AP count reaches the specified
953 limit, or the specified timeout elapses (whichever comes first).
955 @param[in] CpuMpData Pointer to CPU MP Data.
956 @param[in] FinishedApLimit The number of finished APs to wait for.
957 @param[in] TimeLimit The number of microseconds to wait for.
960 TimedWaitForApFinish (
961 IN CPU_MP_DATA
*CpuMpData
,
962 IN UINT32 FinishedApLimit
,
967 Get available system memory below 1MB by specified size.
969 @param[in] CpuMpData The pointer to CPU MP Data structure.
972 BackupAndPrepareWakeupBuffer (
973 IN CPU_MP_DATA
*CpuMpData
977 (VOID
*)CpuMpData
->BackupBuffer
,
978 (VOID
*)CpuMpData
->WakeupBuffer
,
979 CpuMpData
->BackupBufferSize
982 (VOID
*)CpuMpData
->WakeupBuffer
,
983 (VOID
*)CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
984 CpuMpData
->AddressMap
.RendezvousFunnelSize
+
985 CpuMpData
->AddressMap
.SwitchToRealSize
990 Restore wakeup buffer data.
992 @param[in] CpuMpData The pointer to CPU MP Data structure.
995 RestoreWakeupBuffer (
996 IN CPU_MP_DATA
*CpuMpData
1000 (VOID
*)CpuMpData
->WakeupBuffer
,
1001 (VOID
*)CpuMpData
->BackupBuffer
,
1002 CpuMpData
->BackupBufferSize
1007 Calculate the size of the reset vector.
1009 @param[in] AddressMap The pointer to Address Map structure.
1011 @return Total amount of memory required for the AP reset area
1015 GetApResetVectorSize (
1016 IN MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
1021 Size
= AddressMap
->RendezvousFunnelSize
+
1022 AddressMap
->SwitchToRealSize
+
1023 sizeof (MP_CPU_EXCHANGE_INFO
);
1029 Allocate reset vector buffer.
1031 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
1034 AllocateResetVector (
1035 IN OUT CPU_MP_DATA
*CpuMpData
1038 UINTN ApResetVectorSize
;
1039 UINTN ApResetStackSize
;
1041 if (CpuMpData
->WakeupBuffer
== (UINTN
)-1) {
1042 ApResetVectorSize
= GetApResetVectorSize (&CpuMpData
->AddressMap
);
1044 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
1045 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*)(UINTN
)
1046 (CpuMpData
->WakeupBuffer
+
1047 CpuMpData
->AddressMap
.RendezvousFunnelSize
+
1048 CpuMpData
->AddressMap
.SwitchToRealSize
);
1049 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
1050 CpuMpData
->AddressMap
.RendezvousFunnelSize
+
1051 CpuMpData
->AddressMap
.SwitchToRealSize
-
1052 CpuMpData
->AddressMap
.ModeTransitionOffset
1055 // The AP reset stack is only used by SEV-ES guests. Do not allocate it
1056 // if SEV-ES is not enabled.
1058 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs
)) {
1060 // Stack location is based on ProcessorNumber, so use the total number
1061 // of processors for calculating the total stack area.
1063 ApResetStackSize
= (AP_RESET_STACK_SIZE
*
1064 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
1067 // Invoke GetWakeupBuffer a second time to allocate the stack area
1068 // below 1MB. The returned buffer will be page aligned and sized and
1069 // below the previously allocated buffer.
1071 CpuMpData
->SevEsAPResetStackStart
= GetWakeupBuffer (ApResetStackSize
);
1074 // Check to be sure that the "allocate below" behavior hasn't changed.
1075 // This will also catch a failed allocation, as "-1" is returned on
1078 if (CpuMpData
->SevEsAPResetStackStart
>= CpuMpData
->WakeupBuffer
) {
1081 "SEV-ES AP reset stack is not below wakeup buffer\n"
1090 BackupAndPrepareWakeupBuffer (CpuMpData
);
1094 Free AP reset vector buffer.
1096 @param[in] CpuMpData The pointer to CPU MP Data structure.
1100 IN CPU_MP_DATA
*CpuMpData
1104 // If SEV-ES is enabled, the reset area is needed for AP parking and
1105 // and AP startup in the OS, so the reset area is reserved. Do not
1106 // perform the restore as this will overwrite memory which has data
1107 // needed by SEV-ES.
1109 if (!CpuMpData
->SevEsIsEnabled
) {
1110 RestoreWakeupBuffer (CpuMpData
);
1115 This function will be called by BSP to wakeup AP.
1117 @param[in] CpuMpData Pointer to CPU MP Data
1118 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
1119 FALSE: Send IPI to AP by ApicId
1120 @param[in] ProcessorNumber The handle number of specified processor
1121 @param[in] Procedure The function to be invoked by AP
1122 @param[in] ProcedureArgument The argument to be passed into AP function
1123 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1127 IN CPU_MP_DATA
*CpuMpData
,
1128 IN BOOLEAN Broadcast
,
1129 IN UINTN ProcessorNumber
,
1130 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
1131 IN VOID
*ProcedureArgument OPTIONAL
,
1132 IN BOOLEAN WakeUpDisabledAps
1135 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
1137 CPU_AP_DATA
*CpuData
;
1138 BOOLEAN ResetVectorRequired
;
1139 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1141 CpuMpData
->FinishedCount
= 0;
1142 ResetVectorRequired
= FALSE
;
1144 if (CpuMpData
->WakeUpByInitSipiSipi
||
1145 (CpuMpData
->InitFlag
!= ApInitDone
))
1147 ResetVectorRequired
= TRUE
;
1148 AllocateResetVector (CpuMpData
);
1149 AllocateSevEsAPMemory (CpuMpData
);
1150 FillExchangeInfoData (CpuMpData
);
1151 SaveLocalApicTimerSetting (CpuMpData
);
1154 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1156 // Get AP target C-state each time when waking up AP,
1157 // for it maybe updated by platform again
1159 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1162 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1165 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1166 if (Index
!= CpuMpData
->BspNumber
) {
1167 CpuData
= &CpuMpData
->CpuData
[Index
];
1169 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1170 // the AP procedure will be skipped for disabled AP because AP state
1171 // is not CpuStateReady.
1173 if ((GetApState (CpuData
) == CpuStateDisabled
) && !WakeUpDisabledAps
) {
1177 CpuData
->ApFunction
= (UINTN
)Procedure
;
1178 CpuData
->ApFunctionArgument
= (UINTN
)ProcedureArgument
;
1179 SetApState (CpuData
, CpuStateReady
);
1180 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1181 *(UINT32
*)CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1186 if (ResetVectorRequired
) {
1188 // For SEV-ES, the initial AP boot address will be defined by
1189 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address
1190 // from the original INIT-SIPI-SIPI.
1192 if (CpuMpData
->SevEsIsEnabled
) {
1193 SetSevEsJumpTable (ExchangeInfo
->BufferStart
);
1199 SendInitSipiSipiAllExcludingSelf ((UINT32
)ExchangeInfo
->BufferStart
);
1202 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1203 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber
) > 0) {
1205 // The AP enumeration algorithm below is suitable only when the
1206 // platform can tell us the *exact* boot CPU count in advance.
1208 // The wait below finishes only when the detected AP count reaches
1209 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1210 // takes. If at least one AP fails to check in (meaning a platform
1211 // hardware bug), the detection hangs forever, by design. If the actual
1212 // boot CPU count in the system is higher than
1213 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1214 // misconfiguration), then some APs may complete initialization after
1215 // the wait finishes, and cause undefined behavior.
1217 TimedWaitForApFinish (
1219 PcdGet32 (PcdCpuBootLogicalProcessorNumber
) - 1,
1220 MAX_UINT32
// approx. 71 minutes
1224 // The AP enumeration algorithm below is suitable for two use cases.
1226 // (1) The check-in time for an individual AP is bounded, and APs run
1227 // through their initialization routines strongly concurrently. In
1228 // particular, the number of concurrently running APs
1229 // ("NumApsExecuting") is never expected to fall to zero
1230 // *temporarily* -- it is expected to fall to zero only when all
1231 // APs have checked-in.
1233 // In this case, the platform is supposed to set
1234 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1235 // enough for one AP to start initialization). The timeout will be
1236 // reached soon, and remaining APs are collected by watching
1237 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1238 // mid-process, while some APs have not completed initialization,
1239 // the behavior is undefined.
1241 // (2) The check-in time for an individual AP is unbounded, and/or APs
1242 // may complete their initializations widely spread out. In
1243 // particular, some APs may finish initialization before some APs
1246 // In this case, the platform is supposed to set
1247 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1248 // enumeration will always take that long (except when the boot CPU
1249 // count happens to be maximal, that is,
1250 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1251 // check-in before the timeout, and NumApsExecuting is assumed zero
1252 // at timeout. APs that miss the time-out may cause undefined
1255 TimedWaitForApFinish (
1257 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1258 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1261 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1267 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1269 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1270 CpuData
= &CpuMpData
->CpuData
[Index
];
1271 if (Index
!= CpuMpData
->BspNumber
) {
1272 WaitApWakeup (CpuData
->StartupApSignal
);
1277 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1278 CpuData
->ApFunction
= (UINTN
)Procedure
;
1279 CpuData
->ApFunctionArgument
= (UINTN
)ProcedureArgument
;
1280 SetApState (CpuData
, CpuStateReady
);
1282 // Wakeup specified AP
1284 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1285 *(UINT32
*)CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1286 if (ResetVectorRequired
) {
1287 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
1290 // For SEV-ES, the initial AP boot address will be defined by
1291 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address
1292 // from the original INIT-SIPI-SIPI.
1294 if (CpuMpData
->SevEsIsEnabled
) {
1295 SetSevEsJumpTable (ExchangeInfo
->BufferStart
);
1299 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1300 (UINT32
)ExchangeInfo
->BufferStart
1305 // Wait specified AP waken up
1307 WaitApWakeup (CpuData
->StartupApSignal
);
1310 if (ResetVectorRequired
) {
1311 FreeResetVector (CpuMpData
);
1315 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1316 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1317 // S3SmmInitDone Ppi.
1319 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1323 Calculate timeout value and return the current performance counter value.
1325 Calculate the number of performance counter ticks required for a timeout.
1326 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1329 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1330 @param[out] CurrentTime Returns the current value of the performance counter.
1332 @return Expected time stamp counter for timeout.
1333 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1339 IN UINTN TimeoutInMicroseconds
,
1340 OUT UINT64
*CurrentTime
1343 UINT64 TimeoutInSeconds
;
1344 UINT64 TimestampCounterFreq
;
1347 // Read the current value of the performance counter
1349 *CurrentTime
= GetPerformanceCounter ();
1352 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1355 if (TimeoutInMicroseconds
== 0) {
1360 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1363 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1366 // Check the potential overflow before calculate the number of ticks for the timeout value.
1368 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1370 // Convert microseconds into seconds if direct multiplication overflows
1372 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1374 // Assertion if the final tick count exceeds MAX_UINT64
1376 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1377 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1380 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1381 // it by 1,000,000, to get the number of ticks for the timeout value.
1385 TimestampCounterFreq
,
1386 TimeoutInMicroseconds
1394 Checks whether timeout expires.
1396 Check whether the number of elapsed performance counter ticks required for
1397 a timeout condition has been reached.
1398 If Timeout is zero, which means infinity, return value is always FALSE.
1400 @param[in, out] PreviousTime On input, the value of the performance counter
1401 when it was last read.
1402 On output, the current value of the performance
1404 @param[in] TotalTime The total amount of elapsed time in performance
1406 @param[in] Timeout The number of performance counter ticks required
1407 to reach a timeout condition.
1409 @retval TRUE A timeout condition has been reached.
1410 @retval FALSE A timeout condition has not been reached.
1415 IN OUT UINT64
*PreviousTime
,
1416 IN UINT64
*TotalTime
,
1430 GetPerformanceCounterProperties (&Start
, &End
);
1431 Cycle
= End
- Start
;
1437 CurrentTime
= GetPerformanceCounter ();
1438 Delta
= (INT64
)(CurrentTime
- *PreviousTime
);
1447 *TotalTime
+= Delta
;
1448 *PreviousTime
= CurrentTime
;
1449 if (*TotalTime
> Timeout
) {
1457 Helper function that waits until the finished AP count reaches the specified
1458 limit, or the specified timeout elapses (whichever comes first).
1460 @param[in] CpuMpData Pointer to CPU MP Data.
1461 @param[in] FinishedApLimit The number of finished APs to wait for.
1462 @param[in] TimeLimit The number of microseconds to wait for.
1465 TimedWaitForApFinish (
1466 IN CPU_MP_DATA
*CpuMpData
,
1467 IN UINT32 FinishedApLimit
,
1472 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1473 // "infinity", so check for (TimeLimit == 0) explicitly.
1475 if (TimeLimit
== 0) {
1479 CpuMpData
->TotalTime
= 0;
1480 CpuMpData
->ExpectedTime
= CalculateTimeout (
1482 &CpuMpData
->CurrentTime
1484 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1486 &CpuMpData
->CurrentTime
,
1487 &CpuMpData
->TotalTime
,
1488 CpuMpData
->ExpectedTime
1494 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1497 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1500 DivU64x64Remainder (
1501 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1502 GetPerformanceCounterProperties (NULL
, NULL
),
1510 Reset an AP to Idle state.
1512 Any task being executed by the AP will be aborted and the AP
1513 will be waiting for a new task in Wait-For-SIPI state.
1515 @param[in] ProcessorNumber The handle number of processor.
1518 ResetProcessorToIdleState (
1519 IN UINTN ProcessorNumber
1522 CPU_MP_DATA
*CpuMpData
;
1524 CpuMpData
= GetCpuMpData ();
1526 CpuMpData
->InitFlag
= ApInitReconfig
;
1527 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1528 while (CpuMpData
->FinishedCount
< 1) {
1532 CpuMpData
->InitFlag
= ApInitDone
;
1534 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1538 Searches for the next waiting AP.
1540 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1542 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1544 @retval EFI_SUCCESS The next waiting AP has been found.
1545 @retval EFI_NOT_FOUND No waiting AP exists.
1549 GetNextWaitingProcessorNumber (
1550 OUT UINTN
*NextProcessorNumber
1553 UINTN ProcessorNumber
;
1554 CPU_MP_DATA
*CpuMpData
;
1556 CpuMpData
= GetCpuMpData ();
1558 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1559 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1560 *NextProcessorNumber
= ProcessorNumber
;
1565 return EFI_NOT_FOUND
;
1568 /** Checks status of specified AP.
1570 This function checks whether the specified AP has finished the task assigned
1571 by StartupThisAP(), and whether timeout expires.
1573 @param[in] ProcessorNumber The handle number of processor.
1575 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1576 @retval EFI_TIMEOUT The timeout expires.
1577 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1581 IN UINTN ProcessorNumber
1584 CPU_MP_DATA
*CpuMpData
;
1585 CPU_AP_DATA
*CpuData
;
1587 CpuMpData
= GetCpuMpData ();
1588 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1591 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1592 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1593 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1596 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1598 if (GetApState (CpuData
) == CpuStateFinished
) {
1599 if (CpuData
->Finished
!= NULL
) {
1600 *(CpuData
->Finished
) = TRUE
;
1603 SetApState (CpuData
, CpuStateIdle
);
1607 // If timeout expires for StartupThisAP(), report timeout.
1609 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1610 if (CpuData
->Finished
!= NULL
) {
1611 *(CpuData
->Finished
) = FALSE
;
1615 // Reset failed AP to idle state
1617 ResetProcessorToIdleState (ProcessorNumber
);
1623 return EFI_NOT_READY
;
1627 Checks status of all APs.
1629 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1630 and whether timeout expires.
1632 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1633 @retval EFI_TIMEOUT The timeout expires.
1634 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1641 UINTN ProcessorNumber
;
1642 UINTN NextProcessorNumber
;
1645 CPU_MP_DATA
*CpuMpData
;
1646 CPU_AP_DATA
*CpuData
;
1648 CpuMpData
= GetCpuMpData ();
1650 NextProcessorNumber
= 0;
1653 // Go through all APs that are responsible for the StartupAllAPs().
1655 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1656 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1660 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1662 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1663 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1664 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1666 if (GetApState (CpuData
) == CpuStateFinished
) {
1667 CpuMpData
->RunningCount
--;
1668 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1669 SetApState (CpuData
, CpuStateIdle
);
1672 // If in Single Thread mode, then search for the next waiting AP for execution.
1674 if (CpuMpData
->SingleThread
) {
1675 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1677 if (!EFI_ERROR (Status
)) {
1681 (UINT32
)NextProcessorNumber
,
1682 CpuMpData
->Procedure
,
1683 CpuMpData
->ProcArguments
,
1692 // If all APs finish, return EFI_SUCCESS.
1694 if (CpuMpData
->RunningCount
== 0) {
1699 // If timeout expires, report timeout.
1702 &CpuMpData
->CurrentTime
,
1703 &CpuMpData
->TotalTime
,
1704 CpuMpData
->ExpectedTime
1709 // If FailedCpuList is not NULL, record all failed APs in it.
1711 if (CpuMpData
->FailedCpuList
!= NULL
) {
1712 *CpuMpData
->FailedCpuList
=
1713 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1714 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1719 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1721 // Check whether this processor is responsible for StartupAllAPs().
1723 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1725 // Reset failed APs to idle state
1727 ResetProcessorToIdleState (ProcessorNumber
);
1728 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1729 if (CpuMpData
->FailedCpuList
!= NULL
) {
1730 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1735 if (CpuMpData
->FailedCpuList
!= NULL
) {
1736 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1742 return EFI_NOT_READY
;
1746 MP Initialize Library initialization.
1748 This service will allocate AP reset vector and wakeup all APs to do APs
1751 This service must be invoked before all other MP Initialize Library
1752 service are invoked.
1754 @retval EFI_SUCCESS MP initialization succeeds.
1755 @retval Others MP initialization fails.
1760 MpInitLibInitialize (
1764 CPU_MP_DATA
*OldCpuMpData
;
1765 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1766 UINT32 MaxLogicalProcessorNumber
;
1768 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1769 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1771 UINT32 MonitorFilterSize
;
1774 CPU_MP_DATA
*CpuMpData
;
1776 UINT8
*MonitorBuffer
;
1778 UINTN ApResetVectorSize
;
1779 UINTN BackupBufferAddr
;
1782 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1783 if (OldCpuMpData
== NULL
) {
1784 MaxLogicalProcessorNumber
= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
1786 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1789 ASSERT (MaxLogicalProcessorNumber
!= 0);
1791 AsmGetAddressMap (&AddressMap
);
1792 ApResetVectorSize
= GetApResetVectorSize (&AddressMap
);
1793 ApStackSize
= PcdGet32 (PcdCpuApStackSize
);
1794 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1797 // Save BSP's Control registers for APs.
1799 SaveVolatileRegisters (&VolatileRegisters
);
1801 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1802 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1803 BufferSize
+= ApResetVectorSize
;
1804 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1805 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1806 BufferSize
+= sizeof (CPU_MP_DATA
);
1807 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1808 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1809 ASSERT (MpBuffer
!= NULL
);
1810 ZeroMem (MpBuffer
, BufferSize
);
1811 Buffer
= (UINTN
)MpBuffer
;
1814 // The layout of the Buffer is as below:
1816 // +--------------------+ <-- Buffer
1818 // +--------------------+ <-- MonitorBuffer
1819 // AP Monitor Filters (N)
1820 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1822 // +--------------------+
1824 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1825 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1826 // +--------------------+ <-- CpuMpData
1828 // +--------------------+ <-- CpuMpData->CpuData
1830 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1831 // CPU_INFO_IN_HOB (N)
1832 // +--------------------+
1834 MonitorBuffer
= (UINT8
*)(Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1835 BackupBufferAddr
= (UINTN
)MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1836 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1837 CpuMpData
= (CPU_MP_DATA
*)(ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1838 CpuMpData
->Buffer
= Buffer
;
1839 CpuMpData
->CpuApStackSize
= ApStackSize
;
1840 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1841 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1842 CpuMpData
->WakeupBuffer
= (UINTN
)-1;
1843 CpuMpData
->CpuCount
= 1;
1844 CpuMpData
->BspNumber
= 0;
1845 CpuMpData
->WaitEvent
= NULL
;
1846 CpuMpData
->SwitchBspFlag
= FALSE
;
1847 CpuMpData
->CpuData
= (CPU_AP_DATA
*)(CpuMpData
+ 1);
1848 CpuMpData
->CpuInfoInHob
= (UINT64
)(UINTN
)(CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1849 InitializeSpinLock (&CpuMpData
->MpLock
);
1850 CpuMpData
->SevEsIsEnabled
= ConfidentialComputingGuestHas (CCAttrAmdSevEs
);
1851 CpuMpData
->SevEsAPBuffer
= (UINTN
)-1;
1852 CpuMpData
->GhcbBase
= PcdGet64 (PcdGhcbBase
);
1855 // Make sure no memory usage outside of the allocated buffer.
1858 (CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1863 // Duplicate BSP's IDT to APs.
1864 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1866 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1867 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1869 // Don't pass BSP's TR to APs to avoid AP init failure.
1871 VolatileRegisters
.Tr
= 0;
1872 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1874 // Set BSP basic information
1876 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1878 // Save assembly code information
1880 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1882 // Finally set AP loop mode
1884 CpuMpData
->ApLoopMode
= ApLoopMode
;
1885 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1887 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1890 // Set up APs wakeup signal buffer
1892 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1893 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1894 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1898 // Enable the local APIC for Virtual Wire Mode.
1900 ProgramVirtualWireMode ();
1902 if (OldCpuMpData
== NULL
) {
1903 if (MaxLogicalProcessorNumber
> 1) {
1905 // Wakeup all APs and calculate the processor count in system
1907 CollectProcessorCount (CpuMpData
);
1911 // APs have been wakeup before, just get the CPU Information
1914 OldCpuMpData
->NewCpuMpData
= CpuMpData
;
1915 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1916 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1917 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1918 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
1919 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1920 InitializeSpinLock (&CpuMpData
->CpuData
[Index
].ApLock
);
1921 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0) ? TRUE
: FALSE
;
1922 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1926 if (!GetMicrocodePatchInfoFromHob (
1927 &CpuMpData
->MicrocodePatchAddress
,
1928 &CpuMpData
->MicrocodePatchRegionSize
1932 // The microcode patch information cache HOB does not exist, which means
1933 // the microcode patches data has not been loaded into memory yet
1935 ShadowMicrocodeUpdatePatch (CpuMpData
);
1939 // Detect and apply Microcode on BSP
1941 MicrocodeDetect (CpuMpData
, CpuMpData
->BspNumber
);
1943 // Store BSP's MTRR setting
1945 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1948 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1950 if (CpuMpData
->CpuCount
> 1) {
1951 if (OldCpuMpData
!= NULL
) {
1953 // Only needs to use this flag for DXE phase to update the wake up
1954 // buffer. Wakeup buffer allocated in PEI phase is no longer valid
1957 CpuMpData
->InitFlag
= ApInitReconfig
;
1960 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1962 // Wait for all APs finished initialization
1964 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1968 if (OldCpuMpData
!= NULL
) {
1969 CpuMpData
->InitFlag
= ApInitDone
;
1972 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1973 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1978 // Dump the microcode revision for each core.
1980 DEBUG_CODE_BEGIN ();
1982 UINT32 ExpectedMicrocodeRevision
;
1984 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
1985 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1986 GetProcessorLocationByApicId (CpuInfoInHob
[Index
].InitialApicId
, NULL
, NULL
, &ThreadId
);
1987 if (ThreadId
== 0) {
1989 // MicrocodeDetect() loads microcode in first thread of each core, so,
1990 // CpuMpData->CpuData[Index].MicrocodeEntryAddr is initialized only for first thread of each core.
1992 ExpectedMicrocodeRevision
= 0;
1993 if (CpuMpData
->CpuData
[Index
].MicrocodeEntryAddr
!= 0) {
1994 ExpectedMicrocodeRevision
= ((CPU_MICROCODE_HEADER
*)(UINTN
)CpuMpData
->CpuData
[Index
].MicrocodeEntryAddr
)->UpdateRevision
;
1999 "CPU[%04d]: Microcode revision = %08x, expected = %08x\n",
2001 CpuMpData
->CpuData
[Index
].MicrocodeRevision
,
2002 ExpectedMicrocodeRevision
2009 // Initialize global data for MP support
2011 InitMpGlobalData (CpuMpData
);
2017 Gets detailed MP-related information on the requested processor at the
2018 instant this call is made. This service may only be called from the BSP.
2020 @param[in] ProcessorNumber The handle number of processor.
2021 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
2022 the requested processor is deposited.
2023 @param[out] HealthData Return processor health data.
2025 @retval EFI_SUCCESS Processor information was returned.
2026 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2027 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
2028 @retval EFI_NOT_FOUND The processor with the handle specified by
2029 ProcessorNumber does not exist in the platform.
2030 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2035 MpInitLibGetProcessorInfo (
2036 IN UINTN ProcessorNumber
,
2037 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
2038 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
2041 CPU_MP_DATA
*CpuMpData
;
2043 CPU_INFO_IN_HOB
*CpuInfoInHob
;
2044 UINTN OriginalProcessorNumber
;
2046 CpuMpData
= GetCpuMpData ();
2047 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
2050 // Lower 24 bits contains the actual processor number.
2052 OriginalProcessorNumber
= ProcessorNumber
;
2053 ProcessorNumber
&= BIT24
- 1;
2056 // Check whether caller processor is BSP
2058 MpInitLibWhoAmI (&CallerNumber
);
2059 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2060 return EFI_DEVICE_ERROR
;
2063 if (ProcessorInfoBuffer
== NULL
) {
2064 return EFI_INVALID_PARAMETER
;
2067 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2068 return EFI_NOT_FOUND
;
2071 ProcessorInfoBuffer
->ProcessorId
= (UINT64
)CpuInfoInHob
[ProcessorNumber
].ApicId
;
2072 ProcessorInfoBuffer
->StatusFlag
= 0;
2073 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2074 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
2077 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
2078 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
2081 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2082 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
2084 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
2088 // Get processor location information
2090 GetProcessorLocationByApicId (
2091 CpuInfoInHob
[ProcessorNumber
].ApicId
,
2092 &ProcessorInfoBuffer
->Location
.Package
,
2093 &ProcessorInfoBuffer
->Location
.Core
,
2094 &ProcessorInfoBuffer
->Location
.Thread
2097 if ((OriginalProcessorNumber
& CPU_V2_EXTENDED_TOPOLOGY
) != 0) {
2098 GetProcessorLocation2ByApicId (
2099 CpuInfoInHob
[ProcessorNumber
].ApicId
,
2100 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Package
,
2101 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Die
,
2102 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Tile
,
2103 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Module
,
2104 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Core
,
2105 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Thread
2109 if (HealthData
!= NULL
) {
2110 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
2117 Worker function to switch the requested AP to be the BSP from that point onward.
2119 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
2120 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
2121 enabled AP. Otherwise, it will be disabled.
2123 @retval EFI_SUCCESS BSP successfully switched.
2124 @retval others Failed to switch BSP.
2129 IN UINTN ProcessorNumber
,
2130 IN BOOLEAN EnableOldBSP
2133 CPU_MP_DATA
*CpuMpData
;
2136 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
2137 BOOLEAN OldInterruptState
;
2138 BOOLEAN OldTimerInterruptState
;
2141 // Save and Disable Local APIC timer interrupt
2143 OldTimerInterruptState
= GetApicTimerInterruptState ();
2144 DisableApicTimerInterrupt ();
2146 // Before send both BSP and AP to a procedure to exchange their roles,
2147 // interrupt must be disabled. This is because during the exchange role
2148 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
2149 // be corrupted, since interrupt return address will be pushed to stack
2152 OldInterruptState
= SaveAndDisableInterrupts ();
2155 // Mask LINT0 & LINT1 for the old BSP
2157 DisableLvtInterrupts ();
2159 CpuMpData
= GetCpuMpData ();
2162 // Check whether caller processor is BSP
2164 MpInitLibWhoAmI (&CallerNumber
);
2165 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2166 return EFI_DEVICE_ERROR
;
2169 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2170 return EFI_NOT_FOUND
;
2174 // Check whether specified AP is disabled
2176 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
2177 if (State
== CpuStateDisabled
) {
2178 return EFI_INVALID_PARAMETER
;
2182 // Check whether ProcessorNumber specifies the current BSP
2184 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2185 return EFI_INVALID_PARAMETER
;
2189 // Check whether specified AP is busy
2191 if (State
== CpuStateBusy
) {
2192 return EFI_NOT_READY
;
2195 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2196 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2197 CpuMpData
->SwitchBspFlag
= TRUE
;
2198 CpuMpData
->NewBspNumber
= ProcessorNumber
;
2201 // Clear the BSP bit of MSR_IA32_APIC_BASE
2203 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2204 ApicBaseMsr
.Bits
.BSP
= 0;
2205 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2208 // Need to wakeUp AP (future BSP).
2210 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
2212 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
2215 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2217 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2218 ApicBaseMsr
.Bits
.BSP
= 1;
2219 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2220 ProgramVirtualWireMode ();
2223 // Wait for old BSP finished AP task
2225 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
2229 CpuMpData
->SwitchBspFlag
= FALSE
;
2231 // Set old BSP enable state
2233 if (!EnableOldBSP
) {
2234 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
2236 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2240 // Save new BSP number
2242 CpuMpData
->BspNumber
= (UINT32
)ProcessorNumber
;
2245 // Restore interrupt state.
2247 SetInterruptState (OldInterruptState
);
2249 if (OldTimerInterruptState
) {
2250 EnableApicTimerInterrupt ();
2257 Worker function to let the caller enable or disable an AP from this point onward.
2258 This service may only be called from the BSP.
2260 @param[in] ProcessorNumber The handle number of AP.
2261 @param[in] EnableAP Specifies the new state for the processor for
2262 enabled, FALSE for disabled.
2263 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2264 the new health status of the AP.
2266 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2267 @retval others Failed to Enable/Disable AP.
2271 EnableDisableApWorker (
2272 IN UINTN ProcessorNumber
,
2273 IN BOOLEAN EnableAP
,
2274 IN UINT32
*HealthFlag OPTIONAL
2277 CPU_MP_DATA
*CpuMpData
;
2280 CpuMpData
= GetCpuMpData ();
2283 // Check whether caller processor is BSP
2285 MpInitLibWhoAmI (&CallerNumber
);
2286 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2287 return EFI_DEVICE_ERROR
;
2290 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2291 return EFI_INVALID_PARAMETER
;
2294 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2295 return EFI_NOT_FOUND
;
2299 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2301 ResetProcessorToIdleState (ProcessorNumber
);
2304 if (HealthFlag
!= NULL
) {
2305 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2306 (BOOLEAN
)((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2313 This return the handle number for the calling processor. This service may be
2314 called from the BSP and APs.
2316 @param[out] ProcessorNumber Pointer to the handle number of AP.
2317 The range is from 0 to the total number of
2318 logical processors minus 1. The total number of
2319 logical processors can be retrieved by
2320 MpInitLibGetNumberOfProcessors().
2322 @retval EFI_SUCCESS The current processor handle number was returned
2324 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2325 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2331 OUT UINTN
*ProcessorNumber
2334 CPU_MP_DATA
*CpuMpData
;
2336 if (ProcessorNumber
== NULL
) {
2337 return EFI_INVALID_PARAMETER
;
2340 CpuMpData
= GetCpuMpData ();
2342 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2346 Retrieves the number of logical processor in the platform and the number of
2347 those logical processors that are enabled on this boot. This service may only
2348 be called from the BSP.
2350 @param[out] NumberOfProcessors Pointer to the total number of logical
2351 processors in the system, including the BSP
2353 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2354 processors that exist in system, including
2357 @retval EFI_SUCCESS The number of logical processors and enabled
2358 logical processors was retrieved.
2359 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2360 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2362 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2367 MpInitLibGetNumberOfProcessors (
2368 OUT UINTN
*NumberOfProcessors OPTIONAL
,
2369 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2372 CPU_MP_DATA
*CpuMpData
;
2374 UINTN ProcessorNumber
;
2375 UINTN EnabledProcessorNumber
;
2378 CpuMpData
= GetCpuMpData ();
2380 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2381 return EFI_INVALID_PARAMETER
;
2385 // Check whether caller processor is BSP
2387 MpInitLibWhoAmI (&CallerNumber
);
2388 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2389 return EFI_DEVICE_ERROR
;
2392 ProcessorNumber
= CpuMpData
->CpuCount
;
2393 EnabledProcessorNumber
= 0;
2394 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2395 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2396 EnabledProcessorNumber
++;
2400 if (NumberOfProcessors
!= NULL
) {
2401 *NumberOfProcessors
= ProcessorNumber
;
2404 if (NumberOfEnabledProcessors
!= NULL
) {
2405 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2412 Worker function to execute a caller provided function on all enabled APs.
2414 @param[in] Procedure A pointer to the function to be run on
2415 enabled APs of the system.
2416 @param[in] SingleThread If TRUE, then all the enabled APs execute
2417 the function specified by Procedure one by
2418 one, in ascending order of processor handle
2419 number. If FALSE, then all the enabled APs
2420 execute the function specified by Procedure
2422 @param[in] ExcludeBsp Whether let BSP also trig this task.
2423 @param[in] WaitEvent The event created by the caller with CreateEvent()
2425 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2426 APs to return from Procedure, either for
2427 blocking or non-blocking mode.
2428 @param[in] ProcedureArgument The parameter passed into Procedure for
2430 @param[out] FailedCpuList If all APs finish successfully, then its
2431 content is set to NULL. If not all APs
2432 finish before timeout expires, then its
2433 content is set to address of the buffer
2434 holding handle numbers of the failed APs.
2436 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2437 the timeout expired.
2438 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2440 @retval others Failed to Startup all APs.
2444 StartupAllCPUsWorker (
2445 IN EFI_AP_PROCEDURE Procedure
,
2446 IN BOOLEAN SingleThread
,
2447 IN BOOLEAN ExcludeBsp
,
2448 IN EFI_EVENT WaitEvent OPTIONAL
,
2449 IN UINTN TimeoutInMicroseconds
,
2450 IN VOID
*ProcedureArgument OPTIONAL
,
2451 OUT UINTN
**FailedCpuList OPTIONAL
2455 CPU_MP_DATA
*CpuMpData
;
2456 UINTN ProcessorCount
;
2457 UINTN ProcessorNumber
;
2459 CPU_AP_DATA
*CpuData
;
2460 BOOLEAN HasEnabledAp
;
2463 CpuMpData
= GetCpuMpData ();
2465 if (FailedCpuList
!= NULL
) {
2466 *FailedCpuList
= NULL
;
2469 if ((CpuMpData
->CpuCount
== 1) && ExcludeBsp
) {
2470 return EFI_NOT_STARTED
;
2473 if (Procedure
== NULL
) {
2474 return EFI_INVALID_PARAMETER
;
2478 // Check whether caller processor is BSP
2480 MpInitLibWhoAmI (&CallerNumber
);
2481 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2482 return EFI_DEVICE_ERROR
;
2488 CheckAndUpdateApsStatus ();
2490 ProcessorCount
= CpuMpData
->CpuCount
;
2491 HasEnabledAp
= FALSE
;
2493 // Check whether all enabled APs are idle.
2494 // If any enabled AP is not idle, return EFI_NOT_READY.
2496 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2497 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2498 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2499 ApState
= GetApState (CpuData
);
2500 if (ApState
!= CpuStateDisabled
) {
2501 HasEnabledAp
= TRUE
;
2502 if (ApState
!= CpuStateIdle
) {
2504 // If any enabled APs are busy, return EFI_NOT_READY.
2506 return EFI_NOT_READY
;
2512 if (!HasEnabledAp
&& ExcludeBsp
) {
2514 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2516 return EFI_NOT_STARTED
;
2519 CpuMpData
->RunningCount
= 0;
2520 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2521 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2522 CpuData
->Waiting
= FALSE
;
2523 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2524 if (CpuData
->State
== CpuStateIdle
) {
2526 // Mark this processor as responsible for current calling.
2528 CpuData
->Waiting
= TRUE
;
2529 CpuMpData
->RunningCount
++;
2534 CpuMpData
->Procedure
= Procedure
;
2535 CpuMpData
->ProcArguments
= ProcedureArgument
;
2536 CpuMpData
->SingleThread
= SingleThread
;
2537 CpuMpData
->FinishedCount
= 0;
2538 CpuMpData
->FailedCpuList
= FailedCpuList
;
2539 CpuMpData
->ExpectedTime
= CalculateTimeout (
2540 TimeoutInMicroseconds
,
2541 &CpuMpData
->CurrentTime
2543 CpuMpData
->TotalTime
= 0;
2544 CpuMpData
->WaitEvent
= WaitEvent
;
2546 if (!SingleThread
) {
2547 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2549 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2550 if (ProcessorNumber
== CallerNumber
) {
2554 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2555 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2565 Procedure (ProcedureArgument
);
2568 Status
= EFI_SUCCESS
;
2569 if (WaitEvent
== NULL
) {
2571 Status
= CheckAllAPs ();
2572 } while (Status
== EFI_NOT_READY
);
2579 Worker function to let the caller get one enabled AP to execute a caller-provided
2582 @param[in] Procedure A pointer to the function to be run on
2583 enabled APs of the system.
2584 @param[in] ProcessorNumber The handle number of the AP.
2585 @param[in] WaitEvent The event created by the caller with CreateEvent()
2587 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2588 APs to return from Procedure, either for
2589 blocking or non-blocking mode.
2590 @param[in] ProcedureArgument The parameter passed into Procedure for
2592 @param[out] Finished If AP returns from Procedure before the
2593 timeout expires, its content is set to TRUE.
2594 Otherwise, the value is set to FALSE.
2596 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2597 the timeout expires.
2598 @retval others Failed to Startup AP.
2602 StartupThisAPWorker (
2603 IN EFI_AP_PROCEDURE Procedure
,
2604 IN UINTN ProcessorNumber
,
2605 IN EFI_EVENT WaitEvent OPTIONAL
,
2606 IN UINTN TimeoutInMicroseconds
,
2607 IN VOID
*ProcedureArgument OPTIONAL
,
2608 OUT BOOLEAN
*Finished OPTIONAL
2612 CPU_MP_DATA
*CpuMpData
;
2613 CPU_AP_DATA
*CpuData
;
2616 CpuMpData
= GetCpuMpData ();
2618 if (Finished
!= NULL
) {
2623 // Check whether caller processor is BSP
2625 MpInitLibWhoAmI (&CallerNumber
);
2626 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2627 return EFI_DEVICE_ERROR
;
2631 // Check whether processor with the handle specified by ProcessorNumber exists
2633 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2634 return EFI_NOT_FOUND
;
2638 // Check whether specified processor is BSP
2640 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2641 return EFI_INVALID_PARAMETER
;
2645 // Check parameter Procedure
2647 if (Procedure
== NULL
) {
2648 return EFI_INVALID_PARAMETER
;
2654 CheckAndUpdateApsStatus ();
2657 // Check whether specified AP is disabled
2659 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2660 return EFI_INVALID_PARAMETER
;
2664 // If WaitEvent is not NULL, execute in non-blocking mode.
2665 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2666 // CheckAPsStatus() will check completion and timeout periodically.
2668 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2669 CpuData
->WaitEvent
= WaitEvent
;
2670 CpuData
->Finished
= Finished
;
2671 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2672 CpuData
->TotalTime
= 0;
2674 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2677 // If WaitEvent is NULL, execute in blocking mode.
2678 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2680 Status
= EFI_SUCCESS
;
2681 if (WaitEvent
== NULL
) {
2683 Status
= CheckThisAP (ProcessorNumber
);
2684 } while (Status
== EFI_NOT_READY
);
2691 Get pointer to CPU MP Data structure from GUIDed HOB.
2693 @return The pointer to CPU MP Data structure.
2696 GetCpuMpDataFromGuidedHob (
2700 EFI_HOB_GUID_TYPE
*GuidHob
;
2702 CPU_MP_DATA
*CpuMpData
;
2705 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2706 if (GuidHob
!= NULL
) {
2707 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2708 CpuMpData
= (CPU_MP_DATA
*)(*(UINTN
*)DataInHob
);
2715 This service executes a caller provided function on all enabled CPUs.
2717 @param[in] Procedure A pointer to the function to be run on
2718 enabled APs of the system. See type
2720 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2721 APs to return from Procedure, either for
2722 blocking or non-blocking mode. Zero means
2723 infinity. TimeoutInMicroseconds is ignored
2725 @param[in] ProcedureArgument The parameter passed into Procedure for
2728 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2729 the timeout expired.
2730 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2731 to all enabled CPUs.
2732 @retval EFI_DEVICE_ERROR Caller processor is AP.
2733 @retval EFI_NOT_READY Any enabled APs are busy.
2734 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2735 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2736 all enabled APs have finished.
2737 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2742 MpInitLibStartupAllCPUs (
2743 IN EFI_AP_PROCEDURE Procedure
,
2744 IN UINTN TimeoutInMicroseconds
,
2745 IN VOID
*ProcedureArgument OPTIONAL
2748 return StartupAllCPUsWorker (
2753 TimeoutInMicroseconds
,
2760 The function check if the specified Attr is set.
2762 @param[in] CurrentAttr The current attribute.
2763 @param[in] Attr The attribute to check.
2765 @retval TRUE The specified Attr is set.
2766 @retval FALSE The specified Attr is not set.
2771 AmdMemEncryptionAttrCheck (
2772 IN UINT64 CurrentAttr
,
2773 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
2779 // SEV is automatically enabled if SEV-ES or SEV-SNP is active.
2781 return CurrentAttr
>= CCAttrAmdSev
;
2782 case CCAttrAmdSevEs
:
2784 // SEV-ES is automatically enabled if SEV-SNP is active.
2786 return CurrentAttr
>= CCAttrAmdSevEs
;
2787 case CCAttrAmdSevSnp
:
2788 return CurrentAttr
== CCAttrAmdSevSnp
;
2795 Check if the specified confidential computing attribute is active.
2797 @param[in] Attr The attribute to check.
2799 @retval TRUE The specified Attr is active.
2800 @retval FALSE The specified Attr is not active.
2805 ConfidentialComputingGuestHas (
2806 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
2812 // Get the current CC attribute.
2814 CurrentAttr
= PcdGet64 (PcdConfidentialComputingGuestAttr
);
2817 // If attr is for the AMD group then call AMD specific checks.
2819 if (((RShiftU64 (CurrentAttr
, 8)) & 0xff) == 1) {
2820 return AmdMemEncryptionAttrCheck (CurrentAttr
, Attr
);
2823 return (CurrentAttr
== Attr
);