2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 EFI_GUID mCpuInitMpLibHobGuid
= CPU_INIT_MP_LIB_HOB_GUID
;
20 The function will check if BSP Execute Disable is enabled.
22 DxeIpl may have enabled Execute Disable for BSP, APs need to
23 get the status and sync up the settings.
24 If BSP's CR0.Paging is not set, BSP execute Disble feature is
27 @retval TRUE BSP Execute Disable is enabled.
28 @retval FALSE BSP Execute Disable is not enabled.
31 IsBspExecuteDisableEnabled (
36 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
37 MSR_IA32_EFER_REGISTER EferMsr
;
42 Cr0
.UintN
= AsmReadCr0 ();
43 if (Cr0
.Bits
.PG
!= 0) {
45 // If CR0 Paging bit is set
47 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
48 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
49 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
52 // Bit 20: Execute Disable Bit available.
54 if (Edx
.Bits
.NX
!= 0) {
55 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
58 // Bit 11: Execute Disable Bit enable.
60 if (EferMsr
.Bits
.NXE
!= 0) {
71 Worker function for SwitchBSP().
73 Worker function for SwitchBSP(), assigned to the AP which is intended
76 @param[in] Buffer Pointer to CPU MP Data
84 CPU_MP_DATA
*DataInHob
;
86 DataInHob
= (CPU_MP_DATA
*) Buffer
;
87 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
91 Get the Application Processors state.
93 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
99 IN CPU_AP_DATA
*CpuData
102 return CpuData
->State
;
106 Set the Application Processors state.
108 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
109 @param[in] State The AP status
113 IN CPU_AP_DATA
*CpuData
,
117 AcquireSpinLock (&CpuData
->ApLock
);
118 CpuData
->State
= State
;
119 ReleaseSpinLock (&CpuData
->ApLock
);
123 Save BSP's local APIC timer setting.
125 @param[in] CpuMpData Pointer to CPU MP Data
128 SaveLocalApicTimerSetting (
129 IN CPU_MP_DATA
*CpuMpData
133 // Record the current local APIC timer setting of BSP
136 &CpuMpData
->DivideValue
,
137 &CpuMpData
->PeriodicMode
,
140 CpuMpData
->CurrentTimerCount
= GetApicTimerCurrentCount ();
141 CpuMpData
->TimerInterruptState
= GetApicTimerInterruptState ();
145 Sync local APIC timer setting from BSP to AP.
147 @param[in] CpuMpData Pointer to CPU MP Data
150 SyncLocalApicTimerSetting (
151 IN CPU_MP_DATA
*CpuMpData
155 // Sync local APIC timer setting from BSP to AP
157 InitializeApicTimer (
158 CpuMpData
->DivideValue
,
159 CpuMpData
->CurrentTimerCount
,
160 CpuMpData
->PeriodicMode
,
164 // Disable AP's local APIC timer interrupt
166 DisableApicTimerInterrupt ();
170 Save the volatile registers required to be restored following INIT IPI.
172 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
175 SaveVolatileRegisters (
176 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
179 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
181 VolatileRegisters
->Cr0
= AsmReadCr0 ();
182 VolatileRegisters
->Cr3
= AsmReadCr3 ();
183 VolatileRegisters
->Cr4
= AsmReadCr4 ();
185 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
186 if (VersionInfoEdx
.Bits
.DE
!= 0) {
188 // If processor supports Debugging Extensions feature
189 // by CPUID.[EAX=01H]:EDX.BIT2
191 VolatileRegisters
->Dr0
= AsmReadDr0 ();
192 VolatileRegisters
->Dr1
= AsmReadDr1 ();
193 VolatileRegisters
->Dr2
= AsmReadDr2 ();
194 VolatileRegisters
->Dr3
= AsmReadDr3 ();
195 VolatileRegisters
->Dr6
= AsmReadDr6 ();
196 VolatileRegisters
->Dr7
= AsmReadDr7 ();
199 AsmReadGdtr (&VolatileRegisters
->Gdtr
);
200 AsmReadIdtr (&VolatileRegisters
->Idtr
);
201 VolatileRegisters
->Tr
= AsmReadTr ();
205 Restore the volatile registers following INIT IPI.
207 @param[in] VolatileRegisters Pointer to volatile resisters
208 @param[in] IsRestoreDr TRUE: Restore DRx if supported
209 FALSE: Do not restore DRx
212 RestoreVolatileRegisters (
213 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
214 IN BOOLEAN IsRestoreDr
217 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
218 IA32_TSS_DESCRIPTOR
*Tss
;
220 AsmWriteCr0 (VolatileRegisters
->Cr0
);
221 AsmWriteCr3 (VolatileRegisters
->Cr3
);
222 AsmWriteCr4 (VolatileRegisters
->Cr4
);
225 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
226 if (VersionInfoEdx
.Bits
.DE
!= 0) {
228 // If processor supports Debugging Extensions feature
229 // by CPUID.[EAX=01H]:EDX.BIT2
231 AsmWriteDr0 (VolatileRegisters
->Dr0
);
232 AsmWriteDr1 (VolatileRegisters
->Dr1
);
233 AsmWriteDr2 (VolatileRegisters
->Dr2
);
234 AsmWriteDr3 (VolatileRegisters
->Dr3
);
235 AsmWriteDr6 (VolatileRegisters
->Dr6
);
236 AsmWriteDr7 (VolatileRegisters
->Dr7
);
240 AsmWriteGdtr (&VolatileRegisters
->Gdtr
);
241 AsmWriteIdtr (&VolatileRegisters
->Idtr
);
242 if (VolatileRegisters
->Tr
!= 0 &&
243 VolatileRegisters
->Tr
< VolatileRegisters
->Gdtr
.Limit
) {
244 Tss
= (IA32_TSS_DESCRIPTOR
*)(VolatileRegisters
->Gdtr
.Base
+
245 VolatileRegisters
->Tr
);
246 if (Tss
->Bits
.P
== 1) {
247 Tss
->Bits
.Type
&= 0xD; // 1101 - Clear busy bit just in case
248 AsmWriteTr (VolatileRegisters
->Tr
);
254 Detect whether Mwait-monitor feature is supported.
256 @retval TRUE Mwait-monitor feature is supported.
257 @retval FALSE Mwait-monitor feature is not supported.
264 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
266 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
267 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
273 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
275 @return The AP loop mode.
279 OUT UINT32
*MonitorFilterSize
283 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
285 ASSERT (MonitorFilterSize
!= NULL
);
287 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
288 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
289 if (ApLoopMode
== ApInMwaitLoop
) {
290 if (!IsMwaitSupport ()) {
292 // If processor does not support MONITOR/MWAIT feature,
293 // force AP in Hlt-loop mode
295 ApLoopMode
= ApInHltLoop
;
299 if (ApLoopMode
!= ApInMwaitLoop
) {
300 *MonitorFilterSize
= sizeof (UINT32
);
303 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
304 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
306 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
307 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
314 Sort the APIC ID of all processors.
316 This function sorts the APIC ID of all processors so that processor number is
317 assigned in the ascending order of APIC ID which eases MP debugging.
319 @param[in] CpuMpData Pointer to PEI CPU MP Data
323 IN CPU_MP_DATA
*CpuMpData
330 CPU_INFO_IN_HOB CpuInfo
;
332 CPU_INFO_IN_HOB
*CpuInfoInHob
;
333 volatile UINT32
*StartupApSignal
;
335 ApCount
= CpuMpData
->CpuCount
- 1;
336 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
338 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
341 // Sort key is the hardware default APIC ID
343 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
344 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
345 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
347 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
350 if (Index3
!= Index1
) {
351 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
353 &CpuInfoInHob
[Index3
],
354 &CpuInfoInHob
[Index1
],
355 sizeof (CPU_INFO_IN_HOB
)
357 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
360 // Also exchange the StartupApSignal.
362 StartupApSignal
= CpuMpData
->CpuData
[Index3
].StartupApSignal
;
363 CpuMpData
->CpuData
[Index3
].StartupApSignal
=
364 CpuMpData
->CpuData
[Index1
].StartupApSignal
;
365 CpuMpData
->CpuData
[Index1
].StartupApSignal
= StartupApSignal
;
370 // Get the processor number for the BSP
372 ApicId
= GetInitialApicId ();
373 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
374 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
375 CpuMpData
->BspNumber
= (UINT32
) Index1
;
383 Enable x2APIC mode on APs.
385 @param[in, out] Buffer Pointer to private data buffer.
393 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
399 @param[in, out] Buffer Pointer to private data buffer.
407 CPU_MP_DATA
*CpuMpData
;
409 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
411 // Load microcode on AP
413 MicrocodeDetect (CpuMpData
, FALSE
);
415 // Sync BSP's MTRR table to AP
417 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
421 Find the current Processor number by APIC ID.
423 @param[in] CpuMpData Pointer to PEI CPU MP Data
424 @param[out] ProcessorNumber Return the pocessor number found
426 @retval EFI_SUCCESS ProcessorNumber is found and returned.
427 @retval EFI_NOT_FOUND ProcessorNumber is not found.
431 IN CPU_MP_DATA
*CpuMpData
,
432 OUT UINTN
*ProcessorNumber
435 UINTN TotalProcessorNumber
;
437 CPU_INFO_IN_HOB
*CpuInfoInHob
;
438 UINT32 CurrentApicId
;
440 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
442 TotalProcessorNumber
= CpuMpData
->CpuCount
;
443 CurrentApicId
= GetApicId ();
444 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
445 if (CpuInfoInHob
[Index
].ApicId
== CurrentApicId
) {
446 *ProcessorNumber
= Index
;
451 return EFI_NOT_FOUND
;
455 This function will get CPU count in the system.
457 @param[in] CpuMpData Pointer to PEI CPU MP Data
459 @return CPU count detected
462 CollectProcessorCount (
463 IN CPU_MP_DATA
*CpuMpData
469 // Send 1st broadcast IPI to APs to wakeup APs
471 CpuMpData
->InitFlag
= ApInitConfig
;
472 CpuMpData
->X2ApicEnable
= FALSE
;
473 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
);
474 CpuMpData
->InitFlag
= ApInitDone
;
475 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
477 // Wait for all APs finished the initialization
479 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
483 if (CpuMpData
->CpuCount
> 255) {
485 // If there are more than 255 processor found, force to enable X2APIC
487 CpuMpData
->X2ApicEnable
= TRUE
;
489 if (CpuMpData
->X2ApicEnable
) {
490 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
492 // Wakeup all APs to enable x2APIC mode
494 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
496 // Wait for all known APs finished
498 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
502 // Enable x2APIC on BSP
504 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
506 // Set BSP/Aps state to IDLE
508 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
509 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
512 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
514 // Sort BSP/Aps by CPU APIC ID in ascending order
516 SortApicId (CpuMpData
);
518 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
520 return CpuMpData
->CpuCount
;
524 Initialize CPU AP Data when AP is wakeup at the first time.
526 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
527 @param[in] ProcessorNumber The handle number of processor
528 @param[in] BistData Processor BIST data
529 @param[in] ApTopOfStack Top of AP stack
534 IN OUT CPU_MP_DATA
*CpuMpData
,
535 IN UINTN ProcessorNumber
,
537 IN UINT64 ApTopOfStack
540 CPU_INFO_IN_HOB
*CpuInfoInHob
;
542 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
543 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
544 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
545 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
546 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
548 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
549 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
550 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
552 // Set x2APIC mode if there are any logical processor reporting
553 // an Initial APIC ID of 255 or greater.
555 AcquireSpinLock(&CpuMpData
->MpLock
);
556 CpuMpData
->X2ApicEnable
= TRUE
;
557 ReleaseSpinLock(&CpuMpData
->MpLock
);
560 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
561 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
565 This function will be called from AP reset code if BSP uses WakeUpAP.
567 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
568 @param[in] ApIndex Number of current executing AP
573 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
577 CPU_MP_DATA
*CpuMpData
;
578 UINTN ProcessorNumber
;
579 EFI_AP_PROCEDURE Procedure
;
582 volatile UINT32
*ApStartupSignalBuffer
;
583 CPU_INFO_IN_HOB
*CpuInfoInHob
;
585 UINTN CurrentApicMode
;
588 // AP finished assembly code and begin to execute C code
590 CpuMpData
= ExchangeInfo
->CpuMpData
;
593 // AP's local APIC settings will be lost after received INIT IPI
594 // We need to re-initialize them at here
596 ProgramVirtualWireMode ();
598 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
600 DisableLvtInterrupts ();
601 SyncLocalApicTimerSetting (CpuMpData
);
603 CurrentApicMode
= GetApicMode ();
605 if (CpuMpData
->InitFlag
== ApInitConfig
) {
609 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
610 ProcessorNumber
= ApIndex
;
612 // This is first time AP wakeup, get BIST information from AP stack
614 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
615 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
617 // Do some AP initialize sync
619 ApInitializeSync (CpuMpData
);
621 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
622 // to initialize AP in InitConfig path.
623 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
625 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
626 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
627 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
630 // Execute AP function if AP is ready
632 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
634 // Clear AP start-up signal when AP waken up
636 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
637 InterlockedCompareExchange32 (
638 (UINT32
*) ApStartupSignalBuffer
,
642 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
644 // Restore AP's volatile registers saved
646 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
649 // The CPU driver might not flush TLB for APs on spot after updating
650 // page attributes. AP in mwait loop mode needs to take care of it when
656 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
657 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
658 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
659 if (Procedure
!= NULL
) {
660 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
662 // Enable source debugging on AP function
666 // Invoke AP function here
668 Procedure (Parameter
);
669 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
670 if (CpuMpData
->SwitchBspFlag
) {
672 // Re-get the processor number due to BSP/AP maybe exchange in AP function
674 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
675 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
676 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
677 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
678 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
680 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
681 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
682 if (CurrentApicMode
!= GetApicMode ()) {
684 // If APIC mode change happened during AP function execution,
685 // we do not support APIC ID value changed.
691 // Re-get the CPU APICID and Initial APICID if they are changed
693 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
694 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
699 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
704 // AP finished executing C code
706 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
707 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
710 // Place AP is specified loop mode
712 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
714 // Save AP volatile registers
716 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
718 // Place AP in HLT-loop
721 DisableInterrupts ();
727 DisableInterrupts ();
728 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
730 // Place AP in MWAIT-loop
732 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
733 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
735 // Check AP start-up signal again.
736 // If AP start-up signal is not set, place AP into
737 // the specified C-state
739 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
741 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
743 // Place AP in Run-loop
751 // If AP start-up signal is written, AP is waken up
752 // otherwise place AP in loop again
754 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
762 Wait for AP wakeup and write AP start-up signal till AP is waken up.
764 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
768 IN
volatile UINT32
*ApStartupSignalBuffer
772 // If AP is waken up, StartupApSignal should be cleared.
773 // Otherwise, write StartupApSignal again till AP waken up.
775 while (InterlockedCompareExchange32 (
776 (UINT32
*) ApStartupSignalBuffer
,
785 This function will fill the exchange info structure.
787 @param[in] CpuMpData Pointer to CPU MP Data
791 FillExchangeInfoData (
792 IN CPU_MP_DATA
*CpuMpData
795 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
797 IA32_SEGMENT_DESCRIPTOR
*Selector
;
799 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
800 ExchangeInfo
->Lock
= 0;
801 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
802 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
803 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
804 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
806 ExchangeInfo
->CodeSegment
= AsmReadCs ();
807 ExchangeInfo
->DataSegment
= AsmReadDs ();
809 ExchangeInfo
->Cr3
= AsmReadCr3 ();
811 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
812 ExchangeInfo
->ApIndex
= 0;
813 ExchangeInfo
->NumApsExecuting
= 0;
814 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
815 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
816 ExchangeInfo
->CpuMpData
= CpuMpData
;
818 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
820 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
823 // Get the BSP's data of GDT and IDT
825 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
826 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
829 // Find a 32-bit code segment
831 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
832 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
834 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
835 ExchangeInfo
->ModeTransitionSegment
=
836 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
840 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
844 // Copy all 32-bit code and 64-bit code into memory with type of
845 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
847 if (CpuMpData
->WakeupBufferHigh
!= 0) {
848 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
849 CpuMpData
->AddressMap
.ModeTransitionOffset
;
851 (VOID
*)CpuMpData
->WakeupBufferHigh
,
852 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
853 CpuMpData
->AddressMap
.ModeTransitionOffset
,
857 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
859 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
860 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
863 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
864 (UINT32
)ExchangeInfo
->ModeOffset
-
865 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
866 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
870 Helper function that waits until the finished AP count reaches the specified
871 limit, or the specified timeout elapses (whichever comes first).
873 @param[in] CpuMpData Pointer to CPU MP Data.
874 @param[in] FinishedApLimit The number of finished APs to wait for.
875 @param[in] TimeLimit The number of microseconds to wait for.
878 TimedWaitForApFinish (
879 IN CPU_MP_DATA
*CpuMpData
,
880 IN UINT32 FinishedApLimit
,
885 Get available system memory below 1MB by specified size.
887 @param[in] CpuMpData The pointer to CPU MP Data structure.
890 BackupAndPrepareWakeupBuffer(
891 IN CPU_MP_DATA
*CpuMpData
895 (VOID
*) CpuMpData
->BackupBuffer
,
896 (VOID
*) CpuMpData
->WakeupBuffer
,
897 CpuMpData
->BackupBufferSize
900 (VOID
*) CpuMpData
->WakeupBuffer
,
901 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
902 CpuMpData
->AddressMap
.RendezvousFunnelSize
907 Restore wakeup buffer data.
909 @param[in] CpuMpData The pointer to CPU MP Data structure.
913 IN CPU_MP_DATA
*CpuMpData
917 (VOID
*) CpuMpData
->WakeupBuffer
,
918 (VOID
*) CpuMpData
->BackupBuffer
,
919 CpuMpData
->BackupBufferSize
924 Allocate reset vector buffer.
926 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
929 AllocateResetVector (
930 IN OUT CPU_MP_DATA
*CpuMpData
933 UINTN ApResetVectorSize
;
935 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
936 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
937 sizeof (MP_CPU_EXCHANGE_INFO
);
939 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
940 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
941 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
942 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
943 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
944 CpuMpData
->AddressMap
.ModeTransitionOffset
947 BackupAndPrepareWakeupBuffer (CpuMpData
);
951 Free AP reset vector buffer.
953 @param[in] CpuMpData The pointer to CPU MP Data structure.
957 IN CPU_MP_DATA
*CpuMpData
960 RestoreWakeupBuffer (CpuMpData
);
964 This function will be called by BSP to wakeup AP.
966 @param[in] CpuMpData Pointer to CPU MP Data
967 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
968 FALSE: Send IPI to AP by ApicId
969 @param[in] ProcessorNumber The handle number of specified processor
970 @param[in] Procedure The function to be invoked by AP
971 @param[in] ProcedureArgument The argument to be passed into AP function
975 IN CPU_MP_DATA
*CpuMpData
,
976 IN BOOLEAN Broadcast
,
977 IN UINTN ProcessorNumber
,
978 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
979 IN VOID
*ProcedureArgument OPTIONAL
982 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
984 CPU_AP_DATA
*CpuData
;
985 BOOLEAN ResetVectorRequired
;
986 CPU_INFO_IN_HOB
*CpuInfoInHob
;
988 CpuMpData
->FinishedCount
= 0;
989 ResetVectorRequired
= FALSE
;
991 if (CpuMpData
->WakeUpByInitSipiSipi
||
992 CpuMpData
->InitFlag
!= ApInitDone
) {
993 ResetVectorRequired
= TRUE
;
994 AllocateResetVector (CpuMpData
);
995 FillExchangeInfoData (CpuMpData
);
996 SaveLocalApicTimerSetting (CpuMpData
);
999 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
1001 // Get AP target C-state each time when waking up AP,
1002 // for it maybe updated by platform again
1004 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1007 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1010 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1011 if (Index
!= CpuMpData
->BspNumber
) {
1012 CpuData
= &CpuMpData
->CpuData
[Index
];
1013 CpuData
->ApFunction
= (UINTN
) Procedure
;
1014 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1015 SetApState (CpuData
, CpuStateReady
);
1016 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1017 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1021 if (ResetVectorRequired
) {
1025 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1027 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1029 // Here support two methods to collect AP count through adjust
1030 // PcdCpuApInitTimeOutInMicroSeconds values.
1032 // one way is set a value to just let the first AP to start the
1033 // initialization, then through the later while loop to wait all Aps
1034 // finsh the initialization.
1035 // The other way is set a value to let all APs finished the initialzation.
1036 // In this case, the later while loop is useless.
1038 TimedWaitForApFinish (
1040 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1041 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1044 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1049 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1051 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1052 CpuData
= &CpuMpData
->CpuData
[Index
];
1053 if (Index
!= CpuMpData
->BspNumber
) {
1054 WaitApWakeup (CpuData
->StartupApSignal
);
1059 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1060 CpuData
->ApFunction
= (UINTN
) Procedure
;
1061 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1062 SetApState (CpuData
, CpuStateReady
);
1064 // Wakeup specified AP
1066 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1067 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1068 if (ResetVectorRequired
) {
1069 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1071 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1072 (UINT32
) ExchangeInfo
->BufferStart
1076 // Wait specified AP waken up
1078 WaitApWakeup (CpuData
->StartupApSignal
);
1081 if (ResetVectorRequired
) {
1082 FreeResetVector (CpuMpData
);
1086 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1087 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1088 // S3SmmInitDone Ppi.
1090 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1094 Calculate timeout value and return the current performance counter value.
1096 Calculate the number of performance counter ticks required for a timeout.
1097 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1100 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1101 @param[out] CurrentTime Returns the current value of the performance counter.
1103 @return Expected time stamp counter for timeout.
1104 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1110 IN UINTN TimeoutInMicroseconds
,
1111 OUT UINT64
*CurrentTime
1114 UINT64 TimeoutInSeconds
;
1115 UINT64 TimestampCounterFreq
;
1118 // Read the current value of the performance counter
1120 *CurrentTime
= GetPerformanceCounter ();
1123 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1126 if (TimeoutInMicroseconds
== 0) {
1131 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1134 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1137 // Check the potential overflow before calculate the number of ticks for the timeout value.
1139 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1141 // Convert microseconds into seconds if direct multiplication overflows
1143 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1145 // Assertion if the final tick count exceeds MAX_UINT64
1147 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1148 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1151 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1152 // it by 1,000,000, to get the number of ticks for the timeout value.
1156 TimestampCounterFreq
,
1157 TimeoutInMicroseconds
1165 Checks whether timeout expires.
1167 Check whether the number of elapsed performance counter ticks required for
1168 a timeout condition has been reached.
1169 If Timeout is zero, which means infinity, return value is always FALSE.
1171 @param[in, out] PreviousTime On input, the value of the performance counter
1172 when it was last read.
1173 On output, the current value of the performance
1175 @param[in] TotalTime The total amount of elapsed time in performance
1177 @param[in] Timeout The number of performance counter ticks required
1178 to reach a timeout condition.
1180 @retval TRUE A timeout condition has been reached.
1181 @retval FALSE A timeout condition has not been reached.
1186 IN OUT UINT64
*PreviousTime
,
1187 IN UINT64
*TotalTime
,
1200 GetPerformanceCounterProperties (&Start
, &End
);
1201 Cycle
= End
- Start
;
1206 CurrentTime
= GetPerformanceCounter();
1207 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1214 *TotalTime
+= Delta
;
1215 *PreviousTime
= CurrentTime
;
1216 if (*TotalTime
> Timeout
) {
1223 Helper function that waits until the finished AP count reaches the specified
1224 limit, or the specified timeout elapses (whichever comes first).
1226 @param[in] CpuMpData Pointer to CPU MP Data.
1227 @param[in] FinishedApLimit The number of finished APs to wait for.
1228 @param[in] TimeLimit The number of microseconds to wait for.
1231 TimedWaitForApFinish (
1232 IN CPU_MP_DATA
*CpuMpData
,
1233 IN UINT32 FinishedApLimit
,
1238 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1239 // "infinity", so check for (TimeLimit == 0) explicitly.
1241 if (TimeLimit
== 0) {
1245 CpuMpData
->TotalTime
= 0;
1246 CpuMpData
->ExpectedTime
= CalculateTimeout (
1248 &CpuMpData
->CurrentTime
1250 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1252 &CpuMpData
->CurrentTime
,
1253 &CpuMpData
->TotalTime
,
1254 CpuMpData
->ExpectedTime
1259 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1262 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1265 DivU64x64Remainder (
1266 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1267 GetPerformanceCounterProperties (NULL
, NULL
),
1275 Reset an AP to Idle state.
1277 Any task being executed by the AP will be aborted and the AP
1278 will be waiting for a new task in Wait-For-SIPI state.
1280 @param[in] ProcessorNumber The handle number of processor.
1283 ResetProcessorToIdleState (
1284 IN UINTN ProcessorNumber
1287 CPU_MP_DATA
*CpuMpData
;
1289 CpuMpData
= GetCpuMpData ();
1291 CpuMpData
->InitFlag
= ApInitReconfig
;
1292 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
1293 while (CpuMpData
->FinishedCount
< 1) {
1296 CpuMpData
->InitFlag
= ApInitDone
;
1298 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1302 Searches for the next waiting AP.
1304 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1306 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1308 @retval EFI_SUCCESS The next waiting AP has been found.
1309 @retval EFI_NOT_FOUND No waiting AP exists.
1313 GetNextWaitingProcessorNumber (
1314 OUT UINTN
*NextProcessorNumber
1317 UINTN ProcessorNumber
;
1318 CPU_MP_DATA
*CpuMpData
;
1320 CpuMpData
= GetCpuMpData ();
1322 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1323 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1324 *NextProcessorNumber
= ProcessorNumber
;
1329 return EFI_NOT_FOUND
;
1332 /** Checks status of specified AP.
1334 This function checks whether the specified AP has finished the task assigned
1335 by StartupThisAP(), and whether timeout expires.
1337 @param[in] ProcessorNumber The handle number of processor.
1339 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1340 @retval EFI_TIMEOUT The timeout expires.
1341 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1345 IN UINTN ProcessorNumber
1348 CPU_MP_DATA
*CpuMpData
;
1349 CPU_AP_DATA
*CpuData
;
1351 CpuMpData
= GetCpuMpData ();
1352 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1355 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1356 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1357 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1360 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1362 if (GetApState(CpuData
) == CpuStateIdle
) {
1363 if (CpuData
->Finished
!= NULL
) {
1364 *(CpuData
->Finished
) = TRUE
;
1369 // If timeout expires for StartupThisAP(), report timeout.
1371 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1372 if (CpuData
->Finished
!= NULL
) {
1373 *(CpuData
->Finished
) = FALSE
;
1376 // Reset failed AP to idle state
1378 ResetProcessorToIdleState (ProcessorNumber
);
1383 return EFI_NOT_READY
;
1387 Checks status of all APs.
1389 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1390 and whether timeout expires.
1392 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1393 @retval EFI_TIMEOUT The timeout expires.
1394 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1401 UINTN ProcessorNumber
;
1402 UINTN NextProcessorNumber
;
1405 CPU_MP_DATA
*CpuMpData
;
1406 CPU_AP_DATA
*CpuData
;
1408 CpuMpData
= GetCpuMpData ();
1410 NextProcessorNumber
= 0;
1413 // Go through all APs that are responsible for the StartupAllAPs().
1415 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1416 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1420 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1422 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1423 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1424 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1426 if (GetApState(CpuData
) == CpuStateIdle
) {
1427 CpuMpData
->RunningCount
--;
1428 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1431 // If in Single Thread mode, then search for the next waiting AP for execution.
1433 if (CpuMpData
->SingleThread
) {
1434 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1436 if (!EFI_ERROR (Status
)) {
1440 (UINT32
) NextProcessorNumber
,
1441 CpuMpData
->Procedure
,
1442 CpuMpData
->ProcArguments
1450 // If all APs finish, return EFI_SUCCESS.
1452 if (CpuMpData
->RunningCount
== 0) {
1457 // If timeout expires, report timeout.
1460 &CpuMpData
->CurrentTime
,
1461 &CpuMpData
->TotalTime
,
1462 CpuMpData
->ExpectedTime
)
1465 // If FailedCpuList is not NULL, record all failed APs in it.
1467 if (CpuMpData
->FailedCpuList
!= NULL
) {
1468 *CpuMpData
->FailedCpuList
=
1469 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1470 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1474 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1476 // Check whether this processor is responsible for StartupAllAPs().
1478 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1480 // Reset failed APs to idle state
1482 ResetProcessorToIdleState (ProcessorNumber
);
1483 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1484 if (CpuMpData
->FailedCpuList
!= NULL
) {
1485 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1489 if (CpuMpData
->FailedCpuList
!= NULL
) {
1490 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1494 return EFI_NOT_READY
;
1498 MP Initialize Library initialization.
1500 This service will allocate AP reset vector and wakeup all APs to do APs
1503 This service must be invoked before all other MP Initialize Library
1504 service are invoked.
1506 @retval EFI_SUCCESS MP initialization succeeds.
1507 @retval Others MP initialization fails.
1512 MpInitLibInitialize (
1516 CPU_MP_DATA
*OldCpuMpData
;
1517 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1518 UINT32 MaxLogicalProcessorNumber
;
1520 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1521 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1523 UINT32 MonitorFilterSize
;
1526 CPU_MP_DATA
*CpuMpData
;
1528 UINT8
*MonitorBuffer
;
1530 UINTN ApResetVectorSize
;
1531 UINTN BackupBufferAddr
;
1533 VOID
*MicrocodePatchInRam
;
1535 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1536 if (OldCpuMpData
== NULL
) {
1537 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1539 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1541 ASSERT (MaxLogicalProcessorNumber
!= 0);
1543 AsmGetAddressMap (&AddressMap
);
1544 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1545 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1546 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1549 // Save BSP's Control registers for APs
1551 SaveVolatileRegisters (&VolatileRegisters
);
1553 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1554 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1555 BufferSize
+= ApResetVectorSize
;
1556 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1557 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1558 BufferSize
+= sizeof (CPU_MP_DATA
);
1559 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1560 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1561 ASSERT (MpBuffer
!= NULL
);
1562 ZeroMem (MpBuffer
, BufferSize
);
1563 Buffer
= (UINTN
) MpBuffer
;
1566 // The layout of the Buffer is as below:
1568 // +--------------------+ <-- Buffer
1570 // +--------------------+ <-- MonitorBuffer
1571 // AP Monitor Filters (N)
1572 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1574 // +--------------------+
1576 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1577 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1578 // +--------------------+ <-- CpuMpData
1580 // +--------------------+ <-- CpuMpData->CpuData
1582 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1583 // CPU_INFO_IN_HOB (N)
1584 // +--------------------+
1586 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1587 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1588 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1589 CpuMpData
= (CPU_MP_DATA
*) (ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1590 CpuMpData
->Buffer
= Buffer
;
1591 CpuMpData
->CpuApStackSize
= ApStackSize
;
1592 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1593 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1594 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1595 CpuMpData
->CpuCount
= 1;
1596 CpuMpData
->BspNumber
= 0;
1597 CpuMpData
->WaitEvent
= NULL
;
1598 CpuMpData
->SwitchBspFlag
= FALSE
;
1599 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1600 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1601 CpuMpData
->MicrocodePatchRegionSize
= PcdGet64 (PcdCpuMicrocodePatchRegionSize
);
1603 // If platform has more than one CPU, relocate microcode to memory to reduce
1604 // loading microcode time.
1606 MicrocodePatchInRam
= NULL
;
1607 if (MaxLogicalProcessorNumber
> 1) {
1608 MicrocodePatchInRam
= AllocatePages (
1610 (UINTN
)CpuMpData
->MicrocodePatchRegionSize
1614 if (MicrocodePatchInRam
== NULL
) {
1616 // there is only one processor, or no microcode patch is available, or
1617 // memory allocation failed
1619 CpuMpData
->MicrocodePatchAddress
= PcdGet64 (PcdCpuMicrocodePatchAddress
);
1622 // there are multiple processors, and a microcode patch is available, and
1623 // memory allocation succeeded
1626 MicrocodePatchInRam
,
1627 (VOID
*)(UINTN
)PcdGet64 (PcdCpuMicrocodePatchAddress
),
1628 (UINTN
)CpuMpData
->MicrocodePatchRegionSize
1630 CpuMpData
->MicrocodePatchAddress
= (UINTN
)MicrocodePatchInRam
;
1633 InitializeSpinLock(&CpuMpData
->MpLock
);
1636 // Make sure no memory usage outside of the allocated buffer.
1638 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1639 Buffer
+ BufferSize
);
1642 // Duplicate BSP's IDT to APs.
1643 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1645 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1646 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1647 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1649 // Set BSP basic information
1651 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1653 // Save assembly code information
1655 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1657 // Finally set AP loop mode
1659 CpuMpData
->ApLoopMode
= ApLoopMode
;
1660 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1662 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1665 // Set up APs wakeup signal buffer
1667 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1668 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1669 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1672 // Load Microcode on BSP
1674 MicrocodeDetect (CpuMpData
, TRUE
);
1676 // Store BSP's MTRR setting
1678 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1680 // Enable the local APIC for Virtual Wire Mode.
1682 ProgramVirtualWireMode ();
1684 if (OldCpuMpData
== NULL
) {
1685 if (MaxLogicalProcessorNumber
> 1) {
1687 // Wakeup all APs and calculate the processor count in system
1689 CollectProcessorCount (CpuMpData
);
1693 // APs have been wakeup before, just get the CPU Information
1696 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1697 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1698 CpuMpData
->InitFlag
= ApInitReconfig
;
1699 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1700 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1701 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1702 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1703 if (CpuInfoInHob
[Index
].InitialApicId
>= 255 || Index
> 254) {
1704 CpuMpData
->X2ApicEnable
= TRUE
;
1706 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1707 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1708 CopyMem (&CpuMpData
->CpuData
[Index
].VolatileRegisters
, &VolatileRegisters
, sizeof (CPU_VOLATILE_REGISTERS
));
1710 if (MaxLogicalProcessorNumber
> 1) {
1712 // Wakeup APs to do some AP initialize sync
1714 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1716 // Wait for all APs finished initialization
1718 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1721 CpuMpData
->InitFlag
= ApInitDone
;
1722 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1723 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1729 // Initialize global data for MP support
1731 InitMpGlobalData (CpuMpData
);
1737 Gets detailed MP-related information on the requested processor at the
1738 instant this call is made. This service may only be called from the BSP.
1740 @param[in] ProcessorNumber The handle number of processor.
1741 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1742 the requested processor is deposited.
1743 @param[out] HealthData Return processor health data.
1745 @retval EFI_SUCCESS Processor information was returned.
1746 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1747 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1748 @retval EFI_NOT_FOUND The processor with the handle specified by
1749 ProcessorNumber does not exist in the platform.
1750 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1755 MpInitLibGetProcessorInfo (
1756 IN UINTN ProcessorNumber
,
1757 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1758 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1761 CPU_MP_DATA
*CpuMpData
;
1763 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1765 CpuMpData
= GetCpuMpData ();
1766 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1769 // Check whether caller processor is BSP
1771 MpInitLibWhoAmI (&CallerNumber
);
1772 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1773 return EFI_DEVICE_ERROR
;
1776 if (ProcessorInfoBuffer
== NULL
) {
1777 return EFI_INVALID_PARAMETER
;
1780 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1781 return EFI_NOT_FOUND
;
1784 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1785 ProcessorInfoBuffer
->StatusFlag
= 0;
1786 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1787 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1789 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1790 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1792 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1793 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1795 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1799 // Get processor location information
1801 GetProcessorLocationByApicId (
1802 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1803 &ProcessorInfoBuffer
->Location
.Package
,
1804 &ProcessorInfoBuffer
->Location
.Core
,
1805 &ProcessorInfoBuffer
->Location
.Thread
1808 if (HealthData
!= NULL
) {
1809 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1816 Worker function to switch the requested AP to be the BSP from that point onward.
1818 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1819 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1820 enabled AP. Otherwise, it will be disabled.
1822 @retval EFI_SUCCESS BSP successfully switched.
1823 @retval others Failed to switch BSP.
1828 IN UINTN ProcessorNumber
,
1829 IN BOOLEAN EnableOldBSP
1832 CPU_MP_DATA
*CpuMpData
;
1835 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1836 BOOLEAN OldInterruptState
;
1837 BOOLEAN OldTimerInterruptState
;
1840 // Save and Disable Local APIC timer interrupt
1842 OldTimerInterruptState
= GetApicTimerInterruptState ();
1843 DisableApicTimerInterrupt ();
1845 // Before send both BSP and AP to a procedure to exchange their roles,
1846 // interrupt must be disabled. This is because during the exchange role
1847 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1848 // be corrupted, since interrupt return address will be pushed to stack
1851 OldInterruptState
= SaveAndDisableInterrupts ();
1854 // Mask LINT0 & LINT1 for the old BSP
1856 DisableLvtInterrupts ();
1858 CpuMpData
= GetCpuMpData ();
1861 // Check whether caller processor is BSP
1863 MpInitLibWhoAmI (&CallerNumber
);
1864 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1865 return EFI_DEVICE_ERROR
;
1868 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1869 return EFI_NOT_FOUND
;
1873 // Check whether specified AP is disabled
1875 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1876 if (State
== CpuStateDisabled
) {
1877 return EFI_INVALID_PARAMETER
;
1881 // Check whether ProcessorNumber specifies the current BSP
1883 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1884 return EFI_INVALID_PARAMETER
;
1888 // Check whether specified AP is busy
1890 if (State
== CpuStateBusy
) {
1891 return EFI_NOT_READY
;
1894 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1895 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1896 CpuMpData
->SwitchBspFlag
= TRUE
;
1897 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1900 // Clear the BSP bit of MSR_IA32_APIC_BASE
1902 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1903 ApicBaseMsr
.Bits
.BSP
= 0;
1904 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1907 // Need to wakeUp AP (future BSP).
1909 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1911 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1914 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1916 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1917 ApicBaseMsr
.Bits
.BSP
= 1;
1918 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1919 ProgramVirtualWireMode ();
1922 // Wait for old BSP finished AP task
1924 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateIdle
) {
1928 CpuMpData
->SwitchBspFlag
= FALSE
;
1930 // Set old BSP enable state
1932 if (!EnableOldBSP
) {
1933 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1935 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1938 // Save new BSP number
1940 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1943 // Restore interrupt state.
1945 SetInterruptState (OldInterruptState
);
1947 if (OldTimerInterruptState
) {
1948 EnableApicTimerInterrupt ();
1955 Worker function to let the caller enable or disable an AP from this point onward.
1956 This service may only be called from the BSP.
1958 @param[in] ProcessorNumber The handle number of AP.
1959 @param[in] EnableAP Specifies the new state for the processor for
1960 enabled, FALSE for disabled.
1961 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1962 the new health status of the AP.
1964 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1965 @retval others Failed to Enable/Disable AP.
1969 EnableDisableApWorker (
1970 IN UINTN ProcessorNumber
,
1971 IN BOOLEAN EnableAP
,
1972 IN UINT32
*HealthFlag OPTIONAL
1975 CPU_MP_DATA
*CpuMpData
;
1978 CpuMpData
= GetCpuMpData ();
1981 // Check whether caller processor is BSP
1983 MpInitLibWhoAmI (&CallerNumber
);
1984 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1985 return EFI_DEVICE_ERROR
;
1988 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1989 return EFI_INVALID_PARAMETER
;
1992 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1993 return EFI_NOT_FOUND
;
1997 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1999 ResetProcessorToIdleState (ProcessorNumber
);
2002 if (HealthFlag
!= NULL
) {
2003 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2004 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2011 This return the handle number for the calling processor. This service may be
2012 called from the BSP and APs.
2014 @param[out] ProcessorNumber Pointer to the handle number of AP.
2015 The range is from 0 to the total number of
2016 logical processors minus 1. The total number of
2017 logical processors can be retrieved by
2018 MpInitLibGetNumberOfProcessors().
2020 @retval EFI_SUCCESS The current processor handle number was returned
2022 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2023 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2029 OUT UINTN
*ProcessorNumber
2032 CPU_MP_DATA
*CpuMpData
;
2034 if (ProcessorNumber
== NULL
) {
2035 return EFI_INVALID_PARAMETER
;
2038 CpuMpData
= GetCpuMpData ();
2040 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2044 Retrieves the number of logical processor in the platform and the number of
2045 those logical processors that are enabled on this boot. This service may only
2046 be called from the BSP.
2048 @param[out] NumberOfProcessors Pointer to the total number of logical
2049 processors in the system, including the BSP
2051 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2052 processors that exist in system, including
2055 @retval EFI_SUCCESS The number of logical processors and enabled
2056 logical processors was retrieved.
2057 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2058 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2060 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2065 MpInitLibGetNumberOfProcessors (
2066 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2067 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2070 CPU_MP_DATA
*CpuMpData
;
2072 UINTN ProcessorNumber
;
2073 UINTN EnabledProcessorNumber
;
2076 CpuMpData
= GetCpuMpData ();
2078 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2079 return EFI_INVALID_PARAMETER
;
2083 // Check whether caller processor is BSP
2085 MpInitLibWhoAmI (&CallerNumber
);
2086 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2087 return EFI_DEVICE_ERROR
;
2090 ProcessorNumber
= CpuMpData
->CpuCount
;
2091 EnabledProcessorNumber
= 0;
2092 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2093 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2094 EnabledProcessorNumber
++;
2098 if (NumberOfProcessors
!= NULL
) {
2099 *NumberOfProcessors
= ProcessorNumber
;
2101 if (NumberOfEnabledProcessors
!= NULL
) {
2102 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2110 Worker function to execute a caller provided function on all enabled APs.
2112 @param[in] Procedure A pointer to the function to be run on
2113 enabled APs of the system.
2114 @param[in] SingleThread If TRUE, then all the enabled APs execute
2115 the function specified by Procedure one by
2116 one, in ascending order of processor handle
2117 number. If FALSE, then all the enabled APs
2118 execute the function specified by Procedure
2120 @param[in] WaitEvent The event created by the caller with CreateEvent()
2122 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2123 APs to return from Procedure, either for
2124 blocking or non-blocking mode.
2125 @param[in] ProcedureArgument The parameter passed into Procedure for
2127 @param[out] FailedCpuList If all APs finish successfully, then its
2128 content is set to NULL. If not all APs
2129 finish before timeout expires, then its
2130 content is set to address of the buffer
2131 holding handle numbers of the failed APs.
2133 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2134 the timeout expired.
2135 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2137 @retval others Failed to Startup all APs.
2141 StartupAllAPsWorker (
2142 IN EFI_AP_PROCEDURE Procedure
,
2143 IN BOOLEAN SingleThread
,
2144 IN EFI_EVENT WaitEvent OPTIONAL
,
2145 IN UINTN TimeoutInMicroseconds
,
2146 IN VOID
*ProcedureArgument OPTIONAL
,
2147 OUT UINTN
**FailedCpuList OPTIONAL
2151 CPU_MP_DATA
*CpuMpData
;
2152 UINTN ProcessorCount
;
2153 UINTN ProcessorNumber
;
2155 CPU_AP_DATA
*CpuData
;
2156 BOOLEAN HasEnabledAp
;
2159 CpuMpData
= GetCpuMpData ();
2161 if (FailedCpuList
!= NULL
) {
2162 *FailedCpuList
= NULL
;
2165 if (CpuMpData
->CpuCount
== 1) {
2166 return EFI_NOT_STARTED
;
2169 if (Procedure
== NULL
) {
2170 return EFI_INVALID_PARAMETER
;
2174 // Check whether caller processor is BSP
2176 MpInitLibWhoAmI (&CallerNumber
);
2177 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2178 return EFI_DEVICE_ERROR
;
2184 CheckAndUpdateApsStatus ();
2186 ProcessorCount
= CpuMpData
->CpuCount
;
2187 HasEnabledAp
= FALSE
;
2189 // Check whether all enabled APs are idle.
2190 // If any enabled AP is not idle, return EFI_NOT_READY.
2192 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2193 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2194 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2195 ApState
= GetApState (CpuData
);
2196 if (ApState
!= CpuStateDisabled
) {
2197 HasEnabledAp
= TRUE
;
2198 if (ApState
!= CpuStateIdle
) {
2200 // If any enabled APs are busy, return EFI_NOT_READY.
2202 return EFI_NOT_READY
;
2208 if (!HasEnabledAp
) {
2210 // If no enabled AP exists, return EFI_NOT_STARTED.
2212 return EFI_NOT_STARTED
;
2215 CpuMpData
->RunningCount
= 0;
2216 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2217 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2218 CpuData
->Waiting
= FALSE
;
2219 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2220 if (CpuData
->State
== CpuStateIdle
) {
2222 // Mark this processor as responsible for current calling.
2224 CpuData
->Waiting
= TRUE
;
2225 CpuMpData
->RunningCount
++;
2230 CpuMpData
->Procedure
= Procedure
;
2231 CpuMpData
->ProcArguments
= ProcedureArgument
;
2232 CpuMpData
->SingleThread
= SingleThread
;
2233 CpuMpData
->FinishedCount
= 0;
2234 CpuMpData
->FailedCpuList
= FailedCpuList
;
2235 CpuMpData
->ExpectedTime
= CalculateTimeout (
2236 TimeoutInMicroseconds
,
2237 &CpuMpData
->CurrentTime
2239 CpuMpData
->TotalTime
= 0;
2240 CpuMpData
->WaitEvent
= WaitEvent
;
2242 if (!SingleThread
) {
2243 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
2245 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2246 if (ProcessorNumber
== CallerNumber
) {
2249 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2250 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2256 Status
= EFI_SUCCESS
;
2257 if (WaitEvent
== NULL
) {
2259 Status
= CheckAllAPs ();
2260 } while (Status
== EFI_NOT_READY
);
2267 Worker function to let the caller get one enabled AP to execute a caller-provided
2270 @param[in] Procedure A pointer to the function to be run on
2271 enabled APs of the system.
2272 @param[in] ProcessorNumber The handle number of the AP.
2273 @param[in] WaitEvent The event created by the caller with CreateEvent()
2275 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2276 APs to return from Procedure, either for
2277 blocking or non-blocking mode.
2278 @param[in] ProcedureArgument The parameter passed into Procedure for
2280 @param[out] Finished If AP returns from Procedure before the
2281 timeout expires, its content is set to TRUE.
2282 Otherwise, the value is set to FALSE.
2284 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2285 the timeout expires.
2286 @retval others Failed to Startup AP.
2290 StartupThisAPWorker (
2291 IN EFI_AP_PROCEDURE Procedure
,
2292 IN UINTN ProcessorNumber
,
2293 IN EFI_EVENT WaitEvent OPTIONAL
,
2294 IN UINTN TimeoutInMicroseconds
,
2295 IN VOID
*ProcedureArgument OPTIONAL
,
2296 OUT BOOLEAN
*Finished OPTIONAL
2300 CPU_MP_DATA
*CpuMpData
;
2301 CPU_AP_DATA
*CpuData
;
2304 CpuMpData
= GetCpuMpData ();
2306 if (Finished
!= NULL
) {
2311 // Check whether caller processor is BSP
2313 MpInitLibWhoAmI (&CallerNumber
);
2314 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2315 return EFI_DEVICE_ERROR
;
2319 // Check whether processor with the handle specified by ProcessorNumber exists
2321 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2322 return EFI_NOT_FOUND
;
2326 // Check whether specified processor is BSP
2328 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2329 return EFI_INVALID_PARAMETER
;
2333 // Check parameter Procedure
2335 if (Procedure
== NULL
) {
2336 return EFI_INVALID_PARAMETER
;
2342 CheckAndUpdateApsStatus ();
2345 // Check whether specified AP is disabled
2347 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2348 return EFI_INVALID_PARAMETER
;
2352 // If WaitEvent is not NULL, execute in non-blocking mode.
2353 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2354 // CheckAPsStatus() will check completion and timeout periodically.
2356 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2357 CpuData
->WaitEvent
= WaitEvent
;
2358 CpuData
->Finished
= Finished
;
2359 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2360 CpuData
->TotalTime
= 0;
2362 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2365 // If WaitEvent is NULL, execute in blocking mode.
2366 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2368 Status
= EFI_SUCCESS
;
2369 if (WaitEvent
== NULL
) {
2371 Status
= CheckThisAP (ProcessorNumber
);
2372 } while (Status
== EFI_NOT_READY
);
2379 Get pointer to CPU MP Data structure from GUIDed HOB.
2381 @return The pointer to CPU MP Data structure.
2384 GetCpuMpDataFromGuidedHob (
2388 EFI_HOB_GUID_TYPE
*GuidHob
;
2390 CPU_MP_DATA
*CpuMpData
;
2393 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2394 if (GuidHob
!= NULL
) {
2395 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2396 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);