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
) &&
299 !ConfidentialComputingGuestHas (CCAttrAmdSevSnp
))
302 // For SEV-ES (SEV-SNP is also considered SEV-ES), force AP in Hlt-loop
303 // mode in order to use the GHCB protocol for starting APs
305 ApLoopMode
= ApInHltLoop
;
309 if (ApLoopMode
!= ApInMwaitLoop
) {
310 *MonitorFilterSize
= sizeof (UINT32
);
313 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
314 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
316 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
317 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
324 Sort the APIC ID of all processors.
326 This function sorts the APIC ID of all processors so that processor number is
327 assigned in the ascending order of APIC ID which eases MP debugging.
329 @param[in] CpuMpData Pointer to PEI CPU MP Data
333 IN CPU_MP_DATA
*CpuMpData
340 CPU_INFO_IN_HOB CpuInfo
;
342 CPU_INFO_IN_HOB
*CpuInfoInHob
;
343 volatile UINT32
*StartupApSignal
;
345 ApCount
= CpuMpData
->CpuCount
- 1;
346 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
348 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
351 // Sort key is the hardware default APIC ID
353 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
354 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
355 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
357 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
361 if (Index3
!= Index1
) {
362 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
364 &CpuInfoInHob
[Index3
],
365 &CpuInfoInHob
[Index1
],
366 sizeof (CPU_INFO_IN_HOB
)
368 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
371 // Also exchange the StartupApSignal.
373 StartupApSignal
= CpuMpData
->CpuData
[Index3
].StartupApSignal
;
374 CpuMpData
->CpuData
[Index3
].StartupApSignal
=
375 CpuMpData
->CpuData
[Index1
].StartupApSignal
;
376 CpuMpData
->CpuData
[Index1
].StartupApSignal
= StartupApSignal
;
381 // Get the processor number for the BSP
383 ApicId
= GetInitialApicId ();
384 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
385 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
386 CpuMpData
->BspNumber
= (UINT32
)Index1
;
394 Enable x2APIC mode on APs.
396 @param[in, out] Buffer Pointer to private data buffer.
404 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
410 @param[in, out] Buffer Pointer to private data buffer.
418 CPU_MP_DATA
*CpuMpData
;
419 UINTN ProcessorNumber
;
422 CpuMpData
= (CPU_MP_DATA
*)Buffer
;
423 Status
= GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
424 ASSERT_EFI_ERROR (Status
);
426 // Load microcode on AP
428 MicrocodeDetect (CpuMpData
, ProcessorNumber
);
430 // Sync BSP's MTRR table to AP
432 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
436 Find the current Processor number by APIC ID.
438 @param[in] CpuMpData Pointer to PEI CPU MP Data
439 @param[out] ProcessorNumber Return the pocessor number found
441 @retval EFI_SUCCESS ProcessorNumber is found and returned.
442 @retval EFI_NOT_FOUND ProcessorNumber is not found.
446 IN CPU_MP_DATA
*CpuMpData
,
447 OUT UINTN
*ProcessorNumber
450 UINTN TotalProcessorNumber
;
452 CPU_INFO_IN_HOB
*CpuInfoInHob
;
453 UINT32 CurrentApicId
;
455 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
457 TotalProcessorNumber
= CpuMpData
->CpuCount
;
458 CurrentApicId
= GetApicId ();
459 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
460 if (CpuInfoInHob
[Index
].ApicId
== CurrentApicId
) {
461 *ProcessorNumber
= Index
;
466 return EFI_NOT_FOUND
;
470 This function will get CPU count in the system.
472 @param[in] CpuMpData Pointer to PEI CPU MP Data
474 @return CPU count detected
477 CollectProcessorCount (
478 IN CPU_MP_DATA
*CpuMpData
482 CPU_INFO_IN_HOB
*CpuInfoInHob
;
486 // Send 1st broadcast IPI to APs to wakeup APs
488 CpuMpData
->InitFlag
= ApInitConfig
;
489 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
, TRUE
);
490 CpuMpData
->InitFlag
= ApInitDone
;
492 // When InitFlag == ApInitConfig, WakeUpAP () guarantees all APs are checked in.
493 // FinishedCount is the number of check-in APs.
495 CpuMpData
->CpuCount
= CpuMpData
->FinishedCount
+ 1;
496 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
499 // Enable x2APIC mode if
500 // 1. Number of CPU is greater than 255; or
501 // 2. There are any logical processors reporting an Initial APIC ID of 255 or greater.
504 if (CpuMpData
->CpuCount
> 255) {
506 // If there are more than 255 processor found, force to enable X2APIC
510 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
511 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
512 if (CpuInfoInHob
[Index
].InitialApicId
>= 0xFF) {
520 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
522 // Wakeup all APs to enable x2APIC mode
524 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
, TRUE
);
526 // Wait for all known APs finished
528 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
533 // Enable x2APIC on BSP
535 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
537 // Set BSP/Aps state to IDLE
539 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
540 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
544 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
546 // Sort BSP/Aps by CPU APIC ID in ascending order
548 SortApicId (CpuMpData
);
550 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
552 return CpuMpData
->CpuCount
;
556 Initialize CPU AP Data when AP is wakeup at the first time.
558 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
559 @param[in] ProcessorNumber The handle number of processor
560 @param[in] BistData Processor BIST data
561 @param[in] ApTopOfStack Top of AP stack
566 IN OUT CPU_MP_DATA
*CpuMpData
,
567 IN UINTN ProcessorNumber
,
569 IN UINT64 ApTopOfStack
572 CPU_INFO_IN_HOB
*CpuInfoInHob
;
573 MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr
;
575 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
576 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
577 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
578 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
579 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
581 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
582 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
585 // NOTE: PlatformId is not relevant on AMD platforms.
587 if (!StandardSignatureIsAuthenticAMD ()) {
588 PlatformIdMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_PLATFORM_ID
);
589 CpuMpData
->CpuData
[ProcessorNumber
].PlatformId
= (UINT8
)PlatformIdMsr
.Bits
.PlatformId
;
594 &CpuMpData
->CpuData
[ProcessorNumber
].ProcessorSignature
,
600 InitializeSpinLock (&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
601 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
605 This function will be called from AP reset code if BSP uses WakeUpAP.
607 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
608 @param[in] ApIndex Number of current executing AP
613 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
617 CPU_MP_DATA
*CpuMpData
;
618 UINTN ProcessorNumber
;
619 EFI_AP_PROCEDURE Procedure
;
622 volatile UINT32
*ApStartupSignalBuffer
;
623 CPU_INFO_IN_HOB
*CpuInfoInHob
;
625 UINTN CurrentApicMode
;
628 // AP finished assembly code and begin to execute C code
630 CpuMpData
= ExchangeInfo
->CpuMpData
;
633 // AP's local APIC settings will be lost after received INIT IPI
634 // We need to re-initialize them at here
636 ProgramVirtualWireMode ();
638 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
640 DisableLvtInterrupts ();
641 SyncLocalApicTimerSetting (CpuMpData
);
643 CurrentApicMode
= GetApicMode ();
645 if (CpuMpData
->InitFlag
== ApInitConfig
) {
646 ProcessorNumber
= ApIndex
;
648 // This is first time AP wakeup, get BIST information from AP stack
650 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
651 BistData
= *(UINT32
*)((UINTN
)ApTopOfStack
- sizeof (UINTN
));
653 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
654 // to initialize AP in InitConfig path.
655 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
657 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
658 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
659 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
662 // Execute AP function if AP is ready
664 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
666 // Clear AP start-up signal when AP waken up
668 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
669 InterlockedCompareExchange32 (
670 (UINT32
*)ApStartupSignalBuffer
,
675 if (CpuMpData
->InitFlag
== ApInitReconfig
) {
677 // ApInitReconfig happens when:
678 // 1. AP is re-enabled after it's disabled, in either PEI or DXE phase.
679 // 2. AP is initialized in DXE phase.
680 // In either case, use the volatile registers value derived from BSP.
681 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a
682 // different IDT shared by all APs.
684 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
686 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
688 // Restore AP's volatile registers saved before AP is halted
690 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
693 // The CPU driver might not flush TLB for APs on spot after updating
694 // page attributes. AP in mwait loop mode needs to take care of it when
701 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
702 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
703 Parameter
= (VOID
*)CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
704 if (Procedure
!= NULL
) {
705 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
707 // Enable source debugging on AP function
711 // Invoke AP function here
713 Procedure (Parameter
);
714 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
715 if (CpuMpData
->SwitchBspFlag
) {
717 // Re-get the processor number due to BSP/AP maybe exchange in AP function
719 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
720 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
721 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
722 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
723 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
725 if ((CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId ()) ||
726 (CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()))
728 if (CurrentApicMode
!= GetApicMode ()) {
730 // If APIC mode change happened during AP function execution,
731 // we do not support APIC ID value changed.
737 // Re-get the CPU APICID and Initial APICID if they are changed
739 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
740 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
746 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
750 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
752 // Save AP volatile registers
754 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
758 // AP finished executing C code
760 InterlockedIncrement ((UINT32
*)&CpuMpData
->FinishedCount
);
762 if (CpuMpData
->InitFlag
== ApInitConfig
) {
764 // Delay decrementing the APs executing count when SEV-ES is enabled
765 // to allow the APs to issue an AP_RESET_HOLD before the BSP possibly
766 // performs another INIT-SIPI-SIPI sequence.
768 if (!CpuMpData
->UseSevEsAPMethod
) {
769 InterlockedDecrement ((UINT32
*)&CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
774 // Place AP is specified loop mode
776 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
778 // Place AP in HLT-loop
781 DisableInterrupts ();
782 if (CpuMpData
->UseSevEsAPMethod
) {
783 SevEsPlaceApHlt (CpuMpData
);
793 DisableInterrupts ();
794 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
796 // Place AP in MWAIT-loop
798 AsmMonitor ((UINTN
)ApStartupSignalBuffer
, 0, 0);
799 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
801 // Check AP start-up signal again.
802 // If AP start-up signal is not set, place AP into
803 // the specified C-state
805 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
807 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
809 // Place AP in Run-loop
817 // If AP start-up signal is written, AP is waken up
818 // otherwise place AP in loop again
820 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
828 Wait for AP wakeup and write AP start-up signal till AP is waken up.
830 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
834 IN
volatile UINT32
*ApStartupSignalBuffer
838 // If AP is waken up, StartupApSignal should be cleared.
839 // Otherwise, write StartupApSignal again till AP waken up.
841 while (InterlockedCompareExchange32 (
842 (UINT32
*)ApStartupSignalBuffer
,
852 This function will fill the exchange info structure.
854 @param[in] CpuMpData Pointer to CPU MP Data
858 FillExchangeInfoData (
859 IN CPU_MP_DATA
*CpuMpData
862 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
864 IA32_SEGMENT_DESCRIPTOR
*Selector
;
867 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
868 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
869 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
870 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
871 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
873 ExchangeInfo
->CodeSegment
= AsmReadCs ();
874 ExchangeInfo
->DataSegment
= AsmReadDs ();
876 ExchangeInfo
->Cr3
= AsmReadCr3 ();
878 ExchangeInfo
->CFunction
= (UINTN
)ApWakeupFunction
;
879 ExchangeInfo
->ApIndex
= 0;
880 ExchangeInfo
->NumApsExecuting
= 0;
881 ExchangeInfo
->InitFlag
= (UINTN
)CpuMpData
->InitFlag
;
882 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
883 ExchangeInfo
->CpuMpData
= CpuMpData
;
885 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
887 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
890 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
891 // to determin whether 5-Level Paging is enabled.
892 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
893 // current system setting.
894 // Using latter way is simpler because it also eliminates the needs to
895 // check whether platform wants to enable it.
897 Cr4
.UintN
= AsmReadCr4 ();
898 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
)(Cr4
.Bits
.LA57
== 1);
899 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
901 ExchangeInfo
->SevEsIsEnabled
= CpuMpData
->SevEsIsEnabled
;
902 ExchangeInfo
->SevSnpIsEnabled
= CpuMpData
->SevSnpIsEnabled
;
903 ExchangeInfo
->GhcbBase
= (UINTN
)CpuMpData
->GhcbBase
;
906 // Populate SEV-ES specific exchange data.
908 if (ExchangeInfo
->SevSnpIsEnabled
) {
909 FillExchangeInfoDataSevEs (ExchangeInfo
);
913 // Get the BSP's data of GDT and IDT
915 AsmReadGdtr ((IA32_DESCRIPTOR
*)&ExchangeInfo
->GdtrProfile
);
916 AsmReadIdtr ((IA32_DESCRIPTOR
*)&ExchangeInfo
->IdtrProfile
);
919 // Find a 32-bit code segment
921 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
922 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
924 if ((Selector
->Bits
.L
== 0) && (Selector
->Bits
.Type
>= 8)) {
925 ExchangeInfo
->ModeTransitionSegment
=
926 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
931 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
935 // Copy all 32-bit code and 64-bit code into memory with type of
936 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
938 if (CpuMpData
->WakeupBufferHigh
!= 0) {
939 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
940 CpuMpData
->AddressMap
.SwitchToRealSize
-
941 CpuMpData
->AddressMap
.ModeTransitionOffset
;
943 (VOID
*)CpuMpData
->WakeupBufferHigh
,
944 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
945 CpuMpData
->AddressMap
.ModeTransitionOffset
,
949 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
951 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
952 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
955 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
956 (UINT32
)ExchangeInfo
->ModeOffset
-
957 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
958 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
962 Helper function that waits until the finished AP count reaches the specified
963 limit, or the specified timeout elapses (whichever comes first).
965 @param[in] CpuMpData Pointer to CPU MP Data.
966 @param[in] FinishedApLimit The number of finished APs to wait for.
967 @param[in] TimeLimit The number of microseconds to wait for.
970 TimedWaitForApFinish (
971 IN CPU_MP_DATA
*CpuMpData
,
972 IN UINT32 FinishedApLimit
,
977 Get available system memory below 1MB by specified size.
979 @param[in] CpuMpData The pointer to CPU MP Data structure.
982 BackupAndPrepareWakeupBuffer (
983 IN CPU_MP_DATA
*CpuMpData
987 (VOID
*)CpuMpData
->BackupBuffer
,
988 (VOID
*)CpuMpData
->WakeupBuffer
,
989 CpuMpData
->BackupBufferSize
992 (VOID
*)CpuMpData
->WakeupBuffer
,
993 (VOID
*)CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
994 CpuMpData
->AddressMap
.RendezvousFunnelSize
+
995 CpuMpData
->AddressMap
.SwitchToRealSize
1000 Restore wakeup buffer data.
1002 @param[in] CpuMpData The pointer to CPU MP Data structure.
1005 RestoreWakeupBuffer (
1006 IN CPU_MP_DATA
*CpuMpData
1010 (VOID
*)CpuMpData
->WakeupBuffer
,
1011 (VOID
*)CpuMpData
->BackupBuffer
,
1012 CpuMpData
->BackupBufferSize
1017 Calculate the size of the reset vector.
1019 @param[in] AddressMap The pointer to Address Map structure.
1021 @return Total amount of memory required for the AP reset area
1025 GetApResetVectorSize (
1026 IN MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
1031 Size
= AddressMap
->RendezvousFunnelSize
+
1032 AddressMap
->SwitchToRealSize
+
1033 sizeof (MP_CPU_EXCHANGE_INFO
);
1039 Allocate reset vector buffer.
1041 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
1044 AllocateResetVector (
1045 IN OUT CPU_MP_DATA
*CpuMpData
1048 UINTN ApResetVectorSize
;
1049 UINTN ApResetStackSize
;
1051 if (CpuMpData
->WakeupBuffer
== (UINTN
)-1) {
1052 ApResetVectorSize
= GetApResetVectorSize (&CpuMpData
->AddressMap
);
1054 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
1055 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*)(UINTN
)
1056 (CpuMpData
->WakeupBuffer
+
1057 CpuMpData
->AddressMap
.RendezvousFunnelSize
+
1058 CpuMpData
->AddressMap
.SwitchToRealSize
);
1059 CpuMpData
->WakeupBufferHigh
= AllocateCodeBuffer (
1060 CpuMpData
->AddressMap
.RendezvousFunnelSize
+
1061 CpuMpData
->AddressMap
.SwitchToRealSize
-
1062 CpuMpData
->AddressMap
.ModeTransitionOffset
1065 // The AP reset stack is only used by SEV-ES guests. Do not allocate it
1066 // if SEV-ES is not enabled. An SEV-SNP guest is also considered
1067 // an SEV-ES guest, but uses a different method of AP startup, eliminating
1068 // the need for the allocation.
1070 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs
) &&
1071 !ConfidentialComputingGuestHas (CCAttrAmdSevSnp
))
1074 // Stack location is based on ProcessorNumber, so use the total number
1075 // of processors for calculating the total stack area.
1077 ApResetStackSize
= (AP_RESET_STACK_SIZE
*
1078 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
1081 // Invoke GetWakeupBuffer a second time to allocate the stack area
1082 // below 1MB. The returned buffer will be page aligned and sized and
1083 // below the previously allocated buffer.
1085 CpuMpData
->SevEsAPResetStackStart
= GetWakeupBuffer (ApResetStackSize
);
1088 // Check to be sure that the "allocate below" behavior hasn't changed.
1089 // This will also catch a failed allocation, as "-1" is returned on
1092 if (CpuMpData
->SevEsAPResetStackStart
>= CpuMpData
->WakeupBuffer
) {
1095 "SEV-ES AP reset stack is not below wakeup buffer\n"
1104 BackupAndPrepareWakeupBuffer (CpuMpData
);
1108 Free AP reset vector buffer.
1110 @param[in] CpuMpData The pointer to CPU MP Data structure.
1114 IN CPU_MP_DATA
*CpuMpData
1118 // If SEV-ES is enabled, the reset area is needed for AP parking and
1119 // and AP startup in the OS, so the reset area is reserved. Do not
1120 // perform the restore as this will overwrite memory which has data
1121 // needed by SEV-ES.
1123 if (!CpuMpData
->UseSevEsAPMethod
) {
1124 RestoreWakeupBuffer (CpuMpData
);
1129 This function will be called by BSP to wakeup AP.
1131 @param[in] CpuMpData Pointer to CPU MP Data
1132 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
1133 FALSE: Send IPI to AP by ApicId
1134 @param[in] ProcessorNumber The handle number of specified processor
1135 @param[in] Procedure The function to be invoked by AP
1136 @param[in] ProcedureArgument The argument to be passed into AP function
1137 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1141 IN CPU_MP_DATA
*CpuMpData
,
1142 IN BOOLEAN Broadcast
,
1143 IN UINTN ProcessorNumber
,
1144 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
1145 IN VOID
*ProcedureArgument OPTIONAL
,
1146 IN BOOLEAN WakeUpDisabledAps
1149 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
1151 CPU_AP_DATA
*CpuData
;
1152 BOOLEAN ResetVectorRequired
;
1153 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1155 CpuMpData
->FinishedCount
= 0;
1156 ResetVectorRequired
= FALSE
;
1158 if (CpuMpData
->WakeUpByInitSipiSipi
||
1159 (CpuMpData
->InitFlag
!= ApInitDone
))
1161 ResetVectorRequired
= TRUE
;
1162 AllocateResetVector (CpuMpData
);
1163 AllocateSevEsAPMemory (CpuMpData
);
1164 FillExchangeInfoData (CpuMpData
);
1165 SaveLocalApicTimerSetting (CpuMpData
);
1168 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1170 // Get AP target C-state each time when waking up AP,
1171 // for it maybe updated by platform again
1173 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1176 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1179 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1180 if (Index
!= CpuMpData
->BspNumber
) {
1181 CpuData
= &CpuMpData
->CpuData
[Index
];
1183 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1184 // the AP procedure will be skipped for disabled AP because AP state
1185 // is not CpuStateReady.
1187 if ((GetApState (CpuData
) == CpuStateDisabled
) && !WakeUpDisabledAps
) {
1191 CpuData
->ApFunction
= (UINTN
)Procedure
;
1192 CpuData
->ApFunctionArgument
= (UINTN
)ProcedureArgument
;
1193 SetApState (CpuData
, CpuStateReady
);
1194 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1195 *(UINT32
*)CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1200 if (ResetVectorRequired
) {
1202 // For SEV-ES and SEV-SNP, the initial AP boot address will be defined by
1203 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address
1204 // from the original INIT-SIPI-SIPI.
1206 if (CpuMpData
->SevEsIsEnabled
) {
1207 SetSevEsJumpTable (ExchangeInfo
->BufferStart
);
1212 // Must use the INIT-SIPI-SIPI method for initial configuration in
1213 // order to obtain the APIC ID.
1215 if (CpuMpData
->SevSnpIsEnabled
&& (CpuMpData
->InitFlag
!= ApInitConfig
)) {
1216 SevSnpCreateAP (CpuMpData
, -1);
1218 SendInitSipiSipiAllExcludingSelf ((UINT32
)ExchangeInfo
->BufferStart
);
1222 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1223 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber
) > 0) {
1225 // The AP enumeration algorithm below is suitable only when the
1226 // platform can tell us the *exact* boot CPU count in advance.
1228 // The wait below finishes only when the detected AP count reaches
1229 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1230 // takes. If at least one AP fails to check in (meaning a platform
1231 // hardware bug), the detection hangs forever, by design. If the actual
1232 // boot CPU count in the system is higher than
1233 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1234 // misconfiguration), then some APs may complete initialization after
1235 // the wait finishes, and cause undefined behavior.
1237 TimedWaitForApFinish (
1239 PcdGet32 (PcdCpuBootLogicalProcessorNumber
) - 1,
1240 MAX_UINT32
// approx. 71 minutes
1244 // The AP enumeration algorithm below is suitable for two use cases.
1246 // (1) The check-in time for an individual AP is bounded, and APs run
1247 // through their initialization routines strongly concurrently. In
1248 // particular, the number of concurrently running APs
1249 // ("NumApsExecuting") is never expected to fall to zero
1250 // *temporarily* -- it is expected to fall to zero only when all
1251 // APs have checked-in.
1253 // In this case, the platform is supposed to set
1254 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1255 // enough for one AP to start initialization). The timeout will be
1256 // reached soon, and remaining APs are collected by watching
1257 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1258 // mid-process, while some APs have not completed initialization,
1259 // the behavior is undefined.
1261 // (2) The check-in time for an individual AP is unbounded, and/or APs
1262 // may complete their initializations widely spread out. In
1263 // particular, some APs may finish initialization before some APs
1266 // In this case, the platform is supposed to set
1267 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1268 // enumeration will always take that long (except when the boot CPU
1269 // count happens to be maximal, that is,
1270 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1271 // check-in before the timeout, and NumApsExecuting is assumed zero
1272 // at timeout. APs that miss the time-out may cause undefined
1275 TimedWaitForApFinish (
1277 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1278 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1281 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1287 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1289 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1290 CpuData
= &CpuMpData
->CpuData
[Index
];
1291 if (Index
!= CpuMpData
->BspNumber
) {
1292 WaitApWakeup (CpuData
->StartupApSignal
);
1297 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1298 CpuData
->ApFunction
= (UINTN
)Procedure
;
1299 CpuData
->ApFunctionArgument
= (UINTN
)ProcedureArgument
;
1300 SetApState (CpuData
, CpuStateReady
);
1302 // Wakeup specified AP
1304 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1305 *(UINT32
*)CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1306 if (ResetVectorRequired
) {
1307 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
1310 // For SEV-ES and SEV-SNP, the initial AP boot address will be defined by
1311 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address
1312 // from the original INIT-SIPI-SIPI.
1314 if (CpuMpData
->SevEsIsEnabled
) {
1315 SetSevEsJumpTable (ExchangeInfo
->BufferStart
);
1318 if (CpuMpData
->SevSnpIsEnabled
&& (CpuMpData
->InitFlag
!= ApInitConfig
)) {
1319 SevSnpCreateAP (CpuMpData
, (INTN
)ProcessorNumber
);
1322 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1323 (UINT32
)ExchangeInfo
->BufferStart
1329 // Wait specified AP waken up
1331 WaitApWakeup (CpuData
->StartupApSignal
);
1334 if (ResetVectorRequired
) {
1335 FreeResetVector (CpuMpData
);
1339 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1340 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1341 // S3SmmInitDone Ppi.
1343 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1347 Calculate timeout value and return the current performance counter value.
1349 Calculate the number of performance counter ticks required for a timeout.
1350 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1353 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1354 @param[out] CurrentTime Returns the current value of the performance counter.
1356 @return Expected time stamp counter for timeout.
1357 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1363 IN UINTN TimeoutInMicroseconds
,
1364 OUT UINT64
*CurrentTime
1367 UINT64 TimeoutInSeconds
;
1368 UINT64 TimestampCounterFreq
;
1371 // Read the current value of the performance counter
1373 *CurrentTime
= GetPerformanceCounter ();
1376 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1379 if (TimeoutInMicroseconds
== 0) {
1384 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1387 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1390 // Check the potential overflow before calculate the number of ticks for the timeout value.
1392 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1394 // Convert microseconds into seconds if direct multiplication overflows
1396 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1398 // Assertion if the final tick count exceeds MAX_UINT64
1400 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1401 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1404 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1405 // it by 1,000,000, to get the number of ticks for the timeout value.
1409 TimestampCounterFreq
,
1410 TimeoutInMicroseconds
1418 Checks whether timeout expires.
1420 Check whether the number of elapsed performance counter ticks required for
1421 a timeout condition has been reached.
1422 If Timeout is zero, which means infinity, return value is always FALSE.
1424 @param[in, out] PreviousTime On input, the value of the performance counter
1425 when it was last read.
1426 On output, the current value of the performance
1428 @param[in] TotalTime The total amount of elapsed time in performance
1430 @param[in] Timeout The number of performance counter ticks required
1431 to reach a timeout condition.
1433 @retval TRUE A timeout condition has been reached.
1434 @retval FALSE A timeout condition has not been reached.
1439 IN OUT UINT64
*PreviousTime
,
1440 IN UINT64
*TotalTime
,
1454 GetPerformanceCounterProperties (&Start
, &End
);
1455 Cycle
= End
- Start
;
1461 CurrentTime
= GetPerformanceCounter ();
1462 Delta
= (INT64
)(CurrentTime
- *PreviousTime
);
1471 *TotalTime
+= Delta
;
1472 *PreviousTime
= CurrentTime
;
1473 if (*TotalTime
> Timeout
) {
1481 Helper function that waits until the finished AP count reaches the specified
1482 limit, or the specified timeout elapses (whichever comes first).
1484 @param[in] CpuMpData Pointer to CPU MP Data.
1485 @param[in] FinishedApLimit The number of finished APs to wait for.
1486 @param[in] TimeLimit The number of microseconds to wait for.
1489 TimedWaitForApFinish (
1490 IN CPU_MP_DATA
*CpuMpData
,
1491 IN UINT32 FinishedApLimit
,
1496 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1497 // "infinity", so check for (TimeLimit == 0) explicitly.
1499 if (TimeLimit
== 0) {
1503 CpuMpData
->TotalTime
= 0;
1504 CpuMpData
->ExpectedTime
= CalculateTimeout (
1506 &CpuMpData
->CurrentTime
1508 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1510 &CpuMpData
->CurrentTime
,
1511 &CpuMpData
->TotalTime
,
1512 CpuMpData
->ExpectedTime
1518 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1521 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1524 DivU64x64Remainder (
1525 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1526 GetPerformanceCounterProperties (NULL
, NULL
),
1534 Reset an AP to Idle state.
1536 Any task being executed by the AP will be aborted and the AP
1537 will be waiting for a new task in Wait-For-SIPI state.
1539 @param[in] ProcessorNumber The handle number of processor.
1542 ResetProcessorToIdleState (
1543 IN UINTN ProcessorNumber
1546 CPU_MP_DATA
*CpuMpData
;
1548 CpuMpData
= GetCpuMpData ();
1550 CpuMpData
->InitFlag
= ApInitReconfig
;
1551 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1552 while (CpuMpData
->FinishedCount
< 1) {
1556 CpuMpData
->InitFlag
= ApInitDone
;
1558 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1562 Searches for the next waiting AP.
1564 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1566 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1568 @retval EFI_SUCCESS The next waiting AP has been found.
1569 @retval EFI_NOT_FOUND No waiting AP exists.
1573 GetNextWaitingProcessorNumber (
1574 OUT UINTN
*NextProcessorNumber
1577 UINTN ProcessorNumber
;
1578 CPU_MP_DATA
*CpuMpData
;
1580 CpuMpData
= GetCpuMpData ();
1582 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1583 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1584 *NextProcessorNumber
= ProcessorNumber
;
1589 return EFI_NOT_FOUND
;
1592 /** Checks status of specified AP.
1594 This function checks whether the specified AP has finished the task assigned
1595 by StartupThisAP(), and whether timeout expires.
1597 @param[in] ProcessorNumber The handle number of processor.
1599 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1600 @retval EFI_TIMEOUT The timeout expires.
1601 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1605 IN UINTN ProcessorNumber
1608 CPU_MP_DATA
*CpuMpData
;
1609 CPU_AP_DATA
*CpuData
;
1611 CpuMpData
= GetCpuMpData ();
1612 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1615 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1616 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1617 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1620 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1622 if (GetApState (CpuData
) == CpuStateFinished
) {
1623 if (CpuData
->Finished
!= NULL
) {
1624 *(CpuData
->Finished
) = TRUE
;
1627 SetApState (CpuData
, CpuStateIdle
);
1631 // If timeout expires for StartupThisAP(), report timeout.
1633 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1634 if (CpuData
->Finished
!= NULL
) {
1635 *(CpuData
->Finished
) = FALSE
;
1639 // Reset failed AP to idle state
1641 ResetProcessorToIdleState (ProcessorNumber
);
1647 return EFI_NOT_READY
;
1651 Checks status of all APs.
1653 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1654 and whether timeout expires.
1656 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1657 @retval EFI_TIMEOUT The timeout expires.
1658 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1665 UINTN ProcessorNumber
;
1666 UINTN NextProcessorNumber
;
1669 CPU_MP_DATA
*CpuMpData
;
1670 CPU_AP_DATA
*CpuData
;
1672 CpuMpData
= GetCpuMpData ();
1674 NextProcessorNumber
= 0;
1677 // Go through all APs that are responsible for the StartupAllAPs().
1679 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1680 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1684 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1686 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1687 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1688 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1690 if (GetApState (CpuData
) == CpuStateFinished
) {
1691 CpuMpData
->RunningCount
--;
1692 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1693 SetApState (CpuData
, CpuStateIdle
);
1696 // If in Single Thread mode, then search for the next waiting AP for execution.
1698 if (CpuMpData
->SingleThread
) {
1699 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1701 if (!EFI_ERROR (Status
)) {
1705 (UINT32
)NextProcessorNumber
,
1706 CpuMpData
->Procedure
,
1707 CpuMpData
->ProcArguments
,
1716 // If all APs finish, return EFI_SUCCESS.
1718 if (CpuMpData
->RunningCount
== 0) {
1723 // If timeout expires, report timeout.
1726 &CpuMpData
->CurrentTime
,
1727 &CpuMpData
->TotalTime
,
1728 CpuMpData
->ExpectedTime
1733 // If FailedCpuList is not NULL, record all failed APs in it.
1735 if (CpuMpData
->FailedCpuList
!= NULL
) {
1736 *CpuMpData
->FailedCpuList
=
1737 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1738 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1743 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1745 // Check whether this processor is responsible for StartupAllAPs().
1747 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1749 // Reset failed APs to idle state
1751 ResetProcessorToIdleState (ProcessorNumber
);
1752 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1753 if (CpuMpData
->FailedCpuList
!= NULL
) {
1754 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1759 if (CpuMpData
->FailedCpuList
!= NULL
) {
1760 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1766 return EFI_NOT_READY
;
1770 MP Initialize Library initialization.
1772 This service will allocate AP reset vector and wakeup all APs to do APs
1775 This service must be invoked before all other MP Initialize Library
1776 service are invoked.
1778 @retval EFI_SUCCESS MP initialization succeeds.
1779 @retval Others MP initialization fails.
1784 MpInitLibInitialize (
1788 CPU_MP_DATA
*OldCpuMpData
;
1789 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1790 UINT32 MaxLogicalProcessorNumber
;
1792 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1793 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1795 UINT32 MonitorFilterSize
;
1798 CPU_MP_DATA
*CpuMpData
;
1800 UINT8
*MonitorBuffer
;
1802 UINTN ApResetVectorSize
;
1803 UINTN BackupBufferAddr
;
1806 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1807 if (OldCpuMpData
== NULL
) {
1808 MaxLogicalProcessorNumber
= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
1810 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1813 ASSERT (MaxLogicalProcessorNumber
!= 0);
1815 AsmGetAddressMap (&AddressMap
);
1816 ApResetVectorSize
= GetApResetVectorSize (&AddressMap
);
1817 ApStackSize
= PcdGet32 (PcdCpuApStackSize
);
1818 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1821 // Save BSP's Control registers for APs.
1823 SaveVolatileRegisters (&VolatileRegisters
);
1825 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1826 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1827 BufferSize
+= ApResetVectorSize
;
1828 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1829 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1830 BufferSize
+= sizeof (CPU_MP_DATA
);
1831 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1832 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1833 ASSERT (MpBuffer
!= NULL
);
1834 ZeroMem (MpBuffer
, BufferSize
);
1835 Buffer
= (UINTN
)MpBuffer
;
1838 // The layout of the Buffer is as below:
1840 // +--------------------+ <-- Buffer
1842 // +--------------------+ <-- MonitorBuffer
1843 // AP Monitor Filters (N)
1844 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1846 // +--------------------+
1848 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1849 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1850 // +--------------------+ <-- CpuMpData
1852 // +--------------------+ <-- CpuMpData->CpuData
1854 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1855 // CPU_INFO_IN_HOB (N)
1856 // +--------------------+
1858 MonitorBuffer
= (UINT8
*)(Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1859 BackupBufferAddr
= (UINTN
)MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1860 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1861 CpuMpData
= (CPU_MP_DATA
*)(ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1862 CpuMpData
->Buffer
= Buffer
;
1863 CpuMpData
->CpuApStackSize
= ApStackSize
;
1864 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1865 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1866 CpuMpData
->WakeupBuffer
= (UINTN
)-1;
1867 CpuMpData
->CpuCount
= 1;
1868 CpuMpData
->BspNumber
= 0;
1869 CpuMpData
->WaitEvent
= NULL
;
1870 CpuMpData
->SwitchBspFlag
= FALSE
;
1871 CpuMpData
->CpuData
= (CPU_AP_DATA
*)(CpuMpData
+ 1);
1872 CpuMpData
->CpuInfoInHob
= (UINT64
)(UINTN
)(CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1873 InitializeSpinLock (&CpuMpData
->MpLock
);
1874 CpuMpData
->SevEsIsEnabled
= ConfidentialComputingGuestHas (CCAttrAmdSevEs
);
1875 CpuMpData
->SevSnpIsEnabled
= ConfidentialComputingGuestHas (CCAttrAmdSevSnp
);
1876 CpuMpData
->SevEsAPBuffer
= (UINTN
)-1;
1877 CpuMpData
->GhcbBase
= PcdGet64 (PcdGhcbBase
);
1878 CpuMpData
->UseSevEsAPMethod
= CpuMpData
->SevEsIsEnabled
&& !CpuMpData
->SevSnpIsEnabled
;
1880 if (CpuMpData
->SevSnpIsEnabled
) {
1881 ASSERT ((PcdGet64 (PcdGhcbHypervisorFeatures
) & GHCB_HV_FEATURES_SNP_AP_CREATE
) == GHCB_HV_FEATURES_SNP_AP_CREATE
);
1885 // Make sure no memory usage outside of the allocated buffer.
1888 (CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1893 // Duplicate BSP's IDT to APs.
1894 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1896 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1897 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1899 // Don't pass BSP's TR to APs to avoid AP init failure.
1901 VolatileRegisters
.Tr
= 0;
1902 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1904 // Set BSP basic information
1906 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1908 // Save assembly code information
1910 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1912 // Finally set AP loop mode
1914 CpuMpData
->ApLoopMode
= ApLoopMode
;
1915 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1917 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1920 // Set up APs wakeup signal buffer
1922 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1923 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1924 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1928 // Enable the local APIC for Virtual Wire Mode.
1930 ProgramVirtualWireMode ();
1932 if (OldCpuMpData
== NULL
) {
1933 if (MaxLogicalProcessorNumber
> 1) {
1935 // Wakeup all APs and calculate the processor count in system
1937 CollectProcessorCount (CpuMpData
);
1941 // APs have been wakeup before, just get the CPU Information
1944 OldCpuMpData
->NewCpuMpData
= CpuMpData
;
1945 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1946 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1947 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1948 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
1949 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1950 InitializeSpinLock (&CpuMpData
->CpuData
[Index
].ApLock
);
1951 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0) ? TRUE
: FALSE
;
1952 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1956 if (!GetMicrocodePatchInfoFromHob (
1957 &CpuMpData
->MicrocodePatchAddress
,
1958 &CpuMpData
->MicrocodePatchRegionSize
1962 // The microcode patch information cache HOB does not exist, which means
1963 // the microcode patches data has not been loaded into memory yet
1965 ShadowMicrocodeUpdatePatch (CpuMpData
);
1969 // Detect and apply Microcode on BSP
1971 MicrocodeDetect (CpuMpData
, CpuMpData
->BspNumber
);
1973 // Store BSP's MTRR setting
1975 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1978 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1980 if (CpuMpData
->CpuCount
> 1) {
1981 if (OldCpuMpData
!= NULL
) {
1983 // Only needs to use this flag for DXE phase to update the wake up
1984 // buffer. Wakeup buffer allocated in PEI phase is no longer valid
1987 CpuMpData
->InitFlag
= ApInitReconfig
;
1990 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1992 // Wait for all APs finished initialization
1994 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1998 if (OldCpuMpData
!= NULL
) {
1999 CpuMpData
->InitFlag
= ApInitDone
;
2002 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
2003 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
2008 // Dump the microcode revision for each core.
2010 DEBUG_CODE_BEGIN ();
2012 UINT32 ExpectedMicrocodeRevision
;
2014 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
2015 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
2016 GetProcessorLocationByApicId (CpuInfoInHob
[Index
].InitialApicId
, NULL
, NULL
, &ThreadId
);
2017 if (ThreadId
== 0) {
2019 // MicrocodeDetect() loads microcode in first thread of each core, so,
2020 // CpuMpData->CpuData[Index].MicrocodeEntryAddr is initialized only for first thread of each core.
2022 ExpectedMicrocodeRevision
= 0;
2023 if (CpuMpData
->CpuData
[Index
].MicrocodeEntryAddr
!= 0) {
2024 ExpectedMicrocodeRevision
= ((CPU_MICROCODE_HEADER
*)(UINTN
)CpuMpData
->CpuData
[Index
].MicrocodeEntryAddr
)->UpdateRevision
;
2029 "CPU[%04d]: Microcode revision = %08x, expected = %08x\n",
2031 CpuMpData
->CpuData
[Index
].MicrocodeRevision
,
2032 ExpectedMicrocodeRevision
2039 // Initialize global data for MP support
2041 InitMpGlobalData (CpuMpData
);
2047 Gets detailed MP-related information on the requested processor at the
2048 instant this call is made. This service may only be called from the BSP.
2050 @param[in] ProcessorNumber The handle number of processor.
2051 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
2052 the requested processor is deposited.
2053 @param[out] HealthData Return processor health data.
2055 @retval EFI_SUCCESS Processor information was returned.
2056 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2057 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
2058 @retval EFI_NOT_FOUND The processor with the handle specified by
2059 ProcessorNumber does not exist in the platform.
2060 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2065 MpInitLibGetProcessorInfo (
2066 IN UINTN ProcessorNumber
,
2067 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
2068 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
2071 CPU_MP_DATA
*CpuMpData
;
2073 CPU_INFO_IN_HOB
*CpuInfoInHob
;
2074 UINTN OriginalProcessorNumber
;
2076 CpuMpData
= GetCpuMpData ();
2077 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
2080 // Lower 24 bits contains the actual processor number.
2082 OriginalProcessorNumber
= ProcessorNumber
;
2083 ProcessorNumber
&= BIT24
- 1;
2086 // Check whether caller processor is BSP
2088 MpInitLibWhoAmI (&CallerNumber
);
2089 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2090 return EFI_DEVICE_ERROR
;
2093 if (ProcessorInfoBuffer
== NULL
) {
2094 return EFI_INVALID_PARAMETER
;
2097 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2098 return EFI_NOT_FOUND
;
2101 ProcessorInfoBuffer
->ProcessorId
= (UINT64
)CpuInfoInHob
[ProcessorNumber
].ApicId
;
2102 ProcessorInfoBuffer
->StatusFlag
= 0;
2103 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2104 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
2107 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
2108 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
2111 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2112 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
2114 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
2118 // Get processor location information
2120 GetProcessorLocationByApicId (
2121 CpuInfoInHob
[ProcessorNumber
].ApicId
,
2122 &ProcessorInfoBuffer
->Location
.Package
,
2123 &ProcessorInfoBuffer
->Location
.Core
,
2124 &ProcessorInfoBuffer
->Location
.Thread
2127 if ((OriginalProcessorNumber
& CPU_V2_EXTENDED_TOPOLOGY
) != 0) {
2128 GetProcessorLocation2ByApicId (
2129 CpuInfoInHob
[ProcessorNumber
].ApicId
,
2130 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Package
,
2131 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Die
,
2132 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Tile
,
2133 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Module
,
2134 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Core
,
2135 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Thread
2139 if (HealthData
!= NULL
) {
2140 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
2147 Worker function to switch the requested AP to be the BSP from that point onward.
2149 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
2150 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
2151 enabled AP. Otherwise, it will be disabled.
2153 @retval EFI_SUCCESS BSP successfully switched.
2154 @retval others Failed to switch BSP.
2159 IN UINTN ProcessorNumber
,
2160 IN BOOLEAN EnableOldBSP
2163 CPU_MP_DATA
*CpuMpData
;
2166 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
2167 BOOLEAN OldInterruptState
;
2168 BOOLEAN OldTimerInterruptState
;
2171 // Save and Disable Local APIC timer interrupt
2173 OldTimerInterruptState
= GetApicTimerInterruptState ();
2174 DisableApicTimerInterrupt ();
2176 // Before send both BSP and AP to a procedure to exchange their roles,
2177 // interrupt must be disabled. This is because during the exchange role
2178 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
2179 // be corrupted, since interrupt return address will be pushed to stack
2182 OldInterruptState
= SaveAndDisableInterrupts ();
2185 // Mask LINT0 & LINT1 for the old BSP
2187 DisableLvtInterrupts ();
2189 CpuMpData
= GetCpuMpData ();
2192 // Check whether caller processor is BSP
2194 MpInitLibWhoAmI (&CallerNumber
);
2195 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2196 return EFI_DEVICE_ERROR
;
2199 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2200 return EFI_NOT_FOUND
;
2204 // Check whether specified AP is disabled
2206 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
2207 if (State
== CpuStateDisabled
) {
2208 return EFI_INVALID_PARAMETER
;
2212 // Check whether ProcessorNumber specifies the current BSP
2214 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2215 return EFI_INVALID_PARAMETER
;
2219 // Check whether specified AP is busy
2221 if (State
== CpuStateBusy
) {
2222 return EFI_NOT_READY
;
2225 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2226 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2227 CpuMpData
->SwitchBspFlag
= TRUE
;
2228 CpuMpData
->NewBspNumber
= ProcessorNumber
;
2231 // Clear the BSP bit of MSR_IA32_APIC_BASE
2233 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2234 ApicBaseMsr
.Bits
.BSP
= 0;
2235 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2238 // Need to wakeUp AP (future BSP).
2240 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
2242 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
2245 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2247 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2248 ApicBaseMsr
.Bits
.BSP
= 1;
2249 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2250 ProgramVirtualWireMode ();
2253 // Wait for old BSP finished AP task
2255 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
2259 CpuMpData
->SwitchBspFlag
= FALSE
;
2261 // Set old BSP enable state
2263 if (!EnableOldBSP
) {
2264 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
2266 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2270 // Save new BSP number
2272 CpuMpData
->BspNumber
= (UINT32
)ProcessorNumber
;
2275 // Restore interrupt state.
2277 SetInterruptState (OldInterruptState
);
2279 if (OldTimerInterruptState
) {
2280 EnableApicTimerInterrupt ();
2287 Worker function to let the caller enable or disable an AP from this point onward.
2288 This service may only be called from the BSP.
2290 @param[in] ProcessorNumber The handle number of AP.
2291 @param[in] EnableAP Specifies the new state for the processor for
2292 enabled, FALSE for disabled.
2293 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2294 the new health status of the AP.
2296 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2297 @retval others Failed to Enable/Disable AP.
2301 EnableDisableApWorker (
2302 IN UINTN ProcessorNumber
,
2303 IN BOOLEAN EnableAP
,
2304 IN UINT32
*HealthFlag OPTIONAL
2307 CPU_MP_DATA
*CpuMpData
;
2310 CpuMpData
= GetCpuMpData ();
2313 // Check whether caller processor is BSP
2315 MpInitLibWhoAmI (&CallerNumber
);
2316 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2317 return EFI_DEVICE_ERROR
;
2320 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2321 return EFI_INVALID_PARAMETER
;
2324 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2325 return EFI_NOT_FOUND
;
2329 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2331 ResetProcessorToIdleState (ProcessorNumber
);
2334 if (HealthFlag
!= NULL
) {
2335 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2336 (BOOLEAN
)((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2343 This return the handle number for the calling processor. This service may be
2344 called from the BSP and APs.
2346 @param[out] ProcessorNumber Pointer to the handle number of AP.
2347 The range is from 0 to the total number of
2348 logical processors minus 1. The total number of
2349 logical processors can be retrieved by
2350 MpInitLibGetNumberOfProcessors().
2352 @retval EFI_SUCCESS The current processor handle number was returned
2354 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2355 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2361 OUT UINTN
*ProcessorNumber
2364 CPU_MP_DATA
*CpuMpData
;
2366 if (ProcessorNumber
== NULL
) {
2367 return EFI_INVALID_PARAMETER
;
2370 CpuMpData
= GetCpuMpData ();
2372 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2376 Retrieves the number of logical processor in the platform and the number of
2377 those logical processors that are enabled on this boot. This service may only
2378 be called from the BSP.
2380 @param[out] NumberOfProcessors Pointer to the total number of logical
2381 processors in the system, including the BSP
2383 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2384 processors that exist in system, including
2387 @retval EFI_SUCCESS The number of logical processors and enabled
2388 logical processors was retrieved.
2389 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2390 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2392 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2397 MpInitLibGetNumberOfProcessors (
2398 OUT UINTN
*NumberOfProcessors OPTIONAL
,
2399 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2402 CPU_MP_DATA
*CpuMpData
;
2404 UINTN ProcessorNumber
;
2405 UINTN EnabledProcessorNumber
;
2408 CpuMpData
= GetCpuMpData ();
2410 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2411 return EFI_INVALID_PARAMETER
;
2415 // Check whether caller processor is BSP
2417 MpInitLibWhoAmI (&CallerNumber
);
2418 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2419 return EFI_DEVICE_ERROR
;
2422 ProcessorNumber
= CpuMpData
->CpuCount
;
2423 EnabledProcessorNumber
= 0;
2424 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2425 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2426 EnabledProcessorNumber
++;
2430 if (NumberOfProcessors
!= NULL
) {
2431 *NumberOfProcessors
= ProcessorNumber
;
2434 if (NumberOfEnabledProcessors
!= NULL
) {
2435 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2442 Worker function to execute a caller provided function on all enabled APs.
2444 @param[in] Procedure A pointer to the function to be run on
2445 enabled APs of the system.
2446 @param[in] SingleThread If TRUE, then all the enabled APs execute
2447 the function specified by Procedure one by
2448 one, in ascending order of processor handle
2449 number. If FALSE, then all the enabled APs
2450 execute the function specified by Procedure
2452 @param[in] ExcludeBsp Whether let BSP also trig this task.
2453 @param[in] WaitEvent The event created by the caller with CreateEvent()
2455 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2456 APs to return from Procedure, either for
2457 blocking or non-blocking mode.
2458 @param[in] ProcedureArgument The parameter passed into Procedure for
2460 @param[out] FailedCpuList If all APs finish successfully, then its
2461 content is set to NULL. If not all APs
2462 finish before timeout expires, then its
2463 content is set to address of the buffer
2464 holding handle numbers of the failed APs.
2466 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2467 the timeout expired.
2468 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2470 @retval others Failed to Startup all APs.
2474 StartupAllCPUsWorker (
2475 IN EFI_AP_PROCEDURE Procedure
,
2476 IN BOOLEAN SingleThread
,
2477 IN BOOLEAN ExcludeBsp
,
2478 IN EFI_EVENT WaitEvent OPTIONAL
,
2479 IN UINTN TimeoutInMicroseconds
,
2480 IN VOID
*ProcedureArgument OPTIONAL
,
2481 OUT UINTN
**FailedCpuList OPTIONAL
2485 CPU_MP_DATA
*CpuMpData
;
2486 UINTN ProcessorCount
;
2487 UINTN ProcessorNumber
;
2489 CPU_AP_DATA
*CpuData
;
2490 BOOLEAN HasEnabledAp
;
2493 CpuMpData
= GetCpuMpData ();
2495 if (FailedCpuList
!= NULL
) {
2496 *FailedCpuList
= NULL
;
2499 if ((CpuMpData
->CpuCount
== 1) && ExcludeBsp
) {
2500 return EFI_NOT_STARTED
;
2503 if (Procedure
== NULL
) {
2504 return EFI_INVALID_PARAMETER
;
2508 // Check whether caller processor is BSP
2510 MpInitLibWhoAmI (&CallerNumber
);
2511 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2512 return EFI_DEVICE_ERROR
;
2518 CheckAndUpdateApsStatus ();
2520 ProcessorCount
= CpuMpData
->CpuCount
;
2521 HasEnabledAp
= FALSE
;
2523 // Check whether all enabled APs are idle.
2524 // If any enabled AP is not idle, return EFI_NOT_READY.
2526 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2527 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2528 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2529 ApState
= GetApState (CpuData
);
2530 if (ApState
!= CpuStateDisabled
) {
2531 HasEnabledAp
= TRUE
;
2532 if (ApState
!= CpuStateIdle
) {
2534 // If any enabled APs are busy, return EFI_NOT_READY.
2536 return EFI_NOT_READY
;
2542 if (!HasEnabledAp
&& ExcludeBsp
) {
2544 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2546 return EFI_NOT_STARTED
;
2549 CpuMpData
->RunningCount
= 0;
2550 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2551 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2552 CpuData
->Waiting
= FALSE
;
2553 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2554 if (CpuData
->State
== CpuStateIdle
) {
2556 // Mark this processor as responsible for current calling.
2558 CpuData
->Waiting
= TRUE
;
2559 CpuMpData
->RunningCount
++;
2564 CpuMpData
->Procedure
= Procedure
;
2565 CpuMpData
->ProcArguments
= ProcedureArgument
;
2566 CpuMpData
->SingleThread
= SingleThread
;
2567 CpuMpData
->FinishedCount
= 0;
2568 CpuMpData
->FailedCpuList
= FailedCpuList
;
2569 CpuMpData
->ExpectedTime
= CalculateTimeout (
2570 TimeoutInMicroseconds
,
2571 &CpuMpData
->CurrentTime
2573 CpuMpData
->TotalTime
= 0;
2574 CpuMpData
->WaitEvent
= WaitEvent
;
2576 if (!SingleThread
) {
2577 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2579 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2580 if (ProcessorNumber
== CallerNumber
) {
2584 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2585 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2595 Procedure (ProcedureArgument
);
2598 Status
= EFI_SUCCESS
;
2599 if (WaitEvent
== NULL
) {
2601 Status
= CheckAllAPs ();
2602 } while (Status
== EFI_NOT_READY
);
2609 Worker function to let the caller get one enabled AP to execute a caller-provided
2612 @param[in] Procedure A pointer to the function to be run on
2613 enabled APs of the system.
2614 @param[in] ProcessorNumber The handle number of the AP.
2615 @param[in] WaitEvent The event created by the caller with CreateEvent()
2617 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2618 APs to return from Procedure, either for
2619 blocking or non-blocking mode.
2620 @param[in] ProcedureArgument The parameter passed into Procedure for
2622 @param[out] Finished If AP returns from Procedure before the
2623 timeout expires, its content is set to TRUE.
2624 Otherwise, the value is set to FALSE.
2626 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2627 the timeout expires.
2628 @retval others Failed to Startup AP.
2632 StartupThisAPWorker (
2633 IN EFI_AP_PROCEDURE Procedure
,
2634 IN UINTN ProcessorNumber
,
2635 IN EFI_EVENT WaitEvent OPTIONAL
,
2636 IN UINTN TimeoutInMicroseconds
,
2637 IN VOID
*ProcedureArgument OPTIONAL
,
2638 OUT BOOLEAN
*Finished OPTIONAL
2642 CPU_MP_DATA
*CpuMpData
;
2643 CPU_AP_DATA
*CpuData
;
2646 CpuMpData
= GetCpuMpData ();
2648 if (Finished
!= NULL
) {
2653 // Check whether caller processor is BSP
2655 MpInitLibWhoAmI (&CallerNumber
);
2656 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2657 return EFI_DEVICE_ERROR
;
2661 // Check whether processor with the handle specified by ProcessorNumber exists
2663 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2664 return EFI_NOT_FOUND
;
2668 // Check whether specified processor is BSP
2670 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2671 return EFI_INVALID_PARAMETER
;
2675 // Check parameter Procedure
2677 if (Procedure
== NULL
) {
2678 return EFI_INVALID_PARAMETER
;
2684 CheckAndUpdateApsStatus ();
2687 // Check whether specified AP is disabled
2689 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2690 return EFI_INVALID_PARAMETER
;
2694 // If WaitEvent is not NULL, execute in non-blocking mode.
2695 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2696 // CheckAPsStatus() will check completion and timeout periodically.
2698 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2699 CpuData
->WaitEvent
= WaitEvent
;
2700 CpuData
->Finished
= Finished
;
2701 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2702 CpuData
->TotalTime
= 0;
2704 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2707 // If WaitEvent is NULL, execute in blocking mode.
2708 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2710 Status
= EFI_SUCCESS
;
2711 if (WaitEvent
== NULL
) {
2713 Status
= CheckThisAP (ProcessorNumber
);
2714 } while (Status
== EFI_NOT_READY
);
2721 Get pointer to CPU MP Data structure from GUIDed HOB.
2723 @return The pointer to CPU MP Data structure.
2726 GetCpuMpDataFromGuidedHob (
2730 EFI_HOB_GUID_TYPE
*GuidHob
;
2732 CPU_MP_DATA
*CpuMpData
;
2735 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2736 if (GuidHob
!= NULL
) {
2737 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2738 CpuMpData
= (CPU_MP_DATA
*)(*(UINTN
*)DataInHob
);
2745 This service executes a caller provided function on all enabled CPUs.
2747 @param[in] Procedure A pointer to the function to be run on
2748 enabled APs of the system. See type
2750 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2751 APs to return from Procedure, either for
2752 blocking or non-blocking mode. Zero means
2753 infinity. TimeoutInMicroseconds is ignored
2755 @param[in] ProcedureArgument The parameter passed into Procedure for
2758 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2759 the timeout expired.
2760 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2761 to all enabled CPUs.
2762 @retval EFI_DEVICE_ERROR Caller processor is AP.
2763 @retval EFI_NOT_READY Any enabled APs are busy.
2764 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2765 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2766 all enabled APs have finished.
2767 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2772 MpInitLibStartupAllCPUs (
2773 IN EFI_AP_PROCEDURE Procedure
,
2774 IN UINTN TimeoutInMicroseconds
,
2775 IN VOID
*ProcedureArgument OPTIONAL
2778 return StartupAllCPUsWorker (
2783 TimeoutInMicroseconds
,
2790 The function check if the specified Attr is set.
2792 @param[in] CurrentAttr The current attribute.
2793 @param[in] Attr The attribute to check.
2795 @retval TRUE The specified Attr is set.
2796 @retval FALSE The specified Attr is not set.
2801 AmdMemEncryptionAttrCheck (
2802 IN UINT64 CurrentAttr
,
2803 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
2809 // SEV is automatically enabled if SEV-ES or SEV-SNP is active.
2811 return CurrentAttr
>= CCAttrAmdSev
;
2812 case CCAttrAmdSevEs
:
2814 // SEV-ES is automatically enabled if SEV-SNP is active.
2816 return CurrentAttr
>= CCAttrAmdSevEs
;
2817 case CCAttrAmdSevSnp
:
2818 return CurrentAttr
== CCAttrAmdSevSnp
;
2825 Check if the specified confidential computing attribute is active.
2827 @param[in] Attr The attribute to check.
2829 @retval TRUE The specified Attr is active.
2830 @retval FALSE The specified Attr is not active.
2835 ConfidentialComputingGuestHas (
2836 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
2842 // Get the current CC attribute.
2844 CurrentAttr
= PcdGet64 (PcdConfidentialComputingGuestAttr
);
2847 // If attr is for the AMD group then call AMD specific checks.
2849 if (((RShiftU64 (CurrentAttr
, 8)) & 0xff) == 1) {
2850 return AmdMemEncryptionAttrCheck (CurrentAttr
, Attr
);
2853 return (CurrentAttr
== Attr
);