2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2022, 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/CcExitLib.h>
13 #include <Register/Amd/Fam17Msr.h>
14 #include <Register/Amd/Ghcb.h>
16 EFI_GUID mCpuInitMpLibHobGuid
= CPU_INIT_MP_LIB_HOB_GUID
;
19 Save the volatile registers required to be restored following INIT IPI.
21 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
24 SaveVolatileRegisters (
25 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
29 Restore the volatile registers following INIT IPI.
31 @param[in] VolatileRegisters Pointer to volatile resisters
32 @param[in] IsRestoreDr TRUE: Restore DRx if supported
33 FALSE: Do not restore DRx
36 RestoreVolatileRegisters (
37 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
38 IN BOOLEAN IsRestoreDr
42 The function will check if BSP Execute Disable is enabled.
44 DxeIpl may have enabled Execute Disable for BSP, APs need to
45 get the status and sync up the settings.
46 If BSP's CR0.Paging is not set, BSP execute Disble feature is
49 @retval TRUE BSP Execute Disable is enabled.
50 @retval FALSE BSP Execute Disable is not enabled.
53 IsBspExecuteDisableEnabled (
58 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
59 MSR_IA32_EFER_REGISTER EferMsr
;
64 Cr0
.UintN
= AsmReadCr0 ();
65 if (Cr0
.Bits
.PG
!= 0) {
67 // If CR0 Paging bit is set
69 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
70 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
71 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
74 // Bit 20: Execute Disable Bit available.
76 if (Edx
.Bits
.NX
!= 0) {
77 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
80 // Bit 11: Execute Disable Bit enable.
82 if (EferMsr
.Bits
.NXE
!= 0) {
93 Worker function for SwitchBSP().
95 Worker function for SwitchBSP(), assigned to the AP which is intended
98 @param[in] Buffer Pointer to CPU MP Data
106 CPU_MP_DATA
*DataInHob
;
108 DataInHob
= (CPU_MP_DATA
*)Buffer
;
110 // Save and restore volatile registers when switch BSP
112 SaveVolatileRegisters (&DataInHob
->APInfo
.VolatileRegisters
);
113 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
114 RestoreVolatileRegisters (&DataInHob
->APInfo
.VolatileRegisters
, FALSE
);
118 Get the Application Processors state.
120 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
122 @return The AP status
126 IN CPU_AP_DATA
*CpuData
129 return CpuData
->State
;
133 Set the Application Processors state.
135 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
136 @param[in] State The AP status
140 IN CPU_AP_DATA
*CpuData
,
144 AcquireSpinLock (&CpuData
->ApLock
);
145 CpuData
->State
= State
;
146 ReleaseSpinLock (&CpuData
->ApLock
);
150 Save BSP's local APIC timer setting.
152 @param[in] CpuMpData Pointer to CPU MP Data
155 SaveLocalApicTimerSetting (
156 IN CPU_MP_DATA
*CpuMpData
160 // Record the current local APIC timer setting of BSP
163 &CpuMpData
->DivideValue
,
164 &CpuMpData
->PeriodicMode
,
167 CpuMpData
->CurrentTimerCount
= GetApicTimerCurrentCount ();
168 CpuMpData
->TimerInterruptState
= GetApicTimerInterruptState ();
172 Sync local APIC timer setting from BSP to AP.
174 @param[in] CpuMpData Pointer to CPU MP Data
177 SyncLocalApicTimerSetting (
178 IN CPU_MP_DATA
*CpuMpData
182 // Sync local APIC timer setting from BSP to AP
184 InitializeApicTimer (
185 CpuMpData
->DivideValue
,
186 CpuMpData
->CurrentTimerCount
,
187 CpuMpData
->PeriodicMode
,
191 // Disable AP's local APIC timer interrupt
193 DisableApicTimerInterrupt ();
197 Save the volatile registers required to be restored following INIT IPI.
199 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
202 SaveVolatileRegisters (
203 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
206 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
208 VolatileRegisters
->Cr0
= AsmReadCr0 ();
209 VolatileRegisters
->Cr3
= AsmReadCr3 ();
210 VolatileRegisters
->Cr4
= AsmReadCr4 ();
212 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
213 if (VersionInfoEdx
.Bits
.DE
!= 0) {
215 // If processor supports Debugging Extensions feature
216 // by CPUID.[EAX=01H]:EDX.BIT2
218 VolatileRegisters
->Dr0
= AsmReadDr0 ();
219 VolatileRegisters
->Dr1
= AsmReadDr1 ();
220 VolatileRegisters
->Dr2
= AsmReadDr2 ();
221 VolatileRegisters
->Dr3
= AsmReadDr3 ();
222 VolatileRegisters
->Dr6
= AsmReadDr6 ();
223 VolatileRegisters
->Dr7
= AsmReadDr7 ();
226 AsmReadGdtr (&VolatileRegisters
->Gdtr
);
227 AsmReadIdtr (&VolatileRegisters
->Idtr
);
228 VolatileRegisters
->Tr
= AsmReadTr ();
232 Restore the volatile registers following INIT IPI.
234 @param[in] VolatileRegisters Pointer to volatile resisters
235 @param[in] IsRestoreDr TRUE: Restore DRx if supported
236 FALSE: Do not restore DRx
239 RestoreVolatileRegisters (
240 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
241 IN BOOLEAN IsRestoreDr
244 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
245 IA32_TSS_DESCRIPTOR
*Tss
;
247 AsmWriteCr3 (VolatileRegisters
->Cr3
);
248 AsmWriteCr4 (VolatileRegisters
->Cr4
);
249 AsmWriteCr0 (VolatileRegisters
->Cr0
);
252 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
253 if (VersionInfoEdx
.Bits
.DE
!= 0) {
255 // If processor supports Debugging Extensions feature
256 // by CPUID.[EAX=01H]:EDX.BIT2
258 AsmWriteDr0 (VolatileRegisters
->Dr0
);
259 AsmWriteDr1 (VolatileRegisters
->Dr1
);
260 AsmWriteDr2 (VolatileRegisters
->Dr2
);
261 AsmWriteDr3 (VolatileRegisters
->Dr3
);
262 AsmWriteDr6 (VolatileRegisters
->Dr6
);
263 AsmWriteDr7 (VolatileRegisters
->Dr7
);
267 AsmWriteGdtr (&VolatileRegisters
->Gdtr
);
268 AsmWriteIdtr (&VolatileRegisters
->Idtr
);
269 if ((VolatileRegisters
->Tr
!= 0) &&
270 (VolatileRegisters
->Tr
< VolatileRegisters
->Gdtr
.Limit
))
272 Tss
= (IA32_TSS_DESCRIPTOR
*)(VolatileRegisters
->Gdtr
.Base
+
273 VolatileRegisters
->Tr
);
274 if (Tss
->Bits
.P
== 1) {
275 Tss
->Bits
.Type
&= 0xD; // 1101 - Clear busy bit just in case
276 AsmWriteTr (VolatileRegisters
->Tr
);
282 Detect whether Mwait-monitor feature is supported.
284 @retval TRUE Mwait-monitor feature is supported.
285 @retval FALSE Mwait-monitor feature is not supported.
292 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
294 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
295 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
301 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
303 @return The AP loop mode.
307 OUT UINT32
*MonitorFilterSize
311 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
313 ASSERT (MonitorFilterSize
!= NULL
);
315 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
316 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
317 if (ApLoopMode
== ApInMwaitLoop
) {
318 if (!IsMwaitSupport ()) {
320 // If processor does not support MONITOR/MWAIT feature,
321 // force AP in Hlt-loop mode
323 ApLoopMode
= ApInHltLoop
;
326 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs
) &&
327 !ConfidentialComputingGuestHas (CCAttrAmdSevSnp
))
330 // For SEV-ES (SEV-SNP is also considered SEV-ES), force AP in Hlt-loop
331 // mode in order to use the GHCB protocol for starting APs
333 ApLoopMode
= ApInHltLoop
;
337 if (ApLoopMode
!= ApInMwaitLoop
) {
338 *MonitorFilterSize
= sizeof (UINT32
);
341 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
342 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
344 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
345 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
352 Sort the APIC ID of all processors.
354 This function sorts the APIC ID of all processors so that processor number is
355 assigned in the ascending order of APIC ID which eases MP debugging.
357 @param[in] CpuMpData Pointer to PEI CPU MP Data
361 IN CPU_MP_DATA
*CpuMpData
368 CPU_INFO_IN_HOB CpuInfo
;
370 CPU_INFO_IN_HOB
*CpuInfoInHob
;
371 volatile UINT32
*StartupApSignal
;
373 ApCount
= CpuMpData
->CpuCount
- 1;
374 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
376 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
379 // Sort key is the hardware default APIC ID
381 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
382 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
383 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
385 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
389 if (Index3
!= Index1
) {
390 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
392 &CpuInfoInHob
[Index3
],
393 &CpuInfoInHob
[Index1
],
394 sizeof (CPU_INFO_IN_HOB
)
396 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
399 // Also exchange the StartupApSignal.
401 StartupApSignal
= CpuMpData
->CpuData
[Index3
].StartupApSignal
;
402 CpuMpData
->CpuData
[Index3
].StartupApSignal
=
403 CpuMpData
->CpuData
[Index1
].StartupApSignal
;
404 CpuMpData
->CpuData
[Index1
].StartupApSignal
= StartupApSignal
;
409 // Get the processor number for the BSP
411 ApicId
= GetInitialApicId ();
412 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
413 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
414 CpuMpData
->BspNumber
= (UINT32
)Index1
;
422 Enable x2APIC mode on APs.
424 @param[in, out] Buffer Pointer to private data buffer.
432 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
438 @param[in, out] Buffer Pointer to private data buffer.
446 CPU_MP_DATA
*CpuMpData
;
447 UINTN ProcessorNumber
;
450 CpuMpData
= (CPU_MP_DATA
*)Buffer
;
451 Status
= GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
452 ASSERT_EFI_ERROR (Status
);
454 // Load microcode on AP
456 MicrocodeDetect (CpuMpData
, ProcessorNumber
);
458 // Sync BSP's MTRR table to AP
460 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
464 Find the current Processor number by APIC ID.
466 @param[in] CpuMpData Pointer to PEI CPU MP Data
467 @param[out] ProcessorNumber Return the pocessor number found
469 @retval EFI_SUCCESS ProcessorNumber is found and returned.
470 @retval EFI_NOT_FOUND ProcessorNumber is not found.
474 IN CPU_MP_DATA
*CpuMpData
,
475 OUT UINTN
*ProcessorNumber
478 UINTN TotalProcessorNumber
;
480 CPU_INFO_IN_HOB
*CpuInfoInHob
;
481 UINT32 CurrentApicId
;
483 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
485 TotalProcessorNumber
= CpuMpData
->CpuCount
;
486 CurrentApicId
= GetApicId ();
487 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
488 if (CpuInfoInHob
[Index
].ApicId
== CurrentApicId
) {
489 *ProcessorNumber
= Index
;
494 return EFI_NOT_FOUND
;
498 This function will get CPU count in the system.
500 @param[in] CpuMpData Pointer to PEI CPU MP Data
502 @return CPU count detected
505 CollectProcessorCount (
506 IN CPU_MP_DATA
*CpuMpData
510 CPU_INFO_IN_HOB
*CpuInfoInHob
;
514 // Send 1st broadcast IPI to APs to wakeup APs
516 CpuMpData
->InitFlag
= ApInitConfig
;
517 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
, TRUE
);
518 CpuMpData
->InitFlag
= ApInitDone
;
520 // When InitFlag == ApInitConfig, WakeUpAP () guarantees all APs are checked in.
521 // FinishedCount is the number of check-in APs.
523 CpuMpData
->CpuCount
= CpuMpData
->FinishedCount
+ 1;
524 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
527 // Enable x2APIC mode if
528 // 1. Number of CPU is greater than 255; or
529 // 2. There are any logical processors reporting an Initial APIC ID of 255 or greater.
532 if (CpuMpData
->CpuCount
> 255) {
534 // If there are more than 255 processor found, force to enable X2APIC
538 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
539 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
540 if (CpuInfoInHob
[Index
].InitialApicId
>= 0xFF) {
548 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
550 // Wakeup all APs to enable x2APIC mode
552 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
, TRUE
);
554 // Wait for all known APs finished
556 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
561 // Enable x2APIC on BSP
563 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
565 // Set BSP/Aps state to IDLE
567 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
568 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
572 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
574 // Sort BSP/Aps by CPU APIC ID in ascending order
576 SortApicId (CpuMpData
);
578 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
580 return CpuMpData
->CpuCount
;
584 Initialize CPU AP Data when AP is wakeup at the first time.
586 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
587 @param[in] ProcessorNumber The handle number of processor
588 @param[in] BistData Processor BIST data
589 @param[in] ApTopOfStack Top of AP stack
594 IN OUT CPU_MP_DATA
*CpuMpData
,
595 IN UINTN ProcessorNumber
,
597 IN UINT64 ApTopOfStack
600 CPU_INFO_IN_HOB
*CpuInfoInHob
;
601 MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr
;
602 AP_STACK_DATA
*ApStackData
;
604 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
605 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
606 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
607 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
608 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
611 // AP_STACK_DATA is stored at the top of AP Stack
613 ApStackData
= (AP_STACK_DATA
*)((UINTN
)ApTopOfStack
- sizeof (AP_STACK_DATA
));
614 ApStackData
->MpData
= CpuMpData
;
616 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
617 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
620 // NOTE: PlatformId is not relevant on AMD platforms.
622 if (!StandardSignatureIsAuthenticAMD ()) {
623 PlatformIdMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_PLATFORM_ID
);
624 CpuMpData
->CpuData
[ProcessorNumber
].PlatformId
= (UINT8
)PlatformIdMsr
.Bits
.PlatformId
;
629 &CpuMpData
->CpuData
[ProcessorNumber
].ProcessorSignature
,
635 InitializeSpinLock (&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
636 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
640 This function will be called from AP reset code if BSP uses WakeUpAP.
642 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
643 @param[in] ApIndex Number of current executing AP
648 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
652 CPU_MP_DATA
*CpuMpData
;
653 UINTN ProcessorNumber
;
654 EFI_AP_PROCEDURE Procedure
;
657 volatile UINT32
*ApStartupSignalBuffer
;
658 CPU_INFO_IN_HOB
*CpuInfoInHob
;
660 UINTN CurrentApicMode
;
661 AP_STACK_DATA
*ApStackData
;
664 // AP finished assembly code and begin to execute C code
666 CpuMpData
= ExchangeInfo
->CpuMpData
;
669 // AP's local APIC settings will be lost after received INIT IPI
670 // We need to re-initialize them at here
672 ProgramVirtualWireMode ();
674 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
676 DisableLvtInterrupts ();
677 SyncLocalApicTimerSetting (CpuMpData
);
679 CurrentApicMode
= GetApicMode ();
681 if (CpuMpData
->InitFlag
== ApInitConfig
) {
682 ProcessorNumber
= ApIndex
;
684 // This is first time AP wakeup, get BIST information from AP stack
686 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
687 ApStackData
= (AP_STACK_DATA
*)((UINTN
)ApTopOfStack
- sizeof (AP_STACK_DATA
));
688 BistData
= (UINT32
)ApStackData
->Bist
;
691 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
692 // to initialize AP in InitConfig path.
693 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
695 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
696 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
697 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
700 // Execute AP function if AP is ready
702 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
704 // Clear AP start-up signal when AP waken up
706 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
707 InterlockedCompareExchange32 (
708 (UINT32
*)ApStartupSignalBuffer
,
713 if (CpuMpData
->InitFlag
== ApInitReconfig
) {
715 // ApInitReconfig happens when:
716 // 1. AP is re-enabled after it's disabled, in either PEI or DXE phase.
717 // 2. AP is initialized in DXE phase.
718 // In either case, use the volatile registers value derived from BSP.
719 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a
720 // different IDT shared by all APs.
722 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
724 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
726 // Restore AP's volatile registers saved before AP is halted
728 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
731 // The CPU driver might not flush TLB for APs on spot after updating
732 // page attributes. AP in mwait loop mode needs to take care of it when
739 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
740 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
741 Parameter
= (VOID
*)CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
742 if (Procedure
!= NULL
) {
743 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
745 // Enable source debugging on AP function
749 // Invoke AP function here
751 Procedure (Parameter
);
752 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
753 if (CpuMpData
->SwitchBspFlag
) {
755 // Re-get the processor number due to BSP/AP maybe exchange in AP function
757 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
758 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
759 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
760 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
761 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
763 if ((CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId ()) ||
764 (CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()))
766 if (CurrentApicMode
!= GetApicMode ()) {
768 // If APIC mode change happened during AP function execution,
769 // we do not support APIC ID value changed.
775 // Re-get the CPU APICID and Initial APICID if they are changed
777 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
778 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
784 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
788 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
790 // Save AP volatile registers
792 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
796 // AP finished executing C code
798 InterlockedIncrement ((UINT32
*)&CpuMpData
->FinishedCount
);
800 if (CpuMpData
->InitFlag
== ApInitConfig
) {
802 // Delay decrementing the APs executing count when SEV-ES is enabled
803 // to allow the APs to issue an AP_RESET_HOLD before the BSP possibly
804 // performs another INIT-SIPI-SIPI sequence.
806 if (!CpuMpData
->UseSevEsAPMethod
) {
807 InterlockedDecrement ((UINT32
*)&CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
812 // Place AP is specified loop mode
814 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
816 // Place AP in HLT-loop
819 DisableInterrupts ();
820 if (CpuMpData
->UseSevEsAPMethod
) {
821 SevEsPlaceApHlt (CpuMpData
);
831 DisableInterrupts ();
832 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
834 // Place AP in MWAIT-loop
836 AsmMonitor ((UINTN
)ApStartupSignalBuffer
, 0, 0);
837 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
839 // Check AP start-up signal again.
840 // If AP start-up signal is not set, place AP into
841 // the specified C-state
843 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
845 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
847 // Place AP in Run-loop
855 // If AP start-up signal is written, AP is waken up
856 // otherwise place AP in loop again
858 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
866 Wait for AP wakeup and write AP start-up signal till AP is waken up.
868 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
872 IN
volatile UINT32
*ApStartupSignalBuffer
876 // If AP is waken up, StartupApSignal should be cleared.
877 // Otherwise, write StartupApSignal again till AP waken up.
879 while (InterlockedCompareExchange32 (
880 (UINT32
*)ApStartupSignalBuffer
,
890 Calculate the size of the reset vector.
892 @param[in] AddressMap The pointer to Address Map structure.
893 @param[out] SizeBelow1Mb Return the size of below 1MB memory for AP reset area.
894 @param[out] SizeAbove1Mb Return the size of abvoe 1MB memory for AP reset area.
898 GetApResetVectorSize (
899 IN MP_ASSEMBLY_ADDRESS_MAP
*AddressMap
,
900 OUT UINTN
*SizeBelow1Mb OPTIONAL
,
901 OUT UINTN
*SizeAbove1Mb OPTIONAL
904 if (SizeBelow1Mb
!= NULL
) {
905 *SizeBelow1Mb
= AddressMap
->ModeTransitionOffset
+ sizeof (MP_CPU_EXCHANGE_INFO
);
908 if (SizeAbove1Mb
!= NULL
) {
909 *SizeAbove1Mb
= AddressMap
->RendezvousFunnelSize
- AddressMap
->ModeTransitionOffset
;
914 This function will fill the exchange info structure.
916 @param[in] CpuMpData Pointer to CPU MP Data
920 FillExchangeInfoData (
921 IN CPU_MP_DATA
*CpuMpData
924 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
926 IA32_SEGMENT_DESCRIPTOR
*Selector
;
929 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
930 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
931 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
932 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
933 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
935 ExchangeInfo
->CodeSegment
= AsmReadCs ();
936 ExchangeInfo
->DataSegment
= AsmReadDs ();
938 ExchangeInfo
->Cr3
= AsmReadCr3 ();
940 ExchangeInfo
->CFunction
= (UINTN
)ApWakeupFunction
;
941 ExchangeInfo
->ApIndex
= 0;
942 ExchangeInfo
->NumApsExecuting
= 0;
943 ExchangeInfo
->InitFlag
= (UINTN
)CpuMpData
->InitFlag
;
944 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
945 ExchangeInfo
->CpuMpData
= CpuMpData
;
947 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
949 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
952 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
953 // to determin whether 5-Level Paging is enabled.
954 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
955 // current system setting.
956 // Using latter way is simpler because it also eliminates the needs to
957 // check whether platform wants to enable it.
959 Cr4
.UintN
= AsmReadCr4 ();
960 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
)(Cr4
.Bits
.LA57
== 1);
961 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
963 ExchangeInfo
->SevEsIsEnabled
= CpuMpData
->SevEsIsEnabled
;
964 ExchangeInfo
->SevSnpIsEnabled
= CpuMpData
->SevSnpIsEnabled
;
965 ExchangeInfo
->GhcbBase
= (UINTN
)CpuMpData
->GhcbBase
;
968 // Populate SEV-ES specific exchange data.
970 if (ExchangeInfo
->SevSnpIsEnabled
) {
971 FillExchangeInfoDataSevEs (ExchangeInfo
);
975 // Get the BSP's data of GDT and IDT
977 AsmReadGdtr ((IA32_DESCRIPTOR
*)&ExchangeInfo
->GdtrProfile
);
978 AsmReadIdtr ((IA32_DESCRIPTOR
*)&ExchangeInfo
->IdtrProfile
);
981 // Find a 32-bit code segment
983 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
984 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
986 if ((Selector
->Bits
.L
== 0) && (Selector
->Bits
.Type
>= 8)) {
987 ExchangeInfo
->ModeTransitionSegment
=
988 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
993 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
996 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
998 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
999 (UINT32
)ExchangeInfo
->ModeOffset
-
1000 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
1001 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
1005 Helper function that waits until the finished AP count reaches the specified
1006 limit, or the specified timeout elapses (whichever comes first).
1008 @param[in] CpuMpData Pointer to CPU MP Data.
1009 @param[in] FinishedApLimit The number of finished APs to wait for.
1010 @param[in] TimeLimit The number of microseconds to wait for.
1013 TimedWaitForApFinish (
1014 IN CPU_MP_DATA
*CpuMpData
,
1015 IN UINT32 FinishedApLimit
,
1020 Get available system memory below 1MB by specified size.
1022 @param[in] CpuMpData The pointer to CPU MP Data structure.
1025 BackupAndPrepareWakeupBuffer (
1026 IN CPU_MP_DATA
*CpuMpData
1030 (VOID
*)CpuMpData
->BackupBuffer
,
1031 (VOID
*)CpuMpData
->WakeupBuffer
,
1032 CpuMpData
->BackupBufferSize
1035 (VOID
*)CpuMpData
->WakeupBuffer
,
1036 (VOID
*)CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
1037 CpuMpData
->BackupBufferSize
- sizeof (MP_CPU_EXCHANGE_INFO
)
1042 Restore wakeup buffer data.
1044 @param[in] CpuMpData The pointer to CPU MP Data structure.
1047 RestoreWakeupBuffer (
1048 IN CPU_MP_DATA
*CpuMpData
1052 (VOID
*)CpuMpData
->WakeupBuffer
,
1053 (VOID
*)CpuMpData
->BackupBuffer
,
1054 CpuMpData
->BackupBufferSize
1059 Allocate reset vector buffer.
1061 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
1064 AllocateResetVectorBelow1Mb (
1065 IN OUT CPU_MP_DATA
*CpuMpData
1068 UINTN ApResetStackSize
;
1070 if (CpuMpData
->WakeupBuffer
== (UINTN
)-1) {
1071 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (CpuMpData
->BackupBufferSize
);
1072 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*)(UINTN
)
1073 (CpuMpData
->WakeupBuffer
+ CpuMpData
->BackupBufferSize
- sizeof (MP_CPU_EXCHANGE_INFO
));
1076 "AP Vector: 16-bit = %p/%x, ExchangeInfo = %p/%x\n",
1077 CpuMpData
->WakeupBuffer
,
1078 CpuMpData
->BackupBufferSize
- sizeof (MP_CPU_EXCHANGE_INFO
),
1079 CpuMpData
->MpCpuExchangeInfo
,
1080 sizeof (MP_CPU_EXCHANGE_INFO
)
1083 // The AP reset stack is only used by SEV-ES guests. Do not allocate it
1084 // if SEV-ES is not enabled. An SEV-SNP guest is also considered
1085 // an SEV-ES guest, but uses a different method of AP startup, eliminating
1086 // the need for the allocation.
1088 if (ConfidentialComputingGuestHas (CCAttrAmdSevEs
) &&
1089 !ConfidentialComputingGuestHas (CCAttrAmdSevSnp
))
1092 // Stack location is based on ProcessorNumber, so use the total number
1093 // of processors for calculating the total stack area.
1095 ApResetStackSize
= (AP_RESET_STACK_SIZE
*
1096 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
1099 // Invoke GetWakeupBuffer a second time to allocate the stack area
1100 // below 1MB. The returned buffer will be page aligned and sized and
1101 // below the previously allocated buffer.
1103 CpuMpData
->SevEsAPResetStackStart
= GetWakeupBuffer (ApResetStackSize
);
1106 // Check to be sure that the "allocate below" behavior hasn't changed.
1107 // This will also catch a failed allocation, as "-1" is returned on
1110 if (CpuMpData
->SevEsAPResetStackStart
>= CpuMpData
->WakeupBuffer
) {
1113 "SEV-ES AP reset stack is not below wakeup buffer\n"
1122 BackupAndPrepareWakeupBuffer (CpuMpData
);
1126 Free AP reset vector buffer.
1128 @param[in] CpuMpData The pointer to CPU MP Data structure.
1132 IN CPU_MP_DATA
*CpuMpData
1136 // If SEV-ES is enabled, the reset area is needed for AP parking and
1137 // and AP startup in the OS, so the reset area is reserved. Do not
1138 // perform the restore as this will overwrite memory which has data
1139 // needed by SEV-ES.
1141 if (!CpuMpData
->UseSevEsAPMethod
) {
1142 RestoreWakeupBuffer (CpuMpData
);
1147 This function will be called by BSP to wakeup AP.
1149 @param[in] CpuMpData Pointer to CPU MP Data
1150 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
1151 FALSE: Send IPI to AP by ApicId
1152 @param[in] ProcessorNumber The handle number of specified processor
1153 @param[in] Procedure The function to be invoked by AP
1154 @param[in] ProcedureArgument The argument to be passed into AP function
1155 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1159 IN CPU_MP_DATA
*CpuMpData
,
1160 IN BOOLEAN Broadcast
,
1161 IN UINTN ProcessorNumber
,
1162 IN EFI_AP_PROCEDURE Procedure OPTIONAL
,
1163 IN VOID
*ProcedureArgument OPTIONAL
,
1164 IN BOOLEAN WakeUpDisabledAps
1167 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
1169 CPU_AP_DATA
*CpuData
;
1170 BOOLEAN ResetVectorRequired
;
1171 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1173 CpuMpData
->FinishedCount
= 0;
1174 ResetVectorRequired
= FALSE
;
1176 if (CpuMpData
->WakeUpByInitSipiSipi
||
1177 (CpuMpData
->InitFlag
!= ApInitDone
))
1179 ResetVectorRequired
= TRUE
;
1180 AllocateResetVectorBelow1Mb (CpuMpData
);
1181 AllocateSevEsAPMemory (CpuMpData
);
1182 FillExchangeInfoData (CpuMpData
);
1183 SaveLocalApicTimerSetting (CpuMpData
);
1186 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1188 // Get AP target C-state each time when waking up AP,
1189 // for it maybe updated by platform again
1191 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1194 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1197 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1198 if (Index
!= CpuMpData
->BspNumber
) {
1199 CpuData
= &CpuMpData
->CpuData
[Index
];
1201 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1202 // the AP procedure will be skipped for disabled AP because AP state
1203 // is not CpuStateReady.
1205 if ((GetApState (CpuData
) == CpuStateDisabled
) && !WakeUpDisabledAps
) {
1209 CpuData
->ApFunction
= (UINTN
)Procedure
;
1210 CpuData
->ApFunctionArgument
= (UINTN
)ProcedureArgument
;
1211 SetApState (CpuData
, CpuStateReady
);
1212 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1213 *(UINT32
*)CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1218 if (ResetVectorRequired
) {
1220 // For SEV-ES and SEV-SNP, the initial AP boot address will be defined by
1221 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address
1222 // from the original INIT-SIPI-SIPI.
1224 if (CpuMpData
->SevEsIsEnabled
) {
1225 SetSevEsJumpTable (ExchangeInfo
->BufferStart
);
1230 // Must use the INIT-SIPI-SIPI method for initial configuration in
1231 // order to obtain the APIC ID.
1233 if (CpuMpData
->SevSnpIsEnabled
&& (CpuMpData
->InitFlag
!= ApInitConfig
)) {
1234 SevSnpCreateAP (CpuMpData
, -1);
1236 SendInitSipiSipiAllExcludingSelf ((UINT32
)ExchangeInfo
->BufferStart
);
1240 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1241 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber
) > 0) {
1243 // The AP enumeration algorithm below is suitable only when the
1244 // platform can tell us the *exact* boot CPU count in advance.
1246 // The wait below finishes only when the detected AP count reaches
1247 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1248 // takes. If at least one AP fails to check in (meaning a platform
1249 // hardware bug), the detection hangs forever, by design. If the actual
1250 // boot CPU count in the system is higher than
1251 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1252 // misconfiguration), then some APs may complete initialization after
1253 // the wait finishes, and cause undefined behavior.
1255 TimedWaitForApFinish (
1257 PcdGet32 (PcdCpuBootLogicalProcessorNumber
) - 1,
1258 MAX_UINT32
// approx. 71 minutes
1262 // The AP enumeration algorithm below is suitable for two use cases.
1264 // (1) The check-in time for an individual AP is bounded, and APs run
1265 // through their initialization routines strongly concurrently. In
1266 // particular, the number of concurrently running APs
1267 // ("NumApsExecuting") is never expected to fall to zero
1268 // *temporarily* -- it is expected to fall to zero only when all
1269 // APs have checked-in.
1271 // In this case, the platform is supposed to set
1272 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1273 // enough for one AP to start initialization). The timeout will be
1274 // reached soon, and remaining APs are collected by watching
1275 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1276 // mid-process, while some APs have not completed initialization,
1277 // the behavior is undefined.
1279 // (2) The check-in time for an individual AP is unbounded, and/or APs
1280 // may complete their initializations widely spread out. In
1281 // particular, some APs may finish initialization before some APs
1284 // In this case, the platform is supposed to set
1285 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1286 // enumeration will always take that long (except when the boot CPU
1287 // count happens to be maximal, that is,
1288 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1289 // check-in before the timeout, and NumApsExecuting is assumed zero
1290 // at timeout. APs that miss the time-out may cause undefined
1293 TimedWaitForApFinish (
1295 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1296 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1299 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1305 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1307 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1308 CpuData
= &CpuMpData
->CpuData
[Index
];
1309 if (Index
!= CpuMpData
->BspNumber
) {
1310 WaitApWakeup (CpuData
->StartupApSignal
);
1315 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1316 CpuData
->ApFunction
= (UINTN
)Procedure
;
1317 CpuData
->ApFunctionArgument
= (UINTN
)ProcedureArgument
;
1318 SetApState (CpuData
, CpuStateReady
);
1320 // Wakeup specified AP
1322 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1323 *(UINT32
*)CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1324 if (ResetVectorRequired
) {
1325 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
1328 // For SEV-ES and SEV-SNP, the initial AP boot address will be defined by
1329 // PcdSevEsWorkAreaBase. The Segment/Rip must be the jump address
1330 // from the original INIT-SIPI-SIPI.
1332 if (CpuMpData
->SevEsIsEnabled
) {
1333 SetSevEsJumpTable (ExchangeInfo
->BufferStart
);
1336 if (CpuMpData
->SevSnpIsEnabled
&& (CpuMpData
->InitFlag
!= ApInitConfig
)) {
1337 SevSnpCreateAP (CpuMpData
, (INTN
)ProcessorNumber
);
1340 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1341 (UINT32
)ExchangeInfo
->BufferStart
1347 // Wait specified AP waken up
1349 WaitApWakeup (CpuData
->StartupApSignal
);
1352 if (ResetVectorRequired
) {
1353 FreeResetVector (CpuMpData
);
1357 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1358 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1359 // S3SmmInitDone Ppi.
1361 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1365 Calculate timeout value and return the current performance counter value.
1367 Calculate the number of performance counter ticks required for a timeout.
1368 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1371 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1372 @param[out] CurrentTime Returns the current value of the performance counter.
1374 @return Expected time stamp counter for timeout.
1375 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1381 IN UINTN TimeoutInMicroseconds
,
1382 OUT UINT64
*CurrentTime
1385 UINT64 TimeoutInSeconds
;
1386 UINT64 TimestampCounterFreq
;
1389 // Read the current value of the performance counter
1391 *CurrentTime
= GetPerformanceCounter ();
1394 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1397 if (TimeoutInMicroseconds
== 0) {
1402 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1405 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1408 // Check the potential overflow before calculate the number of ticks for the timeout value.
1410 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1412 // Convert microseconds into seconds if direct multiplication overflows
1414 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1416 // Assertion if the final tick count exceeds MAX_UINT64
1418 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1419 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1422 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1423 // it by 1,000,000, to get the number of ticks for the timeout value.
1427 TimestampCounterFreq
,
1428 TimeoutInMicroseconds
1436 Checks whether timeout expires.
1438 Check whether the number of elapsed performance counter ticks required for
1439 a timeout condition has been reached.
1440 If Timeout is zero, which means infinity, return value is always FALSE.
1442 @param[in, out] PreviousTime On input, the value of the performance counter
1443 when it was last read.
1444 On output, the current value of the performance
1446 @param[in] TotalTime The total amount of elapsed time in performance
1448 @param[in] Timeout The number of performance counter ticks required
1449 to reach a timeout condition.
1451 @retval TRUE A timeout condition has been reached.
1452 @retval FALSE A timeout condition has not been reached.
1457 IN OUT UINT64
*PreviousTime
,
1458 IN UINT64
*TotalTime
,
1472 GetPerformanceCounterProperties (&Start
, &End
);
1473 Cycle
= End
- Start
;
1479 CurrentTime
= GetPerformanceCounter ();
1480 Delta
= (INT64
)(CurrentTime
- *PreviousTime
);
1489 *TotalTime
+= Delta
;
1490 *PreviousTime
= CurrentTime
;
1491 if (*TotalTime
> Timeout
) {
1499 Helper function that waits until the finished AP count reaches the specified
1500 limit, or the specified timeout elapses (whichever comes first).
1502 @param[in] CpuMpData Pointer to CPU MP Data.
1503 @param[in] FinishedApLimit The number of finished APs to wait for.
1504 @param[in] TimeLimit The number of microseconds to wait for.
1507 TimedWaitForApFinish (
1508 IN CPU_MP_DATA
*CpuMpData
,
1509 IN UINT32 FinishedApLimit
,
1514 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1515 // "infinity", so check for (TimeLimit == 0) explicitly.
1517 if (TimeLimit
== 0) {
1521 CpuMpData
->TotalTime
= 0;
1522 CpuMpData
->ExpectedTime
= CalculateTimeout (
1524 &CpuMpData
->CurrentTime
1526 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1528 &CpuMpData
->CurrentTime
,
1529 &CpuMpData
->TotalTime
,
1530 CpuMpData
->ExpectedTime
1536 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1539 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1542 DivU64x64Remainder (
1543 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1544 GetPerformanceCounterProperties (NULL
, NULL
),
1552 Reset an AP to Idle state.
1554 Any task being executed by the AP will be aborted and the AP
1555 will be waiting for a new task in Wait-For-SIPI state.
1557 @param[in] ProcessorNumber The handle number of processor.
1560 ResetProcessorToIdleState (
1561 IN UINTN ProcessorNumber
1564 CPU_MP_DATA
*CpuMpData
;
1566 CpuMpData
= GetCpuMpData ();
1568 CpuMpData
->InitFlag
= ApInitReconfig
;
1569 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1570 while (CpuMpData
->FinishedCount
< 1) {
1574 CpuMpData
->InitFlag
= ApInitDone
;
1576 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1580 Searches for the next waiting AP.
1582 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1584 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1586 @retval EFI_SUCCESS The next waiting AP has been found.
1587 @retval EFI_NOT_FOUND No waiting AP exists.
1591 GetNextWaitingProcessorNumber (
1592 OUT UINTN
*NextProcessorNumber
1595 UINTN ProcessorNumber
;
1596 CPU_MP_DATA
*CpuMpData
;
1598 CpuMpData
= GetCpuMpData ();
1600 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1601 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1602 *NextProcessorNumber
= ProcessorNumber
;
1607 return EFI_NOT_FOUND
;
1610 /** Checks status of specified AP.
1612 This function checks whether the specified AP has finished the task assigned
1613 by StartupThisAP(), and whether timeout expires.
1615 @param[in] ProcessorNumber The handle number of processor.
1617 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1618 @retval EFI_TIMEOUT The timeout expires.
1619 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1623 IN UINTN ProcessorNumber
1626 CPU_MP_DATA
*CpuMpData
;
1627 CPU_AP_DATA
*CpuData
;
1629 CpuMpData
= GetCpuMpData ();
1630 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1633 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1634 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1635 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1638 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1640 if (GetApState (CpuData
) == CpuStateFinished
) {
1641 if (CpuData
->Finished
!= NULL
) {
1642 *(CpuData
->Finished
) = TRUE
;
1645 SetApState (CpuData
, CpuStateIdle
);
1649 // If timeout expires for StartupThisAP(), report timeout.
1651 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1652 if (CpuData
->Finished
!= NULL
) {
1653 *(CpuData
->Finished
) = FALSE
;
1657 // Reset failed AP to idle state
1659 ResetProcessorToIdleState (ProcessorNumber
);
1665 return EFI_NOT_READY
;
1669 Checks status of all APs.
1671 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1672 and whether timeout expires.
1674 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1675 @retval EFI_TIMEOUT The timeout expires.
1676 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1683 UINTN ProcessorNumber
;
1684 UINTN NextProcessorNumber
;
1687 CPU_MP_DATA
*CpuMpData
;
1688 CPU_AP_DATA
*CpuData
;
1690 CpuMpData
= GetCpuMpData ();
1692 NextProcessorNumber
= 0;
1695 // Go through all APs that are responsible for the StartupAllAPs().
1697 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1698 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1702 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1704 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1705 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1706 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1708 if (GetApState (CpuData
) == CpuStateFinished
) {
1709 CpuMpData
->RunningCount
--;
1710 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1711 SetApState (CpuData
, CpuStateIdle
);
1714 // If in Single Thread mode, then search for the next waiting AP for execution.
1716 if (CpuMpData
->SingleThread
) {
1717 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1719 if (!EFI_ERROR (Status
)) {
1723 (UINT32
)NextProcessorNumber
,
1724 CpuMpData
->Procedure
,
1725 CpuMpData
->ProcArguments
,
1734 // If all APs finish, return EFI_SUCCESS.
1736 if (CpuMpData
->RunningCount
== 0) {
1741 // If timeout expires, report timeout.
1744 &CpuMpData
->CurrentTime
,
1745 &CpuMpData
->TotalTime
,
1746 CpuMpData
->ExpectedTime
1751 // If FailedCpuList is not NULL, record all failed APs in it.
1753 if (CpuMpData
->FailedCpuList
!= NULL
) {
1754 *CpuMpData
->FailedCpuList
=
1755 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1756 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1761 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1763 // Check whether this processor is responsible for StartupAllAPs().
1765 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1767 // Reset failed APs to idle state
1769 ResetProcessorToIdleState (ProcessorNumber
);
1770 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1771 if (CpuMpData
->FailedCpuList
!= NULL
) {
1772 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1777 if (CpuMpData
->FailedCpuList
!= NULL
) {
1778 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1784 return EFI_NOT_READY
;
1788 MP Initialize Library initialization.
1790 This service will allocate AP reset vector and wakeup all APs to do APs
1793 This service must be invoked before all other MP Initialize Library
1794 service are invoked.
1796 @retval EFI_SUCCESS MP initialization succeeds.
1797 @retval Others MP initialization fails.
1802 MpInitLibInitialize (
1806 CPU_MP_DATA
*OldCpuMpData
;
1807 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1808 UINT32 MaxLogicalProcessorNumber
;
1810 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1811 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1813 UINT32 MonitorFilterSize
;
1816 CPU_MP_DATA
*CpuMpData
;
1818 UINT8
*MonitorBuffer
;
1820 UINTN ApResetVectorSizeBelow1Mb
;
1821 UINTN ApResetVectorSizeAbove1Mb
;
1822 UINTN BackupBufferAddr
;
1825 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1826 if (OldCpuMpData
== NULL
) {
1827 MaxLogicalProcessorNumber
= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
);
1829 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1832 ASSERT (MaxLogicalProcessorNumber
!= 0);
1834 AsmGetAddressMap (&AddressMap
);
1835 GetApResetVectorSize (&AddressMap
, &ApResetVectorSizeBelow1Mb
, &ApResetVectorSizeAbove1Mb
);
1836 ApStackSize
= PcdGet32 (PcdCpuApStackSize
);
1838 // ApStackSize must be power of 2
1840 ASSERT ((ApStackSize
& (ApStackSize
- 1)) == 0);
1841 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1844 // Save BSP's Control registers for APs.
1846 SaveVolatileRegisters (&VolatileRegisters
);
1848 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1850 // Allocate extra ApStackSize to let AP stack align on ApStackSize bounday
1852 BufferSize
+= ApStackSize
;
1853 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1854 BufferSize
+= ApResetVectorSizeBelow1Mb
;
1855 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1856 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1857 BufferSize
+= sizeof (CPU_MP_DATA
);
1858 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1859 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1860 ASSERT (MpBuffer
!= NULL
);
1861 ZeroMem (MpBuffer
, BufferSize
);
1862 Buffer
= ALIGN_VALUE ((UINTN
)MpBuffer
, ApStackSize
);
1865 // The layout of the Buffer is as below (lower address on top):
1867 // +--------------------+ <-- Buffer (Pointer of CpuMpData is stored in the top of each AP's stack.)
1868 // AP Stacks (N) (StackTop = (RSP + ApStackSize) & ~ApStackSize))
1869 // +--------------------+ <-- MonitorBuffer
1870 // AP Monitor Filters (N)
1871 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1873 // +--------------------+
1875 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1876 // AP IDT All APs share one separate IDT.
1877 // +--------------------+ <-- CpuMpData
1879 // +--------------------+ <-- CpuMpData->CpuData
1881 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1882 // CPU_INFO_IN_HOB (N)
1883 // +--------------------+
1885 MonitorBuffer
= (UINT8
*)(Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1886 BackupBufferAddr
= (UINTN
)MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1887 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSizeBelow1Mb
, 8);
1888 CpuMpData
= (CPU_MP_DATA
*)(ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1889 CpuMpData
->Buffer
= Buffer
;
1890 CpuMpData
->CpuApStackSize
= ApStackSize
;
1891 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1892 CpuMpData
->BackupBufferSize
= ApResetVectorSizeBelow1Mb
;
1893 CpuMpData
->WakeupBuffer
= (UINTN
)-1;
1894 CpuMpData
->CpuCount
= 1;
1895 CpuMpData
->BspNumber
= 0;
1896 CpuMpData
->WaitEvent
= NULL
;
1897 CpuMpData
->SwitchBspFlag
= FALSE
;
1898 CpuMpData
->CpuData
= (CPU_AP_DATA
*)(CpuMpData
+ 1);
1899 CpuMpData
->CpuInfoInHob
= (UINT64
)(UINTN
)(CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1900 InitializeSpinLock (&CpuMpData
->MpLock
);
1901 CpuMpData
->SevEsIsEnabled
= ConfidentialComputingGuestHas (CCAttrAmdSevEs
);
1902 CpuMpData
->SevSnpIsEnabled
= ConfidentialComputingGuestHas (CCAttrAmdSevSnp
);
1903 CpuMpData
->SevEsAPBuffer
= (UINTN
)-1;
1904 CpuMpData
->GhcbBase
= PcdGet64 (PcdGhcbBase
);
1905 CpuMpData
->UseSevEsAPMethod
= CpuMpData
->SevEsIsEnabled
&& !CpuMpData
->SevSnpIsEnabled
;
1907 if (CpuMpData
->SevSnpIsEnabled
) {
1908 ASSERT ((PcdGet64 (PcdGhcbHypervisorFeatures
) & GHCB_HV_FEATURES_SNP_AP_CREATE
) == GHCB_HV_FEATURES_SNP_AP_CREATE
);
1912 // Make sure no memory usage outside of the allocated buffer.
1913 // (ApStackSize - (Buffer - (UINTN)MpBuffer)) is the redundant caused by alignment
1916 (CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1917 (UINTN
)MpBuffer
+ BufferSize
- (ApStackSize
- Buffer
+ (UINTN
)MpBuffer
)
1921 // Duplicate BSP's IDT to APs.
1922 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1924 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1925 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1927 // Don't pass BSP's TR to APs to avoid AP init failure.
1929 VolatileRegisters
.Tr
= 0;
1930 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1932 // Set BSP basic information
1934 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1936 // Save assembly code information
1938 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1940 // Finally set AP loop mode
1942 CpuMpData
->ApLoopMode
= ApLoopMode
;
1943 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1945 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1948 // Set up APs wakeup signal buffer
1950 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1951 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1952 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1956 // Copy all 32-bit code and 64-bit code into memory with type of
1957 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
1959 CpuMpData
->WakeupBufferHigh
= AllocateCodeBuffer (ApResetVectorSizeAbove1Mb
);
1961 (VOID
*)CpuMpData
->WakeupBufferHigh
,
1962 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
1963 CpuMpData
->AddressMap
.ModeTransitionOffset
,
1964 ApResetVectorSizeAbove1Mb
1966 DEBUG ((DEBUG_INFO
, "AP Vector: non-16-bit = %p/%x\n", CpuMpData
->WakeupBufferHigh
, ApResetVectorSizeAbove1Mb
));
1969 // Enable the local APIC for Virtual Wire Mode.
1971 ProgramVirtualWireMode ();
1973 if (OldCpuMpData
== NULL
) {
1974 if (MaxLogicalProcessorNumber
> 1) {
1976 // Wakeup all APs and calculate the processor count in system
1978 CollectProcessorCount (CpuMpData
);
1982 // APs have been wakeup before, just get the CPU Information
1985 OldCpuMpData
->NewCpuMpData
= CpuMpData
;
1986 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1987 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1988 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1989 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
1990 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1991 InitializeSpinLock (&CpuMpData
->CpuData
[Index
].ApLock
);
1992 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0) ? TRUE
: FALSE
;
1993 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1997 if (!GetMicrocodePatchInfoFromHob (
1998 &CpuMpData
->MicrocodePatchAddress
,
1999 &CpuMpData
->MicrocodePatchRegionSize
2003 // The microcode patch information cache HOB does not exist, which means
2004 // the microcode patches data has not been loaded into memory yet
2006 ShadowMicrocodeUpdatePatch (CpuMpData
);
2010 // Detect and apply Microcode on BSP
2012 MicrocodeDetect (CpuMpData
, CpuMpData
->BspNumber
);
2014 // Store BSP's MTRR setting
2016 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
2019 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
2021 if (CpuMpData
->CpuCount
> 1) {
2022 if (OldCpuMpData
!= NULL
) {
2024 // Only needs to use this flag for DXE phase to update the wake up
2025 // buffer. Wakeup buffer allocated in PEI phase is no longer valid
2028 CpuMpData
->InitFlag
= ApInitReconfig
;
2031 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
2033 // Wait for all APs finished initialization
2035 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
2039 if (OldCpuMpData
!= NULL
) {
2040 CpuMpData
->InitFlag
= ApInitDone
;
2043 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
2044 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
2049 // Dump the microcode revision for each core.
2051 DEBUG_CODE_BEGIN ();
2053 UINT32 ExpectedMicrocodeRevision
;
2055 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
2056 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
2057 GetProcessorLocationByApicId (CpuInfoInHob
[Index
].InitialApicId
, NULL
, NULL
, &ThreadId
);
2058 if (ThreadId
== 0) {
2060 // MicrocodeDetect() loads microcode in first thread of each core, so,
2061 // CpuMpData->CpuData[Index].MicrocodeEntryAddr is initialized only for first thread of each core.
2063 ExpectedMicrocodeRevision
= 0;
2064 if (CpuMpData
->CpuData
[Index
].MicrocodeEntryAddr
!= 0) {
2065 ExpectedMicrocodeRevision
= ((CPU_MICROCODE_HEADER
*)(UINTN
)CpuMpData
->CpuData
[Index
].MicrocodeEntryAddr
)->UpdateRevision
;
2070 "CPU[%04d]: Microcode revision = %08x, expected = %08x\n",
2072 CpuMpData
->CpuData
[Index
].MicrocodeRevision
,
2073 ExpectedMicrocodeRevision
2080 // Initialize global data for MP support
2082 InitMpGlobalData (CpuMpData
);
2088 Gets detailed MP-related information on the requested processor at the
2089 instant this call is made. This service may only be called from the BSP.
2091 @param[in] ProcessorNumber The handle number of processor.
2092 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
2093 the requested processor is deposited.
2094 @param[out] HealthData Return processor health data.
2096 @retval EFI_SUCCESS Processor information was returned.
2097 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2098 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
2099 @retval EFI_NOT_FOUND The processor with the handle specified by
2100 ProcessorNumber does not exist in the platform.
2101 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2106 MpInitLibGetProcessorInfo (
2107 IN UINTN ProcessorNumber
,
2108 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
2109 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
2112 CPU_MP_DATA
*CpuMpData
;
2114 CPU_INFO_IN_HOB
*CpuInfoInHob
;
2115 UINTN OriginalProcessorNumber
;
2117 CpuMpData
= GetCpuMpData ();
2118 CpuInfoInHob
= (CPU_INFO_IN_HOB
*)(UINTN
)CpuMpData
->CpuInfoInHob
;
2121 // Lower 24 bits contains the actual processor number.
2123 OriginalProcessorNumber
= ProcessorNumber
;
2124 ProcessorNumber
&= BIT24
- 1;
2127 // Check whether caller processor is BSP
2129 MpInitLibWhoAmI (&CallerNumber
);
2130 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2131 return EFI_DEVICE_ERROR
;
2134 if (ProcessorInfoBuffer
== NULL
) {
2135 return EFI_INVALID_PARAMETER
;
2138 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2139 return EFI_NOT_FOUND
;
2142 ProcessorInfoBuffer
->ProcessorId
= (UINT64
)CpuInfoInHob
[ProcessorNumber
].ApicId
;
2143 ProcessorInfoBuffer
->StatusFlag
= 0;
2144 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2145 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
2148 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
2149 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
2152 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2153 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
2155 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
2159 // Get processor location information
2161 GetProcessorLocationByApicId (
2162 CpuInfoInHob
[ProcessorNumber
].ApicId
,
2163 &ProcessorInfoBuffer
->Location
.Package
,
2164 &ProcessorInfoBuffer
->Location
.Core
,
2165 &ProcessorInfoBuffer
->Location
.Thread
2168 if ((OriginalProcessorNumber
& CPU_V2_EXTENDED_TOPOLOGY
) != 0) {
2169 GetProcessorLocation2ByApicId (
2170 CpuInfoInHob
[ProcessorNumber
].ApicId
,
2171 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Package
,
2172 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Die
,
2173 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Tile
,
2174 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Module
,
2175 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Core
,
2176 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Thread
2180 if (HealthData
!= NULL
) {
2181 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
2188 Worker function to switch the requested AP to be the BSP from that point onward.
2190 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
2191 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
2192 enabled AP. Otherwise, it will be disabled.
2194 @retval EFI_SUCCESS BSP successfully switched.
2195 @retval others Failed to switch BSP.
2200 IN UINTN ProcessorNumber
,
2201 IN BOOLEAN EnableOldBSP
2204 CPU_MP_DATA
*CpuMpData
;
2207 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
2208 BOOLEAN OldInterruptState
;
2209 BOOLEAN OldTimerInterruptState
;
2212 // Save and Disable Local APIC timer interrupt
2214 OldTimerInterruptState
= GetApicTimerInterruptState ();
2215 DisableApicTimerInterrupt ();
2217 // Before send both BSP and AP to a procedure to exchange their roles,
2218 // interrupt must be disabled. This is because during the exchange role
2219 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
2220 // be corrupted, since interrupt return address will be pushed to stack
2223 OldInterruptState
= SaveAndDisableInterrupts ();
2226 // Mask LINT0 & LINT1 for the old BSP
2228 DisableLvtInterrupts ();
2230 CpuMpData
= GetCpuMpData ();
2233 // Check whether caller processor is BSP
2235 MpInitLibWhoAmI (&CallerNumber
);
2236 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2237 return EFI_DEVICE_ERROR
;
2240 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2241 return EFI_NOT_FOUND
;
2245 // Check whether specified AP is disabled
2247 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
2248 if (State
== CpuStateDisabled
) {
2249 return EFI_INVALID_PARAMETER
;
2253 // Check whether ProcessorNumber specifies the current BSP
2255 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2256 return EFI_INVALID_PARAMETER
;
2260 // Check whether specified AP is busy
2262 if (State
== CpuStateBusy
) {
2263 return EFI_NOT_READY
;
2266 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2267 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2268 CpuMpData
->SwitchBspFlag
= TRUE
;
2269 CpuMpData
->NewBspNumber
= ProcessorNumber
;
2272 // Clear the BSP bit of MSR_IA32_APIC_BASE
2274 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2275 ApicBaseMsr
.Bits
.BSP
= 0;
2276 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2279 // Need to wakeUp AP (future BSP).
2281 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
2284 // Save and restore volatile registers when switch BSP
2286 SaveVolatileRegisters (&CpuMpData
->BSPInfo
.VolatileRegisters
);
2287 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
2288 RestoreVolatileRegisters (&CpuMpData
->BSPInfo
.VolatileRegisters
, FALSE
);
2291 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2293 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2294 ApicBaseMsr
.Bits
.BSP
= 1;
2295 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2296 ProgramVirtualWireMode ();
2299 // Wait for old BSP finished AP task
2301 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
2305 CpuMpData
->SwitchBspFlag
= FALSE
;
2307 // Set old BSP enable state
2309 if (!EnableOldBSP
) {
2310 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
2312 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2316 // Save new BSP number
2318 CpuMpData
->BspNumber
= (UINT32
)ProcessorNumber
;
2321 // Restore interrupt state.
2323 SetInterruptState (OldInterruptState
);
2325 if (OldTimerInterruptState
) {
2326 EnableApicTimerInterrupt ();
2333 Worker function to let the caller enable or disable an AP from this point onward.
2334 This service may only be called from the BSP.
2336 @param[in] ProcessorNumber The handle number of AP.
2337 @param[in] EnableAP Specifies the new state for the processor for
2338 enabled, FALSE for disabled.
2339 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2340 the new health status of the AP.
2342 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2343 @retval others Failed to Enable/Disable AP.
2347 EnableDisableApWorker (
2348 IN UINTN ProcessorNumber
,
2349 IN BOOLEAN EnableAP
,
2350 IN UINT32
*HealthFlag OPTIONAL
2353 CPU_MP_DATA
*CpuMpData
;
2356 CpuMpData
= GetCpuMpData ();
2359 // Check whether caller processor is BSP
2361 MpInitLibWhoAmI (&CallerNumber
);
2362 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2363 return EFI_DEVICE_ERROR
;
2366 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2367 return EFI_INVALID_PARAMETER
;
2370 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2371 return EFI_NOT_FOUND
;
2375 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2377 ResetProcessorToIdleState (ProcessorNumber
);
2380 if (HealthFlag
!= NULL
) {
2381 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2382 (BOOLEAN
)((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2389 This return the handle number for the calling processor. This service may be
2390 called from the BSP and APs.
2392 @param[out] ProcessorNumber Pointer to the handle number of AP.
2393 The range is from 0 to the total number of
2394 logical processors minus 1. The total number of
2395 logical processors can be retrieved by
2396 MpInitLibGetNumberOfProcessors().
2398 @retval EFI_SUCCESS The current processor handle number was returned
2400 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2401 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2407 OUT UINTN
*ProcessorNumber
2410 CPU_MP_DATA
*CpuMpData
;
2412 if (ProcessorNumber
== NULL
) {
2413 return EFI_INVALID_PARAMETER
;
2416 CpuMpData
= GetCpuMpData ();
2418 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2422 Retrieves the number of logical processor in the platform and the number of
2423 those logical processors that are enabled on this boot. This service may only
2424 be called from the BSP.
2426 @param[out] NumberOfProcessors Pointer to the total number of logical
2427 processors in the system, including the BSP
2429 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2430 processors that exist in system, including
2433 @retval EFI_SUCCESS The number of logical processors and enabled
2434 logical processors was retrieved.
2435 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2436 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2438 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2443 MpInitLibGetNumberOfProcessors (
2444 OUT UINTN
*NumberOfProcessors OPTIONAL
,
2445 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2448 CPU_MP_DATA
*CpuMpData
;
2450 UINTN ProcessorNumber
;
2451 UINTN EnabledProcessorNumber
;
2454 CpuMpData
= GetCpuMpData ();
2456 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2457 return EFI_INVALID_PARAMETER
;
2461 // Check whether caller processor is BSP
2463 MpInitLibWhoAmI (&CallerNumber
);
2464 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2465 return EFI_DEVICE_ERROR
;
2468 ProcessorNumber
= CpuMpData
->CpuCount
;
2469 EnabledProcessorNumber
= 0;
2470 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2471 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2472 EnabledProcessorNumber
++;
2476 if (NumberOfProcessors
!= NULL
) {
2477 *NumberOfProcessors
= ProcessorNumber
;
2480 if (NumberOfEnabledProcessors
!= NULL
) {
2481 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2488 Worker function to execute a caller provided function on all enabled APs.
2490 @param[in] Procedure A pointer to the function to be run on
2491 enabled APs of the system.
2492 @param[in] SingleThread If TRUE, then all the enabled APs execute
2493 the function specified by Procedure one by
2494 one, in ascending order of processor handle
2495 number. If FALSE, then all the enabled APs
2496 execute the function specified by Procedure
2498 @param[in] ExcludeBsp Whether let BSP also trig this task.
2499 @param[in] WaitEvent The event created by the caller with CreateEvent()
2501 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2502 APs to return from Procedure, either for
2503 blocking or non-blocking mode.
2504 @param[in] ProcedureArgument The parameter passed into Procedure for
2506 @param[out] FailedCpuList If all APs finish successfully, then its
2507 content is set to NULL. If not all APs
2508 finish before timeout expires, then its
2509 content is set to address of the buffer
2510 holding handle numbers of the failed APs.
2512 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2513 the timeout expired.
2514 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2516 @retval others Failed to Startup all APs.
2520 StartupAllCPUsWorker (
2521 IN EFI_AP_PROCEDURE Procedure
,
2522 IN BOOLEAN SingleThread
,
2523 IN BOOLEAN ExcludeBsp
,
2524 IN EFI_EVENT WaitEvent OPTIONAL
,
2525 IN UINTN TimeoutInMicroseconds
,
2526 IN VOID
*ProcedureArgument OPTIONAL
,
2527 OUT UINTN
**FailedCpuList OPTIONAL
2531 CPU_MP_DATA
*CpuMpData
;
2532 UINTN ProcessorCount
;
2533 UINTN ProcessorNumber
;
2535 CPU_AP_DATA
*CpuData
;
2536 BOOLEAN HasEnabledAp
;
2539 CpuMpData
= GetCpuMpData ();
2541 if (FailedCpuList
!= NULL
) {
2542 *FailedCpuList
= NULL
;
2545 if ((CpuMpData
->CpuCount
== 1) && ExcludeBsp
) {
2546 return EFI_NOT_STARTED
;
2549 if (Procedure
== NULL
) {
2550 return EFI_INVALID_PARAMETER
;
2554 // Check whether caller processor is BSP
2556 MpInitLibWhoAmI (&CallerNumber
);
2557 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2558 return EFI_DEVICE_ERROR
;
2564 CheckAndUpdateApsStatus ();
2566 ProcessorCount
= CpuMpData
->CpuCount
;
2567 HasEnabledAp
= FALSE
;
2569 // Check whether all enabled APs are idle.
2570 // If any enabled AP is not idle, return EFI_NOT_READY.
2572 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2573 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2574 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2575 ApState
= GetApState (CpuData
);
2576 if (ApState
!= CpuStateDisabled
) {
2577 HasEnabledAp
= TRUE
;
2578 if (ApState
!= CpuStateIdle
) {
2580 // If any enabled APs are busy, return EFI_NOT_READY.
2582 return EFI_NOT_READY
;
2588 if (!HasEnabledAp
&& ExcludeBsp
) {
2590 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2592 return EFI_NOT_STARTED
;
2595 CpuMpData
->RunningCount
= 0;
2596 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2597 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2598 CpuData
->Waiting
= FALSE
;
2599 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2600 if (CpuData
->State
== CpuStateIdle
) {
2602 // Mark this processor as responsible for current calling.
2604 CpuData
->Waiting
= TRUE
;
2605 CpuMpData
->RunningCount
++;
2610 CpuMpData
->Procedure
= Procedure
;
2611 CpuMpData
->ProcArguments
= ProcedureArgument
;
2612 CpuMpData
->SingleThread
= SingleThread
;
2613 CpuMpData
->FinishedCount
= 0;
2614 CpuMpData
->FailedCpuList
= FailedCpuList
;
2615 CpuMpData
->ExpectedTime
= CalculateTimeout (
2616 TimeoutInMicroseconds
,
2617 &CpuMpData
->CurrentTime
2619 CpuMpData
->TotalTime
= 0;
2620 CpuMpData
->WaitEvent
= WaitEvent
;
2622 if (!SingleThread
) {
2623 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2625 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2626 if (ProcessorNumber
== CallerNumber
) {
2630 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2631 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2641 Procedure (ProcedureArgument
);
2644 Status
= EFI_SUCCESS
;
2645 if (WaitEvent
== NULL
) {
2647 Status
= CheckAllAPs ();
2648 } while (Status
== EFI_NOT_READY
);
2655 Worker function to let the caller get one enabled AP to execute a caller-provided
2658 @param[in] Procedure A pointer to the function to be run on
2659 enabled APs of the system.
2660 @param[in] ProcessorNumber The handle number of the AP.
2661 @param[in] WaitEvent The event created by the caller with CreateEvent()
2663 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2664 APs to return from Procedure, either for
2665 blocking or non-blocking mode.
2666 @param[in] ProcedureArgument The parameter passed into Procedure for
2668 @param[out] Finished If AP returns from Procedure before the
2669 timeout expires, its content is set to TRUE.
2670 Otherwise, the value is set to FALSE.
2672 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2673 the timeout expires.
2674 @retval others Failed to Startup AP.
2678 StartupThisAPWorker (
2679 IN EFI_AP_PROCEDURE Procedure
,
2680 IN UINTN ProcessorNumber
,
2681 IN EFI_EVENT WaitEvent OPTIONAL
,
2682 IN UINTN TimeoutInMicroseconds
,
2683 IN VOID
*ProcedureArgument OPTIONAL
,
2684 OUT BOOLEAN
*Finished OPTIONAL
2688 CPU_MP_DATA
*CpuMpData
;
2689 CPU_AP_DATA
*CpuData
;
2692 CpuMpData
= GetCpuMpData ();
2694 if (Finished
!= NULL
) {
2699 // Check whether caller processor is BSP
2701 MpInitLibWhoAmI (&CallerNumber
);
2702 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2703 return EFI_DEVICE_ERROR
;
2707 // Check whether processor with the handle specified by ProcessorNumber exists
2709 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2710 return EFI_NOT_FOUND
;
2714 // Check whether specified processor is BSP
2716 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2717 return EFI_INVALID_PARAMETER
;
2721 // Check parameter Procedure
2723 if (Procedure
== NULL
) {
2724 return EFI_INVALID_PARAMETER
;
2730 CheckAndUpdateApsStatus ();
2733 // Check whether specified AP is disabled
2735 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2736 return EFI_INVALID_PARAMETER
;
2740 // If WaitEvent is not NULL, execute in non-blocking mode.
2741 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2742 // CheckAPsStatus() will check completion and timeout periodically.
2744 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2745 CpuData
->WaitEvent
= WaitEvent
;
2746 CpuData
->Finished
= Finished
;
2747 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2748 CpuData
->TotalTime
= 0;
2750 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2753 // If WaitEvent is NULL, execute in blocking mode.
2754 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2756 Status
= EFI_SUCCESS
;
2757 if (WaitEvent
== NULL
) {
2759 Status
= CheckThisAP (ProcessorNumber
);
2760 } while (Status
== EFI_NOT_READY
);
2767 Get pointer to CPU MP Data structure from GUIDed HOB.
2769 @return The pointer to CPU MP Data structure.
2772 GetCpuMpDataFromGuidedHob (
2776 EFI_HOB_GUID_TYPE
*GuidHob
;
2778 CPU_MP_DATA
*CpuMpData
;
2781 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2782 if (GuidHob
!= NULL
) {
2783 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2784 CpuMpData
= (CPU_MP_DATA
*)(*(UINTN
*)DataInHob
);
2791 This service executes a caller provided function on all enabled CPUs.
2793 @param[in] Procedure A pointer to the function to be run on
2794 enabled APs of the system. See type
2796 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2797 APs to return from Procedure, either for
2798 blocking or non-blocking mode. Zero means
2799 infinity. TimeoutInMicroseconds is ignored
2801 @param[in] ProcedureArgument The parameter passed into Procedure for
2804 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2805 the timeout expired.
2806 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2807 to all enabled CPUs.
2808 @retval EFI_DEVICE_ERROR Caller processor is AP.
2809 @retval EFI_NOT_READY Any enabled APs are busy.
2810 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2811 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2812 all enabled APs have finished.
2813 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2818 MpInitLibStartupAllCPUs (
2819 IN EFI_AP_PROCEDURE Procedure
,
2820 IN UINTN TimeoutInMicroseconds
,
2821 IN VOID
*ProcedureArgument OPTIONAL
2824 return StartupAllCPUsWorker (
2829 TimeoutInMicroseconds
,
2836 The function check if the specified Attr is set.
2838 @param[in] CurrentAttr The current attribute.
2839 @param[in] Attr The attribute to check.
2841 @retval TRUE The specified Attr is set.
2842 @retval FALSE The specified Attr is not set.
2847 AmdMemEncryptionAttrCheck (
2848 IN UINT64 CurrentAttr
,
2849 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
2855 // SEV is automatically enabled if SEV-ES or SEV-SNP is active.
2857 return CurrentAttr
>= CCAttrAmdSev
;
2858 case CCAttrAmdSevEs
:
2860 // SEV-ES is automatically enabled if SEV-SNP is active.
2862 return CurrentAttr
>= CCAttrAmdSevEs
;
2863 case CCAttrAmdSevSnp
:
2864 return CurrentAttr
== CCAttrAmdSevSnp
;
2871 Check if the specified confidential computing attribute is active.
2873 @param[in] Attr The attribute to check.
2875 @retval TRUE The specified Attr is active.
2876 @retval FALSE The specified Attr is not active.
2881 ConfidentialComputingGuestHas (
2882 IN CONFIDENTIAL_COMPUTING_GUEST_ATTR Attr
2888 // Get the current CC attribute.
2890 CurrentAttr
= PcdGet64 (PcdConfidentialComputingGuestAttr
);
2893 // If attr is for the AMD group then call AMD specific checks.
2895 if (((RShiftU64 (CurrentAttr
, 8)) & 0xff) == 1) {
2896 return AmdMemEncryptionAttrCheck (CurrentAttr
, Attr
);
2899 return (CurrentAttr
== Attr
);