2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2019, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
11 EFI_GUID mCpuInitMpLibHobGuid
= CPU_INIT_MP_LIB_HOB_GUID
;
14 The function will check if BSP Execute Disable is enabled.
16 DxeIpl may have enabled Execute Disable for BSP, APs need to
17 get the status and sync up the settings.
18 If BSP's CR0.Paging is not set, BSP execute Disble feature is
21 @retval TRUE BSP Execute Disable is enabled.
22 @retval FALSE BSP Execute Disable is not enabled.
25 IsBspExecuteDisableEnabled (
30 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
31 MSR_IA32_EFER_REGISTER EferMsr
;
36 Cr0
.UintN
= AsmReadCr0 ();
37 if (Cr0
.Bits
.PG
!= 0) {
39 // If CR0 Paging bit is set
41 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
42 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
43 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
46 // Bit 20: Execute Disable Bit available.
48 if (Edx
.Bits
.NX
!= 0) {
49 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
52 // Bit 11: Execute Disable Bit enable.
54 if (EferMsr
.Bits
.NXE
!= 0) {
65 Worker function for SwitchBSP().
67 Worker function for SwitchBSP(), assigned to the AP which is intended
70 @param[in] Buffer Pointer to CPU MP Data
78 CPU_MP_DATA
*DataInHob
;
80 DataInHob
= (CPU_MP_DATA
*) Buffer
;
81 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
85 Get the Application Processors state.
87 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
93 IN CPU_AP_DATA
*CpuData
96 return CpuData
->State
;
100 Set the Application Processors state.
102 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
103 @param[in] State The AP status
107 IN CPU_AP_DATA
*CpuData
,
111 AcquireSpinLock (&CpuData
->ApLock
);
112 CpuData
->State
= State
;
113 ReleaseSpinLock (&CpuData
->ApLock
);
117 Save BSP's local APIC timer setting.
119 @param[in] CpuMpData Pointer to CPU MP Data
122 SaveLocalApicTimerSetting (
123 IN CPU_MP_DATA
*CpuMpData
127 // Record the current local APIC timer setting of BSP
130 &CpuMpData
->DivideValue
,
131 &CpuMpData
->PeriodicMode
,
134 CpuMpData
->CurrentTimerCount
= GetApicTimerCurrentCount ();
135 CpuMpData
->TimerInterruptState
= GetApicTimerInterruptState ();
139 Sync local APIC timer setting from BSP to AP.
141 @param[in] CpuMpData Pointer to CPU MP Data
144 SyncLocalApicTimerSetting (
145 IN CPU_MP_DATA
*CpuMpData
149 // Sync local APIC timer setting from BSP to AP
151 InitializeApicTimer (
152 CpuMpData
->DivideValue
,
153 CpuMpData
->CurrentTimerCount
,
154 CpuMpData
->PeriodicMode
,
158 // Disable AP's local APIC timer interrupt
160 DisableApicTimerInterrupt ();
164 Save the volatile registers required to be restored following INIT IPI.
166 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
169 SaveVolatileRegisters (
170 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
173 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
175 VolatileRegisters
->Cr0
= AsmReadCr0 ();
176 VolatileRegisters
->Cr3
= AsmReadCr3 ();
177 VolatileRegisters
->Cr4
= AsmReadCr4 ();
179 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
180 if (VersionInfoEdx
.Bits
.DE
!= 0) {
182 // If processor supports Debugging Extensions feature
183 // by CPUID.[EAX=01H]:EDX.BIT2
185 VolatileRegisters
->Dr0
= AsmReadDr0 ();
186 VolatileRegisters
->Dr1
= AsmReadDr1 ();
187 VolatileRegisters
->Dr2
= AsmReadDr2 ();
188 VolatileRegisters
->Dr3
= AsmReadDr3 ();
189 VolatileRegisters
->Dr6
= AsmReadDr6 ();
190 VolatileRegisters
->Dr7
= AsmReadDr7 ();
193 AsmReadGdtr (&VolatileRegisters
->Gdtr
);
194 AsmReadIdtr (&VolatileRegisters
->Idtr
);
195 VolatileRegisters
->Tr
= AsmReadTr ();
199 Restore the volatile registers following INIT IPI.
201 @param[in] VolatileRegisters Pointer to volatile resisters
202 @param[in] IsRestoreDr TRUE: Restore DRx if supported
203 FALSE: Do not restore DRx
206 RestoreVolatileRegisters (
207 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
208 IN BOOLEAN IsRestoreDr
211 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
212 IA32_TSS_DESCRIPTOR
*Tss
;
214 AsmWriteCr3 (VolatileRegisters
->Cr3
);
215 AsmWriteCr4 (VolatileRegisters
->Cr4
);
216 AsmWriteCr0 (VolatileRegisters
->Cr0
);
219 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
220 if (VersionInfoEdx
.Bits
.DE
!= 0) {
222 // If processor supports Debugging Extensions feature
223 // by CPUID.[EAX=01H]:EDX.BIT2
225 AsmWriteDr0 (VolatileRegisters
->Dr0
);
226 AsmWriteDr1 (VolatileRegisters
->Dr1
);
227 AsmWriteDr2 (VolatileRegisters
->Dr2
);
228 AsmWriteDr3 (VolatileRegisters
->Dr3
);
229 AsmWriteDr6 (VolatileRegisters
->Dr6
);
230 AsmWriteDr7 (VolatileRegisters
->Dr7
);
234 AsmWriteGdtr (&VolatileRegisters
->Gdtr
);
235 AsmWriteIdtr (&VolatileRegisters
->Idtr
);
236 if (VolatileRegisters
->Tr
!= 0 &&
237 VolatileRegisters
->Tr
< VolatileRegisters
->Gdtr
.Limit
) {
238 Tss
= (IA32_TSS_DESCRIPTOR
*)(VolatileRegisters
->Gdtr
.Base
+
239 VolatileRegisters
->Tr
);
240 if (Tss
->Bits
.P
== 1) {
241 Tss
->Bits
.Type
&= 0xD; // 1101 - Clear busy bit just in case
242 AsmWriteTr (VolatileRegisters
->Tr
);
248 Detect whether Mwait-monitor feature is supported.
250 @retval TRUE Mwait-monitor feature is supported.
251 @retval FALSE Mwait-monitor feature is not supported.
258 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
260 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
261 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
267 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
269 @return The AP loop mode.
273 OUT UINT32
*MonitorFilterSize
277 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
279 ASSERT (MonitorFilterSize
!= NULL
);
281 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
282 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
283 if (ApLoopMode
== ApInMwaitLoop
) {
284 if (!IsMwaitSupport ()) {
286 // If processor does not support MONITOR/MWAIT feature,
287 // force AP in Hlt-loop mode
289 ApLoopMode
= ApInHltLoop
;
293 if (ApLoopMode
!= ApInMwaitLoop
) {
294 *MonitorFilterSize
= sizeof (UINT32
);
297 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
298 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
300 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
301 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
308 Sort the APIC ID of all processors.
310 This function sorts the APIC ID of all processors so that processor number is
311 assigned in the ascending order of APIC ID which eases MP debugging.
313 @param[in] CpuMpData Pointer to PEI CPU MP Data
317 IN CPU_MP_DATA
*CpuMpData
324 CPU_INFO_IN_HOB CpuInfo
;
326 CPU_INFO_IN_HOB
*CpuInfoInHob
;
327 volatile UINT32
*StartupApSignal
;
329 ApCount
= CpuMpData
->CpuCount
- 1;
330 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
332 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
335 // Sort key is the hardware default APIC ID
337 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
338 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
339 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
341 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
344 if (Index3
!= Index1
) {
345 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
347 &CpuInfoInHob
[Index3
],
348 &CpuInfoInHob
[Index1
],
349 sizeof (CPU_INFO_IN_HOB
)
351 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
354 // Also exchange the StartupApSignal.
356 StartupApSignal
= CpuMpData
->CpuData
[Index3
].StartupApSignal
;
357 CpuMpData
->CpuData
[Index3
].StartupApSignal
=
358 CpuMpData
->CpuData
[Index1
].StartupApSignal
;
359 CpuMpData
->CpuData
[Index1
].StartupApSignal
= StartupApSignal
;
364 // Get the processor number for the BSP
366 ApicId
= GetInitialApicId ();
367 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
368 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
369 CpuMpData
->BspNumber
= (UINT32
) Index1
;
377 Enable x2APIC mode on APs.
379 @param[in, out] Buffer Pointer to private data buffer.
387 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
393 @param[in, out] Buffer Pointer to private data buffer.
401 CPU_MP_DATA
*CpuMpData
;
403 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
405 // Load microcode on AP
407 MicrocodeDetect (CpuMpData
, FALSE
);
409 // Sync BSP's MTRR table to AP
411 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
415 Find the current Processor number by APIC ID.
417 @param[in] CpuMpData Pointer to PEI CPU MP Data
418 @param[out] ProcessorNumber Return the pocessor number found
420 @retval EFI_SUCCESS ProcessorNumber is found and returned.
421 @retval EFI_NOT_FOUND ProcessorNumber is not found.
425 IN CPU_MP_DATA
*CpuMpData
,
426 OUT UINTN
*ProcessorNumber
429 UINTN TotalProcessorNumber
;
431 CPU_INFO_IN_HOB
*CpuInfoInHob
;
432 UINT32 CurrentApicId
;
434 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
436 TotalProcessorNumber
= CpuMpData
->CpuCount
;
437 CurrentApicId
= GetApicId ();
438 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
439 if (CpuInfoInHob
[Index
].ApicId
== CurrentApicId
) {
440 *ProcessorNumber
= Index
;
445 return EFI_NOT_FOUND
;
449 This function will get CPU count in the system.
451 @param[in] CpuMpData Pointer to PEI CPU MP Data
453 @return CPU count detected
456 CollectProcessorCount (
457 IN CPU_MP_DATA
*CpuMpData
463 // Send 1st broadcast IPI to APs to wakeup APs
465 CpuMpData
->InitFlag
= ApInitConfig
;
466 CpuMpData
->X2ApicEnable
= FALSE
;
467 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
, TRUE
);
468 CpuMpData
->InitFlag
= ApInitDone
;
469 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
471 // Wait for all APs finished the initialization
473 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
477 if (CpuMpData
->CpuCount
> 255) {
479 // If there are more than 255 processor found, force to enable X2APIC
481 CpuMpData
->X2ApicEnable
= TRUE
;
483 if (CpuMpData
->X2ApicEnable
) {
484 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
486 // Wakeup all APs to enable x2APIC mode
488 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
, TRUE
);
490 // Wait for all known APs finished
492 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
496 // Enable x2APIC on BSP
498 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
500 // Set BSP/Aps state to IDLE
502 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
503 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
506 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
508 // Sort BSP/Aps by CPU APIC ID in ascending order
510 SortApicId (CpuMpData
);
512 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
514 return CpuMpData
->CpuCount
;
518 Initialize CPU AP Data when AP is wakeup at the first time.
520 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
521 @param[in] ProcessorNumber The handle number of processor
522 @param[in] BistData Processor BIST data
523 @param[in] ApTopOfStack Top of AP stack
528 IN OUT CPU_MP_DATA
*CpuMpData
,
529 IN UINTN ProcessorNumber
,
531 IN UINT64 ApTopOfStack
534 CPU_INFO_IN_HOB
*CpuInfoInHob
;
536 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
537 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
538 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
539 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
540 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
542 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
543 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
544 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
546 // Set x2APIC mode if there are any logical processor reporting
547 // an Initial APIC ID of 255 or greater.
549 AcquireSpinLock(&CpuMpData
->MpLock
);
550 CpuMpData
->X2ApicEnable
= TRUE
;
551 ReleaseSpinLock(&CpuMpData
->MpLock
);
554 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
555 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
559 This function will be called from AP reset code if BSP uses WakeUpAP.
561 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
562 @param[in] ApIndex Number of current executing AP
567 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
571 CPU_MP_DATA
*CpuMpData
;
572 UINTN ProcessorNumber
;
573 EFI_AP_PROCEDURE Procedure
;
576 volatile UINT32
*ApStartupSignalBuffer
;
577 CPU_INFO_IN_HOB
*CpuInfoInHob
;
579 UINTN CurrentApicMode
;
582 // AP finished assembly code and begin to execute C code
584 CpuMpData
= ExchangeInfo
->CpuMpData
;
587 // AP's local APIC settings will be lost after received INIT IPI
588 // We need to re-initialize them at here
590 ProgramVirtualWireMode ();
592 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
594 DisableLvtInterrupts ();
595 SyncLocalApicTimerSetting (CpuMpData
);
597 CurrentApicMode
= GetApicMode ();
599 if (CpuMpData
->InitFlag
== ApInitConfig
) {
603 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
604 ProcessorNumber
= ApIndex
;
606 // This is first time AP wakeup, get BIST information from AP stack
608 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
609 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
611 // Do some AP initialize sync
613 ApInitializeSync (CpuMpData
);
615 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
616 // to initialize AP in InitConfig path.
617 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
619 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
620 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
621 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
623 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
626 // Execute AP function if AP is ready
628 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
630 // Clear AP start-up signal when AP waken up
632 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
633 InterlockedCompareExchange32 (
634 (UINT32
*) ApStartupSignalBuffer
,
638 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
640 // Restore AP's volatile registers saved
642 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
645 // The CPU driver might not flush TLB for APs on spot after updating
646 // page attributes. AP in mwait loop mode needs to take care of it when
652 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
653 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
654 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
655 if (Procedure
!= NULL
) {
656 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
658 // Enable source debugging on AP function
662 // Invoke AP function here
664 Procedure (Parameter
);
665 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
666 if (CpuMpData
->SwitchBspFlag
) {
668 // Re-get the processor number due to BSP/AP maybe exchange in AP function
670 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
671 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
672 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
673 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
674 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
676 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
677 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
678 if (CurrentApicMode
!= GetApicMode ()) {
680 // If APIC mode change happened during AP function execution,
681 // we do not support APIC ID value changed.
687 // Re-get the CPU APICID and Initial APICID if they are changed
689 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
690 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
695 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
700 // AP finished executing C code
702 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
705 // Place AP is specified loop mode
707 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
709 // Save AP volatile registers
711 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
713 // Place AP in HLT-loop
716 DisableInterrupts ();
722 DisableInterrupts ();
723 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
725 // Place AP in MWAIT-loop
727 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
728 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
730 // Check AP start-up signal again.
731 // If AP start-up signal is not set, place AP into
732 // the specified C-state
734 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
736 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
738 // Place AP in Run-loop
746 // If AP start-up signal is written, AP is waken up
747 // otherwise place AP in loop again
749 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
757 Wait for AP wakeup and write AP start-up signal till AP is waken up.
759 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
763 IN
volatile UINT32
*ApStartupSignalBuffer
767 // If AP is waken up, StartupApSignal should be cleared.
768 // Otherwise, write StartupApSignal again till AP waken up.
770 while (InterlockedCompareExchange32 (
771 (UINT32
*) ApStartupSignalBuffer
,
780 This function will fill the exchange info structure.
782 @param[in] CpuMpData Pointer to CPU MP Data
786 FillExchangeInfoData (
787 IN CPU_MP_DATA
*CpuMpData
790 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
792 IA32_SEGMENT_DESCRIPTOR
*Selector
;
794 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
795 ExchangeInfo
->Lock
= 0;
796 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
797 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
798 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
799 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
801 ExchangeInfo
->CodeSegment
= AsmReadCs ();
802 ExchangeInfo
->DataSegment
= AsmReadDs ();
804 ExchangeInfo
->Cr3
= AsmReadCr3 ();
806 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
807 ExchangeInfo
->ApIndex
= 0;
808 ExchangeInfo
->NumApsExecuting
= 0;
809 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
810 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
811 ExchangeInfo
->CpuMpData
= CpuMpData
;
813 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
815 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
818 // Get the BSP's data of GDT and IDT
820 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
821 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
824 // Find a 32-bit code segment
826 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
827 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
829 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
830 ExchangeInfo
->ModeTransitionSegment
=
831 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
835 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
839 // Copy all 32-bit code and 64-bit code into memory with type of
840 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
842 if (CpuMpData
->WakeupBufferHigh
!= 0) {
843 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
844 CpuMpData
->AddressMap
.ModeTransitionOffset
;
846 (VOID
*)CpuMpData
->WakeupBufferHigh
,
847 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
848 CpuMpData
->AddressMap
.ModeTransitionOffset
,
852 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
854 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
855 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
858 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
859 (UINT32
)ExchangeInfo
->ModeOffset
-
860 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
861 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
865 Helper function that waits until the finished AP count reaches the specified
866 limit, or the specified timeout elapses (whichever comes first).
868 @param[in] CpuMpData Pointer to CPU MP Data.
869 @param[in] FinishedApLimit The number of finished APs to wait for.
870 @param[in] TimeLimit The number of microseconds to wait for.
873 TimedWaitForApFinish (
874 IN CPU_MP_DATA
*CpuMpData
,
875 IN UINT32 FinishedApLimit
,
880 Get available system memory below 1MB by specified size.
882 @param[in] CpuMpData The pointer to CPU MP Data structure.
885 BackupAndPrepareWakeupBuffer(
886 IN CPU_MP_DATA
*CpuMpData
890 (VOID
*) CpuMpData
->BackupBuffer
,
891 (VOID
*) CpuMpData
->WakeupBuffer
,
892 CpuMpData
->BackupBufferSize
895 (VOID
*) CpuMpData
->WakeupBuffer
,
896 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
897 CpuMpData
->AddressMap
.RendezvousFunnelSize
902 Restore wakeup buffer data.
904 @param[in] CpuMpData The pointer to CPU MP Data structure.
908 IN CPU_MP_DATA
*CpuMpData
912 (VOID
*) CpuMpData
->WakeupBuffer
,
913 (VOID
*) CpuMpData
->BackupBuffer
,
914 CpuMpData
->BackupBufferSize
919 Allocate reset vector buffer.
921 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
924 AllocateResetVector (
925 IN OUT CPU_MP_DATA
*CpuMpData
928 UINTN ApResetVectorSize
;
930 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
931 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
932 sizeof (MP_CPU_EXCHANGE_INFO
);
934 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
935 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
936 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
937 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
938 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
939 CpuMpData
->AddressMap
.ModeTransitionOffset
942 BackupAndPrepareWakeupBuffer (CpuMpData
);
946 Free AP reset vector buffer.
948 @param[in] CpuMpData The pointer to CPU MP Data structure.
952 IN CPU_MP_DATA
*CpuMpData
955 RestoreWakeupBuffer (CpuMpData
);
959 This function will be called by BSP to wakeup AP.
961 @param[in] CpuMpData Pointer to CPU MP Data
962 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
963 FALSE: Send IPI to AP by ApicId
964 @param[in] ProcessorNumber The handle number of specified processor
965 @param[in] Procedure The function to be invoked by AP
966 @param[in] ProcedureArgument The argument to be passed into AP function
967 @param[in] WakeUpDisabledAps Whether need to wake up disabled APs in broadcast mode.
971 IN CPU_MP_DATA
*CpuMpData
,
972 IN BOOLEAN Broadcast
,
973 IN UINTN ProcessorNumber
,
974 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
975 IN VOID
*ProcedureArgument
, OPTIONAL
976 IN BOOLEAN WakeUpDisabledAps
979 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
981 CPU_AP_DATA
*CpuData
;
982 BOOLEAN ResetVectorRequired
;
983 CPU_INFO_IN_HOB
*CpuInfoInHob
;
985 CpuMpData
->FinishedCount
= 0;
986 ResetVectorRequired
= FALSE
;
988 if (CpuMpData
->WakeUpByInitSipiSipi
||
989 CpuMpData
->InitFlag
!= ApInitDone
) {
990 ResetVectorRequired
= TRUE
;
991 AllocateResetVector (CpuMpData
);
992 FillExchangeInfoData (CpuMpData
);
993 SaveLocalApicTimerSetting (CpuMpData
);
996 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
998 // Get AP target C-state each time when waking up AP,
999 // for it maybe updated by platform again
1001 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1004 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1007 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1008 if (Index
!= CpuMpData
->BspNumber
) {
1009 CpuData
= &CpuMpData
->CpuData
[Index
];
1011 // All AP(include disabled AP) will be woke up by INIT-SIPI-SIPI, but
1012 // the AP procedure will be skipped for disabled AP because AP state
1013 // is not CpuStateReady.
1015 if (GetApState (CpuData
) == CpuStateDisabled
&& !WakeUpDisabledAps
) {
1019 CpuData
->ApFunction
= (UINTN
) Procedure
;
1020 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1021 SetApState (CpuData
, CpuStateReady
);
1022 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1023 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1027 if (ResetVectorRequired
) {
1031 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1033 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1035 // Here support two methods to collect AP count through adjust
1036 // PcdCpuApInitTimeOutInMicroSeconds values.
1038 // one way is set a value to just let the first AP to start the
1039 // initialization, then through the later while loop to wait all Aps
1040 // finsh the initialization.
1041 // The other way is set a value to let all APs finished the initialzation.
1042 // In this case, the later while loop is useless.
1044 TimedWaitForApFinish (
1046 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1047 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1050 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1055 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1057 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1058 CpuData
= &CpuMpData
->CpuData
[Index
];
1059 if (Index
!= CpuMpData
->BspNumber
) {
1060 WaitApWakeup (CpuData
->StartupApSignal
);
1065 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1066 CpuData
->ApFunction
= (UINTN
) Procedure
;
1067 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1068 SetApState (CpuData
, CpuStateReady
);
1070 // Wakeup specified AP
1072 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1073 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1074 if (ResetVectorRequired
) {
1075 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1077 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1078 (UINT32
) ExchangeInfo
->BufferStart
1082 // Wait specified AP waken up
1084 WaitApWakeup (CpuData
->StartupApSignal
);
1087 if (ResetVectorRequired
) {
1088 FreeResetVector (CpuMpData
);
1092 // After one round of Wakeup Ap actions, need to re-sync ApLoopMode with
1093 // WakeUpByInitSipiSipi flag. WakeUpByInitSipiSipi flag maybe changed by
1094 // S3SmmInitDone Ppi.
1096 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1100 Calculate timeout value and return the current performance counter value.
1102 Calculate the number of performance counter ticks required for a timeout.
1103 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1106 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1107 @param[out] CurrentTime Returns the current value of the performance counter.
1109 @return Expected time stamp counter for timeout.
1110 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1116 IN UINTN TimeoutInMicroseconds
,
1117 OUT UINT64
*CurrentTime
1120 UINT64 TimeoutInSeconds
;
1121 UINT64 TimestampCounterFreq
;
1124 // Read the current value of the performance counter
1126 *CurrentTime
= GetPerformanceCounter ();
1129 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1132 if (TimeoutInMicroseconds
== 0) {
1137 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1140 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1143 // Check the potential overflow before calculate the number of ticks for the timeout value.
1145 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1147 // Convert microseconds into seconds if direct multiplication overflows
1149 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1151 // Assertion if the final tick count exceeds MAX_UINT64
1153 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1154 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1157 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1158 // it by 1,000,000, to get the number of ticks for the timeout value.
1162 TimestampCounterFreq
,
1163 TimeoutInMicroseconds
1171 Checks whether timeout expires.
1173 Check whether the number of elapsed performance counter ticks required for
1174 a timeout condition has been reached.
1175 If Timeout is zero, which means infinity, return value is always FALSE.
1177 @param[in, out] PreviousTime On input, the value of the performance counter
1178 when it was last read.
1179 On output, the current value of the performance
1181 @param[in] TotalTime The total amount of elapsed time in performance
1183 @param[in] Timeout The number of performance counter ticks required
1184 to reach a timeout condition.
1186 @retval TRUE A timeout condition has been reached.
1187 @retval FALSE A timeout condition has not been reached.
1192 IN OUT UINT64
*PreviousTime
,
1193 IN UINT64
*TotalTime
,
1206 GetPerformanceCounterProperties (&Start
, &End
);
1207 Cycle
= End
- Start
;
1212 CurrentTime
= GetPerformanceCounter();
1213 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1220 *TotalTime
+= Delta
;
1221 *PreviousTime
= CurrentTime
;
1222 if (*TotalTime
> Timeout
) {
1229 Helper function that waits until the finished AP count reaches the specified
1230 limit, or the specified timeout elapses (whichever comes first).
1232 @param[in] CpuMpData Pointer to CPU MP Data.
1233 @param[in] FinishedApLimit The number of finished APs to wait for.
1234 @param[in] TimeLimit The number of microseconds to wait for.
1237 TimedWaitForApFinish (
1238 IN CPU_MP_DATA
*CpuMpData
,
1239 IN UINT32 FinishedApLimit
,
1244 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1245 // "infinity", so check for (TimeLimit == 0) explicitly.
1247 if (TimeLimit
== 0) {
1251 CpuMpData
->TotalTime
= 0;
1252 CpuMpData
->ExpectedTime
= CalculateTimeout (
1254 &CpuMpData
->CurrentTime
1256 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1258 &CpuMpData
->CurrentTime
,
1259 &CpuMpData
->TotalTime
,
1260 CpuMpData
->ExpectedTime
1265 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1268 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1271 DivU64x64Remainder (
1272 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1273 GetPerformanceCounterProperties (NULL
, NULL
),
1281 Reset an AP to Idle state.
1283 Any task being executed by the AP will be aborted and the AP
1284 will be waiting for a new task in Wait-For-SIPI state.
1286 @param[in] ProcessorNumber The handle number of processor.
1289 ResetProcessorToIdleState (
1290 IN UINTN ProcessorNumber
1293 CPU_MP_DATA
*CpuMpData
;
1295 CpuMpData
= GetCpuMpData ();
1297 CpuMpData
->InitFlag
= ApInitReconfig
;
1298 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
, TRUE
);
1299 while (CpuMpData
->FinishedCount
< 1) {
1302 CpuMpData
->InitFlag
= ApInitDone
;
1304 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1308 Searches for the next waiting AP.
1310 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1312 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1314 @retval EFI_SUCCESS The next waiting AP has been found.
1315 @retval EFI_NOT_FOUND No waiting AP exists.
1319 GetNextWaitingProcessorNumber (
1320 OUT UINTN
*NextProcessorNumber
1323 UINTN ProcessorNumber
;
1324 CPU_MP_DATA
*CpuMpData
;
1326 CpuMpData
= GetCpuMpData ();
1328 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1329 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1330 *NextProcessorNumber
= ProcessorNumber
;
1335 return EFI_NOT_FOUND
;
1338 /** Checks status of specified AP.
1340 This function checks whether the specified AP has finished the task assigned
1341 by StartupThisAP(), and whether timeout expires.
1343 @param[in] ProcessorNumber The handle number of processor.
1345 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1346 @retval EFI_TIMEOUT The timeout expires.
1347 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1351 IN UINTN ProcessorNumber
1354 CPU_MP_DATA
*CpuMpData
;
1355 CPU_AP_DATA
*CpuData
;
1357 CpuMpData
= GetCpuMpData ();
1358 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1361 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1362 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1363 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1366 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1368 if (GetApState(CpuData
) == CpuStateFinished
) {
1369 if (CpuData
->Finished
!= NULL
) {
1370 *(CpuData
->Finished
) = TRUE
;
1372 SetApState (CpuData
, CpuStateIdle
);
1376 // If timeout expires for StartupThisAP(), report timeout.
1378 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1379 if (CpuData
->Finished
!= NULL
) {
1380 *(CpuData
->Finished
) = FALSE
;
1383 // Reset failed AP to idle state
1385 ResetProcessorToIdleState (ProcessorNumber
);
1390 return EFI_NOT_READY
;
1394 Checks status of all APs.
1396 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1397 and whether timeout expires.
1399 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1400 @retval EFI_TIMEOUT The timeout expires.
1401 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1408 UINTN ProcessorNumber
;
1409 UINTN NextProcessorNumber
;
1412 CPU_MP_DATA
*CpuMpData
;
1413 CPU_AP_DATA
*CpuData
;
1415 CpuMpData
= GetCpuMpData ();
1417 NextProcessorNumber
= 0;
1420 // Go through all APs that are responsible for the StartupAllAPs().
1422 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1423 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1427 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1429 // Check the CPU state of AP. If it is CpuStateIdle, then the AP has finished its task.
1430 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1431 // value of state after setting the it to CpuStateIdle, so BSP can safely make use of its value.
1433 if (GetApState(CpuData
) == CpuStateFinished
) {
1434 CpuMpData
->RunningCount
--;
1435 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1436 SetApState(CpuData
, CpuStateIdle
);
1439 // If in Single Thread mode, then search for the next waiting AP for execution.
1441 if (CpuMpData
->SingleThread
) {
1442 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1444 if (!EFI_ERROR (Status
)) {
1448 (UINT32
) NextProcessorNumber
,
1449 CpuMpData
->Procedure
,
1450 CpuMpData
->ProcArguments
,
1459 // If all APs finish, return EFI_SUCCESS.
1461 if (CpuMpData
->RunningCount
== 0) {
1466 // If timeout expires, report timeout.
1469 &CpuMpData
->CurrentTime
,
1470 &CpuMpData
->TotalTime
,
1471 CpuMpData
->ExpectedTime
)
1474 // If FailedCpuList is not NULL, record all failed APs in it.
1476 if (CpuMpData
->FailedCpuList
!= NULL
) {
1477 *CpuMpData
->FailedCpuList
=
1478 AllocatePool ((CpuMpData
->RunningCount
+ 1) * sizeof (UINTN
));
1479 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1483 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1485 // Check whether this processor is responsible for StartupAllAPs().
1487 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1489 // Reset failed APs to idle state
1491 ResetProcessorToIdleState (ProcessorNumber
);
1492 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1493 if (CpuMpData
->FailedCpuList
!= NULL
) {
1494 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1498 if (CpuMpData
->FailedCpuList
!= NULL
) {
1499 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1503 return EFI_NOT_READY
;
1507 MP Initialize Library initialization.
1509 This service will allocate AP reset vector and wakeup all APs to do APs
1512 This service must be invoked before all other MP Initialize Library
1513 service are invoked.
1515 @retval EFI_SUCCESS MP initialization succeeds.
1516 @retval Others MP initialization fails.
1521 MpInitLibInitialize (
1525 CPU_MP_DATA
*OldCpuMpData
;
1526 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1527 UINT32 MaxLogicalProcessorNumber
;
1529 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1530 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1532 UINT32 MonitorFilterSize
;
1535 CPU_MP_DATA
*CpuMpData
;
1537 UINT8
*MonitorBuffer
;
1539 UINTN ApResetVectorSize
;
1540 UINTN BackupBufferAddr
;
1542 VOID
*MicrocodePatchInRam
;
1544 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1545 if (OldCpuMpData
== NULL
) {
1546 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1548 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1550 ASSERT (MaxLogicalProcessorNumber
!= 0);
1552 AsmGetAddressMap (&AddressMap
);
1553 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1554 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1555 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1558 // Save BSP's Control registers for APs.
1560 SaveVolatileRegisters (&VolatileRegisters
);
1562 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1563 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1564 BufferSize
+= ApResetVectorSize
;
1565 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1566 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1567 BufferSize
+= sizeof (CPU_MP_DATA
);
1568 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1569 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1570 ASSERT (MpBuffer
!= NULL
);
1571 ZeroMem (MpBuffer
, BufferSize
);
1572 Buffer
= (UINTN
) MpBuffer
;
1575 // The layout of the Buffer is as below:
1577 // +--------------------+ <-- Buffer
1579 // +--------------------+ <-- MonitorBuffer
1580 // AP Monitor Filters (N)
1581 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1583 // +--------------------+
1585 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1586 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1587 // +--------------------+ <-- CpuMpData
1589 // +--------------------+ <-- CpuMpData->CpuData
1591 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1592 // CPU_INFO_IN_HOB (N)
1593 // +--------------------+
1595 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1596 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1597 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1598 CpuMpData
= (CPU_MP_DATA
*) (ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1599 CpuMpData
->Buffer
= Buffer
;
1600 CpuMpData
->CpuApStackSize
= ApStackSize
;
1601 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1602 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1603 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1604 CpuMpData
->CpuCount
= 1;
1605 CpuMpData
->BspNumber
= 0;
1606 CpuMpData
->WaitEvent
= NULL
;
1607 CpuMpData
->SwitchBspFlag
= FALSE
;
1608 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1609 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1610 CpuMpData
->MicrocodePatchRegionSize
= PcdGet64 (PcdCpuMicrocodePatchRegionSize
);
1612 // If platform has more than one CPU, relocate microcode to memory to reduce
1613 // loading microcode time.
1615 MicrocodePatchInRam
= NULL
;
1616 if (MaxLogicalProcessorNumber
> 1) {
1617 MicrocodePatchInRam
= AllocatePages (
1619 (UINTN
)CpuMpData
->MicrocodePatchRegionSize
1623 if (MicrocodePatchInRam
== NULL
) {
1625 // there is only one processor, or no microcode patch is available, or
1626 // memory allocation failed
1628 CpuMpData
->MicrocodePatchAddress
= PcdGet64 (PcdCpuMicrocodePatchAddress
);
1631 // there are multiple processors, and a microcode patch is available, and
1632 // memory allocation succeeded
1635 MicrocodePatchInRam
,
1636 (VOID
*)(UINTN
)PcdGet64 (PcdCpuMicrocodePatchAddress
),
1637 (UINTN
)CpuMpData
->MicrocodePatchRegionSize
1639 CpuMpData
->MicrocodePatchAddress
= (UINTN
)MicrocodePatchInRam
;
1642 InitializeSpinLock(&CpuMpData
->MpLock
);
1645 // Make sure no memory usage outside of the allocated buffer.
1647 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1648 Buffer
+ BufferSize
);
1651 // Duplicate BSP's IDT to APs.
1652 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1654 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1655 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1657 // Don't pass BSP's TR to APs to avoid AP init failure.
1659 VolatileRegisters
.Tr
= 0;
1660 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1662 // Set BSP basic information
1664 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1666 // Save assembly code information
1668 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1670 // Finally set AP loop mode
1672 CpuMpData
->ApLoopMode
= ApLoopMode
;
1673 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1675 CpuMpData
->WakeUpByInitSipiSipi
= (CpuMpData
->ApLoopMode
== ApInHltLoop
);
1678 // Set up APs wakeup signal buffer
1680 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1681 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1682 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1685 // Load Microcode on BSP
1687 MicrocodeDetect (CpuMpData
, TRUE
);
1689 // Store BSP's MTRR setting
1691 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1693 // Enable the local APIC for Virtual Wire Mode.
1695 ProgramVirtualWireMode ();
1697 if (OldCpuMpData
== NULL
) {
1698 if (MaxLogicalProcessorNumber
> 1) {
1700 // Wakeup all APs and calculate the processor count in system
1702 CollectProcessorCount (CpuMpData
);
1706 // APs have been wakeup before, just get the CPU Information
1709 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1710 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1711 CpuMpData
->InitFlag
= ApInitReconfig
;
1712 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1713 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1714 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1715 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1716 if (CpuInfoInHob
[Index
].InitialApicId
>= 255 || Index
> 254) {
1717 CpuMpData
->X2ApicEnable
= TRUE
;
1719 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1720 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1721 CopyMem (&CpuMpData
->CpuData
[Index
].VolatileRegisters
, &VolatileRegisters
, sizeof (CPU_VOLATILE_REGISTERS
));
1723 if (MaxLogicalProcessorNumber
> 1) {
1725 // Wakeup APs to do some AP initialize sync
1727 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
, TRUE
);
1729 // Wait for all APs finished initialization
1731 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1734 CpuMpData
->InitFlag
= ApInitDone
;
1735 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1736 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1742 // Initialize global data for MP support
1744 InitMpGlobalData (CpuMpData
);
1750 Gets detailed MP-related information on the requested processor at the
1751 instant this call is made. This service may only be called from the BSP.
1753 @param[in] ProcessorNumber The handle number of processor.
1754 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1755 the requested processor is deposited.
1756 @param[out] HealthData Return processor health data.
1758 @retval EFI_SUCCESS Processor information was returned.
1759 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1760 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1761 @retval EFI_NOT_FOUND The processor with the handle specified by
1762 ProcessorNumber does not exist in the platform.
1763 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1768 MpInitLibGetProcessorInfo (
1769 IN UINTN ProcessorNumber
,
1770 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1771 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1774 CPU_MP_DATA
*CpuMpData
;
1776 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1778 CpuMpData
= GetCpuMpData ();
1779 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1782 // Check whether caller processor is BSP
1784 MpInitLibWhoAmI (&CallerNumber
);
1785 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1786 return EFI_DEVICE_ERROR
;
1789 if (ProcessorInfoBuffer
== NULL
) {
1790 return EFI_INVALID_PARAMETER
;
1793 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1794 return EFI_NOT_FOUND
;
1797 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1798 ProcessorInfoBuffer
->StatusFlag
= 0;
1799 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1800 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1802 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1803 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1805 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1806 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1808 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1812 // Get processor location information
1814 GetProcessorLocationByApicId (
1815 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1816 &ProcessorInfoBuffer
->Location
.Package
,
1817 &ProcessorInfoBuffer
->Location
.Core
,
1818 &ProcessorInfoBuffer
->Location
.Thread
1821 if (HealthData
!= NULL
) {
1822 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1829 Worker function to switch the requested AP to be the BSP from that point onward.
1831 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1832 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1833 enabled AP. Otherwise, it will be disabled.
1835 @retval EFI_SUCCESS BSP successfully switched.
1836 @retval others Failed to switch BSP.
1841 IN UINTN ProcessorNumber
,
1842 IN BOOLEAN EnableOldBSP
1845 CPU_MP_DATA
*CpuMpData
;
1848 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1849 BOOLEAN OldInterruptState
;
1850 BOOLEAN OldTimerInterruptState
;
1853 // Save and Disable Local APIC timer interrupt
1855 OldTimerInterruptState
= GetApicTimerInterruptState ();
1856 DisableApicTimerInterrupt ();
1858 // Before send both BSP and AP to a procedure to exchange their roles,
1859 // interrupt must be disabled. This is because during the exchange role
1860 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1861 // be corrupted, since interrupt return address will be pushed to stack
1864 OldInterruptState
= SaveAndDisableInterrupts ();
1867 // Mask LINT0 & LINT1 for the old BSP
1869 DisableLvtInterrupts ();
1871 CpuMpData
= GetCpuMpData ();
1874 // Check whether caller processor is BSP
1876 MpInitLibWhoAmI (&CallerNumber
);
1877 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1878 return EFI_DEVICE_ERROR
;
1881 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1882 return EFI_NOT_FOUND
;
1886 // Check whether specified AP is disabled
1888 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1889 if (State
== CpuStateDisabled
) {
1890 return EFI_INVALID_PARAMETER
;
1894 // Check whether ProcessorNumber specifies the current BSP
1896 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1897 return EFI_INVALID_PARAMETER
;
1901 // Check whether specified AP is busy
1903 if (State
== CpuStateBusy
) {
1904 return EFI_NOT_READY
;
1907 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1908 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1909 CpuMpData
->SwitchBspFlag
= TRUE
;
1910 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1913 // Clear the BSP bit of MSR_IA32_APIC_BASE
1915 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1916 ApicBaseMsr
.Bits
.BSP
= 0;
1917 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1920 // Need to wakeUp AP (future BSP).
1922 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
, TRUE
);
1924 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1927 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1929 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1930 ApicBaseMsr
.Bits
.BSP
= 1;
1931 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1932 ProgramVirtualWireMode ();
1935 // Wait for old BSP finished AP task
1937 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1941 CpuMpData
->SwitchBspFlag
= FALSE
;
1943 // Set old BSP enable state
1945 if (!EnableOldBSP
) {
1946 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1948 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1951 // Save new BSP number
1953 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1956 // Restore interrupt state.
1958 SetInterruptState (OldInterruptState
);
1960 if (OldTimerInterruptState
) {
1961 EnableApicTimerInterrupt ();
1968 Worker function to let the caller enable or disable an AP from this point onward.
1969 This service may only be called from the BSP.
1971 @param[in] ProcessorNumber The handle number of AP.
1972 @param[in] EnableAP Specifies the new state for the processor for
1973 enabled, FALSE for disabled.
1974 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1975 the new health status of the AP.
1977 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1978 @retval others Failed to Enable/Disable AP.
1982 EnableDisableApWorker (
1983 IN UINTN ProcessorNumber
,
1984 IN BOOLEAN EnableAP
,
1985 IN UINT32
*HealthFlag OPTIONAL
1988 CPU_MP_DATA
*CpuMpData
;
1991 CpuMpData
= GetCpuMpData ();
1994 // Check whether caller processor is BSP
1996 MpInitLibWhoAmI (&CallerNumber
);
1997 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1998 return EFI_DEVICE_ERROR
;
2001 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2002 return EFI_INVALID_PARAMETER
;
2005 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2006 return EFI_NOT_FOUND
;
2010 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
2012 ResetProcessorToIdleState (ProcessorNumber
);
2015 if (HealthFlag
!= NULL
) {
2016 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
2017 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
2024 This return the handle number for the calling processor. This service may be
2025 called from the BSP and APs.
2027 @param[out] ProcessorNumber Pointer to the handle number of AP.
2028 The range is from 0 to the total number of
2029 logical processors minus 1. The total number of
2030 logical processors can be retrieved by
2031 MpInitLibGetNumberOfProcessors().
2033 @retval EFI_SUCCESS The current processor handle number was returned
2035 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
2036 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2042 OUT UINTN
*ProcessorNumber
2045 CPU_MP_DATA
*CpuMpData
;
2047 if (ProcessorNumber
== NULL
) {
2048 return EFI_INVALID_PARAMETER
;
2051 CpuMpData
= GetCpuMpData ();
2053 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2057 Retrieves the number of logical processor in the platform and the number of
2058 those logical processors that are enabled on this boot. This service may only
2059 be called from the BSP.
2061 @param[out] NumberOfProcessors Pointer to the total number of logical
2062 processors in the system, including the BSP
2064 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2065 processors that exist in system, including
2068 @retval EFI_SUCCESS The number of logical processors and enabled
2069 logical processors was retrieved.
2070 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2071 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2073 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2078 MpInitLibGetNumberOfProcessors (
2079 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2080 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2083 CPU_MP_DATA
*CpuMpData
;
2085 UINTN ProcessorNumber
;
2086 UINTN EnabledProcessorNumber
;
2089 CpuMpData
= GetCpuMpData ();
2091 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2092 return EFI_INVALID_PARAMETER
;
2096 // Check whether caller processor is BSP
2098 MpInitLibWhoAmI (&CallerNumber
);
2099 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2100 return EFI_DEVICE_ERROR
;
2103 ProcessorNumber
= CpuMpData
->CpuCount
;
2104 EnabledProcessorNumber
= 0;
2105 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2106 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2107 EnabledProcessorNumber
++;
2111 if (NumberOfProcessors
!= NULL
) {
2112 *NumberOfProcessors
= ProcessorNumber
;
2114 if (NumberOfEnabledProcessors
!= NULL
) {
2115 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2123 Worker function to execute a caller provided function on all enabled APs.
2125 @param[in] Procedure A pointer to the function to be run on
2126 enabled APs of the system.
2127 @param[in] SingleThread If TRUE, then all the enabled APs execute
2128 the function specified by Procedure one by
2129 one, in ascending order of processor handle
2130 number. If FALSE, then all the enabled APs
2131 execute the function specified by Procedure
2133 @param[in] WaitEvent The event created by the caller with CreateEvent()
2135 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2136 APs to return from Procedure, either for
2137 blocking or non-blocking mode.
2138 @param[in] ProcedureArgument The parameter passed into Procedure for
2140 @param[out] FailedCpuList If all APs finish successfully, then its
2141 content is set to NULL. If not all APs
2142 finish before timeout expires, then its
2143 content is set to address of the buffer
2144 holding handle numbers of the failed APs.
2146 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2147 the timeout expired.
2148 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2150 @retval others Failed to Startup all APs.
2154 StartupAllAPsWorker (
2155 IN EFI_AP_PROCEDURE Procedure
,
2156 IN BOOLEAN SingleThread
,
2157 IN EFI_EVENT WaitEvent OPTIONAL
,
2158 IN UINTN TimeoutInMicroseconds
,
2159 IN VOID
*ProcedureArgument OPTIONAL
,
2160 OUT UINTN
**FailedCpuList OPTIONAL
2164 CPU_MP_DATA
*CpuMpData
;
2165 UINTN ProcessorCount
;
2166 UINTN ProcessorNumber
;
2168 CPU_AP_DATA
*CpuData
;
2169 BOOLEAN HasEnabledAp
;
2172 CpuMpData
= GetCpuMpData ();
2174 if (FailedCpuList
!= NULL
) {
2175 *FailedCpuList
= NULL
;
2178 if (CpuMpData
->CpuCount
== 1) {
2179 return EFI_NOT_STARTED
;
2182 if (Procedure
== NULL
) {
2183 return EFI_INVALID_PARAMETER
;
2187 // Check whether caller processor is BSP
2189 MpInitLibWhoAmI (&CallerNumber
);
2190 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2191 return EFI_DEVICE_ERROR
;
2197 CheckAndUpdateApsStatus ();
2199 ProcessorCount
= CpuMpData
->CpuCount
;
2200 HasEnabledAp
= FALSE
;
2202 // Check whether all enabled APs are idle.
2203 // If any enabled AP is not idle, return EFI_NOT_READY.
2205 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2206 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2207 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2208 ApState
= GetApState (CpuData
);
2209 if (ApState
!= CpuStateDisabled
) {
2210 HasEnabledAp
= TRUE
;
2211 if (ApState
!= CpuStateIdle
) {
2213 // If any enabled APs are busy, return EFI_NOT_READY.
2215 return EFI_NOT_READY
;
2221 if (!HasEnabledAp
) {
2223 // If no enabled AP exists, return EFI_NOT_STARTED.
2225 return EFI_NOT_STARTED
;
2228 CpuMpData
->RunningCount
= 0;
2229 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2230 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2231 CpuData
->Waiting
= FALSE
;
2232 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2233 if (CpuData
->State
== CpuStateIdle
) {
2235 // Mark this processor as responsible for current calling.
2237 CpuData
->Waiting
= TRUE
;
2238 CpuMpData
->RunningCount
++;
2243 CpuMpData
->Procedure
= Procedure
;
2244 CpuMpData
->ProcArguments
= ProcedureArgument
;
2245 CpuMpData
->SingleThread
= SingleThread
;
2246 CpuMpData
->FinishedCount
= 0;
2247 CpuMpData
->FailedCpuList
= FailedCpuList
;
2248 CpuMpData
->ExpectedTime
= CalculateTimeout (
2249 TimeoutInMicroseconds
,
2250 &CpuMpData
->CurrentTime
2252 CpuMpData
->TotalTime
= 0;
2253 CpuMpData
->WaitEvent
= WaitEvent
;
2255 if (!SingleThread
) {
2256 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
, FALSE
);
2258 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2259 if (ProcessorNumber
== CallerNumber
) {
2262 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2263 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2269 Status
= EFI_SUCCESS
;
2270 if (WaitEvent
== NULL
) {
2272 Status
= CheckAllAPs ();
2273 } while (Status
== EFI_NOT_READY
);
2280 Worker function to let the caller get one enabled AP to execute a caller-provided
2283 @param[in] Procedure A pointer to the function to be run on
2284 enabled APs of the system.
2285 @param[in] ProcessorNumber The handle number of the AP.
2286 @param[in] WaitEvent The event created by the caller with CreateEvent()
2288 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2289 APs to return from Procedure, either for
2290 blocking or non-blocking mode.
2291 @param[in] ProcedureArgument The parameter passed into Procedure for
2293 @param[out] Finished If AP returns from Procedure before the
2294 timeout expires, its content is set to TRUE.
2295 Otherwise, the value is set to FALSE.
2297 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2298 the timeout expires.
2299 @retval others Failed to Startup AP.
2303 StartupThisAPWorker (
2304 IN EFI_AP_PROCEDURE Procedure
,
2305 IN UINTN ProcessorNumber
,
2306 IN EFI_EVENT WaitEvent OPTIONAL
,
2307 IN UINTN TimeoutInMicroseconds
,
2308 IN VOID
*ProcedureArgument OPTIONAL
,
2309 OUT BOOLEAN
*Finished OPTIONAL
2313 CPU_MP_DATA
*CpuMpData
;
2314 CPU_AP_DATA
*CpuData
;
2317 CpuMpData
= GetCpuMpData ();
2319 if (Finished
!= NULL
) {
2324 // Check whether caller processor is BSP
2326 MpInitLibWhoAmI (&CallerNumber
);
2327 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2328 return EFI_DEVICE_ERROR
;
2332 // Check whether processor with the handle specified by ProcessorNumber exists
2334 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2335 return EFI_NOT_FOUND
;
2339 // Check whether specified processor is BSP
2341 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2342 return EFI_INVALID_PARAMETER
;
2346 // Check parameter Procedure
2348 if (Procedure
== NULL
) {
2349 return EFI_INVALID_PARAMETER
;
2355 CheckAndUpdateApsStatus ();
2358 // Check whether specified AP is disabled
2360 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2361 return EFI_INVALID_PARAMETER
;
2365 // If WaitEvent is not NULL, execute in non-blocking mode.
2366 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2367 // CheckAPsStatus() will check completion and timeout periodically.
2369 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2370 CpuData
->WaitEvent
= WaitEvent
;
2371 CpuData
->Finished
= Finished
;
2372 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2373 CpuData
->TotalTime
= 0;
2375 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
, TRUE
);
2378 // If WaitEvent is NULL, execute in blocking mode.
2379 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2381 Status
= EFI_SUCCESS
;
2382 if (WaitEvent
== NULL
) {
2384 Status
= CheckThisAP (ProcessorNumber
);
2385 } while (Status
== EFI_NOT_READY
);
2392 Get pointer to CPU MP Data structure from GUIDed HOB.
2394 @return The pointer to CPU MP Data structure.
2397 GetCpuMpDataFromGuidedHob (
2401 EFI_HOB_GUID_TYPE
*GuidHob
;
2403 CPU_MP_DATA
*CpuMpData
;
2406 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2407 if (GuidHob
!= NULL
) {
2408 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2409 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);