2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2017, 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 ();
201 Restore the volatile registers following INIT IPI.
203 @param[in] VolatileRegisters Pointer to volatile resisters
204 @param[in] IsRestoreDr TRUE: Restore DRx if supported
205 FALSE: Do not restore DRx
208 RestoreVolatileRegisters (
209 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
210 IN BOOLEAN IsRestoreDr
213 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
215 AsmWriteCr0 (VolatileRegisters
->Cr0
);
216 AsmWriteCr3 (VolatileRegisters
->Cr3
);
217 AsmWriteCr4 (VolatileRegisters
->Cr4
);
220 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
221 if (VersionInfoEdx
.Bits
.DE
!= 0) {
223 // If processor supports Debugging Extensions feature
224 // by CPUID.[EAX=01H]:EDX.BIT2
226 AsmWriteDr0 (VolatileRegisters
->Dr0
);
227 AsmWriteDr1 (VolatileRegisters
->Dr1
);
228 AsmWriteDr2 (VolatileRegisters
->Dr2
);
229 AsmWriteDr3 (VolatileRegisters
->Dr3
);
230 AsmWriteDr6 (VolatileRegisters
->Dr6
);
231 AsmWriteDr7 (VolatileRegisters
->Dr7
);
237 Detect whether Mwait-monitor feature is supported.
239 @retval TRUE Mwait-monitor feature is supported.
240 @retval FALSE Mwait-monitor feature is not supported.
247 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
249 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
250 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
256 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
258 @return The AP loop mode.
262 OUT UINT32
*MonitorFilterSize
266 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
268 ASSERT (MonitorFilterSize
!= NULL
);
270 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
271 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
272 if (ApLoopMode
== ApInMwaitLoop
) {
273 if (!IsMwaitSupport ()) {
275 // If processor does not support MONITOR/MWAIT feature,
276 // force AP in Hlt-loop mode
278 ApLoopMode
= ApInHltLoop
;
282 if (ApLoopMode
!= ApInMwaitLoop
) {
283 *MonitorFilterSize
= sizeof (UINT32
);
286 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
287 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
289 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
290 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
297 Sort the APIC ID of all processors.
299 This function sorts the APIC ID of all processors so that processor number is
300 assigned in the ascending order of APIC ID which eases MP debugging.
302 @param[in] CpuMpData Pointer to PEI CPU MP Data
306 IN CPU_MP_DATA
*CpuMpData
313 CPU_INFO_IN_HOB CpuInfo
;
315 CPU_INFO_IN_HOB
*CpuInfoInHob
;
317 ApCount
= CpuMpData
->CpuCount
- 1;
318 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
320 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
323 // Sort key is the hardware default APIC ID
325 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
326 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
327 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
329 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
332 if (Index3
!= Index1
) {
333 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
335 &CpuInfoInHob
[Index3
],
336 &CpuInfoInHob
[Index1
],
337 sizeof (CPU_INFO_IN_HOB
)
339 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
344 // Get the processor number for the BSP
346 ApicId
= GetInitialApicId ();
347 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
348 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
349 CpuMpData
->BspNumber
= (UINT32
) Index1
;
357 Enable x2APIC mode on APs.
359 @param[in, out] Buffer Pointer to private data buffer.
367 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
373 @param[in, out] Buffer Pointer to private data buffer.
381 CPU_MP_DATA
*CpuMpData
;
383 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
385 // Load microcode on AP
387 MicrocodeDetect (CpuMpData
);
389 // Sync BSP's MTRR table to AP
391 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
395 Find the current Processor number by APIC ID.
397 @param[in] CpuMpData Pointer to PEI CPU MP Data
398 @param[out] ProcessorNumber Return the pocessor number found
400 @retval EFI_SUCCESS ProcessorNumber is found and returned.
401 @retval EFI_NOT_FOUND ProcessorNumber is not found.
405 IN CPU_MP_DATA
*CpuMpData
,
406 OUT UINTN
*ProcessorNumber
409 UINTN TotalProcessorNumber
;
411 CPU_INFO_IN_HOB
*CpuInfoInHob
;
413 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
415 TotalProcessorNumber
= CpuMpData
->CpuCount
;
416 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
417 if (CpuInfoInHob
[Index
].ApicId
== GetApicId ()) {
418 *ProcessorNumber
= Index
;
422 return EFI_NOT_FOUND
;
426 This function will get CPU count in the system.
428 @param[in] CpuMpData Pointer to PEI CPU MP Data
430 @return CPU count detected
433 CollectProcessorCount (
434 IN CPU_MP_DATA
*CpuMpData
440 // Send 1st broadcast IPI to APs to wakeup APs
442 CpuMpData
->InitFlag
= ApInitConfig
;
443 CpuMpData
->X2ApicEnable
= FALSE
;
444 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
);
445 CpuMpData
->InitFlag
= ApInitDone
;
446 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
448 // Wait for all APs finished the initialization
450 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
454 if (CpuMpData
->X2ApicEnable
) {
455 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
457 // Wakeup all APs to enable x2APIC mode
459 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
461 // Wait for all known APs finished
463 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
467 // Enable x2APIC on BSP
469 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
471 // Set BSP/Aps state to IDLE
473 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
474 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
477 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
479 // Sort BSP/Aps by CPU APIC ID in ascending order
481 SortApicId (CpuMpData
);
483 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
485 return CpuMpData
->CpuCount
;
489 Initialize CPU AP Data when AP is wakeup at the first time.
491 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
492 @param[in] ProcessorNumber The handle number of processor
493 @param[in] BistData Processor BIST data
494 @param[in] ApTopOfStack Top of AP stack
499 IN OUT CPU_MP_DATA
*CpuMpData
,
500 IN UINTN ProcessorNumber
,
502 IN UINT64 ApTopOfStack
505 CPU_INFO_IN_HOB
*CpuInfoInHob
;
507 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
508 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
509 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
510 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
511 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
513 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
514 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
515 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
517 // Set x2APIC mode if there are any logical processor reporting
518 // an Initial APIC ID of 255 or greater.
520 AcquireSpinLock(&CpuMpData
->MpLock
);
521 CpuMpData
->X2ApicEnable
= TRUE
;
522 ReleaseSpinLock(&CpuMpData
->MpLock
);
525 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
526 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
530 This function will be called from AP reset code if BSP uses WakeUpAP.
532 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
533 @param[in] NumApsExecuting Number of current executing AP
538 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
539 IN UINTN NumApsExecuting
542 CPU_MP_DATA
*CpuMpData
;
543 UINTN ProcessorNumber
;
544 EFI_AP_PROCEDURE Procedure
;
547 volatile UINT32
*ApStartupSignalBuffer
;
548 CPU_INFO_IN_HOB
*CpuInfoInHob
;
552 // AP finished assembly code and begin to execute C code
554 CpuMpData
= ExchangeInfo
->CpuMpData
;
557 // AP's local APIC settings will be lost after received INIT IPI
558 // We need to re-initialize them at here
560 ProgramVirtualWireMode ();
561 SyncLocalApicTimerSetting (CpuMpData
);
564 if (CpuMpData
->InitFlag
== ApInitConfig
) {
568 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
569 ProcessorNumber
= NumApsExecuting
;
571 // This is first time AP wakeup, get BIST information from AP stack
573 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
574 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
576 // Do some AP initialize sync
578 ApInitializeSync (CpuMpData
);
580 // Sync BSP's Control registers to APs
582 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
583 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
584 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
587 // Execute AP function if AP is ready
589 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
591 // Clear AP start-up signal when AP waken up
593 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
594 InterlockedCompareExchange32 (
595 (UINT32
*) ApStartupSignalBuffer
,
599 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
601 // Restore AP's volatile registers saved
603 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
606 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
607 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
608 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
609 if (Procedure
!= NULL
) {
610 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
612 // Enable source debugging on AP function
616 // Invoke AP function here
618 Procedure (Parameter
);
619 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
620 if (CpuMpData
->SwitchBspFlag
) {
622 // Re-get the processor number due to BSP/AP maybe exchange in AP function
624 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
625 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
626 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
627 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
628 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
631 // Re-get the CPU APICID and Initial APICID
633 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
634 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
637 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
642 // AP finished executing C code
644 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
647 // Place AP is specified loop mode
649 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
651 // Save AP volatile registers
653 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
655 // Place AP in HLT-loop
658 DisableInterrupts ();
664 DisableInterrupts ();
665 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
667 // Place AP in MWAIT-loop
669 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
670 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
672 // Check AP start-up signal again.
673 // If AP start-up signal is not set, place AP into
674 // the specified C-state
676 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
678 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
680 // Place AP in Run-loop
688 // If AP start-up signal is written, AP is waken up
689 // otherwise place AP in loop again
691 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
699 Wait for AP wakeup and write AP start-up signal till AP is waken up.
701 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
705 IN
volatile UINT32
*ApStartupSignalBuffer
709 // If AP is waken up, StartupApSignal should be cleared.
710 // Otherwise, write StartupApSignal again till AP waken up.
712 while (InterlockedCompareExchange32 (
713 (UINT32
*) ApStartupSignalBuffer
,
722 This function will fill the exchange info structure.
724 @param[in] CpuMpData Pointer to CPU MP Data
728 FillExchangeInfoData (
729 IN CPU_MP_DATA
*CpuMpData
732 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
734 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
735 ExchangeInfo
->Lock
= 0;
736 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
737 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
738 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
739 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
741 ExchangeInfo
->CodeSegment
= AsmReadCs ();
742 ExchangeInfo
->DataSegment
= AsmReadDs ();
744 ExchangeInfo
->Cr3
= AsmReadCr3 ();
746 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
747 ExchangeInfo
->NumApsExecuting
= 0;
748 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
749 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
750 ExchangeInfo
->CpuMpData
= CpuMpData
;
752 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
754 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
757 // Get the BSP's data of GDT and IDT
759 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
760 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
764 Helper function that waits until the finished AP count reaches the specified
765 limit, or the specified timeout elapses (whichever comes first).
767 @param[in] CpuMpData Pointer to CPU MP Data.
768 @param[in] FinishedApLimit The number of finished APs to wait for.
769 @param[in] TimeLimit The number of microseconds to wait for.
772 TimedWaitForApFinish (
773 IN CPU_MP_DATA
*CpuMpData
,
774 IN UINT32 FinishedApLimit
,
779 This function will be called by BSP to wakeup AP.
781 @param[in] CpuMpData Pointer to CPU MP Data
782 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
783 FALSE: Send IPI to AP by ApicId
784 @param[in] ProcessorNumber The handle number of specified processor
785 @param[in] Procedure The function to be invoked by AP
786 @param[in] ProcedureArgument The argument to be passed into AP function
790 IN CPU_MP_DATA
*CpuMpData
,
791 IN BOOLEAN Broadcast
,
792 IN UINTN ProcessorNumber
,
793 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
794 IN VOID
*ProcedureArgument OPTIONAL
797 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
799 CPU_AP_DATA
*CpuData
;
800 BOOLEAN ResetVectorRequired
;
801 CPU_INFO_IN_HOB
*CpuInfoInHob
;
803 CpuMpData
->FinishedCount
= 0;
804 ResetVectorRequired
= FALSE
;
806 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
807 CpuMpData
->InitFlag
!= ApInitDone
) {
808 ResetVectorRequired
= TRUE
;
809 AllocateResetVector (CpuMpData
);
810 FillExchangeInfoData (CpuMpData
);
811 SaveLocalApicTimerSetting (CpuMpData
);
812 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
814 // Get AP target C-state each time when waking up AP,
815 // for it maybe updated by platform again
817 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
820 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
823 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
824 if (Index
!= CpuMpData
->BspNumber
) {
825 CpuData
= &CpuMpData
->CpuData
[Index
];
826 CpuData
->ApFunction
= (UINTN
) Procedure
;
827 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
828 SetApState (CpuData
, CpuStateReady
);
829 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
830 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
834 if (ResetVectorRequired
) {
838 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
840 if (CpuMpData
->InitFlag
== ApInitConfig
) {
842 // Wait for all potential APs waken up in one specified period
844 TimedWaitForApFinish (
846 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
847 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
851 // Wait all APs waken up if this is not the 1st broadcast of SIPI
853 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
854 CpuData
= &CpuMpData
->CpuData
[Index
];
855 if (Index
!= CpuMpData
->BspNumber
) {
856 WaitApWakeup (CpuData
->StartupApSignal
);
861 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
862 CpuData
->ApFunction
= (UINTN
) Procedure
;
863 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
864 SetApState (CpuData
, CpuStateReady
);
866 // Wakeup specified AP
868 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
869 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
870 if (ResetVectorRequired
) {
871 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
873 CpuInfoInHob
[ProcessorNumber
].ApicId
,
874 (UINT32
) ExchangeInfo
->BufferStart
878 // Wait specified AP waken up
880 WaitApWakeup (CpuData
->StartupApSignal
);
883 if (ResetVectorRequired
) {
884 FreeResetVector (CpuMpData
);
889 Calculate timeout value and return the current performance counter value.
891 Calculate the number of performance counter ticks required for a timeout.
892 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
895 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
896 @param[out] CurrentTime Returns the current value of the performance counter.
898 @return Expected time stamp counter for timeout.
899 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
905 IN UINTN TimeoutInMicroseconds
,
906 OUT UINT64
*CurrentTime
910 // Read the current value of the performance counter
912 *CurrentTime
= GetPerformanceCounter ();
915 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
918 if (TimeoutInMicroseconds
== 0) {
923 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
924 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
925 // it by 1,000,000, to get the number of ticks for the timeout value.
929 GetPerformanceCounterProperties (NULL
, NULL
),
930 TimeoutInMicroseconds
937 Checks whether timeout expires.
939 Check whether the number of elapsed performance counter ticks required for
940 a timeout condition has been reached.
941 If Timeout is zero, which means infinity, return value is always FALSE.
943 @param[in, out] PreviousTime On input, the value of the performance counter
944 when it was last read.
945 On output, the current value of the performance
947 @param[in] TotalTime The total amount of elapsed time in performance
949 @param[in] Timeout The number of performance counter ticks required
950 to reach a timeout condition.
952 @retval TRUE A timeout condition has been reached.
953 @retval FALSE A timeout condition has not been reached.
958 IN OUT UINT64
*PreviousTime
,
959 IN UINT64
*TotalTime
,
972 GetPerformanceCounterProperties (&Start
, &End
);
978 CurrentTime
= GetPerformanceCounter();
979 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
987 *PreviousTime
= CurrentTime
;
988 if (*TotalTime
> Timeout
) {
995 Helper function that waits until the finished AP count reaches the specified
996 limit, or the specified timeout elapses (whichever comes first).
998 @param[in] CpuMpData Pointer to CPU MP Data.
999 @param[in] FinishedApLimit The number of finished APs to wait for.
1000 @param[in] TimeLimit The number of microseconds to wait for.
1003 TimedWaitForApFinish (
1004 IN CPU_MP_DATA
*CpuMpData
,
1005 IN UINT32 FinishedApLimit
,
1010 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1011 // "infinity", so check for (TimeLimit == 0) explicitly.
1013 if (TimeLimit
== 0) {
1017 CpuMpData
->TotalTime
= 0;
1018 CpuMpData
->ExpectedTime
= CalculateTimeout (
1020 &CpuMpData
->CurrentTime
1022 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1024 &CpuMpData
->CurrentTime
,
1025 &CpuMpData
->TotalTime
,
1026 CpuMpData
->ExpectedTime
1031 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1034 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1037 DivU64x64Remainder (
1038 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1039 GetPerformanceCounterProperties (NULL
, NULL
),
1047 Reset an AP to Idle state.
1049 Any task being executed by the AP will be aborted and the AP
1050 will be waiting for a new task in Wait-For-SIPI state.
1052 @param[in] ProcessorNumber The handle number of processor.
1055 ResetProcessorToIdleState (
1056 IN UINTN ProcessorNumber
1059 CPU_MP_DATA
*CpuMpData
;
1061 CpuMpData
= GetCpuMpData ();
1063 CpuMpData
->InitFlag
= ApInitReconfig
;
1064 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
1065 while (CpuMpData
->FinishedCount
< 1) {
1068 CpuMpData
->InitFlag
= ApInitDone
;
1070 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1074 Searches for the next waiting AP.
1076 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1078 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1080 @retval EFI_SUCCESS The next waiting AP has been found.
1081 @retval EFI_NOT_FOUND No waiting AP exists.
1085 GetNextWaitingProcessorNumber (
1086 OUT UINTN
*NextProcessorNumber
1089 UINTN ProcessorNumber
;
1090 CPU_MP_DATA
*CpuMpData
;
1092 CpuMpData
= GetCpuMpData ();
1094 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1095 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1096 *NextProcessorNumber
= ProcessorNumber
;
1101 return EFI_NOT_FOUND
;
1104 /** Checks status of specified AP.
1106 This function checks whether the specified AP has finished the task assigned
1107 by StartupThisAP(), and whether timeout expires.
1109 @param[in] ProcessorNumber The handle number of processor.
1111 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1112 @retval EFI_TIMEOUT The timeout expires.
1113 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1117 IN UINTN ProcessorNumber
1120 CPU_MP_DATA
*CpuMpData
;
1121 CPU_AP_DATA
*CpuData
;
1123 CpuMpData
= GetCpuMpData ();
1124 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1127 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1128 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1129 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1132 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1134 if (GetApState(CpuData
) == CpuStateFinished
) {
1135 if (CpuData
->Finished
!= NULL
) {
1136 *(CpuData
->Finished
) = TRUE
;
1138 SetApState (CpuData
, CpuStateIdle
);
1142 // If timeout expires for StartupThisAP(), report timeout.
1144 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1145 if (CpuData
->Finished
!= NULL
) {
1146 *(CpuData
->Finished
) = FALSE
;
1149 // Reset failed AP to idle state
1151 ResetProcessorToIdleState (ProcessorNumber
);
1156 return EFI_NOT_READY
;
1160 Checks status of all APs.
1162 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1163 and whether timeout expires.
1165 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1166 @retval EFI_TIMEOUT The timeout expires.
1167 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1174 UINTN ProcessorNumber
;
1175 UINTN NextProcessorNumber
;
1178 CPU_MP_DATA
*CpuMpData
;
1179 CPU_AP_DATA
*CpuData
;
1181 CpuMpData
= GetCpuMpData ();
1183 NextProcessorNumber
= 0;
1186 // Go through all APs that are responsible for the StartupAllAPs().
1188 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1189 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1193 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1195 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1196 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1197 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1199 if (GetApState(CpuData
) == CpuStateFinished
) {
1200 CpuMpData
->RunningCount
++;
1201 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1202 SetApState(CpuData
, CpuStateIdle
);
1205 // If in Single Thread mode, then search for the next waiting AP for execution.
1207 if (CpuMpData
->SingleThread
) {
1208 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1210 if (!EFI_ERROR (Status
)) {
1214 (UINT32
) NextProcessorNumber
,
1215 CpuMpData
->Procedure
,
1216 CpuMpData
->ProcArguments
1224 // If all APs finish, return EFI_SUCCESS.
1226 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1231 // If timeout expires, report timeout.
1234 &CpuMpData
->CurrentTime
,
1235 &CpuMpData
->TotalTime
,
1236 CpuMpData
->ExpectedTime
)
1239 // If FailedCpuList is not NULL, record all failed APs in it.
1241 if (CpuMpData
->FailedCpuList
!= NULL
) {
1242 *CpuMpData
->FailedCpuList
=
1243 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1244 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1248 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1250 // Check whether this processor is responsible for StartupAllAPs().
1252 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1254 // Reset failed APs to idle state
1256 ResetProcessorToIdleState (ProcessorNumber
);
1257 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1258 if (CpuMpData
->FailedCpuList
!= NULL
) {
1259 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1263 if (CpuMpData
->FailedCpuList
!= NULL
) {
1264 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1268 return EFI_NOT_READY
;
1272 MP Initialize Library initialization.
1274 This service will allocate AP reset vector and wakeup all APs to do APs
1277 This service must be invoked before all other MP Initialize Library
1278 service are invoked.
1280 @retval EFI_SUCCESS MP initialization succeeds.
1281 @retval Others MP initialization fails.
1286 MpInitLibInitialize (
1290 CPU_MP_DATA
*OldCpuMpData
;
1291 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1292 UINT32 MaxLogicalProcessorNumber
;
1294 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1296 UINT32 MonitorFilterSize
;
1299 CPU_MP_DATA
*CpuMpData
;
1301 UINT8
*MonitorBuffer
;
1303 UINTN ApResetVectorSize
;
1304 UINTN BackupBufferAddr
;
1306 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1307 if (OldCpuMpData
== NULL
) {
1308 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1310 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1312 ASSERT (MaxLogicalProcessorNumber
!= 0);
1314 AsmGetAddressMap (&AddressMap
);
1315 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1316 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1317 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1319 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1320 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1321 BufferSize
+= sizeof (CPU_MP_DATA
);
1322 BufferSize
+= ApResetVectorSize
;
1323 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1324 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1325 ASSERT (MpBuffer
!= NULL
);
1326 ZeroMem (MpBuffer
, BufferSize
);
1327 Buffer
= (UINTN
) MpBuffer
;
1329 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1330 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1331 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1332 CpuMpData
->Buffer
= Buffer
;
1333 CpuMpData
->CpuApStackSize
= ApStackSize
;
1334 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1335 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1336 CpuMpData
->SaveRestoreFlag
= FALSE
;
1337 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1338 CpuMpData
->CpuCount
= 1;
1339 CpuMpData
->BspNumber
= 0;
1340 CpuMpData
->WaitEvent
= NULL
;
1341 CpuMpData
->SwitchBspFlag
= FALSE
;
1342 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1343 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1344 InitializeSpinLock(&CpuMpData
->MpLock
);
1346 // Save BSP's Control registers to APs
1348 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1350 // Set BSP basic information
1352 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
);
1354 // Save assembly code information
1356 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1358 // Finally set AP loop mode
1360 CpuMpData
->ApLoopMode
= ApLoopMode
;
1361 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1363 // Set up APs wakeup signal buffer
1365 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1366 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1367 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1370 // Load Microcode on BSP
1372 MicrocodeDetect (CpuMpData
);
1374 // Store BSP's MTRR setting
1376 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1378 // Enable the local APIC for Virtual Wire Mode.
1380 ProgramVirtualWireMode ();
1382 if (OldCpuMpData
== NULL
) {
1383 if (MaxLogicalProcessorNumber
> 1) {
1385 // Wakeup all APs and calculate the processor count in system
1387 CollectProcessorCount (CpuMpData
);
1391 // APs have been wakeup before, just get the CPU Information
1394 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1395 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1396 CpuMpData
->InitFlag
= ApInitReconfig
;
1397 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1398 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1399 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1400 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1401 if (CpuInfoInHob
[Index
].InitialApicId
>= 255) {
1402 CpuMpData
->X2ApicEnable
= TRUE
;
1404 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1405 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1407 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1408 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1409 sizeof (CPU_VOLATILE_REGISTERS
)
1412 if (MaxLogicalProcessorNumber
> 1) {
1414 // Wakeup APs to do some AP initialize sync
1416 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1418 // Wait for all APs finished initialization
1420 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1423 CpuMpData
->InitFlag
= ApInitDone
;
1424 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1425 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1431 // Initialize global data for MP support
1433 InitMpGlobalData (CpuMpData
);
1439 Gets detailed MP-related information on the requested processor at the
1440 instant this call is made. This service may only be called from the BSP.
1442 @param[in] ProcessorNumber The handle number of processor.
1443 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1444 the requested processor is deposited.
1445 @param[out] HealthData Return processor health data.
1447 @retval EFI_SUCCESS Processor information was returned.
1448 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1449 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1450 @retval EFI_NOT_FOUND The processor with the handle specified by
1451 ProcessorNumber does not exist in the platform.
1452 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1457 MpInitLibGetProcessorInfo (
1458 IN UINTN ProcessorNumber
,
1459 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1460 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1463 CPU_MP_DATA
*CpuMpData
;
1465 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1467 CpuMpData
= GetCpuMpData ();
1468 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1471 // Check whether caller processor is BSP
1473 MpInitLibWhoAmI (&CallerNumber
);
1474 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1475 return EFI_DEVICE_ERROR
;
1478 if (ProcessorInfoBuffer
== NULL
) {
1479 return EFI_INVALID_PARAMETER
;
1482 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1483 return EFI_NOT_FOUND
;
1486 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1487 ProcessorInfoBuffer
->StatusFlag
= 0;
1488 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1489 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1491 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1492 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1494 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1495 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1497 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1501 // Get processor location information
1503 GetProcessorLocationByApicId (
1504 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1505 &ProcessorInfoBuffer
->Location
.Package
,
1506 &ProcessorInfoBuffer
->Location
.Core
,
1507 &ProcessorInfoBuffer
->Location
.Thread
1510 if (HealthData
!= NULL
) {
1511 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1518 Worker function to switch the requested AP to be the BSP from that point onward.
1520 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1521 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1522 enabled AP. Otherwise, it will be disabled.
1524 @retval EFI_SUCCESS BSP successfully switched.
1525 @retval others Failed to switch BSP.
1530 IN UINTN ProcessorNumber
,
1531 IN BOOLEAN EnableOldBSP
1534 CPU_MP_DATA
*CpuMpData
;
1537 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1538 BOOLEAN OldInterruptState
;
1539 BOOLEAN OldTimerInterruptState
;
1542 // Save and Disable Local APIC timer interrupt
1544 OldTimerInterruptState
= GetApicTimerInterruptState ();
1545 DisableApicTimerInterrupt ();
1547 // Before send both BSP and AP to a procedure to exchange their roles,
1548 // interrupt must be disabled. This is because during the exchange role
1549 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1550 // be corrupted, since interrupt return address will be pushed to stack
1553 OldInterruptState
= SaveAndDisableInterrupts ();
1556 // Mask LINT0 & LINT1 for the old BSP
1558 DisableLvtInterrupts ();
1560 CpuMpData
= GetCpuMpData ();
1563 // Check whether caller processor is BSP
1565 MpInitLibWhoAmI (&CallerNumber
);
1566 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1570 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1571 return EFI_NOT_FOUND
;
1575 // Check whether specified AP is disabled
1577 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1578 if (State
== CpuStateDisabled
) {
1579 return EFI_INVALID_PARAMETER
;
1583 // Check whether ProcessorNumber specifies the current BSP
1585 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1586 return EFI_INVALID_PARAMETER
;
1590 // Check whether specified AP is busy
1592 if (State
== CpuStateBusy
) {
1593 return EFI_NOT_READY
;
1596 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1597 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1598 CpuMpData
->SwitchBspFlag
= TRUE
;
1599 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1602 // Clear the BSP bit of MSR_IA32_APIC_BASE
1604 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1605 ApicBaseMsr
.Bits
.BSP
= 0;
1606 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1609 // Need to wakeUp AP (future BSP).
1611 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1613 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1616 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1618 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1619 ApicBaseMsr
.Bits
.BSP
= 1;
1620 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1623 // Wait for old BSP finished AP task
1625 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1629 CpuMpData
->SwitchBspFlag
= FALSE
;
1631 // Set old BSP enable state
1633 if (!EnableOldBSP
) {
1634 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1636 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1639 // Save new BSP number
1641 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1644 // Restore interrupt state.
1646 SetInterruptState (OldInterruptState
);
1648 if (OldTimerInterruptState
) {
1649 EnableApicTimerInterrupt ();
1656 Worker function to let the caller enable or disable an AP from this point onward.
1657 This service may only be called from the BSP.
1659 @param[in] ProcessorNumber The handle number of AP.
1660 @param[in] EnableAP Specifies the new state for the processor for
1661 enabled, FALSE for disabled.
1662 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1663 the new health status of the AP.
1665 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1666 @retval others Failed to Enable/Disable AP.
1670 EnableDisableApWorker (
1671 IN UINTN ProcessorNumber
,
1672 IN BOOLEAN EnableAP
,
1673 IN UINT32
*HealthFlag OPTIONAL
1676 CPU_MP_DATA
*CpuMpData
;
1679 CpuMpData
= GetCpuMpData ();
1682 // Check whether caller processor is BSP
1684 MpInitLibWhoAmI (&CallerNumber
);
1685 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1686 return EFI_DEVICE_ERROR
;
1689 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1690 return EFI_INVALID_PARAMETER
;
1693 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1694 return EFI_NOT_FOUND
;
1698 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1700 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1703 if (HealthFlag
!= NULL
) {
1704 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1705 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1712 This return the handle number for the calling processor. This service may be
1713 called from the BSP and APs.
1715 @param[out] ProcessorNumber Pointer to the handle number of AP.
1716 The range is from 0 to the total number of
1717 logical processors minus 1. The total number of
1718 logical processors can be retrieved by
1719 MpInitLibGetNumberOfProcessors().
1721 @retval EFI_SUCCESS The current processor handle number was returned
1723 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1724 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1730 OUT UINTN
*ProcessorNumber
1733 CPU_MP_DATA
*CpuMpData
;
1735 if (ProcessorNumber
== NULL
) {
1736 return EFI_INVALID_PARAMETER
;
1739 CpuMpData
= GetCpuMpData ();
1741 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1745 Retrieves the number of logical processor in the platform and the number of
1746 those logical processors that are enabled on this boot. This service may only
1747 be called from the BSP.
1749 @param[out] NumberOfProcessors Pointer to the total number of logical
1750 processors in the system, including the BSP
1752 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1753 processors that exist in system, including
1756 @retval EFI_SUCCESS The number of logical processors and enabled
1757 logical processors was retrieved.
1758 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1759 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1761 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1766 MpInitLibGetNumberOfProcessors (
1767 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1768 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1771 CPU_MP_DATA
*CpuMpData
;
1773 UINTN ProcessorNumber
;
1774 UINTN EnabledProcessorNumber
;
1777 CpuMpData
= GetCpuMpData ();
1779 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1780 return EFI_INVALID_PARAMETER
;
1784 // Check whether caller processor is BSP
1786 MpInitLibWhoAmI (&CallerNumber
);
1787 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1788 return EFI_DEVICE_ERROR
;
1791 ProcessorNumber
= CpuMpData
->CpuCount
;
1792 EnabledProcessorNumber
= 0;
1793 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1794 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1795 EnabledProcessorNumber
++;
1799 if (NumberOfProcessors
!= NULL
) {
1800 *NumberOfProcessors
= ProcessorNumber
;
1802 if (NumberOfEnabledProcessors
!= NULL
) {
1803 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1811 Worker function to execute a caller provided function on all enabled APs.
1813 @param[in] Procedure A pointer to the function to be run on
1814 enabled APs of the system.
1815 @param[in] SingleThread If TRUE, then all the enabled APs execute
1816 the function specified by Procedure one by
1817 one, in ascending order of processor handle
1818 number. If FALSE, then all the enabled APs
1819 execute the function specified by Procedure
1821 @param[in] WaitEvent The event created by the caller with CreateEvent()
1823 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1824 APs to return from Procedure, either for
1825 blocking or non-blocking mode.
1826 @param[in] ProcedureArgument The parameter passed into Procedure for
1828 @param[out] FailedCpuList If all APs finish successfully, then its
1829 content is set to NULL. If not all APs
1830 finish before timeout expires, then its
1831 content is set to address of the buffer
1832 holding handle numbers of the failed APs.
1834 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1835 the timeout expired.
1836 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1838 @retval others Failed to Startup all APs.
1842 StartupAllAPsWorker (
1843 IN EFI_AP_PROCEDURE Procedure
,
1844 IN BOOLEAN SingleThread
,
1845 IN EFI_EVENT WaitEvent OPTIONAL
,
1846 IN UINTN TimeoutInMicroseconds
,
1847 IN VOID
*ProcedureArgument OPTIONAL
,
1848 OUT UINTN
**FailedCpuList OPTIONAL
1852 CPU_MP_DATA
*CpuMpData
;
1853 UINTN ProcessorCount
;
1854 UINTN ProcessorNumber
;
1856 CPU_AP_DATA
*CpuData
;
1857 BOOLEAN HasEnabledAp
;
1860 CpuMpData
= GetCpuMpData ();
1862 if (FailedCpuList
!= NULL
) {
1863 *FailedCpuList
= NULL
;
1866 if (CpuMpData
->CpuCount
== 1) {
1867 return EFI_NOT_STARTED
;
1870 if (Procedure
== NULL
) {
1871 return EFI_INVALID_PARAMETER
;
1875 // Check whether caller processor is BSP
1877 MpInitLibWhoAmI (&CallerNumber
);
1878 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1879 return EFI_DEVICE_ERROR
;
1885 CheckAndUpdateApsStatus ();
1887 ProcessorCount
= CpuMpData
->CpuCount
;
1888 HasEnabledAp
= FALSE
;
1890 // Check whether all enabled APs are idle.
1891 // If any enabled AP is not idle, return EFI_NOT_READY.
1893 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1894 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1895 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1896 ApState
= GetApState (CpuData
);
1897 if (ApState
!= CpuStateDisabled
) {
1898 HasEnabledAp
= TRUE
;
1899 if (ApState
!= CpuStateIdle
) {
1901 // If any enabled APs are busy, return EFI_NOT_READY.
1903 return EFI_NOT_READY
;
1909 if (!HasEnabledAp
) {
1911 // If no enabled AP exists, return EFI_NOT_STARTED.
1913 return EFI_NOT_STARTED
;
1916 CpuMpData
->StartCount
= 0;
1917 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1918 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1919 CpuData
->Waiting
= FALSE
;
1920 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1921 if (CpuData
->State
== CpuStateIdle
) {
1923 // Mark this processor as responsible for current calling.
1925 CpuData
->Waiting
= TRUE
;
1926 CpuMpData
->StartCount
++;
1931 CpuMpData
->Procedure
= Procedure
;
1932 CpuMpData
->ProcArguments
= ProcedureArgument
;
1933 CpuMpData
->SingleThread
= SingleThread
;
1934 CpuMpData
->FinishedCount
= 0;
1935 CpuMpData
->RunningCount
= 0;
1936 CpuMpData
->FailedCpuList
= FailedCpuList
;
1937 CpuMpData
->ExpectedTime
= CalculateTimeout (
1938 TimeoutInMicroseconds
,
1939 &CpuMpData
->CurrentTime
1941 CpuMpData
->TotalTime
= 0;
1942 CpuMpData
->WaitEvent
= WaitEvent
;
1944 if (!SingleThread
) {
1945 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1947 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1948 if (ProcessorNumber
== CallerNumber
) {
1951 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1952 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1958 Status
= EFI_SUCCESS
;
1959 if (WaitEvent
== NULL
) {
1961 Status
= CheckAllAPs ();
1962 } while (Status
== EFI_NOT_READY
);
1969 Worker function to let the caller get one enabled AP to execute a caller-provided
1972 @param[in] Procedure A pointer to the function to be run on
1973 enabled APs of the system.
1974 @param[in] ProcessorNumber The handle number of the AP.
1975 @param[in] WaitEvent The event created by the caller with CreateEvent()
1977 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1978 APs to return from Procedure, either for
1979 blocking or non-blocking mode.
1980 @param[in] ProcedureArgument The parameter passed into Procedure for
1982 @param[out] Finished If AP returns from Procedure before the
1983 timeout expires, its content is set to TRUE.
1984 Otherwise, the value is set to FALSE.
1986 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1987 the timeout expires.
1988 @retval others Failed to Startup AP.
1992 StartupThisAPWorker (
1993 IN EFI_AP_PROCEDURE Procedure
,
1994 IN UINTN ProcessorNumber
,
1995 IN EFI_EVENT WaitEvent OPTIONAL
,
1996 IN UINTN TimeoutInMicroseconds
,
1997 IN VOID
*ProcedureArgument OPTIONAL
,
1998 OUT BOOLEAN
*Finished OPTIONAL
2002 CPU_MP_DATA
*CpuMpData
;
2003 CPU_AP_DATA
*CpuData
;
2006 CpuMpData
= GetCpuMpData ();
2008 if (Finished
!= NULL
) {
2013 // Check whether caller processor is BSP
2015 MpInitLibWhoAmI (&CallerNumber
);
2016 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2017 return EFI_DEVICE_ERROR
;
2021 // Check whether processor with the handle specified by ProcessorNumber exists
2023 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2024 return EFI_NOT_FOUND
;
2028 // Check whether specified processor is BSP
2030 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2031 return EFI_INVALID_PARAMETER
;
2035 // Check parameter Procedure
2037 if (Procedure
== NULL
) {
2038 return EFI_INVALID_PARAMETER
;
2044 CheckAndUpdateApsStatus ();
2047 // Check whether specified AP is disabled
2049 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2050 return EFI_INVALID_PARAMETER
;
2054 // If WaitEvent is not NULL, execute in non-blocking mode.
2055 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2056 // CheckAPsStatus() will check completion and timeout periodically.
2058 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2059 CpuData
->WaitEvent
= WaitEvent
;
2060 CpuData
->Finished
= Finished
;
2061 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2062 CpuData
->TotalTime
= 0;
2064 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2067 // If WaitEvent is NULL, execute in blocking mode.
2068 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2070 Status
= EFI_SUCCESS
;
2071 if (WaitEvent
== NULL
) {
2073 Status
= CheckThisAP (ProcessorNumber
);
2074 } while (Status
== EFI_NOT_READY
);
2081 Get pointer to CPU MP Data structure from GUIDed HOB.
2083 @return The pointer to CPU MP Data structure.
2086 GetCpuMpDataFromGuidedHob (
2090 EFI_HOB_GUID_TYPE
*GuidHob
;
2092 CPU_MP_DATA
*CpuMpData
;
2095 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2096 if (GuidHob
!= NULL
) {
2097 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2098 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2104 Get available system memory below 1MB by specified size.
2106 @param[in] CpuMpData The pointer to CPU MP Data structure.
2109 BackupAndPrepareWakeupBuffer(
2110 IN CPU_MP_DATA
*CpuMpData
2114 (VOID
*) CpuMpData
->BackupBuffer
,
2115 (VOID
*) CpuMpData
->WakeupBuffer
,
2116 CpuMpData
->BackupBufferSize
2119 (VOID
*) CpuMpData
->WakeupBuffer
,
2120 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
2121 CpuMpData
->AddressMap
.RendezvousFunnelSize
2126 Restore wakeup buffer data.
2128 @param[in] CpuMpData The pointer to CPU MP Data structure.
2131 RestoreWakeupBuffer(
2132 IN CPU_MP_DATA
*CpuMpData
2136 (VOID
*) CpuMpData
->WakeupBuffer
,
2137 (VOID
*) CpuMpData
->BackupBuffer
,
2138 CpuMpData
->BackupBufferSize