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
438 // Send 1st broadcast IPI to APs to wakeup APs
440 CpuMpData
->InitFlag
= ApInitConfig
;
441 CpuMpData
->X2ApicEnable
= FALSE
;
442 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
);
443 CpuMpData
->InitFlag
= ApInitDone
;
444 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
446 // Wait for all APs finished the initialization
448 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
452 if (CpuMpData
->X2ApicEnable
) {
453 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
455 // Wakeup all APs to enable x2APIC mode
457 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
459 // Wait for all known APs finished
461 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
465 // Enable x2APIC on BSP
467 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
469 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
471 // Sort BSP/Aps by CPU APIC ID in ascending order
473 SortApicId (CpuMpData
);
475 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
477 return CpuMpData
->CpuCount
;
481 Initialize CPU AP Data when AP is wakeup at the first time.
483 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
484 @param[in] ProcessorNumber The handle number of processor
485 @param[in] BistData Processor BIST data
486 @param[in] ApTopOfStack Top of AP stack
491 IN OUT CPU_MP_DATA
*CpuMpData
,
492 IN UINTN ProcessorNumber
,
494 IN UINT64 ApTopOfStack
497 CPU_INFO_IN_HOB
*CpuInfoInHob
;
499 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
500 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
501 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
502 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
503 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
505 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
506 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
507 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
509 // Set x2APIC mode if there are any logical processor reporting
510 // an Initial APIC ID of 255 or greater.
512 AcquireSpinLock(&CpuMpData
->MpLock
);
513 CpuMpData
->X2ApicEnable
= TRUE
;
514 ReleaseSpinLock(&CpuMpData
->MpLock
);
517 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
518 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
522 This function will be called from AP reset code if BSP uses WakeUpAP.
524 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
525 @param[in] NumApsExecuting Number of current executing AP
530 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
531 IN UINTN NumApsExecuting
534 CPU_MP_DATA
*CpuMpData
;
535 UINTN ProcessorNumber
;
536 EFI_AP_PROCEDURE Procedure
;
539 volatile UINT32
*ApStartupSignalBuffer
;
540 CPU_INFO_IN_HOB
*CpuInfoInHob
;
544 // AP finished assembly code and begin to execute C code
546 CpuMpData
= ExchangeInfo
->CpuMpData
;
549 // AP's local APIC settings will be lost after received INIT IPI
550 // We need to re-initialize them at here
552 ProgramVirtualWireMode ();
553 SyncLocalApicTimerSetting (CpuMpData
);
556 if (CpuMpData
->InitFlag
== ApInitConfig
) {
560 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
561 ProcessorNumber
= NumApsExecuting
;
563 // This is first time AP wakeup, get BIST information from AP stack
565 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
566 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
568 // Do some AP initialize sync
570 ApInitializeSync (CpuMpData
);
572 // Sync BSP's Control registers to APs
574 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
575 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
576 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
579 // Execute AP function if AP is ready
581 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
583 // Clear AP start-up signal when AP waken up
585 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
586 InterlockedCompareExchange32 (
587 (UINT32
*) ApStartupSignalBuffer
,
591 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
593 // Restore AP's volatile registers saved
595 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
598 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
599 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
600 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
601 if (Procedure
!= NULL
) {
602 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
604 // Enable source debugging on AP function
608 // Invoke AP function here
610 Procedure (Parameter
);
611 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
612 if (CpuMpData
->SwitchBspFlag
) {
614 // Re-get the processor number due to BSP/AP maybe exchange in AP function
616 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
617 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
618 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
619 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
620 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
623 // Re-get the CPU APICID and Initial APICID
625 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
626 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
629 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
634 // AP finished executing C code
636 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
639 // Place AP is specified loop mode
641 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
643 // Save AP volatile registers
645 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
647 // Place AP in HLT-loop
650 DisableInterrupts ();
656 DisableInterrupts ();
657 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
659 // Place AP in MWAIT-loop
661 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
662 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
664 // Check AP start-up signal again.
665 // If AP start-up signal is not set, place AP into
666 // the specified C-state
668 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
670 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
672 // Place AP in Run-loop
680 // If AP start-up signal is written, AP is waken up
681 // otherwise place AP in loop again
683 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
691 Wait for AP wakeup and write AP start-up signal till AP is waken up.
693 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
697 IN
volatile UINT32
*ApStartupSignalBuffer
701 // If AP is waken up, StartupApSignal should be cleared.
702 // Otherwise, write StartupApSignal again till AP waken up.
704 while (InterlockedCompareExchange32 (
705 (UINT32
*) ApStartupSignalBuffer
,
714 This function will fill the exchange info structure.
716 @param[in] CpuMpData Pointer to CPU MP Data
720 FillExchangeInfoData (
721 IN CPU_MP_DATA
*CpuMpData
724 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
726 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
727 ExchangeInfo
->Lock
= 0;
728 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
729 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
730 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
731 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
733 ExchangeInfo
->CodeSegment
= AsmReadCs ();
734 ExchangeInfo
->DataSegment
= AsmReadDs ();
736 ExchangeInfo
->Cr3
= AsmReadCr3 ();
738 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
739 ExchangeInfo
->NumApsExecuting
= 0;
740 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
741 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
742 ExchangeInfo
->CpuMpData
= CpuMpData
;
744 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
747 // Get the BSP's data of GDT and IDT
749 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
750 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
754 Helper function that waits until the finished AP count reaches the specified
755 limit, or the specified timeout elapses (whichever comes first).
757 @param[in] CpuMpData Pointer to CPU MP Data.
758 @param[in] FinishedApLimit The number of finished APs to wait for.
759 @param[in] TimeLimit The number of microseconds to wait for.
762 TimedWaitForApFinish (
763 IN CPU_MP_DATA
*CpuMpData
,
764 IN UINT32 FinishedApLimit
,
769 This function will be called by BSP to wakeup AP.
771 @param[in] CpuMpData Pointer to CPU MP Data
772 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
773 FALSE: Send IPI to AP by ApicId
774 @param[in] ProcessorNumber The handle number of specified processor
775 @param[in] Procedure The function to be invoked by AP
776 @param[in] ProcedureArgument The argument to be passed into AP function
780 IN CPU_MP_DATA
*CpuMpData
,
781 IN BOOLEAN Broadcast
,
782 IN UINTN ProcessorNumber
,
783 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
784 IN VOID
*ProcedureArgument OPTIONAL
787 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
789 CPU_AP_DATA
*CpuData
;
790 BOOLEAN ResetVectorRequired
;
791 CPU_INFO_IN_HOB
*CpuInfoInHob
;
793 CpuMpData
->FinishedCount
= 0;
794 ResetVectorRequired
= FALSE
;
796 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
797 CpuMpData
->InitFlag
!= ApInitDone
) {
798 ResetVectorRequired
= TRUE
;
799 AllocateResetVector (CpuMpData
);
800 FillExchangeInfoData (CpuMpData
);
801 SaveLocalApicTimerSetting (CpuMpData
);
802 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
804 // Get AP target C-state each time when waking up AP,
805 // for it maybe updated by platform again
807 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
810 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
813 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
814 if (Index
!= CpuMpData
->BspNumber
) {
815 CpuData
= &CpuMpData
->CpuData
[Index
];
816 CpuData
->ApFunction
= (UINTN
) Procedure
;
817 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
818 SetApState (CpuData
, CpuStateReady
);
819 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
820 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
824 if (ResetVectorRequired
) {
828 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
830 if (CpuMpData
->InitFlag
== ApInitConfig
) {
832 // Wait for all potential APs waken up in one specified period
834 TimedWaitForApFinish (
836 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
837 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
841 // Wait all APs waken up if this is not the 1st broadcast of SIPI
843 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
844 CpuData
= &CpuMpData
->CpuData
[Index
];
845 if (Index
!= CpuMpData
->BspNumber
) {
846 WaitApWakeup (CpuData
->StartupApSignal
);
851 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
852 CpuData
->ApFunction
= (UINTN
) Procedure
;
853 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
854 SetApState (CpuData
, CpuStateReady
);
856 // Wakeup specified AP
858 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
859 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
860 if (ResetVectorRequired
) {
861 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
863 CpuInfoInHob
[ProcessorNumber
].ApicId
,
864 (UINT32
) ExchangeInfo
->BufferStart
868 // Wait specified AP waken up
870 WaitApWakeup (CpuData
->StartupApSignal
);
873 if (ResetVectorRequired
) {
874 FreeResetVector (CpuMpData
);
879 Calculate timeout value and return the current performance counter value.
881 Calculate the number of performance counter ticks required for a timeout.
882 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
885 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
886 @param[out] CurrentTime Returns the current value of the performance counter.
888 @return Expected time stamp counter for timeout.
889 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
895 IN UINTN TimeoutInMicroseconds
,
896 OUT UINT64
*CurrentTime
900 // Read the current value of the performance counter
902 *CurrentTime
= GetPerformanceCounter ();
905 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
908 if (TimeoutInMicroseconds
== 0) {
913 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
914 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
915 // it by 1,000,000, to get the number of ticks for the timeout value.
919 GetPerformanceCounterProperties (NULL
, NULL
),
920 TimeoutInMicroseconds
927 Checks whether timeout expires.
929 Check whether the number of elapsed performance counter ticks required for
930 a timeout condition has been reached.
931 If Timeout is zero, which means infinity, return value is always FALSE.
933 @param[in, out] PreviousTime On input, the value of the performance counter
934 when it was last read.
935 On output, the current value of the performance
937 @param[in] TotalTime The total amount of elapsed time in performance
939 @param[in] Timeout The number of performance counter ticks required
940 to reach a timeout condition.
942 @retval TRUE A timeout condition has been reached.
943 @retval FALSE A timeout condition has not been reached.
948 IN OUT UINT64
*PreviousTime
,
949 IN UINT64
*TotalTime
,
962 GetPerformanceCounterProperties (&Start
, &End
);
968 CurrentTime
= GetPerformanceCounter();
969 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
977 *PreviousTime
= CurrentTime
;
978 if (*TotalTime
> Timeout
) {
985 Helper function that waits until the finished AP count reaches the specified
986 limit, or the specified timeout elapses (whichever comes first).
988 @param[in] CpuMpData Pointer to CPU MP Data.
989 @param[in] FinishedApLimit The number of finished APs to wait for.
990 @param[in] TimeLimit The number of microseconds to wait for.
993 TimedWaitForApFinish (
994 IN CPU_MP_DATA
*CpuMpData
,
995 IN UINT32 FinishedApLimit
,
1000 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1001 // "infinity", so check for (TimeLimit == 0) explicitly.
1003 if (TimeLimit
== 0) {
1007 CpuMpData
->TotalTime
= 0;
1008 CpuMpData
->ExpectedTime
= CalculateTimeout (
1010 &CpuMpData
->CurrentTime
1012 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1014 &CpuMpData
->CurrentTime
,
1015 &CpuMpData
->TotalTime
,
1016 CpuMpData
->ExpectedTime
1021 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1024 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1027 DivU64x64Remainder (
1028 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1029 GetPerformanceCounterProperties (NULL
, NULL
),
1037 Reset an AP to Idle state.
1039 Any task being executed by the AP will be aborted and the AP
1040 will be waiting for a new task in Wait-For-SIPI state.
1042 @param[in] ProcessorNumber The handle number of processor.
1045 ResetProcessorToIdleState (
1046 IN UINTN ProcessorNumber
1049 CPU_MP_DATA
*CpuMpData
;
1051 CpuMpData
= GetCpuMpData ();
1053 CpuMpData
->InitFlag
= ApInitReconfig
;
1054 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
1055 while (CpuMpData
->FinishedCount
< 1) {
1058 CpuMpData
->InitFlag
= ApInitDone
;
1060 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1064 Searches for the next waiting AP.
1066 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1068 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1070 @retval EFI_SUCCESS The next waiting AP has been found.
1071 @retval EFI_NOT_FOUND No waiting AP exists.
1075 GetNextWaitingProcessorNumber (
1076 OUT UINTN
*NextProcessorNumber
1079 UINTN ProcessorNumber
;
1080 CPU_MP_DATA
*CpuMpData
;
1082 CpuMpData
= GetCpuMpData ();
1084 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1085 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1086 *NextProcessorNumber
= ProcessorNumber
;
1091 return EFI_NOT_FOUND
;
1094 /** Checks status of specified AP.
1096 This function checks whether the specified AP has finished the task assigned
1097 by StartupThisAP(), and whether timeout expires.
1099 @param[in] ProcessorNumber The handle number of processor.
1101 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1102 @retval EFI_TIMEOUT The timeout expires.
1103 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1107 IN UINTN ProcessorNumber
1110 CPU_MP_DATA
*CpuMpData
;
1111 CPU_AP_DATA
*CpuData
;
1113 CpuMpData
= GetCpuMpData ();
1114 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1117 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1118 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1119 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1122 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1124 if (GetApState(CpuData
) == CpuStateFinished
) {
1125 if (CpuData
->Finished
!= NULL
) {
1126 *(CpuData
->Finished
) = TRUE
;
1128 SetApState (CpuData
, CpuStateIdle
);
1132 // If timeout expires for StartupThisAP(), report timeout.
1134 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1135 if (CpuData
->Finished
!= NULL
) {
1136 *(CpuData
->Finished
) = FALSE
;
1139 // Reset failed AP to idle state
1141 ResetProcessorToIdleState (ProcessorNumber
);
1146 return EFI_NOT_READY
;
1150 Checks status of all APs.
1152 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1153 and whether timeout expires.
1155 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1156 @retval EFI_TIMEOUT The timeout expires.
1157 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1164 UINTN ProcessorNumber
;
1165 UINTN NextProcessorNumber
;
1168 CPU_MP_DATA
*CpuMpData
;
1169 CPU_AP_DATA
*CpuData
;
1171 CpuMpData
= GetCpuMpData ();
1173 NextProcessorNumber
= 0;
1176 // Go through all APs that are responsible for the StartupAllAPs().
1178 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1179 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1183 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1185 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1186 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1187 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1189 if (GetApState(CpuData
) == CpuStateFinished
) {
1190 CpuMpData
->RunningCount
++;
1191 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1192 SetApState(CpuData
, CpuStateIdle
);
1195 // If in Single Thread mode, then search for the next waiting AP for execution.
1197 if (CpuMpData
->SingleThread
) {
1198 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1200 if (!EFI_ERROR (Status
)) {
1204 (UINT32
) NextProcessorNumber
,
1205 CpuMpData
->Procedure
,
1206 CpuMpData
->ProcArguments
1214 // If all APs finish, return EFI_SUCCESS.
1216 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1221 // If timeout expires, report timeout.
1224 &CpuMpData
->CurrentTime
,
1225 &CpuMpData
->TotalTime
,
1226 CpuMpData
->ExpectedTime
)
1229 // If FailedCpuList is not NULL, record all failed APs in it.
1231 if (CpuMpData
->FailedCpuList
!= NULL
) {
1232 *CpuMpData
->FailedCpuList
=
1233 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1234 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1238 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1240 // Check whether this processor is responsible for StartupAllAPs().
1242 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1244 // Reset failed APs to idle state
1246 ResetProcessorToIdleState (ProcessorNumber
);
1247 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1248 if (CpuMpData
->FailedCpuList
!= NULL
) {
1249 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1253 if (CpuMpData
->FailedCpuList
!= NULL
) {
1254 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1258 return EFI_NOT_READY
;
1262 MP Initialize Library initialization.
1264 This service will allocate AP reset vector and wakeup all APs to do APs
1267 This service must be invoked before all other MP Initialize Library
1268 service are invoked.
1270 @retval EFI_SUCCESS MP initialization succeeds.
1271 @retval Others MP initialization fails.
1276 MpInitLibInitialize (
1280 CPU_MP_DATA
*OldCpuMpData
;
1281 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1282 UINT32 MaxLogicalProcessorNumber
;
1284 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1286 UINT32 MonitorFilterSize
;
1289 CPU_MP_DATA
*CpuMpData
;
1291 UINT8
*MonitorBuffer
;
1293 UINTN ApResetVectorSize
;
1294 UINTN BackupBufferAddr
;
1296 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1297 if (OldCpuMpData
== NULL
) {
1298 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1300 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1302 ASSERT (MaxLogicalProcessorNumber
!= 0);
1304 AsmGetAddressMap (&AddressMap
);
1305 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1306 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1307 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1309 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1310 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1311 BufferSize
+= sizeof (CPU_MP_DATA
);
1312 BufferSize
+= ApResetVectorSize
;
1313 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1314 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1315 ASSERT (MpBuffer
!= NULL
);
1316 ZeroMem (MpBuffer
, BufferSize
);
1317 Buffer
= (UINTN
) MpBuffer
;
1319 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1320 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1321 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1322 CpuMpData
->Buffer
= Buffer
;
1323 CpuMpData
->CpuApStackSize
= ApStackSize
;
1324 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1325 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1326 CpuMpData
->SaveRestoreFlag
= FALSE
;
1327 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1328 CpuMpData
->CpuCount
= 1;
1329 CpuMpData
->BspNumber
= 0;
1330 CpuMpData
->WaitEvent
= NULL
;
1331 CpuMpData
->SwitchBspFlag
= FALSE
;
1332 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1333 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1334 InitializeSpinLock(&CpuMpData
->MpLock
);
1336 // Save BSP's Control registers to APs
1338 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1340 // Set BSP basic information
1342 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
);
1344 // Save assembly code information
1346 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1348 // Finally set AP loop mode
1350 CpuMpData
->ApLoopMode
= ApLoopMode
;
1351 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1353 // Set up APs wakeup signal buffer
1355 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1356 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1357 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1360 // Load Microcode on BSP
1362 MicrocodeDetect (CpuMpData
);
1364 // Store BSP's MTRR setting
1366 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1368 // Enable the local APIC for Virtual Wire Mode.
1370 ProgramVirtualWireMode ();
1372 if (OldCpuMpData
== NULL
) {
1373 if (MaxLogicalProcessorNumber
> 1) {
1375 // Wakeup all APs and calculate the processor count in system
1377 CollectProcessorCount (CpuMpData
);
1381 // APs have been wakeup before, just get the CPU Information
1384 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1385 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1386 CpuMpData
->InitFlag
= ApInitReconfig
;
1387 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1388 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1389 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1390 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1391 if (CpuInfoInHob
[Index
].InitialApicId
>= 255) {
1392 CpuMpData
->X2ApicEnable
= TRUE
;
1394 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1395 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1397 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1398 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1399 sizeof (CPU_VOLATILE_REGISTERS
)
1402 if (MaxLogicalProcessorNumber
> 1) {
1404 // Wakeup APs to do some AP initialize sync
1406 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1408 // Wait for all APs finished initialization
1410 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1413 CpuMpData
->InitFlag
= ApInitDone
;
1414 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1415 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1421 // Initialize global data for MP support
1423 InitMpGlobalData (CpuMpData
);
1429 Gets detailed MP-related information on the requested processor at the
1430 instant this call is made. This service may only be called from the BSP.
1432 @param[in] ProcessorNumber The handle number of processor.
1433 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1434 the requested processor is deposited.
1435 @param[out] HealthData Return processor health data.
1437 @retval EFI_SUCCESS Processor information was returned.
1438 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1439 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1440 @retval EFI_NOT_FOUND The processor with the handle specified by
1441 ProcessorNumber does not exist in the platform.
1442 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1447 MpInitLibGetProcessorInfo (
1448 IN UINTN ProcessorNumber
,
1449 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1450 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1453 CPU_MP_DATA
*CpuMpData
;
1455 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1457 CpuMpData
= GetCpuMpData ();
1458 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1461 // Check whether caller processor is BSP
1463 MpInitLibWhoAmI (&CallerNumber
);
1464 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1465 return EFI_DEVICE_ERROR
;
1468 if (ProcessorInfoBuffer
== NULL
) {
1469 return EFI_INVALID_PARAMETER
;
1472 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1473 return EFI_NOT_FOUND
;
1476 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1477 ProcessorInfoBuffer
->StatusFlag
= 0;
1478 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1479 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1481 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1482 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1484 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1485 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1487 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1491 // Get processor location information
1493 GetProcessorLocationByApicId (
1494 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1495 &ProcessorInfoBuffer
->Location
.Package
,
1496 &ProcessorInfoBuffer
->Location
.Core
,
1497 &ProcessorInfoBuffer
->Location
.Thread
1500 if (HealthData
!= NULL
) {
1501 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1508 Worker function to switch the requested AP to be the BSP from that point onward.
1510 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1511 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1512 enabled AP. Otherwise, it will be disabled.
1514 @retval EFI_SUCCESS BSP successfully switched.
1515 @retval others Failed to switch BSP.
1520 IN UINTN ProcessorNumber
,
1521 IN BOOLEAN EnableOldBSP
1524 CPU_MP_DATA
*CpuMpData
;
1527 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1528 BOOLEAN OldInterruptState
;
1529 BOOLEAN OldTimerInterruptState
;
1532 // Save and Disable Local APIC timer interrupt
1534 OldTimerInterruptState
= GetApicTimerInterruptState ();
1535 DisableApicTimerInterrupt ();
1537 // Before send both BSP and AP to a procedure to exchange their roles,
1538 // interrupt must be disabled. This is because during the exchange role
1539 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1540 // be corrupted, since interrupt return address will be pushed to stack
1543 OldInterruptState
= SaveAndDisableInterrupts ();
1546 // Mask LINT0 & LINT1 for the old BSP
1548 DisableLvtInterrupts ();
1550 CpuMpData
= GetCpuMpData ();
1553 // Check whether caller processor is BSP
1555 MpInitLibWhoAmI (&CallerNumber
);
1556 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1560 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1561 return EFI_NOT_FOUND
;
1565 // Check whether specified AP is disabled
1567 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1568 if (State
== CpuStateDisabled
) {
1569 return EFI_INVALID_PARAMETER
;
1573 // Check whether ProcessorNumber specifies the current BSP
1575 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1576 return EFI_INVALID_PARAMETER
;
1580 // Check whether specified AP is busy
1582 if (State
== CpuStateBusy
) {
1583 return EFI_NOT_READY
;
1586 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1587 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1588 CpuMpData
->SwitchBspFlag
= TRUE
;
1589 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1592 // Clear the BSP bit of MSR_IA32_APIC_BASE
1594 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1595 ApicBaseMsr
.Bits
.BSP
= 0;
1596 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1599 // Need to wakeUp AP (future BSP).
1601 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1603 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1606 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1608 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1609 ApicBaseMsr
.Bits
.BSP
= 1;
1610 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1613 // Wait for old BSP finished AP task
1615 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1619 CpuMpData
->SwitchBspFlag
= FALSE
;
1621 // Set old BSP enable state
1623 if (!EnableOldBSP
) {
1624 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1626 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1629 // Save new BSP number
1631 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1634 // Restore interrupt state.
1636 SetInterruptState (OldInterruptState
);
1638 if (OldTimerInterruptState
) {
1639 EnableApicTimerInterrupt ();
1646 Worker function to let the caller enable or disable an AP from this point onward.
1647 This service may only be called from the BSP.
1649 @param[in] ProcessorNumber The handle number of AP.
1650 @param[in] EnableAP Specifies the new state for the processor for
1651 enabled, FALSE for disabled.
1652 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1653 the new health status of the AP.
1655 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1656 @retval others Failed to Enable/Disable AP.
1660 EnableDisableApWorker (
1661 IN UINTN ProcessorNumber
,
1662 IN BOOLEAN EnableAP
,
1663 IN UINT32
*HealthFlag OPTIONAL
1666 CPU_MP_DATA
*CpuMpData
;
1669 CpuMpData
= GetCpuMpData ();
1672 // Check whether caller processor is BSP
1674 MpInitLibWhoAmI (&CallerNumber
);
1675 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1676 return EFI_DEVICE_ERROR
;
1679 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1680 return EFI_INVALID_PARAMETER
;
1683 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1684 return EFI_NOT_FOUND
;
1688 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1690 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1693 if (HealthFlag
!= NULL
) {
1694 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1695 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1702 This return the handle number for the calling processor. This service may be
1703 called from the BSP and APs.
1705 @param[out] ProcessorNumber Pointer to the handle number of AP.
1706 The range is from 0 to the total number of
1707 logical processors minus 1. The total number of
1708 logical processors can be retrieved by
1709 MpInitLibGetNumberOfProcessors().
1711 @retval EFI_SUCCESS The current processor handle number was returned
1713 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1714 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1720 OUT UINTN
*ProcessorNumber
1723 CPU_MP_DATA
*CpuMpData
;
1725 if (ProcessorNumber
== NULL
) {
1726 return EFI_INVALID_PARAMETER
;
1729 CpuMpData
= GetCpuMpData ();
1731 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1735 Retrieves the number of logical processor in the platform and the number of
1736 those logical processors that are enabled on this boot. This service may only
1737 be called from the BSP.
1739 @param[out] NumberOfProcessors Pointer to the total number of logical
1740 processors in the system, including the BSP
1742 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1743 processors that exist in system, including
1746 @retval EFI_SUCCESS The number of logical processors and enabled
1747 logical processors was retrieved.
1748 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1749 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1751 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1756 MpInitLibGetNumberOfProcessors (
1757 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1758 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1761 CPU_MP_DATA
*CpuMpData
;
1763 UINTN ProcessorNumber
;
1764 UINTN EnabledProcessorNumber
;
1767 CpuMpData
= GetCpuMpData ();
1769 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1770 return EFI_INVALID_PARAMETER
;
1774 // Check whether caller processor is BSP
1776 MpInitLibWhoAmI (&CallerNumber
);
1777 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1778 return EFI_DEVICE_ERROR
;
1781 ProcessorNumber
= CpuMpData
->CpuCount
;
1782 EnabledProcessorNumber
= 0;
1783 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1784 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1785 EnabledProcessorNumber
++;
1789 if (NumberOfProcessors
!= NULL
) {
1790 *NumberOfProcessors
= ProcessorNumber
;
1792 if (NumberOfEnabledProcessors
!= NULL
) {
1793 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1801 Worker function to execute a caller provided function on all enabled APs.
1803 @param[in] Procedure A pointer to the function to be run on
1804 enabled APs of the system.
1805 @param[in] SingleThread If TRUE, then all the enabled APs execute
1806 the function specified by Procedure one by
1807 one, in ascending order of processor handle
1808 number. If FALSE, then all the enabled APs
1809 execute the function specified by Procedure
1811 @param[in] WaitEvent The event created by the caller with CreateEvent()
1813 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1814 APs to return from Procedure, either for
1815 blocking or non-blocking mode.
1816 @param[in] ProcedureArgument The parameter passed into Procedure for
1818 @param[out] FailedCpuList If all APs finish successfully, then its
1819 content is set to NULL. If not all APs
1820 finish before timeout expires, then its
1821 content is set to address of the buffer
1822 holding handle numbers of the failed APs.
1824 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1825 the timeout expired.
1826 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1828 @retval others Failed to Startup all APs.
1832 StartupAllAPsWorker (
1833 IN EFI_AP_PROCEDURE Procedure
,
1834 IN BOOLEAN SingleThread
,
1835 IN EFI_EVENT WaitEvent OPTIONAL
,
1836 IN UINTN TimeoutInMicroseconds
,
1837 IN VOID
*ProcedureArgument OPTIONAL
,
1838 OUT UINTN
**FailedCpuList OPTIONAL
1842 CPU_MP_DATA
*CpuMpData
;
1843 UINTN ProcessorCount
;
1844 UINTN ProcessorNumber
;
1846 CPU_AP_DATA
*CpuData
;
1847 BOOLEAN HasEnabledAp
;
1850 CpuMpData
= GetCpuMpData ();
1852 if (FailedCpuList
!= NULL
) {
1853 *FailedCpuList
= NULL
;
1856 if (CpuMpData
->CpuCount
== 1) {
1857 return EFI_NOT_STARTED
;
1860 if (Procedure
== NULL
) {
1861 return EFI_INVALID_PARAMETER
;
1865 // Check whether caller processor is BSP
1867 MpInitLibWhoAmI (&CallerNumber
);
1868 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1869 return EFI_DEVICE_ERROR
;
1875 CheckAndUpdateApsStatus ();
1877 ProcessorCount
= CpuMpData
->CpuCount
;
1878 HasEnabledAp
= FALSE
;
1880 // Check whether all enabled APs are idle.
1881 // If any enabled AP is not idle, return EFI_NOT_READY.
1883 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1884 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1885 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1886 ApState
= GetApState (CpuData
);
1887 if (ApState
!= CpuStateDisabled
) {
1888 HasEnabledAp
= TRUE
;
1889 if (ApState
!= CpuStateIdle
) {
1891 // If any enabled APs are busy, return EFI_NOT_READY.
1893 return EFI_NOT_READY
;
1899 if (!HasEnabledAp
) {
1901 // If no enabled AP exists, return EFI_NOT_STARTED.
1903 return EFI_NOT_STARTED
;
1906 CpuMpData
->StartCount
= 0;
1907 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1908 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1909 CpuData
->Waiting
= FALSE
;
1910 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1911 if (CpuData
->State
== CpuStateIdle
) {
1913 // Mark this processor as responsible for current calling.
1915 CpuData
->Waiting
= TRUE
;
1916 CpuMpData
->StartCount
++;
1921 CpuMpData
->Procedure
= Procedure
;
1922 CpuMpData
->ProcArguments
= ProcedureArgument
;
1923 CpuMpData
->SingleThread
= SingleThread
;
1924 CpuMpData
->FinishedCount
= 0;
1925 CpuMpData
->RunningCount
= 0;
1926 CpuMpData
->FailedCpuList
= FailedCpuList
;
1927 CpuMpData
->ExpectedTime
= CalculateTimeout (
1928 TimeoutInMicroseconds
,
1929 &CpuMpData
->CurrentTime
1931 CpuMpData
->TotalTime
= 0;
1932 CpuMpData
->WaitEvent
= WaitEvent
;
1934 if (!SingleThread
) {
1935 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1937 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1938 if (ProcessorNumber
== CallerNumber
) {
1941 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1942 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1948 Status
= EFI_SUCCESS
;
1949 if (WaitEvent
== NULL
) {
1951 Status
= CheckAllAPs ();
1952 } while (Status
== EFI_NOT_READY
);
1959 Worker function to let the caller get one enabled AP to execute a caller-provided
1962 @param[in] Procedure A pointer to the function to be run on
1963 enabled APs of the system.
1964 @param[in] ProcessorNumber The handle number of the AP.
1965 @param[in] WaitEvent The event created by the caller with CreateEvent()
1967 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1968 APs to return from Procedure, either for
1969 blocking or non-blocking mode.
1970 @param[in] ProcedureArgument The parameter passed into Procedure for
1972 @param[out] Finished If AP returns from Procedure before the
1973 timeout expires, its content is set to TRUE.
1974 Otherwise, the value is set to FALSE.
1976 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1977 the timeout expires.
1978 @retval others Failed to Startup AP.
1982 StartupThisAPWorker (
1983 IN EFI_AP_PROCEDURE Procedure
,
1984 IN UINTN ProcessorNumber
,
1985 IN EFI_EVENT WaitEvent OPTIONAL
,
1986 IN UINTN TimeoutInMicroseconds
,
1987 IN VOID
*ProcedureArgument OPTIONAL
,
1988 OUT BOOLEAN
*Finished OPTIONAL
1992 CPU_MP_DATA
*CpuMpData
;
1993 CPU_AP_DATA
*CpuData
;
1996 CpuMpData
= GetCpuMpData ();
1998 if (Finished
!= NULL
) {
2003 // Check whether caller processor is BSP
2005 MpInitLibWhoAmI (&CallerNumber
);
2006 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2007 return EFI_DEVICE_ERROR
;
2011 // Check whether processor with the handle specified by ProcessorNumber exists
2013 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2014 return EFI_NOT_FOUND
;
2018 // Check whether specified processor is BSP
2020 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2021 return EFI_INVALID_PARAMETER
;
2025 // Check parameter Procedure
2027 if (Procedure
== NULL
) {
2028 return EFI_INVALID_PARAMETER
;
2034 CheckAndUpdateApsStatus ();
2037 // Check whether specified AP is disabled
2039 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2040 return EFI_INVALID_PARAMETER
;
2044 // If WaitEvent is not NULL, execute in non-blocking mode.
2045 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2046 // CheckAPsStatus() will check completion and timeout periodically.
2048 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2049 CpuData
->WaitEvent
= WaitEvent
;
2050 CpuData
->Finished
= Finished
;
2051 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2052 CpuData
->TotalTime
= 0;
2054 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2057 // If WaitEvent is NULL, execute in blocking mode.
2058 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2060 Status
= EFI_SUCCESS
;
2061 if (WaitEvent
== NULL
) {
2063 Status
= CheckThisAP (ProcessorNumber
);
2064 } while (Status
== EFI_NOT_READY
);
2071 Get pointer to CPU MP Data structure from GUIDed HOB.
2073 @return The pointer to CPU MP Data structure.
2076 GetCpuMpDataFromGuidedHob (
2080 EFI_HOB_GUID_TYPE
*GuidHob
;
2082 CPU_MP_DATA
*CpuMpData
;
2085 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2086 if (GuidHob
!= NULL
) {
2087 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2088 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2094 Get available system memory below 1MB by specified size.
2096 @param[in] CpuMpData The pointer to CPU MP Data structure.
2099 BackupAndPrepareWakeupBuffer(
2100 IN CPU_MP_DATA
*CpuMpData
2104 (VOID
*) CpuMpData
->BackupBuffer
,
2105 (VOID
*) CpuMpData
->WakeupBuffer
,
2106 CpuMpData
->BackupBufferSize
2109 (VOID
*) CpuMpData
->WakeupBuffer
,
2110 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
2111 CpuMpData
->AddressMap
.RendezvousFunnelSize
2116 Restore wakeup buffer data.
2118 @param[in] CpuMpData The pointer to CPU MP Data structure.
2121 RestoreWakeupBuffer(
2122 IN CPU_MP_DATA
*CpuMpData
2126 (VOID
*) CpuMpData
->WakeupBuffer
,
2127 (VOID
*) CpuMpData
->BackupBuffer
,
2128 CpuMpData
->BackupBufferSize