2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2020, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2020, AMD Inc. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
13 EFI_GUID mCpuInitMpLibHobGuid
= CPU_INIT_MP_LIB_HOB_GUID
;
17 Determine if the standard CPU signature is "AuthenticAMD".
19 @retval TRUE The CPU signature matches.
20 @retval FALSE The CPU signature does not match.
25 StandardSignatureIsAuthenticAMD (
33 AsmCpuid (CPUID_SIGNATURE
, NULL
, &RegEbx
, &RegEcx
, &RegEdx
);
34 return (RegEbx
== CPUID_SIGNATURE_AUTHENTIC_AMD_EBX
&&
35 RegEcx
== CPUID_SIGNATURE_AUTHENTIC_AMD_ECX
&&
36 RegEdx
== CPUID_SIGNATURE_AUTHENTIC_AMD_EDX
);
40 The function will check if BSP Execute Disable is enabled.
42 DxeIpl may have enabled Execute Disable for BSP, APs need to
43 get the status and sync up the settings.
44 If BSP's CR0.Paging is not set, BSP execute Disble feature is
47 @retval TRUE BSP Execute Disable is enabled.
48 @retval FALSE BSP Execute Disable is not enabled.
51 IsBspExecuteDisableEnabled (
56 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
57 MSR_IA32_EFER_REGISTER EferMsr
;
62 Cr0
.UintN
= AsmReadCr0 ();
63 if (Cr0
.Bits
.PG
!= 0) {
65 // If CR0 Paging bit is set
67 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
68 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
69 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
72 // Bit 20: Execute Disable Bit available.
74 if (Edx
.Bits
.NX
!= 0) {
75 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
78 // Bit 11: Execute Disable Bit enable.
80 if (EferMsr
.Bits
.NXE
!= 0) {
91 Worker function for SwitchBSP().
93 Worker function for SwitchBSP(), assigned to the AP which is intended
96 @param[in] Buffer Pointer to CPU MP Data
104 CPU_MP_DATA
*DataInHob
;
106 DataInHob
= (CPU_MP_DATA
*) Buffer
;
107 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
111 Get the Application Processors state.
113 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
115 @return The AP status
119 IN CPU_AP_DATA
*CpuData
122 return CpuData
->State
;
126 Set the Application Processors state.
128 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
129 @param[in] State The AP status
133 IN CPU_AP_DATA
*CpuData
,
137 AcquireSpinLock (&CpuData
->ApLock
);
138 CpuData
->State
= State
;
139 ReleaseSpinLock (&CpuData
->ApLock
);
143 Save BSP's local APIC timer setting.
145 @param[in] CpuMpData Pointer to CPU MP Data
148 SaveLocalApicTimerSetting (
149 IN CPU_MP_DATA
*CpuMpData
153 // Record the current local APIC timer setting of BSP
156 &CpuMpData
->DivideValue
,
157 &CpuMpData
->PeriodicMode
,
160 CpuMpData
->CurrentTimerCount
= GetApicTimerCurrentCount ();
161 CpuMpData
->TimerInterruptState
= GetApicTimerInterruptState ();
165 Sync local APIC timer setting from BSP to AP.
167 @param[in] CpuMpData Pointer to CPU MP Data
170 SyncLocalApicTimerSetting (
171 IN CPU_MP_DATA
*CpuMpData
175 // Sync local APIC timer setting from BSP to AP
177 InitializeApicTimer (
178 CpuMpData
->DivideValue
,
179 CpuMpData
->CurrentTimerCount
,
180 CpuMpData
->PeriodicMode
,
184 // Disable AP's local APIC timer interrupt
186 DisableApicTimerInterrupt ();
190 Save the volatile registers required to be restored following INIT IPI.
192 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
195 SaveVolatileRegisters (
196 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
199 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
201 VolatileRegisters
->Cr0
= AsmReadCr0 ();
202 VolatileRegisters
->Cr3
= AsmReadCr3 ();
203 VolatileRegisters
->Cr4
= AsmReadCr4 ();
205 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
206 if (VersionInfoEdx
.Bits
.DE
!= 0) {
208 // If processor supports Debugging Extensions feature
209 // by CPUID.[EAX=01H]:EDX.BIT2
211 VolatileRegisters
->Dr0
= AsmReadDr0 ();
212 VolatileRegisters
->Dr1
= AsmReadDr1 ();
213 VolatileRegisters
->Dr2
= AsmReadDr2 ();
214 VolatileRegisters
->Dr3
= AsmReadDr3 ();
215 VolatileRegisters
->Dr6
= AsmReadDr6 ();
216 VolatileRegisters
->Dr7
= AsmReadDr7 ();
219 AsmReadGdtr (&VolatileRegisters
->Gdtr
);
220 AsmReadIdtr (&VolatileRegisters
->Idtr
);
221 VolatileRegisters
->Tr
= AsmReadTr ();
225 Restore the volatile registers following INIT IPI.
227 @param[in] VolatileRegisters Pointer to volatile resisters
228 @param[in] IsRestoreDr TRUE: Restore DRx if supported
229 FALSE: Do not restore DRx
232 RestoreVolatileRegisters (
233 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
234 IN BOOLEAN IsRestoreDr
237 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
238 IA32_TSS_DESCRIPTOR
*Tss
;
240 AsmWriteCr3 (VolatileRegisters
->Cr3
);
241 AsmWriteCr4 (VolatileRegisters
->Cr4
);
242 AsmWriteCr0 (VolatileRegisters
->Cr0
);
245 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
246 if (VersionInfoEdx
.Bits
.DE
!= 0) {
248 // If processor supports Debugging Extensions feature
249 // by CPUID.[EAX=01H]:EDX.BIT2
251 AsmWriteDr0 (VolatileRegisters
->Dr0
);
252 AsmWriteDr1 (VolatileRegisters
->Dr1
);
253 AsmWriteDr2 (VolatileRegisters
->Dr2
);
254 AsmWriteDr3 (VolatileRegisters
->Dr3
);
255 AsmWriteDr6 (VolatileRegisters
->Dr6
);
256 AsmWriteDr7 (VolatileRegisters
->Dr7
);
260 AsmWriteGdtr (&VolatileRegisters
->Gdtr
);
261 AsmWriteIdtr (&VolatileRegisters
->Idtr
);
262 if (VolatileRegisters
->Tr
!= 0 &&
263 VolatileRegisters
->Tr
< VolatileRegisters
->Gdtr
.Limit
) {
264 Tss
= (IA32_TSS_DESCRIPTOR
*)(VolatileRegisters
->Gdtr
.Base
+
265 VolatileRegisters
->Tr
);
266 if (Tss
->Bits
.P
== 1) {
267 Tss
->Bits
.Type
&= 0xD; // 1101 - Clear busy bit just in case
268 AsmWriteTr (VolatileRegisters
->Tr
);
274 Detect whether Mwait-monitor feature is supported.
276 @retval TRUE Mwait-monitor feature is supported.
277 @retval FALSE Mwait-monitor feature is not supported.
284 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
286 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
287 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
293 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
295 @return The AP loop mode.
299 OUT UINT32
*MonitorFilterSize
303 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
305 ASSERT (MonitorFilterSize
!= NULL
);
307 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
308 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
309 if (ApLoopMode
== ApInMwaitLoop
) {
310 if (!IsMwaitSupport ()) {
312 // If processor does not support MONITOR/MWAIT feature,
313 // force AP in Hlt-loop mode
315 ApLoopMode
= ApInHltLoop
;
319 if (ApLoopMode
!= ApInMwaitLoop
) {
320 *MonitorFilterSize
= sizeof (UINT32
);
323 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
324 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
326 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
327 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
334 Sort the APIC ID of all processors.
336 This function sorts the APIC ID of all processors so that processor number is
337 assigned in the ascending order of APIC ID which eases MP debugging.
339 @param[in] CpuMpData Pointer to PEI CPU MP Data
343 IN CPU_MP_DATA
*CpuMpData
350 CPU_INFO_IN_HOB CpuInfo
;
352 CPU_INFO_IN_HOB
*CpuInfoInHob
;
353 volatile UINT32
*StartupApSignal
;
355 ApCount
= CpuMpData
->CpuCount
- 1;
356 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
358 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
361 // Sort key is the hardware default APIC ID
363 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
364 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
365 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
367 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
370 if (Index3
!= Index1
) {
371 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
373 &CpuInfoInHob
[Index3
],
374 &CpuInfoInHob
[Index1
],
375 sizeof (CPU_INFO_IN_HOB
)
377 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
380 // Also exchange the StartupApSignal.
382 StartupApSignal
= CpuMpData
->CpuData
[Index3
].StartupApSignal
;
383 CpuMpData
->CpuData
[Index3
].StartupApSignal
=
384 CpuMpData
->CpuData
[Index1
].StartupApSignal
;
385 CpuMpData
->CpuData
[Index1
].StartupApSignal
= StartupApSignal
;
390 // Get the processor number for the BSP
392 ApicId
= GetInitialApicId ();
393 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
394 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
395 CpuMpData
->BspNumber
= (UINT32
) Index1
;
403 Enable x2APIC mode on APs.
405 @param[in, out] Buffer Pointer to private data buffer.
413 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
419 @param[in, out] Buffer Pointer to private data buffer.
427 CPU_MP_DATA
*CpuMpData
;
428 UINTN ProcessorNumber
;
431 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
432 Status
= GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
433 ASSERT_EFI_ERROR (Status
);
435 // Load microcode on AP
437 MicrocodeDetect (CpuMpData
, ProcessorNumber
);
439 // Sync BSP's MTRR table to AP
441 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
445 Find the current Processor number by APIC ID.
447 @param[in] CpuMpData Pointer to PEI CPU MP Data
448 @param[out] ProcessorNumber Return the pocessor number found
450 @retval EFI_SUCCESS ProcessorNumber is found and returned.
451 @retval EFI_NOT_FOUND ProcessorNumber is not found.
455 IN CPU_MP_DATA
*CpuMpData
,
456 OUT UINTN
*ProcessorNumber
459 UINTN TotalProcessorNumber
;
461 CPU_INFO_IN_HOB
*CpuInfoInHob
;
462 UINT32 CurrentApicId
;
464 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
466 TotalProcessorNumber
= CpuMpData
->CpuCount
;
467 CurrentApicId
= GetApicId ();
468 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
469 if (CpuInfoInHob
[Index
].ApicId
== CurrentApicId
) {
470 *ProcessorNumber
= Index
;
475 return EFI_NOT_FOUND
;
479 This function will get CPU count in the system.
481 @param[in] CpuMpData Pointer to PEI CPU MP Data
483 @return CPU count detected
486 CollectProcessorCount (
487 IN CPU_MP_DATA
*CpuMpData
491 CPU_INFO_IN_HOB
*CpuInfoInHob
;
495 // Send 1st broadcast IPI to APs to wakeup APs
497 CpuMpData
->InitFlag
= ApInitConfig
;
498 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
, TRUE
);
499 CpuMpData
->InitFlag
= ApInitDone
;
500 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
502 // Wait for all APs finished the initialization
504 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
510 // Enable x2APIC mode if
511 // 1. Number of CPU is greater than 255; or
512 // 2. There are any logical processors reporting an Initial APIC ID of 255 or greater.
515 if (CpuMpData
->CpuCount
> 255) {
517 // If there are more than 255 processor found, force to enable X2APIC
521 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
522 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
523 if (CpuInfoInHob
[Index
].InitialApicId
>= 0xFF) {
531 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
533 // Wakeup all APs to enable x2APIC mode
535 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
, TRUE
);
537 // Wait for all known APs finished
539 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
543 // Enable x2APIC on BSP
545 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
547 // Set BSP/Aps state to IDLE
549 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
550 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
553 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
555 // Sort BSP/Aps by CPU APIC ID in ascending order
557 SortApicId (CpuMpData
);
559 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
561 return CpuMpData
->CpuCount
;
565 Initialize CPU AP Data when AP is wakeup at the first time.
567 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
568 @param[in] ProcessorNumber The handle number of processor
569 @param[in] BistData Processor BIST data
570 @param[in] ApTopOfStack Top of AP stack
575 IN OUT CPU_MP_DATA
*CpuMpData
,
576 IN UINTN ProcessorNumber
,
578 IN UINT64 ApTopOfStack
581 CPU_INFO_IN_HOB
*CpuInfoInHob
;
582 MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr
;
584 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
585 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
586 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
587 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
588 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
590 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
591 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
594 // NOTE: PlatformId is not relevant on AMD platforms.
596 if (!StandardSignatureIsAuthenticAMD ()) {
597 PlatformIdMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_PLATFORM_ID
);
598 CpuMpData
->CpuData
[ProcessorNumber
].PlatformId
= (UINT8
)PlatformIdMsr
.Bits
.PlatformId
;
603 &CpuMpData
->CpuData
[ProcessorNumber
].ProcessorSignature
,
609 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
610 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
614 This function will be called from AP reset code if BSP uses WakeUpAP.
616 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
617 @param[in] ApIndex Number of current executing AP
622 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
626 CPU_MP_DATA
*CpuMpData
;
627 UINTN ProcessorNumber
;
628 EFI_AP_PROCEDURE Procedure
;
631 volatile UINT32
*ApStartupSignalBuffer
;
632 CPU_INFO_IN_HOB
*CpuInfoInHob
;
634 UINTN CurrentApicMode
;
637 // AP finished assembly code and begin to execute C code
639 CpuMpData
= ExchangeInfo
->CpuMpData
;
642 // AP's local APIC settings will be lost after received INIT IPI
643 // We need to re-initialize them at here
645 ProgramVirtualWireMode ();
647 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
649 DisableLvtInterrupts ();
650 SyncLocalApicTimerSetting (CpuMpData
);
652 CurrentApicMode
= GetApicMode ();
654 if (CpuMpData
->InitFlag
== ApInitConfig
) {
658 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
659 ProcessorNumber
= ApIndex
;
661 // This is first time AP wakeup, get BIST information from AP stack
663 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
664 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
666 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
667 // to initialize AP in InitConfig path.
668 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
670 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
671 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
672 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
674 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
677 // Execute AP function if AP is ready
679 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
681 // Clear AP start-up signal when AP waken up
683 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
684 InterlockedCompareExchange32 (
685 (UINT32
*) ApStartupSignalBuffer
,
689 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
691 // Restore AP's volatile registers saved
693 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
696 // The CPU driver might not flush TLB for APs on spot after updating
697 // page attributes. AP in mwait loop mode needs to take care of it when
703 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
704 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
705 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
706 if (Procedure
!= NULL
) {
707 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
709 // Enable source debugging on AP function
713 // Invoke AP function here
715 Procedure (Parameter
);
716 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
717 if (CpuMpData
->SwitchBspFlag
) {
719 // Re-get the processor number due to BSP/AP maybe exchange in AP function
721 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
722 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
723 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
724 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
725 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
727 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
728 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
729 if (CurrentApicMode
!= GetApicMode ()) {
731 // If APIC mode change happened during AP function execution,
732 // we do not support APIC ID value changed.
738 // Re-get the CPU APICID and Initial APICID if they are changed
740 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
741 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
746 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
751 // AP finished executing C code
753 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
756 // Place AP is specified loop mode
758 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
760 // Save AP volatile registers
762 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
764 // Place AP in HLT-loop
767 DisableInterrupts ();
773 DisableInterrupts ();
774 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
776 // Place AP in MWAIT-loop
778 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
779 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
781 // Check AP start-up signal again.
782 // If AP start-up signal is not set, place AP into
783 // the specified C-state
785 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
787 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
789 // Place AP in Run-loop
797 // If AP start-up signal is written, AP is waken up
798 // otherwise place AP in loop again
800 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
808 Wait for AP wakeup and write AP start-up signal till AP is waken up.
810 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
814 IN
volatile UINT32
*ApStartupSignalBuffer
818 // If AP is waken up, StartupApSignal should be cleared.
819 // Otherwise, write StartupApSignal again till AP waken up.
821 while (InterlockedCompareExchange32 (
822 (UINT32
*) ApStartupSignalBuffer
,
831 This function will fill the exchange info structure.
833 @param[in] CpuMpData Pointer to CPU MP Data
837 FillExchangeInfoData (
838 IN CPU_MP_DATA
*CpuMpData
841 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
843 IA32_SEGMENT_DESCRIPTOR
*Selector
;
846 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
847 ExchangeInfo
->Lock
= 0;
848 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
849 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
850 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
851 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
853 ExchangeInfo
->CodeSegment
= AsmReadCs ();
854 ExchangeInfo
->DataSegment
= AsmReadDs ();
856 ExchangeInfo
->Cr3
= AsmReadCr3 ();
858 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
859 ExchangeInfo
->ApIndex
= 0;
860 ExchangeInfo
->NumApsExecuting
= 0;
861 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
862 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
863 ExchangeInfo
->CpuMpData
= CpuMpData
;
865 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
867 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
870 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
871 // to determin whether 5-Level Paging is enabled.
872 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
873 // current system setting.
874 // Using latter way is simpler because it also eliminates the needs to
875 // check whether platform wants to enable it.
877 Cr4
.UintN
= AsmReadCr4 ();
878 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
) (Cr4
.Bits
.LA57
== 1);
879 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
882 // Get the BSP's data of GDT and IDT
884 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
885 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
888 // Find a 32-bit code segment
890 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
891 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
893 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
894 ExchangeInfo
->ModeTransitionSegment
=
895 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
899 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
903 // Copy all 32-bit code and 64-bit code into memory with type of
904 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
906 if (CpuMpData
->WakeupBufferHigh
!= 0) {
907 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
908 CpuMpData
->AddressMap
.ModeTransitionOffset
;
910 (VOID
*)CpuMpData
->WakeupBufferHigh
,
911 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
912 CpuMpData
->AddressMap
.ModeTransitionOffset
,
916 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
918 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
919 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
922 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
923 (UINT32
)ExchangeInfo
->ModeOffset
-
924 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
925 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
929 Helper function that waits until the finished AP count reaches the specified
930 limit, or the specified timeout elapses (whichever comes first).
932 @param[in] CpuMpData Pointer to CPU MP Data.
933 @param[in] FinishedApLimit The number of finished APs to wait for.
934 @param[in] TimeLimit The number of microseconds to wait for.
937 TimedWaitForApFinish (
938 IN CPU_MP_DATA
*CpuMpData
,
939 IN UINT32 FinishedApLimit
,
944 Get available system memory below 1MB by specified size.
946 @param[in] CpuMpData The pointer to CPU MP Data structure.
949 BackupAndPrepareWakeupBuffer(
950 IN CPU_MP_DATA
*CpuMpData
954 (VOID
*) CpuMpData
->BackupBuffer
,
955 (VOID
*) CpuMpData
->WakeupBuffer
,
956 CpuMpData
->BackupBufferSize
959 (VOID
*) CpuMpData
->WakeupBuffer
,
960 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
961 CpuMpData
->AddressMap
.RendezvousFunnelSize
966 Restore wakeup buffer data.
968 @param[in] CpuMpData The pointer to CPU MP Data structure.
972 IN CPU_MP_DATA
*CpuMpData
976 (VOID
*) CpuMpData
->WakeupBuffer
,
977 (VOID
*) CpuMpData
->BackupBuffer
,
978 CpuMpData
->BackupBufferSize
983 Allocate reset vector buffer.
985 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
988 AllocateResetVector (
989 IN OUT CPU_MP_DATA
*CpuMpData
992 UINTN ApResetVectorSize
;
994 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
995 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
996 sizeof (MP_CPU_EXCHANGE_INFO
);
998 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
999 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
1000 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
1001 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
1002 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
1003 CpuMpData
->AddressMap
.ModeTransitionOffset
1006 BackupAndPrepareWakeupBuffer (CpuMpData
);
1010 Free AP reset vector buffer.
1012 @param[in] CpuMpData The pointer to CPU MP Data structure.
1016 IN CPU_MP_DATA
*CpuMpData
1019 RestoreWakeupBuffer (CpuMpData
);
1023 This function will be called by BSP to wakeup AP.
1025 @param[in] CpuMpData Pointer to CPU MP Data
1026 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
1027 FALSE: Send IPI to AP by ApicId
1028 @param[in] ProcessorNumber The handle number of specified processor
1029 @param[in] Procedure The function to be invoked by AP
1030 @param[in] ProcedureArgument The argument to be passed into AP function
1031 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1035 IN CPU_MP_DATA
*CpuMpData
,
1036 IN BOOLEAN Broadcast
,
1037 IN UINTN ProcessorNumber
,
1038 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
1039 IN VOID
*ProcedureArgument
, OPTIONAL
1040 IN BOOLEAN WakeUpDisabledAps
1043 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
1045 CPU_AP_DATA
*CpuData
;
1046 BOOLEAN ResetVectorRequired
;
1047 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1049 CpuMpData
->FinishedCount
= 0;
1050 ResetVectorRequired
= FALSE
;
1052 if (CpuMpData
->WakeUpByInitSipiSipi
||
1053 CpuMpData
->InitFlag
!= ApInitDone
) {
1054 ResetVectorRequired
= TRUE
;
1055 AllocateResetVector (CpuMpData
);
1056 FillExchangeInfoData (CpuMpData
);
1057 SaveLocalApicTimerSetting (CpuMpData
);
1060 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1062 // Get AP target C-state each time when waking up AP,
1063 // for it maybe updated by platform again
1065 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1068 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1071 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1072 if (Index
!= CpuMpData
->BspNumber
) {
1073 CpuData
= &CpuMpData
->CpuData
[Index
];
1075 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1076 // the AP procedure will be skipped for disabled AP because AP state
1077 // is not CpuStateReady.
1079 if (GetApState (CpuData
) == CpuStateDisabled
&& !WakeUpDisabledAps
) {
1083 CpuData
->ApFunction
= (UINTN
) Procedure
;
1084 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1085 SetApState (CpuData
, CpuStateReady
);
1086 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1087 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1091 if (ResetVectorRequired
) {
1095 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1097 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1098 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber
) > 0) {
1100 // The AP enumeration algorithm below is suitable only when the
1101 // platform can tell us the *exact* boot CPU count in advance.
1103 // The wait below finishes only when the detected AP count reaches
1104 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1105 // takes. If at least one AP fails to check in (meaning a platform
1106 // hardware bug), the detection hangs forever, by design. If the actual
1107 // boot CPU count in the system is higher than
1108 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1109 // misconfiguration), then some APs may complete initialization after
1110 // the wait finishes, and cause undefined behavior.
1112 TimedWaitForApFinish (
1114 PcdGet32 (PcdCpuBootLogicalProcessorNumber
) - 1,
1115 MAX_UINT32
// approx. 71 minutes
1119 // The AP enumeration algorithm below is suitable for two use cases.
1121 // (1) The check-in time for an individual AP is bounded, and APs run
1122 // through their initialization routines strongly concurrently. In
1123 // particular, the number of concurrently running APs
1124 // ("NumApsExecuting") is never expected to fall to zero
1125 // *temporarily* -- it is expected to fall to zero only when all
1126 // APs have checked-in.
1128 // In this case, the platform is supposed to set
1129 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1130 // enough for one AP to start initialization). The timeout will be
1131 // reached soon, and remaining APs are collected by watching
1132 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1133 // mid-process, while some APs have not completed initialization,
1134 // the behavior is undefined.
1136 // (2) The check-in time for an individual AP is unbounded, and/or APs
1137 // may complete their initializations widely spread out. In
1138 // particular, some APs may finish initialization before some APs
1141 // In this case, the platform is supposed to set
1142 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1143 // enumeration will always take that long (except when the boot CPU
1144 // count happens to be maximal, that is,
1145 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1146 // check-in before the timeout, and NumApsExecuting is assumed zero
1147 // at timeout. APs that miss the time-out may cause undefined
1150 TimedWaitForApFinish (
1152 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1153 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1156 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1162 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1164 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1165 CpuData
= &CpuMpData
->CpuData
[Index
];
1166 if (Index
!= CpuMpData
->BspNumber
) {
1167 WaitApWakeup (CpuData
->StartupApSignal
);
1172 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1173 CpuData
->ApFunction
= (UINTN
) Procedure
;
1174 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1175 SetApState (CpuData
, CpuStateReady
);
1177 // Wakeup specified AP
1179 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1180 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1181 if (ResetVectorRequired
) {
1182 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1184 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1185 (UINT32
) ExchangeInfo
->BufferStart
1189 // Wait specified AP waken up
1191 WaitApWakeup (CpuData
->StartupApSignal
);
1194 if (ResetVectorRequired
) {
1195 FreeResetVector (CpuMpData
);
1199 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1200 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1201 // S3SmmInitDone Ppi.
1203 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1207 Calculate timeout value and return the current performance counter value.
1209 Calculate the number of performance counter ticks required for a timeout.
1210 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1213 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1214 @param[out] CurrentTime Returns the current value of the performance counter.
1216 @return Expected time stamp counter for timeout.
1217 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1223 IN UINTN TimeoutInMicroseconds
,
1224 OUT UINT64
*CurrentTime
1227 UINT64 TimeoutInSeconds
;
1228 UINT64 TimestampCounterFreq
;
1231 // Read the current value of the performance counter
1233 *CurrentTime
= GetPerformanceCounter ();
1236 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1239 if (TimeoutInMicroseconds
== 0) {
1244 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1247 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1250 // Check the potential overflow before calculate the number of ticks for the timeout value.
1252 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1254 // Convert microseconds into seconds if direct multiplication overflows
1256 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1258 // Assertion if the final tick count exceeds MAX_UINT64
1260 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1261 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1264 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1265 // it by 1,000,000, to get the number of ticks for the timeout value.
1269 TimestampCounterFreq
,
1270 TimeoutInMicroseconds
1278 Checks whether timeout expires.
1280 Check whether the number of elapsed performance counter ticks required for
1281 a timeout condition has been reached.
1282 If Timeout is zero, which means infinity, return value is always FALSE.
1284 @param[in, out] PreviousTime On input, the value of the performance counter
1285 when it was last read.
1286 On output, the current value of the performance
1288 @param[in] TotalTime The total amount of elapsed time in performance
1290 @param[in] Timeout The number of performance counter ticks required
1291 to reach a timeout condition.
1293 @retval TRUE A timeout condition has been reached.
1294 @retval FALSE A timeout condition has not been reached.
1299 IN OUT UINT64
*PreviousTime
,
1300 IN UINT64
*TotalTime
,
1313 GetPerformanceCounterProperties (&Start
, &End
);
1314 Cycle
= End
- Start
;
1319 CurrentTime
= GetPerformanceCounter();
1320 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1327 *TotalTime
+= Delta
;
1328 *PreviousTime
= CurrentTime
;
1329 if (*TotalTime
> Timeout
) {
1336 Helper function that waits until the finished AP count reaches the specified
1337 limit, or the specified timeout elapses (whichever comes first).
1339 @param[in] CpuMpData Pointer to CPU MP Data.
1340 @param[in] FinishedApLimit The number of finished APs to wait for.
1341 @param[in] TimeLimit The number of microseconds to wait for.
1344 TimedWaitForApFinish (
1345 IN CPU_MP_DATA
*CpuMpData
,
1346 IN UINT32 FinishedApLimit
,
1351 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1352 // "infinity", so check for (TimeLimit == 0) explicitly.
1354 if (TimeLimit
== 0) {
1358 CpuMpData
->TotalTime
= 0;
1359 CpuMpData
->ExpectedTime
= CalculateTimeout (
1361 &CpuMpData
->CurrentTime
1363 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1365 &CpuMpData
->CurrentTime
,
1366 &CpuMpData
->TotalTime
,
1367 CpuMpData
->ExpectedTime
1372 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1375 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1378 DivU64x64Remainder (
1379 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1380 GetPerformanceCounterProperties (NULL
, NULL
),
1388 Reset an AP to Idle state.
1390 Any task being executed by the AP will be aborted and the AP
1391 will be waiting for a new task in Wait-For-SIPI state.
1393 @param[in] ProcessorNumber The handle number of processor.
1396 ResetProcessorToIdleState (
1397 IN UINTN ProcessorNumber
1400 CPU_MP_DATA
*CpuMpData
;
1402 CpuMpData
= GetCpuMpData ();
1404 CpuMpData
->InitFlag
= ApInitReconfig
;
1405 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1406 while (CpuMpData
->FinishedCount
< 1) {
1409 CpuMpData
->InitFlag
= ApInitDone
;
1411 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1415 Searches for the next waiting AP.
1417 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1419 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1421 @retval EFI_SUCCESS The next waiting AP has been found.
1422 @retval EFI_NOT_FOUND No waiting AP exists.
1426 GetNextWaitingProcessorNumber (
1427 OUT UINTN
*NextProcessorNumber
1430 UINTN ProcessorNumber
;
1431 CPU_MP_DATA
*CpuMpData
;
1433 CpuMpData
= GetCpuMpData ();
1435 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1436 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1437 *NextProcessorNumber
= ProcessorNumber
;
1442 return EFI_NOT_FOUND
;
1445 /** Checks status of specified AP.
1447 This function checks whether the specified AP has finished the task assigned
1448 by StartupThisAP(), and whether timeout expires.
1450 @param[in] ProcessorNumber The handle number of processor.
1452 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1453 @retval EFI_TIMEOUT The timeout expires.
1454 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1458 IN UINTN ProcessorNumber
1461 CPU_MP_DATA
*CpuMpData
;
1462 CPU_AP_DATA
*CpuData
;
1464 CpuMpData
= GetCpuMpData ();
1465 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1468 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1469 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1470 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1473 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1475 if (GetApState(CpuData
) == CpuStateFinished
) {
1476 if (CpuData
->Finished
!= NULL
) {
1477 *(CpuData
->Finished
) = TRUE
;
1479 SetApState (CpuData
, CpuStateIdle
);
1483 // If timeout expires for StartupThisAP(), report timeout.
1485 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1486 if (CpuData
->Finished
!= NULL
) {
1487 *(CpuData
->Finished
) = FALSE
;
1490 // Reset failed AP to idle state
1492 ResetProcessorToIdleState (ProcessorNumber
);
1497 return EFI_NOT_READY
;
1501 Checks status of all APs.
1503 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1504 and whether timeout expires.
1506 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1507 @retval EFI_TIMEOUT The timeout expires.
1508 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1515 UINTN ProcessorNumber
;
1516 UINTN NextProcessorNumber
;
1519 CPU_MP_DATA
*CpuMpData
;
1520 CPU_AP_DATA
*CpuData
;
1522 CpuMpData
= GetCpuMpData ();
1524 NextProcessorNumber
= 0;
1527 // Go through all APs that are responsible for the StartupAllAPs().
1529 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1530 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1534 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1536 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1537 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1538 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1540 if (GetApState(CpuData
) == CpuStateFinished
) {
1541 CpuMpData
->RunningCount
--;
1542 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1543 SetApState(CpuData
, CpuStateIdle
);
1546 // If in Single Thread mode, then search for the next waiting AP for execution.
1548 if (CpuMpData
->SingleThread
) {
1549 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1551 if (!EFI_ERROR (Status
)) {
1555 (UINT32
) NextProcessorNumber
,
1556 CpuMpData
->Procedure
,
1557 CpuMpData
->ProcArguments
,
1566 // If all APs finish, return EFI_SUCCESS.
1568 if (CpuMpData
->RunningCount
== 0) {
1573 // If timeout expires, report timeout.
1576 &CpuMpData
->CurrentTime
,
1577 &CpuMpData
->TotalTime
,
1578 CpuMpData
->ExpectedTime
)
1581 // If FailedCpuList is not NULL, record all failed APs in it.
1583 if (CpuMpData
->FailedCpuList
!= NULL
) {
1584 *CpuMpData
->FailedCpuList
=
1585 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1586 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1590 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1592 // Check whether this processor is responsible for StartupAllAPs().
1594 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1596 // Reset failed APs to idle state
1598 ResetProcessorToIdleState (ProcessorNumber
);
1599 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1600 if (CpuMpData
->FailedCpuList
!= NULL
) {
1601 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1605 if (CpuMpData
->FailedCpuList
!= NULL
) {
1606 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1610 return EFI_NOT_READY
;
1614 MP Initialize Library initialization.
1616 This service will allocate AP reset vector and wakeup all APs to do APs
1619 This service must be invoked before all other MP Initialize Library
1620 service are invoked.
1622 @retval EFI_SUCCESS MP initialization succeeds.
1623 @retval Others MP initialization fails.
1628 MpInitLibInitialize (
1632 CPU_MP_DATA
*OldCpuMpData
;
1633 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1634 UINT32 MaxLogicalProcessorNumber
;
1636 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1637 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1639 UINT32 MonitorFilterSize
;
1642 CPU_MP_DATA
*CpuMpData
;
1644 UINT8
*MonitorBuffer
;
1646 UINTN ApResetVectorSize
;
1647 UINTN BackupBufferAddr
;
1650 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1651 if (OldCpuMpData
== NULL
) {
1652 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1654 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1656 ASSERT (MaxLogicalProcessorNumber
!= 0);
1658 AsmGetAddressMap (&AddressMap
);
1659 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1660 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1661 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1664 // Save BSP's Control registers for APs.
1666 SaveVolatileRegisters (&VolatileRegisters
);
1668 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1669 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1670 BufferSize
+= ApResetVectorSize
;
1671 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1672 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1673 BufferSize
+= sizeof (CPU_MP_DATA
);
1674 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1675 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1676 ASSERT (MpBuffer
!= NULL
);
1677 ZeroMem (MpBuffer
, BufferSize
);
1678 Buffer
= (UINTN
) MpBuffer
;
1681 // The layout of the Buffer is as below:
1683 // +--------------------+ <-- Buffer
1685 // +--------------------+ <-- MonitorBuffer
1686 // AP Monitor Filters (N)
1687 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1689 // +--------------------+
1691 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1692 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1693 // +--------------------+ <-- CpuMpData
1695 // +--------------------+ <-- CpuMpData->CpuData
1697 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1698 // CPU_INFO_IN_HOB (N)
1699 // +--------------------+
1701 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1702 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1703 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1704 CpuMpData
= (CPU_MP_DATA
*) (ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1705 CpuMpData
->Buffer
= Buffer
;
1706 CpuMpData
->CpuApStackSize
= ApStackSize
;
1707 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1708 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1709 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1710 CpuMpData
->CpuCount
= 1;
1711 CpuMpData
->BspNumber
= 0;
1712 CpuMpData
->WaitEvent
= NULL
;
1713 CpuMpData
->SwitchBspFlag
= FALSE
;
1714 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1715 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1716 InitializeSpinLock(&CpuMpData
->MpLock
);
1719 // Make sure no memory usage outside of the allocated buffer.
1721 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1722 Buffer
+ BufferSize
);
1725 // Duplicate BSP's IDT to APs.
1726 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1728 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1729 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1731 // Don't pass BSP's TR to APs to avoid AP init failure.
1733 VolatileRegisters
.Tr
= 0;
1734 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1736 // Set BSP basic information
1738 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1740 // Save assembly code information
1742 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1744 // Finally set AP loop mode
1746 CpuMpData
->ApLoopMode
= ApLoopMode
;
1747 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1749 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1752 // Set up APs wakeup signal buffer
1754 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1755 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1756 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1759 // Enable the local APIC for Virtual Wire Mode.
1761 ProgramVirtualWireMode ();
1763 if (OldCpuMpData
== NULL
) {
1764 if (MaxLogicalProcessorNumber
> 1) {
1766 // Wakeup all APs and calculate the processor count in system
1768 CollectProcessorCount (CpuMpData
);
1772 // APs have been wakeup before, just get the CPU Information
1775 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1776 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1777 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1778 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1779 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1780 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1781 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1782 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1783 CopyMem (&CpuMpData
->CpuData
[Index
].VolatileRegisters
, &VolatileRegisters
, sizeof (CPU_VOLATILE_REGISTERS
));
1787 if (!GetMicrocodePatchInfoFromHob (
1788 &CpuMpData
->MicrocodePatchAddress
,
1789 &CpuMpData
->MicrocodePatchRegionSize
1792 // The microcode patch information cache HOB does not exist, which means
1793 // the microcode patches data has not been loaded into memory yet
1795 ShadowMicrocodeUpdatePatch (CpuMpData
);
1799 // Detect and apply Microcode on BSP
1801 MicrocodeDetect (CpuMpData
, CpuMpData
->BspNumber
);
1803 // Store BSP's MTRR setting
1805 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1808 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1810 if (CpuMpData
->CpuCount
> 1) {
1811 CpuMpData
->InitFlag
= ApInitReconfig
;
1812 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1814 // Wait for all APs finished initialization
1816 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1819 CpuMpData
->InitFlag
= ApInitDone
;
1820 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1821 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1826 // Initialize global data for MP support
1828 InitMpGlobalData (CpuMpData
);
1834 Gets detailed MP-related information on the requested processor at the
1835 instant this call is made. This service may only be called from the BSP.
1837 @param[in] ProcessorNumber The handle number of processor.
1838 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1839 the requested processor is deposited.
1840 @param[out] HealthData Return processor health data.
1842 @retval EFI_SUCCESS Processor information was returned.
1843 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1844 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1845 @retval EFI_NOT_FOUND The processor with the handle specified by
1846 ProcessorNumber does not exist in the platform.
1847 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1852 MpInitLibGetProcessorInfo (
1853 IN UINTN ProcessorNumber
,
1854 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1855 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1858 CPU_MP_DATA
*CpuMpData
;
1860 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1862 CpuMpData
= GetCpuMpData ();
1863 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1866 // Check whether caller processor is BSP
1868 MpInitLibWhoAmI (&CallerNumber
);
1869 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1870 return EFI_DEVICE_ERROR
;
1873 if (ProcessorInfoBuffer
== NULL
) {
1874 return EFI_INVALID_PARAMETER
;
1877 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1878 return EFI_NOT_FOUND
;
1881 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1882 ProcessorInfoBuffer
->StatusFlag
= 0;
1883 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1884 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1886 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1887 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1889 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1890 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1892 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1896 // Get processor location information
1898 GetProcessorLocationByApicId (
1899 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1900 &ProcessorInfoBuffer
->Location
.Package
,
1901 &ProcessorInfoBuffer
->Location
.Core
,
1902 &ProcessorInfoBuffer
->Location
.Thread
1905 if (HealthData
!= NULL
) {
1906 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1913 Worker function to switch the requested AP to be the BSP from that point onward.
1915 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1916 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1917 enabled AP. Otherwise, it will be disabled.
1919 @retval EFI_SUCCESS BSP successfully switched.
1920 @retval others Failed to switch BSP.
1925 IN UINTN ProcessorNumber
,
1926 IN BOOLEAN EnableOldBSP
1929 CPU_MP_DATA
*CpuMpData
;
1932 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1933 BOOLEAN OldInterruptState
;
1934 BOOLEAN OldTimerInterruptState
;
1937 // Save and Disable Local APIC timer interrupt
1939 OldTimerInterruptState
= GetApicTimerInterruptState ();
1940 DisableApicTimerInterrupt ();
1942 // Before send both BSP and AP to a procedure to exchange their roles,
1943 // interrupt must be disabled. This is because during the exchange role
1944 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1945 // be corrupted, since interrupt return address will be pushed to stack
1948 OldInterruptState
= SaveAndDisableInterrupts ();
1951 // Mask LINT0 & LINT1 for the old BSP
1953 DisableLvtInterrupts ();
1955 CpuMpData
= GetCpuMpData ();
1958 // Check whether caller processor is BSP
1960 MpInitLibWhoAmI (&CallerNumber
);
1961 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1962 return EFI_DEVICE_ERROR
;
1965 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1966 return EFI_NOT_FOUND
;
1970 // Check whether specified AP is disabled
1972 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1973 if (State
== CpuStateDisabled
) {
1974 return EFI_INVALID_PARAMETER
;
1978 // Check whether ProcessorNumber specifies the current BSP
1980 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1981 return EFI_INVALID_PARAMETER
;
1985 // Check whether specified AP is busy
1987 if (State
== CpuStateBusy
) {
1988 return EFI_NOT_READY
;
1991 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1992 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1993 CpuMpData
->SwitchBspFlag
= TRUE
;
1994 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1997 // Clear the BSP bit of MSR_IA32_APIC_BASE
1999 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2000 ApicBaseMsr
.Bits
.BSP
= 0;
2001 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2004 // Need to wakeUp AP (future BSP).
2006 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
2008 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
2011 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2013 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2014 ApicBaseMsr
.Bits
.BSP
= 1;
2015 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2016 ProgramVirtualWireMode ();
2019 // Wait for old BSP finished AP task
2021 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
2025 CpuMpData
->SwitchBspFlag
= FALSE
;
2027 // Set old BSP enable state
2029 if (!EnableOldBSP
) {
2030 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
2032 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2035 // Save new BSP number
2037 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
2040 // Restore interrupt state.
2042 SetInterruptState (OldInterruptState
);
2044 if (OldTimerInterruptState
) {
2045 EnableApicTimerInterrupt ();
2052 Worker function to let the caller enable or disable an AP from this point onward.
2053 This service may only be called from the BSP.
2055 @param[in] ProcessorNumber The handle number of AP.
2056 @param[in] EnableAP Specifies the new state for the processor for
2057 enabled, FALSE for disabled.
2058 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2059 the new health status of the AP.
2061 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2062 @retval others Failed to Enable/Disable AP.
2066 EnableDisableApWorker (
2067 IN UINTN ProcessorNumber
,
2068 IN BOOLEAN EnableAP
,
2069 IN UINT32
*HealthFlag OPTIONAL
2072 CPU_MP_DATA
*CpuMpData
;
2075 CpuMpData
= GetCpuMpData ();
2078 // Check whether caller processor is BSP
2080 MpInitLibWhoAmI (&CallerNumber
);
2081 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2082 return EFI_DEVICE_ERROR
;
2085 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2086 return EFI_INVALID_PARAMETER
;
2089 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2090 return EFI_NOT_FOUND
;
2094 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2096 ResetProcessorToIdleState (ProcessorNumber
);
2099 if (HealthFlag
!= NULL
) {
2100 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2101 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2108 This return the handle number for the calling processor. This service may be
2109 called from the BSP and APs.
2111 @param[out] ProcessorNumber Pointer to the handle number of AP.
2112 The range is from 0 to the total number of
2113 logical processors minus 1. The total number of
2114 logical processors can be retrieved by
2115 MpInitLibGetNumberOfProcessors().
2117 @retval EFI_SUCCESS The current processor handle number was returned
2119 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2120 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2126 OUT UINTN
*ProcessorNumber
2129 CPU_MP_DATA
*CpuMpData
;
2131 if (ProcessorNumber
== NULL
) {
2132 return EFI_INVALID_PARAMETER
;
2135 CpuMpData
= GetCpuMpData ();
2137 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2141 Retrieves the number of logical processor in the platform and the number of
2142 those logical processors that are enabled on this boot. This service may only
2143 be called from the BSP.
2145 @param[out] NumberOfProcessors Pointer to the total number of logical
2146 processors in the system, including the BSP
2148 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2149 processors that exist in system, including
2152 @retval EFI_SUCCESS The number of logical processors and enabled
2153 logical processors was retrieved.
2154 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2155 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2157 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2162 MpInitLibGetNumberOfProcessors (
2163 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2164 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2167 CPU_MP_DATA
*CpuMpData
;
2169 UINTN ProcessorNumber
;
2170 UINTN EnabledProcessorNumber
;
2173 CpuMpData
= GetCpuMpData ();
2175 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2176 return EFI_INVALID_PARAMETER
;
2180 // Check whether caller processor is BSP
2182 MpInitLibWhoAmI (&CallerNumber
);
2183 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2184 return EFI_DEVICE_ERROR
;
2187 ProcessorNumber
= CpuMpData
->CpuCount
;
2188 EnabledProcessorNumber
= 0;
2189 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2190 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2191 EnabledProcessorNumber
++;
2195 if (NumberOfProcessors
!= NULL
) {
2196 *NumberOfProcessors
= ProcessorNumber
;
2198 if (NumberOfEnabledProcessors
!= NULL
) {
2199 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2207 Worker function to execute a caller provided function on all enabled APs.
2209 @param[in] Procedure A pointer to the function to be run on
2210 enabled APs of the system.
2211 @param[in] SingleThread If TRUE, then all the enabled APs execute
2212 the function specified by Procedure one by
2213 one, in ascending order of processor handle
2214 number. If FALSE, then all the enabled APs
2215 execute the function specified by Procedure
2217 @param[in] ExcludeBsp Whether let BSP also trig this task.
2218 @param[in] WaitEvent The event created by the caller with CreateEvent()
2220 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2221 APs to return from Procedure, either for
2222 blocking or non-blocking mode.
2223 @param[in] ProcedureArgument The parameter passed into Procedure for
2225 @param[out] FailedCpuList If all APs finish successfully, then its
2226 content is set to NULL. If not all APs
2227 finish before timeout expires, then its
2228 content is set to address of the buffer
2229 holding handle numbers of the failed APs.
2231 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2232 the timeout expired.
2233 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2235 @retval others Failed to Startup all APs.
2239 StartupAllCPUsWorker (
2240 IN EFI_AP_PROCEDURE Procedure
,
2241 IN BOOLEAN SingleThread
,
2242 IN BOOLEAN ExcludeBsp
,
2243 IN EFI_EVENT WaitEvent OPTIONAL
,
2244 IN UINTN TimeoutInMicroseconds
,
2245 IN VOID
*ProcedureArgument OPTIONAL
,
2246 OUT UINTN
**FailedCpuList OPTIONAL
2250 CPU_MP_DATA
*CpuMpData
;
2251 UINTN ProcessorCount
;
2252 UINTN ProcessorNumber
;
2254 CPU_AP_DATA
*CpuData
;
2255 BOOLEAN HasEnabledAp
;
2258 CpuMpData
= GetCpuMpData ();
2260 if (FailedCpuList
!= NULL
) {
2261 *FailedCpuList
= NULL
;
2264 if (CpuMpData
->CpuCount
== 1 && ExcludeBsp
) {
2265 return EFI_NOT_STARTED
;
2268 if (Procedure
== NULL
) {
2269 return EFI_INVALID_PARAMETER
;
2273 // Check whether caller processor is BSP
2275 MpInitLibWhoAmI (&CallerNumber
);
2276 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2277 return EFI_DEVICE_ERROR
;
2283 CheckAndUpdateApsStatus ();
2285 ProcessorCount
= CpuMpData
->CpuCount
;
2286 HasEnabledAp
= FALSE
;
2288 // Check whether all enabled APs are idle.
2289 // If any enabled AP is not idle, return EFI_NOT_READY.
2291 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2292 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2293 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2294 ApState
= GetApState (CpuData
);
2295 if (ApState
!= CpuStateDisabled
) {
2296 HasEnabledAp
= TRUE
;
2297 if (ApState
!= CpuStateIdle
) {
2299 // If any enabled APs are busy, return EFI_NOT_READY.
2301 return EFI_NOT_READY
;
2307 if (!HasEnabledAp
&& ExcludeBsp
) {
2309 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2311 return EFI_NOT_STARTED
;
2314 CpuMpData
->RunningCount
= 0;
2315 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2316 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2317 CpuData
->Waiting
= FALSE
;
2318 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2319 if (CpuData
->State
== CpuStateIdle
) {
2321 // Mark this processor as responsible for current calling.
2323 CpuData
->Waiting
= TRUE
;
2324 CpuMpData
->RunningCount
++;
2329 CpuMpData
->Procedure
= Procedure
;
2330 CpuMpData
->ProcArguments
= ProcedureArgument
;
2331 CpuMpData
->SingleThread
= SingleThread
;
2332 CpuMpData
->FinishedCount
= 0;
2333 CpuMpData
->FailedCpuList
= FailedCpuList
;
2334 CpuMpData
->ExpectedTime
= CalculateTimeout (
2335 TimeoutInMicroseconds
,
2336 &CpuMpData
->CurrentTime
2338 CpuMpData
->TotalTime
= 0;
2339 CpuMpData
->WaitEvent
= WaitEvent
;
2341 if (!SingleThread
) {
2342 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2344 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2345 if (ProcessorNumber
== CallerNumber
) {
2348 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2349 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2359 Procedure (ProcedureArgument
);
2362 Status
= EFI_SUCCESS
;
2363 if (WaitEvent
== NULL
) {
2365 Status
= CheckAllAPs ();
2366 } while (Status
== EFI_NOT_READY
);
2373 Worker function to let the caller get one enabled AP to execute a caller-provided
2376 @param[in] Procedure A pointer to the function to be run on
2377 enabled APs of the system.
2378 @param[in] ProcessorNumber The handle number of the AP.
2379 @param[in] WaitEvent The event created by the caller with CreateEvent()
2381 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2382 APs to return from Procedure, either for
2383 blocking or non-blocking mode.
2384 @param[in] ProcedureArgument The parameter passed into Procedure for
2386 @param[out] Finished If AP returns from Procedure before the
2387 timeout expires, its content is set to TRUE.
2388 Otherwise, the value is set to FALSE.
2390 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2391 the timeout expires.
2392 @retval others Failed to Startup AP.
2396 StartupThisAPWorker (
2397 IN EFI_AP_PROCEDURE Procedure
,
2398 IN UINTN ProcessorNumber
,
2399 IN EFI_EVENT WaitEvent OPTIONAL
,
2400 IN UINTN TimeoutInMicroseconds
,
2401 IN VOID
*ProcedureArgument OPTIONAL
,
2402 OUT BOOLEAN
*Finished OPTIONAL
2406 CPU_MP_DATA
*CpuMpData
;
2407 CPU_AP_DATA
*CpuData
;
2410 CpuMpData
= GetCpuMpData ();
2412 if (Finished
!= NULL
) {
2417 // Check whether caller processor is BSP
2419 MpInitLibWhoAmI (&CallerNumber
);
2420 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2421 return EFI_DEVICE_ERROR
;
2425 // Check whether processor with the handle specified by ProcessorNumber exists
2427 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2428 return EFI_NOT_FOUND
;
2432 // Check whether specified processor is BSP
2434 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2435 return EFI_INVALID_PARAMETER
;
2439 // Check parameter Procedure
2441 if (Procedure
== NULL
) {
2442 return EFI_INVALID_PARAMETER
;
2448 CheckAndUpdateApsStatus ();
2451 // Check whether specified AP is disabled
2453 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2454 return EFI_INVALID_PARAMETER
;
2458 // If WaitEvent is not NULL, execute in non-blocking mode.
2459 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2460 // CheckAPsStatus() will check completion and timeout periodically.
2462 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2463 CpuData
->WaitEvent
= WaitEvent
;
2464 CpuData
->Finished
= Finished
;
2465 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2466 CpuData
->TotalTime
= 0;
2468 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2471 // If WaitEvent is NULL, execute in blocking mode.
2472 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2474 Status
= EFI_SUCCESS
;
2475 if (WaitEvent
== NULL
) {
2477 Status
= CheckThisAP (ProcessorNumber
);
2478 } while (Status
== EFI_NOT_READY
);
2485 Get pointer to CPU MP Data structure from GUIDed HOB.
2487 @return The pointer to CPU MP Data structure.
2490 GetCpuMpDataFromGuidedHob (
2494 EFI_HOB_GUID_TYPE
*GuidHob
;
2496 CPU_MP_DATA
*CpuMpData
;
2499 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2500 if (GuidHob
!= NULL
) {
2501 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2502 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2508 This service executes a caller provided function on all enabled CPUs.
2510 @param[in] Procedure A pointer to the function to be run on
2511 enabled APs of the system. See type
2513 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2514 APs to return from Procedure, either for
2515 blocking or non-blocking mode. Zero means
2516 infinity. TimeoutInMicroseconds is ignored
2518 @param[in] ProcedureArgument The parameter passed into Procedure for
2521 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2522 the timeout expired.
2523 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2524 to all enabled CPUs.
2525 @retval EFI_DEVICE_ERROR Caller processor is AP.
2526 @retval EFI_NOT_READY Any enabled APs are busy.
2527 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2528 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2529 all enabled APs have finished.
2530 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2535 MpInitLibStartupAllCPUs (
2536 IN EFI_AP_PROCEDURE Procedure
,
2537 IN UINTN TimeoutInMicroseconds
,
2538 IN VOID
*ProcedureArgument OPTIONAL
2541 return StartupAllCPUsWorker (
2546 TimeoutInMicroseconds
,