2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 EFI_GUID mCpuInitMpLibHobGuid
= CPU_INIT_MP_LIB_HOB_GUID
;
20 The function will check if BSP Execute Disable is enabled.
22 DxeIpl may have enabled Execute Disable for BSP, APs need to
23 get the status and sync up the settings.
24 If BSP's CR0.Paging is not set, BSP execute Disble feature is
27 @retval TRUE BSP Execute Disable is enabled.
28 @retval FALSE BSP Execute Disable is not enabled.
31 IsBspExecuteDisableEnabled (
36 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
37 MSR_IA32_EFER_REGISTER EferMsr
;
42 Cr0
.UintN
= AsmReadCr0 ();
43 if (Cr0
.Bits
.PG
!= 0) {
45 // If CR0 Paging bit is set
47 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
48 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
49 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
52 // Bit 20: Execute Disable Bit available.
54 if (Edx
.Bits
.NX
!= 0) {
55 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
58 // Bit 11: Execute Disable Bit enable.
60 if (EferMsr
.Bits
.NXE
!= 0) {
71 Worker function for SwitchBSP().
73 Worker function for SwitchBSP(), assigned to the AP which is intended
76 @param[in] Buffer Pointer to CPU MP Data
84 CPU_MP_DATA
*DataInHob
;
86 DataInHob
= (CPU_MP_DATA
*) Buffer
;
87 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
91 Get the Application Processors state.
93 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
99 IN CPU_AP_DATA
*CpuData
102 return CpuData
->State
;
106 Set the Application Processors state.
108 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
109 @param[in] State The AP status
113 IN CPU_AP_DATA
*CpuData
,
117 AcquireSpinLock (&CpuData
->ApLock
);
118 CpuData
->State
= State
;
119 ReleaseSpinLock (&CpuData
->ApLock
);
123 Save BSP's local APIC timer setting.
125 @param[in] CpuMpData Pointer to CPU MP Data
128 SaveLocalApicTimerSetting (
129 IN CPU_MP_DATA
*CpuMpData
133 // Record the current local APIC timer setting of BSP
136 &CpuMpData
->DivideValue
,
137 &CpuMpData
->PeriodicMode
,
140 CpuMpData
->CurrentTimerCount
= GetApicTimerCurrentCount ();
141 CpuMpData
->TimerInterruptState
= GetApicTimerInterruptState ();
145 Sync local APIC timer setting from BSP to AP.
147 @param[in] CpuMpData Pointer to CPU MP Data
150 SyncLocalApicTimerSetting (
151 IN CPU_MP_DATA
*CpuMpData
155 // Sync local APIC timer setting from BSP to AP
157 InitializeApicTimer (
158 CpuMpData
->DivideValue
,
159 CpuMpData
->CurrentTimerCount
,
160 CpuMpData
->PeriodicMode
,
164 // Disable AP's local APIC timer interrupt
166 DisableApicTimerInterrupt ();
170 Save the volatile registers required to be restored following INIT IPI.
172 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
175 SaveVolatileRegisters (
176 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
179 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
181 VolatileRegisters
->Cr0
= AsmReadCr0 ();
182 VolatileRegisters
->Cr3
= AsmReadCr3 ();
183 VolatileRegisters
->Cr4
= AsmReadCr4 ();
185 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
186 if (VersionInfoEdx
.Bits
.DE
!= 0) {
188 // If processor supports Debugging Extensions feature
189 // by CPUID.[EAX=01H]:EDX.BIT2
191 VolatileRegisters
->Dr0
= AsmReadDr0 ();
192 VolatileRegisters
->Dr1
= AsmReadDr1 ();
193 VolatileRegisters
->Dr2
= AsmReadDr2 ();
194 VolatileRegisters
->Dr3
= AsmReadDr3 ();
195 VolatileRegisters
->Dr6
= AsmReadDr6 ();
196 VolatileRegisters
->Dr7
= AsmReadDr7 ();
201 Restore the volatile registers following INIT IPI.
203 @param[in] VolatileRegisters Pointer to volatile resisters
204 @param[in] IsRestoreDr TRUE: Restore DRx if supported
205 FALSE: Do not restore DRx
208 RestoreVolatileRegisters (
209 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
210 IN BOOLEAN IsRestoreDr
213 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
215 AsmWriteCr0 (VolatileRegisters
->Cr0
);
216 AsmWriteCr3 (VolatileRegisters
->Cr3
);
217 AsmWriteCr4 (VolatileRegisters
->Cr4
);
220 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
221 if (VersionInfoEdx
.Bits
.DE
!= 0) {
223 // If processor supports Debugging Extensions feature
224 // by CPUID.[EAX=01H]:EDX.BIT2
226 AsmWriteDr0 (VolatileRegisters
->Dr0
);
227 AsmWriteDr1 (VolatileRegisters
->Dr1
);
228 AsmWriteDr2 (VolatileRegisters
->Dr2
);
229 AsmWriteDr3 (VolatileRegisters
->Dr3
);
230 AsmWriteDr6 (VolatileRegisters
->Dr6
);
231 AsmWriteDr7 (VolatileRegisters
->Dr7
);
237 Detect whether Mwait-monitor feature is supported.
239 @retval TRUE Mwait-monitor feature is supported.
240 @retval FALSE Mwait-monitor feature is not supported.
247 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
249 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
250 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
256 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
258 @return The AP loop mode.
262 OUT UINT32
*MonitorFilterSize
266 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
268 ASSERT (MonitorFilterSize
!= NULL
);
270 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
271 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
272 if (ApLoopMode
== ApInMwaitLoop
) {
273 if (!IsMwaitSupport ()) {
275 // If processor does not support MONITOR/MWAIT feature,
276 // force AP in Hlt-loop mode
278 ApLoopMode
= ApInHltLoop
;
282 if (ApLoopMode
!= ApInMwaitLoop
) {
283 *MonitorFilterSize
= sizeof (UINT32
);
286 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
287 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
289 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
290 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
297 Sort the APIC ID of all processors.
299 This function sorts the APIC ID of all processors so that processor number is
300 assigned in the ascending order of APIC ID which eases MP debugging.
302 @param[in] CpuMpData Pointer to PEI CPU MP Data
306 IN CPU_MP_DATA
*CpuMpData
313 CPU_INFO_IN_HOB CpuInfo
;
315 CPU_INFO_IN_HOB
*CpuInfoInHob
;
317 ApCount
= CpuMpData
->CpuCount
- 1;
318 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
320 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
323 // Sort key is the hardware default APIC ID
325 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
326 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
327 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
329 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
332 if (Index3
!= Index1
) {
333 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
335 &CpuInfoInHob
[Index3
],
336 &CpuInfoInHob
[Index1
],
337 sizeof (CPU_INFO_IN_HOB
)
339 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
344 // Get the processor number for the BSP
346 ApicId
= GetInitialApicId ();
347 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
348 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
349 CpuMpData
->BspNumber
= (UINT32
) Index1
;
357 Enable x2APIC mode on APs.
359 @param[in, out] Buffer Pointer to private data buffer.
367 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
373 @param[in, out] Buffer Pointer to private data buffer.
381 CPU_MP_DATA
*CpuMpData
;
383 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
385 // Load microcode on AP
387 MicrocodeDetect (CpuMpData
);
389 // Sync BSP's MTRR table to AP
391 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
395 Find the current Processor number by APIC ID.
397 @param[in] CpuMpData Pointer to PEI CPU MP Data
398 @param[out] ProcessorNumber Return the pocessor number found
400 @retval EFI_SUCCESS ProcessorNumber is found and returned.
401 @retval EFI_NOT_FOUND ProcessorNumber is not found.
405 IN CPU_MP_DATA
*CpuMpData
,
406 OUT UINTN
*ProcessorNumber
409 UINTN TotalProcessorNumber
;
411 CPU_INFO_IN_HOB
*CpuInfoInHob
;
413 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
415 TotalProcessorNumber
= CpuMpData
->CpuCount
;
416 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
417 if (CpuInfoInHob
[Index
].ApicId
== GetApicId ()) {
418 *ProcessorNumber
= Index
;
422 return EFI_NOT_FOUND
;
426 This function will get CPU count in the system.
428 @param[in] CpuMpData Pointer to PEI CPU MP Data
430 @return CPU count detected
433 CollectProcessorCount (
434 IN CPU_MP_DATA
*CpuMpData
440 // Send 1st broadcast IPI to APs to wakeup APs
442 CpuMpData
->InitFlag
= ApInitConfig
;
443 CpuMpData
->X2ApicEnable
= FALSE
;
444 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
);
445 CpuMpData
->InitFlag
= ApInitDone
;
446 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
448 // Wait for all APs finished the initialization
450 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
454 if (CpuMpData
->CpuCount
> 255) {
456 // If there are more than 255 processor found, force to enable X2APIC
458 CpuMpData
->X2ApicEnable
= TRUE
;
460 if (CpuMpData
->X2ApicEnable
) {
461 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
463 // Wakeup all APs to enable x2APIC mode
465 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
467 // Wait for all known APs finished
469 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
473 // Enable x2APIC on BSP
475 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
477 // Set BSP/Aps state to IDLE
479 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
480 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
483 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
485 // Sort BSP/Aps by CPU APIC ID in ascending order
487 SortApicId (CpuMpData
);
489 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
491 return CpuMpData
->CpuCount
;
495 Initialize CPU AP Data when AP is wakeup at the first time.
497 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
498 @param[in] ProcessorNumber The handle number of processor
499 @param[in] BistData Processor BIST data
500 @param[in] ApTopOfStack Top of AP stack
505 IN OUT CPU_MP_DATA
*CpuMpData
,
506 IN UINTN ProcessorNumber
,
508 IN UINT64 ApTopOfStack
511 CPU_INFO_IN_HOB
*CpuInfoInHob
;
513 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
514 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
515 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
516 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
517 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
519 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
520 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
521 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
523 // Set x2APIC mode if there are any logical processor reporting
524 // an Initial APIC ID of 255 or greater.
526 AcquireSpinLock(&CpuMpData
->MpLock
);
527 CpuMpData
->X2ApicEnable
= TRUE
;
528 ReleaseSpinLock(&CpuMpData
->MpLock
);
531 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
532 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
536 This function will be called from AP reset code if BSP uses WakeUpAP.
538 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
539 @param[in] ApIndex Number of current executing AP
544 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
548 CPU_MP_DATA
*CpuMpData
;
549 UINTN ProcessorNumber
;
550 EFI_AP_PROCEDURE Procedure
;
553 volatile UINT32
*ApStartupSignalBuffer
;
554 CPU_INFO_IN_HOB
*CpuInfoInHob
;
556 UINTN CurrentApicMode
;
559 // AP finished assembly code and begin to execute C code
561 CpuMpData
= ExchangeInfo
->CpuMpData
;
564 // AP's local APIC settings will be lost after received INIT IPI
565 // We need to re-initialize them at here
567 ProgramVirtualWireMode ();
568 SyncLocalApicTimerSetting (CpuMpData
);
570 CurrentApicMode
= GetApicMode ();
572 if (CpuMpData
->InitFlag
== ApInitConfig
) {
576 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
577 ProcessorNumber
= ApIndex
;
579 // This is first time AP wakeup, get BIST information from AP stack
581 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
582 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
584 // Do some AP initialize sync
586 ApInitializeSync (CpuMpData
);
588 // Sync BSP's Control registers to APs
590 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
591 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
592 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
595 // Execute AP function if AP is ready
597 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
599 // Clear AP start-up signal when AP waken up
601 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
602 InterlockedCompareExchange32 (
603 (UINT32
*) ApStartupSignalBuffer
,
607 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
609 // Restore AP's volatile registers saved
611 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
614 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
615 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
616 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
617 if (Procedure
!= NULL
) {
618 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
620 // Enable source debugging on AP function
624 // Invoke AP function here
626 Procedure (Parameter
);
627 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
628 if (CpuMpData
->SwitchBspFlag
) {
630 // Re-get the processor number due to BSP/AP maybe exchange in AP function
632 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
633 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
634 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
635 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
636 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
638 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
639 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
640 if (CurrentApicMode
!= GetApicMode ()) {
642 // If APIC mode change happened during AP function execution,
643 // we do not support APIC ID value changed.
649 // Re-get the CPU APICID and Initial APICID if they are changed
651 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
652 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
657 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
662 // AP finished executing C code
664 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
665 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
668 // Place AP is specified loop mode
670 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
672 // Save AP volatile registers
674 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
676 // Place AP in HLT-loop
679 DisableInterrupts ();
685 DisableInterrupts ();
686 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
688 // Place AP in MWAIT-loop
690 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
691 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
693 // Check AP start-up signal again.
694 // If AP start-up signal is not set, place AP into
695 // the specified C-state
697 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
699 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
701 // Place AP in Run-loop
709 // If AP start-up signal is written, AP is waken up
710 // otherwise place AP in loop again
712 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
720 Wait for AP wakeup and write AP start-up signal till AP is waken up.
722 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
726 IN
volatile UINT32
*ApStartupSignalBuffer
730 // If AP is waken up, StartupApSignal should be cleared.
731 // Otherwise, write StartupApSignal again till AP waken up.
733 while (InterlockedCompareExchange32 (
734 (UINT32
*) ApStartupSignalBuffer
,
743 This function will fill the exchange info structure.
745 @param[in] CpuMpData Pointer to CPU MP Data
749 FillExchangeInfoData (
750 IN CPU_MP_DATA
*CpuMpData
753 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
755 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
756 ExchangeInfo
->Lock
= 0;
757 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
758 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
759 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
760 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
762 ExchangeInfo
->CodeSegment
= AsmReadCs ();
763 ExchangeInfo
->DataSegment
= AsmReadDs ();
765 ExchangeInfo
->Cr3
= AsmReadCr3 ();
767 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
768 ExchangeInfo
->ApIndex
= 0;
769 ExchangeInfo
->NumApsExecuting
= 0;
770 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
771 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
772 ExchangeInfo
->CpuMpData
= CpuMpData
;
774 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
776 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
779 // Get the BSP's data of GDT and IDT
781 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
782 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
786 Helper function that waits until the finished AP count reaches the specified
787 limit, or the specified timeout elapses (whichever comes first).
789 @param[in] CpuMpData Pointer to CPU MP Data.
790 @param[in] FinishedApLimit The number of finished APs to wait for.
791 @param[in] TimeLimit The number of microseconds to wait for.
794 TimedWaitForApFinish (
795 IN CPU_MP_DATA
*CpuMpData
,
796 IN UINT32 FinishedApLimit
,
801 Get available system memory below 1MB by specified size.
803 @param[in] CpuMpData The pointer to CPU MP Data structure.
806 BackupAndPrepareWakeupBuffer(
807 IN CPU_MP_DATA
*CpuMpData
811 (VOID
*) CpuMpData
->BackupBuffer
,
812 (VOID
*) CpuMpData
->WakeupBuffer
,
813 CpuMpData
->BackupBufferSize
816 (VOID
*) CpuMpData
->WakeupBuffer
,
817 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
818 CpuMpData
->AddressMap
.RendezvousFunnelSize
823 Restore wakeup buffer data.
825 @param[in] CpuMpData The pointer to CPU MP Data structure.
829 IN CPU_MP_DATA
*CpuMpData
833 (VOID
*) CpuMpData
->WakeupBuffer
,
834 (VOID
*) CpuMpData
->BackupBuffer
,
835 CpuMpData
->BackupBufferSize
840 Allocate reset vector buffer.
842 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
845 AllocateResetVector (
846 IN OUT CPU_MP_DATA
*CpuMpData
849 UINTN ApResetVectorSize
;
851 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
852 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
853 sizeof (MP_CPU_EXCHANGE_INFO
);
855 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
856 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
857 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
859 BackupAndPrepareWakeupBuffer (CpuMpData
);
863 Free AP reset vector buffer.
865 @param[in] CpuMpData The pointer to CPU MP Data structure.
869 IN CPU_MP_DATA
*CpuMpData
872 RestoreWakeupBuffer (CpuMpData
);
876 This function will be called by BSP to wakeup AP.
878 @param[in] CpuMpData Pointer to CPU MP Data
879 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
880 FALSE: Send IPI to AP by ApicId
881 @param[in] ProcessorNumber The handle number of specified processor
882 @param[in] Procedure The function to be invoked by AP
883 @param[in] ProcedureArgument The argument to be passed into AP function
887 IN CPU_MP_DATA
*CpuMpData
,
888 IN BOOLEAN Broadcast
,
889 IN UINTN ProcessorNumber
,
890 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
891 IN VOID
*ProcedureArgument OPTIONAL
894 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
896 CPU_AP_DATA
*CpuData
;
897 BOOLEAN ResetVectorRequired
;
898 CPU_INFO_IN_HOB
*CpuInfoInHob
;
900 CpuMpData
->FinishedCount
= 0;
901 ResetVectorRequired
= FALSE
;
903 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
904 CpuMpData
->InitFlag
!= ApInitDone
) {
905 ResetVectorRequired
= TRUE
;
906 AllocateResetVector (CpuMpData
);
907 FillExchangeInfoData (CpuMpData
);
908 SaveLocalApicTimerSetting (CpuMpData
);
909 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
911 // Get AP target C-state each time when waking up AP,
912 // for it maybe updated by platform again
914 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
917 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
920 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
921 if (Index
!= CpuMpData
->BspNumber
) {
922 CpuData
= &CpuMpData
->CpuData
[Index
];
923 CpuData
->ApFunction
= (UINTN
) Procedure
;
924 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
925 SetApState (CpuData
, CpuStateReady
);
926 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
927 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
931 if (ResetVectorRequired
) {
935 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
937 if (CpuMpData
->InitFlag
== ApInitConfig
) {
939 // Here support two methods to collect AP count through adjust
940 // PcdCpuApInitTimeOutInMicroSeconds values.
942 // one way is set a value to just let the first AP to start the
943 // initialization, then through the later while loop to wait all Aps
944 // finsh the initialization.
945 // The other way is set a value to let all APs finished the initialzation.
946 // In this case, the later while loop is useless.
948 TimedWaitForApFinish (
950 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
951 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
954 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
959 // Wait all APs waken up if this is not the 1st broadcast of SIPI
961 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
962 CpuData
= &CpuMpData
->CpuData
[Index
];
963 if (Index
!= CpuMpData
->BspNumber
) {
964 WaitApWakeup (CpuData
->StartupApSignal
);
969 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
970 CpuData
->ApFunction
= (UINTN
) Procedure
;
971 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
972 SetApState (CpuData
, CpuStateReady
);
974 // Wakeup specified AP
976 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
977 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
978 if (ResetVectorRequired
) {
979 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
981 CpuInfoInHob
[ProcessorNumber
].ApicId
,
982 (UINT32
) ExchangeInfo
->BufferStart
986 // Wait specified AP waken up
988 WaitApWakeup (CpuData
->StartupApSignal
);
991 if (ResetVectorRequired
) {
992 FreeResetVector (CpuMpData
);
997 Calculate timeout value and return the current performance counter value.
999 Calculate the number of performance counter ticks required for a timeout.
1000 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1003 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1004 @param[out] CurrentTime Returns the current value of the performance counter.
1006 @return Expected time stamp counter for timeout.
1007 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1013 IN UINTN TimeoutInMicroseconds
,
1014 OUT UINT64
*CurrentTime
1017 UINT64 TimeoutInSeconds
;
1018 UINT64 TimestampCounterFreq
;
1021 // Read the current value of the performance counter
1023 *CurrentTime
= GetPerformanceCounter ();
1026 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1029 if (TimeoutInMicroseconds
== 0) {
1034 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1037 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1040 // Check the potential overflow before calculate the number of ticks for the timeout value.
1042 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1044 // Convert microseconds into seconds if direct multiplication overflows
1046 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1048 // Assertion if the final tick count exceeds MAX_UINT64
1050 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1051 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1054 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1055 // it by 1,000,000, to get the number of ticks for the timeout value.
1059 TimestampCounterFreq
,
1060 TimeoutInMicroseconds
1068 Checks whether timeout expires.
1070 Check whether the number of elapsed performance counter ticks required for
1071 a timeout condition has been reached.
1072 If Timeout is zero, which means infinity, return value is always FALSE.
1074 @param[in, out] PreviousTime On input, the value of the performance counter
1075 when it was last read.
1076 On output, the current value of the performance
1078 @param[in] TotalTime The total amount of elapsed time in performance
1080 @param[in] Timeout The number of performance counter ticks required
1081 to reach a timeout condition.
1083 @retval TRUE A timeout condition has been reached.
1084 @retval FALSE A timeout condition has not been reached.
1089 IN OUT UINT64
*PreviousTime
,
1090 IN UINT64
*TotalTime
,
1103 GetPerformanceCounterProperties (&Start
, &End
);
1104 Cycle
= End
- Start
;
1109 CurrentTime
= GetPerformanceCounter();
1110 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1117 *TotalTime
+= Delta
;
1118 *PreviousTime
= CurrentTime
;
1119 if (*TotalTime
> Timeout
) {
1126 Helper function that waits until the finished AP count reaches the specified
1127 limit, or the specified timeout elapses (whichever comes first).
1129 @param[in] CpuMpData Pointer to CPU MP Data.
1130 @param[in] FinishedApLimit The number of finished APs to wait for.
1131 @param[in] TimeLimit The number of microseconds to wait for.
1134 TimedWaitForApFinish (
1135 IN CPU_MP_DATA
*CpuMpData
,
1136 IN UINT32 FinishedApLimit
,
1141 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1142 // "infinity", so check for (TimeLimit == 0) explicitly.
1144 if (TimeLimit
== 0) {
1148 CpuMpData
->TotalTime
= 0;
1149 CpuMpData
->ExpectedTime
= CalculateTimeout (
1151 &CpuMpData
->CurrentTime
1153 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1155 &CpuMpData
->CurrentTime
,
1156 &CpuMpData
->TotalTime
,
1157 CpuMpData
->ExpectedTime
1162 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1165 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1168 DivU64x64Remainder (
1169 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1170 GetPerformanceCounterProperties (NULL
, NULL
),
1178 Reset an AP to Idle state.
1180 Any task being executed by the AP will be aborted and the AP
1181 will be waiting for a new task in Wait-For-SIPI state.
1183 @param[in] ProcessorNumber The handle number of processor.
1186 ResetProcessorToIdleState (
1187 IN UINTN ProcessorNumber
1190 CPU_MP_DATA
*CpuMpData
;
1192 CpuMpData
= GetCpuMpData ();
1194 CpuMpData
->InitFlag
= ApInitReconfig
;
1195 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
1196 while (CpuMpData
->FinishedCount
< 1) {
1199 CpuMpData
->InitFlag
= ApInitDone
;
1201 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1205 Searches for the next waiting AP.
1207 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1209 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1211 @retval EFI_SUCCESS The next waiting AP has been found.
1212 @retval EFI_NOT_FOUND No waiting AP exists.
1216 GetNextWaitingProcessorNumber (
1217 OUT UINTN
*NextProcessorNumber
1220 UINTN ProcessorNumber
;
1221 CPU_MP_DATA
*CpuMpData
;
1223 CpuMpData
= GetCpuMpData ();
1225 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1226 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1227 *NextProcessorNumber
= ProcessorNumber
;
1232 return EFI_NOT_FOUND
;
1235 /** Checks status of specified AP.
1237 This function checks whether the specified AP has finished the task assigned
1238 by StartupThisAP(), and whether timeout expires.
1240 @param[in] ProcessorNumber The handle number of processor.
1242 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1243 @retval EFI_TIMEOUT The timeout expires.
1244 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1248 IN UINTN ProcessorNumber
1251 CPU_MP_DATA
*CpuMpData
;
1252 CPU_AP_DATA
*CpuData
;
1254 CpuMpData
= GetCpuMpData ();
1255 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1258 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1259 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1260 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1263 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1265 if (GetApState(CpuData
) == CpuStateFinished
) {
1266 if (CpuData
->Finished
!= NULL
) {
1267 *(CpuData
->Finished
) = TRUE
;
1269 SetApState (CpuData
, CpuStateIdle
);
1273 // If timeout expires for StartupThisAP(), report timeout.
1275 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1276 if (CpuData
->Finished
!= NULL
) {
1277 *(CpuData
->Finished
) = FALSE
;
1280 // Reset failed AP to idle state
1282 ResetProcessorToIdleState (ProcessorNumber
);
1287 return EFI_NOT_READY
;
1291 Checks status of all APs.
1293 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1294 and whether timeout expires.
1296 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1297 @retval EFI_TIMEOUT The timeout expires.
1298 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1305 UINTN ProcessorNumber
;
1306 UINTN NextProcessorNumber
;
1309 CPU_MP_DATA
*CpuMpData
;
1310 CPU_AP_DATA
*CpuData
;
1312 CpuMpData
= GetCpuMpData ();
1314 NextProcessorNumber
= 0;
1317 // Go through all APs that are responsible for the StartupAllAPs().
1319 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1320 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1324 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1326 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1327 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1328 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1330 if (GetApState(CpuData
) == CpuStateFinished
) {
1331 CpuMpData
->RunningCount
++;
1332 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1333 SetApState(CpuData
, CpuStateIdle
);
1336 // If in Single Thread mode, then search for the next waiting AP for execution.
1338 if (CpuMpData
->SingleThread
) {
1339 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1341 if (!EFI_ERROR (Status
)) {
1345 (UINT32
) NextProcessorNumber
,
1346 CpuMpData
->Procedure
,
1347 CpuMpData
->ProcArguments
1355 // If all APs finish, return EFI_SUCCESS.
1357 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1362 // If timeout expires, report timeout.
1365 &CpuMpData
->CurrentTime
,
1366 &CpuMpData
->TotalTime
,
1367 CpuMpData
->ExpectedTime
)
1370 // If FailedCpuList is not NULL, record all failed APs in it.
1372 if (CpuMpData
->FailedCpuList
!= NULL
) {
1373 *CpuMpData
->FailedCpuList
=
1374 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1375 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1379 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1381 // Check whether this processor is responsible for StartupAllAPs().
1383 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1385 // Reset failed APs to idle state
1387 ResetProcessorToIdleState (ProcessorNumber
);
1388 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1389 if (CpuMpData
->FailedCpuList
!= NULL
) {
1390 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1394 if (CpuMpData
->FailedCpuList
!= NULL
) {
1395 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1399 return EFI_NOT_READY
;
1403 MP Initialize Library initialization.
1405 This service will allocate AP reset vector and wakeup all APs to do APs
1408 This service must be invoked before all other MP Initialize Library
1409 service are invoked.
1411 @retval EFI_SUCCESS MP initialization succeeds.
1412 @retval Others MP initialization fails.
1417 MpInitLibInitialize (
1421 CPU_MP_DATA
*OldCpuMpData
;
1422 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1423 UINT32 MaxLogicalProcessorNumber
;
1425 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1427 UINT32 MonitorFilterSize
;
1430 CPU_MP_DATA
*CpuMpData
;
1432 UINT8
*MonitorBuffer
;
1434 UINTN ApResetVectorSize
;
1435 UINTN BackupBufferAddr
;
1437 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1438 if (OldCpuMpData
== NULL
) {
1439 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1441 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1443 ASSERT (MaxLogicalProcessorNumber
!= 0);
1445 AsmGetAddressMap (&AddressMap
);
1446 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1447 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1448 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1450 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1451 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1452 BufferSize
+= sizeof (CPU_MP_DATA
);
1453 BufferSize
+= ApResetVectorSize
;
1454 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1455 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1456 ASSERT (MpBuffer
!= NULL
);
1457 ZeroMem (MpBuffer
, BufferSize
);
1458 Buffer
= (UINTN
) MpBuffer
;
1460 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1461 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1462 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1463 CpuMpData
->Buffer
= Buffer
;
1464 CpuMpData
->CpuApStackSize
= ApStackSize
;
1465 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1466 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1467 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1468 CpuMpData
->CpuCount
= 1;
1469 CpuMpData
->BspNumber
= 0;
1470 CpuMpData
->WaitEvent
= NULL
;
1471 CpuMpData
->SwitchBspFlag
= FALSE
;
1472 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1473 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1474 CpuMpData
->MicrocodePatchAddress
= PcdGet64 (PcdCpuMicrocodePatchAddress
);
1475 CpuMpData
->MicrocodePatchRegionSize
= PcdGet64 (PcdCpuMicrocodePatchRegionSize
);
1476 InitializeSpinLock(&CpuMpData
->MpLock
);
1478 // Save BSP's Control registers to APs
1480 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1482 // Set BSP basic information
1484 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
);
1486 // Save assembly code information
1488 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1490 // Finally set AP loop mode
1492 CpuMpData
->ApLoopMode
= ApLoopMode
;
1493 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1495 // Set up APs wakeup signal buffer
1497 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1498 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1499 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1502 // Load Microcode on BSP
1504 MicrocodeDetect (CpuMpData
);
1506 // Store BSP's MTRR setting
1508 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1510 // Enable the local APIC for Virtual Wire Mode.
1512 ProgramVirtualWireMode ();
1514 if (OldCpuMpData
== NULL
) {
1515 if (MaxLogicalProcessorNumber
> 1) {
1517 // Wakeup all APs and calculate the processor count in system
1519 CollectProcessorCount (CpuMpData
);
1523 // APs have been wakeup before, just get the CPU Information
1526 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1527 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1528 CpuMpData
->InitFlag
= ApInitReconfig
;
1529 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1530 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1531 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1532 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1533 if (CpuInfoInHob
[Index
].InitialApicId
>= 255 || Index
> 254) {
1534 CpuMpData
->X2ApicEnable
= TRUE
;
1536 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1537 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1539 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1540 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1541 sizeof (CPU_VOLATILE_REGISTERS
)
1544 if (MaxLogicalProcessorNumber
> 1) {
1546 // Wakeup APs to do some AP initialize sync
1548 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1550 // Wait for all APs finished initialization
1552 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1555 CpuMpData
->InitFlag
= ApInitDone
;
1556 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1557 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1563 // Initialize global data for MP support
1565 InitMpGlobalData (CpuMpData
);
1571 Gets detailed MP-related information on the requested processor at the
1572 instant this call is made. This service may only be called from the BSP.
1574 @param[in] ProcessorNumber The handle number of processor.
1575 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1576 the requested processor is deposited.
1577 @param[out] HealthData Return processor health data.
1579 @retval EFI_SUCCESS Processor information was returned.
1580 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1581 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1582 @retval EFI_NOT_FOUND The processor with the handle specified by
1583 ProcessorNumber does not exist in the platform.
1584 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1589 MpInitLibGetProcessorInfo (
1590 IN UINTN ProcessorNumber
,
1591 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1592 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1595 CPU_MP_DATA
*CpuMpData
;
1597 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1599 CpuMpData
= GetCpuMpData ();
1600 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1603 // Check whether caller processor is BSP
1605 MpInitLibWhoAmI (&CallerNumber
);
1606 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1607 return EFI_DEVICE_ERROR
;
1610 if (ProcessorInfoBuffer
== NULL
) {
1611 return EFI_INVALID_PARAMETER
;
1614 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1615 return EFI_NOT_FOUND
;
1618 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1619 ProcessorInfoBuffer
->StatusFlag
= 0;
1620 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1621 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1623 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1624 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1626 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1627 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1629 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1633 // Get processor location information
1635 GetProcessorLocationByApicId (
1636 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1637 &ProcessorInfoBuffer
->Location
.Package
,
1638 &ProcessorInfoBuffer
->Location
.Core
,
1639 &ProcessorInfoBuffer
->Location
.Thread
1642 if (HealthData
!= NULL
) {
1643 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1650 Worker function to switch the requested AP to be the BSP from that point onward.
1652 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1653 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1654 enabled AP. Otherwise, it will be disabled.
1656 @retval EFI_SUCCESS BSP successfully switched.
1657 @retval others Failed to switch BSP.
1662 IN UINTN ProcessorNumber
,
1663 IN BOOLEAN EnableOldBSP
1666 CPU_MP_DATA
*CpuMpData
;
1669 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1670 BOOLEAN OldInterruptState
;
1671 BOOLEAN OldTimerInterruptState
;
1674 // Save and Disable Local APIC timer interrupt
1676 OldTimerInterruptState
= GetApicTimerInterruptState ();
1677 DisableApicTimerInterrupt ();
1679 // Before send both BSP and AP to a procedure to exchange their roles,
1680 // interrupt must be disabled. This is because during the exchange role
1681 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1682 // be corrupted, since interrupt return address will be pushed to stack
1685 OldInterruptState
= SaveAndDisableInterrupts ();
1688 // Mask LINT0 & LINT1 for the old BSP
1690 DisableLvtInterrupts ();
1692 CpuMpData
= GetCpuMpData ();
1695 // Check whether caller processor is BSP
1697 MpInitLibWhoAmI (&CallerNumber
);
1698 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1699 return EFI_DEVICE_ERROR
;
1702 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1703 return EFI_NOT_FOUND
;
1707 // Check whether specified AP is disabled
1709 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1710 if (State
== CpuStateDisabled
) {
1711 return EFI_INVALID_PARAMETER
;
1715 // Check whether ProcessorNumber specifies the current BSP
1717 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1718 return EFI_INVALID_PARAMETER
;
1722 // Check whether specified AP is busy
1724 if (State
== CpuStateBusy
) {
1725 return EFI_NOT_READY
;
1728 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1729 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1730 CpuMpData
->SwitchBspFlag
= TRUE
;
1731 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1734 // Clear the BSP bit of MSR_IA32_APIC_BASE
1736 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1737 ApicBaseMsr
.Bits
.BSP
= 0;
1738 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1741 // Need to wakeUp AP (future BSP).
1743 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1745 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1748 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1750 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1751 ApicBaseMsr
.Bits
.BSP
= 1;
1752 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1755 // Wait for old BSP finished AP task
1757 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1761 CpuMpData
->SwitchBspFlag
= FALSE
;
1763 // Set old BSP enable state
1765 if (!EnableOldBSP
) {
1766 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1768 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1771 // Save new BSP number
1773 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1776 // Restore interrupt state.
1778 SetInterruptState (OldInterruptState
);
1780 if (OldTimerInterruptState
) {
1781 EnableApicTimerInterrupt ();
1788 Worker function to let the caller enable or disable an AP from this point onward.
1789 This service may only be called from the BSP.
1791 @param[in] ProcessorNumber The handle number of AP.
1792 @param[in] EnableAP Specifies the new state for the processor for
1793 enabled, FALSE for disabled.
1794 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1795 the new health status of the AP.
1797 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1798 @retval others Failed to Enable/Disable AP.
1802 EnableDisableApWorker (
1803 IN UINTN ProcessorNumber
,
1804 IN BOOLEAN EnableAP
,
1805 IN UINT32
*HealthFlag OPTIONAL
1808 CPU_MP_DATA
*CpuMpData
;
1811 CpuMpData
= GetCpuMpData ();
1814 // Check whether caller processor is BSP
1816 MpInitLibWhoAmI (&CallerNumber
);
1817 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1818 return EFI_DEVICE_ERROR
;
1821 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1822 return EFI_INVALID_PARAMETER
;
1825 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1826 return EFI_NOT_FOUND
;
1830 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1832 ResetProcessorToIdleState (ProcessorNumber
);
1835 if (HealthFlag
!= NULL
) {
1836 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1837 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1844 This return the handle number for the calling processor. This service may be
1845 called from the BSP and APs.
1847 @param[out] ProcessorNumber Pointer to the handle number of AP.
1848 The range is from 0 to the total number of
1849 logical processors minus 1. The total number of
1850 logical processors can be retrieved by
1851 MpInitLibGetNumberOfProcessors().
1853 @retval EFI_SUCCESS The current processor handle number was returned
1855 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1856 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1862 OUT UINTN
*ProcessorNumber
1865 CPU_MP_DATA
*CpuMpData
;
1867 if (ProcessorNumber
== NULL
) {
1868 return EFI_INVALID_PARAMETER
;
1871 CpuMpData
= GetCpuMpData ();
1873 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1877 Retrieves the number of logical processor in the platform and the number of
1878 those logical processors that are enabled on this boot. This service may only
1879 be called from the BSP.
1881 @param[out] NumberOfProcessors Pointer to the total number of logical
1882 processors in the system, including the BSP
1884 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1885 processors that exist in system, including
1888 @retval EFI_SUCCESS The number of logical processors and enabled
1889 logical processors was retrieved.
1890 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1891 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1893 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1898 MpInitLibGetNumberOfProcessors (
1899 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1900 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1903 CPU_MP_DATA
*CpuMpData
;
1905 UINTN ProcessorNumber
;
1906 UINTN EnabledProcessorNumber
;
1909 CpuMpData
= GetCpuMpData ();
1911 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1912 return EFI_INVALID_PARAMETER
;
1916 // Check whether caller processor is BSP
1918 MpInitLibWhoAmI (&CallerNumber
);
1919 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1920 return EFI_DEVICE_ERROR
;
1923 ProcessorNumber
= CpuMpData
->CpuCount
;
1924 EnabledProcessorNumber
= 0;
1925 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1926 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1927 EnabledProcessorNumber
++;
1931 if (NumberOfProcessors
!= NULL
) {
1932 *NumberOfProcessors
= ProcessorNumber
;
1934 if (NumberOfEnabledProcessors
!= NULL
) {
1935 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1943 Worker function to execute a caller provided function on all enabled APs.
1945 @param[in] Procedure A pointer to the function to be run on
1946 enabled APs of the system.
1947 @param[in] SingleThread If TRUE, then all the enabled APs execute
1948 the function specified by Procedure one by
1949 one, in ascending order of processor handle
1950 number. If FALSE, then all the enabled APs
1951 execute the function specified by Procedure
1953 @param[in] WaitEvent The event created by the caller with CreateEvent()
1955 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1956 APs to return from Procedure, either for
1957 blocking or non-blocking mode.
1958 @param[in] ProcedureArgument The parameter passed into Procedure for
1960 @param[out] FailedCpuList If all APs finish successfully, then its
1961 content is set to NULL. If not all APs
1962 finish before timeout expires, then its
1963 content is set to address of the buffer
1964 holding handle numbers of the failed APs.
1966 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1967 the timeout expired.
1968 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1970 @retval others Failed to Startup all APs.
1974 StartupAllAPsWorker (
1975 IN EFI_AP_PROCEDURE Procedure
,
1976 IN BOOLEAN SingleThread
,
1977 IN EFI_EVENT WaitEvent OPTIONAL
,
1978 IN UINTN TimeoutInMicroseconds
,
1979 IN VOID
*ProcedureArgument OPTIONAL
,
1980 OUT UINTN
**FailedCpuList OPTIONAL
1984 CPU_MP_DATA
*CpuMpData
;
1985 UINTN ProcessorCount
;
1986 UINTN ProcessorNumber
;
1988 CPU_AP_DATA
*CpuData
;
1989 BOOLEAN HasEnabledAp
;
1992 CpuMpData
= GetCpuMpData ();
1994 if (FailedCpuList
!= NULL
) {
1995 *FailedCpuList
= NULL
;
1998 if (CpuMpData
->CpuCount
== 1) {
1999 return EFI_NOT_STARTED
;
2002 if (Procedure
== NULL
) {
2003 return EFI_INVALID_PARAMETER
;
2007 // Check whether caller processor is BSP
2009 MpInitLibWhoAmI (&CallerNumber
);
2010 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2011 return EFI_DEVICE_ERROR
;
2017 CheckAndUpdateApsStatus ();
2019 ProcessorCount
= CpuMpData
->CpuCount
;
2020 HasEnabledAp
= FALSE
;
2022 // Check whether all enabled APs are idle.
2023 // If any enabled AP is not idle, return EFI_NOT_READY.
2025 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2026 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2027 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2028 ApState
= GetApState (CpuData
);
2029 if (ApState
!= CpuStateDisabled
) {
2030 HasEnabledAp
= TRUE
;
2031 if (ApState
!= CpuStateIdle
) {
2033 // If any enabled APs are busy, return EFI_NOT_READY.
2035 return EFI_NOT_READY
;
2041 if (!HasEnabledAp
) {
2043 // If no enabled AP exists, return EFI_NOT_STARTED.
2045 return EFI_NOT_STARTED
;
2048 CpuMpData
->StartCount
= 0;
2049 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2050 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2051 CpuData
->Waiting
= FALSE
;
2052 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2053 if (CpuData
->State
== CpuStateIdle
) {
2055 // Mark this processor as responsible for current calling.
2057 CpuData
->Waiting
= TRUE
;
2058 CpuMpData
->StartCount
++;
2063 CpuMpData
->Procedure
= Procedure
;
2064 CpuMpData
->ProcArguments
= ProcedureArgument
;
2065 CpuMpData
->SingleThread
= SingleThread
;
2066 CpuMpData
->FinishedCount
= 0;
2067 CpuMpData
->RunningCount
= 0;
2068 CpuMpData
->FailedCpuList
= FailedCpuList
;
2069 CpuMpData
->ExpectedTime
= CalculateTimeout (
2070 TimeoutInMicroseconds
,
2071 &CpuMpData
->CurrentTime
2073 CpuMpData
->TotalTime
= 0;
2074 CpuMpData
->WaitEvent
= WaitEvent
;
2076 if (!SingleThread
) {
2077 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
2079 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2080 if (ProcessorNumber
== CallerNumber
) {
2083 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2084 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2090 Status
= EFI_SUCCESS
;
2091 if (WaitEvent
== NULL
) {
2093 Status
= CheckAllAPs ();
2094 } while (Status
== EFI_NOT_READY
);
2101 Worker function to let the caller get one enabled AP to execute a caller-provided
2104 @param[in] Procedure A pointer to the function to be run on
2105 enabled APs of the system.
2106 @param[in] ProcessorNumber The handle number of the AP.
2107 @param[in] WaitEvent The event created by the caller with CreateEvent()
2109 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2110 APs to return from Procedure, either for
2111 blocking or non-blocking mode.
2112 @param[in] ProcedureArgument The parameter passed into Procedure for
2114 @param[out] Finished If AP returns from Procedure before the
2115 timeout expires, its content is set to TRUE.
2116 Otherwise, the value is set to FALSE.
2118 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2119 the timeout expires.
2120 @retval others Failed to Startup AP.
2124 StartupThisAPWorker (
2125 IN EFI_AP_PROCEDURE Procedure
,
2126 IN UINTN ProcessorNumber
,
2127 IN EFI_EVENT WaitEvent OPTIONAL
,
2128 IN UINTN TimeoutInMicroseconds
,
2129 IN VOID
*ProcedureArgument OPTIONAL
,
2130 OUT BOOLEAN
*Finished OPTIONAL
2134 CPU_MP_DATA
*CpuMpData
;
2135 CPU_AP_DATA
*CpuData
;
2138 CpuMpData
= GetCpuMpData ();
2140 if (Finished
!= NULL
) {
2145 // Check whether caller processor is BSP
2147 MpInitLibWhoAmI (&CallerNumber
);
2148 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2149 return EFI_DEVICE_ERROR
;
2153 // Check whether processor with the handle specified by ProcessorNumber exists
2155 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2156 return EFI_NOT_FOUND
;
2160 // Check whether specified processor is BSP
2162 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2163 return EFI_INVALID_PARAMETER
;
2167 // Check parameter Procedure
2169 if (Procedure
== NULL
) {
2170 return EFI_INVALID_PARAMETER
;
2176 CheckAndUpdateApsStatus ();
2179 // Check whether specified AP is disabled
2181 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2182 return EFI_INVALID_PARAMETER
;
2186 // If WaitEvent is not NULL, execute in non-blocking mode.
2187 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2188 // CheckAPsStatus() will check completion and timeout periodically.
2190 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2191 CpuData
->WaitEvent
= WaitEvent
;
2192 CpuData
->Finished
= Finished
;
2193 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2194 CpuData
->TotalTime
= 0;
2196 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2199 // If WaitEvent is NULL, execute in blocking mode.
2200 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2202 Status
= EFI_SUCCESS
;
2203 if (WaitEvent
== NULL
) {
2205 Status
= CheckThisAP (ProcessorNumber
);
2206 } while (Status
== EFI_NOT_READY
);
2213 Get pointer to CPU MP Data structure from GUIDed HOB.
2215 @return The pointer to CPU MP Data structure.
2218 GetCpuMpDataFromGuidedHob (
2222 EFI_HOB_GUID_TYPE
*GuidHob
;
2224 CPU_MP_DATA
*CpuMpData
;
2227 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2228 if (GuidHob
!= NULL
) {
2229 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2230 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);