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
,
690 if (CpuMpData
->InitFlag
== ApInitReconfig
) {
692 // ApInitReconfig happens when:
693 // 1. AP is re-enabled after it's disabled, in either PEI or DXE phase.
694 // 2. AP is initialized in DXE phase.
695 // In either case, use the volatile registers value derived from BSP.
696 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a
697 // different IDT shared by all APs.
699 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
701 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
703 // Restore AP's volatile registers saved before AP is halted
705 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
708 // The CPU driver might not flush TLB for APs on spot after updating
709 // page attributes. AP in mwait loop mode needs to take care of it when
716 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
717 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
718 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
719 if (Procedure
!= NULL
) {
720 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
722 // Enable source debugging on AP function
726 // Invoke AP function here
728 Procedure (Parameter
);
729 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
730 if (CpuMpData
->SwitchBspFlag
) {
732 // Re-get the processor number due to BSP/AP maybe exchange in AP function
734 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
735 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
736 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
737 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
738 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
740 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
741 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
742 if (CurrentApicMode
!= GetApicMode ()) {
744 // If APIC mode change happened during AP function execution,
745 // we do not support APIC ID value changed.
751 // Re-get the CPU APICID and Initial APICID if they are changed
753 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
754 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
759 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
764 // AP finished executing C code
766 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
769 // Place AP is specified loop mode
771 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
773 // Save AP volatile registers
775 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
777 // Place AP in HLT-loop
780 DisableInterrupts ();
786 DisableInterrupts ();
787 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
789 // Place AP in MWAIT-loop
791 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
792 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
794 // Check AP start-up signal again.
795 // If AP start-up signal is not set, place AP into
796 // the specified C-state
798 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
800 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
802 // Place AP in Run-loop
810 // If AP start-up signal is written, AP is waken up
811 // otherwise place AP in loop again
813 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
821 Wait for AP wakeup and write AP start-up signal till AP is waken up.
823 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
827 IN
volatile UINT32
*ApStartupSignalBuffer
831 // If AP is waken up, StartupApSignal should be cleared.
832 // Otherwise, write StartupApSignal again till AP waken up.
834 while (InterlockedCompareExchange32 (
835 (UINT32
*) ApStartupSignalBuffer
,
844 This function will fill the exchange info structure.
846 @param[in] CpuMpData Pointer to CPU MP Data
850 FillExchangeInfoData (
851 IN CPU_MP_DATA
*CpuMpData
854 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
856 IA32_SEGMENT_DESCRIPTOR
*Selector
;
859 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
860 ExchangeInfo
->Lock
= 0;
861 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
862 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
863 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
864 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
866 ExchangeInfo
->CodeSegment
= AsmReadCs ();
867 ExchangeInfo
->DataSegment
= AsmReadDs ();
869 ExchangeInfo
->Cr3
= AsmReadCr3 ();
871 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
872 ExchangeInfo
->ApIndex
= 0;
873 ExchangeInfo
->NumApsExecuting
= 0;
874 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
875 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
876 ExchangeInfo
->CpuMpData
= CpuMpData
;
878 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
880 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
883 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
884 // to determin whether 5-Level Paging is enabled.
885 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
886 // current system setting.
887 // Using latter way is simpler because it also eliminates the needs to
888 // check whether platform wants to enable it.
890 Cr4
.UintN
= AsmReadCr4 ();
891 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
) (Cr4
.Bits
.LA57
== 1);
892 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
895 // Get the BSP's data of GDT and IDT
897 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
898 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
901 // Find a 32-bit code segment
903 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
904 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
906 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
907 ExchangeInfo
->ModeTransitionSegment
=
908 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
912 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
916 // Copy all 32-bit code and 64-bit code into memory with type of
917 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
919 if (CpuMpData
->WakeupBufferHigh
!= 0) {
920 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
921 CpuMpData
->AddressMap
.ModeTransitionOffset
;
923 (VOID
*)CpuMpData
->WakeupBufferHigh
,
924 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
925 CpuMpData
->AddressMap
.ModeTransitionOffset
,
929 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
931 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
932 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
935 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
936 (UINT32
)ExchangeInfo
->ModeOffset
-
937 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
938 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
942 Helper function that waits until the finished AP count reaches the specified
943 limit, or the specified timeout elapses (whichever comes first).
945 @param[in] CpuMpData Pointer to CPU MP Data.
946 @param[in] FinishedApLimit The number of finished APs to wait for.
947 @param[in] TimeLimit The number of microseconds to wait for.
950 TimedWaitForApFinish (
951 IN CPU_MP_DATA
*CpuMpData
,
952 IN UINT32 FinishedApLimit
,
957 Get available system memory below 1MB by specified size.
959 @param[in] CpuMpData The pointer to CPU MP Data structure.
962 BackupAndPrepareWakeupBuffer(
963 IN CPU_MP_DATA
*CpuMpData
967 (VOID
*) CpuMpData
->BackupBuffer
,
968 (VOID
*) CpuMpData
->WakeupBuffer
,
969 CpuMpData
->BackupBufferSize
972 (VOID
*) CpuMpData
->WakeupBuffer
,
973 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
974 CpuMpData
->AddressMap
.RendezvousFunnelSize
979 Restore wakeup buffer data.
981 @param[in] CpuMpData The pointer to CPU MP Data structure.
985 IN CPU_MP_DATA
*CpuMpData
989 (VOID
*) CpuMpData
->WakeupBuffer
,
990 (VOID
*) CpuMpData
->BackupBuffer
,
991 CpuMpData
->BackupBufferSize
996 Allocate reset vector buffer.
998 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
1001 AllocateResetVector (
1002 IN OUT CPU_MP_DATA
*CpuMpData
1005 UINTN ApResetVectorSize
;
1007 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
1008 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
1009 sizeof (MP_CPU_EXCHANGE_INFO
);
1011 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
1012 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
1013 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
1014 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
1015 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
1016 CpuMpData
->AddressMap
.ModeTransitionOffset
1019 BackupAndPrepareWakeupBuffer (CpuMpData
);
1023 Free AP reset vector buffer.
1025 @param[in] CpuMpData The pointer to CPU MP Data structure.
1029 IN CPU_MP_DATA
*CpuMpData
1032 RestoreWakeupBuffer (CpuMpData
);
1036 This function will be called by BSP to wakeup AP.
1038 @param[in] CpuMpData Pointer to CPU MP Data
1039 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
1040 FALSE: Send IPI to AP by ApicId
1041 @param[in] ProcessorNumber The handle number of specified processor
1042 @param[in] Procedure The function to be invoked by AP
1043 @param[in] ProcedureArgument The argument to be passed into AP function
1044 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1048 IN CPU_MP_DATA
*CpuMpData
,
1049 IN BOOLEAN Broadcast
,
1050 IN UINTN ProcessorNumber
,
1051 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
1052 IN VOID
*ProcedureArgument
, OPTIONAL
1053 IN BOOLEAN WakeUpDisabledAps
1056 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
1058 CPU_AP_DATA
*CpuData
;
1059 BOOLEAN ResetVectorRequired
;
1060 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1062 CpuMpData
->FinishedCount
= 0;
1063 ResetVectorRequired
= FALSE
;
1065 if (CpuMpData
->WakeUpByInitSipiSipi
||
1066 CpuMpData
->InitFlag
!= ApInitDone
) {
1067 ResetVectorRequired
= TRUE
;
1068 AllocateResetVector (CpuMpData
);
1069 FillExchangeInfoData (CpuMpData
);
1070 SaveLocalApicTimerSetting (CpuMpData
);
1073 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1075 // Get AP target C-state each time when waking up AP,
1076 // for it maybe updated by platform again
1078 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1081 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1084 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1085 if (Index
!= CpuMpData
->BspNumber
) {
1086 CpuData
= &CpuMpData
->CpuData
[Index
];
1088 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1089 // the AP procedure will be skipped for disabled AP because AP state
1090 // is not CpuStateReady.
1092 if (GetApState (CpuData
) == CpuStateDisabled
&& !WakeUpDisabledAps
) {
1096 CpuData
->ApFunction
= (UINTN
) Procedure
;
1097 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1098 SetApState (CpuData
, CpuStateReady
);
1099 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1100 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1104 if (ResetVectorRequired
) {
1108 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1110 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1111 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber
) > 0) {
1113 // The AP enumeration algorithm below is suitable only when the
1114 // platform can tell us the *exact* boot CPU count in advance.
1116 // The wait below finishes only when the detected AP count reaches
1117 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1118 // takes. If at least one AP fails to check in (meaning a platform
1119 // hardware bug), the detection hangs forever, by design. If the actual
1120 // boot CPU count in the system is higher than
1121 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1122 // misconfiguration), then some APs may complete initialization after
1123 // the wait finishes, and cause undefined behavior.
1125 TimedWaitForApFinish (
1127 PcdGet32 (PcdCpuBootLogicalProcessorNumber
) - 1,
1128 MAX_UINT32
// approx. 71 minutes
1132 // The AP enumeration algorithm below is suitable for two use cases.
1134 // (1) The check-in time for an individual AP is bounded, and APs run
1135 // through their initialization routines strongly concurrently. In
1136 // particular, the number of concurrently running APs
1137 // ("NumApsExecuting") is never expected to fall to zero
1138 // *temporarily* -- it is expected to fall to zero only when all
1139 // APs have checked-in.
1141 // In this case, the platform is supposed to set
1142 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1143 // enough for one AP to start initialization). The timeout will be
1144 // reached soon, and remaining APs are collected by watching
1145 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1146 // mid-process, while some APs have not completed initialization,
1147 // the behavior is undefined.
1149 // (2) The check-in time for an individual AP is unbounded, and/or APs
1150 // may complete their initializations widely spread out. In
1151 // particular, some APs may finish initialization before some APs
1154 // In this case, the platform is supposed to set
1155 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1156 // enumeration will always take that long (except when the boot CPU
1157 // count happens to be maximal, that is,
1158 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1159 // check-in before the timeout, and NumApsExecuting is assumed zero
1160 // at timeout. APs that miss the time-out may cause undefined
1163 TimedWaitForApFinish (
1165 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1166 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1169 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1175 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1177 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1178 CpuData
= &CpuMpData
->CpuData
[Index
];
1179 if (Index
!= CpuMpData
->BspNumber
) {
1180 WaitApWakeup (CpuData
->StartupApSignal
);
1185 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1186 CpuData
->ApFunction
= (UINTN
) Procedure
;
1187 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1188 SetApState (CpuData
, CpuStateReady
);
1190 // Wakeup specified AP
1192 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1193 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1194 if (ResetVectorRequired
) {
1195 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1197 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1198 (UINT32
) ExchangeInfo
->BufferStart
1202 // Wait specified AP waken up
1204 WaitApWakeup (CpuData
->StartupApSignal
);
1207 if (ResetVectorRequired
) {
1208 FreeResetVector (CpuMpData
);
1212 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1213 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1214 // S3SmmInitDone Ppi.
1216 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1220 Calculate timeout value and return the current performance counter value.
1222 Calculate the number of performance counter ticks required for a timeout.
1223 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1226 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1227 @param[out] CurrentTime Returns the current value of the performance counter.
1229 @return Expected time stamp counter for timeout.
1230 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1236 IN UINTN TimeoutInMicroseconds
,
1237 OUT UINT64
*CurrentTime
1240 UINT64 TimeoutInSeconds
;
1241 UINT64 TimestampCounterFreq
;
1244 // Read the current value of the performance counter
1246 *CurrentTime
= GetPerformanceCounter ();
1249 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1252 if (TimeoutInMicroseconds
== 0) {
1257 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1260 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1263 // Check the potential overflow before calculate the number of ticks for the timeout value.
1265 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1267 // Convert microseconds into seconds if direct multiplication overflows
1269 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1271 // Assertion if the final tick count exceeds MAX_UINT64
1273 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1274 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1277 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1278 // it by 1,000,000, to get the number of ticks for the timeout value.
1282 TimestampCounterFreq
,
1283 TimeoutInMicroseconds
1291 Checks whether timeout expires.
1293 Check whether the number of elapsed performance counter ticks required for
1294 a timeout condition has been reached.
1295 If Timeout is zero, which means infinity, return value is always FALSE.
1297 @param[in, out] PreviousTime On input, the value of the performance counter
1298 when it was last read.
1299 On output, the current value of the performance
1301 @param[in] TotalTime The total amount of elapsed time in performance
1303 @param[in] Timeout The number of performance counter ticks required
1304 to reach a timeout condition.
1306 @retval TRUE A timeout condition has been reached.
1307 @retval FALSE A timeout condition has not been reached.
1312 IN OUT UINT64
*PreviousTime
,
1313 IN UINT64
*TotalTime
,
1326 GetPerformanceCounterProperties (&Start
, &End
);
1327 Cycle
= End
- Start
;
1332 CurrentTime
= GetPerformanceCounter();
1333 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1340 *TotalTime
+= Delta
;
1341 *PreviousTime
= CurrentTime
;
1342 if (*TotalTime
> Timeout
) {
1349 Helper function that waits until the finished AP count reaches the specified
1350 limit, or the specified timeout elapses (whichever comes first).
1352 @param[in] CpuMpData Pointer to CPU MP Data.
1353 @param[in] FinishedApLimit The number of finished APs to wait for.
1354 @param[in] TimeLimit The number of microseconds to wait for.
1357 TimedWaitForApFinish (
1358 IN CPU_MP_DATA
*CpuMpData
,
1359 IN UINT32 FinishedApLimit
,
1364 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1365 // "infinity", so check for (TimeLimit == 0) explicitly.
1367 if (TimeLimit
== 0) {
1371 CpuMpData
->TotalTime
= 0;
1372 CpuMpData
->ExpectedTime
= CalculateTimeout (
1374 &CpuMpData
->CurrentTime
1376 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1378 &CpuMpData
->CurrentTime
,
1379 &CpuMpData
->TotalTime
,
1380 CpuMpData
->ExpectedTime
1385 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1388 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1391 DivU64x64Remainder (
1392 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1393 GetPerformanceCounterProperties (NULL
, NULL
),
1401 Reset an AP to Idle state.
1403 Any task being executed by the AP will be aborted and the AP
1404 will be waiting for a new task in Wait-For-SIPI state.
1406 @param[in] ProcessorNumber The handle number of processor.
1409 ResetProcessorToIdleState (
1410 IN UINTN ProcessorNumber
1413 CPU_MP_DATA
*CpuMpData
;
1415 CpuMpData
= GetCpuMpData ();
1417 CpuMpData
->InitFlag
= ApInitReconfig
;
1418 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1419 while (CpuMpData
->FinishedCount
< 1) {
1422 CpuMpData
->InitFlag
= ApInitDone
;
1424 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1428 Searches for the next waiting AP.
1430 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1432 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1434 @retval EFI_SUCCESS The next waiting AP has been found.
1435 @retval EFI_NOT_FOUND No waiting AP exists.
1439 GetNextWaitingProcessorNumber (
1440 OUT UINTN
*NextProcessorNumber
1443 UINTN ProcessorNumber
;
1444 CPU_MP_DATA
*CpuMpData
;
1446 CpuMpData
= GetCpuMpData ();
1448 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1449 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1450 *NextProcessorNumber
= ProcessorNumber
;
1455 return EFI_NOT_FOUND
;
1458 /** Checks status of specified AP.
1460 This function checks whether the specified AP has finished the task assigned
1461 by StartupThisAP(), and whether timeout expires.
1463 @param[in] ProcessorNumber The handle number of processor.
1465 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1466 @retval EFI_TIMEOUT The timeout expires.
1467 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1471 IN UINTN ProcessorNumber
1474 CPU_MP_DATA
*CpuMpData
;
1475 CPU_AP_DATA
*CpuData
;
1477 CpuMpData
= GetCpuMpData ();
1478 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1481 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1482 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1483 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1486 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1488 if (GetApState(CpuData
) == CpuStateFinished
) {
1489 if (CpuData
->Finished
!= NULL
) {
1490 *(CpuData
->Finished
) = TRUE
;
1492 SetApState (CpuData
, CpuStateIdle
);
1496 // If timeout expires for StartupThisAP(), report timeout.
1498 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1499 if (CpuData
->Finished
!= NULL
) {
1500 *(CpuData
->Finished
) = FALSE
;
1503 // Reset failed AP to idle state
1505 ResetProcessorToIdleState (ProcessorNumber
);
1510 return EFI_NOT_READY
;
1514 Checks status of all APs.
1516 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1517 and whether timeout expires.
1519 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1520 @retval EFI_TIMEOUT The timeout expires.
1521 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1528 UINTN ProcessorNumber
;
1529 UINTN NextProcessorNumber
;
1532 CPU_MP_DATA
*CpuMpData
;
1533 CPU_AP_DATA
*CpuData
;
1535 CpuMpData
= GetCpuMpData ();
1537 NextProcessorNumber
= 0;
1540 // Go through all APs that are responsible for the StartupAllAPs().
1542 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1543 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1547 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1549 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1550 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1551 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1553 if (GetApState(CpuData
) == CpuStateFinished
) {
1554 CpuMpData
->RunningCount
--;
1555 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1556 SetApState(CpuData
, CpuStateIdle
);
1559 // If in Single Thread mode, then search for the next waiting AP for execution.
1561 if (CpuMpData
->SingleThread
) {
1562 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1564 if (!EFI_ERROR (Status
)) {
1568 (UINT32
) NextProcessorNumber
,
1569 CpuMpData
->Procedure
,
1570 CpuMpData
->ProcArguments
,
1579 // If all APs finish, return EFI_SUCCESS.
1581 if (CpuMpData
->RunningCount
== 0) {
1586 // If timeout expires, report timeout.
1589 &CpuMpData
->CurrentTime
,
1590 &CpuMpData
->TotalTime
,
1591 CpuMpData
->ExpectedTime
)
1594 // If FailedCpuList is not NULL, record all failed APs in it.
1596 if (CpuMpData
->FailedCpuList
!= NULL
) {
1597 *CpuMpData
->FailedCpuList
=
1598 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1599 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1603 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1605 // Check whether this processor is responsible for StartupAllAPs().
1607 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1609 // Reset failed APs to idle state
1611 ResetProcessorToIdleState (ProcessorNumber
);
1612 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1613 if (CpuMpData
->FailedCpuList
!= NULL
) {
1614 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1618 if (CpuMpData
->FailedCpuList
!= NULL
) {
1619 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1623 return EFI_NOT_READY
;
1627 MP Initialize Library initialization.
1629 This service will allocate AP reset vector and wakeup all APs to do APs
1632 This service must be invoked before all other MP Initialize Library
1633 service are invoked.
1635 @retval EFI_SUCCESS MP initialization succeeds.
1636 @retval Others MP initialization fails.
1641 MpInitLibInitialize (
1645 CPU_MP_DATA
*OldCpuMpData
;
1646 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1647 UINT32 MaxLogicalProcessorNumber
;
1649 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1650 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1652 UINT32 MonitorFilterSize
;
1655 CPU_MP_DATA
*CpuMpData
;
1657 UINT8
*MonitorBuffer
;
1659 UINTN ApResetVectorSize
;
1660 UINTN BackupBufferAddr
;
1663 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1664 if (OldCpuMpData
== NULL
) {
1665 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1667 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1669 ASSERT (MaxLogicalProcessorNumber
!= 0);
1671 AsmGetAddressMap (&AddressMap
);
1672 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1673 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1674 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1677 // Save BSP's Control registers for APs.
1679 SaveVolatileRegisters (&VolatileRegisters
);
1681 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1682 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1683 BufferSize
+= ApResetVectorSize
;
1684 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1685 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1686 BufferSize
+= sizeof (CPU_MP_DATA
);
1687 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1688 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1689 ASSERT (MpBuffer
!= NULL
);
1690 ZeroMem (MpBuffer
, BufferSize
);
1691 Buffer
= (UINTN
) MpBuffer
;
1694 // The layout of the Buffer is as below:
1696 // +--------------------+ <-- Buffer
1698 // +--------------------+ <-- MonitorBuffer
1699 // AP Monitor Filters (N)
1700 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1702 // +--------------------+
1704 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1705 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1706 // +--------------------+ <-- CpuMpData
1708 // +--------------------+ <-- CpuMpData->CpuData
1710 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1711 // CPU_INFO_IN_HOB (N)
1712 // +--------------------+
1714 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1715 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1716 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1717 CpuMpData
= (CPU_MP_DATA
*) (ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1718 CpuMpData
->Buffer
= Buffer
;
1719 CpuMpData
->CpuApStackSize
= ApStackSize
;
1720 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1721 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1722 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1723 CpuMpData
->CpuCount
= 1;
1724 CpuMpData
->BspNumber
= 0;
1725 CpuMpData
->WaitEvent
= NULL
;
1726 CpuMpData
->SwitchBspFlag
= FALSE
;
1727 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1728 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1729 InitializeSpinLock(&CpuMpData
->MpLock
);
1732 // Make sure no memory usage outside of the allocated buffer.
1734 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1735 Buffer
+ BufferSize
);
1738 // Duplicate BSP's IDT to APs.
1739 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1741 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1742 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1744 // Don't pass BSP's TR to APs to avoid AP init failure.
1746 VolatileRegisters
.Tr
= 0;
1747 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1749 // Set BSP basic information
1751 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1753 // Save assembly code information
1755 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1757 // Finally set AP loop mode
1759 CpuMpData
->ApLoopMode
= ApLoopMode
;
1760 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1762 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1765 // Set up APs wakeup signal buffer
1767 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1768 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1769 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1772 // Enable the local APIC for Virtual Wire Mode.
1774 ProgramVirtualWireMode ();
1776 if (OldCpuMpData
== NULL
) {
1777 if (MaxLogicalProcessorNumber
> 1) {
1779 // Wakeup all APs and calculate the processor count in system
1781 CollectProcessorCount (CpuMpData
);
1785 // APs have been wakeup before, just get the CPU Information
1788 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1789 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1790 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1791 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1792 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1793 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1794 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1795 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1799 if (!GetMicrocodePatchInfoFromHob (
1800 &CpuMpData
->MicrocodePatchAddress
,
1801 &CpuMpData
->MicrocodePatchRegionSize
1804 // The microcode patch information cache HOB does not exist, which means
1805 // the microcode patches data has not been loaded into memory yet
1807 ShadowMicrocodeUpdatePatch (CpuMpData
);
1811 // Detect and apply Microcode on BSP
1813 MicrocodeDetect (CpuMpData
, CpuMpData
->BspNumber
);
1815 // Store BSP's MTRR setting
1817 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1820 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1822 if (CpuMpData
->CpuCount
> 1) {
1823 if (OldCpuMpData
!= NULL
) {
1825 // Only needs to use this flag for DXE phase to update the wake up
1826 // buffer. Wakeup buffer allocated in PEI phase is no longer valid
1829 CpuMpData
->InitFlag
= ApInitReconfig
;
1831 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1833 // Wait for all APs finished initialization
1835 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1838 if (OldCpuMpData
!= NULL
) {
1839 CpuMpData
->InitFlag
= ApInitDone
;
1841 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1842 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1847 // Initialize global data for MP support
1849 InitMpGlobalData (CpuMpData
);
1855 Gets detailed MP-related information on the requested processor at the
1856 instant this call is made. This service may only be called from the BSP.
1858 @param[in] ProcessorNumber The handle number of processor.
1859 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1860 the requested processor is deposited.
1861 @param[out] HealthData Return processor health data.
1863 @retval EFI_SUCCESS Processor information was returned.
1864 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1865 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1866 @retval EFI_NOT_FOUND The processor with the handle specified by
1867 ProcessorNumber does not exist in the platform.
1868 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1873 MpInitLibGetProcessorInfo (
1874 IN UINTN ProcessorNumber
,
1875 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1876 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1879 CPU_MP_DATA
*CpuMpData
;
1881 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1882 UINTN OriginalProcessorNumber
;
1884 CpuMpData
= GetCpuMpData ();
1885 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1888 // Lower 24 bits contains the actual processor number.
1890 OriginalProcessorNumber
= ProcessorNumber
;
1891 ProcessorNumber
&= BIT24
- 1;
1894 // Check whether caller processor is BSP
1896 MpInitLibWhoAmI (&CallerNumber
);
1897 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1898 return EFI_DEVICE_ERROR
;
1901 if (ProcessorInfoBuffer
== NULL
) {
1902 return EFI_INVALID_PARAMETER
;
1905 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1906 return EFI_NOT_FOUND
;
1909 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1910 ProcessorInfoBuffer
->StatusFlag
= 0;
1911 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1912 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1914 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1915 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1917 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1918 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1920 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1924 // Get processor location information
1926 GetProcessorLocationByApicId (
1927 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1928 &ProcessorInfoBuffer
->Location
.Package
,
1929 &ProcessorInfoBuffer
->Location
.Core
,
1930 &ProcessorInfoBuffer
->Location
.Thread
1933 if ((OriginalProcessorNumber
& CPU_V2_EXTENDED_TOPOLOGY
) != 0) {
1934 GetProcessorLocation2ByApicId (
1935 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1936 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Package
,
1937 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Die
,
1938 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Tile
,
1939 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Module
,
1940 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Core
,
1941 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Thread
1945 if (HealthData
!= NULL
) {
1946 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1953 Worker function to switch the requested AP to be the BSP from that point onward.
1955 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1956 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1957 enabled AP. Otherwise, it will be disabled.
1959 @retval EFI_SUCCESS BSP successfully switched.
1960 @retval others Failed to switch BSP.
1965 IN UINTN ProcessorNumber
,
1966 IN BOOLEAN EnableOldBSP
1969 CPU_MP_DATA
*CpuMpData
;
1972 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1973 BOOLEAN OldInterruptState
;
1974 BOOLEAN OldTimerInterruptState
;
1977 // Save and Disable Local APIC timer interrupt
1979 OldTimerInterruptState
= GetApicTimerInterruptState ();
1980 DisableApicTimerInterrupt ();
1982 // Before send both BSP and AP to a procedure to exchange their roles,
1983 // interrupt must be disabled. This is because during the exchange role
1984 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1985 // be corrupted, since interrupt return address will be pushed to stack
1988 OldInterruptState
= SaveAndDisableInterrupts ();
1991 // Mask LINT0 & LINT1 for the old BSP
1993 DisableLvtInterrupts ();
1995 CpuMpData
= GetCpuMpData ();
1998 // Check whether caller processor is BSP
2000 MpInitLibWhoAmI (&CallerNumber
);
2001 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2002 return EFI_DEVICE_ERROR
;
2005 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2006 return EFI_NOT_FOUND
;
2010 // Check whether specified AP is disabled
2012 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
2013 if (State
== CpuStateDisabled
) {
2014 return EFI_INVALID_PARAMETER
;
2018 // Check whether ProcessorNumber specifies the current BSP
2020 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2021 return EFI_INVALID_PARAMETER
;
2025 // Check whether specified AP is busy
2027 if (State
== CpuStateBusy
) {
2028 return EFI_NOT_READY
;
2031 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2032 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2033 CpuMpData
->SwitchBspFlag
= TRUE
;
2034 CpuMpData
->NewBspNumber
= ProcessorNumber
;
2037 // Clear the BSP bit of MSR_IA32_APIC_BASE
2039 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2040 ApicBaseMsr
.Bits
.BSP
= 0;
2041 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2044 // Need to wakeUp AP (future BSP).
2046 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
2048 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
2051 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2053 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2054 ApicBaseMsr
.Bits
.BSP
= 1;
2055 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2056 ProgramVirtualWireMode ();
2059 // Wait for old BSP finished AP task
2061 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
2065 CpuMpData
->SwitchBspFlag
= FALSE
;
2067 // Set old BSP enable state
2069 if (!EnableOldBSP
) {
2070 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
2072 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2075 // Save new BSP number
2077 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
2080 // Restore interrupt state.
2082 SetInterruptState (OldInterruptState
);
2084 if (OldTimerInterruptState
) {
2085 EnableApicTimerInterrupt ();
2092 Worker function to let the caller enable or disable an AP from this point onward.
2093 This service may only be called from the BSP.
2095 @param[in] ProcessorNumber The handle number of AP.
2096 @param[in] EnableAP Specifies the new state for the processor for
2097 enabled, FALSE for disabled.
2098 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2099 the new health status of the AP.
2101 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2102 @retval others Failed to Enable/Disable AP.
2106 EnableDisableApWorker (
2107 IN UINTN ProcessorNumber
,
2108 IN BOOLEAN EnableAP
,
2109 IN UINT32
*HealthFlag OPTIONAL
2112 CPU_MP_DATA
*CpuMpData
;
2115 CpuMpData
= GetCpuMpData ();
2118 // Check whether caller processor is BSP
2120 MpInitLibWhoAmI (&CallerNumber
);
2121 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2122 return EFI_DEVICE_ERROR
;
2125 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2126 return EFI_INVALID_PARAMETER
;
2129 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2130 return EFI_NOT_FOUND
;
2134 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2136 ResetProcessorToIdleState (ProcessorNumber
);
2139 if (HealthFlag
!= NULL
) {
2140 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2141 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2148 This return the handle number for the calling processor. This service may be
2149 called from the BSP and APs.
2151 @param[out] ProcessorNumber Pointer to the handle number of AP.
2152 The range is from 0 to the total number of
2153 logical processors minus 1. The total number of
2154 logical processors can be retrieved by
2155 MpInitLibGetNumberOfProcessors().
2157 @retval EFI_SUCCESS The current processor handle number was returned
2159 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2160 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2166 OUT UINTN
*ProcessorNumber
2169 CPU_MP_DATA
*CpuMpData
;
2171 if (ProcessorNumber
== NULL
) {
2172 return EFI_INVALID_PARAMETER
;
2175 CpuMpData
= GetCpuMpData ();
2177 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2181 Retrieves the number of logical processor in the platform and the number of
2182 those logical processors that are enabled on this boot. This service may only
2183 be called from the BSP.
2185 @param[out] NumberOfProcessors Pointer to the total number of logical
2186 processors in the system, including the BSP
2188 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2189 processors that exist in system, including
2192 @retval EFI_SUCCESS The number of logical processors and enabled
2193 logical processors was retrieved.
2194 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2195 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2197 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2202 MpInitLibGetNumberOfProcessors (
2203 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2204 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2207 CPU_MP_DATA
*CpuMpData
;
2209 UINTN ProcessorNumber
;
2210 UINTN EnabledProcessorNumber
;
2213 CpuMpData
= GetCpuMpData ();
2215 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2216 return EFI_INVALID_PARAMETER
;
2220 // Check whether caller processor is BSP
2222 MpInitLibWhoAmI (&CallerNumber
);
2223 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2224 return EFI_DEVICE_ERROR
;
2227 ProcessorNumber
= CpuMpData
->CpuCount
;
2228 EnabledProcessorNumber
= 0;
2229 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2230 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2231 EnabledProcessorNumber
++;
2235 if (NumberOfProcessors
!= NULL
) {
2236 *NumberOfProcessors
= ProcessorNumber
;
2238 if (NumberOfEnabledProcessors
!= NULL
) {
2239 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2247 Worker function to execute a caller provided function on all enabled APs.
2249 @param[in] Procedure A pointer to the function to be run on
2250 enabled APs of the system.
2251 @param[in] SingleThread If TRUE, then all the enabled APs execute
2252 the function specified by Procedure one by
2253 one, in ascending order of processor handle
2254 number. If FALSE, then all the enabled APs
2255 execute the function specified by Procedure
2257 @param[in] ExcludeBsp Whether let BSP also trig this task.
2258 @param[in] WaitEvent The event created by the caller with CreateEvent()
2260 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2261 APs to return from Procedure, either for
2262 blocking or non-blocking mode.
2263 @param[in] ProcedureArgument The parameter passed into Procedure for
2265 @param[out] FailedCpuList If all APs finish successfully, then its
2266 content is set to NULL. If not all APs
2267 finish before timeout expires, then its
2268 content is set to address of the buffer
2269 holding handle numbers of the failed APs.
2271 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2272 the timeout expired.
2273 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2275 @retval others Failed to Startup all APs.
2279 StartupAllCPUsWorker (
2280 IN EFI_AP_PROCEDURE Procedure
,
2281 IN BOOLEAN SingleThread
,
2282 IN BOOLEAN ExcludeBsp
,
2283 IN EFI_EVENT WaitEvent OPTIONAL
,
2284 IN UINTN TimeoutInMicroseconds
,
2285 IN VOID
*ProcedureArgument OPTIONAL
,
2286 OUT UINTN
**FailedCpuList OPTIONAL
2290 CPU_MP_DATA
*CpuMpData
;
2291 UINTN ProcessorCount
;
2292 UINTN ProcessorNumber
;
2294 CPU_AP_DATA
*CpuData
;
2295 BOOLEAN HasEnabledAp
;
2298 CpuMpData
= GetCpuMpData ();
2300 if (FailedCpuList
!= NULL
) {
2301 *FailedCpuList
= NULL
;
2304 if (CpuMpData
->CpuCount
== 1 && ExcludeBsp
) {
2305 return EFI_NOT_STARTED
;
2308 if (Procedure
== NULL
) {
2309 return EFI_INVALID_PARAMETER
;
2313 // Check whether caller processor is BSP
2315 MpInitLibWhoAmI (&CallerNumber
);
2316 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2317 return EFI_DEVICE_ERROR
;
2323 CheckAndUpdateApsStatus ();
2325 ProcessorCount
= CpuMpData
->CpuCount
;
2326 HasEnabledAp
= FALSE
;
2328 // Check whether all enabled APs are idle.
2329 // If any enabled AP is not idle, return EFI_NOT_READY.
2331 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2332 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2333 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2334 ApState
= GetApState (CpuData
);
2335 if (ApState
!= CpuStateDisabled
) {
2336 HasEnabledAp
= TRUE
;
2337 if (ApState
!= CpuStateIdle
) {
2339 // If any enabled APs are busy, return EFI_NOT_READY.
2341 return EFI_NOT_READY
;
2347 if (!HasEnabledAp
&& ExcludeBsp
) {
2349 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2351 return EFI_NOT_STARTED
;
2354 CpuMpData
->RunningCount
= 0;
2355 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2356 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2357 CpuData
->Waiting
= FALSE
;
2358 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2359 if (CpuData
->State
== CpuStateIdle
) {
2361 // Mark this processor as responsible for current calling.
2363 CpuData
->Waiting
= TRUE
;
2364 CpuMpData
->RunningCount
++;
2369 CpuMpData
->Procedure
= Procedure
;
2370 CpuMpData
->ProcArguments
= ProcedureArgument
;
2371 CpuMpData
->SingleThread
= SingleThread
;
2372 CpuMpData
->FinishedCount
= 0;
2373 CpuMpData
->FailedCpuList
= FailedCpuList
;
2374 CpuMpData
->ExpectedTime
= CalculateTimeout (
2375 TimeoutInMicroseconds
,
2376 &CpuMpData
->CurrentTime
2378 CpuMpData
->TotalTime
= 0;
2379 CpuMpData
->WaitEvent
= WaitEvent
;
2381 if (!SingleThread
) {
2382 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2384 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2385 if (ProcessorNumber
== CallerNumber
) {
2388 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2389 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2399 Procedure (ProcedureArgument
);
2402 Status
= EFI_SUCCESS
;
2403 if (WaitEvent
== NULL
) {
2405 Status
= CheckAllAPs ();
2406 } while (Status
== EFI_NOT_READY
);
2413 Worker function to let the caller get one enabled AP to execute a caller-provided
2416 @param[in] Procedure A pointer to the function to be run on
2417 enabled APs of the system.
2418 @param[in] ProcessorNumber The handle number of the AP.
2419 @param[in] WaitEvent The event created by the caller with CreateEvent()
2421 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2422 APs to return from Procedure, either for
2423 blocking or non-blocking mode.
2424 @param[in] ProcedureArgument The parameter passed into Procedure for
2426 @param[out] Finished If AP returns from Procedure before the
2427 timeout expires, its content is set to TRUE.
2428 Otherwise, the value is set to FALSE.
2430 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2431 the timeout expires.
2432 @retval others Failed to Startup AP.
2436 StartupThisAPWorker (
2437 IN EFI_AP_PROCEDURE Procedure
,
2438 IN UINTN ProcessorNumber
,
2439 IN EFI_EVENT WaitEvent OPTIONAL
,
2440 IN UINTN TimeoutInMicroseconds
,
2441 IN VOID
*ProcedureArgument OPTIONAL
,
2442 OUT BOOLEAN
*Finished OPTIONAL
2446 CPU_MP_DATA
*CpuMpData
;
2447 CPU_AP_DATA
*CpuData
;
2450 CpuMpData
= GetCpuMpData ();
2452 if (Finished
!= NULL
) {
2457 // Check whether caller processor is BSP
2459 MpInitLibWhoAmI (&CallerNumber
);
2460 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2461 return EFI_DEVICE_ERROR
;
2465 // Check whether processor with the handle specified by ProcessorNumber exists
2467 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2468 return EFI_NOT_FOUND
;
2472 // Check whether specified processor is BSP
2474 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2475 return EFI_INVALID_PARAMETER
;
2479 // Check parameter Procedure
2481 if (Procedure
== NULL
) {
2482 return EFI_INVALID_PARAMETER
;
2488 CheckAndUpdateApsStatus ();
2491 // Check whether specified AP is disabled
2493 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2494 return EFI_INVALID_PARAMETER
;
2498 // If WaitEvent is not NULL, execute in non-blocking mode.
2499 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2500 // CheckAPsStatus() will check completion and timeout periodically.
2502 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2503 CpuData
->WaitEvent
= WaitEvent
;
2504 CpuData
->Finished
= Finished
;
2505 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2506 CpuData
->TotalTime
= 0;
2508 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2511 // If WaitEvent is NULL, execute in blocking mode.
2512 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2514 Status
= EFI_SUCCESS
;
2515 if (WaitEvent
== NULL
) {
2517 Status
= CheckThisAP (ProcessorNumber
);
2518 } while (Status
== EFI_NOT_READY
);
2525 Get pointer to CPU MP Data structure from GUIDed HOB.
2527 @return The pointer to CPU MP Data structure.
2530 GetCpuMpDataFromGuidedHob (
2534 EFI_HOB_GUID_TYPE
*GuidHob
;
2536 CPU_MP_DATA
*CpuMpData
;
2539 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2540 if (GuidHob
!= NULL
) {
2541 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2542 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2548 This service executes a caller provided function on all enabled CPUs.
2550 @param[in] Procedure A pointer to the function to be run on
2551 enabled APs of the system. See type
2553 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2554 APs to return from Procedure, either for
2555 blocking or non-blocking mode. Zero means
2556 infinity. TimeoutInMicroseconds is ignored
2558 @param[in] ProcedureArgument The parameter passed into Procedure for
2561 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2562 the timeout expired.
2563 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2564 to all enabled CPUs.
2565 @retval EFI_DEVICE_ERROR Caller processor is AP.
2566 @retval EFI_NOT_READY Any enabled APs are busy.
2567 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2568 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2569 all enabled APs have finished.
2570 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2575 MpInitLibStartupAllCPUs (
2576 IN EFI_AP_PROCEDURE Procedure
,
2577 IN UINTN TimeoutInMicroseconds
,
2578 IN VOID
*ProcedureArgument OPTIONAL
2581 return StartupAllCPUsWorker (
2586 TimeoutInMicroseconds
,