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
461 CPU_INFO_IN_HOB
*CpuInfoInHob
;
465 // Send 1st broadcast IPI to APs to wakeup APs
467 CpuMpData
->InitFlag
= ApInitConfig
;
468 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
, TRUE
);
469 CpuMpData
->InitFlag
= ApInitDone
;
470 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
472 // Wait for all APs finished the initialization
474 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
480 // Enable x2APIC mode if
481 // 1. Number of CPU is greater than 255; or
482 // 2. There are any logical processors reporting an Initial APIC ID of 255 or greater.
485 if (CpuMpData
->CpuCount
> 255) {
487 // If there are more than 255 processor found, force to enable X2APIC
491 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
492 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
493 if (CpuInfoInHob
[Index
].InitialApicId
>= 0xFF) {
501 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
503 // Wakeup all APs to enable x2APIC mode
505 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
, TRUE
);
507 // Wait for all known APs finished
509 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
513 // Enable x2APIC on BSP
515 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
517 // Set BSP/Aps state to IDLE
519 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
520 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
523 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
525 // Sort BSP/Aps by CPU APIC ID in ascending order
527 SortApicId (CpuMpData
);
529 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
531 return CpuMpData
->CpuCount
;
535 Initialize CPU AP Data when AP is wakeup at the first time.
537 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
538 @param[in] ProcessorNumber The handle number of processor
539 @param[in] BistData Processor BIST data
540 @param[in] ApTopOfStack Top of AP stack
545 IN OUT CPU_MP_DATA
*CpuMpData
,
546 IN UINTN ProcessorNumber
,
548 IN UINT64 ApTopOfStack
551 CPU_INFO_IN_HOB
*CpuInfoInHob
;
553 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
554 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
555 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
556 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
557 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
559 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
560 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
562 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
563 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
567 This function will be called from AP reset code if BSP uses WakeUpAP.
569 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
570 @param[in] ApIndex Number of current executing AP
575 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
579 CPU_MP_DATA
*CpuMpData
;
580 UINTN ProcessorNumber
;
581 EFI_AP_PROCEDURE Procedure
;
584 volatile UINT32
*ApStartupSignalBuffer
;
585 CPU_INFO_IN_HOB
*CpuInfoInHob
;
587 UINTN CurrentApicMode
;
590 // AP finished assembly code and begin to execute C code
592 CpuMpData
= ExchangeInfo
->CpuMpData
;
595 // AP's local APIC settings will be lost after received INIT IPI
596 // We need to re-initialize them at here
598 ProgramVirtualWireMode ();
600 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
602 DisableLvtInterrupts ();
603 SyncLocalApicTimerSetting (CpuMpData
);
605 CurrentApicMode
= GetApicMode ();
607 if (CpuMpData
->InitFlag
== ApInitConfig
) {
611 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
612 ProcessorNumber
= ApIndex
;
614 // This is first time AP wakeup, get BIST information from AP stack
616 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
617 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
619 // Do some AP initialize sync
621 ApInitializeSync (CpuMpData
);
623 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
624 // to initialize AP in InitConfig path.
625 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
627 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
628 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
629 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
631 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
634 // Execute AP function if AP is ready
636 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
638 // Clear AP start-up signal when AP waken up
640 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
641 InterlockedCompareExchange32 (
642 (UINT32
*) ApStartupSignalBuffer
,
646 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
648 // Restore AP's volatile registers saved
650 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
653 // The CPU driver might not flush TLB for APs on spot after updating
654 // page attributes. AP in mwait loop mode needs to take care of it when
660 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
661 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
662 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
663 if (Procedure
!= NULL
) {
664 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
666 // Enable source debugging on AP function
670 // Invoke AP function here
672 Procedure (Parameter
);
673 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
674 if (CpuMpData
->SwitchBspFlag
) {
676 // Re-get the processor number due to BSP/AP maybe exchange in AP function
678 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
679 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
680 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
681 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
682 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
684 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
685 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
686 if (CurrentApicMode
!= GetApicMode ()) {
688 // If APIC mode change happened during AP function execution,
689 // we do not support APIC ID value changed.
695 // Re-get the CPU APICID and Initial APICID if they are changed
697 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
698 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
703 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
708 // AP finished executing C code
710 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
713 // Place AP is specified loop mode
715 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
717 // Save AP volatile registers
719 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
721 // Place AP in HLT-loop
724 DisableInterrupts ();
730 DisableInterrupts ();
731 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
733 // Place AP in MWAIT-loop
735 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
736 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
738 // Check AP start-up signal again.
739 // If AP start-up signal is not set, place AP into
740 // the specified C-state
742 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
744 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
746 // Place AP in Run-loop
754 // If AP start-up signal is written, AP is waken up
755 // otherwise place AP in loop again
757 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
765 Wait for AP wakeup and write AP start-up signal till AP is waken up.
767 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
771 IN
volatile UINT32
*ApStartupSignalBuffer
775 // If AP is waken up, StartupApSignal should be cleared.
776 // Otherwise, write StartupApSignal again till AP waken up.
778 while (InterlockedCompareExchange32 (
779 (UINT32
*) ApStartupSignalBuffer
,
788 This function will fill the exchange info structure.
790 @param[in] CpuMpData Pointer to CPU MP Data
794 FillExchangeInfoData (
795 IN CPU_MP_DATA
*CpuMpData
798 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
800 IA32_SEGMENT_DESCRIPTOR
*Selector
;
803 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
804 ExchangeInfo
->Lock
= 0;
805 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
806 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
807 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
808 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
810 ExchangeInfo
->CodeSegment
= AsmReadCs ();
811 ExchangeInfo
->DataSegment
= AsmReadDs ();
813 ExchangeInfo
->Cr3
= AsmReadCr3 ();
815 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
816 ExchangeInfo
->ApIndex
= 0;
817 ExchangeInfo
->NumApsExecuting
= 0;
818 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
819 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
820 ExchangeInfo
->CpuMpData
= CpuMpData
;
822 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
824 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
827 // We can check either CPUID(7).ECX[bit16] or check CR4.LA57[bit12]
828 // to determin whether 5-Level Paging is enabled.
829 // CPUID(7).ECX[bit16] shows CPU's capability, CR4.LA57[bit12] shows
830 // current system setting.
831 // Using latter way is simpler because it also eliminates the needs to
832 // check whether platform wants to enable it.
834 Cr4
.UintN
= AsmReadCr4 ();
835 ExchangeInfo
->Enable5LevelPaging
= (BOOLEAN
) (Cr4
.Bits
.LA57
== 1);
836 DEBUG ((DEBUG_INFO
, "%a: 5-Level Paging = %d\n", gEfiCallerBaseName
, ExchangeInfo
->Enable5LevelPaging
));
839 // Get the BSP's data of GDT and IDT
841 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
842 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
845 // Find a 32-bit code segment
847 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
848 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
850 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
851 ExchangeInfo
->ModeTransitionSegment
=
852 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
856 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
860 // Copy all 32-bit code and 64-bit code into memory with type of
861 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
863 if (CpuMpData
->WakeupBufferHigh
!= 0) {
864 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
865 CpuMpData
->AddressMap
.ModeTransitionOffset
;
867 (VOID
*)CpuMpData
->WakeupBufferHigh
,
868 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
869 CpuMpData
->AddressMap
.ModeTransitionOffset
,
873 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
875 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
876 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
879 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
880 (UINT32
)ExchangeInfo
->ModeOffset
-
881 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
882 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
886 Helper function that waits until the finished AP count reaches the specified
887 limit, or the specified timeout elapses (whichever comes first).
889 @param[in] CpuMpData Pointer to CPU MP Data.
890 @param[in] FinishedApLimit The number of finished APs to wait for.
891 @param[in] TimeLimit The number of microseconds to wait for.
894 TimedWaitForApFinish (
895 IN CPU_MP_DATA
*CpuMpData
,
896 IN UINT32 FinishedApLimit
,
901 Get available system memory below 1MB by specified size.
903 @param[in] CpuMpData The pointer to CPU MP Data structure.
906 BackupAndPrepareWakeupBuffer(
907 IN CPU_MP_DATA
*CpuMpData
911 (VOID
*) CpuMpData
->BackupBuffer
,
912 (VOID
*) CpuMpData
->WakeupBuffer
,
913 CpuMpData
->BackupBufferSize
916 (VOID
*) CpuMpData
->WakeupBuffer
,
917 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
918 CpuMpData
->AddressMap
.RendezvousFunnelSize
923 Restore wakeup buffer data.
925 @param[in] CpuMpData The pointer to CPU MP Data structure.
929 IN CPU_MP_DATA
*CpuMpData
933 (VOID
*) CpuMpData
->WakeupBuffer
,
934 (VOID
*) CpuMpData
->BackupBuffer
,
935 CpuMpData
->BackupBufferSize
940 Allocate reset vector buffer.
942 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
945 AllocateResetVector (
946 IN OUT CPU_MP_DATA
*CpuMpData
949 UINTN ApResetVectorSize
;
951 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
952 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
953 sizeof (MP_CPU_EXCHANGE_INFO
);
955 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
956 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
957 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
958 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
959 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
960 CpuMpData
->AddressMap
.ModeTransitionOffset
963 BackupAndPrepareWakeupBuffer (CpuMpData
);
967 Free AP reset vector buffer.
969 @param[in] CpuMpData The pointer to CPU MP Data structure.
973 IN CPU_MP_DATA
*CpuMpData
976 RestoreWakeupBuffer (CpuMpData
);
980 This function will be called by BSP to wakeup AP.
982 @param[in] CpuMpData Pointer to CPU MP Data
983 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
984 FALSE: Send IPI to AP by ApicId
985 @param[in] ProcessorNumber The handle number of specified processor
986 @param[in] Procedure The function to be invoked by AP
987 @param[in] ProcedureArgument The argument to be passed into AP function
988 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
992 IN CPU_MP_DATA
*CpuMpData
,
993 IN BOOLEAN Broadcast
,
994 IN UINTN ProcessorNumber
,
995 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
996 IN VOID
*ProcedureArgument
, OPTIONAL
997 IN BOOLEAN WakeUpDisabledAps
1000 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
1002 CPU_AP_DATA
*CpuData
;
1003 BOOLEAN ResetVectorRequired
;
1004 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1006 CpuMpData
->FinishedCount
= 0;
1007 ResetVectorRequired
= FALSE
;
1009 if (CpuMpData
->WakeUpByInitSipiSipi
||
1010 CpuMpData
->InitFlag
!= ApInitDone
) {
1011 ResetVectorRequired
= TRUE
;
1012 AllocateResetVector (CpuMpData
);
1013 FillExchangeInfoData (CpuMpData
);
1014 SaveLocalApicTimerSetting (CpuMpData
);
1017 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1019 // Get AP target C-state each time when waking up AP,
1020 // for it maybe updated by platform again
1022 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1025 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1028 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1029 if (Index
!= CpuMpData
->BspNumber
) {
1030 CpuData
= &CpuMpData
->CpuData
[Index
];
1032 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1033 // the AP procedure will be skipped for disabled AP because AP state
1034 // is not CpuStateReady.
1036 if (GetApState (CpuData
) == CpuStateDisabled
&& !WakeUpDisabledAps
) {
1040 CpuData
->ApFunction
= (UINTN
) Procedure
;
1041 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1042 SetApState (CpuData
, CpuStateReady
);
1043 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1044 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1048 if (ResetVectorRequired
) {
1052 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1054 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1055 if (PcdGet32 (PcdCpuBootLogicalProcessorNumber
) > 0) {
1057 // The AP enumeration algorithm below is suitable only when the
1058 // platform can tell us the *exact* boot CPU count in advance.
1060 // The wait below finishes only when the detected AP count reaches
1061 // (PcdCpuBootLogicalProcessorNumber - 1), regardless of how long that
1062 // takes. If at least one AP fails to check in (meaning a platform
1063 // hardware bug), the detection hangs forever, by design. If the actual
1064 // boot CPU count in the system is higher than
1065 // PcdCpuBootLogicalProcessorNumber (meaning a platform
1066 // misconfiguration), then some APs may complete initialization after
1067 // the wait finishes, and cause undefined behavior.
1069 TimedWaitForApFinish (
1071 PcdGet32 (PcdCpuBootLogicalProcessorNumber
) - 1,
1072 MAX_UINT32
// approx. 71 minutes
1076 // The AP enumeration algorithm below is suitable for two use cases.
1078 // (1) The check-in time for an individual AP is bounded, and APs run
1079 // through their initialization routines strongly concurrently. In
1080 // particular, the number of concurrently running APs
1081 // ("NumApsExecuting") is never expected to fall to zero
1082 // *temporarily* -- it is expected to fall to zero only when all
1083 // APs have checked-in.
1085 // In this case, the platform is supposed to set
1086 // PcdCpuApInitTimeOutInMicroSeconds to a low-ish value (just long
1087 // enough for one AP to start initialization). The timeout will be
1088 // reached soon, and remaining APs are collected by watching
1089 // NumApsExecuting fall to zero. If NumApsExecuting falls to zero
1090 // mid-process, while some APs have not completed initialization,
1091 // the behavior is undefined.
1093 // (2) The check-in time for an individual AP is unbounded, and/or APs
1094 // may complete their initializations widely spread out. In
1095 // particular, some APs may finish initialization before some APs
1098 // In this case, the platform is supposed to set
1099 // PcdCpuApInitTimeOutInMicroSeconds to a high-ish value. The AP
1100 // enumeration will always take that long (except when the boot CPU
1101 // count happens to be maximal, that is,
1102 // PcdCpuMaxLogicalProcessorNumber). All APs are expected to
1103 // check-in before the timeout, and NumApsExecuting is assumed zero
1104 // at timeout. APs that miss the time-out may cause undefined
1107 TimedWaitForApFinish (
1109 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1110 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1113 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1119 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1121 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1122 CpuData
= &CpuMpData
->CpuData
[Index
];
1123 if (Index
!= CpuMpData
->BspNumber
) {
1124 WaitApWakeup (CpuData
->StartupApSignal
);
1129 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1130 CpuData
->ApFunction
= (UINTN
) Procedure
;
1131 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1132 SetApState (CpuData
, CpuStateReady
);
1134 // Wakeup specified AP
1136 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1137 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1138 if (ResetVectorRequired
) {
1139 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1141 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1142 (UINT32
) ExchangeInfo
->BufferStart
1146 // Wait specified AP waken up
1148 WaitApWakeup (CpuData
->StartupApSignal
);
1151 if (ResetVectorRequired
) {
1152 FreeResetVector (CpuMpData
);
1156 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1157 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1158 // S3SmmInitDone Ppi.
1160 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1164 Calculate timeout value and return the current performance counter value.
1166 Calculate the number of performance counter ticks required for a timeout.
1167 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1170 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1171 @param[out] CurrentTime Returns the current value of the performance counter.
1173 @return Expected time stamp counter for timeout.
1174 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1180 IN UINTN TimeoutInMicroseconds
,
1181 OUT UINT64
*CurrentTime
1184 UINT64 TimeoutInSeconds
;
1185 UINT64 TimestampCounterFreq
;
1188 // Read the current value of the performance counter
1190 *CurrentTime
= GetPerformanceCounter ();
1193 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1196 if (TimeoutInMicroseconds
== 0) {
1201 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1204 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1207 // Check the potential overflow before calculate the number of ticks for the timeout value.
1209 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1211 // Convert microseconds into seconds if direct multiplication overflows
1213 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1215 // Assertion if the final tick count exceeds MAX_UINT64
1217 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1218 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1221 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1222 // it by 1,000,000, to get the number of ticks for the timeout value.
1226 TimestampCounterFreq
,
1227 TimeoutInMicroseconds
1235 Checks whether timeout expires.
1237 Check whether the number of elapsed performance counter ticks required for
1238 a timeout condition has been reached.
1239 If Timeout is zero, which means infinity, return value is always FALSE.
1241 @param[in, out] PreviousTime On input, the value of the performance counter
1242 when it was last read.
1243 On output, the current value of the performance
1245 @param[in] TotalTime The total amount of elapsed time in performance
1247 @param[in] Timeout The number of performance counter ticks required
1248 to reach a timeout condition.
1250 @retval TRUE A timeout condition has been reached.
1251 @retval FALSE A timeout condition has not been reached.
1256 IN OUT UINT64
*PreviousTime
,
1257 IN UINT64
*TotalTime
,
1270 GetPerformanceCounterProperties (&Start
, &End
);
1271 Cycle
= End
- Start
;
1276 CurrentTime
= GetPerformanceCounter();
1277 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1284 *TotalTime
+= Delta
;
1285 *PreviousTime
= CurrentTime
;
1286 if (*TotalTime
> Timeout
) {
1293 Helper function that waits until the finished AP count reaches the specified
1294 limit, or the specified timeout elapses (whichever comes first).
1296 @param[in] CpuMpData Pointer to CPU MP Data.
1297 @param[in] FinishedApLimit The number of finished APs to wait for.
1298 @param[in] TimeLimit The number of microseconds to wait for.
1301 TimedWaitForApFinish (
1302 IN CPU_MP_DATA
*CpuMpData
,
1303 IN UINT32 FinishedApLimit
,
1308 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1309 // "infinity", so check for (TimeLimit == 0) explicitly.
1311 if (TimeLimit
== 0) {
1315 CpuMpData
->TotalTime
= 0;
1316 CpuMpData
->ExpectedTime
= CalculateTimeout (
1318 &CpuMpData
->CurrentTime
1320 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1322 &CpuMpData
->CurrentTime
,
1323 &CpuMpData
->TotalTime
,
1324 CpuMpData
->ExpectedTime
1329 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1332 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1335 DivU64x64Remainder (
1336 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1337 GetPerformanceCounterProperties (NULL
, NULL
),
1345 Reset an AP to Idle state.
1347 Any task being executed by the AP will be aborted and the AP
1348 will be waiting for a new task in Wait-For-SIPI state.
1350 @param[in] ProcessorNumber The handle number of processor.
1353 ResetProcessorToIdleState (
1354 IN UINTN ProcessorNumber
1357 CPU_MP_DATA
*CpuMpData
;
1359 CpuMpData
= GetCpuMpData ();
1361 CpuMpData
->InitFlag
= ApInitReconfig
;
1362 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1363 while (CpuMpData
->FinishedCount
< 1) {
1366 CpuMpData
->InitFlag
= ApInitDone
;
1368 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1372 Searches for the next waiting AP.
1374 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1376 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1378 @retval EFI_SUCCESS The next waiting AP has been found.
1379 @retval EFI_NOT_FOUND No waiting AP exists.
1383 GetNextWaitingProcessorNumber (
1384 OUT UINTN
*NextProcessorNumber
1387 UINTN ProcessorNumber
;
1388 CPU_MP_DATA
*CpuMpData
;
1390 CpuMpData
= GetCpuMpData ();
1392 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1393 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1394 *NextProcessorNumber
= ProcessorNumber
;
1399 return EFI_NOT_FOUND
;
1402 /** Checks status of specified AP.
1404 This function checks whether the specified AP has finished the task assigned
1405 by StartupThisAP(), and whether timeout expires.
1407 @param[in] ProcessorNumber The handle number of processor.
1409 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1410 @retval EFI_TIMEOUT The timeout expires.
1411 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1415 IN UINTN ProcessorNumber
1418 CPU_MP_DATA
*CpuMpData
;
1419 CPU_AP_DATA
*CpuData
;
1421 CpuMpData
= GetCpuMpData ();
1422 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1425 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1426 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1427 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1430 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1432 if (GetApState(CpuData
) == CpuStateFinished
) {
1433 if (CpuData
->Finished
!= NULL
) {
1434 *(CpuData
->Finished
) = TRUE
;
1436 SetApState (CpuData
, CpuStateIdle
);
1440 // If timeout expires for StartupThisAP(), report timeout.
1442 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1443 if (CpuData
->Finished
!= NULL
) {
1444 *(CpuData
->Finished
) = FALSE
;
1447 // Reset failed AP to idle state
1449 ResetProcessorToIdleState (ProcessorNumber
);
1454 return EFI_NOT_READY
;
1458 Checks status of all APs.
1460 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1461 and whether timeout expires.
1463 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1464 @retval EFI_TIMEOUT The timeout expires.
1465 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1472 UINTN ProcessorNumber
;
1473 UINTN NextProcessorNumber
;
1476 CPU_MP_DATA
*CpuMpData
;
1477 CPU_AP_DATA
*CpuData
;
1479 CpuMpData
= GetCpuMpData ();
1481 NextProcessorNumber
= 0;
1484 // Go through all APs that are responsible for the StartupAllAPs().
1486 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1487 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1491 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1493 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1494 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1495 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1497 if (GetApState(CpuData
) == CpuStateFinished
) {
1498 CpuMpData
->RunningCount
--;
1499 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1500 SetApState(CpuData
, CpuStateIdle
);
1503 // If in Single Thread mode, then search for the next waiting AP for execution.
1505 if (CpuMpData
->SingleThread
) {
1506 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1508 if (!EFI_ERROR (Status
)) {
1512 (UINT32
) NextProcessorNumber
,
1513 CpuMpData
->Procedure
,
1514 CpuMpData
->ProcArguments
,
1523 // If all APs finish, return EFI_SUCCESS.
1525 if (CpuMpData
->RunningCount
== 0) {
1530 // If timeout expires, report timeout.
1533 &CpuMpData
->CurrentTime
,
1534 &CpuMpData
->TotalTime
,
1535 CpuMpData
->ExpectedTime
)
1538 // If FailedCpuList is not NULL, record all failed APs in it.
1540 if (CpuMpData
->FailedCpuList
!= NULL
) {
1541 *CpuMpData
->FailedCpuList
=
1542 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1543 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1547 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1549 // Check whether this processor is responsible for StartupAllAPs().
1551 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1553 // Reset failed APs to idle state
1555 ResetProcessorToIdleState (ProcessorNumber
);
1556 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1557 if (CpuMpData
->FailedCpuList
!= NULL
) {
1558 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1562 if (CpuMpData
->FailedCpuList
!= NULL
) {
1563 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1567 return EFI_NOT_READY
;
1571 MP Initialize Library initialization.
1573 This service will allocate AP reset vector and wakeup all APs to do APs
1576 This service must be invoked before all other MP Initialize Library
1577 service are invoked.
1579 @retval EFI_SUCCESS MP initialization succeeds.
1580 @retval Others MP initialization fails.
1585 MpInitLibInitialize (
1589 CPU_MP_DATA
*OldCpuMpData
;
1590 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1591 UINT32 MaxLogicalProcessorNumber
;
1593 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1594 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1596 UINT32 MonitorFilterSize
;
1599 CPU_MP_DATA
*CpuMpData
;
1601 UINT8
*MonitorBuffer
;
1603 UINTN ApResetVectorSize
;
1604 UINTN BackupBufferAddr
;
1606 VOID
*MicrocodePatchInRam
;
1608 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1609 if (OldCpuMpData
== NULL
) {
1610 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1612 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1614 ASSERT (MaxLogicalProcessorNumber
!= 0);
1616 AsmGetAddressMap (&AddressMap
);
1617 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1618 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1619 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1622 // Save BSP's Control registers for APs.
1624 SaveVolatileRegisters (&VolatileRegisters
);
1626 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1627 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1628 BufferSize
+= ApResetVectorSize
;
1629 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1630 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1631 BufferSize
+= sizeof (CPU_MP_DATA
);
1632 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1633 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1634 ASSERT (MpBuffer
!= NULL
);
1635 ZeroMem (MpBuffer
, BufferSize
);
1636 Buffer
= (UINTN
) MpBuffer
;
1639 // The layout of the Buffer is as below:
1641 // +--------------------+ <-- Buffer
1643 // +--------------------+ <-- MonitorBuffer
1644 // AP Monitor Filters (N)
1645 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1647 // +--------------------+
1649 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1650 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1651 // +--------------------+ <-- CpuMpData
1653 // +--------------------+ <-- CpuMpData->CpuData
1655 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1656 // CPU_INFO_IN_HOB (N)
1657 // +--------------------+
1659 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1660 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1661 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1662 CpuMpData
= (CPU_MP_DATA
*) (ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1663 CpuMpData
->Buffer
= Buffer
;
1664 CpuMpData
->CpuApStackSize
= ApStackSize
;
1665 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1666 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1667 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1668 CpuMpData
->CpuCount
= 1;
1669 CpuMpData
->BspNumber
= 0;
1670 CpuMpData
->WaitEvent
= NULL
;
1671 CpuMpData
->SwitchBspFlag
= FALSE
;
1672 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1673 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1674 if (OldCpuMpData
== NULL
) {
1675 CpuMpData
->MicrocodePatchRegionSize
= PcdGet64 (PcdCpuMicrocodePatchRegionSize
);
1677 // If platform has more than one CPU, relocate microcode to memory to reduce
1678 // loading microcode time.
1680 MicrocodePatchInRam
= NULL
;
1681 if (MaxLogicalProcessorNumber
> 1) {
1682 MicrocodePatchInRam
= AllocatePages (
1684 (UINTN
)CpuMpData
->MicrocodePatchRegionSize
1688 if (MicrocodePatchInRam
== NULL
) {
1690 // there is only one processor, or no microcode patch is available, or
1691 // memory allocation failed
1693 CpuMpData
->MicrocodePatchAddress
= PcdGet64 (PcdCpuMicrocodePatchAddress
);
1696 // there are multiple processors, and a microcode patch is available, and
1697 // memory allocation succeeded
1700 MicrocodePatchInRam
,
1701 (VOID
*)(UINTN
)PcdGet64 (PcdCpuMicrocodePatchAddress
),
1702 (UINTN
)CpuMpData
->MicrocodePatchRegionSize
1704 CpuMpData
->MicrocodePatchAddress
= (UINTN
)MicrocodePatchInRam
;
1707 CpuMpData
->MicrocodePatchRegionSize
= OldCpuMpData
->MicrocodePatchRegionSize
;
1708 CpuMpData
->MicrocodePatchAddress
= OldCpuMpData
->MicrocodePatchAddress
;
1710 InitializeSpinLock(&CpuMpData
->MpLock
);
1713 // Make sure no memory usage outside of the allocated buffer.
1715 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1716 Buffer
+ BufferSize
);
1719 // Duplicate BSP's IDT to APs.
1720 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1722 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1723 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1725 // Don't pass BSP's TR to APs to avoid AP init failure.
1727 VolatileRegisters
.Tr
= 0;
1728 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1730 // Set BSP basic information
1732 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1734 // Save assembly code information
1736 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1738 // Finally set AP loop mode
1740 CpuMpData
->ApLoopMode
= ApLoopMode
;
1741 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1743 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1746 // Set up APs wakeup signal buffer
1748 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1749 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1750 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1753 // Load Microcode on BSP
1755 MicrocodeDetect (CpuMpData
, TRUE
);
1757 // Store BSP's MTRR setting
1759 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1761 // Enable the local APIC for Virtual Wire Mode.
1763 ProgramVirtualWireMode ();
1765 if (OldCpuMpData
== NULL
) {
1766 if (MaxLogicalProcessorNumber
> 1) {
1768 // Wakeup all APs and calculate the processor count in system
1770 CollectProcessorCount (CpuMpData
);
1774 // APs have been wakeup before, just get the CPU Information
1777 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1778 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1779 CpuMpData
->InitFlag
= ApInitReconfig
;
1780 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1781 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1782 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1783 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1784 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1785 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1786 CopyMem (&CpuMpData
->CpuData
[Index
].VolatileRegisters
, &VolatileRegisters
, sizeof (CPU_VOLATILE_REGISTERS
));
1788 if (MaxLogicalProcessorNumber
> 1) {
1790 // Wakeup APs to do some AP initialize sync
1792 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1794 // Wait for all APs finished initialization
1796 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1799 CpuMpData
->InitFlag
= ApInitDone
;
1800 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1801 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1807 // Initialize global data for MP support
1809 InitMpGlobalData (CpuMpData
);
1815 Gets detailed MP-related information on the requested processor at the
1816 instant this call is made. This service may only be called from the BSP.
1818 @param[in] ProcessorNumber The handle number of processor.
1819 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1820 the requested processor is deposited.
1821 @param[out] HealthData Return processor health data.
1823 @retval EFI_SUCCESS Processor information was returned.
1824 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1825 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1826 @retval EFI_NOT_FOUND The processor with the handle specified by
1827 ProcessorNumber does not exist in the platform.
1828 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1833 MpInitLibGetProcessorInfo (
1834 IN UINTN ProcessorNumber
,
1835 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1836 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1839 CPU_MP_DATA
*CpuMpData
;
1841 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1843 CpuMpData
= GetCpuMpData ();
1844 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1847 // Check whether caller processor is BSP
1849 MpInitLibWhoAmI (&CallerNumber
);
1850 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1851 return EFI_DEVICE_ERROR
;
1854 if (ProcessorInfoBuffer
== NULL
) {
1855 return EFI_INVALID_PARAMETER
;
1858 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1859 return EFI_NOT_FOUND
;
1862 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1863 ProcessorInfoBuffer
->StatusFlag
= 0;
1864 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1865 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1867 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1868 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1870 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1871 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1873 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1877 // Get processor location information
1879 GetProcessorLocationByApicId (
1880 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1881 &ProcessorInfoBuffer
->Location
.Package
,
1882 &ProcessorInfoBuffer
->Location
.Core
,
1883 &ProcessorInfoBuffer
->Location
.Thread
1886 if (HealthData
!= NULL
) {
1887 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1894 Worker function to switch the requested AP to be the BSP from that point onward.
1896 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1897 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1898 enabled AP. Otherwise, it will be disabled.
1900 @retval EFI_SUCCESS BSP successfully switched.
1901 @retval others Failed to switch BSP.
1906 IN UINTN ProcessorNumber
,
1907 IN BOOLEAN EnableOldBSP
1910 CPU_MP_DATA
*CpuMpData
;
1913 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1914 BOOLEAN OldInterruptState
;
1915 BOOLEAN OldTimerInterruptState
;
1918 // Save and Disable Local APIC timer interrupt
1920 OldTimerInterruptState
= GetApicTimerInterruptState ();
1921 DisableApicTimerInterrupt ();
1923 // Before send both BSP and AP to a procedure to exchange their roles,
1924 // interrupt must be disabled. This is because during the exchange role
1925 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1926 // be corrupted, since interrupt return address will be pushed to stack
1929 OldInterruptState
= SaveAndDisableInterrupts ();
1932 // Mask LINT0 & LINT1 for the old BSP
1934 DisableLvtInterrupts ();
1936 CpuMpData
= GetCpuMpData ();
1939 // Check whether caller processor is BSP
1941 MpInitLibWhoAmI (&CallerNumber
);
1942 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1943 return EFI_DEVICE_ERROR
;
1946 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1947 return EFI_NOT_FOUND
;
1951 // Check whether specified AP is disabled
1953 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1954 if (State
== CpuStateDisabled
) {
1955 return EFI_INVALID_PARAMETER
;
1959 // Check whether ProcessorNumber specifies the current BSP
1961 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1962 return EFI_INVALID_PARAMETER
;
1966 // Check whether specified AP is busy
1968 if (State
== CpuStateBusy
) {
1969 return EFI_NOT_READY
;
1972 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1973 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1974 CpuMpData
->SwitchBspFlag
= TRUE
;
1975 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1978 // Clear the BSP bit of MSR_IA32_APIC_BASE
1980 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1981 ApicBaseMsr
.Bits
.BSP
= 0;
1982 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1985 // Need to wakeUp AP (future BSP).
1987 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
1989 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1992 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1994 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1995 ApicBaseMsr
.Bits
.BSP
= 1;
1996 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1997 ProgramVirtualWireMode ();
2000 // Wait for old BSP finished AP task
2002 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
2006 CpuMpData
->SwitchBspFlag
= FALSE
;
2008 // Set old BSP enable state
2010 if (!EnableOldBSP
) {
2011 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
2013 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
2016 // Save new BSP number
2018 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
2021 // Restore interrupt state.
2023 SetInterruptState (OldInterruptState
);
2025 if (OldTimerInterruptState
) {
2026 EnableApicTimerInterrupt ();
2033 Worker function to let the caller enable or disable an AP from this point onward.
2034 This service may only be called from the BSP.
2036 @param[in] ProcessorNumber The handle number of AP.
2037 @param[in] EnableAP Specifies the new state for the processor for
2038 enabled, FALSE for disabled.
2039 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
2040 the new health status of the AP.
2042 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
2043 @retval others Failed to Enable/Disable AP.
2047 EnableDisableApWorker (
2048 IN UINTN ProcessorNumber
,
2049 IN BOOLEAN EnableAP
,
2050 IN UINT32
*HealthFlag OPTIONAL
2053 CPU_MP_DATA
*CpuMpData
;
2056 CpuMpData
= GetCpuMpData ();
2059 // Check whether caller processor is BSP
2061 MpInitLibWhoAmI (&CallerNumber
);
2062 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2063 return EFI_DEVICE_ERROR
;
2066 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2067 return EFI_INVALID_PARAMETER
;
2070 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2071 return EFI_NOT_FOUND
;
2075 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2077 ResetProcessorToIdleState (ProcessorNumber
);
2080 if (HealthFlag
!= NULL
) {
2081 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2082 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2089 This return the handle number for the calling processor. This service may be
2090 called from the BSP and APs.
2092 @param[out] ProcessorNumber Pointer to the handle number of AP.
2093 The range is from 0 to the total number of
2094 logical processors minus 1. The total number of
2095 logical processors can be retrieved by
2096 MpInitLibGetNumberOfProcessors().
2098 @retval EFI_SUCCESS The current processor handle number was returned
2100 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2101 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2107 OUT UINTN
*ProcessorNumber
2110 CPU_MP_DATA
*CpuMpData
;
2112 if (ProcessorNumber
== NULL
) {
2113 return EFI_INVALID_PARAMETER
;
2116 CpuMpData
= GetCpuMpData ();
2118 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2122 Retrieves the number of logical processor in the platform and the number of
2123 those logical processors that are enabled on this boot. This service may only
2124 be called from the BSP.
2126 @param[out] NumberOfProcessors Pointer to the total number of logical
2127 processors in the system, including the BSP
2129 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2130 processors that exist in system, including
2133 @retval EFI_SUCCESS The number of logical processors and enabled
2134 logical processors was retrieved.
2135 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2136 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2138 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2143 MpInitLibGetNumberOfProcessors (
2144 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2145 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2148 CPU_MP_DATA
*CpuMpData
;
2150 UINTN ProcessorNumber
;
2151 UINTN EnabledProcessorNumber
;
2154 CpuMpData
= GetCpuMpData ();
2156 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2157 return EFI_INVALID_PARAMETER
;
2161 // Check whether caller processor is BSP
2163 MpInitLibWhoAmI (&CallerNumber
);
2164 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2165 return EFI_DEVICE_ERROR
;
2168 ProcessorNumber
= CpuMpData
->CpuCount
;
2169 EnabledProcessorNumber
= 0;
2170 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2171 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2172 EnabledProcessorNumber
++;
2176 if (NumberOfProcessors
!= NULL
) {
2177 *NumberOfProcessors
= ProcessorNumber
;
2179 if (NumberOfEnabledProcessors
!= NULL
) {
2180 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2188 Worker function to execute a caller provided function on all enabled APs.
2190 @param[in] Procedure A pointer to the function to be run on
2191 enabled APs of the system.
2192 @param[in] SingleThread If TRUE, then all the enabled APs execute
2193 the function specified by Procedure one by
2194 one, in ascending order of processor handle
2195 number. If FALSE, then all the enabled APs
2196 execute the function specified by Procedure
2198 @param[in] ExcludeBsp Whether let BSP also trig this task.
2199 @param[in] WaitEvent The event created by the caller with CreateEvent()
2201 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2202 APs to return from Procedure, either for
2203 blocking or non-blocking mode.
2204 @param[in] ProcedureArgument The parameter passed into Procedure for
2206 @param[out] FailedCpuList If all APs finish successfully, then its
2207 content is set to NULL. If not all APs
2208 finish before timeout expires, then its
2209 content is set to address of the buffer
2210 holding handle numbers of the failed APs.
2212 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2213 the timeout expired.
2214 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2216 @retval others Failed to Startup all APs.
2220 StartupAllCPUsWorker (
2221 IN EFI_AP_PROCEDURE Procedure
,
2222 IN BOOLEAN SingleThread
,
2223 IN BOOLEAN ExcludeBsp
,
2224 IN EFI_EVENT WaitEvent OPTIONAL
,
2225 IN UINTN TimeoutInMicroseconds
,
2226 IN VOID
*ProcedureArgument OPTIONAL
,
2227 OUT UINTN
**FailedCpuList OPTIONAL
2231 CPU_MP_DATA
*CpuMpData
;
2232 UINTN ProcessorCount
;
2233 UINTN ProcessorNumber
;
2235 CPU_AP_DATA
*CpuData
;
2236 BOOLEAN HasEnabledAp
;
2239 CpuMpData
= GetCpuMpData ();
2241 if (FailedCpuList
!= NULL
) {
2242 *FailedCpuList
= NULL
;
2245 if (CpuMpData
->CpuCount
== 1 && ExcludeBsp
) {
2246 return EFI_NOT_STARTED
;
2249 if (Procedure
== NULL
) {
2250 return EFI_INVALID_PARAMETER
;
2254 // Check whether caller processor is BSP
2256 MpInitLibWhoAmI (&CallerNumber
);
2257 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2258 return EFI_DEVICE_ERROR
;
2264 CheckAndUpdateApsStatus ();
2266 ProcessorCount
= CpuMpData
->CpuCount
;
2267 HasEnabledAp
= FALSE
;
2269 // Check whether all enabled APs are idle.
2270 // If any enabled AP is not idle, return EFI_NOT_READY.
2272 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2273 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2274 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2275 ApState
= GetApState (CpuData
);
2276 if (ApState
!= CpuStateDisabled
) {
2277 HasEnabledAp
= TRUE
;
2278 if (ApState
!= CpuStateIdle
) {
2280 // If any enabled APs are busy, return EFI_NOT_READY.
2282 return EFI_NOT_READY
;
2288 if (!HasEnabledAp
&& ExcludeBsp
) {
2290 // If no enabled AP exists and not include Bsp to do the procedure, return EFI_NOT_STARTED.
2292 return EFI_NOT_STARTED
;
2295 CpuMpData
->RunningCount
= 0;
2296 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2297 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2298 CpuData
->Waiting
= FALSE
;
2299 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2300 if (CpuData
->State
== CpuStateIdle
) {
2302 // Mark this processor as responsible for current calling.
2304 CpuData
->Waiting
= TRUE
;
2305 CpuMpData
->RunningCount
++;
2310 CpuMpData
->Procedure
= Procedure
;
2311 CpuMpData
->ProcArguments
= ProcedureArgument
;
2312 CpuMpData
->SingleThread
= SingleThread
;
2313 CpuMpData
->FinishedCount
= 0;
2314 CpuMpData
->FailedCpuList
= FailedCpuList
;
2315 CpuMpData
->ExpectedTime
= CalculateTimeout (
2316 TimeoutInMicroseconds
,
2317 &CpuMpData
->CurrentTime
2319 CpuMpData
->TotalTime
= 0;
2320 CpuMpData
->WaitEvent
= WaitEvent
;
2322 if (!SingleThread
) {
2323 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2325 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2326 if (ProcessorNumber
== CallerNumber
) {
2329 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2330 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2340 Procedure (ProcedureArgument
);
2343 Status
= EFI_SUCCESS
;
2344 if (WaitEvent
== NULL
) {
2346 Status
= CheckAllAPs ();
2347 } while (Status
== EFI_NOT_READY
);
2354 Worker function to let the caller get one enabled AP to execute a caller-provided
2357 @param[in] Procedure A pointer to the function to be run on
2358 enabled APs of the system.
2359 @param[in] ProcessorNumber The handle number of the AP.
2360 @param[in] WaitEvent The event created by the caller with CreateEvent()
2362 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2363 APs to return from Procedure, either for
2364 blocking or non-blocking mode.
2365 @param[in] ProcedureArgument The parameter passed into Procedure for
2367 @param[out] Finished If AP returns from Procedure before the
2368 timeout expires, its content is set to TRUE.
2369 Otherwise, the value is set to FALSE.
2371 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2372 the timeout expires.
2373 @retval others Failed to Startup AP.
2377 StartupThisAPWorker (
2378 IN EFI_AP_PROCEDURE Procedure
,
2379 IN UINTN ProcessorNumber
,
2380 IN EFI_EVENT WaitEvent OPTIONAL
,
2381 IN UINTN TimeoutInMicroseconds
,
2382 IN VOID
*ProcedureArgument OPTIONAL
,
2383 OUT BOOLEAN
*Finished OPTIONAL
2387 CPU_MP_DATA
*CpuMpData
;
2388 CPU_AP_DATA
*CpuData
;
2391 CpuMpData
= GetCpuMpData ();
2393 if (Finished
!= NULL
) {
2398 // Check whether caller processor is BSP
2400 MpInitLibWhoAmI (&CallerNumber
);
2401 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2402 return EFI_DEVICE_ERROR
;
2406 // Check whether processor with the handle specified by ProcessorNumber exists
2408 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2409 return EFI_NOT_FOUND
;
2413 // Check whether specified processor is BSP
2415 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2416 return EFI_INVALID_PARAMETER
;
2420 // Check parameter Procedure
2422 if (Procedure
== NULL
) {
2423 return EFI_INVALID_PARAMETER
;
2429 CheckAndUpdateApsStatus ();
2432 // Check whether specified AP is disabled
2434 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2435 return EFI_INVALID_PARAMETER
;
2439 // If WaitEvent is not NULL, execute in non-blocking mode.
2440 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2441 // CheckAPsStatus() will check completion and timeout periodically.
2443 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2444 CpuData
->WaitEvent
= WaitEvent
;
2445 CpuData
->Finished
= Finished
;
2446 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2447 CpuData
->TotalTime
= 0;
2449 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2452 // If WaitEvent is NULL, execute in blocking mode.
2453 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2455 Status
= EFI_SUCCESS
;
2456 if (WaitEvent
== NULL
) {
2458 Status
= CheckThisAP (ProcessorNumber
);
2459 } while (Status
== EFI_NOT_READY
);
2466 Get pointer to CPU MP Data structure from GUIDed HOB.
2468 @return The pointer to CPU MP Data structure.
2471 GetCpuMpDataFromGuidedHob (
2475 EFI_HOB_GUID_TYPE
*GuidHob
;
2477 CPU_MP_DATA
*CpuMpData
;
2480 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2481 if (GuidHob
!= NULL
) {
2482 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2483 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2489 This service executes a caller provided function on all enabled CPUs.
2491 @param[in] Procedure A pointer to the function to be run on
2492 enabled APs of the system. See type
2494 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2495 APs to return from Procedure, either for
2496 blocking or non-blocking mode. Zero means
2497 infinity. TimeoutInMicroseconds is ignored
2499 @param[in] ProcedureArgument The parameter passed into Procedure for
2502 @retval EFI_SUCCESS In blocking mode, all CPUs have finished before
2503 the timeout expired.
2504 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2505 to all enabled CPUs.
2506 @retval EFI_DEVICE_ERROR Caller processor is AP.
2507 @retval EFI_NOT_READY Any enabled APs are busy.
2508 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2509 @retval EFI_TIMEOUT In blocking mode, the timeout expired before
2510 all enabled APs have finished.
2511 @retval EFI_INVALID_PARAMETER Procedure is NULL.
2516 MpInitLibStartupAllCPUs (
2517 IN EFI_AP_PROCEDURE Procedure
,
2518 IN UINTN TimeoutInMicroseconds
,
2519 IN VOID
*ProcedureArgument OPTIONAL
2522 return StartupAllCPUsWorker (
2527 TimeoutInMicroseconds
,