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 The function will check if BSP Execute Disable is enabled.
19 DxeIpl may have enabled Execute Disable for BSP, APs need to
20 get the status and sync up the settings.
21 If BSP's CR0.Paging is not set, BSP execute Disble feature is
24 @retval TRUE BSP Execute Disable is enabled.
25 @retval FALSE BSP Execute Disable is not enabled.
28 IsBspExecuteDisableEnabled (
33 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
34 MSR_IA32_EFER_REGISTER EferMsr
;
39 Cr0
.UintN
= AsmReadCr0 ();
40 if (Cr0
.Bits
.PG
!= 0) {
42 // If CR0 Paging bit is set
44 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
45 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
46 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
49 // Bit 20: Execute Disable Bit available.
51 if (Edx
.Bits
.NX
!= 0) {
52 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
55 // Bit 11: Execute Disable Bit enable.
57 if (EferMsr
.Bits
.NXE
!= 0) {
68 Worker function for SwitchBSP().
70 Worker function for SwitchBSP(), assigned to the AP which is intended
73 @param[in] Buffer Pointer to CPU MP Data
81 CPU_MP_DATA
*DataInHob
;
83 DataInHob
= (CPU_MP_DATA
*) Buffer
;
84 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
88 Get the Application Processors state.
90 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
96 IN CPU_AP_DATA
*CpuData
99 return CpuData
->State
;
103 Set the Application Processors state.
105 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
106 @param[in] State The AP status
110 IN CPU_AP_DATA
*CpuData
,
114 AcquireSpinLock (&CpuData
->ApLock
);
115 CpuData
->State
= State
;
116 ReleaseSpinLock (&CpuData
->ApLock
);
120 Save BSP's local APIC timer setting.
122 @param[in] CpuMpData Pointer to CPU MP Data
125 SaveLocalApicTimerSetting (
126 IN CPU_MP_DATA
*CpuMpData
130 // Record the current local APIC timer setting of BSP
133 &CpuMpData
->DivideValue
,
134 &CpuMpData
->PeriodicMode
,
137 CpuMpData
->CurrentTimerCount
= GetApicTimerCurrentCount ();
138 CpuMpData
->TimerInterruptState
= GetApicTimerInterruptState ();
142 Sync local APIC timer setting from BSP to AP.
144 @param[in] CpuMpData Pointer to CPU MP Data
147 SyncLocalApicTimerSetting (
148 IN CPU_MP_DATA
*CpuMpData
152 // Sync local APIC timer setting from BSP to AP
154 InitializeApicTimer (
155 CpuMpData
->DivideValue
,
156 CpuMpData
->CurrentTimerCount
,
157 CpuMpData
->PeriodicMode
,
161 // Disable AP's local APIC timer interrupt
163 DisableApicTimerInterrupt ();
167 Save the volatile registers required to be restored following INIT IPI.
169 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
172 SaveVolatileRegisters (
173 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
176 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
178 VolatileRegisters
->Cr0
= AsmReadCr0 ();
179 VolatileRegisters
->Cr3
= AsmReadCr3 ();
180 VolatileRegisters
->Cr4
= AsmReadCr4 ();
182 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
183 if (VersionInfoEdx
.Bits
.DE
!= 0) {
185 // If processor supports Debugging Extensions feature
186 // by CPUID.[EAX=01H]:EDX.BIT2
188 VolatileRegisters
->Dr0
= AsmReadDr0 ();
189 VolatileRegisters
->Dr1
= AsmReadDr1 ();
190 VolatileRegisters
->Dr2
= AsmReadDr2 ();
191 VolatileRegisters
->Dr3
= AsmReadDr3 ();
192 VolatileRegisters
->Dr6
= AsmReadDr6 ();
193 VolatileRegisters
->Dr7
= AsmReadDr7 ();
196 AsmReadGdtr (&VolatileRegisters
->Gdtr
);
197 AsmReadIdtr (&VolatileRegisters
->Idtr
);
198 VolatileRegisters
->Tr
= AsmReadTr ();
202 Restore the volatile registers following INIT IPI.
204 @param[in] VolatileRegisters Pointer to volatile resisters
205 @param[in] IsRestoreDr TRUE: Restore DRx if supported
206 FALSE: Do not restore DRx
209 RestoreVolatileRegisters (
210 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
211 IN BOOLEAN IsRestoreDr
214 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
215 IA32_TSS_DESCRIPTOR
*Tss
;
217 AsmWriteCr3 (VolatileRegisters
->Cr3
);
218 AsmWriteCr4 (VolatileRegisters
->Cr4
);
219 AsmWriteCr0 (VolatileRegisters
->Cr0
);
222 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
223 if (VersionInfoEdx
.Bits
.DE
!= 0) {
225 // If processor supports Debugging Extensions feature
226 // by CPUID.[EAX=01H]:EDX.BIT2
228 AsmWriteDr0 (VolatileRegisters
->Dr0
);
229 AsmWriteDr1 (VolatileRegisters
->Dr1
);
230 AsmWriteDr2 (VolatileRegisters
->Dr2
);
231 AsmWriteDr3 (VolatileRegisters
->Dr3
);
232 AsmWriteDr6 (VolatileRegisters
->Dr6
);
233 AsmWriteDr7 (VolatileRegisters
->Dr7
);
237 AsmWriteGdtr (&VolatileRegisters
->Gdtr
);
238 AsmWriteIdtr (&VolatileRegisters
->Idtr
);
239 if (VolatileRegisters
->Tr
!= 0 &&
240 VolatileRegisters
->Tr
< VolatileRegisters
->Gdtr
.Limit
) {
241 Tss
= (IA32_TSS_DESCRIPTOR
*)(VolatileRegisters
->Gdtr
.Base
+
242 VolatileRegisters
->Tr
);
243 if (Tss
->Bits
.P
== 1) {
244 Tss
->Bits
.Type
&= 0xD; // 1101 - Clear busy bit just in case
245 AsmWriteTr (VolatileRegisters
->Tr
);
251 Detect whether Mwait-monitor feature is supported.
253 @retval TRUE Mwait-monitor feature is supported.
254 @retval FALSE Mwait-monitor feature is not supported.
261 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
263 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
264 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
270 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
272 @return The AP loop mode.
276 OUT UINT32
*MonitorFilterSize
280 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
282 ASSERT (MonitorFilterSize
!= NULL
);
284 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
285 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
286 if (ApLoopMode
== ApInMwaitLoop
) {
287 if (!IsMwaitSupport ()) {
289 // If processor does not support MONITOR/MWAIT feature,
290 // force AP in Hlt-loop mode
292 ApLoopMode
= ApInHltLoop
;
296 if (ApLoopMode
!= ApInMwaitLoop
) {
297 *MonitorFilterSize
= sizeof (UINT32
);
300 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
301 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
303 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
304 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
311 Sort the APIC ID of all processors.
313 This function sorts the APIC ID of all processors so that processor number is
314 assigned in the ascending order of APIC ID which eases MP debugging.
316 @param[in] CpuMpData Pointer to PEI CPU MP Data
320 IN CPU_MP_DATA
*CpuMpData
327 CPU_INFO_IN_HOB CpuInfo
;
329 CPU_INFO_IN_HOB
*CpuInfoInHob
;
330 volatile UINT32
*StartupApSignal
;
332 ApCount
= CpuMpData
->CpuCount
- 1;
333 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
335 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
338 // Sort key is the hardware default APIC ID
340 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
341 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
342 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
344 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
347 if (Index3
!= Index1
) {
348 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
350 &CpuInfoInHob
[Index3
],
351 &CpuInfoInHob
[Index1
],
352 sizeof (CPU_INFO_IN_HOB
)
354 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
357 // Also exchange the StartupApSignal.
359 StartupApSignal
= CpuMpData
->CpuData
[Index3
].StartupApSignal
;
360 CpuMpData
->CpuData
[Index3
].StartupApSignal
=
361 CpuMpData
->CpuData
[Index1
].StartupApSignal
;
362 CpuMpData
->CpuData
[Index1
].StartupApSignal
= StartupApSignal
;
367 // Get the processor number for the BSP
369 ApicId
= GetInitialApicId ();
370 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
371 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
372 CpuMpData
->BspNumber
= (UINT32
) Index1
;
380 Enable x2APIC mode on APs.
382 @param[in, out] Buffer Pointer to private data buffer.
390 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
396 @param[in, out] Buffer Pointer to private data buffer.
404 CPU_MP_DATA
*CpuMpData
;
405 UINTN ProcessorNumber
;
408 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
409 Status
= GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
410 ASSERT_EFI_ERROR (Status
);
412 // Load microcode on AP
414 MicrocodeDetect (CpuMpData
, ProcessorNumber
);
416 // Sync BSP's MTRR table to AP
418 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
422 Find the current Processor number by APIC ID.
424 @param[in] CpuMpData Pointer to PEI CPU MP Data
425 @param[out] ProcessorNumber Return the pocessor number found
427 @retval EFI_SUCCESS ProcessorNumber is found and returned.
428 @retval EFI_NOT_FOUND ProcessorNumber is not found.
432 IN CPU_MP_DATA
*CpuMpData
,
433 OUT UINTN
*ProcessorNumber
436 UINTN TotalProcessorNumber
;
438 CPU_INFO_IN_HOB
*CpuInfoInHob
;
439 UINT32 CurrentApicId
;
441 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
443 TotalProcessorNumber
= CpuMpData
->CpuCount
;
444 CurrentApicId
= GetApicId ();
445 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
446 if (CpuInfoInHob
[Index
].ApicId
== CurrentApicId
) {
447 *ProcessorNumber
= Index
;
452 return EFI_NOT_FOUND
;
456 This function will get CPU count in the system.
458 @param[in] CpuMpData Pointer to PEI CPU MP Data
460 @return CPU count detected
463 CollectProcessorCount (
464 IN CPU_MP_DATA
*CpuMpData
468 CPU_INFO_IN_HOB
*CpuInfoInHob
;
472 // Send 1st broadcast IPI to APs to wakeup APs
474 CpuMpData
->InitFlag
= ApInitConfig
;
475 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
, TRUE
);
476 CpuMpData
->InitFlag
= ApInitDone
;
477 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
479 // Wait for all APs finished the initialization
481 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
487 // Enable x2APIC mode if
488 // 1. Number of CPU is greater than 255; or
489 // 2. There are any logical processors reporting an Initial APIC ID of 255 or greater.
492 if (CpuMpData
->CpuCount
> 255) {
494 // If there are more than 255 processor found, force to enable X2APIC
498 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
499 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
500 if (CpuInfoInHob
[Index
].InitialApicId
>= 0xFF) {
508 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
510 // Wakeup all APs to enable x2APIC mode
512 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
, TRUE
);
514 // Wait for all known APs finished
516 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
520 // Enable x2APIC on BSP
522 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
524 // Set BSP/Aps state to IDLE
526 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
527 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
530 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
532 // Sort BSP/Aps by CPU APIC ID in ascending order
534 SortApicId (CpuMpData
);
536 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
538 return CpuMpData
->CpuCount
;
542 Initialize CPU AP Data when AP is wakeup at the first time.
544 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
545 @param[in] ProcessorNumber The handle number of processor
546 @param[in] BistData Processor BIST data
547 @param[in] ApTopOfStack Top of AP stack
552 IN OUT CPU_MP_DATA
*CpuMpData
,
553 IN UINTN ProcessorNumber
,
555 IN UINT64 ApTopOfStack
558 CPU_INFO_IN_HOB
*CpuInfoInHob
;
559 MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr
;
561 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
562 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
563 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
564 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
565 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
567 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
568 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
571 // NOTE: PlatformId is not relevant on AMD platforms.
573 if (!StandardSignatureIsAuthenticAMD ()) {
574 PlatformIdMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_PLATFORM_ID
);
575 CpuMpData
->CpuData
[ProcessorNumber
].PlatformId
= (UINT8
)PlatformIdMsr
.Bits
.PlatformId
;
580 &CpuMpData
->CpuData
[ProcessorNumber
].ProcessorSignature
,
586 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
587 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
591 This function will be called from AP reset code if BSP uses WakeUpAP.
593 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
594 @param[in] ApIndex Number of current executing AP
599 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
603 CPU_MP_DATA
*CpuMpData
;
604 UINTN ProcessorNumber
;
605 EFI_AP_PROCEDURE Procedure
;
608 volatile UINT32
*ApStartupSignalBuffer
;
609 CPU_INFO_IN_HOB
*CpuInfoInHob
;
611 UINTN CurrentApicMode
;
614 // AP finished assembly code and begin to execute C code
616 CpuMpData
= ExchangeInfo
->CpuMpData
;
619 // AP's local APIC settings will be lost after received INIT IPI
620 // We need to re-initialize them at here
622 ProgramVirtualWireMode ();
624 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
626 DisableLvtInterrupts ();
627 SyncLocalApicTimerSetting (CpuMpData
);
629 CurrentApicMode
= GetApicMode ();
631 if (CpuMpData
->InitFlag
== ApInitConfig
) {
635 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
636 ProcessorNumber
= ApIndex
;
638 // This is first time AP wakeup, get BIST information from AP stack
640 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
641 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
643 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
644 // to initialize AP in InitConfig path.
645 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
647 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
648 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
649 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
651 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
654 // Execute AP function if AP is ready
656 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
658 // Clear AP start-up signal when AP waken up
660 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
661 InterlockedCompareExchange32 (
662 (UINT32
*) ApStartupSignalBuffer
,
667 if (CpuMpData
->InitFlag
== ApInitReconfig
) {
669 // ApInitReconfig happens when:
670 // 1. AP is re-enabled after it's disabled, in either PEI or DXE phase.
671 // 2. AP is initialized in DXE phase.
672 // In either case, use the volatile registers value derived from BSP.
673 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a
674 // different IDT shared by all APs.
676 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
678 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
680 // Restore AP's volatile registers saved before AP is halted
682 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
685 // The CPU driver might not flush TLB for APs on spot after updating
686 // page attributes. AP in mwait loop mode needs to take care of it when
693 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
694 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
695 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
696 if (Procedure
!= NULL
) {
697 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
699 // Enable source debugging on AP function
703 // Invoke AP function here
705 Procedure (Parameter
);
706 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
707 if (CpuMpData
->SwitchBspFlag
) {
709 // Re-get the processor number due to BSP/AP maybe exchange in AP function
711 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
712 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
713 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
714 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
715 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
717 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
718 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
719 if (CurrentApicMode
!= GetApicMode ()) {
721 // If APIC mode change happened during AP function execution,
722 // we do not support APIC ID value changed.
728 // Re-get the CPU APICID and Initial APICID if they are changed
730 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
731 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
736 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
741 // AP finished executing C code
743 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
746 // Place AP is specified loop mode
748 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
750 // Save AP volatile registers
752 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
754 // Place AP in HLT-loop
757 DisableInterrupts ();
763 DisableInterrupts ();
764 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
766 // Place AP in MWAIT-loop
768 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
769 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
771 // Check AP start-up signal again.
772 // If AP start-up signal is not set, place AP into
773 // the specified C-state
775 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
777 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
779 // Place AP in Run-loop
787 // If AP start-up signal is written, AP is waken up
788 // otherwise place AP in loop again
790 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
798 Wait for AP wakeup and write AP start-up signal till AP is waken up.
800 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
804 IN
volatile UINT32
*ApStartupSignalBuffer
808 // If AP is waken up, StartupApSignal should be cleared.
809 // Otherwise, write StartupApSignal again till AP waken up.
811 while (InterlockedCompareExchange32 (
812 (UINT32
*) ApStartupSignalBuffer
,
821 This function will fill the exchange info structure.
823 @param[in] CpuMpData Pointer to CPU MP Data
827 FillExchangeInfoData (
828 IN CPU_MP_DATA
*CpuMpData
831 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
833 IA32_SEGMENT_DESCRIPTOR
*Selector
;
836 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
837 ExchangeInfo
->Lock
= 0;
838 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
839 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
840 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
841 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
843 ExchangeInfo
->CodeSegment
= AsmReadCs ();
844 ExchangeInfo
->DataSegment
= AsmReadDs ();
846 ExchangeInfo
->Cr3
= AsmReadCr3 ();
848 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
849 ExchangeInfo
->ApIndex
= 0;
850 ExchangeInfo
->NumApsExecuting
= 0;
851 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
852 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
853 ExchangeInfo
->CpuMpData
= CpuMpData
;
855 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
857 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
860 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
861 // to determin whether 5-Level Paging is enabled.
862 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
863 // current system setting.
864 // Using latter way is simpler because it also eliminates the needs to
865 // check whether platform wants to enable it.
867 Cr4
.UintN
= AsmReadCr4 ();
868 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
) (Cr4
.Bits
.LA57
== 1);
869 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
872 // Get the BSP's data of GDT and IDT
874 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
875 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
878 // Find a 32-bit code segment
880 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
881 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
883 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
884 ExchangeInfo
->ModeTransitionSegment
=
885 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
889 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
893 // Copy all 32-bit code and 64-bit code into memory with type of
894 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
896 if (CpuMpData
->WakeupBufferHigh
!= 0) {
897 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
898 CpuMpData
->AddressMap
.ModeTransitionOffset
;
900 (VOID
*)CpuMpData
->WakeupBufferHigh
,
901 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
902 CpuMpData
->AddressMap
.ModeTransitionOffset
,
906 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
908 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
909 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
912 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
913 (UINT32
)ExchangeInfo
->ModeOffset
-
914 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
915 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
919 Helper function that waits until the finished AP count reaches the specified
920 limit, or the specified timeout elapses (whichever comes first).
922 @param[in] CpuMpData Pointer to CPU MP Data.
923 @param[in] FinishedApLimit The number of finished APs to wait for.
924 @param[in] TimeLimit The number of microseconds to wait for.
927 TimedWaitForApFinish (
928 IN CPU_MP_DATA
*CpuMpData
,
929 IN UINT32 FinishedApLimit
,
934 Get available system memory below 1MB by specified size.
936 @param[in] CpuMpData The pointer to CPU MP Data structure.
939 BackupAndPrepareWakeupBuffer(
940 IN CPU_MP_DATA
*CpuMpData
944 (VOID
*) CpuMpData
->BackupBuffer
,
945 (VOID
*) CpuMpData
->WakeupBuffer
,
946 CpuMpData
->BackupBufferSize
949 (VOID
*) CpuMpData
->WakeupBuffer
,
950 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
951 CpuMpData
->AddressMap
.RendezvousFunnelSize
956 Restore wakeup buffer data.
958 @param[in] CpuMpData The pointer to CPU MP Data structure.
962 IN CPU_MP_DATA
*CpuMpData
966 (VOID
*) CpuMpData
->WakeupBuffer
,
967 (VOID
*) CpuMpData
->BackupBuffer
,
968 CpuMpData
->BackupBufferSize
973 Allocate reset vector buffer.
975 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
978 AllocateResetVector (
979 IN OUT CPU_MP_DATA
*CpuMpData
982 UINTN ApResetVectorSize
;
984 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
985 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
986 sizeof (MP_CPU_EXCHANGE_INFO
);
988 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
989 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
990 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
991 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
992 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
993 CpuMpData
->AddressMap
.ModeTransitionOffset
996 BackupAndPrepareWakeupBuffer (CpuMpData
);
1000 Free AP reset vector buffer.
1002 @param[in] CpuMpData The pointer to CPU MP Data structure.
1006 IN CPU_MP_DATA
*CpuMpData
1009 RestoreWakeupBuffer (CpuMpData
);
1013 This function will be called by BSP to wakeup AP.
1015 @param[in] CpuMpData Pointer to CPU MP Data
1016 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
1017 FALSE: Send IPI to AP by ApicId
1018 @param[in] ProcessorNumber The handle number of specified processor
1019 @param[in] Procedure The function to be invoked by AP
1020 @param[in] ProcedureArgument The argument to be passed into AP function
1021 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1025 IN CPU_MP_DATA
*CpuMpData
,
1026 IN BOOLEAN Broadcast
,
1027 IN UINTN ProcessorNumber
,
1028 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
1029 IN VOID
*ProcedureArgument
, OPTIONAL
1030 IN BOOLEAN WakeUpDisabledAps
1033 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
1035 CPU_AP_DATA
*CpuData
;
1036 BOOLEAN ResetVectorRequired
;
1037 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1039 CpuMpData
->FinishedCount
= 0;
1040 ResetVectorRequired
= FALSE
;
1042 if (CpuMpData
->WakeUpByInitSipiSipi
||
1043 CpuMpData
->InitFlag
!= ApInitDone
) {
1044 ResetVectorRequired
= TRUE
;
1045 AllocateResetVector (CpuMpData
);
1046 FillExchangeInfoData (CpuMpData
);
1047 SaveLocalApicTimerSetting (CpuMpData
);
1050 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1052 // Get AP target C-state each time when waking up AP,
1053 // for it maybe updated by platform again
1055 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1058 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1061 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1062 if (Index
!= CpuMpData
->BspNumber
) {
1063 CpuData
= &CpuMpData
->CpuData
[Index
];
1065 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1066 // the AP procedure will be skipped for disabled AP because AP state
1067 // is not CpuStateReady.
1069 if (GetApState (CpuData
) == CpuStateDisabled
&& !WakeUpDisabledAps
) {
1073 CpuData
->ApFunction
= (UINTN
) Procedure
;
1074 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1075 SetApState (CpuData
, CpuStateReady
);
1076 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1077 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1081 if (ResetVectorRequired
) {
1085 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1087 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1088 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber
) > 0) {
1090 // The AP enumeration algorithm below is suitable only when the
1091 // platform can tell us the *exact* boot CPU count in advance.
1093 // The wait below finishes only when the detected AP count reaches
1094 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1095 // takes. If at least one AP fails to check in (meaning a platform
1096 // hardware bug), the detection hangs forever, by design. If the actual
1097 // boot CPU count in the system is higher than
1098 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1099 // misconfiguration), then some APs may complete initialization after
1100 // the wait finishes, and cause undefined behavior.
1102 TimedWaitForApFinish (
1104 PcdGet32 (PcdCpuBootLogicalProcessorNumber
) - 1,
1105 MAX_UINT32
// approx. 71 minutes
1109 // The AP enumeration algorithm below is suitable for two use cases.
1111 // (1) The check-in time for an individual AP is bounded, and APs run
1112 // through their initialization routines strongly concurrently. In
1113 // particular, the number of concurrently running APs
1114 // ("NumApsExecuting") is never expected to fall to zero
1115 // *temporarily* -- it is expected to fall to zero only when all
1116 // APs have checked-in.
1118 // In this case, the platform is supposed to set
1119 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1120 // enough for one AP to start initialization). The timeout will be
1121 // reached soon, and remaining APs are collected by watching
1122 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1123 // mid-process, while some APs have not completed initialization,
1124 // the behavior is undefined.
1126 // (2) The check-in time for an individual AP is unbounded, and/or APs
1127 // may complete their initializations widely spread out. In
1128 // particular, some APs may finish initialization before some APs
1131 // In this case, the platform is supposed to set
1132 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1133 // enumeration will always take that long (except when the boot CPU
1134 // count happens to be maximal, that is,
1135 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1136 // check-in before the timeout, and NumApsExecuting is assumed zero
1137 // at timeout. APs that miss the time-out may cause undefined
1140 TimedWaitForApFinish (
1142 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1143 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1146 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1152 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1154 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1155 CpuData
= &CpuMpData
->CpuData
[Index
];
1156 if (Index
!= CpuMpData
->BspNumber
) {
1157 WaitApWakeup (CpuData
->StartupApSignal
);
1162 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1163 CpuData
->ApFunction
= (UINTN
) Procedure
;
1164 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1165 SetApState (CpuData
, CpuStateReady
);
1167 // Wakeup specified AP
1169 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1170 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1171 if (ResetVectorRequired
) {
1172 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1174 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1175 (UINT32
) ExchangeInfo
->BufferStart
1179 // Wait specified AP waken up
1181 WaitApWakeup (CpuData
->StartupApSignal
);
1184 if (ResetVectorRequired
) {
1185 FreeResetVector (CpuMpData
);
1189 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1190 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1191 // S3SmmInitDone Ppi.
1193 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1197 Calculate timeout value and return the current performance counter value.
1199 Calculate the number of performance counter ticks required for a timeout.
1200 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1203 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1204 @param[out] CurrentTime Returns the current value of the performance counter.
1206 @return Expected time stamp counter for timeout.
1207 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1213 IN UINTN TimeoutInMicroseconds
,
1214 OUT UINT64
*CurrentTime
1217 UINT64 TimeoutInSeconds
;
1218 UINT64 TimestampCounterFreq
;
1221 // Read the current value of the performance counter
1223 *CurrentTime
= GetPerformanceCounter ();
1226 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1229 if (TimeoutInMicroseconds
== 0) {
1234 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1237 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1240 // Check the potential overflow before calculate the number of ticks for the timeout value.
1242 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1244 // Convert microseconds into seconds if direct multiplication overflows
1246 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1248 // Assertion if the final tick count exceeds MAX_UINT64
1250 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1251 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1254 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1255 // it by 1,000,000, to get the number of ticks for the timeout value.
1259 TimestampCounterFreq
,
1260 TimeoutInMicroseconds
1268 Checks whether timeout expires.
1270 Check whether the number of elapsed performance counter ticks required for
1271 a timeout condition has been reached.
1272 If Timeout is zero, which means infinity, return value is always FALSE.
1274 @param[in, out] PreviousTime On input, the value of the performance counter
1275 when it was last read.
1276 On output, the current value of the performance
1278 @param[in] TotalTime The total amount of elapsed time in performance
1280 @param[in] Timeout The number of performance counter ticks required
1281 to reach a timeout condition.
1283 @retval TRUE A timeout condition has been reached.
1284 @retval FALSE A timeout condition has not been reached.
1289 IN OUT UINT64
*PreviousTime
,
1290 IN UINT64
*TotalTime
,
1303 GetPerformanceCounterProperties (&Start
, &End
);
1304 Cycle
= End
- Start
;
1309 CurrentTime
= GetPerformanceCounter();
1310 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1317 *TotalTime
+= Delta
;
1318 *PreviousTime
= CurrentTime
;
1319 if (*TotalTime
> Timeout
) {
1326 Helper function that waits until the finished AP count reaches the specified
1327 limit, or the specified timeout elapses (whichever comes first).
1329 @param[in] CpuMpData Pointer to CPU MP Data.
1330 @param[in] FinishedApLimit The number of finished APs to wait for.
1331 @param[in] TimeLimit The number of microseconds to wait for.
1334 TimedWaitForApFinish (
1335 IN CPU_MP_DATA
*CpuMpData
,
1336 IN UINT32 FinishedApLimit
,
1341 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1342 // "infinity", so check for (TimeLimit == 0) explicitly.
1344 if (TimeLimit
== 0) {
1348 CpuMpData
->TotalTime
= 0;
1349 CpuMpData
->ExpectedTime
= CalculateTimeout (
1351 &CpuMpData
->CurrentTime
1353 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1355 &CpuMpData
->CurrentTime
,
1356 &CpuMpData
->TotalTime
,
1357 CpuMpData
->ExpectedTime
1362 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1365 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1368 DivU64x64Remainder (
1369 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1370 GetPerformanceCounterProperties (NULL
, NULL
),
1378 Reset an AP to Idle state.
1380 Any task being executed by the AP will be aborted and the AP
1381 will be waiting for a new task in Wait-For-SIPI state.
1383 @param[in] ProcessorNumber The handle number of processor.
1386 ResetProcessorToIdleState (
1387 IN UINTN ProcessorNumber
1390 CPU_MP_DATA
*CpuMpData
;
1392 CpuMpData
= GetCpuMpData ();
1394 CpuMpData
->InitFlag
= ApInitReconfig
;
1395 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1396 while (CpuMpData
->FinishedCount
< 1) {
1399 CpuMpData
->InitFlag
= ApInitDone
;
1401 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1405 Searches for the next waiting AP.
1407 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1409 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1411 @retval EFI_SUCCESS The next waiting AP has been found.
1412 @retval EFI_NOT_FOUND No waiting AP exists.
1416 GetNextWaitingProcessorNumber (
1417 OUT UINTN
*NextProcessorNumber
1420 UINTN ProcessorNumber
;
1421 CPU_MP_DATA
*CpuMpData
;
1423 CpuMpData
= GetCpuMpData ();
1425 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1426 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1427 *NextProcessorNumber
= ProcessorNumber
;
1432 return EFI_NOT_FOUND
;
1435 /** Checks status of specified AP.
1437 This function checks whether the specified AP has finished the task assigned
1438 by StartupThisAP(), and whether timeout expires.
1440 @param[in] ProcessorNumber The handle number of processor.
1442 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1443 @retval EFI_TIMEOUT The timeout expires.
1444 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1448 IN UINTN ProcessorNumber
1451 CPU_MP_DATA
*CpuMpData
;
1452 CPU_AP_DATA
*CpuData
;
1454 CpuMpData
= GetCpuMpData ();
1455 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1458 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1459 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1460 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1463 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1465 if (GetApState(CpuData
) == CpuStateFinished
) {
1466 if (CpuData
->Finished
!= NULL
) {
1467 *(CpuData
->Finished
) = TRUE
;
1469 SetApState (CpuData
, CpuStateIdle
);
1473 // If timeout expires for StartupThisAP(), report timeout.
1475 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1476 if (CpuData
->Finished
!= NULL
) {
1477 *(CpuData
->Finished
) = FALSE
;
1480 // Reset failed AP to idle state
1482 ResetProcessorToIdleState (ProcessorNumber
);
1487 return EFI_NOT_READY
;
1491 Checks status of all APs.
1493 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1494 and whether timeout expires.
1496 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1497 @retval EFI_TIMEOUT The timeout expires.
1498 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1505 UINTN ProcessorNumber
;
1506 UINTN NextProcessorNumber
;
1509 CPU_MP_DATA
*CpuMpData
;
1510 CPU_AP_DATA
*CpuData
;
1512 CpuMpData
= GetCpuMpData ();
1514 NextProcessorNumber
= 0;
1517 // Go through all APs that are responsible for the StartupAllAPs().
1519 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1520 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1524 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1526 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1527 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1528 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1530 if (GetApState(CpuData
) == CpuStateFinished
) {
1531 CpuMpData
->RunningCount
--;
1532 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1533 SetApState(CpuData
, CpuStateIdle
);
1536 // If in Single Thread mode, then search for the next waiting AP for execution.
1538 if (CpuMpData
->SingleThread
) {
1539 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1541 if (!EFI_ERROR (Status
)) {
1545 (UINT32
) NextProcessorNumber
,
1546 CpuMpData
->Procedure
,
1547 CpuMpData
->ProcArguments
,
1556 // If all APs finish, return EFI_SUCCESS.
1558 if (CpuMpData
->RunningCount
== 0) {
1563 // If timeout expires, report timeout.
1566 &CpuMpData
->CurrentTime
,
1567 &CpuMpData
->TotalTime
,
1568 CpuMpData
->ExpectedTime
)
1571 // If FailedCpuList is not NULL, record all failed APs in it.
1573 if (CpuMpData
->FailedCpuList
!= NULL
) {
1574 *CpuMpData
->FailedCpuList
=
1575 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1576 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1580 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1582 // Check whether this processor is responsible for StartupAllAPs().
1584 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1586 // Reset failed APs to idle state
1588 ResetProcessorToIdleState (ProcessorNumber
);
1589 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1590 if (CpuMpData
->FailedCpuList
!= NULL
) {
1591 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1595 if (CpuMpData
->FailedCpuList
!= NULL
) {
1596 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1600 return EFI_NOT_READY
;
1604 MP Initialize Library initialization.
1606 This service will allocate AP reset vector and wakeup all APs to do APs
1609 This service must be invoked before all other MP Initialize Library
1610 service are invoked.
1612 @retval EFI_SUCCESS MP initialization succeeds.
1613 @retval Others MP initialization fails.
1618 MpInitLibInitialize (
1622 CPU_MP_DATA
*OldCpuMpData
;
1623 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1624 UINT32 MaxLogicalProcessorNumber
;
1626 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1627 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1629 UINT32 MonitorFilterSize
;
1632 CPU_MP_DATA
*CpuMpData
;
1634 UINT8
*MonitorBuffer
;
1636 UINTN ApResetVectorSize
;
1637 UINTN BackupBufferAddr
;
1640 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1641 if (OldCpuMpData
== NULL
) {
1642 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1644 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1646 ASSERT (MaxLogicalProcessorNumber
!= 0);
1648 AsmGetAddressMap (&AddressMap
);
1649 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1650 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1651 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1654 // Save BSP's Control registers for APs.
1656 SaveVolatileRegisters (&VolatileRegisters
);
1658 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1659 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1660 BufferSize
+= ApResetVectorSize
;
1661 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1662 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1663 BufferSize
+= sizeof (CPU_MP_DATA
);
1664 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1665 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1666 ASSERT (MpBuffer
!= NULL
);
1667 ZeroMem (MpBuffer
, BufferSize
);
1668 Buffer
= (UINTN
) MpBuffer
;
1671 // The layout of the Buffer is as below:
1673 // +--------------------+ <-- Buffer
1675 // +--------------------+ <-- MonitorBuffer
1676 // AP Monitor Filters (N)
1677 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1679 // +--------------------+
1681 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1682 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1683 // +--------------------+ <-- CpuMpData
1685 // +--------------------+ <-- CpuMpData->CpuData
1687 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1688 // CPU_INFO_IN_HOB (N)
1689 // +--------------------+
1691 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1692 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1693 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1694 CpuMpData
= (CPU_MP_DATA
*) (ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1695 CpuMpData
->Buffer
= Buffer
;
1696 CpuMpData
->CpuApStackSize
= ApStackSize
;
1697 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1698 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1699 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1700 CpuMpData
->CpuCount
= 1;
1701 CpuMpData
->BspNumber
= 0;
1702 CpuMpData
->WaitEvent
= NULL
;
1703 CpuMpData
->SwitchBspFlag
= FALSE
;
1704 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1705 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1706 InitializeSpinLock(&CpuMpData
->MpLock
);
1709 // Make sure no memory usage outside of the allocated buffer.
1711 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1712 Buffer
+ BufferSize
);
1715 // Duplicate BSP's IDT to APs.
1716 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1718 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1719 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1721 // Don't pass BSP's TR to APs to avoid AP init failure.
1723 VolatileRegisters
.Tr
= 0;
1724 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1726 // Set BSP basic information
1728 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1730 // Save assembly code information
1732 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1734 // Finally set AP loop mode
1736 CpuMpData
->ApLoopMode
= ApLoopMode
;
1737 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1739 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1742 // Set up APs wakeup signal buffer
1744 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1745 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1746 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1749 // Enable the local APIC for Virtual Wire Mode.
1751 ProgramVirtualWireMode ();
1753 if (OldCpuMpData
== NULL
) {
1754 if (MaxLogicalProcessorNumber
> 1) {
1756 // Wakeup all APs and calculate the processor count in system
1758 CollectProcessorCount (CpuMpData
);
1762 // APs have been wakeup before, just get the CPU Information
1765 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1766 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1767 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1768 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1769 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1770 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1771 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1772 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1776 if (!GetMicrocodePatchInfoFromHob (
1777 &CpuMpData
->MicrocodePatchAddress
,
1778 &CpuMpData
->MicrocodePatchRegionSize
1781 // The microcode patch information cache HOB does not exist, which means
1782 // the microcode patches data has not been loaded into memory yet
1784 ShadowMicrocodeUpdatePatch (CpuMpData
);
1788 // Detect and apply Microcode on BSP
1790 MicrocodeDetect (CpuMpData
, CpuMpData
->BspNumber
);
1792 // Store BSP's MTRR setting
1794 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1797 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1799 if (CpuMpData
->CpuCount
> 1) {
1800 if (OldCpuMpData
!= NULL
) {
1802 // Only needs to use this flag for DXE phase to update the wake up
1803 // buffer. Wakeup buffer allocated in PEI phase is no longer valid
1806 CpuMpData
->InitFlag
= ApInitReconfig
;
1808 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1810 // Wait for all APs finished initialization
1812 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1815 if (OldCpuMpData
!= NULL
) {
1816 CpuMpData
->InitFlag
= ApInitDone
;
1818 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1819 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1824 // Initialize global data for MP support
1826 InitMpGlobalData (CpuMpData
);
1832 Gets detailed MP-related information on the requested processor at the
1833 instant this call is made. This service may only be called from the BSP.
1835 @param[in] ProcessorNumber The handle number of processor.
1836 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1837 the requested processor is deposited.
1838 @param[out] HealthData Return processor health data.
1840 @retval EFI_SUCCESS Processor information was returned.
1841 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1842 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1843 @retval EFI_NOT_FOUND The processor with the handle specified by
1844 ProcessorNumber does not exist in the platform.
1845 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1850 MpInitLibGetProcessorInfo (
1851 IN UINTN ProcessorNumber
,
1852 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1853 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1856 CPU_MP_DATA
*CpuMpData
;
1858 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1859 UINTN OriginalProcessorNumber
;
1861 CpuMpData
= GetCpuMpData ();
1862 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1865 // Lower 24 bits contains the actual processor number.
1867 OriginalProcessorNumber
= ProcessorNumber
;
1868 ProcessorNumber
&= BIT24
- 1;
1871 // Check whether caller processor is BSP
1873 MpInitLibWhoAmI (&CallerNumber
);
1874 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1875 return EFI_DEVICE_ERROR
;
1878 if (ProcessorInfoBuffer
== NULL
) {
1879 return EFI_INVALID_PARAMETER
;
1882 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1883 return EFI_NOT_FOUND
;
1886 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1887 ProcessorInfoBuffer
->StatusFlag
= 0;
1888 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1889 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1891 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1892 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1894 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1895 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1897 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1901 // Get processor location information
1903 GetProcessorLocationByApicId (
1904 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1905 &ProcessorInfoBuffer
->Location
.Package
,
1906 &ProcessorInfoBuffer
->Location
.Core
,
1907 &ProcessorInfoBuffer
->Location
.Thread
1910 if ((OriginalProcessorNumber
& CPU_V2_EXTENDED_TOPOLOGY
) != 0) {
1911 GetProcessorLocation2ByApicId (
1912 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1913 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Package
,
1914 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Die
,
1915 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Tile
,
1916 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Module
,
1917 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Core
,
1918 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Thread
1922 if (HealthData
!= NULL
) {
1923 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1930 Worker function to switch the requested AP to be the BSP from that point onward.
1932 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1933 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1934 enabled AP. Otherwise, it will be disabled.
1936 @retval EFI_SUCCESS BSP successfully switched.
1937 @retval others Failed to switch BSP.
1942 IN UINTN ProcessorNumber
,
1943 IN BOOLEAN EnableOldBSP
1946 CPU_MP_DATA
*CpuMpData
;
1949 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1950 BOOLEAN OldInterruptState
;
1951 BOOLEAN OldTimerInterruptState
;
1954 // Save and Disable Local APIC timer interrupt
1956 OldTimerInterruptState
= GetApicTimerInterruptState ();
1957 DisableApicTimerInterrupt ();
1959 // Before send both BSP and AP to a procedure to exchange their roles,
1960 // interrupt must be disabled. This is because during the exchange role
1961 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1962 // be corrupted, since interrupt return address will be pushed to stack
1965 OldInterruptState
= SaveAndDisableInterrupts ();
1968 // Mask LINT0 & LINT1 for the old BSP
1970 DisableLvtInterrupts ();
1972 CpuMpData
= GetCpuMpData ();
1975 // Check whether caller processor is BSP
1977 MpInitLibWhoAmI (&CallerNumber
);
1978 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1979 return EFI_DEVICE_ERROR
;
1982 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1983 return EFI_NOT_FOUND
;
1987 // Check whether specified AP is disabled
1989 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1990 if (State
== CpuStateDisabled
) {
1991 return EFI_INVALID_PARAMETER
;
1995 // Check whether ProcessorNumber specifies the current BSP
1997 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1998 return EFI_INVALID_PARAMETER
;
2002 // Check whether specified AP is busy
2004 if (State
== CpuStateBusy
) {
2005 return EFI_NOT_READY
;
2008 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2009 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2010 CpuMpData
->SwitchBspFlag
= TRUE
;
2011 CpuMpData
->NewBspNumber
= ProcessorNumber
;
2014 // Clear the BSP bit of MSR_IA32_APIC_BASE
2016 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2017 ApicBaseMsr
.Bits
.BSP
= 0;
2018 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2021 // Need to wakeUp AP (future BSP).
2023 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
2025 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
2028 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2030 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2031 ApicBaseMsr
.Bits
.BSP
= 1;
2032 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2033 ProgramVirtualWireMode ();
2036 // Wait for old BSP finished AP task
2038 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
2042 CpuMpData
->SwitchBspFlag
= FALSE
;
2044 // Set old BSP enable state
2046 if (!EnableOldBSP
) {
2047 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
2049 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2052 // Save new BSP number
2054 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
2057 // Restore interrupt state.
2059 SetInterruptState (OldInterruptState
);
2061 if (OldTimerInterruptState
) {
2062 EnableApicTimerInterrupt ();
2069 Worker function to let the caller enable or disable an AP from this point onward.
2070 This service may only be called from the BSP.
2072 @param[in] ProcessorNumber The handle number of AP.
2073 @param[in] EnableAP Specifies the new state for the processor for
2074 enabled, FALSE for disabled.
2075 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2076 the new health status of the AP.
2078 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2079 @retval others Failed to Enable/Disable AP.
2083 EnableDisableApWorker (
2084 IN UINTN ProcessorNumber
,
2085 IN BOOLEAN EnableAP
,
2086 IN UINT32
*HealthFlag OPTIONAL
2089 CPU_MP_DATA
*CpuMpData
;
2092 CpuMpData
= GetCpuMpData ();
2095 // Check whether caller processor is BSP
2097 MpInitLibWhoAmI (&CallerNumber
);
2098 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2099 return EFI_DEVICE_ERROR
;
2102 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2103 return EFI_INVALID_PARAMETER
;
2106 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2107 return EFI_NOT_FOUND
;
2111 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2113 ResetProcessorToIdleState (ProcessorNumber
);
2116 if (HealthFlag
!= NULL
) {
2117 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2118 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2125 This return the handle number for the calling processor. This service may be
2126 called from the BSP and APs.
2128 @param[out] ProcessorNumber Pointer to the handle number of AP.
2129 The range is from 0 to the total number of
2130 logical processors minus 1. The total number of
2131 logical processors can be retrieved by
2132 MpInitLibGetNumberOfProcessors().
2134 @retval EFI_SUCCESS The current processor handle number was returned
2136 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2137 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2143 OUT UINTN
*ProcessorNumber
2146 CPU_MP_DATA
*CpuMpData
;
2148 if (ProcessorNumber
== NULL
) {
2149 return EFI_INVALID_PARAMETER
;
2152 CpuMpData
= GetCpuMpData ();
2154 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2158 Retrieves the number of logical processor in the platform and the number of
2159 those logical processors that are enabled on this boot. This service may only
2160 be called from the BSP.
2162 @param[out] NumberOfProcessors Pointer to the total number of logical
2163 processors in the system, including the BSP
2165 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2166 processors that exist in system, including
2169 @retval EFI_SUCCESS The number of logical processors and enabled
2170 logical processors was retrieved.
2171 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2172 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2174 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2179 MpInitLibGetNumberOfProcessors (
2180 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2181 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2184 CPU_MP_DATA
*CpuMpData
;
2186 UINTN ProcessorNumber
;
2187 UINTN EnabledProcessorNumber
;
2190 CpuMpData
= GetCpuMpData ();
2192 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2193 return EFI_INVALID_PARAMETER
;
2197 // Check whether caller processor is BSP
2199 MpInitLibWhoAmI (&CallerNumber
);
2200 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2201 return EFI_DEVICE_ERROR
;
2204 ProcessorNumber
= CpuMpData
->CpuCount
;
2205 EnabledProcessorNumber
= 0;
2206 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2207 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2208 EnabledProcessorNumber
++;
2212 if (NumberOfProcessors
!= NULL
) {
2213 *NumberOfProcessors
= ProcessorNumber
;
2215 if (NumberOfEnabledProcessors
!= NULL
) {
2216 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2224 Worker function to execute a caller provided function on all enabled APs.
2226 @param[in] Procedure A pointer to the function to be run on
2227 enabled APs of the system.
2228 @param[in] SingleThread If TRUE, then all the enabled APs execute
2229 the function specified by Procedure one by
2230 one, in ascending order of processor handle
2231 number. If FALSE, then all the enabled APs
2232 execute the function specified by Procedure
2234 @param[in] ExcludeBsp Whether let BSP also trig this task.
2235 @param[in] WaitEvent The event created by the caller with CreateEvent()
2237 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2238 APs to return from Procedure, either for
2239 blocking or non-blocking mode.
2240 @param[in] ProcedureArgument The parameter passed into Procedure for
2242 @param[out] FailedCpuList If all APs finish successfully, then its
2243 content is set to NULL. If not all APs
2244 finish before timeout expires, then its
2245 content is set to address of the buffer
2246 holding handle numbers of the failed APs.
2248 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2249 the timeout expired.
2250 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2252 @retval others Failed to Startup all APs.
2256 StartupAllCPUsWorker (
2257 IN EFI_AP_PROCEDURE Procedure
,
2258 IN BOOLEAN SingleThread
,
2259 IN BOOLEAN ExcludeBsp
,
2260 IN EFI_EVENT WaitEvent OPTIONAL
,
2261 IN UINTN TimeoutInMicroseconds
,
2262 IN VOID
*ProcedureArgument OPTIONAL
,
2263 OUT UINTN
**FailedCpuList OPTIONAL
2267 CPU_MP_DATA
*CpuMpData
;
2268 UINTN ProcessorCount
;
2269 UINTN ProcessorNumber
;
2271 CPU_AP_DATA
*CpuData
;
2272 BOOLEAN HasEnabledAp
;
2275 CpuMpData
= GetCpuMpData ();
2277 if (FailedCpuList
!= NULL
) {
2278 *FailedCpuList
= NULL
;
2281 if (CpuMpData
->CpuCount
== 1 && ExcludeBsp
) {
2282 return EFI_NOT_STARTED
;
2285 if (Procedure
== NULL
) {
2286 return EFI_INVALID_PARAMETER
;
2290 // Check whether caller processor is BSP
2292 MpInitLibWhoAmI (&CallerNumber
);
2293 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2294 return EFI_DEVICE_ERROR
;
2300 CheckAndUpdateApsStatus ();
2302 ProcessorCount
= CpuMpData
->CpuCount
;
2303 HasEnabledAp
= FALSE
;
2305 // Check whether all enabled APs are idle.
2306 // If any enabled AP is not idle, return EFI_NOT_READY.
2308 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2309 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2310 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2311 ApState
= GetApState (CpuData
);
2312 if (ApState
!= CpuStateDisabled
) {
2313 HasEnabledAp
= TRUE
;
2314 if (ApState
!= CpuStateIdle
) {
2316 // If any enabled APs are busy, return EFI_NOT_READY.
2318 return EFI_NOT_READY
;
2324 if (!HasEnabledAp
&& ExcludeBsp
) {
2326 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2328 return EFI_NOT_STARTED
;
2331 CpuMpData
->RunningCount
= 0;
2332 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2333 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2334 CpuData
->Waiting
= FALSE
;
2335 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2336 if (CpuData
->State
== CpuStateIdle
) {
2338 // Mark this processor as responsible for current calling.
2340 CpuData
->Waiting
= TRUE
;
2341 CpuMpData
->RunningCount
++;
2346 CpuMpData
->Procedure
= Procedure
;
2347 CpuMpData
->ProcArguments
= ProcedureArgument
;
2348 CpuMpData
->SingleThread
= SingleThread
;
2349 CpuMpData
->FinishedCount
= 0;
2350 CpuMpData
->FailedCpuList
= FailedCpuList
;
2351 CpuMpData
->ExpectedTime
= CalculateTimeout (
2352 TimeoutInMicroseconds
,
2353 &CpuMpData
->CurrentTime
2355 CpuMpData
->TotalTime
= 0;
2356 CpuMpData
->WaitEvent
= WaitEvent
;
2358 if (!SingleThread
) {
2359 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2361 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2362 if (ProcessorNumber
== CallerNumber
) {
2365 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2366 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2376 Procedure (ProcedureArgument
);
2379 Status
= EFI_SUCCESS
;
2380 if (WaitEvent
== NULL
) {
2382 Status
= CheckAllAPs ();
2383 } while (Status
== EFI_NOT_READY
);
2390 Worker function to let the caller get one enabled AP to execute a caller-provided
2393 @param[in] Procedure A pointer to the function to be run on
2394 enabled APs of the system.
2395 @param[in] ProcessorNumber The handle number of the AP.
2396 @param[in] WaitEvent The event created by the caller with CreateEvent()
2398 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2399 APs to return from Procedure, either for
2400 blocking or non-blocking mode.
2401 @param[in] ProcedureArgument The parameter passed into Procedure for
2403 @param[out] Finished If AP returns from Procedure before the
2404 timeout expires, its content is set to TRUE.
2405 Otherwise, the value is set to FALSE.
2407 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2408 the timeout expires.
2409 @retval others Failed to Startup AP.
2413 StartupThisAPWorker (
2414 IN EFI_AP_PROCEDURE Procedure
,
2415 IN UINTN ProcessorNumber
,
2416 IN EFI_EVENT WaitEvent OPTIONAL
,
2417 IN UINTN TimeoutInMicroseconds
,
2418 IN VOID
*ProcedureArgument OPTIONAL
,
2419 OUT BOOLEAN
*Finished OPTIONAL
2423 CPU_MP_DATA
*CpuMpData
;
2424 CPU_AP_DATA
*CpuData
;
2427 CpuMpData
= GetCpuMpData ();
2429 if (Finished
!= NULL
) {
2434 // Check whether caller processor is BSP
2436 MpInitLibWhoAmI (&CallerNumber
);
2437 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2438 return EFI_DEVICE_ERROR
;
2442 // Check whether processor with the handle specified by ProcessorNumber exists
2444 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2445 return EFI_NOT_FOUND
;
2449 // Check whether specified processor is BSP
2451 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2452 return EFI_INVALID_PARAMETER
;
2456 // Check parameter Procedure
2458 if (Procedure
== NULL
) {
2459 return EFI_INVALID_PARAMETER
;
2465 CheckAndUpdateApsStatus ();
2468 // Check whether specified AP is disabled
2470 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2471 return EFI_INVALID_PARAMETER
;
2475 // If WaitEvent is not NULL, execute in non-blocking mode.
2476 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2477 // CheckAPsStatus() will check completion and timeout periodically.
2479 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2480 CpuData
->WaitEvent
= WaitEvent
;
2481 CpuData
->Finished
= Finished
;
2482 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2483 CpuData
->TotalTime
= 0;
2485 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2488 // If WaitEvent is NULL, execute in blocking mode.
2489 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2491 Status
= EFI_SUCCESS
;
2492 if (WaitEvent
== NULL
) {
2494 Status
= CheckThisAP (ProcessorNumber
);
2495 } while (Status
== EFI_NOT_READY
);
2502 Get pointer to CPU MP Data structure from GUIDed HOB.
2504 @return The pointer to CPU MP Data structure.
2507 GetCpuMpDataFromGuidedHob (
2511 EFI_HOB_GUID_TYPE
*GuidHob
;
2513 CPU_MP_DATA
*CpuMpData
;
2516 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2517 if (GuidHob
!= NULL
) {
2518 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2519 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2525 This service executes a caller provided function on all enabled CPUs.
2527 @param[in] Procedure A pointer to the function to be run on
2528 enabled APs of the system. See type
2530 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2531 APs to return from Procedure, either for
2532 blocking or non-blocking mode. Zero means
2533 infinity. TimeoutInMicroseconds is ignored
2535 @param[in] ProcedureArgument The parameter passed into Procedure for
2538 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2539 the timeout expired.
2540 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2541 to all enabled CPUs.
2542 @retval EFI_DEVICE_ERROR Caller processor is AP.
2543 @retval EFI_NOT_READY Any enabled APs are busy.
2544 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2545 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2546 all enabled APs have finished.
2547 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2552 MpInitLibStartupAllCPUs (
2553 IN EFI_AP_PROCEDURE Procedure
,
2554 IN UINTN TimeoutInMicroseconds
,
2555 IN VOID
*ProcedureArgument OPTIONAL
2558 return StartupAllCPUsWorker (
2563 TimeoutInMicroseconds
,