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
);
1707 CpuMpData
->SevEsIsEnabled
= PcdGetBool (PcdSevEsIsEnabled
);
1710 // Make sure no memory usage outside of the allocated buffer.
1712 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1713 Buffer
+ BufferSize
);
1716 // Duplicate BSP's IDT to APs.
1717 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1719 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1720 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1722 // Don't pass BSP's TR to APs to avoid AP init failure.
1724 VolatileRegisters
.Tr
= 0;
1725 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1727 // Set BSP basic information
1729 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1731 // Save assembly code information
1733 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1735 // Finally set AP loop mode
1737 CpuMpData
->ApLoopMode
= ApLoopMode
;
1738 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1740 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1743 // Set up APs wakeup signal buffer
1745 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1746 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1747 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1750 // Enable the local APIC for Virtual Wire Mode.
1752 ProgramVirtualWireMode ();
1754 if (OldCpuMpData
== NULL
) {
1755 if (MaxLogicalProcessorNumber
> 1) {
1757 // Wakeup all APs and calculate the processor count in system
1759 CollectProcessorCount (CpuMpData
);
1763 // APs have been wakeup before, just get the CPU Information
1766 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1767 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1768 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1769 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1770 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1771 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1772 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1773 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1777 if (!GetMicrocodePatchInfoFromHob (
1778 &CpuMpData
->MicrocodePatchAddress
,
1779 &CpuMpData
->MicrocodePatchRegionSize
1782 // The microcode patch information cache HOB does not exist, which means
1783 // the microcode patches data has not been loaded into memory yet
1785 ShadowMicrocodeUpdatePatch (CpuMpData
);
1789 // Detect and apply Microcode on BSP
1791 MicrocodeDetect (CpuMpData
, CpuMpData
->BspNumber
);
1793 // Store BSP's MTRR setting
1795 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1798 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1800 if (CpuMpData
->CpuCount
> 1) {
1801 if (OldCpuMpData
!= NULL
) {
1803 // Only needs to use this flag for DXE phase to update the wake up
1804 // buffer. Wakeup buffer allocated in PEI phase is no longer valid
1807 CpuMpData
->InitFlag
= ApInitReconfig
;
1809 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1811 // Wait for all APs finished initialization
1813 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1816 if (OldCpuMpData
!= NULL
) {
1817 CpuMpData
->InitFlag
= ApInitDone
;
1819 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1820 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1825 // Initialize global data for MP support
1827 InitMpGlobalData (CpuMpData
);
1833 Gets detailed MP-related information on the requested processor at the
1834 instant this call is made. This service may only be called from the BSP.
1836 @param[in] ProcessorNumber The handle number of processor.
1837 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1838 the requested processor is deposited.
1839 @param[out] HealthData Return processor health data.
1841 @retval EFI_SUCCESS Processor information was returned.
1842 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1843 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1844 @retval EFI_NOT_FOUND The processor with the handle specified by
1845 ProcessorNumber does not exist in the platform.
1846 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1851 MpInitLibGetProcessorInfo (
1852 IN UINTN ProcessorNumber
,
1853 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1854 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1857 CPU_MP_DATA
*CpuMpData
;
1859 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1860 UINTN OriginalProcessorNumber
;
1862 CpuMpData
= GetCpuMpData ();
1863 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1866 // Lower 24 bits contains the actual processor number.
1868 OriginalProcessorNumber
= ProcessorNumber
;
1869 ProcessorNumber
&= BIT24
- 1;
1872 // Check whether caller processor is BSP
1874 MpInitLibWhoAmI (&CallerNumber
);
1875 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1876 return EFI_DEVICE_ERROR
;
1879 if (ProcessorInfoBuffer
== NULL
) {
1880 return EFI_INVALID_PARAMETER
;
1883 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1884 return EFI_NOT_FOUND
;
1887 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1888 ProcessorInfoBuffer
->StatusFlag
= 0;
1889 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1890 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1892 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1893 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1895 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1896 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1898 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1902 // Get processor location information
1904 GetProcessorLocationByApicId (
1905 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1906 &ProcessorInfoBuffer
->Location
.Package
,
1907 &ProcessorInfoBuffer
->Location
.Core
,
1908 &ProcessorInfoBuffer
->Location
.Thread
1911 if ((OriginalProcessorNumber
& CPU_V2_EXTENDED_TOPOLOGY
) != 0) {
1912 GetProcessorLocation2ByApicId (
1913 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1914 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Package
,
1915 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Die
,
1916 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Tile
,
1917 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Module
,
1918 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Core
,
1919 &ProcessorInfoBuffer
->ExtendedInformation
.Location2
.Thread
1923 if (HealthData
!= NULL
) {
1924 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1931 Worker function to switch the requested AP to be the BSP from that point onward.
1933 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1934 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1935 enabled AP. Otherwise, it will be disabled.
1937 @retval EFI_SUCCESS BSP successfully switched.
1938 @retval others Failed to switch BSP.
1943 IN UINTN ProcessorNumber
,
1944 IN BOOLEAN EnableOldBSP
1947 CPU_MP_DATA
*CpuMpData
;
1950 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1951 BOOLEAN OldInterruptState
;
1952 BOOLEAN OldTimerInterruptState
;
1955 // Save and Disable Local APIC timer interrupt
1957 OldTimerInterruptState
= GetApicTimerInterruptState ();
1958 DisableApicTimerInterrupt ();
1960 // Before send both BSP and AP to a procedure to exchange their roles,
1961 // interrupt must be disabled. This is because during the exchange role
1962 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1963 // be corrupted, since interrupt return address will be pushed to stack
1966 OldInterruptState
= SaveAndDisableInterrupts ();
1969 // Mask LINT0 & LINT1 for the old BSP
1971 DisableLvtInterrupts ();
1973 CpuMpData
= GetCpuMpData ();
1976 // Check whether caller processor is BSP
1978 MpInitLibWhoAmI (&CallerNumber
);
1979 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1980 return EFI_DEVICE_ERROR
;
1983 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1984 return EFI_NOT_FOUND
;
1988 // Check whether specified AP is disabled
1990 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1991 if (State
== CpuStateDisabled
) {
1992 return EFI_INVALID_PARAMETER
;
1996 // Check whether ProcessorNumber specifies the current BSP
1998 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1999 return EFI_INVALID_PARAMETER
;
2003 // Check whether specified AP is busy
2005 if (State
== CpuStateBusy
) {
2006 return EFI_NOT_READY
;
2009 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2010 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
2011 CpuMpData
->SwitchBspFlag
= TRUE
;
2012 CpuMpData
->NewBspNumber
= ProcessorNumber
;
2015 // Clear the BSP bit of MSR_IA32_APIC_BASE
2017 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2018 ApicBaseMsr
.Bits
.BSP
= 0;
2019 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2022 // Need to wakeUp AP (future BSP).
2024 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
2026 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
2029 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
2031 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
2032 ApicBaseMsr
.Bits
.BSP
= 1;
2033 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
2034 ProgramVirtualWireMode ();
2037 // Wait for old BSP finished AP task
2039 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
2043 CpuMpData
->SwitchBspFlag
= FALSE
;
2045 // Set old BSP enable state
2047 if (!EnableOldBSP
) {
2048 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
2050 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2053 // Save new BSP number
2055 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
2058 // Restore interrupt state.
2060 SetInterruptState (OldInterruptState
);
2062 if (OldTimerInterruptState
) {
2063 EnableApicTimerInterrupt ();
2070 Worker function to let the caller enable or disable an AP from this point onward.
2071 This service may only be called from the BSP.
2073 @param[in] ProcessorNumber The handle number of AP.
2074 @param[in] EnableAP Specifies the new state for the processor for
2075 enabled, FALSE for disabled.
2076 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2077 the new health status of the AP.
2079 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2080 @retval others Failed to Enable/Disable AP.
2084 EnableDisableApWorker (
2085 IN UINTN ProcessorNumber
,
2086 IN BOOLEAN EnableAP
,
2087 IN UINT32
*HealthFlag OPTIONAL
2090 CPU_MP_DATA
*CpuMpData
;
2093 CpuMpData
= GetCpuMpData ();
2096 // Check whether caller processor is BSP
2098 MpInitLibWhoAmI (&CallerNumber
);
2099 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2100 return EFI_DEVICE_ERROR
;
2103 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2104 return EFI_INVALID_PARAMETER
;
2107 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2108 return EFI_NOT_FOUND
;
2112 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2114 ResetProcessorToIdleState (ProcessorNumber
);
2117 if (HealthFlag
!= NULL
) {
2118 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2119 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2126 This return the handle number for the calling processor. This service may be
2127 called from the BSP and APs.
2129 @param[out] ProcessorNumber Pointer to the handle number of AP.
2130 The range is from 0 to the total number of
2131 logical processors minus 1. The total number of
2132 logical processors can be retrieved by
2133 MpInitLibGetNumberOfProcessors().
2135 @retval EFI_SUCCESS The current processor handle number was returned
2137 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2138 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2144 OUT UINTN
*ProcessorNumber
2147 CPU_MP_DATA
*CpuMpData
;
2149 if (ProcessorNumber
== NULL
) {
2150 return EFI_INVALID_PARAMETER
;
2153 CpuMpData
= GetCpuMpData ();
2155 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2159 Retrieves the number of logical processor in the platform and the number of
2160 those logical processors that are enabled on this boot. This service may only
2161 be called from the BSP.
2163 @param[out] NumberOfProcessors Pointer to the total number of logical
2164 processors in the system, including the BSP
2166 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2167 processors that exist in system, including
2170 @retval EFI_SUCCESS The number of logical processors and enabled
2171 logical processors was retrieved.
2172 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2173 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2175 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2180 MpInitLibGetNumberOfProcessors (
2181 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2182 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2185 CPU_MP_DATA
*CpuMpData
;
2187 UINTN ProcessorNumber
;
2188 UINTN EnabledProcessorNumber
;
2191 CpuMpData
= GetCpuMpData ();
2193 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2194 return EFI_INVALID_PARAMETER
;
2198 // Check whether caller processor is BSP
2200 MpInitLibWhoAmI (&CallerNumber
);
2201 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2202 return EFI_DEVICE_ERROR
;
2205 ProcessorNumber
= CpuMpData
->CpuCount
;
2206 EnabledProcessorNumber
= 0;
2207 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2208 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2209 EnabledProcessorNumber
++;
2213 if (NumberOfProcessors
!= NULL
) {
2214 *NumberOfProcessors
= ProcessorNumber
;
2216 if (NumberOfEnabledProcessors
!= NULL
) {
2217 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2225 Worker function to execute a caller provided function on all enabled APs.
2227 @param[in] Procedure A pointer to the function to be run on
2228 enabled APs of the system.
2229 @param[in] SingleThread If TRUE, then all the enabled APs execute
2230 the function specified by Procedure one by
2231 one, in ascending order of processor handle
2232 number. If FALSE, then all the enabled APs
2233 execute the function specified by Procedure
2235 @param[in] ExcludeBsp Whether let BSP also trig this task.
2236 @param[in] WaitEvent The event created by the caller with CreateEvent()
2238 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2239 APs to return from Procedure, either for
2240 blocking or non-blocking mode.
2241 @param[in] ProcedureArgument The parameter passed into Procedure for
2243 @param[out] FailedCpuList If all APs finish successfully, then its
2244 content is set to NULL. If not all APs
2245 finish before timeout expires, then its
2246 content is set to address of the buffer
2247 holding handle numbers of the failed APs.
2249 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2250 the timeout expired.
2251 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2253 @retval others Failed to Startup all APs.
2257 StartupAllCPUsWorker (
2258 IN EFI_AP_PROCEDURE Procedure
,
2259 IN BOOLEAN SingleThread
,
2260 IN BOOLEAN ExcludeBsp
,
2261 IN EFI_EVENT WaitEvent OPTIONAL
,
2262 IN UINTN TimeoutInMicroseconds
,
2263 IN VOID
*ProcedureArgument OPTIONAL
,
2264 OUT UINTN
**FailedCpuList OPTIONAL
2268 CPU_MP_DATA
*CpuMpData
;
2269 UINTN ProcessorCount
;
2270 UINTN ProcessorNumber
;
2272 CPU_AP_DATA
*CpuData
;
2273 BOOLEAN HasEnabledAp
;
2276 CpuMpData
= GetCpuMpData ();
2278 if (FailedCpuList
!= NULL
) {
2279 *FailedCpuList
= NULL
;
2282 if (CpuMpData
->CpuCount
== 1 && ExcludeBsp
) {
2283 return EFI_NOT_STARTED
;
2286 if (Procedure
== NULL
) {
2287 return EFI_INVALID_PARAMETER
;
2291 // Check whether caller processor is BSP
2293 MpInitLibWhoAmI (&CallerNumber
);
2294 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2295 return EFI_DEVICE_ERROR
;
2301 CheckAndUpdateApsStatus ();
2303 ProcessorCount
= CpuMpData
->CpuCount
;
2304 HasEnabledAp
= FALSE
;
2306 // Check whether all enabled APs are idle.
2307 // If any enabled AP is not idle, return EFI_NOT_READY.
2309 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2310 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2311 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2312 ApState
= GetApState (CpuData
);
2313 if (ApState
!= CpuStateDisabled
) {
2314 HasEnabledAp
= TRUE
;
2315 if (ApState
!= CpuStateIdle
) {
2317 // If any enabled APs are busy, return EFI_NOT_READY.
2319 return EFI_NOT_READY
;
2325 if (!HasEnabledAp
&& ExcludeBsp
) {
2327 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2329 return EFI_NOT_STARTED
;
2332 CpuMpData
->RunningCount
= 0;
2333 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2334 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2335 CpuData
->Waiting
= FALSE
;
2336 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2337 if (CpuData
->State
== CpuStateIdle
) {
2339 // Mark this processor as responsible for current calling.
2341 CpuData
->Waiting
= TRUE
;
2342 CpuMpData
->RunningCount
++;
2347 CpuMpData
->Procedure
= Procedure
;
2348 CpuMpData
->ProcArguments
= ProcedureArgument
;
2349 CpuMpData
->SingleThread
= SingleThread
;
2350 CpuMpData
->FinishedCount
= 0;
2351 CpuMpData
->FailedCpuList
= FailedCpuList
;
2352 CpuMpData
->ExpectedTime
= CalculateTimeout (
2353 TimeoutInMicroseconds
,
2354 &CpuMpData
->CurrentTime
2356 CpuMpData
->TotalTime
= 0;
2357 CpuMpData
->WaitEvent
= WaitEvent
;
2359 if (!SingleThread
) {
2360 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2362 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2363 if (ProcessorNumber
== CallerNumber
) {
2366 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2367 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2377 Procedure (ProcedureArgument
);
2380 Status
= EFI_SUCCESS
;
2381 if (WaitEvent
== NULL
) {
2383 Status
= CheckAllAPs ();
2384 } while (Status
== EFI_NOT_READY
);
2391 Worker function to let the caller get one enabled AP to execute a caller-provided
2394 @param[in] Procedure A pointer to the function to be run on
2395 enabled APs of the system.
2396 @param[in] ProcessorNumber The handle number of the AP.
2397 @param[in] WaitEvent The event created by the caller with CreateEvent()
2399 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2400 APs to return from Procedure, either for
2401 blocking or non-blocking mode.
2402 @param[in] ProcedureArgument The parameter passed into Procedure for
2404 @param[out] Finished If AP returns from Procedure before the
2405 timeout expires, its content is set to TRUE.
2406 Otherwise, the value is set to FALSE.
2408 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2409 the timeout expires.
2410 @retval others Failed to Startup AP.
2414 StartupThisAPWorker (
2415 IN EFI_AP_PROCEDURE Procedure
,
2416 IN UINTN ProcessorNumber
,
2417 IN EFI_EVENT WaitEvent OPTIONAL
,
2418 IN UINTN TimeoutInMicroseconds
,
2419 IN VOID
*ProcedureArgument OPTIONAL
,
2420 OUT BOOLEAN
*Finished OPTIONAL
2424 CPU_MP_DATA
*CpuMpData
;
2425 CPU_AP_DATA
*CpuData
;
2428 CpuMpData
= GetCpuMpData ();
2430 if (Finished
!= NULL
) {
2435 // Check whether caller processor is BSP
2437 MpInitLibWhoAmI (&CallerNumber
);
2438 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2439 return EFI_DEVICE_ERROR
;
2443 // Check whether processor with the handle specified by ProcessorNumber exists
2445 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2446 return EFI_NOT_FOUND
;
2450 // Check whether specified processor is BSP
2452 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2453 return EFI_INVALID_PARAMETER
;
2457 // Check parameter Procedure
2459 if (Procedure
== NULL
) {
2460 return EFI_INVALID_PARAMETER
;
2466 CheckAndUpdateApsStatus ();
2469 // Check whether specified AP is disabled
2471 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2472 return EFI_INVALID_PARAMETER
;
2476 // If WaitEvent is not NULL, execute in non-blocking mode.
2477 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2478 // CheckAPsStatus() will check completion and timeout periodically.
2480 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2481 CpuData
->WaitEvent
= WaitEvent
;
2482 CpuData
->Finished
= Finished
;
2483 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2484 CpuData
->TotalTime
= 0;
2486 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2489 // If WaitEvent is NULL, execute in blocking mode.
2490 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2492 Status
= EFI_SUCCESS
;
2493 if (WaitEvent
== NULL
) {
2495 Status
= CheckThisAP (ProcessorNumber
);
2496 } while (Status
== EFI_NOT_READY
);
2503 Get pointer to CPU MP Data structure from GUIDed HOB.
2505 @return The pointer to CPU MP Data structure.
2508 GetCpuMpDataFromGuidedHob (
2512 EFI_HOB_GUID_TYPE
*GuidHob
;
2514 CPU_MP_DATA
*CpuMpData
;
2517 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2518 if (GuidHob
!= NULL
) {
2519 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2520 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2526 This service executes a caller provided function on all enabled CPUs.
2528 @param[in] Procedure A pointer to the function to be run on
2529 enabled APs of the system. See type
2531 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2532 APs to return from Procedure, either for
2533 blocking or non-blocking mode. Zero means
2534 infinity. TimeoutInMicroseconds is ignored
2536 @param[in] ProcedureArgument The parameter passed into Procedure for
2539 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2540 the timeout expired.
2541 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2542 to all enabled CPUs.
2543 @retval EFI_DEVICE_ERROR Caller processor is AP.
2544 @retval EFI_NOT_READY Any enabled APs are busy.
2545 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2546 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2547 all enabled APs have finished.
2548 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2553 MpInitLibStartupAllCPUs (
2554 IN EFI_AP_PROCEDURE Procedure
,
2555 IN UINTN TimeoutInMicroseconds
,
2556 IN VOID
*ProcedureArgument OPTIONAL
2559 return StartupAllCPUsWorker (
2564 TimeoutInMicroseconds
,