2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2020, 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
;
402 UINTN ProcessorNumber
;
405 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
406 Status
= GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
407 ASSERT_EFI_ERROR (Status
);
409 // Load microcode on AP
411 MicrocodeDetect (CpuMpData
, ProcessorNumber
);
413 // Sync BSP's MTRR table to AP
415 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
419 Find the current Processor number by APIC ID.
421 @param[in] CpuMpData Pointer to PEI CPU MP Data
422 @param[out] ProcessorNumber Return the pocessor number found
424 @retval EFI_SUCCESS ProcessorNumber is found and returned.
425 @retval EFI_NOT_FOUND ProcessorNumber is not found.
429 IN CPU_MP_DATA
*CpuMpData
,
430 OUT UINTN
*ProcessorNumber
433 UINTN TotalProcessorNumber
;
435 CPU_INFO_IN_HOB
*CpuInfoInHob
;
436 UINT32 CurrentApicId
;
438 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
440 TotalProcessorNumber
= CpuMpData
->CpuCount
;
441 CurrentApicId
= GetApicId ();
442 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
443 if (CpuInfoInHob
[Index
].ApicId
== CurrentApicId
) {
444 *ProcessorNumber
= Index
;
449 return EFI_NOT_FOUND
;
453 This function will get CPU count in the system.
455 @param[in] CpuMpData Pointer to PEI CPU MP Data
457 @return CPU count detected
460 CollectProcessorCount (
461 IN CPU_MP_DATA
*CpuMpData
465 CPU_INFO_IN_HOB
*CpuInfoInHob
;
469 // Send 1st broadcast IPI to APs to wakeup APs
471 CpuMpData
->InitFlag
= ApInitConfig
;
472 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
, TRUE
);
473 CpuMpData
->InitFlag
= ApInitDone
;
474 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
476 // Wait for all APs finished the initialization
478 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
484 // Enable x2APIC mode if
485 // 1. Number of CPU is greater than 255; or
486 // 2. There are any logical processors reporting an Initial APIC ID of 255 or greater.
489 if (CpuMpData
->CpuCount
> 255) {
491 // If there are more than 255 processor found, force to enable X2APIC
495 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
496 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
497 if (CpuInfoInHob
[Index
].InitialApicId
>= 0xFF) {
505 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
507 // Wakeup all APs to enable x2APIC mode
509 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
, TRUE
);
511 // Wait for all known APs finished
513 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
517 // Enable x2APIC on BSP
519 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
521 // Set BSP/Aps state to IDLE
523 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
524 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
527 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
529 // Sort BSP/Aps by CPU APIC ID in ascending order
531 SortApicId (CpuMpData
);
533 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
535 return CpuMpData
->CpuCount
;
539 Initialize CPU AP Data when AP is wakeup at the first time.
541 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
542 @param[in] ProcessorNumber The handle number of processor
543 @param[in] BistData Processor BIST data
544 @param[in] ApTopOfStack Top of AP stack
549 IN OUT CPU_MP_DATA
*CpuMpData
,
550 IN UINTN ProcessorNumber
,
552 IN UINT64 ApTopOfStack
555 CPU_INFO_IN_HOB
*CpuInfoInHob
;
556 MSR_IA32_PLATFORM_ID_REGISTER PlatformIdMsr
;
558 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
559 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
560 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
561 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
562 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
564 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
565 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
567 PlatformIdMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_PLATFORM_ID
);
568 CpuMpData
->CpuData
[ProcessorNumber
].PlatformId
= (UINT8
) PlatformIdMsr
.Bits
.PlatformId
;
572 &CpuMpData
->CpuData
[ProcessorNumber
].ProcessorSignature
,
578 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
579 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
583 This function will be called from AP reset code if BSP uses WakeUpAP.
585 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
586 @param[in] ApIndex Number of current executing AP
591 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
595 CPU_MP_DATA
*CpuMpData
;
596 UINTN ProcessorNumber
;
597 EFI_AP_PROCEDURE Procedure
;
600 volatile UINT32
*ApStartupSignalBuffer
;
601 CPU_INFO_IN_HOB
*CpuInfoInHob
;
603 UINTN CurrentApicMode
;
606 // AP finished assembly code and begin to execute C code
608 CpuMpData
= ExchangeInfo
->CpuMpData
;
611 // AP's local APIC settings will be lost after received INIT IPI
612 // We need to re-initialize them at here
614 ProgramVirtualWireMode ();
616 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
618 DisableLvtInterrupts ();
619 SyncLocalApicTimerSetting (CpuMpData
);
621 CurrentApicMode
= GetApicMode ();
623 if (CpuMpData
->InitFlag
== ApInitConfig
) {
627 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
628 ProcessorNumber
= ApIndex
;
630 // This is first time AP wakeup, get BIST information from AP stack
632 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
633 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
635 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
636 // to initialize AP in InitConfig path.
637 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
639 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
640 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
641 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
643 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
646 // Execute AP function if AP is ready
648 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
650 // Clear AP start-up signal when AP waken up
652 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
653 InterlockedCompareExchange32 (
654 (UINT32
*) ApStartupSignalBuffer
,
658 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
660 // Restore AP's volatile registers saved
662 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
665 // The CPU driver might not flush TLB for APs on spot after updating
666 // page attributes. AP in mwait loop mode needs to take care of it when
672 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
673 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
674 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
675 if (Procedure
!= NULL
) {
676 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
678 // Enable source debugging on AP function
682 // Invoke AP function here
684 Procedure (Parameter
);
685 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
686 if (CpuMpData
->SwitchBspFlag
) {
688 // Re-get the processor number due to BSP/AP maybe exchange in AP function
690 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
691 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
692 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
693 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
694 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
696 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
697 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
698 if (CurrentApicMode
!= GetApicMode ()) {
700 // If APIC mode change happened during AP function execution,
701 // we do not support APIC ID value changed.
707 // Re-get the CPU APICID and Initial APICID if they are changed
709 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
710 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
715 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
720 // AP finished executing C code
722 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
725 // Place AP is specified loop mode
727 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
729 // Save AP volatile registers
731 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
733 // Place AP in HLT-loop
736 DisableInterrupts ();
742 DisableInterrupts ();
743 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
745 // Place AP in MWAIT-loop
747 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
748 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
750 // Check AP start-up signal again.
751 // If AP start-up signal is not set, place AP into
752 // the specified C-state
754 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
756 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
758 // Place AP in Run-loop
766 // If AP start-up signal is written, AP is waken up
767 // otherwise place AP in loop again
769 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
777 Wait for AP wakeup and write AP start-up signal till AP is waken up.
779 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
783 IN
volatile UINT32
*ApStartupSignalBuffer
787 // If AP is waken up, StartupApSignal should be cleared.
788 // Otherwise, write StartupApSignal again till AP waken up.
790 while (InterlockedCompareExchange32 (
791 (UINT32
*) ApStartupSignalBuffer
,
800 This function will fill the exchange info structure.
802 @param[in] CpuMpData Pointer to CPU MP Data
806 FillExchangeInfoData (
807 IN CPU_MP_DATA
*CpuMpData
810 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
812 IA32_SEGMENT_DESCRIPTOR
*Selector
;
815 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
816 ExchangeInfo
->Lock
= 0;
817 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
818 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
819 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
820 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
822 ExchangeInfo
->CodeSegment
= AsmReadCs ();
823 ExchangeInfo
->DataSegment
= AsmReadDs ();
825 ExchangeInfo
->Cr3
= AsmReadCr3 ();
827 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
828 ExchangeInfo
->ApIndex
= 0;
829 ExchangeInfo
->NumApsExecuting
= 0;
830 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
831 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
832 ExchangeInfo
->CpuMpData
= CpuMpData
;
834 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
836 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
839 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
840 // to determin whether 5-Level Paging is enabled.
841 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
842 // current system setting.
843 // Using latter way is simpler because it also eliminates the needs to
844 // check whether platform wants to enable it.
846 Cr4
.UintN
= AsmReadCr4 ();
847 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
) (Cr4
.Bits
.LA57
== 1);
848 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
851 // Get the BSP's data of GDT and IDT
853 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
854 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
857 // Find a 32-bit code segment
859 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
860 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
862 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
863 ExchangeInfo
->ModeTransitionSegment
=
864 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
868 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
872 // Copy all 32-bit code and 64-bit code into memory with type of
873 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
875 if (CpuMpData
->WakeupBufferHigh
!= 0) {
876 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
877 CpuMpData
->AddressMap
.ModeTransitionOffset
;
879 (VOID
*)CpuMpData
->WakeupBufferHigh
,
880 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
881 CpuMpData
->AddressMap
.ModeTransitionOffset
,
885 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
887 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
888 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
891 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
892 (UINT32
)ExchangeInfo
->ModeOffset
-
893 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
894 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
898 Helper function that waits until the finished AP count reaches the specified
899 limit, or the specified timeout elapses (whichever comes first).
901 @param[in] CpuMpData Pointer to CPU MP Data.
902 @param[in] FinishedApLimit The number of finished APs to wait for.
903 @param[in] TimeLimit The number of microseconds to wait for.
906 TimedWaitForApFinish (
907 IN CPU_MP_DATA
*CpuMpData
,
908 IN UINT32 FinishedApLimit
,
913 Get available system memory below 1MB by specified size.
915 @param[in] CpuMpData The pointer to CPU MP Data structure.
918 BackupAndPrepareWakeupBuffer(
919 IN CPU_MP_DATA
*CpuMpData
923 (VOID
*) CpuMpData
->BackupBuffer
,
924 (VOID
*) CpuMpData
->WakeupBuffer
,
925 CpuMpData
->BackupBufferSize
928 (VOID
*) CpuMpData
->WakeupBuffer
,
929 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
930 CpuMpData
->AddressMap
.RendezvousFunnelSize
935 Restore wakeup buffer data.
937 @param[in] CpuMpData The pointer to CPU MP Data structure.
941 IN CPU_MP_DATA
*CpuMpData
945 (VOID
*) CpuMpData
->WakeupBuffer
,
946 (VOID
*) CpuMpData
->BackupBuffer
,
947 CpuMpData
->BackupBufferSize
952 Allocate reset vector buffer.
954 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
957 AllocateResetVector (
958 IN OUT CPU_MP_DATA
*CpuMpData
961 UINTN ApResetVectorSize
;
963 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
964 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
965 sizeof (MP_CPU_EXCHANGE_INFO
);
967 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
968 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
969 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
970 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
971 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
972 CpuMpData
->AddressMap
.ModeTransitionOffset
975 BackupAndPrepareWakeupBuffer (CpuMpData
);
979 Free AP reset vector buffer.
981 @param[in] CpuMpData The pointer to CPU MP Data structure.
985 IN CPU_MP_DATA
*CpuMpData
988 RestoreWakeupBuffer (CpuMpData
);
992 This function will be called by BSP to wakeup AP.
994 @param[in] CpuMpData Pointer to CPU MP Data
995 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
996 FALSE: Send IPI to AP by ApicId
997 @param[in] ProcessorNumber The handle number of specified processor
998 @param[in] Procedure The function to be invoked by AP
999 @param[in] ProcedureArgument The argument to be passed into AP function
1000 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
1004 IN CPU_MP_DATA
*CpuMpData
,
1005 IN BOOLEAN Broadcast
,
1006 IN UINTN ProcessorNumber
,
1007 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
1008 IN VOID
*ProcedureArgument
, OPTIONAL
1009 IN BOOLEAN WakeUpDisabledAps
1012 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
1014 CPU_AP_DATA
*CpuData
;
1015 BOOLEAN ResetVectorRequired
;
1016 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1018 CpuMpData
->FinishedCount
= 0;
1019 ResetVectorRequired
= FALSE
;
1021 if (CpuMpData
->WakeUpByInitSipiSipi
||
1022 CpuMpData
->InitFlag
!= ApInitDone
) {
1023 ResetVectorRequired
= TRUE
;
1024 AllocateResetVector (CpuMpData
);
1025 FillExchangeInfoData (CpuMpData
);
1026 SaveLocalApicTimerSetting (CpuMpData
);
1029 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1031 // Get AP target C-state each time when waking up AP,
1032 // for it maybe updated by platform again
1034 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1037 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1040 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1041 if (Index
!= CpuMpData
->BspNumber
) {
1042 CpuData
= &CpuMpData
->CpuData
[Index
];
1044 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1045 // the AP procedure will be skipped for disabled AP because AP state
1046 // is not CpuStateReady.
1048 if (GetApState (CpuData
) == CpuStateDisabled
&& !WakeUpDisabledAps
) {
1052 CpuData
->ApFunction
= (UINTN
) Procedure
;
1053 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1054 SetApState (CpuData
, CpuStateReady
);
1055 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1056 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1060 if (ResetVectorRequired
) {
1064 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1066 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1067 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber
) > 0) {
1069 // The AP enumeration algorithm below is suitable only when the
1070 // platform can tell us the *exact* boot CPU count in advance.
1072 // The wait below finishes only when the detected AP count reaches
1073 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1074 // takes. If at least one AP fails to check in (meaning a platform
1075 // hardware bug), the detection hangs forever, by design. If the actual
1076 // boot CPU count in the system is higher than
1077 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1078 // misconfiguration), then some APs may complete initialization after
1079 // the wait finishes, and cause undefined behavior.
1081 TimedWaitForApFinish (
1083 PcdGet32 (PcdCpuBootLogicalProcessorNumber
) - 1,
1084 MAX_UINT32
// approx. 71 minutes
1088 // The AP enumeration algorithm below is suitable for two use cases.
1090 // (1) The check-in time for an individual AP is bounded, and APs run
1091 // through their initialization routines strongly concurrently. In
1092 // particular, the number of concurrently running APs
1093 // ("NumApsExecuting") is never expected to fall to zero
1094 // *temporarily* -- it is expected to fall to zero only when all
1095 // APs have checked-in.
1097 // In this case, the platform is supposed to set
1098 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1099 // enough for one AP to start initialization). The timeout will be
1100 // reached soon, and remaining APs are collected by watching
1101 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1102 // mid-process, while some APs have not completed initialization,
1103 // the behavior is undefined.
1105 // (2) The check-in time for an individual AP is unbounded, and/or APs
1106 // may complete their initializations widely spread out. In
1107 // particular, some APs may finish initialization before some APs
1110 // In this case, the platform is supposed to set
1111 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1112 // enumeration will always take that long (except when the boot CPU
1113 // count happens to be maximal, that is,
1114 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1115 // check-in before the timeout, and NumApsExecuting is assumed zero
1116 // at timeout. APs that miss the time-out may cause undefined
1119 TimedWaitForApFinish (
1121 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1122 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1125 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1131 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1133 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1134 CpuData
= &CpuMpData
->CpuData
[Index
];
1135 if (Index
!= CpuMpData
->BspNumber
) {
1136 WaitApWakeup (CpuData
->StartupApSignal
);
1141 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1142 CpuData
->ApFunction
= (UINTN
) Procedure
;
1143 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1144 SetApState (CpuData
, CpuStateReady
);
1146 // Wakeup specified AP
1148 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1149 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1150 if (ResetVectorRequired
) {
1151 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1153 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1154 (UINT32
) ExchangeInfo
->BufferStart
1158 // Wait specified AP waken up
1160 WaitApWakeup (CpuData
->StartupApSignal
);
1163 if (ResetVectorRequired
) {
1164 FreeResetVector (CpuMpData
);
1168 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1169 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1170 // S3SmmInitDone Ppi.
1172 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1176 Calculate timeout value and return the current performance counter value.
1178 Calculate the number of performance counter ticks required for a timeout.
1179 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1182 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1183 @param[out] CurrentTime Returns the current value of the performance counter.
1185 @return Expected time stamp counter for timeout.
1186 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1192 IN UINTN TimeoutInMicroseconds
,
1193 OUT UINT64
*CurrentTime
1196 UINT64 TimeoutInSeconds
;
1197 UINT64 TimestampCounterFreq
;
1200 // Read the current value of the performance counter
1202 *CurrentTime
= GetPerformanceCounter ();
1205 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1208 if (TimeoutInMicroseconds
== 0) {
1213 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1216 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1219 // Check the potential overflow before calculate the number of ticks for the timeout value.
1221 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1223 // Convert microseconds into seconds if direct multiplication overflows
1225 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1227 // Assertion if the final tick count exceeds MAX_UINT64
1229 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1230 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1233 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1234 // it by 1,000,000, to get the number of ticks for the timeout value.
1238 TimestampCounterFreq
,
1239 TimeoutInMicroseconds
1247 Checks whether timeout expires.
1249 Check whether the number of elapsed performance counter ticks required for
1250 a timeout condition has been reached.
1251 If Timeout is zero, which means infinity, return value is always FALSE.
1253 @param[in, out] PreviousTime On input, the value of the performance counter
1254 when it was last read.
1255 On output, the current value of the performance
1257 @param[in] TotalTime The total amount of elapsed time in performance
1259 @param[in] Timeout The number of performance counter ticks required
1260 to reach a timeout condition.
1262 @retval TRUE A timeout condition has been reached.
1263 @retval FALSE A timeout condition has not been reached.
1268 IN OUT UINT64
*PreviousTime
,
1269 IN UINT64
*TotalTime
,
1282 GetPerformanceCounterProperties (&Start
, &End
);
1283 Cycle
= End
- Start
;
1288 CurrentTime
= GetPerformanceCounter();
1289 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1296 *TotalTime
+= Delta
;
1297 *PreviousTime
= CurrentTime
;
1298 if (*TotalTime
> Timeout
) {
1305 Helper function that waits until the finished AP count reaches the specified
1306 limit, or the specified timeout elapses (whichever comes first).
1308 @param[in] CpuMpData Pointer to CPU MP Data.
1309 @param[in] FinishedApLimit The number of finished APs to wait for.
1310 @param[in] TimeLimit The number of microseconds to wait for.
1313 TimedWaitForApFinish (
1314 IN CPU_MP_DATA
*CpuMpData
,
1315 IN UINT32 FinishedApLimit
,
1320 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1321 // "infinity", so check for (TimeLimit == 0) explicitly.
1323 if (TimeLimit
== 0) {
1327 CpuMpData
->TotalTime
= 0;
1328 CpuMpData
->ExpectedTime
= CalculateTimeout (
1330 &CpuMpData
->CurrentTime
1332 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1334 &CpuMpData
->CurrentTime
,
1335 &CpuMpData
->TotalTime
,
1336 CpuMpData
->ExpectedTime
1341 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1344 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1347 DivU64x64Remainder (
1348 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1349 GetPerformanceCounterProperties (NULL
, NULL
),
1357 Reset an AP to Idle state.
1359 Any task being executed by the AP will be aborted and the AP
1360 will be waiting for a new task in Wait-For-SIPI state.
1362 @param[in] ProcessorNumber The handle number of processor.
1365 ResetProcessorToIdleState (
1366 IN UINTN ProcessorNumber
1369 CPU_MP_DATA
*CpuMpData
;
1371 CpuMpData
= GetCpuMpData ();
1373 CpuMpData
->InitFlag
= ApInitReconfig
;
1374 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1375 while (CpuMpData
->FinishedCount
< 1) {
1378 CpuMpData
->InitFlag
= ApInitDone
;
1380 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1384 Searches for the next waiting AP.
1386 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1388 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1390 @retval EFI_SUCCESS The next waiting AP has been found.
1391 @retval EFI_NOT_FOUND No waiting AP exists.
1395 GetNextWaitingProcessorNumber (
1396 OUT UINTN
*NextProcessorNumber
1399 UINTN ProcessorNumber
;
1400 CPU_MP_DATA
*CpuMpData
;
1402 CpuMpData
= GetCpuMpData ();
1404 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1405 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1406 *NextProcessorNumber
= ProcessorNumber
;
1411 return EFI_NOT_FOUND
;
1414 /** Checks status of specified AP.
1416 This function checks whether the specified AP has finished the task assigned
1417 by StartupThisAP(), and whether timeout expires.
1419 @param[in] ProcessorNumber The handle number of processor.
1421 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1422 @retval EFI_TIMEOUT The timeout expires.
1423 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1427 IN UINTN ProcessorNumber
1430 CPU_MP_DATA
*CpuMpData
;
1431 CPU_AP_DATA
*CpuData
;
1433 CpuMpData
= GetCpuMpData ();
1434 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1437 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1438 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1439 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1442 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1444 if (GetApState(CpuData
) == CpuStateFinished
) {
1445 if (CpuData
->Finished
!= NULL
) {
1446 *(CpuData
->Finished
) = TRUE
;
1448 SetApState (CpuData
, CpuStateIdle
);
1452 // If timeout expires for StartupThisAP(), report timeout.
1454 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1455 if (CpuData
->Finished
!= NULL
) {
1456 *(CpuData
->Finished
) = FALSE
;
1459 // Reset failed AP to idle state
1461 ResetProcessorToIdleState (ProcessorNumber
);
1466 return EFI_NOT_READY
;
1470 Checks status of all APs.
1472 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1473 and whether timeout expires.
1475 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1476 @retval EFI_TIMEOUT The timeout expires.
1477 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1484 UINTN ProcessorNumber
;
1485 UINTN NextProcessorNumber
;
1488 CPU_MP_DATA
*CpuMpData
;
1489 CPU_AP_DATA
*CpuData
;
1491 CpuMpData
= GetCpuMpData ();
1493 NextProcessorNumber
= 0;
1496 // Go through all APs that are responsible for the StartupAllAPs().
1498 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1499 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1503 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1505 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1506 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1507 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1509 if (GetApState(CpuData
) == CpuStateFinished
) {
1510 CpuMpData
->RunningCount
--;
1511 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1512 SetApState(CpuData
, CpuStateIdle
);
1515 // If in Single Thread mode, then search for the next waiting AP for execution.
1517 if (CpuMpData
->SingleThread
) {
1518 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1520 if (!EFI_ERROR (Status
)) {
1524 (UINT32
) NextProcessorNumber
,
1525 CpuMpData
->Procedure
,
1526 CpuMpData
->ProcArguments
,
1535 // If all APs finish, return EFI_SUCCESS.
1537 if (CpuMpData
->RunningCount
== 0) {
1542 // If timeout expires, report timeout.
1545 &CpuMpData
->CurrentTime
,
1546 &CpuMpData
->TotalTime
,
1547 CpuMpData
->ExpectedTime
)
1550 // If FailedCpuList is not NULL, record all failed APs in it.
1552 if (CpuMpData
->FailedCpuList
!= NULL
) {
1553 *CpuMpData
->FailedCpuList
=
1554 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1555 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1559 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1561 // Check whether this processor is responsible for StartupAllAPs().
1563 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1565 // Reset failed APs to idle state
1567 ResetProcessorToIdleState (ProcessorNumber
);
1568 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1569 if (CpuMpData
->FailedCpuList
!= NULL
) {
1570 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1574 if (CpuMpData
->FailedCpuList
!= NULL
) {
1575 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1579 return EFI_NOT_READY
;
1583 MP Initialize Library initialization.
1585 This service will allocate AP reset vector and wakeup all APs to do APs
1588 This service must be invoked before all other MP Initialize Library
1589 service are invoked.
1591 @retval EFI_SUCCESS MP initialization succeeds.
1592 @retval Others MP initialization fails.
1597 MpInitLibInitialize (
1601 CPU_MP_DATA
*OldCpuMpData
;
1602 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1603 UINT32 MaxLogicalProcessorNumber
;
1605 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1606 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1608 UINT32 MonitorFilterSize
;
1611 CPU_MP_DATA
*CpuMpData
;
1613 UINT8
*MonitorBuffer
;
1615 UINTN ApResetVectorSize
;
1616 UINTN BackupBufferAddr
;
1619 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1620 if (OldCpuMpData
== NULL
) {
1621 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1623 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1625 ASSERT (MaxLogicalProcessorNumber
!= 0);
1627 AsmGetAddressMap (&AddressMap
);
1628 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1629 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1630 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1633 // Save BSP's Control registers for APs.
1635 SaveVolatileRegisters (&VolatileRegisters
);
1637 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1638 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1639 BufferSize
+= ApResetVectorSize
;
1640 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1641 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1642 BufferSize
+= sizeof (CPU_MP_DATA
);
1643 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1644 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1645 ASSERT (MpBuffer
!= NULL
);
1646 ZeroMem (MpBuffer
, BufferSize
);
1647 Buffer
= (UINTN
) MpBuffer
;
1650 // The layout of the Buffer is as below:
1652 // +--------------------+ <-- Buffer
1654 // +--------------------+ <-- MonitorBuffer
1655 // AP Monitor Filters (N)
1656 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1658 // +--------------------+
1660 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1661 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1662 // +--------------------+ <-- CpuMpData
1664 // +--------------------+ <-- CpuMpData->CpuData
1666 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1667 // CPU_INFO_IN_HOB (N)
1668 // +--------------------+
1670 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1671 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1672 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1673 CpuMpData
= (CPU_MP_DATA
*) (ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1674 CpuMpData
->Buffer
= Buffer
;
1675 CpuMpData
->CpuApStackSize
= ApStackSize
;
1676 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1677 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1678 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1679 CpuMpData
->CpuCount
= 1;
1680 CpuMpData
->BspNumber
= 0;
1681 CpuMpData
->WaitEvent
= NULL
;
1682 CpuMpData
->SwitchBspFlag
= FALSE
;
1683 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1684 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1685 if (OldCpuMpData
!= NULL
) {
1686 CpuMpData
->MicrocodePatchRegionSize
= OldCpuMpData
->MicrocodePatchRegionSize
;
1687 CpuMpData
->MicrocodePatchAddress
= OldCpuMpData
->MicrocodePatchAddress
;
1689 InitializeSpinLock(&CpuMpData
->MpLock
);
1692 // Make sure no memory usage outside of the allocated buffer.
1694 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1695 Buffer
+ BufferSize
);
1698 // Duplicate BSP's IDT to APs.
1699 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1701 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1702 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1704 // Don't pass BSP's TR to APs to avoid AP init failure.
1706 VolatileRegisters
.Tr
= 0;
1707 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1709 // Set BSP basic information
1711 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1713 // Save assembly code information
1715 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1717 // Finally set AP loop mode
1719 CpuMpData
->ApLoopMode
= ApLoopMode
;
1720 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1722 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1725 // Set up APs wakeup signal buffer
1727 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1728 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1729 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1732 // Enable the local APIC for Virtual Wire Mode.
1734 ProgramVirtualWireMode ();
1736 if (OldCpuMpData
== NULL
) {
1737 if (MaxLogicalProcessorNumber
> 1) {
1739 // Wakeup all APs and calculate the processor count in system
1741 CollectProcessorCount (CpuMpData
);
1745 // Load required microcode patches data into memory
1747 ShadowMicrocodeUpdatePatch (CpuMpData
);
1750 // APs have been wakeup before, just get the CPU Information
1753 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1754 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1755 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1756 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1757 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1758 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1759 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1760 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1761 CopyMem (&CpuMpData
->CpuData
[Index
].VolatileRegisters
, &VolatileRegisters
, sizeof (CPU_VOLATILE_REGISTERS
));
1766 // Detect and apply Microcode on BSP
1768 MicrocodeDetect (CpuMpData
, CpuMpData
->BspNumber
);
1770 // Store BSP's MTRR setting
1772 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1775 // Wakeup APs to do some AP initialize sync (Microcode & MTRR)
1777 if (CpuMpData
->CpuCount
> 1) {
1778 CpuMpData
->InitFlag
= ApInitReconfig
;
1779 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1781 // Wait for all APs finished initialization
1783 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1786 CpuMpData
->InitFlag
= ApInitDone
;
1787 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1788 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1793 // Initialize global data for MP support
1795 InitMpGlobalData (CpuMpData
);
1801 Gets detailed MP-related information on the requested processor at the
1802 instant this call is made. This service may only be called from the BSP.
1804 @param[in] ProcessorNumber The handle number of processor.
1805 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1806 the requested processor is deposited.
1807 @param[out] HealthData Return processor health data.
1809 @retval EFI_SUCCESS Processor information was returned.
1810 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1811 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1812 @retval EFI_NOT_FOUND The processor with the handle specified by
1813 ProcessorNumber does not exist in the platform.
1814 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1819 MpInitLibGetProcessorInfo (
1820 IN UINTN ProcessorNumber
,
1821 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1822 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1825 CPU_MP_DATA
*CpuMpData
;
1827 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1829 CpuMpData
= GetCpuMpData ();
1830 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1833 // Check whether caller processor is BSP
1835 MpInitLibWhoAmI (&CallerNumber
);
1836 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1837 return EFI_DEVICE_ERROR
;
1840 if (ProcessorInfoBuffer
== NULL
) {
1841 return EFI_INVALID_PARAMETER
;
1844 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1845 return EFI_NOT_FOUND
;
1848 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1849 ProcessorInfoBuffer
->StatusFlag
= 0;
1850 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1851 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1853 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1854 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1856 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1857 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1859 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1863 // Get processor location information
1865 GetProcessorLocationByApicId (
1866 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1867 &ProcessorInfoBuffer
->Location
.Package
,
1868 &ProcessorInfoBuffer
->Location
.Core
,
1869 &ProcessorInfoBuffer
->Location
.Thread
1872 if (HealthData
!= NULL
) {
1873 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1880 Worker function to switch the requested AP to be the BSP from that point onward.
1882 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1883 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1884 enabled AP. Otherwise, it will be disabled.
1886 @retval EFI_SUCCESS BSP successfully switched.
1887 @retval others Failed to switch BSP.
1892 IN UINTN ProcessorNumber
,
1893 IN BOOLEAN EnableOldBSP
1896 CPU_MP_DATA
*CpuMpData
;
1899 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1900 BOOLEAN OldInterruptState
;
1901 BOOLEAN OldTimerInterruptState
;
1904 // Save and Disable Local APIC timer interrupt
1906 OldTimerInterruptState
= GetApicTimerInterruptState ();
1907 DisableApicTimerInterrupt ();
1909 // Before send both BSP and AP to a procedure to exchange their roles,
1910 // interrupt must be disabled. This is because during the exchange role
1911 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1912 // be corrupted, since interrupt return address will be pushed to stack
1915 OldInterruptState
= SaveAndDisableInterrupts ();
1918 // Mask LINT0 & LINT1 for the old BSP
1920 DisableLvtInterrupts ();
1922 CpuMpData
= GetCpuMpData ();
1925 // Check whether caller processor is BSP
1927 MpInitLibWhoAmI (&CallerNumber
);
1928 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1929 return EFI_DEVICE_ERROR
;
1932 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1933 return EFI_NOT_FOUND
;
1937 // Check whether specified AP is disabled
1939 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1940 if (State
== CpuStateDisabled
) {
1941 return EFI_INVALID_PARAMETER
;
1945 // Check whether ProcessorNumber specifies the current BSP
1947 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1948 return EFI_INVALID_PARAMETER
;
1952 // Check whether specified AP is busy
1954 if (State
== CpuStateBusy
) {
1955 return EFI_NOT_READY
;
1958 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1959 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1960 CpuMpData
->SwitchBspFlag
= TRUE
;
1961 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1964 // Clear the BSP bit of MSR_IA32_APIC_BASE
1966 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1967 ApicBaseMsr
.Bits
.BSP
= 0;
1968 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1971 // Need to wakeUp AP (future BSP).
1973 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
1975 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1978 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1980 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1981 ApicBaseMsr
.Bits
.BSP
= 1;
1982 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1983 ProgramVirtualWireMode ();
1986 // Wait for old BSP finished AP task
1988 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1992 CpuMpData
->SwitchBspFlag
= FALSE
;
1994 // Set old BSP enable state
1996 if (!EnableOldBSP
) {
1997 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1999 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2002 // Save new BSP number
2004 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
2007 // Restore interrupt state.
2009 SetInterruptState (OldInterruptState
);
2011 if (OldTimerInterruptState
) {
2012 EnableApicTimerInterrupt ();
2019 Worker function to let the caller enable or disable an AP from this point onward.
2020 This service may only be called from the BSP.
2022 @param[in] ProcessorNumber The handle number of AP.
2023 @param[in] EnableAP Specifies the new state for the processor for
2024 enabled, FALSE for disabled.
2025 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2026 the new health status of the AP.
2028 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2029 @retval others Failed to Enable/Disable AP.
2033 EnableDisableApWorker (
2034 IN UINTN ProcessorNumber
,
2035 IN BOOLEAN EnableAP
,
2036 IN UINT32
*HealthFlag OPTIONAL
2039 CPU_MP_DATA
*CpuMpData
;
2042 CpuMpData
= GetCpuMpData ();
2045 // Check whether caller processor is BSP
2047 MpInitLibWhoAmI (&CallerNumber
);
2048 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2049 return EFI_DEVICE_ERROR
;
2052 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2053 return EFI_INVALID_PARAMETER
;
2056 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2057 return EFI_NOT_FOUND
;
2061 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2063 ResetProcessorToIdleState (ProcessorNumber
);
2066 if (HealthFlag
!= NULL
) {
2067 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2068 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2075 This return the handle number for the calling processor. This service may be
2076 called from the BSP and APs.
2078 @param[out] ProcessorNumber Pointer to the handle number of AP.
2079 The range is from 0 to the total number of
2080 logical processors minus 1. The total number of
2081 logical processors can be retrieved by
2082 MpInitLibGetNumberOfProcessors().
2084 @retval EFI_SUCCESS The current processor handle number was returned
2086 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2087 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2093 OUT UINTN
*ProcessorNumber
2096 CPU_MP_DATA
*CpuMpData
;
2098 if (ProcessorNumber
== NULL
) {
2099 return EFI_INVALID_PARAMETER
;
2102 CpuMpData
= GetCpuMpData ();
2104 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2108 Retrieves the number of logical processor in the platform and the number of
2109 those logical processors that are enabled on this boot. This service may only
2110 be called from the BSP.
2112 @param[out] NumberOfProcessors Pointer to the total number of logical
2113 processors in the system, including the BSP
2115 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2116 processors that exist in system, including
2119 @retval EFI_SUCCESS The number of logical processors and enabled
2120 logical processors was retrieved.
2121 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2122 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2124 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2129 MpInitLibGetNumberOfProcessors (
2130 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2131 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2134 CPU_MP_DATA
*CpuMpData
;
2136 UINTN ProcessorNumber
;
2137 UINTN EnabledProcessorNumber
;
2140 CpuMpData
= GetCpuMpData ();
2142 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2143 return EFI_INVALID_PARAMETER
;
2147 // Check whether caller processor is BSP
2149 MpInitLibWhoAmI (&CallerNumber
);
2150 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2151 return EFI_DEVICE_ERROR
;
2154 ProcessorNumber
= CpuMpData
->CpuCount
;
2155 EnabledProcessorNumber
= 0;
2156 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2157 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2158 EnabledProcessorNumber
++;
2162 if (NumberOfProcessors
!= NULL
) {
2163 *NumberOfProcessors
= ProcessorNumber
;
2165 if (NumberOfEnabledProcessors
!= NULL
) {
2166 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2174 Worker function to execute a caller provided function on all enabled APs.
2176 @param[in] Procedure A pointer to the function to be run on
2177 enabled APs of the system.
2178 @param[in] SingleThread If TRUE, then all the enabled APs execute
2179 the function specified by Procedure one by
2180 one, in ascending order of processor handle
2181 number. If FALSE, then all the enabled APs
2182 execute the function specified by Procedure
2184 @param[in] ExcludeBsp Whether let BSP also trig this task.
2185 @param[in] WaitEvent The event created by the caller with CreateEvent()
2187 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2188 APs to return from Procedure, either for
2189 blocking or non-blocking mode.
2190 @param[in] ProcedureArgument The parameter passed into Procedure for
2192 @param[out] FailedCpuList If all APs finish successfully, then its
2193 content is set to NULL. If not all APs
2194 finish before timeout expires, then its
2195 content is set to address of the buffer
2196 holding handle numbers of the failed APs.
2198 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2199 the timeout expired.
2200 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2202 @retval others Failed to Startup all APs.
2206 StartupAllCPUsWorker (
2207 IN EFI_AP_PROCEDURE Procedure
,
2208 IN BOOLEAN SingleThread
,
2209 IN BOOLEAN ExcludeBsp
,
2210 IN EFI_EVENT WaitEvent OPTIONAL
,
2211 IN UINTN TimeoutInMicroseconds
,
2212 IN VOID
*ProcedureArgument OPTIONAL
,
2213 OUT UINTN
**FailedCpuList OPTIONAL
2217 CPU_MP_DATA
*CpuMpData
;
2218 UINTN ProcessorCount
;
2219 UINTN ProcessorNumber
;
2221 CPU_AP_DATA
*CpuData
;
2222 BOOLEAN HasEnabledAp
;
2225 CpuMpData
= GetCpuMpData ();
2227 if (FailedCpuList
!= NULL
) {
2228 *FailedCpuList
= NULL
;
2231 if (CpuMpData
->CpuCount
== 1 && ExcludeBsp
) {
2232 return EFI_NOT_STARTED
;
2235 if (Procedure
== NULL
) {
2236 return EFI_INVALID_PARAMETER
;
2240 // Check whether caller processor is BSP
2242 MpInitLibWhoAmI (&CallerNumber
);
2243 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2244 return EFI_DEVICE_ERROR
;
2250 CheckAndUpdateApsStatus ();
2252 ProcessorCount
= CpuMpData
->CpuCount
;
2253 HasEnabledAp
= FALSE
;
2255 // Check whether all enabled APs are idle.
2256 // If any enabled AP is not idle, return EFI_NOT_READY.
2258 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2259 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2260 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2261 ApState
= GetApState (CpuData
);
2262 if (ApState
!= CpuStateDisabled
) {
2263 HasEnabledAp
= TRUE
;
2264 if (ApState
!= CpuStateIdle
) {
2266 // If any enabled APs are busy, return EFI_NOT_READY.
2268 return EFI_NOT_READY
;
2274 if (!HasEnabledAp
&& ExcludeBsp
) {
2276 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2278 return EFI_NOT_STARTED
;
2281 CpuMpData
->RunningCount
= 0;
2282 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2283 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2284 CpuData
->Waiting
= FALSE
;
2285 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2286 if (CpuData
->State
== CpuStateIdle
) {
2288 // Mark this processor as responsible for current calling.
2290 CpuData
->Waiting
= TRUE
;
2291 CpuMpData
->RunningCount
++;
2296 CpuMpData
->Procedure
= Procedure
;
2297 CpuMpData
->ProcArguments
= ProcedureArgument
;
2298 CpuMpData
->SingleThread
= SingleThread
;
2299 CpuMpData
->FinishedCount
= 0;
2300 CpuMpData
->FailedCpuList
= FailedCpuList
;
2301 CpuMpData
->ExpectedTime
= CalculateTimeout (
2302 TimeoutInMicroseconds
,
2303 &CpuMpData
->CurrentTime
2305 CpuMpData
->TotalTime
= 0;
2306 CpuMpData
->WaitEvent
= WaitEvent
;
2308 if (!SingleThread
) {
2309 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2311 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2312 if (ProcessorNumber
== CallerNumber
) {
2315 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2316 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2326 Procedure (ProcedureArgument
);
2329 Status
= EFI_SUCCESS
;
2330 if (WaitEvent
== NULL
) {
2332 Status
= CheckAllAPs ();
2333 } while (Status
== EFI_NOT_READY
);
2340 Worker function to let the caller get one enabled AP to execute a caller-provided
2343 @param[in] Procedure A pointer to the function to be run on
2344 enabled APs of the system.
2345 @param[in] ProcessorNumber The handle number of the AP.
2346 @param[in] WaitEvent The event created by the caller with CreateEvent()
2348 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2349 APs to return from Procedure, either for
2350 blocking or non-blocking mode.
2351 @param[in] ProcedureArgument The parameter passed into Procedure for
2353 @param[out] Finished If AP returns from Procedure before the
2354 timeout expires, its content is set to TRUE.
2355 Otherwise, the value is set to FALSE.
2357 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2358 the timeout expires.
2359 @retval others Failed to Startup AP.
2363 StartupThisAPWorker (
2364 IN EFI_AP_PROCEDURE Procedure
,
2365 IN UINTN ProcessorNumber
,
2366 IN EFI_EVENT WaitEvent OPTIONAL
,
2367 IN UINTN TimeoutInMicroseconds
,
2368 IN VOID
*ProcedureArgument OPTIONAL
,
2369 OUT BOOLEAN
*Finished OPTIONAL
2373 CPU_MP_DATA
*CpuMpData
;
2374 CPU_AP_DATA
*CpuData
;
2377 CpuMpData
= GetCpuMpData ();
2379 if (Finished
!= NULL
) {
2384 // Check whether caller processor is BSP
2386 MpInitLibWhoAmI (&CallerNumber
);
2387 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2388 return EFI_DEVICE_ERROR
;
2392 // Check whether processor with the handle specified by ProcessorNumber exists
2394 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2395 return EFI_NOT_FOUND
;
2399 // Check whether specified processor is BSP
2401 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2402 return EFI_INVALID_PARAMETER
;
2406 // Check parameter Procedure
2408 if (Procedure
== NULL
) {
2409 return EFI_INVALID_PARAMETER
;
2415 CheckAndUpdateApsStatus ();
2418 // Check whether specified AP is disabled
2420 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2421 return EFI_INVALID_PARAMETER
;
2425 // If WaitEvent is not NULL, execute in non-blocking mode.
2426 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2427 // CheckAPsStatus() will check completion and timeout periodically.
2429 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2430 CpuData
->WaitEvent
= WaitEvent
;
2431 CpuData
->Finished
= Finished
;
2432 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2433 CpuData
->TotalTime
= 0;
2435 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2438 // If WaitEvent is NULL, execute in blocking mode.
2439 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2441 Status
= EFI_SUCCESS
;
2442 if (WaitEvent
== NULL
) {
2444 Status
= CheckThisAP (ProcessorNumber
);
2445 } while (Status
== EFI_NOT_READY
);
2452 Get pointer to CPU MP Data structure from GUIDed HOB.
2454 @return The pointer to CPU MP Data structure.
2457 GetCpuMpDataFromGuidedHob (
2461 EFI_HOB_GUID_TYPE
*GuidHob
;
2463 CPU_MP_DATA
*CpuMpData
;
2466 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2467 if (GuidHob
!= NULL
) {
2468 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2469 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2475 This service executes a caller provided function on all enabled CPUs.
2477 @param[in] Procedure A pointer to the function to be run on
2478 enabled APs of the system. See type
2480 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2481 APs to return from Procedure, either for
2482 blocking or non-blocking mode. Zero means
2483 infinity. TimeoutInMicroseconds is ignored
2485 @param[in] ProcedureArgument The parameter passed into Procedure for
2488 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2489 the timeout expired.
2490 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2491 to all enabled CPUs.
2492 @retval EFI_DEVICE_ERROR Caller processor is AP.
2493 @retval EFI_NOT_READY Any enabled APs are busy.
2494 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2495 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2496 all enabled APs have finished.
2497 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2502 MpInitLibStartupAllCPUs (
2503 IN EFI_AP_PROCEDURE Procedure
,
2504 IN UINTN TimeoutInMicroseconds
,
2505 IN VOID
*ProcedureArgument OPTIONAL
2508 return StartupAllCPUsWorker (
2513 TimeoutInMicroseconds
,