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 Calculate the size of the reset vector.
854 @param[in] AddressMap The pointer to Address Map structure.
855 @param[out] SizeBelow1Mb Return the size of below 1MB memory for AP reset area.
856 @param[out] SizeAbove1Mb Return the size of abvoe 1MB memory for AP reset area.
860 GetApResetVectorSize (
861 IN MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
,
862 OUT UINTN
*SizeBelow1Mb OPTIONAL
,
863 OUT UINTN
*SizeAbove1Mb OPTIONAL
866 if (SizeBelow1Mb
!= NULL
) {
867 *SizeBelow1Mb
= AddressMap
->ModeTransitionOffset
+ sizeof (MP_CPU_EXCHANGE_INFO
);
870 if (SizeAbove1Mb
!= NULL
) {
871 *SizeAbove1Mb
= AddressMap
->RendezvousFunnelSize
- AddressMap
->ModeTransitionOffset
;
876 This function will fill the exchange info structure.
878 @param[in] CpuMpData Pointer to CPU MP Data
882 FillExchangeInfoData (
883 IN CPU_MP_DATA
*CpuMpData
886 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
888 IA32_SEGMENT_DESCRIPTOR
*Selector
;
891 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
892 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
893 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
894 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
895 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
897 ExchangeInfo
->CodeSegment
= AsmReadCs ();
898 ExchangeInfo
->DataSegment
= AsmReadDs ();
900 ExchangeInfo
->Cr3
= AsmReadCr3 ();
902 ExchangeInfo
->CFunction
= (UINTN
)ApWakeupFunction
;
903 ExchangeInfo
->ApIndex
= 0;
904 ExchangeInfo
->NumApsExecuting
= 0;
905 ExchangeInfo
->InitFlag
= (UINTN
)CpuMpData
->InitFlag
;
906 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
907 ExchangeInfo
->CpuMpData
= CpuMpData
;
909 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
911 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
914 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
915 // to determin whether 5-Level Paging is enabled.
916 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
917 // current system setting.
918 // Using latter way is simpler because it also eliminates the needs to
919 // check whether platform wants to enable it.
921 Cr4
.UintN
= AsmReadCr4 ();
922 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
)(Cr4
.Bits
.LA57
== 1);
923 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
925 ExchangeInfo
->SevEsIsEnabled
= CpuMpData
->SevEsIsEnabled
;
926 ExchangeInfo
->SevSnpIsEnabled
= CpuMpData
->SevSnpIsEnabled
;
927 ExchangeInfo
->GhcbBase
= (UINTN
)CpuMpData
->GhcbBase
;
930 // Populate SEV-ES specific exchange data.
932 if (ExchangeInfo
->SevSnpIsEnabled
) {
933 FillExchangeInfoDataSevEs (ExchangeInfo
);
937 // Get the BSP's data of GDT and IDT
939 AsmReadGdtr ((IA32_DESCRIPTOR
*)&ExchangeInfo
->GdtrProfile
);
940 AsmReadIdtr ((IA32_DESCRIPTOR
*)&ExchangeInfo
->IdtrProfile
);
943 // Find a 32-bit code segment
945 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
946 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
948 if ((Selector
->Bits
.L
== 0) && (Selector
->Bits
.Type
>= 8)) {
949 ExchangeInfo
->ModeTransitionSegment
=
950 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
955 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
958 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
960 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
961 (UINT32
)ExchangeInfo
->ModeOffset
-
962 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
963 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
967 Helper function that waits until the finished AP count reaches the specified
968 limit, or the specified timeout elapses (whichever comes first).
970 @param[in] CpuMpData Pointer to CPU MP Data.
971 @param[in] FinishedApLimit The number of finished APs to wait for.
972 @param[in] TimeLimit The number of microseconds to wait for.
975 TimedWaitForApFinish (
976 IN CPU_MP_DATA
*CpuMpData
,
977 IN UINT32 FinishedApLimit
,
982 Get available system memory below 1MB by specified size.
984 @param[in] CpuMpData The pointer to CPU MP Data structure.
987 BackupAndPrepareWakeupBuffer (
988 IN CPU_MP_DATA
*CpuMpData
992 (VOID
*)CpuMpData
->BackupBuffer
,
993 (VOID
*)CpuMpData
->WakeupBuffer
,
994 CpuMpData
->BackupBufferSize
997 (VOID
*)CpuMpData
->WakeupBuffer
,
998 (VOID
*)CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
999 CpuMpData
->BackupBufferSize
- sizeof (MP_CPU_EXCHANGE_INFO
)
1004 Restore wakeup buffer data.
1006 @param[in] CpuMpData The pointer to CPU MP Data structure.
1009 RestoreWakeupBuffer (
1010 IN CPU_MP_DATA
*CpuMpData
1014 (VOID
*)CpuMpData
->WakeupBuffer
,
1015 (VOID
*)CpuMpData
->BackupBuffer
,
1016 CpuMpData
->BackupBufferSize
1021 Allocate reset vector buffer.
1023 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
1026 AllocateResetVectorBelow1Mb (
1027 IN OUT CPU_MP_DATA
*CpuMpData
1030 UINTN ApResetStackSize
;
1032 if (CpuMpData
->WakeupBuffer
== (UINTN
)-1) {
1033 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (CpuMpData
->BackupBufferSize
);
1034 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*)(UINTN
)
1035 (CpuMpData
->WakeupBuffer
+ CpuMpData
->BackupBufferSize
- sizeof (MP_CPU_EXCHANGE_INFO
));
1038 "AP Vector: 16-bit = %p/%x, ExchangeInfo = %p/%x\n",
1039 CpuMpData
->WakeupBuffer
,
1040 CpuMpData
->BackupBufferSize
- sizeof (MP_CPU_EXCHANGE_INFO
),
1041 CpuMpData
->MpCpuExchangeInfo
,
1042 sizeof (MP_CPU_EXCHANGE_INFO
)
1045 // The AP reset stack is only used by SEV-ES guests. Do not allocate it
1046 // if SEV-ES is not enabled. An SEV-SNP guest is also considered
1047 // an SEV-ES guest, but uses a different method of AP startup, eliminating
1048 // the need for the allocation.
1050 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs
) &&
1051 !ConfidentialComputingGuestHas (CCAttrAmdSevSnp
))
1054 // Stack location is based on ProcessorNumber, so use the total number
1055 // of processors for calculating the total stack area.
1057 ApResetStackSize
= (AP_RESET_STACK_SIZE
*
1058 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
1061 // Invoke GetWakeupBuffer a second time to allocate the stack area
1062 // below 1MB. The returned buffer will be page aligned and sized and
1063 // below the previously allocated buffer.
1065 CpuMpData
->SevEsAPResetStackStart
= GetWakeupBuffer (ApResetStackSize
);
1068 // Check to be sure that the "allocate below" behavior hasn't changed.
1069 // This will also catch a failed allocation, as "-1" is returned on
1072 if (CpuMpData
->SevEsAPResetStackStart
>= CpuMpData
->WakeupBuffer
) {
1075 "SEV-ES AP reset stack is not below wakeup buffer\n"
1084 BackupAndPrepareWakeupBuffer (CpuMpData
);
1088 Free AP reset vector buffer.
1090 @param[in] CpuMpData The pointer to CPU MP Data structure.
1094 IN CPU_MP_DATA
*CpuMpData
1098 // If SEV-ES is enabled, the reset area is needed for AP parking and
1099 // and AP startup in the OS, so the reset area is reserved. Do not
1100 // perform the restore as this will overwrite memory which has data
1101 // needed by SEV-ES.
1103 if (!CpuMpData
->UseSevEsAPMethod
) {
1104 RestoreWakeupBuffer (CpuMpData
);
1109 This function will be called by BSP to wakeup AP.
1111 @param[in] CpuMpData Pointer to CPU MP Data
1112 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
1113 FALSE: Send IPI to AP by ApicId
1114 @param[in] ProcessorNumber The handle number of specified processor
1115 @param[in] Procedure The function to be invoked by AP
1116 @param[in] ProcedureArgument The argument to be passed into AP function
1117 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1121 IN CPU_MP_DATA
*CpuMpData
,
1122 IN BOOLEAN Broadcast
,
1123 IN UINTN ProcessorNumber
,
1124 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
1125 IN VOID
*ProcedureArgument OPTIONAL
,
1126 IN BOOLEAN WakeUpDisabledAps
1129 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
1131 CPU_AP_DATA
*CpuData
;
1132 BOOLEAN ResetVectorRequired
;
1133 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1135 CpuMpData
->FinishedCount
= 0;
1136 ResetVectorRequired
= FALSE
;
1138 if (CpuMpData
->WakeUpByInitSipiSipi
||
1139 (CpuMpData
->InitFlag
!= ApInitDone
))
1141 ResetVectorRequired
= TRUE
;
1142 AllocateResetVectorBelow1Mb (CpuMpData
);
1143 AllocateSevEsAPMemory (CpuMpData
);
1144 FillExchangeInfoData (CpuMpData
);
1145 SaveLocalApicTimerSetting (CpuMpData
);
1148 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1150 // Get AP target C-state each time when waking up AP,
1151 // for it maybe updated by platform again
1153 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1156 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1159 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1160 if (Index
!= CpuMpData
->BspNumber
) {
1161 CpuData
= &CpuMpData
->CpuData
[Index
];
1163 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1164 // the AP procedure will be skipped for disabled AP because AP state
1165 // is not CpuStateReady.
1167 if ((GetApState (CpuData
) == CpuStateDisabled
) && !WakeUpDisabledAps
) {
1171 CpuData
->ApFunction
= (UINTN
)Procedure
;
1172 CpuData
->ApFunctionArgument
= (UINTN
)ProcedureArgument
;
1173 SetApState (CpuData
, CpuStateReady
);
1174 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1175 *(UINT32
*)CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1180 if (ResetVectorRequired
) {
1182 // For SEV-ES and SEV-SNP, the initial AP boot address will be defined by
1183 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address
1184 // from the original INIT-SIPI-SIPI.
1186 if (CpuMpData
->SevEsIsEnabled
) {
1187 SetSevEsJumpTable (ExchangeInfo
->BufferStart
);
1192 // Must use the INIT-SIPI-SIPI method for initial configuration in
1193 // order to obtain the APIC ID.
1195 if (CpuMpData
->SevSnpIsEnabled
&& (CpuMpData
->InitFlag
!= ApInitConfig
)) {
1196 SevSnpCreateAP (CpuMpData
, -1);
1198 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 and SEV-SNP, 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
);
1298 if (CpuMpData
->SevSnpIsEnabled
&& (CpuMpData
->InitFlag
!= ApInitConfig
)) {
1299 SevSnpCreateAP (CpuMpData
, (INTN
)ProcessorNumber
);
1302 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1303 (UINT32
)ExchangeInfo
->BufferStart
1309 // Wait specified AP waken up
1311 WaitApWakeup (CpuData
->StartupApSignal
);
1314 if (ResetVectorRequired
) {
1315 FreeResetVector (CpuMpData
);
1319 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1320 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1321 // S3SmmInitDone Ppi.
1323 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1327 Calculate timeout value and return the current performance counter value.
1329 Calculate the number of performance counter ticks required for a timeout.
1330 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1333 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1334 @param[out] CurrentTime Returns the current value of the performance counter.
1336 @return Expected time stamp counter for timeout.
1337 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1343 IN UINTN TimeoutInMicroseconds
,
1344 OUT UINT64
*CurrentTime
1347 UINT64 TimeoutInSeconds
;
1348 UINT64 TimestampCounterFreq
;
1351 // Read the current value of the performance counter
1353 *CurrentTime
= GetPerformanceCounter ();
1356 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1359 if (TimeoutInMicroseconds
== 0) {
1364 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1367 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1370 // Check the potential overflow before calculate the number of ticks for the timeout value.
1372 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1374 // Convert microseconds into seconds if direct multiplication overflows
1376 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1378 // Assertion if the final tick count exceeds MAX_UINT64
1380 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1381 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1384 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1385 // it by 1,000,000, to get the number of ticks for the timeout value.
1389 TimestampCounterFreq
,
1390 TimeoutInMicroseconds
1398 Checks whether timeout expires.
1400 Check whether the number of elapsed performance counter ticks required for
1401 a timeout condition has been reached.
1402 If Timeout is zero, which means infinity, return value is always FALSE.
1404 @param[in, out] PreviousTime On input, the value of the performance counter
1405 when it was last read.
1406 On output, the current value of the performance
1408 @param[in] TotalTime The total amount of elapsed time in performance
1410 @param[in] Timeout The number of performance counter ticks required
1411 to reach a timeout condition.
1413 @retval TRUE A timeout condition has been reached.
1414 @retval FALSE A timeout condition has not been reached.
1419 IN OUT UINT64
*PreviousTime
,
1420 IN UINT64
*TotalTime
,
1434 GetPerformanceCounterProperties (&Start
, &End
);
1435 Cycle
= End
- Start
;
1441 CurrentTime
= GetPerformanceCounter ();
1442 Delta
= (INT64
)(CurrentTime
- *PreviousTime
);
1451 *TotalTime
+= Delta
;
1452 *PreviousTime
= CurrentTime
;
1453 if (*TotalTime
> Timeout
) {
1461 Helper function that waits until the finished AP count reaches the specified
1462 limit, or the specified timeout elapses (whichever comes first).
1464 @param[in] CpuMpData Pointer to CPU MP Data.
1465 @param[in] FinishedApLimit The number of finished APs to wait for.
1466 @param[in] TimeLimit The number of microseconds to wait for.
1469 TimedWaitForApFinish (
1470 IN CPU_MP_DATA
*CpuMpData
,
1471 IN UINT32 FinishedApLimit
,
1476 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1477 // "infinity", so check for (TimeLimit == 0) explicitly.
1479 if (TimeLimit
== 0) {
1483 CpuMpData
->TotalTime
= 0;
1484 CpuMpData
->ExpectedTime
= CalculateTimeout (
1486 &CpuMpData
->CurrentTime
1488 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1490 &CpuMpData
->CurrentTime
,
1491 &CpuMpData
->TotalTime
,
1492 CpuMpData
->ExpectedTime
1498 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1501 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1504 DivU64x64Remainder (
1505 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1506 GetPerformanceCounterProperties (NULL
, NULL
),
1514 Reset an AP to Idle state.
1516 Any task being executed by the AP will be aborted and the AP
1517 will be waiting for a new task in Wait-For-SIPI state.
1519 @param[in] ProcessorNumber The handle number of processor.
1522 ResetProcessorToIdleState (
1523 IN UINTN ProcessorNumber
1526 CPU_MP_DATA
*CpuMpData
;
1528 CpuMpData
= GetCpuMpData ();
1530 CpuMpData
->InitFlag
= ApInitReconfig
;
1531 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1532 while (CpuMpData
->FinishedCount
< 1) {
1536 CpuMpData
->InitFlag
= ApInitDone
;
1538 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1542 Searches for the next waiting AP.
1544 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1546 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1548 @retval EFI_SUCCESS The next waiting AP has been found.
1549 @retval EFI_NOT_FOUND No waiting AP exists.
1553 GetNextWaitingProcessorNumber (
1554 OUT UINTN
*NextProcessorNumber
1557 UINTN ProcessorNumber
;
1558 CPU_MP_DATA
*CpuMpData
;
1560 CpuMpData
= GetCpuMpData ();
1562 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1563 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1564 *NextProcessorNumber
= ProcessorNumber
;
1569 return EFI_NOT_FOUND
;
1572 /** Checks status of specified AP.
1574 This function checks whether the specified AP has finished the task assigned
1575 by StartupThisAP(), and whether timeout expires.
1577 @param[in] ProcessorNumber The handle number of processor.
1579 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1580 @retval EFI_TIMEOUT The timeout expires.
1581 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1585 IN UINTN ProcessorNumber
1588 CPU_MP_DATA
*CpuMpData
;
1589 CPU_AP_DATA
*CpuData
;
1591 CpuMpData
= GetCpuMpData ();
1592 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1595 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1596 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1597 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1600 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1602 if (GetApState (CpuData
) == CpuStateFinished
) {
1603 if (CpuData
->Finished
!= NULL
) {
1604 *(CpuData
->Finished
) = TRUE
;
1607 SetApState (CpuData
, CpuStateIdle
);
1611 // If timeout expires for StartupThisAP(), report timeout.
1613 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1614 if (CpuData
->Finished
!= NULL
) {
1615 *(CpuData
->Finished
) = FALSE
;
1619 // Reset failed AP to idle state
1621 ResetProcessorToIdleState (ProcessorNumber
);
1627 return EFI_NOT_READY
;
1631 Checks status of all APs.
1633 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1634 and whether timeout expires.
1636 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1637 @retval EFI_TIMEOUT The timeout expires.
1638 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1645 UINTN ProcessorNumber
;
1646 UINTN NextProcessorNumber
;
1649 CPU_MP_DATA
*CpuMpData
;
1650 CPU_AP_DATA
*CpuData
;
1652 CpuMpData
= GetCpuMpData ();
1654 NextProcessorNumber
= 0;
1657 // Go through all APs that are responsible for the StartupAllAPs().
1659 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1660 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1664 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1666 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1667 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1668 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1670 if (GetApState (CpuData
) == CpuStateFinished
) {
1671 CpuMpData
->RunningCount
--;
1672 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1673 SetApState (CpuData
, CpuStateIdle
);
1676 // If in Single Thread mode, then search for the next waiting AP for execution.
1678 if (CpuMpData
->SingleThread
) {
1679 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1681 if (!EFI_ERROR (Status
)) {
1685 (UINT32
)NextProcessorNumber
,
1686 CpuMpData
->Procedure
,
1687 CpuMpData
->ProcArguments
,
1696 // If all APs finish, return EFI_SUCCESS.
1698 if (CpuMpData
->RunningCount
== 0) {
1703 // If timeout expires, report timeout.
1706 &CpuMpData
->CurrentTime
,
1707 &CpuMpData
->TotalTime
,
1708 CpuMpData
->ExpectedTime
1713 // If FailedCpuList is not NULL, record all failed APs in it.
1715 if (CpuMpData
->FailedCpuList
!= NULL
) {
1716 *CpuMpData
->FailedCpuList
=
1717 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1718 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1723 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1725 // Check whether this processor is responsible for StartupAllAPs().
1727 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1729 // Reset failed APs to idle state
1731 ResetProcessorToIdleState (ProcessorNumber
);
1732 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1733 if (CpuMpData
->FailedCpuList
!= NULL
) {
1734 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1739 if (CpuMpData
->FailedCpuList
!= NULL
) {
1740 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1746 return EFI_NOT_READY
;
1750 MP Initialize Library initialization.
1752 This service will allocate AP reset vector and wakeup all APs to do APs
1755 This service must be invoked before all other MP Initialize Library
1756 service are invoked.
1758 @retval EFI_SUCCESS MP initialization succeeds.
1759 @retval Others MP initialization fails.
1764 MpInitLibInitialize (
1768 CPU_MP_DATA
*OldCpuMpData
;
1769 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1770 UINT32 MaxLogicalProcessorNumber
;
1772 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1773 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1775 UINT32 MonitorFilterSize
;
1778 CPU_MP_DATA
*CpuMpData
;
1780 UINT8
*MonitorBuffer
;
1782 UINTN ApResetVectorSizeBelow1Mb
;
1783 UINTN ApResetVectorSizeAbove1Mb
;
1784 UINTN BackupBufferAddr
;
1787 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1788 if (OldCpuMpData
== NULL
) {
1789 MaxLogicalProcessorNumber
= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
1791 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1794 ASSERT (MaxLogicalProcessorNumber
!= 0);
1796 AsmGetAddressMap (&AddressMap
);
1797 GetApResetVectorSize (&AddressMap
, &ApResetVectorSizeBelow1Mb
, &ApResetVectorSizeAbove1Mb
);
1798 ApStackSize
= PcdGet32 (PcdCpuApStackSize
);
1799 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1802 // Save BSP's Control registers for APs.
1804 SaveVolatileRegisters (&VolatileRegisters
);
1806 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1807 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1808 BufferSize
+= ApResetVectorSizeBelow1Mb
;
1809 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1810 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1811 BufferSize
+= sizeof (CPU_MP_DATA
);
1812 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1813 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1814 ASSERT (MpBuffer
!= NULL
);
1815 ZeroMem (MpBuffer
, BufferSize
);
1816 Buffer
= (UINTN
)MpBuffer
;
1819 // The layout of the Buffer is as below:
1821 // +--------------------+ <-- Buffer
1823 // +--------------------+ <-- MonitorBuffer
1824 // AP Monitor Filters (N)
1825 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1827 // +--------------------+
1829 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1830 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1831 // +--------------------+ <-- CpuMpData
1833 // +--------------------+ <-- CpuMpData->CpuData
1835 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1836 // CPU_INFO_IN_HOB (N)
1837 // +--------------------+
1839 MonitorBuffer
= (UINT8
*)(Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1840 BackupBufferAddr
= (UINTN
)MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1841 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSizeBelow1Mb
, 8);
1842 CpuMpData
= (CPU_MP_DATA
*)(ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1843 CpuMpData
->Buffer
= Buffer
;
1844 CpuMpData
->CpuApStackSize
= ApStackSize
;
1845 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1846 CpuMpData
->BackupBufferSize
= ApResetVectorSizeBelow1Mb
;
1847 CpuMpData
->WakeupBuffer
= (UINTN
)-1;
1848 CpuMpData
->CpuCount
= 1;
1849 CpuMpData
->BspNumber
= 0;
1850 CpuMpData
->WaitEvent
= NULL
;
1851 CpuMpData
->SwitchBspFlag
= FALSE
;
1852 CpuMpData
->CpuData
= (CPU_AP_DATA
*)(CpuMpData
+ 1);
1853 CpuMpData
->CpuInfoInHob
= (UINT64
)(UINTN
)(CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1854 InitializeSpinLock (&CpuMpData
->MpLock
);
1855 CpuMpData
->SevEsIsEnabled
= ConfidentialComputingGuestHas (CCAttrAmdSevEs
);
1856 CpuMpData
->SevSnpIsEnabled
= ConfidentialComputingGuestHas (CCAttrAmdSevSnp
);
1857 CpuMpData
->SevEsAPBuffer
= (UINTN
)-1;
1858 CpuMpData
->GhcbBase
= PcdGet64 (PcdGhcbBase
);
1859 CpuMpData
->UseSevEsAPMethod
= CpuMpData
->SevEsIsEnabled
&& !CpuMpData
->SevSnpIsEnabled
;
1861 if (CpuMpData
->SevSnpIsEnabled
) {
1862 ASSERT ((PcdGet64 (PcdGhcbHypervisorFeatures
) & GHCB_HV_FEATURES_SNP_AP_CREATE
) == GHCB_HV_FEATURES_SNP_AP_CREATE
);
1866 // Make sure no memory usage outside of the allocated buffer.
1869 (CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1874 // Duplicate BSP's IDT to APs.
1875 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1877 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1878 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1880 // Don't pass BSP's TR to APs to avoid AP init failure.
1882 VolatileRegisters
.Tr
= 0;
1883 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1885 // Set BSP basic information
1887 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1889 // Save assembly code information
1891 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1893 // Finally set AP loop mode
1895 CpuMpData
->ApLoopMode
= ApLoopMode
;
1896 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1898 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1901 // Set up APs wakeup signal buffer
1903 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1904 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1905 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1909 // Copy all 32-bit code and 64-bit code into memory with type of
1910 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
1912 CpuMpData
->WakeupBufferHigh
= AllocateCodeBuffer (ApResetVectorSizeAbove1Mb
);
1914 (VOID
*)CpuMpData
->WakeupBufferHigh
,
1915 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
1916 CpuMpData
->AddressMap
.ModeTransitionOffset
,
1917 ApResetVectorSizeAbove1Mb
1919 DEBUG ((DEBUG_INFO
, "AP Vector: non-16-bit = %p/%x\n", CpuMpData
->WakeupBufferHigh
, ApResetVectorSizeAbove1Mb
));
1922 // Enable the local APIC for Virtual Wire Mode.
1924 ProgramVirtualWireMode ();
1926 if (OldCpuMpData
== NULL
) {
1927 if (MaxLogicalProcessorNumber
> 1) {
1929 // Wakeup all APs and calculate the processor count in system
1931 CollectProcessorCount (CpuMpData
);
1935 // APs have been wakeup before, just get the CPU Information
1938 OldCpuMpData
->NewCpuMpData
= CpuMpData
;
1939 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1940 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1941 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1942 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
1943 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1944 InitializeSpinLock (&CpuMpData
->CpuData
[Index
].ApLock
);
1945 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0) ? TRUE
: FALSE
;
1946 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1950 if (!GetMicrocodePatchInfoFromHob (
1951 &CpuMpData
->MicrocodePatchAddress
,
1952 &CpuMpData
->MicrocodePatchRegionSize
1956 // The microcode patch information cache HOB does not exist, which means
1957 // the microcode patches data has not been loaded into memory yet
1959 ShadowMicrocodeUpdatePatch (CpuMpData
);
1963 // Detect and apply Microcode on BSP
1965 MicrocodeDetect (CpuMpData
, CpuMpData
->BspNumber
);
1967 // Store BSP's MTRR setting
1969 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1972 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1974 if (CpuMpData
->CpuCount
> 1) {
1975 if (OldCpuMpData
!= NULL
) {
1977 // Only needs to use this flag for DXE phase to update the wake up
1978 // buffer. Wakeup buffer allocated in PEI phase is no longer valid
1981 CpuMpData
->InitFlag
= ApInitReconfig
;
1984 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1986 // Wait for all APs finished initialization
1988 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1992 if (OldCpuMpData
!= NULL
) {
1993 CpuMpData
->InitFlag
= ApInitDone
;
1996 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1997 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
2002 // Dump the microcode revision for each core.
2004 DEBUG_CODE_BEGIN ();
2006 UINT32 ExpectedMicrocodeRevision
;
2008 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
2009 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
2010 GetProcessorLocationByApicId (CpuInfoInHob
[Index
].InitialApicId
, NULL
, NULL
, &ThreadId
);
2011 if (ThreadId
== 0) {
2013 // MicrocodeDetect() loads microcode in first thread of each core, so,
2014 // CpuMpData->CpuData[Index].MicrocodeEntryAddr is initialized only for first thread of each core.
2016 ExpectedMicrocodeRevision
= 0;
2017 if (CpuMpData
->CpuData
[Index
].MicrocodeEntryAddr
!= 0) {
2018 ExpectedMicrocodeRevision
= ((CPU_MICROCODE_HEADER
*)(UINTN
)CpuMpData
->CpuData
[Index
].MicrocodeEntryAddr
)->UpdateRevision
;
2023 "CPU[%04d]: Microcode revision = %08x, expected = %08x\n",
2025 CpuMpData
->CpuData
[Index
].MicrocodeRevision
,
2026 ExpectedMicrocodeRevision
2033 // Initialize global data for MP support
2035 InitMpGlobalData (CpuMpData
);
2041 Gets detailed MP-related information on the requested processor at the
2042 instant this call is made. This service may only be called from the BSP.
2044 @param[in] ProcessorNumber The handle number of processor.
2045 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
2046 the requested processor is deposited.
2047 @param[out] HealthData Return processor health data.
2049 @retval EFI_SUCCESS Processor information was returned.
2050 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2051 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
2052 @retval EFI_NOT_FOUND The processor with the handle specified by
2053 ProcessorNumber does not exist in the platform.
2054 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2059 MpInitLibGetProcessorInfo (
2060 IN UINTN ProcessorNumber
,
2061 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
2062 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
2065 CPU_MP_DATA
*CpuMpData
;
2067 CPU_INFO_IN_HOB
*CpuInfoInHob
;
2068 UINTN OriginalProcessorNumber
;
2070 CpuMpData
= GetCpuMpData ();
2071 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
2074 // Lower 24 bits contains the actual processor number.
2076 OriginalProcessorNumber
= ProcessorNumber
;
2077 ProcessorNumber
&= BIT24
- 1;
2080 // Check whether caller processor is BSP
2082 MpInitLibWhoAmI (&CallerNumber
);
2083 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2084 return EFI_DEVICE_ERROR
;
2087 if (ProcessorInfoBuffer
== NULL
) {
2088 return EFI_INVALID_PARAMETER
;
2091 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2092 return EFI_NOT_FOUND
;
2095 ProcessorInfoBuffer
->ProcessorId
= (UINT64
)CpuInfoInHob
[ProcessorNumber
].ApicId
;
2096 ProcessorInfoBuffer
->StatusFlag
= 0;
2097 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2098 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
2101 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
2102 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
2105 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2106 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
2108 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
2112 // Get processor location information
2114 GetProcessorLocationByApicId (
2115 CpuInfoInHob
[ProcessorNumber
].ApicId
,
2116 &ProcessorInfoBuffer
->Location
.Package
,
2117 &ProcessorInfoBuffer
->Location
.Core
,
2118 &ProcessorInfoBuffer
->Location
.Thread
2121 if ((OriginalProcessorNumber
& CPU_V2_EXTENDED_TOPOLOGY
) != 0) {
2122 GetProcessorLocation2ByApicId (
2123 CpuInfoInHob
[ProcessorNumber
].ApicId
,
2124 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Package
,
2125 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Die
,
2126 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Tile
,
2127 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Module
,
2128 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Core
,
2129 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Thread
2133 if (HealthData
!= NULL
) {
2134 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
2141 Worker function to switch the requested AP to be the BSP from that point onward.
2143 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
2144 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
2145 enabled AP. Otherwise, it will be disabled.
2147 @retval EFI_SUCCESS BSP successfully switched.
2148 @retval others Failed to switch BSP.
2153 IN UINTN ProcessorNumber
,
2154 IN BOOLEAN EnableOldBSP
2157 CPU_MP_DATA
*CpuMpData
;
2160 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
2161 BOOLEAN OldInterruptState
;
2162 BOOLEAN OldTimerInterruptState
;
2165 // Save and Disable Local APIC timer interrupt
2167 OldTimerInterruptState
= GetApicTimerInterruptState ();
2168 DisableApicTimerInterrupt ();
2170 // Before send both BSP and AP to a procedure to exchange their roles,
2171 // interrupt must be disabled. This is because during the exchange role
2172 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
2173 // be corrupted, since interrupt return address will be pushed to stack
2176 OldInterruptState
= SaveAndDisableInterrupts ();
2179 // Mask LINT0 & LINT1 for the old BSP
2181 DisableLvtInterrupts ();
2183 CpuMpData
= GetCpuMpData ();
2186 // Check whether caller processor is BSP
2188 MpInitLibWhoAmI (&CallerNumber
);
2189 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2190 return EFI_DEVICE_ERROR
;
2193 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2194 return EFI_NOT_FOUND
;
2198 // Check whether specified AP is disabled
2200 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
2201 if (State
== CpuStateDisabled
) {
2202 return EFI_INVALID_PARAMETER
;
2206 // Check whether ProcessorNumber specifies the current BSP
2208 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2209 return EFI_INVALID_PARAMETER
;
2213 // Check whether specified AP is busy
2215 if (State
== CpuStateBusy
) {
2216 return EFI_NOT_READY
;
2219 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2220 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2221 CpuMpData
->SwitchBspFlag
= TRUE
;
2222 CpuMpData
->NewBspNumber
= ProcessorNumber
;
2225 // Clear the BSP bit of MSR_IA32_APIC_BASE
2227 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2228 ApicBaseMsr
.Bits
.BSP
= 0;
2229 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2232 // Need to wakeUp AP (future BSP).
2234 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
2236 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
2239 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2241 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2242 ApicBaseMsr
.Bits
.BSP
= 1;
2243 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2244 ProgramVirtualWireMode ();
2247 // Wait for old BSP finished AP task
2249 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
2253 CpuMpData
->SwitchBspFlag
= FALSE
;
2255 // Set old BSP enable state
2257 if (!EnableOldBSP
) {
2258 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
2260 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2264 // Save new BSP number
2266 CpuMpData
->BspNumber
= (UINT32
)ProcessorNumber
;
2269 // Restore interrupt state.
2271 SetInterruptState (OldInterruptState
);
2273 if (OldTimerInterruptState
) {
2274 EnableApicTimerInterrupt ();
2281 Worker function to let the caller enable or disable an AP from this point onward.
2282 This service may only be called from the BSP.
2284 @param[in] ProcessorNumber The handle number of AP.
2285 @param[in] EnableAP Specifies the new state for the processor for
2286 enabled, FALSE for disabled.
2287 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2288 the new health status of the AP.
2290 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2291 @retval others Failed to Enable/Disable AP.
2295 EnableDisableApWorker (
2296 IN UINTN ProcessorNumber
,
2297 IN BOOLEAN EnableAP
,
2298 IN UINT32
*HealthFlag OPTIONAL
2301 CPU_MP_DATA
*CpuMpData
;
2304 CpuMpData
= GetCpuMpData ();
2307 // Check whether caller processor is BSP
2309 MpInitLibWhoAmI (&CallerNumber
);
2310 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2311 return EFI_DEVICE_ERROR
;
2314 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2315 return EFI_INVALID_PARAMETER
;
2318 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2319 return EFI_NOT_FOUND
;
2323 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2325 ResetProcessorToIdleState (ProcessorNumber
);
2328 if (HealthFlag
!= NULL
) {
2329 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2330 (BOOLEAN
)((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2337 This return the handle number for the calling processor. This service may be
2338 called from the BSP and APs.
2340 @param[out] ProcessorNumber Pointer to the handle number of AP.
2341 The range is from 0 to the total number of
2342 logical processors minus 1. The total number of
2343 logical processors can be retrieved by
2344 MpInitLibGetNumberOfProcessors().
2346 @retval EFI_SUCCESS The current processor handle number was returned
2348 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2349 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2355 OUT UINTN
*ProcessorNumber
2358 CPU_MP_DATA
*CpuMpData
;
2360 if (ProcessorNumber
== NULL
) {
2361 return EFI_INVALID_PARAMETER
;
2364 CpuMpData
= GetCpuMpData ();
2366 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2370 Retrieves the number of logical processor in the platform and the number of
2371 those logical processors that are enabled on this boot. This service may only
2372 be called from the BSP.
2374 @param[out] NumberOfProcessors Pointer to the total number of logical
2375 processors in the system, including the BSP
2377 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2378 processors that exist in system, including
2381 @retval EFI_SUCCESS The number of logical processors and enabled
2382 logical processors was retrieved.
2383 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2384 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2386 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2391 MpInitLibGetNumberOfProcessors (
2392 OUT UINTN
*NumberOfProcessors OPTIONAL
,
2393 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2396 CPU_MP_DATA
*CpuMpData
;
2398 UINTN ProcessorNumber
;
2399 UINTN EnabledProcessorNumber
;
2402 CpuMpData
= GetCpuMpData ();
2404 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2405 return EFI_INVALID_PARAMETER
;
2409 // Check whether caller processor is BSP
2411 MpInitLibWhoAmI (&CallerNumber
);
2412 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2413 return EFI_DEVICE_ERROR
;
2416 ProcessorNumber
= CpuMpData
->CpuCount
;
2417 EnabledProcessorNumber
= 0;
2418 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2419 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2420 EnabledProcessorNumber
++;
2424 if (NumberOfProcessors
!= NULL
) {
2425 *NumberOfProcessors
= ProcessorNumber
;
2428 if (NumberOfEnabledProcessors
!= NULL
) {
2429 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2436 Worker function to execute a caller provided function on all enabled APs.
2438 @param[in] Procedure A pointer to the function to be run on
2439 enabled APs of the system.
2440 @param[in] SingleThread If TRUE, then all the enabled APs execute
2441 the function specified by Procedure one by
2442 one, in ascending order of processor handle
2443 number. If FALSE, then all the enabled APs
2444 execute the function specified by Procedure
2446 @param[in] ExcludeBsp Whether let BSP also trig this task.
2447 @param[in] WaitEvent The event created by the caller with CreateEvent()
2449 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2450 APs to return from Procedure, either for
2451 blocking or non-blocking mode.
2452 @param[in] ProcedureArgument The parameter passed into Procedure for
2454 @param[out] FailedCpuList If all APs finish successfully, then its
2455 content is set to NULL. If not all APs
2456 finish before timeout expires, then its
2457 content is set to address of the buffer
2458 holding handle numbers of the failed APs.
2460 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2461 the timeout expired.
2462 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2464 @retval others Failed to Startup all APs.
2468 StartupAllCPUsWorker (
2469 IN EFI_AP_PROCEDURE Procedure
,
2470 IN BOOLEAN SingleThread
,
2471 IN BOOLEAN ExcludeBsp
,
2472 IN EFI_EVENT WaitEvent OPTIONAL
,
2473 IN UINTN TimeoutInMicroseconds
,
2474 IN VOID
*ProcedureArgument OPTIONAL
,
2475 OUT UINTN
**FailedCpuList OPTIONAL
2479 CPU_MP_DATA
*CpuMpData
;
2480 UINTN ProcessorCount
;
2481 UINTN ProcessorNumber
;
2483 CPU_AP_DATA
*CpuData
;
2484 BOOLEAN HasEnabledAp
;
2487 CpuMpData
= GetCpuMpData ();
2489 if (FailedCpuList
!= NULL
) {
2490 *FailedCpuList
= NULL
;
2493 if ((CpuMpData
->CpuCount
== 1) && ExcludeBsp
) {
2494 return EFI_NOT_STARTED
;
2497 if (Procedure
== NULL
) {
2498 return EFI_INVALID_PARAMETER
;
2502 // Check whether caller processor is BSP
2504 MpInitLibWhoAmI (&CallerNumber
);
2505 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2506 return EFI_DEVICE_ERROR
;
2512 CheckAndUpdateApsStatus ();
2514 ProcessorCount
= CpuMpData
->CpuCount
;
2515 HasEnabledAp
= FALSE
;
2517 // Check whether all enabled APs are idle.
2518 // If any enabled AP is not idle, return EFI_NOT_READY.
2520 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2521 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2522 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2523 ApState
= GetApState (CpuData
);
2524 if (ApState
!= CpuStateDisabled
) {
2525 HasEnabledAp
= TRUE
;
2526 if (ApState
!= CpuStateIdle
) {
2528 // If any enabled APs are busy, return EFI_NOT_READY.
2530 return EFI_NOT_READY
;
2536 if (!HasEnabledAp
&& ExcludeBsp
) {
2538 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2540 return EFI_NOT_STARTED
;
2543 CpuMpData
->RunningCount
= 0;
2544 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2545 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2546 CpuData
->Waiting
= FALSE
;
2547 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2548 if (CpuData
->State
== CpuStateIdle
) {
2550 // Mark this processor as responsible for current calling.
2552 CpuData
->Waiting
= TRUE
;
2553 CpuMpData
->RunningCount
++;
2558 CpuMpData
->Procedure
= Procedure
;
2559 CpuMpData
->ProcArguments
= ProcedureArgument
;
2560 CpuMpData
->SingleThread
= SingleThread
;
2561 CpuMpData
->FinishedCount
= 0;
2562 CpuMpData
->FailedCpuList
= FailedCpuList
;
2563 CpuMpData
->ExpectedTime
= CalculateTimeout (
2564 TimeoutInMicroseconds
,
2565 &CpuMpData
->CurrentTime
2567 CpuMpData
->TotalTime
= 0;
2568 CpuMpData
->WaitEvent
= WaitEvent
;
2570 if (!SingleThread
) {
2571 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2573 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2574 if (ProcessorNumber
== CallerNumber
) {
2578 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2579 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2589 Procedure (ProcedureArgument
);
2592 Status
= EFI_SUCCESS
;
2593 if (WaitEvent
== NULL
) {
2595 Status
= CheckAllAPs ();
2596 } while (Status
== EFI_NOT_READY
);
2603 Worker function to let the caller get one enabled AP to execute a caller-provided
2606 @param[in] Procedure A pointer to the function to be run on
2607 enabled APs of the system.
2608 @param[in] ProcessorNumber The handle number of the AP.
2609 @param[in] WaitEvent The event created by the caller with CreateEvent()
2611 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2612 APs to return from Procedure, either for
2613 blocking or non-blocking mode.
2614 @param[in] ProcedureArgument The parameter passed into Procedure for
2616 @param[out] Finished If AP returns from Procedure before the
2617 timeout expires, its content is set to TRUE.
2618 Otherwise, the value is set to FALSE.
2620 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2621 the timeout expires.
2622 @retval others Failed to Startup AP.
2626 StartupThisAPWorker (
2627 IN EFI_AP_PROCEDURE Procedure
,
2628 IN UINTN ProcessorNumber
,
2629 IN EFI_EVENT WaitEvent OPTIONAL
,
2630 IN UINTN TimeoutInMicroseconds
,
2631 IN VOID
*ProcedureArgument OPTIONAL
,
2632 OUT BOOLEAN
*Finished OPTIONAL
2636 CPU_MP_DATA
*CpuMpData
;
2637 CPU_AP_DATA
*CpuData
;
2640 CpuMpData
= GetCpuMpData ();
2642 if (Finished
!= NULL
) {
2647 // Check whether caller processor is BSP
2649 MpInitLibWhoAmI (&CallerNumber
);
2650 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2651 return EFI_DEVICE_ERROR
;
2655 // Check whether processor with the handle specified by ProcessorNumber exists
2657 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2658 return EFI_NOT_FOUND
;
2662 // Check whether specified processor is BSP
2664 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2665 return EFI_INVALID_PARAMETER
;
2669 // Check parameter Procedure
2671 if (Procedure
== NULL
) {
2672 return EFI_INVALID_PARAMETER
;
2678 CheckAndUpdateApsStatus ();
2681 // Check whether specified AP is disabled
2683 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2684 return EFI_INVALID_PARAMETER
;
2688 // If WaitEvent is not NULL, execute in non-blocking mode.
2689 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2690 // CheckAPsStatus() will check completion and timeout periodically.
2692 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2693 CpuData
->WaitEvent
= WaitEvent
;
2694 CpuData
->Finished
= Finished
;
2695 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2696 CpuData
->TotalTime
= 0;
2698 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2701 // If WaitEvent is NULL, execute in blocking mode.
2702 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2704 Status
= EFI_SUCCESS
;
2705 if (WaitEvent
== NULL
) {
2707 Status
= CheckThisAP (ProcessorNumber
);
2708 } while (Status
== EFI_NOT_READY
);
2715 Get pointer to CPU MP Data structure from GUIDed HOB.
2717 @return The pointer to CPU MP Data structure.
2720 GetCpuMpDataFromGuidedHob (
2724 EFI_HOB_GUID_TYPE
*GuidHob
;
2726 CPU_MP_DATA
*CpuMpData
;
2729 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2730 if (GuidHob
!= NULL
) {
2731 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2732 CpuMpData
= (CPU_MP_DATA
*)(*(UINTN
*)DataInHob
);
2739 This service executes a caller provided function on all enabled CPUs.
2741 @param[in] Procedure A pointer to the function to be run on
2742 enabled APs of the system. See type
2744 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2745 APs to return from Procedure, either for
2746 blocking or non-blocking mode. Zero means
2747 infinity. TimeoutInMicroseconds is ignored
2749 @param[in] ProcedureArgument The parameter passed into Procedure for
2752 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2753 the timeout expired.
2754 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2755 to all enabled CPUs.
2756 @retval EFI_DEVICE_ERROR Caller processor is AP.
2757 @retval EFI_NOT_READY Any enabled APs are busy.
2758 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2759 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2760 all enabled APs have finished.
2761 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2766 MpInitLibStartupAllCPUs (
2767 IN EFI_AP_PROCEDURE Procedure
,
2768 IN UINTN TimeoutInMicroseconds
,
2769 IN VOID
*ProcedureArgument OPTIONAL
2772 return StartupAllCPUsWorker (
2777 TimeoutInMicroseconds
,
2784 The function check if the specified Attr is set.
2786 @param[in] CurrentAttr The current attribute.
2787 @param[in] Attr The attribute to check.
2789 @retval TRUE The specified Attr is set.
2790 @retval FALSE The specified Attr is not set.
2795 AmdMemEncryptionAttrCheck (
2796 IN UINT64 CurrentAttr
,
2797 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
2803 // SEV is automatically enabled if SEV-ES or SEV-SNP is active.
2805 return CurrentAttr
>= CCAttrAmdSev
;
2806 case CCAttrAmdSevEs
:
2808 // SEV-ES is automatically enabled if SEV-SNP is active.
2810 return CurrentAttr
>= CCAttrAmdSevEs
;
2811 case CCAttrAmdSevSnp
:
2812 return CurrentAttr
== CCAttrAmdSevSnp
;
2819 Check if the specified confidential computing attribute is active.
2821 @param[in] Attr The attribute to check.
2823 @retval TRUE The specified Attr is active.
2824 @retval FALSE The specified Attr is not active.
2829 ConfidentialComputingGuestHas (
2830 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
2836 // Get the current CC attribute.
2838 CurrentAttr
= PcdGet64 (PcdConfidentialComputingGuestAttr
);
2841 // If attr is for the AMD group then call AMD specific checks.
2843 if (((RShiftU64 (CurrentAttr
, 8)) & 0xff) == 1) {
2844 return AmdMemEncryptionAttrCheck (CurrentAttr
, Attr
);
2847 return (CurrentAttr
== Attr
);