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 ();
755 // Get the BSP's data of GDT and IDT
757 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
758 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
762 Helper function that waits until the finished AP count reaches the specified
763 limit, or the specified timeout elapses (whichever comes first).
765 @param[in] CpuMpData Pointer to CPU MP Data.
766 @param[in] FinishedApLimit The number of finished APs to wait for.
767 @param[in] TimeLimit The number of microseconds to wait for.
770 TimedWaitForApFinish (
771 IN CPU_MP_DATA
*CpuMpData
,
772 IN UINT32 FinishedApLimit
,
777 This function will be called by BSP to wakeup AP.
779 @param[in] CpuMpData Pointer to CPU MP Data
780 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
781 FALSE: Send IPI to AP by ApicId
782 @param[in] ProcessorNumber The handle number of specified processor
783 @param[in] Procedure The function to be invoked by AP
784 @param[in] ProcedureArgument The argument to be passed into AP function
788 IN CPU_MP_DATA
*CpuMpData
,
789 IN BOOLEAN Broadcast
,
790 IN UINTN ProcessorNumber
,
791 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
792 IN VOID
*ProcedureArgument OPTIONAL
795 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
797 CPU_AP_DATA
*CpuData
;
798 BOOLEAN ResetVectorRequired
;
799 CPU_INFO_IN_HOB
*CpuInfoInHob
;
801 CpuMpData
->FinishedCount
= 0;
802 ResetVectorRequired
= FALSE
;
804 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
805 CpuMpData
->InitFlag
!= ApInitDone
) {
806 ResetVectorRequired
= TRUE
;
807 AllocateResetVector (CpuMpData
);
808 FillExchangeInfoData (CpuMpData
);
809 SaveLocalApicTimerSetting (CpuMpData
);
810 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
812 // Get AP target C-state each time when waking up AP,
813 // for it maybe updated by platform again
815 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
818 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
821 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
822 if (Index
!= CpuMpData
->BspNumber
) {
823 CpuData
= &CpuMpData
->CpuData
[Index
];
824 CpuData
->ApFunction
= (UINTN
) Procedure
;
825 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
826 SetApState (CpuData
, CpuStateReady
);
827 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
828 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
832 if (ResetVectorRequired
) {
836 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
838 if (CpuMpData
->InitFlag
== ApInitConfig
) {
840 // Wait for all potential APs waken up in one specified period
842 TimedWaitForApFinish (
844 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
845 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
849 // Wait all APs waken up if this is not the 1st broadcast of SIPI
851 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
852 CpuData
= &CpuMpData
->CpuData
[Index
];
853 if (Index
!= CpuMpData
->BspNumber
) {
854 WaitApWakeup (CpuData
->StartupApSignal
);
859 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
860 CpuData
->ApFunction
= (UINTN
) Procedure
;
861 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
862 SetApState (CpuData
, CpuStateReady
);
864 // Wakeup specified AP
866 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
867 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
868 if (ResetVectorRequired
) {
869 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
871 CpuInfoInHob
[ProcessorNumber
].ApicId
,
872 (UINT32
) ExchangeInfo
->BufferStart
876 // Wait specified AP waken up
878 WaitApWakeup (CpuData
->StartupApSignal
);
881 if (ResetVectorRequired
) {
882 FreeResetVector (CpuMpData
);
887 Calculate timeout value and return the current performance counter value.
889 Calculate the number of performance counter ticks required for a timeout.
890 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
893 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
894 @param[out] CurrentTime Returns the current value of the performance counter.
896 @return Expected time stamp counter for timeout.
897 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
903 IN UINTN TimeoutInMicroseconds
,
904 OUT UINT64
*CurrentTime
908 // Read the current value of the performance counter
910 *CurrentTime
= GetPerformanceCounter ();
913 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
916 if (TimeoutInMicroseconds
== 0) {
921 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
922 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
923 // it by 1,000,000, to get the number of ticks for the timeout value.
927 GetPerformanceCounterProperties (NULL
, NULL
),
928 TimeoutInMicroseconds
935 Checks whether timeout expires.
937 Check whether the number of elapsed performance counter ticks required for
938 a timeout condition has been reached.
939 If Timeout is zero, which means infinity, return value is always FALSE.
941 @param[in, out] PreviousTime On input, the value of the performance counter
942 when it was last read.
943 On output, the current value of the performance
945 @param[in] TotalTime The total amount of elapsed time in performance
947 @param[in] Timeout The number of performance counter ticks required
948 to reach a timeout condition.
950 @retval TRUE A timeout condition has been reached.
951 @retval FALSE A timeout condition has not been reached.
956 IN OUT UINT64
*PreviousTime
,
957 IN UINT64
*TotalTime
,
970 GetPerformanceCounterProperties (&Start
, &End
);
976 CurrentTime
= GetPerformanceCounter();
977 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
985 *PreviousTime
= CurrentTime
;
986 if (*TotalTime
> Timeout
) {
993 Helper function that waits until the finished AP count reaches the specified
994 limit, or the specified timeout elapses (whichever comes first).
996 @param[in] CpuMpData Pointer to CPU MP Data.
997 @param[in] FinishedApLimit The number of finished APs to wait for.
998 @param[in] TimeLimit The number of microseconds to wait for.
1001 TimedWaitForApFinish (
1002 IN CPU_MP_DATA
*CpuMpData
,
1003 IN UINT32 FinishedApLimit
,
1008 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1009 // "infinity", so check for (TimeLimit == 0) explicitly.
1011 if (TimeLimit
== 0) {
1015 CpuMpData
->TotalTime
= 0;
1016 CpuMpData
->ExpectedTime
= CalculateTimeout (
1018 &CpuMpData
->CurrentTime
1020 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1022 &CpuMpData
->CurrentTime
,
1023 &CpuMpData
->TotalTime
,
1024 CpuMpData
->ExpectedTime
1029 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1032 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1035 DivU64x64Remainder (
1036 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1037 GetPerformanceCounterProperties (NULL
, NULL
),
1045 Reset an AP to Idle state.
1047 Any task being executed by the AP will be aborted and the AP
1048 will be waiting for a new task in Wait-For-SIPI state.
1050 @param[in] ProcessorNumber The handle number of processor.
1053 ResetProcessorToIdleState (
1054 IN UINTN ProcessorNumber
1057 CPU_MP_DATA
*CpuMpData
;
1059 CpuMpData
= GetCpuMpData ();
1061 CpuMpData
->InitFlag
= ApInitReconfig
;
1062 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
1063 while (CpuMpData
->FinishedCount
< 1) {
1066 CpuMpData
->InitFlag
= ApInitDone
;
1068 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1072 Searches for the next waiting AP.
1074 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1076 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1078 @retval EFI_SUCCESS The next waiting AP has been found.
1079 @retval EFI_NOT_FOUND No waiting AP exists.
1083 GetNextWaitingProcessorNumber (
1084 OUT UINTN
*NextProcessorNumber
1087 UINTN ProcessorNumber
;
1088 CPU_MP_DATA
*CpuMpData
;
1090 CpuMpData
= GetCpuMpData ();
1092 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1093 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1094 *NextProcessorNumber
= ProcessorNumber
;
1099 return EFI_NOT_FOUND
;
1102 /** Checks status of specified AP.
1104 This function checks whether the specified AP has finished the task assigned
1105 by StartupThisAP(), and whether timeout expires.
1107 @param[in] ProcessorNumber The handle number of processor.
1109 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1110 @retval EFI_TIMEOUT The timeout expires.
1111 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1115 IN UINTN ProcessorNumber
1118 CPU_MP_DATA
*CpuMpData
;
1119 CPU_AP_DATA
*CpuData
;
1121 CpuMpData
= GetCpuMpData ();
1122 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1125 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1126 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1127 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1130 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1132 if (GetApState(CpuData
) == CpuStateFinished
) {
1133 if (CpuData
->Finished
!= NULL
) {
1134 *(CpuData
->Finished
) = TRUE
;
1136 SetApState (CpuData
, CpuStateIdle
);
1140 // If timeout expires for StartupThisAP(), report timeout.
1142 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1143 if (CpuData
->Finished
!= NULL
) {
1144 *(CpuData
->Finished
) = FALSE
;
1147 // Reset failed AP to idle state
1149 ResetProcessorToIdleState (ProcessorNumber
);
1154 return EFI_NOT_READY
;
1158 Checks status of all APs.
1160 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1161 and whether timeout expires.
1163 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1164 @retval EFI_TIMEOUT The timeout expires.
1165 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1172 UINTN ProcessorNumber
;
1173 UINTN NextProcessorNumber
;
1176 CPU_MP_DATA
*CpuMpData
;
1177 CPU_AP_DATA
*CpuData
;
1179 CpuMpData
= GetCpuMpData ();
1181 NextProcessorNumber
= 0;
1184 // Go through all APs that are responsible for the StartupAllAPs().
1186 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1187 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1191 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1193 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1194 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1195 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1197 if (GetApState(CpuData
) == CpuStateFinished
) {
1198 CpuMpData
->RunningCount
++;
1199 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1200 SetApState(CpuData
, CpuStateIdle
);
1203 // If in Single Thread mode, then search for the next waiting AP for execution.
1205 if (CpuMpData
->SingleThread
) {
1206 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1208 if (!EFI_ERROR (Status
)) {
1212 (UINT32
) NextProcessorNumber
,
1213 CpuMpData
->Procedure
,
1214 CpuMpData
->ProcArguments
1222 // If all APs finish, return EFI_SUCCESS.
1224 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1229 // If timeout expires, report timeout.
1232 &CpuMpData
->CurrentTime
,
1233 &CpuMpData
->TotalTime
,
1234 CpuMpData
->ExpectedTime
)
1237 // If FailedCpuList is not NULL, record all failed APs in it.
1239 if (CpuMpData
->FailedCpuList
!= NULL
) {
1240 *CpuMpData
->FailedCpuList
=
1241 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1242 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1246 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1248 // Check whether this processor is responsible for StartupAllAPs().
1250 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1252 // Reset failed APs to idle state
1254 ResetProcessorToIdleState (ProcessorNumber
);
1255 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1256 if (CpuMpData
->FailedCpuList
!= NULL
) {
1257 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1261 if (CpuMpData
->FailedCpuList
!= NULL
) {
1262 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1266 return EFI_NOT_READY
;
1270 MP Initialize Library initialization.
1272 This service will allocate AP reset vector and wakeup all APs to do APs
1275 This service must be invoked before all other MP Initialize Library
1276 service are invoked.
1278 @retval EFI_SUCCESS MP initialization succeeds.
1279 @retval Others MP initialization fails.
1284 MpInitLibInitialize (
1288 CPU_MP_DATA
*OldCpuMpData
;
1289 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1290 UINT32 MaxLogicalProcessorNumber
;
1292 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1294 UINT32 MonitorFilterSize
;
1297 CPU_MP_DATA
*CpuMpData
;
1299 UINT8
*MonitorBuffer
;
1301 UINTN ApResetVectorSize
;
1302 UINTN BackupBufferAddr
;
1304 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1305 if (OldCpuMpData
== NULL
) {
1306 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1308 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1310 ASSERT (MaxLogicalProcessorNumber
!= 0);
1312 AsmGetAddressMap (&AddressMap
);
1313 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1314 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1315 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1317 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1318 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1319 BufferSize
+= sizeof (CPU_MP_DATA
);
1320 BufferSize
+= ApResetVectorSize
;
1321 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1322 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1323 ASSERT (MpBuffer
!= NULL
);
1324 ZeroMem (MpBuffer
, BufferSize
);
1325 Buffer
= (UINTN
) MpBuffer
;
1327 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1328 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1329 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1330 CpuMpData
->Buffer
= Buffer
;
1331 CpuMpData
->CpuApStackSize
= ApStackSize
;
1332 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1333 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1334 CpuMpData
->SaveRestoreFlag
= FALSE
;
1335 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1336 CpuMpData
->CpuCount
= 1;
1337 CpuMpData
->BspNumber
= 0;
1338 CpuMpData
->WaitEvent
= NULL
;
1339 CpuMpData
->SwitchBspFlag
= FALSE
;
1340 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1341 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1342 InitializeSpinLock(&CpuMpData
->MpLock
);
1344 // Save BSP's Control registers to APs
1346 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1348 // Set BSP basic information
1350 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
);
1352 // Save assembly code information
1354 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1356 // Finally set AP loop mode
1358 CpuMpData
->ApLoopMode
= ApLoopMode
;
1359 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1361 // Set up APs wakeup signal buffer
1363 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1364 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1365 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1368 // Load Microcode on BSP
1370 MicrocodeDetect (CpuMpData
);
1372 // Store BSP's MTRR setting
1374 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1376 // Enable the local APIC for Virtual Wire Mode.
1378 ProgramVirtualWireMode ();
1380 if (OldCpuMpData
== NULL
) {
1381 if (MaxLogicalProcessorNumber
> 1) {
1383 // Wakeup all APs and calculate the processor count in system
1385 CollectProcessorCount (CpuMpData
);
1389 // APs have been wakeup before, just get the CPU Information
1392 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1393 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1394 CpuMpData
->InitFlag
= ApInitReconfig
;
1395 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1396 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1397 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1398 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1399 if (CpuInfoInHob
[Index
].InitialApicId
>= 255) {
1400 CpuMpData
->X2ApicEnable
= TRUE
;
1402 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1403 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1405 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1406 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1407 sizeof (CPU_VOLATILE_REGISTERS
)
1410 if (MaxLogicalProcessorNumber
> 1) {
1412 // Wakeup APs to do some AP initialize sync
1414 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1416 // Wait for all APs finished initialization
1418 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1421 CpuMpData
->InitFlag
= ApInitDone
;
1422 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1423 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1429 // Initialize global data for MP support
1431 InitMpGlobalData (CpuMpData
);
1437 Gets detailed MP-related information on the requested processor at the
1438 instant this call is made. This service may only be called from the BSP.
1440 @param[in] ProcessorNumber The handle number of processor.
1441 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1442 the requested processor is deposited.
1443 @param[out] HealthData Return processor health data.
1445 @retval EFI_SUCCESS Processor information was returned.
1446 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1447 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1448 @retval EFI_NOT_FOUND The processor with the handle specified by
1449 ProcessorNumber does not exist in the platform.
1450 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1455 MpInitLibGetProcessorInfo (
1456 IN UINTN ProcessorNumber
,
1457 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1458 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1461 CPU_MP_DATA
*CpuMpData
;
1463 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1465 CpuMpData
= GetCpuMpData ();
1466 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1469 // Check whether caller processor is BSP
1471 MpInitLibWhoAmI (&CallerNumber
);
1472 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1473 return EFI_DEVICE_ERROR
;
1476 if (ProcessorInfoBuffer
== NULL
) {
1477 return EFI_INVALID_PARAMETER
;
1480 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1481 return EFI_NOT_FOUND
;
1484 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1485 ProcessorInfoBuffer
->StatusFlag
= 0;
1486 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1487 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1489 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1490 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1492 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1493 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1495 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1499 // Get processor location information
1501 GetProcessorLocationByApicId (
1502 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1503 &ProcessorInfoBuffer
->Location
.Package
,
1504 &ProcessorInfoBuffer
->Location
.Core
,
1505 &ProcessorInfoBuffer
->Location
.Thread
1508 if (HealthData
!= NULL
) {
1509 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1516 Worker function to switch the requested AP to be the BSP from that point onward.
1518 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1519 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1520 enabled AP. Otherwise, it will be disabled.
1522 @retval EFI_SUCCESS BSP successfully switched.
1523 @retval others Failed to switch BSP.
1528 IN UINTN ProcessorNumber
,
1529 IN BOOLEAN EnableOldBSP
1532 CPU_MP_DATA
*CpuMpData
;
1535 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1536 BOOLEAN OldInterruptState
;
1537 BOOLEAN OldTimerInterruptState
;
1540 // Save and Disable Local APIC timer interrupt
1542 OldTimerInterruptState
= GetApicTimerInterruptState ();
1543 DisableApicTimerInterrupt ();
1545 // Before send both BSP and AP to a procedure to exchange their roles,
1546 // interrupt must be disabled. This is because during the exchange role
1547 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1548 // be corrupted, since interrupt return address will be pushed to stack
1551 OldInterruptState
= SaveAndDisableInterrupts ();
1554 // Mask LINT0 & LINT1 for the old BSP
1556 DisableLvtInterrupts ();
1558 CpuMpData
= GetCpuMpData ();
1561 // Check whether caller processor is BSP
1563 MpInitLibWhoAmI (&CallerNumber
);
1564 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1568 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1569 return EFI_NOT_FOUND
;
1573 // Check whether specified AP is disabled
1575 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1576 if (State
== CpuStateDisabled
) {
1577 return EFI_INVALID_PARAMETER
;
1581 // Check whether ProcessorNumber specifies the current BSP
1583 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1584 return EFI_INVALID_PARAMETER
;
1588 // Check whether specified AP is busy
1590 if (State
== CpuStateBusy
) {
1591 return EFI_NOT_READY
;
1594 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1595 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1596 CpuMpData
->SwitchBspFlag
= TRUE
;
1597 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1600 // Clear the BSP bit of MSR_IA32_APIC_BASE
1602 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1603 ApicBaseMsr
.Bits
.BSP
= 0;
1604 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1607 // Need to wakeUp AP (future BSP).
1609 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1611 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1614 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1616 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1617 ApicBaseMsr
.Bits
.BSP
= 1;
1618 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1621 // Wait for old BSP finished AP task
1623 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1627 CpuMpData
->SwitchBspFlag
= FALSE
;
1629 // Set old BSP enable state
1631 if (!EnableOldBSP
) {
1632 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1634 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1637 // Save new BSP number
1639 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1642 // Restore interrupt state.
1644 SetInterruptState (OldInterruptState
);
1646 if (OldTimerInterruptState
) {
1647 EnableApicTimerInterrupt ();
1654 Worker function to let the caller enable or disable an AP from this point onward.
1655 This service may only be called from the BSP.
1657 @param[in] ProcessorNumber The handle number of AP.
1658 @param[in] EnableAP Specifies the new state for the processor for
1659 enabled, FALSE for disabled.
1660 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1661 the new health status of the AP.
1663 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1664 @retval others Failed to Enable/Disable AP.
1668 EnableDisableApWorker (
1669 IN UINTN ProcessorNumber
,
1670 IN BOOLEAN EnableAP
,
1671 IN UINT32
*HealthFlag OPTIONAL
1674 CPU_MP_DATA
*CpuMpData
;
1677 CpuMpData
= GetCpuMpData ();
1680 // Check whether caller processor is BSP
1682 MpInitLibWhoAmI (&CallerNumber
);
1683 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1684 return EFI_DEVICE_ERROR
;
1687 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1688 return EFI_INVALID_PARAMETER
;
1691 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1692 return EFI_NOT_FOUND
;
1696 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1698 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1701 if (HealthFlag
!= NULL
) {
1702 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1703 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1710 This return the handle number for the calling processor. This service may be
1711 called from the BSP and APs.
1713 @param[out] ProcessorNumber Pointer to the handle number of AP.
1714 The range is from 0 to the total number of
1715 logical processors minus 1. The total number of
1716 logical processors can be retrieved by
1717 MpInitLibGetNumberOfProcessors().
1719 @retval EFI_SUCCESS The current processor handle number was returned
1721 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1722 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1728 OUT UINTN
*ProcessorNumber
1731 CPU_MP_DATA
*CpuMpData
;
1733 if (ProcessorNumber
== NULL
) {
1734 return EFI_INVALID_PARAMETER
;
1737 CpuMpData
= GetCpuMpData ();
1739 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1743 Retrieves the number of logical processor in the platform and the number of
1744 those logical processors that are enabled on this boot. This service may only
1745 be called from the BSP.
1747 @param[out] NumberOfProcessors Pointer to the total number of logical
1748 processors in the system, including the BSP
1750 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1751 processors that exist in system, including
1754 @retval EFI_SUCCESS The number of logical processors and enabled
1755 logical processors was retrieved.
1756 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1757 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1759 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1764 MpInitLibGetNumberOfProcessors (
1765 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1766 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1769 CPU_MP_DATA
*CpuMpData
;
1771 UINTN ProcessorNumber
;
1772 UINTN EnabledProcessorNumber
;
1775 CpuMpData
= GetCpuMpData ();
1777 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1778 return EFI_INVALID_PARAMETER
;
1782 // Check whether caller processor is BSP
1784 MpInitLibWhoAmI (&CallerNumber
);
1785 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1786 return EFI_DEVICE_ERROR
;
1789 ProcessorNumber
= CpuMpData
->CpuCount
;
1790 EnabledProcessorNumber
= 0;
1791 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1792 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1793 EnabledProcessorNumber
++;
1797 if (NumberOfProcessors
!= NULL
) {
1798 *NumberOfProcessors
= ProcessorNumber
;
1800 if (NumberOfEnabledProcessors
!= NULL
) {
1801 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1809 Worker function to execute a caller provided function on all enabled APs.
1811 @param[in] Procedure A pointer to the function to be run on
1812 enabled APs of the system.
1813 @param[in] SingleThread If TRUE, then all the enabled APs execute
1814 the function specified by Procedure one by
1815 one, in ascending order of processor handle
1816 number. If FALSE, then all the enabled APs
1817 execute the function specified by Procedure
1819 @param[in] WaitEvent The event created by the caller with CreateEvent()
1821 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1822 APs to return from Procedure, either for
1823 blocking or non-blocking mode.
1824 @param[in] ProcedureArgument The parameter passed into Procedure for
1826 @param[out] FailedCpuList If all APs finish successfully, then its
1827 content is set to NULL. If not all APs
1828 finish before timeout expires, then its
1829 content is set to address of the buffer
1830 holding handle numbers of the failed APs.
1832 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1833 the timeout expired.
1834 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1836 @retval others Failed to Startup all APs.
1840 StartupAllAPsWorker (
1841 IN EFI_AP_PROCEDURE Procedure
,
1842 IN BOOLEAN SingleThread
,
1843 IN EFI_EVENT WaitEvent OPTIONAL
,
1844 IN UINTN TimeoutInMicroseconds
,
1845 IN VOID
*ProcedureArgument OPTIONAL
,
1846 OUT UINTN
**FailedCpuList OPTIONAL
1850 CPU_MP_DATA
*CpuMpData
;
1851 UINTN ProcessorCount
;
1852 UINTN ProcessorNumber
;
1854 CPU_AP_DATA
*CpuData
;
1855 BOOLEAN HasEnabledAp
;
1858 CpuMpData
= GetCpuMpData ();
1860 if (FailedCpuList
!= NULL
) {
1861 *FailedCpuList
= NULL
;
1864 if (CpuMpData
->CpuCount
== 1) {
1865 return EFI_NOT_STARTED
;
1868 if (Procedure
== NULL
) {
1869 return EFI_INVALID_PARAMETER
;
1873 // Check whether caller processor is BSP
1875 MpInitLibWhoAmI (&CallerNumber
);
1876 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1877 return EFI_DEVICE_ERROR
;
1883 CheckAndUpdateApsStatus ();
1885 ProcessorCount
= CpuMpData
->CpuCount
;
1886 HasEnabledAp
= FALSE
;
1888 // Check whether all enabled APs are idle.
1889 // If any enabled AP is not idle, return EFI_NOT_READY.
1891 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1892 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1893 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1894 ApState
= GetApState (CpuData
);
1895 if (ApState
!= CpuStateDisabled
) {
1896 HasEnabledAp
= TRUE
;
1897 if (ApState
!= CpuStateIdle
) {
1899 // If any enabled APs are busy, return EFI_NOT_READY.
1901 return EFI_NOT_READY
;
1907 if (!HasEnabledAp
) {
1909 // If no enabled AP exists, return EFI_NOT_STARTED.
1911 return EFI_NOT_STARTED
;
1914 CpuMpData
->StartCount
= 0;
1915 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1916 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1917 CpuData
->Waiting
= FALSE
;
1918 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1919 if (CpuData
->State
== CpuStateIdle
) {
1921 // Mark this processor as responsible for current calling.
1923 CpuData
->Waiting
= TRUE
;
1924 CpuMpData
->StartCount
++;
1929 CpuMpData
->Procedure
= Procedure
;
1930 CpuMpData
->ProcArguments
= ProcedureArgument
;
1931 CpuMpData
->SingleThread
= SingleThread
;
1932 CpuMpData
->FinishedCount
= 0;
1933 CpuMpData
->RunningCount
= 0;
1934 CpuMpData
->FailedCpuList
= FailedCpuList
;
1935 CpuMpData
->ExpectedTime
= CalculateTimeout (
1936 TimeoutInMicroseconds
,
1937 &CpuMpData
->CurrentTime
1939 CpuMpData
->TotalTime
= 0;
1940 CpuMpData
->WaitEvent
= WaitEvent
;
1942 if (!SingleThread
) {
1943 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1945 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1946 if (ProcessorNumber
== CallerNumber
) {
1949 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1950 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1956 Status
= EFI_SUCCESS
;
1957 if (WaitEvent
== NULL
) {
1959 Status
= CheckAllAPs ();
1960 } while (Status
== EFI_NOT_READY
);
1967 Worker function to let the caller get one enabled AP to execute a caller-provided
1970 @param[in] Procedure A pointer to the function to be run on
1971 enabled APs of the system.
1972 @param[in] ProcessorNumber The handle number of the AP.
1973 @param[in] WaitEvent The event created by the caller with CreateEvent()
1975 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1976 APs to return from Procedure, either for
1977 blocking or non-blocking mode.
1978 @param[in] ProcedureArgument The parameter passed into Procedure for
1980 @param[out] Finished If AP returns from Procedure before the
1981 timeout expires, its content is set to TRUE.
1982 Otherwise, the value is set to FALSE.
1984 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1985 the timeout expires.
1986 @retval others Failed to Startup AP.
1990 StartupThisAPWorker (
1991 IN EFI_AP_PROCEDURE Procedure
,
1992 IN UINTN ProcessorNumber
,
1993 IN EFI_EVENT WaitEvent OPTIONAL
,
1994 IN UINTN TimeoutInMicroseconds
,
1995 IN VOID
*ProcedureArgument OPTIONAL
,
1996 OUT BOOLEAN
*Finished OPTIONAL
2000 CPU_MP_DATA
*CpuMpData
;
2001 CPU_AP_DATA
*CpuData
;
2004 CpuMpData
= GetCpuMpData ();
2006 if (Finished
!= NULL
) {
2011 // Check whether caller processor is BSP
2013 MpInitLibWhoAmI (&CallerNumber
);
2014 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2015 return EFI_DEVICE_ERROR
;
2019 // Check whether processor with the handle specified by ProcessorNumber exists
2021 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2022 return EFI_NOT_FOUND
;
2026 // Check whether specified processor is BSP
2028 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2029 return EFI_INVALID_PARAMETER
;
2033 // Check parameter Procedure
2035 if (Procedure
== NULL
) {
2036 return EFI_INVALID_PARAMETER
;
2042 CheckAndUpdateApsStatus ();
2045 // Check whether specified AP is disabled
2047 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2048 return EFI_INVALID_PARAMETER
;
2052 // If WaitEvent is not NULL, execute in non-blocking mode.
2053 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2054 // CheckAPsStatus() will check completion and timeout periodically.
2056 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2057 CpuData
->WaitEvent
= WaitEvent
;
2058 CpuData
->Finished
= Finished
;
2059 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2060 CpuData
->TotalTime
= 0;
2062 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2065 // If WaitEvent is NULL, execute in blocking mode.
2066 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2068 Status
= EFI_SUCCESS
;
2069 if (WaitEvent
== NULL
) {
2071 Status
= CheckThisAP (ProcessorNumber
);
2072 } while (Status
== EFI_NOT_READY
);
2079 Get pointer to CPU MP Data structure from GUIDed HOB.
2081 @return The pointer to CPU MP Data structure.
2084 GetCpuMpDataFromGuidedHob (
2088 EFI_HOB_GUID_TYPE
*GuidHob
;
2090 CPU_MP_DATA
*CpuMpData
;
2093 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2094 if (GuidHob
!= NULL
) {
2095 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2096 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2102 Get available system memory below 1MB by specified size.
2104 @param[in] CpuMpData The pointer to CPU MP Data structure.
2107 BackupAndPrepareWakeupBuffer(
2108 IN CPU_MP_DATA
*CpuMpData
2112 (VOID
*) CpuMpData
->BackupBuffer
,
2113 (VOID
*) CpuMpData
->WakeupBuffer
,
2114 CpuMpData
->BackupBufferSize
2117 (VOID
*) CpuMpData
->WakeupBuffer
,
2118 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
2119 CpuMpData
->AddressMap
.RendezvousFunnelSize
2124 Restore wakeup buffer data.
2126 @param[in] CpuMpData The pointer to CPU MP Data structure.
2129 RestoreWakeupBuffer(
2130 IN CPU_MP_DATA
*CpuMpData
2134 (VOID
*) CpuMpData
->WakeupBuffer
,
2135 (VOID
*) CpuMpData
->BackupBuffer
,
2136 CpuMpData
->BackupBufferSize