2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 EFI_GUID mCpuInitMpLibHobGuid
= CPU_INIT_MP_LIB_HOB_GUID
;
14 The function will check if BSP Execute Disable is enabled.
16 DxeIpl may have enabled Execute Disable for BSP, APs need to
17 get the status and sync up the settings.
18 If BSP's CR0.Paging is not set, BSP execute Disble feature is
21 @retval TRUE BSP Execute Disable is enabled.
22 @retval FALSE BSP Execute Disable is not enabled.
25 IsBspExecuteDisableEnabled (
30 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
31 MSR_IA32_EFER_REGISTER EferMsr
;
36 Cr0
.UintN
= AsmReadCr0 ();
37 if (Cr0
.Bits
.PG
!= 0) {
39 // If CR0 Paging bit is set
41 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
42 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
43 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
46 // Bit 20: Execute Disable Bit available.
48 if (Edx
.Bits
.NX
!= 0) {
49 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
52 // Bit 11: Execute Disable Bit enable.
54 if (EferMsr
.Bits
.NXE
!= 0) {
65 Worker function for SwitchBSP().
67 Worker function for SwitchBSP(), assigned to the AP which is intended
70 @param[in] Buffer Pointer to CPU MP Data
78 CPU_MP_DATA
*DataInHob
;
80 DataInHob
= (CPU_MP_DATA
*) Buffer
;
81 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
85 Get the Application Processors state.
87 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
93 IN CPU_AP_DATA
*CpuData
96 return CpuData
->State
;
100 Set the Application Processors state.
102 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
103 @param[in] State The AP status
107 IN CPU_AP_DATA
*CpuData
,
111 AcquireSpinLock (&CpuData
->ApLock
);
112 CpuData
->State
= State
;
113 ReleaseSpinLock (&CpuData
->ApLock
);
117 Save BSP's local APIC timer setting.
119 @param[in] CpuMpData Pointer to CPU MP Data
122 SaveLocalApicTimerSetting (
123 IN CPU_MP_DATA
*CpuMpData
127 // Record the current local APIC timer setting of BSP
130 &CpuMpData
->DivideValue
,
131 &CpuMpData
->PeriodicMode
,
134 CpuMpData
->CurrentTimerCount
= GetApicTimerCurrentCount ();
135 CpuMpData
->TimerInterruptState
= GetApicTimerInterruptState ();
139 Sync local APIC timer setting from BSP to AP.
141 @param[in] CpuMpData Pointer to CPU MP Data
144 SyncLocalApicTimerSetting (
145 IN CPU_MP_DATA
*CpuMpData
149 // Sync local APIC timer setting from BSP to AP
151 InitializeApicTimer (
152 CpuMpData
->DivideValue
,
153 CpuMpData
->CurrentTimerCount
,
154 CpuMpData
->PeriodicMode
,
158 // Disable AP's local APIC timer interrupt
160 DisableApicTimerInterrupt ();
164 Save the volatile registers required to be restored following INIT IPI.
166 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
169 SaveVolatileRegisters (
170 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
173 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
175 VolatileRegisters
->Cr0
= AsmReadCr0 ();
176 VolatileRegisters
->Cr3
= AsmReadCr3 ();
177 VolatileRegisters
->Cr4
= AsmReadCr4 ();
179 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
180 if (VersionInfoEdx
.Bits
.DE
!= 0) {
182 // If processor supports Debugging Extensions feature
183 // by CPUID.[EAX=01H]:EDX.BIT2
185 VolatileRegisters
->Dr0
= AsmReadDr0 ();
186 VolatileRegisters
->Dr1
= AsmReadDr1 ();
187 VolatileRegisters
->Dr2
= AsmReadDr2 ();
188 VolatileRegisters
->Dr3
= AsmReadDr3 ();
189 VolatileRegisters
->Dr6
= AsmReadDr6 ();
190 VolatileRegisters
->Dr7
= AsmReadDr7 ();
193 AsmReadGdtr (&VolatileRegisters
->Gdtr
);
194 AsmReadIdtr (&VolatileRegisters
->Idtr
);
195 VolatileRegisters
->Tr
= AsmReadTr ();
199 Restore the volatile registers following INIT IPI.
201 @param[in] VolatileRegisters Pointer to volatile resisters
202 @param[in] IsRestoreDr TRUE: Restore DRx if supported
203 FALSE: Do not restore DRx
206 RestoreVolatileRegisters (
207 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
208 IN BOOLEAN IsRestoreDr
211 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
212 IA32_TSS_DESCRIPTOR
*Tss
;
214 AsmWriteCr3 (VolatileRegisters
->Cr3
);
215 AsmWriteCr4 (VolatileRegisters
->Cr4
);
216 AsmWriteCr0 (VolatileRegisters
->Cr0
);
219 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
220 if (VersionInfoEdx
.Bits
.DE
!= 0) {
222 // If processor supports Debugging Extensions feature
223 // by CPUID.[EAX=01H]:EDX.BIT2
225 AsmWriteDr0 (VolatileRegisters
->Dr0
);
226 AsmWriteDr1 (VolatileRegisters
->Dr1
);
227 AsmWriteDr2 (VolatileRegisters
->Dr2
);
228 AsmWriteDr3 (VolatileRegisters
->Dr3
);
229 AsmWriteDr6 (VolatileRegisters
->Dr6
);
230 AsmWriteDr7 (VolatileRegisters
->Dr7
);
234 AsmWriteGdtr (&VolatileRegisters
->Gdtr
);
235 AsmWriteIdtr (&VolatileRegisters
->Idtr
);
236 if (VolatileRegisters
->Tr
!= 0 &&
237 VolatileRegisters
->Tr
< VolatileRegisters
->Gdtr
.Limit
) {
238 Tss
= (IA32_TSS_DESCRIPTOR
*)(VolatileRegisters
->Gdtr
.Base
+
239 VolatileRegisters
->Tr
);
240 if (Tss
->Bits
.P
== 1) {
241 Tss
->Bits
.Type
&= 0xD; // 1101 - Clear busy bit just in case
242 AsmWriteTr (VolatileRegisters
->Tr
);
248 Detect whether Mwait-monitor feature is supported.
250 @retval TRUE Mwait-monitor feature is supported.
251 @retval FALSE Mwait-monitor feature is not supported.
258 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
260 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
261 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
267 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
269 @return The AP loop mode.
273 OUT UINT32
*MonitorFilterSize
277 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
279 ASSERT (MonitorFilterSize
!= NULL
);
281 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
282 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
283 if (ApLoopMode
== ApInMwaitLoop
) {
284 if (!IsMwaitSupport ()) {
286 // If processor does not support MONITOR/MWAIT feature,
287 // force AP in Hlt-loop mode
289 ApLoopMode
= ApInHltLoop
;
293 if (ApLoopMode
!= ApInMwaitLoop
) {
294 *MonitorFilterSize
= sizeof (UINT32
);
297 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
298 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
300 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
301 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
308 Sort the APIC ID of all processors.
310 This function sorts the APIC ID of all processors so that processor number is
311 assigned in the ascending order of APIC ID which eases MP debugging.
313 @param[in] CpuMpData Pointer to PEI CPU MP Data
317 IN CPU_MP_DATA
*CpuMpData
324 CPU_INFO_IN_HOB CpuInfo
;
326 CPU_INFO_IN_HOB
*CpuInfoInHob
;
327 volatile UINT32
*StartupApSignal
;
329 ApCount
= CpuMpData
->CpuCount
- 1;
330 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
332 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
335 // Sort key is the hardware default APIC ID
337 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
338 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
339 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
341 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
344 if (Index3
!= Index1
) {
345 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
347 &CpuInfoInHob
[Index3
],
348 &CpuInfoInHob
[Index1
],
349 sizeof (CPU_INFO_IN_HOB
)
351 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
354 // Also exchange the StartupApSignal.
356 StartupApSignal
= CpuMpData
->CpuData
[Index3
].StartupApSignal
;
357 CpuMpData
->CpuData
[Index3
].StartupApSignal
=
358 CpuMpData
->CpuData
[Index1
].StartupApSignal
;
359 CpuMpData
->CpuData
[Index1
].StartupApSignal
= StartupApSignal
;
364 // Get the processor number for the BSP
366 ApicId
= GetInitialApicId ();
367 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
368 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
369 CpuMpData
->BspNumber
= (UINT32
) Index1
;
377 Enable x2APIC mode on APs.
379 @param[in, out] Buffer Pointer to private data buffer.
387 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
393 @param[in, out] Buffer Pointer to private data buffer.
401 CPU_MP_DATA
*CpuMpData
;
403 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
405 // Load microcode on AP
407 MicrocodeDetect (CpuMpData
, FALSE
);
409 // Sync BSP's MTRR table to AP
411 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
415 Find the current Processor number by APIC ID.
417 @param[in] CpuMpData Pointer to PEI CPU MP Data
418 @param[out] ProcessorNumber Return the pocessor number found
420 @retval EFI_SUCCESS ProcessorNumber is found and returned.
421 @retval EFI_NOT_FOUND ProcessorNumber is not found.
425 IN CPU_MP_DATA
*CpuMpData
,
426 OUT UINTN
*ProcessorNumber
429 UINTN TotalProcessorNumber
;
431 CPU_INFO_IN_HOB
*CpuInfoInHob
;
432 UINT32 CurrentApicId
;
434 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
436 TotalProcessorNumber
= CpuMpData
->CpuCount
;
437 CurrentApicId
= GetApicId ();
438 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
439 if (CpuInfoInHob
[Index
].ApicId
== CurrentApicId
) {
440 *ProcessorNumber
= Index
;
445 return EFI_NOT_FOUND
;
449 This function will get CPU count in the system.
451 @param[in] CpuMpData Pointer to PEI CPU MP Data
453 @return CPU count detected
456 CollectProcessorCount (
457 IN CPU_MP_DATA
*CpuMpData
463 // Send 1st broadcast IPI to APs to wakeup APs
465 CpuMpData
->InitFlag
= ApInitConfig
;
466 CpuMpData
->X2ApicEnable
= FALSE
;
467 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
, TRUE
);
468 CpuMpData
->InitFlag
= ApInitDone
;
469 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
471 // Wait for all APs finished the initialization
473 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
477 if (CpuMpData
->CpuCount
> 255) {
479 // If there are more than 255 processor found, force to enable X2APIC
481 CpuMpData
->X2ApicEnable
= TRUE
;
483 if (CpuMpData
->X2ApicEnable
) {
484 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
486 // Wakeup all APs to enable x2APIC mode
488 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
, TRUE
);
490 // Wait for all known APs finished
492 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
496 // Enable x2APIC on BSP
498 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
500 // Set BSP/Aps state to IDLE
502 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
503 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
506 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
508 // Sort BSP/Aps by CPU APIC ID in ascending order
510 SortApicId (CpuMpData
);
512 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
514 return CpuMpData
->CpuCount
;
518 Initialize CPU AP Data when AP is wakeup at the first time.
520 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
521 @param[in] ProcessorNumber The handle number of processor
522 @param[in] BistData Processor BIST data
523 @param[in] ApTopOfStack Top of AP stack
528 IN OUT CPU_MP_DATA
*CpuMpData
,
529 IN UINTN ProcessorNumber
,
531 IN UINT64 ApTopOfStack
534 CPU_INFO_IN_HOB
*CpuInfoInHob
;
536 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
537 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
538 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
539 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
540 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
542 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
543 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
544 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
546 // Set x2APIC mode if there are any logical processor reporting
547 // an Initial APIC ID of 255 or greater.
549 AcquireSpinLock(&CpuMpData
->MpLock
);
550 CpuMpData
->X2ApicEnable
= TRUE
;
551 ReleaseSpinLock(&CpuMpData
->MpLock
);
554 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
555 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
559 This function will be called from AP reset code if BSP uses WakeUpAP.
561 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
562 @param[in] ApIndex Number of current executing AP
567 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
571 CPU_MP_DATA
*CpuMpData
;
572 UINTN ProcessorNumber
;
573 EFI_AP_PROCEDURE Procedure
;
576 volatile UINT32
*ApStartupSignalBuffer
;
577 CPU_INFO_IN_HOB
*CpuInfoInHob
;
579 UINTN CurrentApicMode
;
582 // AP finished assembly code and begin to execute C code
584 CpuMpData
= ExchangeInfo
->CpuMpData
;
587 // AP's local APIC settings will be lost after received INIT IPI
588 // We need to re-initialize them at here
590 ProgramVirtualWireMode ();
592 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
594 DisableLvtInterrupts ();
595 SyncLocalApicTimerSetting (CpuMpData
);
597 CurrentApicMode
= GetApicMode ();
599 if (CpuMpData
->InitFlag
== ApInitConfig
) {
603 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
604 ProcessorNumber
= ApIndex
;
606 // This is first time AP wakeup, get BIST information from AP stack
608 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
609 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
611 // Do some AP initialize sync
613 ApInitializeSync (CpuMpData
);
615 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
616 // to initialize AP in InitConfig path.
617 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
619 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
620 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
621 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
623 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
626 // Execute AP function if AP is ready
628 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
630 // Clear AP start-up signal when AP waken up
632 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
633 InterlockedCompareExchange32 (
634 (UINT32
*) ApStartupSignalBuffer
,
638 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
640 // Restore AP's volatile registers saved
642 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
645 // The CPU driver might not flush TLB for APs on spot after updating
646 // page attributes. AP in mwait loop mode needs to take care of it when
652 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
653 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
654 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
655 if (Procedure
!= NULL
) {
656 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
658 // Enable source debugging on AP function
662 // Invoke AP function here
664 Procedure (Parameter
);
665 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
666 if (CpuMpData
->SwitchBspFlag
) {
668 // Re-get the processor number due to BSP/AP maybe exchange in AP function
670 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
671 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
672 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
673 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
674 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
676 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
677 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
678 if (CurrentApicMode
!= GetApicMode ()) {
680 // If APIC mode change happened during AP function execution,
681 // we do not support APIC ID value changed.
687 // Re-get the CPU APICID and Initial APICID if they are changed
689 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
690 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
695 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
700 // AP finished executing C code
702 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
705 // Place AP is specified loop mode
707 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
709 // Save AP volatile registers
711 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
713 // Place AP in HLT-loop
716 DisableInterrupts ();
722 DisableInterrupts ();
723 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
725 // Place AP in MWAIT-loop
727 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
728 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
730 // Check AP start-up signal again.
731 // If AP start-up signal is not set, place AP into
732 // the specified C-state
734 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
736 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
738 // Place AP in Run-loop
746 // If AP start-up signal is written, AP is waken up
747 // otherwise place AP in loop again
749 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
757 Wait for AP wakeup and write AP start-up signal till AP is waken up.
759 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
763 IN
volatile UINT32
*ApStartupSignalBuffer
767 // If AP is waken up, StartupApSignal should be cleared.
768 // Otherwise, write StartupApSignal again till AP waken up.
770 while (InterlockedCompareExchange32 (
771 (UINT32
*) ApStartupSignalBuffer
,
780 This function will fill the exchange info structure.
782 @param[in] CpuMpData Pointer to CPU MP Data
786 FillExchangeInfoData (
787 IN CPU_MP_DATA
*CpuMpData
790 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
792 IA32_SEGMENT_DESCRIPTOR
*Selector
;
795 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
796 ExchangeInfo
->Lock
= 0;
797 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
798 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
799 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
800 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
802 ExchangeInfo
->CodeSegment
= AsmReadCs ();
803 ExchangeInfo
->DataSegment
= AsmReadDs ();
805 ExchangeInfo
->Cr3
= AsmReadCr3 ();
807 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
808 ExchangeInfo
->ApIndex
= 0;
809 ExchangeInfo
->NumApsExecuting
= 0;
810 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
811 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
812 ExchangeInfo
->CpuMpData
= CpuMpData
;
814 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
816 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
819 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
820 // to determin whether 5-Level Paging is enabled.
821 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
822 // current system setting.
823 // Using latter way is simpler because it also eliminates the needs to
824 // check whether platform wants to enable it.
826 Cr4
.UintN
= AsmReadCr4 ();
827 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
) (Cr4
.Bits
.LA57
== 1);
828 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
831 // Get the BSP's data of GDT and IDT
833 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
834 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
837 // Find a 32-bit code segment
839 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
840 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
842 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
843 ExchangeInfo
->ModeTransitionSegment
=
844 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
848 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
852 // Copy all 32-bit code and 64-bit code into memory with type of
853 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
855 if (CpuMpData
->WakeupBufferHigh
!= 0) {
856 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
857 CpuMpData
->AddressMap
.ModeTransitionOffset
;
859 (VOID
*)CpuMpData
->WakeupBufferHigh
,
860 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
861 CpuMpData
->AddressMap
.ModeTransitionOffset
,
865 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
867 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
868 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
871 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
872 (UINT32
)ExchangeInfo
->ModeOffset
-
873 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
874 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
878 Helper function that waits until the finished AP count reaches the specified
879 limit, or the specified timeout elapses (whichever comes first).
881 @param[in] CpuMpData Pointer to CPU MP Data.
882 @param[in] FinishedApLimit The number of finished APs to wait for.
883 @param[in] TimeLimit The number of microseconds to wait for.
886 TimedWaitForApFinish (
887 IN CPU_MP_DATA
*CpuMpData
,
888 IN UINT32 FinishedApLimit
,
893 Get available system memory below 1MB by specified size.
895 @param[in] CpuMpData The pointer to CPU MP Data structure.
898 BackupAndPrepareWakeupBuffer(
899 IN CPU_MP_DATA
*CpuMpData
903 (VOID
*) CpuMpData
->BackupBuffer
,
904 (VOID
*) CpuMpData
->WakeupBuffer
,
905 CpuMpData
->BackupBufferSize
908 (VOID
*) CpuMpData
->WakeupBuffer
,
909 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
910 CpuMpData
->AddressMap
.RendezvousFunnelSize
915 Restore wakeup buffer data.
917 @param[in] CpuMpData The pointer to CPU MP Data structure.
921 IN CPU_MP_DATA
*CpuMpData
925 (VOID
*) CpuMpData
->WakeupBuffer
,
926 (VOID
*) CpuMpData
->BackupBuffer
,
927 CpuMpData
->BackupBufferSize
932 Allocate reset vector buffer.
934 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
937 AllocateResetVector (
938 IN OUT CPU_MP_DATA
*CpuMpData
941 UINTN ApResetVectorSize
;
943 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
944 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
945 sizeof (MP_CPU_EXCHANGE_INFO
);
947 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
948 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
949 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
950 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
951 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
952 CpuMpData
->AddressMap
.ModeTransitionOffset
955 BackupAndPrepareWakeupBuffer (CpuMpData
);
959 Free AP reset vector buffer.
961 @param[in] CpuMpData The pointer to CPU MP Data structure.
965 IN CPU_MP_DATA
*CpuMpData
968 RestoreWakeupBuffer (CpuMpData
);
972 This function will be called by BSP to wakeup AP.
974 @param[in] CpuMpData Pointer to CPU MP Data
975 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
976 FALSE: Send IPI to AP by ApicId
977 @param[in] ProcessorNumber The handle number of specified processor
978 @param[in] Procedure The function to be invoked by AP
979 @param[in] ProcedureArgument The argument to be passed into AP function
980 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
984 IN CPU_MP_DATA
*CpuMpData
,
985 IN BOOLEAN Broadcast
,
986 IN UINTN ProcessorNumber
,
987 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
988 IN VOID
*ProcedureArgument
, OPTIONAL
989 IN BOOLEAN WakeUpDisabledAps
992 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
994 CPU_AP_DATA
*CpuData
;
995 BOOLEAN ResetVectorRequired
;
996 CPU_INFO_IN_HOB
*CpuInfoInHob
;
998 CpuMpData
->FinishedCount
= 0;
999 ResetVectorRequired
= FALSE
;
1001 if (CpuMpData
->WakeUpByInitSipiSipi
||
1002 CpuMpData
->InitFlag
!= ApInitDone
) {
1003 ResetVectorRequired
= TRUE
;
1004 AllocateResetVector (CpuMpData
);
1005 FillExchangeInfoData (CpuMpData
);
1006 SaveLocalApicTimerSetting (CpuMpData
);
1009 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1011 // Get AP target C-state each time when waking up AP,
1012 // for it maybe updated by platform again
1014 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1017 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1020 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1021 if (Index
!= CpuMpData
->BspNumber
) {
1022 CpuData
= &CpuMpData
->CpuData
[Index
];
1024 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1025 // the AP procedure will be skipped for disabled AP because AP state
1026 // is not CpuStateReady.
1028 if (GetApState (CpuData
) == CpuStateDisabled
&& !WakeUpDisabledAps
) {
1032 CpuData
->ApFunction
= (UINTN
) Procedure
;
1033 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1034 SetApState (CpuData
, CpuStateReady
);
1035 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1036 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1040 if (ResetVectorRequired
) {
1044 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1046 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1048 // Here support two methods to collect AP count through adjust
1049 // PcdCpuApInitTimeOutInMicroSeconds values.
1051 // one way is set a value to just let the first AP to start the
1052 // initialization, then through the later while loop to wait all Aps
1053 // finsh the initialization.
1054 // The other way is set a value to let all APs finished the initialzation.
1055 // In this case, the later while loop is useless.
1057 TimedWaitForApFinish (
1059 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1060 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1063 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1068 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1070 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1071 CpuData
= &CpuMpData
->CpuData
[Index
];
1072 if (Index
!= CpuMpData
->BspNumber
) {
1073 WaitApWakeup (CpuData
->StartupApSignal
);
1078 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1079 CpuData
->ApFunction
= (UINTN
) Procedure
;
1080 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1081 SetApState (CpuData
, CpuStateReady
);
1083 // Wakeup specified AP
1085 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1086 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1087 if (ResetVectorRequired
) {
1088 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1090 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1091 (UINT32
) ExchangeInfo
->BufferStart
1095 // Wait specified AP waken up
1097 WaitApWakeup (CpuData
->StartupApSignal
);
1100 if (ResetVectorRequired
) {
1101 FreeResetVector (CpuMpData
);
1105 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1106 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1107 // S3SmmInitDone Ppi.
1109 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1113 Calculate timeout value and return the current performance counter value.
1115 Calculate the number of performance counter ticks required for a timeout.
1116 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1119 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1120 @param[out] CurrentTime Returns the current value of the performance counter.
1122 @return Expected time stamp counter for timeout.
1123 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1129 IN UINTN TimeoutInMicroseconds
,
1130 OUT UINT64
*CurrentTime
1133 UINT64 TimeoutInSeconds
;
1134 UINT64 TimestampCounterFreq
;
1137 // Read the current value of the performance counter
1139 *CurrentTime
= GetPerformanceCounter ();
1142 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1145 if (TimeoutInMicroseconds
== 0) {
1150 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1153 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1156 // Check the potential overflow before calculate the number of ticks for the timeout value.
1158 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1160 // Convert microseconds into seconds if direct multiplication overflows
1162 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1164 // Assertion if the final tick count exceeds MAX_UINT64
1166 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1167 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1170 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1171 // it by 1,000,000, to get the number of ticks for the timeout value.
1175 TimestampCounterFreq
,
1176 TimeoutInMicroseconds
1184 Checks whether timeout expires.
1186 Check whether the number of elapsed performance counter ticks required for
1187 a timeout condition has been reached.
1188 If Timeout is zero, which means infinity, return value is always FALSE.
1190 @param[in, out] PreviousTime On input, the value of the performance counter
1191 when it was last read.
1192 On output, the current value of the performance
1194 @param[in] TotalTime The total amount of elapsed time in performance
1196 @param[in] Timeout The number of performance counter ticks required
1197 to reach a timeout condition.
1199 @retval TRUE A timeout condition has been reached.
1200 @retval FALSE A timeout condition has not been reached.
1205 IN OUT UINT64
*PreviousTime
,
1206 IN UINT64
*TotalTime
,
1219 GetPerformanceCounterProperties (&Start
, &End
);
1220 Cycle
= End
- Start
;
1225 CurrentTime
= GetPerformanceCounter();
1226 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1233 *TotalTime
+= Delta
;
1234 *PreviousTime
= CurrentTime
;
1235 if (*TotalTime
> Timeout
) {
1242 Helper function that waits until the finished AP count reaches the specified
1243 limit, or the specified timeout elapses (whichever comes first).
1245 @param[in] CpuMpData Pointer to CPU MP Data.
1246 @param[in] FinishedApLimit The number of finished APs to wait for.
1247 @param[in] TimeLimit The number of microseconds to wait for.
1250 TimedWaitForApFinish (
1251 IN CPU_MP_DATA
*CpuMpData
,
1252 IN UINT32 FinishedApLimit
,
1257 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1258 // "infinity", so check for (TimeLimit == 0) explicitly.
1260 if (TimeLimit
== 0) {
1264 CpuMpData
->TotalTime
= 0;
1265 CpuMpData
->ExpectedTime
= CalculateTimeout (
1267 &CpuMpData
->CurrentTime
1269 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1271 &CpuMpData
->CurrentTime
,
1272 &CpuMpData
->TotalTime
,
1273 CpuMpData
->ExpectedTime
1278 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1281 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1284 DivU64x64Remainder (
1285 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1286 GetPerformanceCounterProperties (NULL
, NULL
),
1294 Reset an AP to Idle state.
1296 Any task being executed by the AP will be aborted and the AP
1297 will be waiting for a new task in Wait-For-SIPI state.
1299 @param[in] ProcessorNumber The handle number of processor.
1302 ResetProcessorToIdleState (
1303 IN UINTN ProcessorNumber
1306 CPU_MP_DATA
*CpuMpData
;
1308 CpuMpData
= GetCpuMpData ();
1310 CpuMpData
->InitFlag
= ApInitReconfig
;
1311 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1312 while (CpuMpData
->FinishedCount
< 1) {
1315 CpuMpData
->InitFlag
= ApInitDone
;
1317 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1321 Searches for the next waiting AP.
1323 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1325 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1327 @retval EFI_SUCCESS The next waiting AP has been found.
1328 @retval EFI_NOT_FOUND No waiting AP exists.
1332 GetNextWaitingProcessorNumber (
1333 OUT UINTN
*NextProcessorNumber
1336 UINTN ProcessorNumber
;
1337 CPU_MP_DATA
*CpuMpData
;
1339 CpuMpData
= GetCpuMpData ();
1341 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1342 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1343 *NextProcessorNumber
= ProcessorNumber
;
1348 return EFI_NOT_FOUND
;
1351 /** Checks status of specified AP.
1353 This function checks whether the specified AP has finished the task assigned
1354 by StartupThisAP(), and whether timeout expires.
1356 @param[in] ProcessorNumber The handle number of processor.
1358 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1359 @retval EFI_TIMEOUT The timeout expires.
1360 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1364 IN UINTN ProcessorNumber
1367 CPU_MP_DATA
*CpuMpData
;
1368 CPU_AP_DATA
*CpuData
;
1370 CpuMpData
= GetCpuMpData ();
1371 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1374 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1375 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1376 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1379 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1381 if (GetApState(CpuData
) == CpuStateFinished
) {
1382 if (CpuData
->Finished
!= NULL
) {
1383 *(CpuData
->Finished
) = TRUE
;
1385 SetApState (CpuData
, CpuStateIdle
);
1389 // If timeout expires for StartupThisAP(), report timeout.
1391 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1392 if (CpuData
->Finished
!= NULL
) {
1393 *(CpuData
->Finished
) = FALSE
;
1396 // Reset failed AP to idle state
1398 ResetProcessorToIdleState (ProcessorNumber
);
1403 return EFI_NOT_READY
;
1407 Checks status of all APs.
1409 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1410 and whether timeout expires.
1412 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1413 @retval EFI_TIMEOUT The timeout expires.
1414 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1421 UINTN ProcessorNumber
;
1422 UINTN NextProcessorNumber
;
1425 CPU_MP_DATA
*CpuMpData
;
1426 CPU_AP_DATA
*CpuData
;
1428 CpuMpData
= GetCpuMpData ();
1430 NextProcessorNumber
= 0;
1433 // Go through all APs that are responsible for the StartupAllAPs().
1435 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1436 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1440 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1442 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1443 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1444 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1446 if (GetApState(CpuData
) == CpuStateFinished
) {
1447 CpuMpData
->RunningCount
--;
1448 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1449 SetApState(CpuData
, CpuStateIdle
);
1452 // If in Single Thread mode, then search for the next waiting AP for execution.
1454 if (CpuMpData
->SingleThread
) {
1455 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1457 if (!EFI_ERROR (Status
)) {
1461 (UINT32
) NextProcessorNumber
,
1462 CpuMpData
->Procedure
,
1463 CpuMpData
->ProcArguments
,
1472 // If all APs finish, return EFI_SUCCESS.
1474 if (CpuMpData
->RunningCount
== 0) {
1479 // If timeout expires, report timeout.
1482 &CpuMpData
->CurrentTime
,
1483 &CpuMpData
->TotalTime
,
1484 CpuMpData
->ExpectedTime
)
1487 // If FailedCpuList is not NULL, record all failed APs in it.
1489 if (CpuMpData
->FailedCpuList
!= NULL
) {
1490 *CpuMpData
->FailedCpuList
=
1491 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1492 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1496 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1498 // Check whether this processor is responsible for StartupAllAPs().
1500 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1502 // Reset failed APs to idle state
1504 ResetProcessorToIdleState (ProcessorNumber
);
1505 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1506 if (CpuMpData
->FailedCpuList
!= NULL
) {
1507 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1511 if (CpuMpData
->FailedCpuList
!= NULL
) {
1512 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1516 return EFI_NOT_READY
;
1520 MP Initialize Library initialization.
1522 This service will allocate AP reset vector and wakeup all APs to do APs
1525 This service must be invoked before all other MP Initialize Library
1526 service are invoked.
1528 @retval EFI_SUCCESS MP initialization succeeds.
1529 @retval Others MP initialization fails.
1534 MpInitLibInitialize (
1538 CPU_MP_DATA
*OldCpuMpData
;
1539 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1540 UINT32 MaxLogicalProcessorNumber
;
1542 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1543 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1545 UINT32 MonitorFilterSize
;
1548 CPU_MP_DATA
*CpuMpData
;
1550 UINT8
*MonitorBuffer
;
1552 UINTN ApResetVectorSize
;
1553 UINTN BackupBufferAddr
;
1555 VOID
*MicrocodePatchInRam
;
1557 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1558 if (OldCpuMpData
== NULL
) {
1559 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1561 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1563 ASSERT (MaxLogicalProcessorNumber
!= 0);
1565 AsmGetAddressMap (&AddressMap
);
1566 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1567 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1568 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1571 // Save BSP's Control registers for APs.
1573 SaveVolatileRegisters (&VolatileRegisters
);
1575 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1576 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1577 BufferSize
+= ApResetVectorSize
;
1578 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1579 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1580 BufferSize
+= sizeof (CPU_MP_DATA
);
1581 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1582 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1583 ASSERT (MpBuffer
!= NULL
);
1584 ZeroMem (MpBuffer
, BufferSize
);
1585 Buffer
= (UINTN
) MpBuffer
;
1588 // The layout of the Buffer is as below:
1590 // +--------------------+ <-- Buffer
1592 // +--------------------+ <-- MonitorBuffer
1593 // AP Monitor Filters (N)
1594 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1596 // +--------------------+
1598 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1599 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1600 // +--------------------+ <-- CpuMpData
1602 // +--------------------+ <-- CpuMpData->CpuData
1604 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1605 // CPU_INFO_IN_HOB (N)
1606 // +--------------------+
1608 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1609 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1610 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1611 CpuMpData
= (CPU_MP_DATA
*) (ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1612 CpuMpData
->Buffer
= Buffer
;
1613 CpuMpData
->CpuApStackSize
= ApStackSize
;
1614 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1615 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1616 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1617 CpuMpData
->CpuCount
= 1;
1618 CpuMpData
->BspNumber
= 0;
1619 CpuMpData
->WaitEvent
= NULL
;
1620 CpuMpData
->SwitchBspFlag
= FALSE
;
1621 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1622 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1623 if (OldCpuMpData
== NULL
) {
1624 CpuMpData
->MicrocodePatchRegionSize
= PcdGet64 (PcdCpuMicrocodePatchRegionSize
);
1626 // If platform has more than one CPU, relocate microcode to memory to reduce
1627 // loading microcode time.
1629 MicrocodePatchInRam
= NULL
;
1630 if (MaxLogicalProcessorNumber
> 1) {
1631 MicrocodePatchInRam
= AllocatePages (
1633 (UINTN
)CpuMpData
->MicrocodePatchRegionSize
1637 if (MicrocodePatchInRam
== NULL
) {
1639 // there is only one processor, or no microcode patch is available, or
1640 // memory allocation failed
1642 CpuMpData
->MicrocodePatchAddress
= PcdGet64 (PcdCpuMicrocodePatchAddress
);
1645 // there are multiple processors, and a microcode patch is available, and
1646 // memory allocation succeeded
1649 MicrocodePatchInRam
,
1650 (VOID
*)(UINTN
)PcdGet64 (PcdCpuMicrocodePatchAddress
),
1651 (UINTN
)CpuMpData
->MicrocodePatchRegionSize
1653 CpuMpData
->MicrocodePatchAddress
= (UINTN
)MicrocodePatchInRam
;
1656 CpuMpData
->MicrocodePatchRegionSize
= OldCpuMpData
->MicrocodePatchRegionSize
;
1657 CpuMpData
->MicrocodePatchAddress
= OldCpuMpData
->MicrocodePatchAddress
;
1659 InitializeSpinLock(&CpuMpData
->MpLock
);
1662 // Make sure no memory usage outside of the allocated buffer.
1664 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1665 Buffer
+ BufferSize
);
1668 // Duplicate BSP's IDT to APs.
1669 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1671 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1672 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1674 // Don't pass BSP's TR to APs to avoid AP init failure.
1676 VolatileRegisters
.Tr
= 0;
1677 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1679 // Set BSP basic information
1681 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1683 // Save assembly code information
1685 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1687 // Finally set AP loop mode
1689 CpuMpData
->ApLoopMode
= ApLoopMode
;
1690 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1692 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1695 // Set up APs wakeup signal buffer
1697 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1698 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1699 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1702 // Load Microcode on BSP
1704 MicrocodeDetect (CpuMpData
, TRUE
);
1706 // Store BSP's MTRR setting
1708 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1710 // Enable the local APIC for Virtual Wire Mode.
1712 ProgramVirtualWireMode ();
1714 if (OldCpuMpData
== NULL
) {
1715 if (MaxLogicalProcessorNumber
> 1) {
1717 // Wakeup all APs and calculate the processor count in system
1719 CollectProcessorCount (CpuMpData
);
1723 // APs have been wakeup before, just get the CPU Information
1726 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1727 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1728 CpuMpData
->InitFlag
= ApInitReconfig
;
1729 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1730 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1731 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1732 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1733 if (CpuInfoInHob
[Index
].InitialApicId
>= 255 || Index
> 254) {
1734 CpuMpData
->X2ApicEnable
= TRUE
;
1736 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1737 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1738 CopyMem (&CpuMpData
->CpuData
[Index
].VolatileRegisters
, &VolatileRegisters
, sizeof (CPU_VOLATILE_REGISTERS
));
1740 if (MaxLogicalProcessorNumber
> 1) {
1742 // Wakeup APs to do some AP initialize sync
1744 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1746 // Wait for all APs finished initialization
1748 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1751 CpuMpData
->InitFlag
= ApInitDone
;
1752 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1753 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1759 // Initialize global data for MP support
1761 InitMpGlobalData (CpuMpData
);
1767 Gets detailed MP-related information on the requested processor at the
1768 instant this call is made. This service may only be called from the BSP.
1770 @param[in] ProcessorNumber The handle number of processor.
1771 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1772 the requested processor is deposited.
1773 @param[out] HealthData Return processor health data.
1775 @retval EFI_SUCCESS Processor information was returned.
1776 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1777 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1778 @retval EFI_NOT_FOUND The processor with the handle specified by
1779 ProcessorNumber does not exist in the platform.
1780 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1785 MpInitLibGetProcessorInfo (
1786 IN UINTN ProcessorNumber
,
1787 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1788 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1791 CPU_MP_DATA
*CpuMpData
;
1793 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1795 CpuMpData
= GetCpuMpData ();
1796 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1799 // Check whether caller processor is BSP
1801 MpInitLibWhoAmI (&CallerNumber
);
1802 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1803 return EFI_DEVICE_ERROR
;
1806 if (ProcessorInfoBuffer
== NULL
) {
1807 return EFI_INVALID_PARAMETER
;
1810 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1811 return EFI_NOT_FOUND
;
1814 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1815 ProcessorInfoBuffer
->StatusFlag
= 0;
1816 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1817 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1819 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1820 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1822 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1823 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1825 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1829 // Get processor location information
1831 GetProcessorLocationByApicId (
1832 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1833 &ProcessorInfoBuffer
->Location
.Package
,
1834 &ProcessorInfoBuffer
->Location
.Core
,
1835 &ProcessorInfoBuffer
->Location
.Thread
1838 if (HealthData
!= NULL
) {
1839 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1846 Worker function to switch the requested AP to be the BSP from that point onward.
1848 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1849 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1850 enabled AP. Otherwise, it will be disabled.
1852 @retval EFI_SUCCESS BSP successfully switched.
1853 @retval others Failed to switch BSP.
1858 IN UINTN ProcessorNumber
,
1859 IN BOOLEAN EnableOldBSP
1862 CPU_MP_DATA
*CpuMpData
;
1865 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1866 BOOLEAN OldInterruptState
;
1867 BOOLEAN OldTimerInterruptState
;
1870 // Save and Disable Local APIC timer interrupt
1872 OldTimerInterruptState
= GetApicTimerInterruptState ();
1873 DisableApicTimerInterrupt ();
1875 // Before send both BSP and AP to a procedure to exchange their roles,
1876 // interrupt must be disabled. This is because during the exchange role
1877 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1878 // be corrupted, since interrupt return address will be pushed to stack
1881 OldInterruptState
= SaveAndDisableInterrupts ();
1884 // Mask LINT0 & LINT1 for the old BSP
1886 DisableLvtInterrupts ();
1888 CpuMpData
= GetCpuMpData ();
1891 // Check whether caller processor is BSP
1893 MpInitLibWhoAmI (&CallerNumber
);
1894 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1895 return EFI_DEVICE_ERROR
;
1898 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1899 return EFI_NOT_FOUND
;
1903 // Check whether specified AP is disabled
1905 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1906 if (State
== CpuStateDisabled
) {
1907 return EFI_INVALID_PARAMETER
;
1911 // Check whether ProcessorNumber specifies the current BSP
1913 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1914 return EFI_INVALID_PARAMETER
;
1918 // Check whether specified AP is busy
1920 if (State
== CpuStateBusy
) {
1921 return EFI_NOT_READY
;
1924 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1925 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1926 CpuMpData
->SwitchBspFlag
= TRUE
;
1927 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1930 // Clear the BSP bit of MSR_IA32_APIC_BASE
1932 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1933 ApicBaseMsr
.Bits
.BSP
= 0;
1934 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1937 // Need to wakeUp AP (future BSP).
1939 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
1941 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1944 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1946 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1947 ApicBaseMsr
.Bits
.BSP
= 1;
1948 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1949 ProgramVirtualWireMode ();
1952 // Wait for old BSP finished AP task
1954 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1958 CpuMpData
->SwitchBspFlag
= FALSE
;
1960 // Set old BSP enable state
1962 if (!EnableOldBSP
) {
1963 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1965 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1968 // Save new BSP number
1970 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1973 // Restore interrupt state.
1975 SetInterruptState (OldInterruptState
);
1977 if (OldTimerInterruptState
) {
1978 EnableApicTimerInterrupt ();
1985 Worker function to let the caller enable or disable an AP from this point onward.
1986 This service may only be called from the BSP.
1988 @param[in] ProcessorNumber The handle number of AP.
1989 @param[in] EnableAP Specifies the new state for the processor for
1990 enabled, FALSE for disabled.
1991 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1992 the new health status of the AP.
1994 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1995 @retval others Failed to Enable/Disable AP.
1999 EnableDisableApWorker (
2000 IN UINTN ProcessorNumber
,
2001 IN BOOLEAN EnableAP
,
2002 IN UINT32
*HealthFlag OPTIONAL
2005 CPU_MP_DATA
*CpuMpData
;
2008 CpuMpData
= GetCpuMpData ();
2011 // Check whether caller processor is BSP
2013 MpInitLibWhoAmI (&CallerNumber
);
2014 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2015 return EFI_DEVICE_ERROR
;
2018 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2019 return EFI_INVALID_PARAMETER
;
2022 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2023 return EFI_NOT_FOUND
;
2027 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2029 ResetProcessorToIdleState (ProcessorNumber
);
2032 if (HealthFlag
!= NULL
) {
2033 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2034 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2041 This return the handle number for the calling processor. This service may be
2042 called from the BSP and APs.
2044 @param[out] ProcessorNumber Pointer to the handle number of AP.
2045 The range is from 0 to the total number of
2046 logical processors minus 1. The total number of
2047 logical processors can be retrieved by
2048 MpInitLibGetNumberOfProcessors().
2050 @retval EFI_SUCCESS The current processor handle number was returned
2052 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2053 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2059 OUT UINTN
*ProcessorNumber
2062 CPU_MP_DATA
*CpuMpData
;
2064 if (ProcessorNumber
== NULL
) {
2065 return EFI_INVALID_PARAMETER
;
2068 CpuMpData
= GetCpuMpData ();
2070 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2074 Retrieves the number of logical processor in the platform and the number of
2075 those logical processors that are enabled on this boot. This service may only
2076 be called from the BSP.
2078 @param[out] NumberOfProcessors Pointer to the total number of logical
2079 processors in the system, including the BSP
2081 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2082 processors that exist in system, including
2085 @retval EFI_SUCCESS The number of logical processors and enabled
2086 logical processors was retrieved.
2087 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2088 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2090 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2095 MpInitLibGetNumberOfProcessors (
2096 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2097 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2100 CPU_MP_DATA
*CpuMpData
;
2102 UINTN ProcessorNumber
;
2103 UINTN EnabledProcessorNumber
;
2106 CpuMpData
= GetCpuMpData ();
2108 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2109 return EFI_INVALID_PARAMETER
;
2113 // Check whether caller processor is BSP
2115 MpInitLibWhoAmI (&CallerNumber
);
2116 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2117 return EFI_DEVICE_ERROR
;
2120 ProcessorNumber
= CpuMpData
->CpuCount
;
2121 EnabledProcessorNumber
= 0;
2122 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2123 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2124 EnabledProcessorNumber
++;
2128 if (NumberOfProcessors
!= NULL
) {
2129 *NumberOfProcessors
= ProcessorNumber
;
2131 if (NumberOfEnabledProcessors
!= NULL
) {
2132 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2140 Worker function to execute a caller provided function on all enabled APs.
2142 @param[in] Procedure A pointer to the function to be run on
2143 enabled APs of the system.
2144 @param[in] SingleThread If TRUE, then all the enabled APs execute
2145 the function specified by Procedure one by
2146 one, in ascending order of processor handle
2147 number. If FALSE, then all the enabled APs
2148 execute the function specified by Procedure
2150 @param[in] ExcludeBsp Whether let BSP also trig this task.
2151 @param[in] WaitEvent The event created by the caller with CreateEvent()
2153 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2154 APs to return from Procedure, either for
2155 blocking or non-blocking mode.
2156 @param[in] ProcedureArgument The parameter passed into Procedure for
2158 @param[out] FailedCpuList If all APs finish successfully, then its
2159 content is set to NULL. If not all APs
2160 finish before timeout expires, then its
2161 content is set to address of the buffer
2162 holding handle numbers of the failed APs.
2164 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2165 the timeout expired.
2166 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2168 @retval others Failed to Startup all APs.
2172 StartupAllCPUsWorker (
2173 IN EFI_AP_PROCEDURE Procedure
,
2174 IN BOOLEAN SingleThread
,
2175 IN BOOLEAN ExcludeBsp
,
2176 IN EFI_EVENT WaitEvent OPTIONAL
,
2177 IN UINTN TimeoutInMicroseconds
,
2178 IN VOID
*ProcedureArgument OPTIONAL
,
2179 OUT UINTN
**FailedCpuList OPTIONAL
2183 CPU_MP_DATA
*CpuMpData
;
2184 UINTN ProcessorCount
;
2185 UINTN ProcessorNumber
;
2187 CPU_AP_DATA
*CpuData
;
2188 BOOLEAN HasEnabledAp
;
2191 CpuMpData
= GetCpuMpData ();
2193 if (FailedCpuList
!= NULL
) {
2194 *FailedCpuList
= NULL
;
2197 if (CpuMpData
->CpuCount
== 1 && ExcludeBsp
) {
2198 return EFI_NOT_STARTED
;
2201 if (Procedure
== NULL
) {
2202 return EFI_INVALID_PARAMETER
;
2206 // Check whether caller processor is BSP
2208 MpInitLibWhoAmI (&CallerNumber
);
2209 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2210 return EFI_DEVICE_ERROR
;
2216 CheckAndUpdateApsStatus ();
2218 ProcessorCount
= CpuMpData
->CpuCount
;
2219 HasEnabledAp
= FALSE
;
2221 // Check whether all enabled APs are idle.
2222 // If any enabled AP is not idle, return EFI_NOT_READY.
2224 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2225 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2226 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2227 ApState
= GetApState (CpuData
);
2228 if (ApState
!= CpuStateDisabled
) {
2229 HasEnabledAp
= TRUE
;
2230 if (ApState
!= CpuStateIdle
) {
2232 // If any enabled APs are busy, return EFI_NOT_READY.
2234 return EFI_NOT_READY
;
2240 if (!HasEnabledAp
&& ExcludeBsp
) {
2242 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2244 return EFI_NOT_STARTED
;
2247 CpuMpData
->RunningCount
= 0;
2248 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2249 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2250 CpuData
->Waiting
= FALSE
;
2251 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2252 if (CpuData
->State
== CpuStateIdle
) {
2254 // Mark this processor as responsible for current calling.
2256 CpuData
->Waiting
= TRUE
;
2257 CpuMpData
->RunningCount
++;
2262 CpuMpData
->Procedure
= Procedure
;
2263 CpuMpData
->ProcArguments
= ProcedureArgument
;
2264 CpuMpData
->SingleThread
= SingleThread
;
2265 CpuMpData
->FinishedCount
= 0;
2266 CpuMpData
->FailedCpuList
= FailedCpuList
;
2267 CpuMpData
->ExpectedTime
= CalculateTimeout (
2268 TimeoutInMicroseconds
,
2269 &CpuMpData
->CurrentTime
2271 CpuMpData
->TotalTime
= 0;
2272 CpuMpData
->WaitEvent
= WaitEvent
;
2274 if (!SingleThread
) {
2275 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2277 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2278 if (ProcessorNumber
== CallerNumber
) {
2281 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2282 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2292 Procedure (ProcedureArgument
);
2295 Status
= EFI_SUCCESS
;
2296 if (WaitEvent
== NULL
) {
2298 Status
= CheckAllAPs ();
2299 } while (Status
== EFI_NOT_READY
);
2306 Worker function to let the caller get one enabled AP to execute a caller-provided
2309 @param[in] Procedure A pointer to the function to be run on
2310 enabled APs of the system.
2311 @param[in] ProcessorNumber The handle number of the AP.
2312 @param[in] WaitEvent The event created by the caller with CreateEvent()
2314 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2315 APs to return from Procedure, either for
2316 blocking or non-blocking mode.
2317 @param[in] ProcedureArgument The parameter passed into Procedure for
2319 @param[out] Finished If AP returns from Procedure before the
2320 timeout expires, its content is set to TRUE.
2321 Otherwise, the value is set to FALSE.
2323 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2324 the timeout expires.
2325 @retval others Failed to Startup AP.
2329 StartupThisAPWorker (
2330 IN EFI_AP_PROCEDURE Procedure
,
2331 IN UINTN ProcessorNumber
,
2332 IN EFI_EVENT WaitEvent OPTIONAL
,
2333 IN UINTN TimeoutInMicroseconds
,
2334 IN VOID
*ProcedureArgument OPTIONAL
,
2335 OUT BOOLEAN
*Finished OPTIONAL
2339 CPU_MP_DATA
*CpuMpData
;
2340 CPU_AP_DATA
*CpuData
;
2343 CpuMpData
= GetCpuMpData ();
2345 if (Finished
!= NULL
) {
2350 // Check whether caller processor is BSP
2352 MpInitLibWhoAmI (&CallerNumber
);
2353 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2354 return EFI_DEVICE_ERROR
;
2358 // Check whether processor with the handle specified by ProcessorNumber exists
2360 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2361 return EFI_NOT_FOUND
;
2365 // Check whether specified processor is BSP
2367 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2368 return EFI_INVALID_PARAMETER
;
2372 // Check parameter Procedure
2374 if (Procedure
== NULL
) {
2375 return EFI_INVALID_PARAMETER
;
2381 CheckAndUpdateApsStatus ();
2384 // Check whether specified AP is disabled
2386 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2387 return EFI_INVALID_PARAMETER
;
2391 // If WaitEvent is not NULL, execute in non-blocking mode.
2392 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2393 // CheckAPsStatus() will check completion and timeout periodically.
2395 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2396 CpuData
->WaitEvent
= WaitEvent
;
2397 CpuData
->Finished
= Finished
;
2398 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2399 CpuData
->TotalTime
= 0;
2401 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2404 // If WaitEvent is NULL, execute in blocking mode.
2405 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2407 Status
= EFI_SUCCESS
;
2408 if (WaitEvent
== NULL
) {
2410 Status
= CheckThisAP (ProcessorNumber
);
2411 } while (Status
== EFI_NOT_READY
);
2418 Get pointer to CPU MP Data structure from GUIDed HOB.
2420 @return The pointer to CPU MP Data structure.
2423 GetCpuMpDataFromGuidedHob (
2427 EFI_HOB_GUID_TYPE
*GuidHob
;
2429 CPU_MP_DATA
*CpuMpData
;
2432 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2433 if (GuidHob
!= NULL
) {
2434 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2435 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2441 This service executes a caller provided function on all enabled CPUs.
2443 @param[in] Procedure A pointer to the function to be run on
2444 enabled APs of the system. See type
2446 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2447 APs to return from Procedure, either for
2448 blocking or non-blocking mode. Zero means
2449 infinity. TimeoutInMicroseconds is ignored
2451 @param[in] ProcedureArgument The parameter passed into Procedure for
2454 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2455 the timeout expired.
2456 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2457 to all enabled CPUs.
2458 @retval EFI_DEVICE_ERROR Caller processor is AP.
2459 @retval EFI_NOT_READY Any enabled APs are busy.
2460 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2461 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2462 all enabled APs have finished.
2463 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2468 MpInitLibStartupAllCPUs (
2469 IN EFI_AP_PROCEDURE Procedure
,
2470 IN UINTN TimeoutInMicroseconds
,
2471 IN VOID
*ProcedureArgument OPTIONAL
2474 return StartupAllCPUsWorker (
2479 TimeoutInMicroseconds
,