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 // Sync BSP's MTRR table to AP
387 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
389 // Load microcode on AP
391 MicrocodeDetect (CpuMpData
);
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 if (OldCpuMpData
== NULL
) {
1369 if (MaxLogicalProcessorNumber
> 1) {
1371 // Wakeup all APs and calculate the processor count in system
1373 CollectProcessorCount (CpuMpData
);
1377 // APs have been wakeup before, just get the CPU Information
1380 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1381 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1382 CpuMpData
->InitFlag
= ApInitReconfig
;
1383 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1384 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1385 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1386 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1387 if (CpuInfoInHob
[Index
].InitialApicId
>= 255) {
1388 CpuMpData
->X2ApicEnable
= TRUE
;
1390 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1391 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1393 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1394 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1395 sizeof (CPU_VOLATILE_REGISTERS
)
1398 if (MaxLogicalProcessorNumber
> 1) {
1400 // Wakeup APs to do some AP initialize sync
1402 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1404 // Wait for all APs finished initialization
1406 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1409 CpuMpData
->InitFlag
= ApInitDone
;
1410 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1411 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1417 // Initialize global data for MP support
1419 InitMpGlobalData (CpuMpData
);
1425 Gets detailed MP-related information on the requested processor at the
1426 instant this call is made. This service may only be called from the BSP.
1428 @param[in] ProcessorNumber The handle number of processor.
1429 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1430 the requested processor is deposited.
1431 @param[out] HealthData Return processor health data.
1433 @retval EFI_SUCCESS Processor information was returned.
1434 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1435 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1436 @retval EFI_NOT_FOUND The processor with the handle specified by
1437 ProcessorNumber does not exist in the platform.
1438 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1443 MpInitLibGetProcessorInfo (
1444 IN UINTN ProcessorNumber
,
1445 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1446 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1449 CPU_MP_DATA
*CpuMpData
;
1451 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1453 CpuMpData
= GetCpuMpData ();
1454 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1457 // Check whether caller processor is BSP
1459 MpInitLibWhoAmI (&CallerNumber
);
1460 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1461 return EFI_DEVICE_ERROR
;
1464 if (ProcessorInfoBuffer
== NULL
) {
1465 return EFI_INVALID_PARAMETER
;
1468 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1469 return EFI_NOT_FOUND
;
1472 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1473 ProcessorInfoBuffer
->StatusFlag
= 0;
1474 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1475 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1477 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1478 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1480 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1481 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1483 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1487 // Get processor location information
1489 GetProcessorLocationByApicId (
1490 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1491 &ProcessorInfoBuffer
->Location
.Package
,
1492 &ProcessorInfoBuffer
->Location
.Core
,
1493 &ProcessorInfoBuffer
->Location
.Thread
1496 if (HealthData
!= NULL
) {
1497 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1504 Worker function to switch the requested AP to be the BSP from that point onward.
1506 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1507 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1508 enabled AP. Otherwise, it will be disabled.
1510 @retval EFI_SUCCESS BSP successfully switched.
1511 @retval others Failed to switch BSP.
1516 IN UINTN ProcessorNumber
,
1517 IN BOOLEAN EnableOldBSP
1520 CPU_MP_DATA
*CpuMpData
;
1523 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1524 BOOLEAN OldInterruptState
;
1525 BOOLEAN OldTimerInterruptState
;
1528 // Save and Disable Local APIC timer interrupt
1530 OldTimerInterruptState
= GetApicTimerInterruptState ();
1531 DisableApicTimerInterrupt ();
1533 // Before send both BSP and AP to a procedure to exchange their roles,
1534 // interrupt must be disabled. This is because during the exchange role
1535 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1536 // be corrupted, since interrupt return address will be pushed to stack
1539 OldInterruptState
= SaveAndDisableInterrupts ();
1542 // Mask LINT0 & LINT1 for the old BSP
1544 DisableLvtInterrupts ();
1546 CpuMpData
= GetCpuMpData ();
1549 // Check whether caller processor is BSP
1551 MpInitLibWhoAmI (&CallerNumber
);
1552 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1556 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1557 return EFI_NOT_FOUND
;
1561 // Check whether specified AP is disabled
1563 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1564 if (State
== CpuStateDisabled
) {
1565 return EFI_INVALID_PARAMETER
;
1569 // Check whether ProcessorNumber specifies the current BSP
1571 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1572 return EFI_INVALID_PARAMETER
;
1576 // Check whether specified AP is busy
1578 if (State
== CpuStateBusy
) {
1579 return EFI_NOT_READY
;
1582 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1583 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1584 CpuMpData
->SwitchBspFlag
= TRUE
;
1585 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1588 // Clear the BSP bit of MSR_IA32_APIC_BASE
1590 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1591 ApicBaseMsr
.Bits
.BSP
= 0;
1592 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1595 // Need to wakeUp AP (future BSP).
1597 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1599 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1602 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1604 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1605 ApicBaseMsr
.Bits
.BSP
= 1;
1606 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1609 // Wait for old BSP finished AP task
1611 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1615 CpuMpData
->SwitchBspFlag
= FALSE
;
1617 // Set old BSP enable state
1619 if (!EnableOldBSP
) {
1620 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1622 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1625 // Save new BSP number
1627 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1630 // Restore interrupt state.
1632 SetInterruptState (OldInterruptState
);
1634 if (OldTimerInterruptState
) {
1635 EnableApicTimerInterrupt ();
1642 Worker function to let the caller enable or disable an AP from this point onward.
1643 This service may only be called from the BSP.
1645 @param[in] ProcessorNumber The handle number of AP.
1646 @param[in] EnableAP Specifies the new state for the processor for
1647 enabled, FALSE for disabled.
1648 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1649 the new health status of the AP.
1651 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1652 @retval others Failed to Enable/Disable AP.
1656 EnableDisableApWorker (
1657 IN UINTN ProcessorNumber
,
1658 IN BOOLEAN EnableAP
,
1659 IN UINT32
*HealthFlag OPTIONAL
1662 CPU_MP_DATA
*CpuMpData
;
1665 CpuMpData
= GetCpuMpData ();
1668 // Check whether caller processor is BSP
1670 MpInitLibWhoAmI (&CallerNumber
);
1671 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1672 return EFI_DEVICE_ERROR
;
1675 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1676 return EFI_INVALID_PARAMETER
;
1679 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1680 return EFI_NOT_FOUND
;
1684 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1686 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1689 if (HealthFlag
!= NULL
) {
1690 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1691 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1698 This return the handle number for the calling processor. This service may be
1699 called from the BSP and APs.
1701 @param[out] ProcessorNumber Pointer to the handle number of AP.
1702 The range is from 0 to the total number of
1703 logical processors minus 1. The total number of
1704 logical processors can be retrieved by
1705 MpInitLibGetNumberOfProcessors().
1707 @retval EFI_SUCCESS The current processor handle number was returned
1709 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1710 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1716 OUT UINTN
*ProcessorNumber
1719 CPU_MP_DATA
*CpuMpData
;
1721 if (ProcessorNumber
== NULL
) {
1722 return EFI_INVALID_PARAMETER
;
1725 CpuMpData
= GetCpuMpData ();
1727 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1731 Retrieves the number of logical processor in the platform and the number of
1732 those logical processors that are enabled on this boot. This service may only
1733 be called from the BSP.
1735 @param[out] NumberOfProcessors Pointer to the total number of logical
1736 processors in the system, including the BSP
1738 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1739 processors that exist in system, including
1742 @retval EFI_SUCCESS The number of logical processors and enabled
1743 logical processors was retrieved.
1744 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1745 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1747 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1752 MpInitLibGetNumberOfProcessors (
1753 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1754 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1757 CPU_MP_DATA
*CpuMpData
;
1759 UINTN ProcessorNumber
;
1760 UINTN EnabledProcessorNumber
;
1763 CpuMpData
= GetCpuMpData ();
1765 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1766 return EFI_INVALID_PARAMETER
;
1770 // Check whether caller processor is BSP
1772 MpInitLibWhoAmI (&CallerNumber
);
1773 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1774 return EFI_DEVICE_ERROR
;
1777 ProcessorNumber
= CpuMpData
->CpuCount
;
1778 EnabledProcessorNumber
= 0;
1779 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1780 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1781 EnabledProcessorNumber
++;
1785 if (NumberOfProcessors
!= NULL
) {
1786 *NumberOfProcessors
= ProcessorNumber
;
1788 if (NumberOfEnabledProcessors
!= NULL
) {
1789 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1797 Worker function to execute a caller provided function on all enabled APs.
1799 @param[in] Procedure A pointer to the function to be run on
1800 enabled APs of the system.
1801 @param[in] SingleThread If TRUE, then all the enabled APs execute
1802 the function specified by Procedure one by
1803 one, in ascending order of processor handle
1804 number. If FALSE, then all the enabled APs
1805 execute the function specified by Procedure
1807 @param[in] WaitEvent The event created by the caller with CreateEvent()
1809 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1810 APs to return from Procedure, either for
1811 blocking or non-blocking mode.
1812 @param[in] ProcedureArgument The parameter passed into Procedure for
1814 @param[out] FailedCpuList If all APs finish successfully, then its
1815 content is set to NULL. If not all APs
1816 finish before timeout expires, then its
1817 content is set to address of the buffer
1818 holding handle numbers of the failed APs.
1820 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1821 the timeout expired.
1822 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1824 @retval others Failed to Startup all APs.
1828 StartupAllAPsWorker (
1829 IN EFI_AP_PROCEDURE Procedure
,
1830 IN BOOLEAN SingleThread
,
1831 IN EFI_EVENT WaitEvent OPTIONAL
,
1832 IN UINTN TimeoutInMicroseconds
,
1833 IN VOID
*ProcedureArgument OPTIONAL
,
1834 OUT UINTN
**FailedCpuList OPTIONAL
1838 CPU_MP_DATA
*CpuMpData
;
1839 UINTN ProcessorCount
;
1840 UINTN ProcessorNumber
;
1842 CPU_AP_DATA
*CpuData
;
1843 BOOLEAN HasEnabledAp
;
1846 CpuMpData
= GetCpuMpData ();
1848 if (FailedCpuList
!= NULL
) {
1849 *FailedCpuList
= NULL
;
1852 if (CpuMpData
->CpuCount
== 1) {
1853 return EFI_NOT_STARTED
;
1856 if (Procedure
== NULL
) {
1857 return EFI_INVALID_PARAMETER
;
1861 // Check whether caller processor is BSP
1863 MpInitLibWhoAmI (&CallerNumber
);
1864 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1865 return EFI_DEVICE_ERROR
;
1871 CheckAndUpdateApsStatus ();
1873 ProcessorCount
= CpuMpData
->CpuCount
;
1874 HasEnabledAp
= FALSE
;
1876 // Check whether all enabled APs are idle.
1877 // If any enabled AP is not idle, return EFI_NOT_READY.
1879 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1880 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1881 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1882 ApState
= GetApState (CpuData
);
1883 if (ApState
!= CpuStateDisabled
) {
1884 HasEnabledAp
= TRUE
;
1885 if (ApState
!= CpuStateIdle
) {
1887 // If any enabled APs are busy, return EFI_NOT_READY.
1889 return EFI_NOT_READY
;
1895 if (!HasEnabledAp
) {
1897 // If no enabled AP exists, return EFI_NOT_STARTED.
1899 return EFI_NOT_STARTED
;
1902 CpuMpData
->StartCount
= 0;
1903 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1904 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1905 CpuData
->Waiting
= FALSE
;
1906 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1907 if (CpuData
->State
== CpuStateIdle
) {
1909 // Mark this processor as responsible for current calling.
1911 CpuData
->Waiting
= TRUE
;
1912 CpuMpData
->StartCount
++;
1917 CpuMpData
->Procedure
= Procedure
;
1918 CpuMpData
->ProcArguments
= ProcedureArgument
;
1919 CpuMpData
->SingleThread
= SingleThread
;
1920 CpuMpData
->FinishedCount
= 0;
1921 CpuMpData
->RunningCount
= 0;
1922 CpuMpData
->FailedCpuList
= FailedCpuList
;
1923 CpuMpData
->ExpectedTime
= CalculateTimeout (
1924 TimeoutInMicroseconds
,
1925 &CpuMpData
->CurrentTime
1927 CpuMpData
->TotalTime
= 0;
1928 CpuMpData
->WaitEvent
= WaitEvent
;
1930 if (!SingleThread
) {
1931 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1933 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1934 if (ProcessorNumber
== CallerNumber
) {
1937 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1938 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1944 Status
= EFI_SUCCESS
;
1945 if (WaitEvent
== NULL
) {
1947 Status
= CheckAllAPs ();
1948 } while (Status
== EFI_NOT_READY
);
1955 Worker function to let the caller get one enabled AP to execute a caller-provided
1958 @param[in] Procedure A pointer to the function to be run on
1959 enabled APs of the system.
1960 @param[in] ProcessorNumber The handle number of the AP.
1961 @param[in] WaitEvent The event created by the caller with CreateEvent()
1963 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1964 APs to return from Procedure, either for
1965 blocking or non-blocking mode.
1966 @param[in] ProcedureArgument The parameter passed into Procedure for
1968 @param[out] Finished If AP returns from Procedure before the
1969 timeout expires, its content is set to TRUE.
1970 Otherwise, the value is set to FALSE.
1972 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1973 the timeout expires.
1974 @retval others Failed to Startup AP.
1978 StartupThisAPWorker (
1979 IN EFI_AP_PROCEDURE Procedure
,
1980 IN UINTN ProcessorNumber
,
1981 IN EFI_EVENT WaitEvent OPTIONAL
,
1982 IN UINTN TimeoutInMicroseconds
,
1983 IN VOID
*ProcedureArgument OPTIONAL
,
1984 OUT BOOLEAN
*Finished OPTIONAL
1988 CPU_MP_DATA
*CpuMpData
;
1989 CPU_AP_DATA
*CpuData
;
1992 CpuMpData
= GetCpuMpData ();
1994 if (Finished
!= NULL
) {
1999 // Check whether caller processor is BSP
2001 MpInitLibWhoAmI (&CallerNumber
);
2002 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2003 return EFI_DEVICE_ERROR
;
2007 // Check whether processor with the handle specified by ProcessorNumber exists
2009 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2010 return EFI_NOT_FOUND
;
2014 // Check whether specified processor is BSP
2016 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2017 return EFI_INVALID_PARAMETER
;
2021 // Check parameter Procedure
2023 if (Procedure
== NULL
) {
2024 return EFI_INVALID_PARAMETER
;
2030 CheckAndUpdateApsStatus ();
2033 // Check whether specified AP is disabled
2035 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2036 return EFI_INVALID_PARAMETER
;
2040 // If WaitEvent is not NULL, execute in non-blocking mode.
2041 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2042 // CheckAPsStatus() will check completion and timeout periodically.
2044 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2045 CpuData
->WaitEvent
= WaitEvent
;
2046 CpuData
->Finished
= Finished
;
2047 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2048 CpuData
->TotalTime
= 0;
2050 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2053 // If WaitEvent is NULL, execute in blocking mode.
2054 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2056 Status
= EFI_SUCCESS
;
2057 if (WaitEvent
== NULL
) {
2059 Status
= CheckThisAP (ProcessorNumber
);
2060 } while (Status
== EFI_NOT_READY
);
2067 Get pointer to CPU MP Data structure from GUIDed HOB.
2069 @return The pointer to CPU MP Data structure.
2072 GetCpuMpDataFromGuidedHob (
2076 EFI_HOB_GUID_TYPE
*GuidHob
;
2078 CPU_MP_DATA
*CpuMpData
;
2081 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2082 if (GuidHob
!= NULL
) {
2083 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2084 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2090 Get available system memory below 1MB by specified size.
2092 @param[in] CpuMpData The pointer to CPU MP Data structure.
2095 BackupAndPrepareWakeupBuffer(
2096 IN CPU_MP_DATA
*CpuMpData
2100 (VOID
*) CpuMpData
->BackupBuffer
,
2101 (VOID
*) CpuMpData
->WakeupBuffer
,
2102 CpuMpData
->BackupBufferSize
2105 (VOID
*) CpuMpData
->WakeupBuffer
,
2106 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
2107 CpuMpData
->AddressMap
.RendezvousFunnelSize
2112 Restore wakeup buffer data.
2114 @param[in] CpuMpData The pointer to CPU MP Data structure.
2117 RestoreWakeupBuffer(
2118 IN CPU_MP_DATA
*CpuMpData
2122 (VOID
*) CpuMpData
->WakeupBuffer
,
2123 (VOID
*) CpuMpData
->BackupBuffer
,
2124 CpuMpData
->BackupBufferSize