2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016, 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.
21 DxeIpl may have enabled Execute Disable for BSP,
22 APs need to get the status and sync up the settings.
24 @retval TRUE BSP Execute Disable is enabled.
25 @retval FALSE BSP Execute Disable is not enabled.
28 IsBspExecuteDisableEnabled (
33 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
34 MSR_IA32_EFER_REGISTER EferMsr
;
38 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
39 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
40 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
43 // Bit 20: Execute Disable Bit available.
45 if (Edx
.Bits
.NX
!= 0) {
46 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
49 // Bit 11: Execute Disable Bit enable.
51 if (EferMsr
.Bits
.NXE
!= 0) {
61 Worker function for SwitchBSP().
63 Worker function for SwitchBSP(), assigned to the AP which is intended
66 @param[in] Buffer Pointer to CPU MP Data
74 CPU_MP_DATA
*DataInHob
;
76 DataInHob
= (CPU_MP_DATA
*) Buffer
;
77 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
81 Get the Application Processors state.
83 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
89 IN CPU_AP_DATA
*CpuData
92 return CpuData
->State
;
96 Set the Application Processors state.
98 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
99 @param[in] State The AP status
103 IN CPU_AP_DATA
*CpuData
,
107 AcquireSpinLock (&CpuData
->ApLock
);
108 CpuData
->State
= State
;
109 ReleaseSpinLock (&CpuData
->ApLock
);
113 Save BSP's local APIC timer setting
115 @param[in] CpuMpData Pointer to CPU MP Data
118 SaveLocalApicTimerSetting (
119 IN CPU_MP_DATA
*CpuMpData
123 // Record the current local APIC timer setting of BSP
126 &CpuMpData
->DivideValue
,
127 &CpuMpData
->PeriodicMode
,
130 CpuMpData
->CurrentTimerCount
= GetApicTimerCurrentCount ();
131 CpuMpData
->TimerInterruptState
= GetApicTimerInterruptState ();
135 Sync local APIC timer setting from BSP to AP.
137 @param[in] CpuMpData Pointer to CPU MP Data
140 SyncLocalApicTimerSetting (
141 IN CPU_MP_DATA
*CpuMpData
145 // Sync local APIC timer setting from BSP to AP
147 InitializeApicTimer (
148 CpuMpData
->DivideValue
,
149 CpuMpData
->CurrentTimerCount
,
150 CpuMpData
->PeriodicMode
,
154 // Disable AP's local APIC timer interrupt
156 DisableApicTimerInterrupt ();
160 Save the volatile registers required to be restored following INIT IPI.
162 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
165 SaveVolatileRegisters (
166 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
169 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
171 VolatileRegisters
->Cr0
= AsmReadCr0 ();
172 VolatileRegisters
->Cr3
= AsmReadCr3 ();
173 VolatileRegisters
->Cr4
= AsmReadCr4 ();
175 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
176 if (VersionInfoEdx
.Bits
.DE
!= 0) {
178 // If processor supports Debugging Extensions feature
179 // by CPUID.[EAX=01H]:EDX.BIT2
181 VolatileRegisters
->Dr0
= AsmReadDr0 ();
182 VolatileRegisters
->Dr1
= AsmReadDr1 ();
183 VolatileRegisters
->Dr2
= AsmReadDr2 ();
184 VolatileRegisters
->Dr3
= AsmReadDr3 ();
185 VolatileRegisters
->Dr6
= AsmReadDr6 ();
186 VolatileRegisters
->Dr7
= AsmReadDr7 ();
191 Restore the volatile registers following INIT IPI.
193 @param[in] VolatileRegisters Pointer to volatile resisters
194 @param[in] IsRestoreDr TRUE: Restore DRx if supported
195 FALSE: Do not restore DRx
198 RestoreVolatileRegisters (
199 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
200 IN BOOLEAN IsRestoreDr
203 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
205 AsmWriteCr0 (VolatileRegisters
->Cr0
);
206 AsmWriteCr3 (VolatileRegisters
->Cr3
);
207 AsmWriteCr4 (VolatileRegisters
->Cr4
);
210 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
211 if (VersionInfoEdx
.Bits
.DE
!= 0) {
213 // If processor supports Debugging Extensions feature
214 // by CPUID.[EAX=01H]:EDX.BIT2
216 AsmWriteDr0 (VolatileRegisters
->Dr0
);
217 AsmWriteDr1 (VolatileRegisters
->Dr1
);
218 AsmWriteDr2 (VolatileRegisters
->Dr2
);
219 AsmWriteDr3 (VolatileRegisters
->Dr3
);
220 AsmWriteDr6 (VolatileRegisters
->Dr6
);
221 AsmWriteDr7 (VolatileRegisters
->Dr7
);
227 Detect whether Mwait-monitor feature is supported.
229 @retval TRUE Mwait-monitor feature is supported.
230 @retval FALSE Mwait-monitor feature is not supported.
237 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
239 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
240 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
246 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
248 @return The AP loop mode.
252 OUT UINT32
*MonitorFilterSize
256 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
258 ASSERT (MonitorFilterSize
!= NULL
);
260 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
261 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
262 if (ApLoopMode
== ApInMwaitLoop
) {
263 if (!IsMwaitSupport ()) {
265 // If processor does not support MONITOR/MWAIT feature,
266 // force AP in Hlt-loop mode
268 ApLoopMode
= ApInHltLoop
;
272 if (ApLoopMode
!= ApInMwaitLoop
) {
273 *MonitorFilterSize
= sizeof (UINT32
);
276 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
277 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
279 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
280 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
287 Sort the APIC ID of all processors.
289 This function sorts the APIC ID of all processors so that processor number is
290 assigned in the ascending order of APIC ID which eases MP debugging.
292 @param[in] CpuMpData Pointer to PEI CPU MP Data
296 IN CPU_MP_DATA
*CpuMpData
303 CPU_INFO_IN_HOB CpuInfo
;
305 CPU_INFO_IN_HOB
*CpuInfoInHob
;
307 ApCount
= CpuMpData
->CpuCount
- 1;
308 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
310 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
313 // Sort key is the hardware default APIC ID
315 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
316 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
317 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
319 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
322 if (Index3
!= Index1
) {
323 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
325 &CpuInfoInHob
[Index3
],
326 &CpuInfoInHob
[Index1
],
327 sizeof (CPU_INFO_IN_HOB
)
329 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
334 // Get the processor number for the BSP
336 ApicId
= GetInitialApicId ();
337 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
338 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
339 CpuMpData
->BspNumber
= (UINT32
) Index1
;
347 Enable x2APIC mode on APs.
349 @param[in, out] Buffer Pointer to private data buffer.
357 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
363 @param[in, out] Buffer Pointer to private data buffer.
371 CPU_MP_DATA
*CpuMpData
;
373 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
375 // Sync BSP's MTRR table to AP
377 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
379 // Load microcode on AP
381 MicrocodeDetect (CpuMpData
);
385 Find the current Processor number by APIC ID.
387 @param[in] CpuMpData Pointer to PEI CPU MP Data
388 @param[out] ProcessorNumber Return the pocessor number found
390 @retval EFI_SUCCESS ProcessorNumber is found and returned.
391 @retval EFI_NOT_FOUND ProcessorNumber is not found.
395 IN CPU_MP_DATA
*CpuMpData
,
396 OUT UINTN
*ProcessorNumber
399 UINTN TotalProcessorNumber
;
401 CPU_INFO_IN_HOB
*CpuInfoInHob
;
403 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
405 TotalProcessorNumber
= CpuMpData
->CpuCount
;
406 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
407 if (CpuInfoInHob
[Index
].ApicId
== GetApicId ()) {
408 *ProcessorNumber
= Index
;
412 return EFI_NOT_FOUND
;
416 This function will get CPU count in the system.
418 @param[in] CpuMpData Pointer to PEI CPU MP Data
420 @return CPU count detected
423 CollectProcessorCount (
424 IN CPU_MP_DATA
*CpuMpData
428 // Send 1st broadcast IPI to APs to wakeup APs
430 CpuMpData
->InitFlag
= ApInitConfig
;
431 CpuMpData
->X2ApicEnable
= FALSE
;
432 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
);
433 CpuMpData
->InitFlag
= ApInitDone
;
434 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
436 // Wait for all APs finished the initialization
438 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
442 if (CpuMpData
->X2ApicEnable
) {
443 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
445 // Wakeup all APs to enable x2APIC mode
447 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
449 // Wait for all known APs finished
451 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
455 // Enable x2APIC on BSP
457 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
459 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
461 // Sort BSP/Aps by CPU APIC ID in ascending order
463 SortApicId (CpuMpData
);
465 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
467 return CpuMpData
->CpuCount
;
471 Initialize CPU AP Data when AP is wakeup at the first time.
473 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
474 @param[in] ProcessorNumber The handle number of processor
475 @param[in] BistData Processor BIST data
476 @param[in] ApTopOfStack Top of AP stack
481 IN OUT CPU_MP_DATA
*CpuMpData
,
482 IN UINTN ProcessorNumber
,
484 IN UINT64 ApTopOfStack
487 CPU_INFO_IN_HOB
*CpuInfoInHob
;
489 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
490 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
491 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
492 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
493 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
495 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
496 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
497 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
499 // Set x2APIC mode if there are any logical processor reporting
500 // an Initial APIC ID of 255 or greater.
502 AcquireSpinLock(&CpuMpData
->MpLock
);
503 CpuMpData
->X2ApicEnable
= TRUE
;
504 ReleaseSpinLock(&CpuMpData
->MpLock
);
507 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
508 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
512 This function will be called from AP reset code if BSP uses WakeUpAP.
514 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
515 @param[in] NumApsExecuting Number of current executing AP
520 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
521 IN UINTN NumApsExecuting
524 CPU_MP_DATA
*CpuMpData
;
525 UINTN ProcessorNumber
;
526 EFI_AP_PROCEDURE Procedure
;
529 volatile UINT32
*ApStartupSignalBuffer
;
530 CPU_INFO_IN_HOB
*CpuInfoInHob
;
534 // AP finished assembly code and begin to execute C code
536 CpuMpData
= ExchangeInfo
->CpuMpData
;
539 // AP's local APIC settings will be lost after received INIT IPI
540 // We need to re-initialize them at here
542 ProgramVirtualWireMode ();
543 SyncLocalApicTimerSetting (CpuMpData
);
546 if (CpuMpData
->InitFlag
== ApInitConfig
) {
550 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
551 ProcessorNumber
= NumApsExecuting
;
553 // This is first time AP wakeup, get BIST information from AP stack
555 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
556 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
558 // Do some AP initialize sync
560 ApInitializeSync (CpuMpData
);
562 // Sync BSP's Control registers to APs
564 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
565 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
566 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
569 // Execute AP function if AP is ready
571 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
573 // Clear AP start-up signal when AP waken up
575 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
576 InterlockedCompareExchange32 (
577 (UINT32
*) ApStartupSignalBuffer
,
581 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
583 // Restore AP's volatile registers saved
585 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
588 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
589 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
590 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
591 if (Procedure
!= NULL
) {
592 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
594 // Enable source debugging on AP function
598 // Invoke AP function here
600 Procedure (Parameter
);
601 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
602 if (CpuMpData
->SwitchBspFlag
) {
604 // Re-get the processor number due to BSP/AP maybe exchange in AP function
606 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
607 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
608 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
609 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
610 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
613 // Re-get the CPU APICID and Initial APICID
615 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
616 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
619 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
624 // AP finished executing C code
626 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
629 // Place AP is specified loop mode
631 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
633 // Save AP volatile registers
635 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
637 // Place AP in HLT-loop
640 DisableInterrupts ();
646 DisableInterrupts ();
647 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
649 // Place AP in MWAIT-loop
651 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
652 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
654 // Check AP start-up signal again.
655 // If AP start-up signal is not set, place AP into
656 // the specified C-state
658 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
660 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
662 // Place AP in Run-loop
670 // If AP start-up signal is written, AP is waken up
671 // otherwise place AP in loop again
673 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
681 Wait for AP wakeup and write AP start-up signal till AP is waken up.
683 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
687 IN
volatile UINT32
*ApStartupSignalBuffer
691 // If AP is waken up, StartupApSignal should be cleared.
692 // Otherwise, write StartupApSignal again till AP waken up.
694 while (InterlockedCompareExchange32 (
695 (UINT32
*) ApStartupSignalBuffer
,
704 This function will fill the exchange info structure.
706 @param[in] CpuMpData Pointer to CPU MP Data
710 FillExchangeInfoData (
711 IN CPU_MP_DATA
*CpuMpData
714 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
716 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
717 ExchangeInfo
->Lock
= 0;
718 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
719 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
720 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
721 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
723 ExchangeInfo
->CodeSegment
= AsmReadCs ();
724 ExchangeInfo
->DataSegment
= AsmReadDs ();
726 ExchangeInfo
->Cr3
= AsmReadCr3 ();
728 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
729 ExchangeInfo
->NumApsExecuting
= 0;
730 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
731 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
732 ExchangeInfo
->CpuMpData
= CpuMpData
;
734 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
737 // Get the BSP's data of GDT and IDT
739 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
740 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
744 Helper function that waits until the finished AP count reaches the specified
745 limit, or the specified timeout elapses (whichever comes first).
747 @param[in] CpuMpData Pointer to CPU MP Data.
748 @param[in] FinishedApLimit The number of finished APs to wait for.
749 @param[in] TimeLimit The number of microseconds to wait for.
752 TimedWaitForApFinish (
753 IN CPU_MP_DATA
*CpuMpData
,
754 IN UINT32 FinishedApLimit
,
759 This function will be called by BSP to wakeup AP.
761 @param[in] CpuMpData Pointer to CPU MP Data
762 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
763 FALSE: Send IPI to AP by ApicId
764 @param[in] ProcessorNumber The handle number of specified processor
765 @param[in] Procedure The function to be invoked by AP
766 @param[in] ProcedureArgument The argument to be passed into AP function
770 IN CPU_MP_DATA
*CpuMpData
,
771 IN BOOLEAN Broadcast
,
772 IN UINTN ProcessorNumber
,
773 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
774 IN VOID
*ProcedureArgument OPTIONAL
777 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
779 CPU_AP_DATA
*CpuData
;
780 BOOLEAN ResetVectorRequired
;
781 CPU_INFO_IN_HOB
*CpuInfoInHob
;
783 CpuMpData
->FinishedCount
= 0;
784 ResetVectorRequired
= FALSE
;
786 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
787 CpuMpData
->InitFlag
!= ApInitDone
) {
788 ResetVectorRequired
= TRUE
;
789 AllocateResetVector (CpuMpData
);
790 FillExchangeInfoData (CpuMpData
);
791 SaveLocalApicTimerSetting (CpuMpData
);
792 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
794 // Get AP target C-state each time when waking up AP,
795 // for it maybe updated by platform again
797 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
800 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
803 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
804 if (Index
!= CpuMpData
->BspNumber
) {
805 CpuData
= &CpuMpData
->CpuData
[Index
];
806 CpuData
->ApFunction
= (UINTN
) Procedure
;
807 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
808 SetApState (CpuData
, CpuStateReady
);
809 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
810 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
814 if (ResetVectorRequired
) {
818 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
820 if (CpuMpData
->InitFlag
== ApInitConfig
) {
822 // Wait for all potential APs waken up in one specified period
824 TimedWaitForApFinish (
826 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
827 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
831 // Wait all APs waken up if this is not the 1st broadcast of SIPI
833 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
834 CpuData
= &CpuMpData
->CpuData
[Index
];
835 if (Index
!= CpuMpData
->BspNumber
) {
836 WaitApWakeup (CpuData
->StartupApSignal
);
841 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
842 CpuData
->ApFunction
= (UINTN
) Procedure
;
843 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
844 SetApState (CpuData
, CpuStateReady
);
846 // Wakeup specified AP
848 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
849 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
850 if (ResetVectorRequired
) {
851 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
853 CpuInfoInHob
[ProcessorNumber
].ApicId
,
854 (UINT32
) ExchangeInfo
->BufferStart
858 // Wait specified AP waken up
860 WaitApWakeup (CpuData
->StartupApSignal
);
863 if (ResetVectorRequired
) {
864 FreeResetVector (CpuMpData
);
869 Calculate timeout value and return the current performance counter value.
871 Calculate the number of performance counter ticks required for a timeout.
872 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
875 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
876 @param[out] CurrentTime Returns the current value of the performance counter.
878 @return Expected time stamp counter for timeout.
879 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
885 IN UINTN TimeoutInMicroseconds
,
886 OUT UINT64
*CurrentTime
890 // Read the current value of the performance counter
892 *CurrentTime
= GetPerformanceCounter ();
895 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
898 if (TimeoutInMicroseconds
== 0) {
903 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
904 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
905 // it by 1,000,000, to get the number of ticks for the timeout value.
909 GetPerformanceCounterProperties (NULL
, NULL
),
910 TimeoutInMicroseconds
917 Checks whether timeout expires.
919 Check whether the number of elapsed performance counter ticks required for
920 a timeout condition has been reached.
921 If Timeout is zero, which means infinity, return value is always FALSE.
923 @param[in, out] PreviousTime On input, the value of the performance counter
924 when it was last read.
925 On output, the current value of the performance
927 @param[in] TotalTime The total amount of elapsed time in performance
929 @param[in] Timeout The number of performance counter ticks required
930 to reach a timeout condition.
932 @retval TRUE A timeout condition has been reached.
933 @retval FALSE A timeout condition has not been reached.
938 IN OUT UINT64
*PreviousTime
,
939 IN UINT64
*TotalTime
,
952 GetPerformanceCounterProperties (&Start
, &End
);
958 CurrentTime
= GetPerformanceCounter();
959 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
967 *PreviousTime
= CurrentTime
;
968 if (*TotalTime
> Timeout
) {
975 Helper function that waits until the finished AP count reaches the specified
976 limit, or the specified timeout elapses (whichever comes first).
978 @param[in] CpuMpData Pointer to CPU MP Data.
979 @param[in] FinishedApLimit The number of finished APs to wait for.
980 @param[in] TimeLimit The number of microseconds to wait for.
983 TimedWaitForApFinish (
984 IN CPU_MP_DATA
*CpuMpData
,
985 IN UINT32 FinishedApLimit
,
990 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
991 // "infinity", so check for (TimeLimit == 0) explicitly.
993 if (TimeLimit
== 0) {
997 CpuMpData
->TotalTime
= 0;
998 CpuMpData
->ExpectedTime
= CalculateTimeout (
1000 &CpuMpData
->CurrentTime
1002 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1004 &CpuMpData
->CurrentTime
,
1005 &CpuMpData
->TotalTime
,
1006 CpuMpData
->ExpectedTime
1011 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1014 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1017 DivU64x64Remainder (
1018 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1019 GetPerformanceCounterProperties (NULL
, NULL
),
1027 Reset an AP to Idle state.
1029 Any task being executed by the AP will be aborted and the AP
1030 will be waiting for a new task in Wait-For-SIPI state.
1032 @param[in] ProcessorNumber The handle number of processor.
1035 ResetProcessorToIdleState (
1036 IN UINTN ProcessorNumber
1039 CPU_MP_DATA
*CpuMpData
;
1041 CpuMpData
= GetCpuMpData ();
1043 CpuMpData
->InitFlag
= ApInitReconfig
;
1044 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
1045 while (CpuMpData
->FinishedCount
< 1) {
1048 CpuMpData
->InitFlag
= ApInitDone
;
1050 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1054 Searches for the next waiting AP.
1056 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1058 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1060 @retval EFI_SUCCESS The next waiting AP has been found.
1061 @retval EFI_NOT_FOUND No waiting AP exists.
1065 GetNextWaitingProcessorNumber (
1066 OUT UINTN
*NextProcessorNumber
1069 UINTN ProcessorNumber
;
1070 CPU_MP_DATA
*CpuMpData
;
1072 CpuMpData
= GetCpuMpData ();
1074 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1075 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1076 *NextProcessorNumber
= ProcessorNumber
;
1081 return EFI_NOT_FOUND
;
1084 /** Checks status of specified AP.
1086 This function checks whether the specified AP has finished the task assigned
1087 by StartupThisAP(), and whether timeout expires.
1089 @param[in] ProcessorNumber The handle number of processor.
1091 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1092 @retval EFI_TIMEOUT The timeout expires.
1093 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1097 IN UINTN ProcessorNumber
1100 CPU_MP_DATA
*CpuMpData
;
1101 CPU_AP_DATA
*CpuData
;
1103 CpuMpData
= GetCpuMpData ();
1104 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1107 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1108 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1109 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1112 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1114 if (GetApState(CpuData
) == CpuStateFinished
) {
1115 if (CpuData
->Finished
!= NULL
) {
1116 *(CpuData
->Finished
) = TRUE
;
1118 SetApState (CpuData
, CpuStateIdle
);
1122 // If timeout expires for StartupThisAP(), report timeout.
1124 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1125 if (CpuData
->Finished
!= NULL
) {
1126 *(CpuData
->Finished
) = FALSE
;
1129 // Reset failed AP to idle state
1131 ResetProcessorToIdleState (ProcessorNumber
);
1136 return EFI_NOT_READY
;
1140 Checks status of all APs.
1142 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1143 and whether timeout expires.
1145 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1146 @retval EFI_TIMEOUT The timeout expires.
1147 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1154 UINTN ProcessorNumber
;
1155 UINTN NextProcessorNumber
;
1158 CPU_MP_DATA
*CpuMpData
;
1159 CPU_AP_DATA
*CpuData
;
1161 CpuMpData
= GetCpuMpData ();
1163 NextProcessorNumber
= 0;
1166 // Go through all APs that are responsible for the StartupAllAPs().
1168 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1169 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1173 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1175 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1176 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1177 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1179 if (GetApState(CpuData
) == CpuStateFinished
) {
1180 CpuMpData
->RunningCount
++;
1181 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1182 SetApState(CpuData
, CpuStateIdle
);
1185 // If in Single Thread mode, then search for the next waiting AP for execution.
1187 if (CpuMpData
->SingleThread
) {
1188 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1190 if (!EFI_ERROR (Status
)) {
1194 (UINT32
) NextProcessorNumber
,
1195 CpuMpData
->Procedure
,
1196 CpuMpData
->ProcArguments
1204 // If all APs finish, return EFI_SUCCESS.
1206 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1211 // If timeout expires, report timeout.
1214 &CpuMpData
->CurrentTime
,
1215 &CpuMpData
->TotalTime
,
1216 CpuMpData
->ExpectedTime
)
1219 // If FailedCpuList is not NULL, record all failed APs in it.
1221 if (CpuMpData
->FailedCpuList
!= NULL
) {
1222 *CpuMpData
->FailedCpuList
=
1223 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1224 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1228 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1230 // Check whether this processor is responsible for StartupAllAPs().
1232 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1234 // Reset failed APs to idle state
1236 ResetProcessorToIdleState (ProcessorNumber
);
1237 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1238 if (CpuMpData
->FailedCpuList
!= NULL
) {
1239 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1243 if (CpuMpData
->FailedCpuList
!= NULL
) {
1244 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1248 return EFI_NOT_READY
;
1252 MP Initialize Library initialization.
1254 This service will allocate AP reset vector and wakeup all APs to do APs
1257 This service must be invoked before all other MP Initialize Library
1258 service are invoked.
1260 @retval EFI_SUCCESS MP initialization succeeds.
1261 @retval Others MP initialization fails.
1266 MpInitLibInitialize (
1270 CPU_MP_DATA
*OldCpuMpData
;
1271 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1272 UINT32 MaxLogicalProcessorNumber
;
1274 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1276 UINT32 MonitorFilterSize
;
1279 CPU_MP_DATA
*CpuMpData
;
1281 UINT8
*MonitorBuffer
;
1283 UINTN ApResetVectorSize
;
1284 UINTN BackupBufferAddr
;
1286 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1287 if (OldCpuMpData
== NULL
) {
1288 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1290 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1292 ASSERT (MaxLogicalProcessorNumber
!= 0);
1294 AsmGetAddressMap (&AddressMap
);
1295 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1296 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1297 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1299 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1300 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1301 BufferSize
+= sizeof (CPU_MP_DATA
);
1302 BufferSize
+= ApResetVectorSize
;
1303 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1304 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1305 ASSERT (MpBuffer
!= NULL
);
1306 ZeroMem (MpBuffer
, BufferSize
);
1307 Buffer
= (UINTN
) MpBuffer
;
1309 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1310 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1311 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1312 CpuMpData
->Buffer
= Buffer
;
1313 CpuMpData
->CpuApStackSize
= ApStackSize
;
1314 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1315 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1316 CpuMpData
->SaveRestoreFlag
= FALSE
;
1317 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1318 CpuMpData
->CpuCount
= 1;
1319 CpuMpData
->BspNumber
= 0;
1320 CpuMpData
->WaitEvent
= NULL
;
1321 CpuMpData
->SwitchBspFlag
= FALSE
;
1322 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1323 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1324 InitializeSpinLock(&CpuMpData
->MpLock
);
1326 // Save BSP's Control registers to APs
1328 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1330 // Set BSP basic information
1332 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
);
1334 // Save assembly code information
1336 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1338 // Finally set AP loop mode
1340 CpuMpData
->ApLoopMode
= ApLoopMode
;
1341 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1343 // Set up APs wakeup signal buffer
1345 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1346 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1347 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1350 // Load Microcode on BSP
1352 MicrocodeDetect (CpuMpData
);
1354 // Store BSP's MTRR setting
1356 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1358 if (OldCpuMpData
== NULL
) {
1359 if (MaxLogicalProcessorNumber
> 1) {
1361 // Wakeup all APs and calculate the processor count in system
1363 CollectProcessorCount (CpuMpData
);
1367 // APs have been wakeup before, just get the CPU Information
1370 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1371 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1372 CpuMpData
->InitFlag
= ApInitReconfig
;
1373 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1374 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1375 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1376 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1377 if (CpuInfoInHob
[Index
].InitialApicId
>= 255) {
1378 CpuMpData
->X2ApicEnable
= TRUE
;
1380 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1381 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1383 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1384 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1385 sizeof (CPU_VOLATILE_REGISTERS
)
1388 if (MaxLogicalProcessorNumber
> 1) {
1390 // Wakeup APs to do some AP initialize sync
1392 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1394 // Wait for all APs finished initialization
1396 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1399 CpuMpData
->InitFlag
= ApInitDone
;
1400 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1401 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1407 // Initialize global data for MP support
1409 InitMpGlobalData (CpuMpData
);
1415 Gets detailed MP-related information on the requested processor at the
1416 instant this call is made. This service may only be called from the BSP.
1418 @param[in] ProcessorNumber The handle number of processor.
1419 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1420 the requested processor is deposited.
1421 @param[out] HealthData Return processor health data.
1423 @retval EFI_SUCCESS Processor information was returned.
1424 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1425 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1426 @retval EFI_NOT_FOUND The processor with the handle specified by
1427 ProcessorNumber does not exist in the platform.
1428 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1433 MpInitLibGetProcessorInfo (
1434 IN UINTN ProcessorNumber
,
1435 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1436 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1439 CPU_MP_DATA
*CpuMpData
;
1441 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1443 CpuMpData
= GetCpuMpData ();
1444 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1447 // Check whether caller processor is BSP
1449 MpInitLibWhoAmI (&CallerNumber
);
1450 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1451 return EFI_DEVICE_ERROR
;
1454 if (ProcessorInfoBuffer
== NULL
) {
1455 return EFI_INVALID_PARAMETER
;
1458 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1459 return EFI_NOT_FOUND
;
1462 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1463 ProcessorInfoBuffer
->StatusFlag
= 0;
1464 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1465 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1467 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1468 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1470 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1471 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1473 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1477 // Get processor location information
1479 GetProcessorLocationByApicId (
1480 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1481 &ProcessorInfoBuffer
->Location
.Package
,
1482 &ProcessorInfoBuffer
->Location
.Core
,
1483 &ProcessorInfoBuffer
->Location
.Thread
1486 if (HealthData
!= NULL
) {
1487 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1494 Worker function to switch the requested AP to be the BSP from that point onward.
1496 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1497 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1498 enabled AP. Otherwise, it will be disabled.
1500 @retval EFI_SUCCESS BSP successfully switched.
1501 @retval others Failed to switch BSP.
1506 IN UINTN ProcessorNumber
,
1507 IN BOOLEAN EnableOldBSP
1510 CPU_MP_DATA
*CpuMpData
;
1513 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1515 CpuMpData
= GetCpuMpData ();
1518 // Check whether caller processor is BSP
1520 MpInitLibWhoAmI (&CallerNumber
);
1521 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1525 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1526 return EFI_NOT_FOUND
;
1530 // Check whether specified AP is disabled
1532 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1533 if (State
== CpuStateDisabled
) {
1534 return EFI_INVALID_PARAMETER
;
1538 // Check whether ProcessorNumber specifies the current BSP
1540 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1541 return EFI_INVALID_PARAMETER
;
1545 // Check whether specified AP is busy
1547 if (State
== CpuStateBusy
) {
1548 return EFI_NOT_READY
;
1551 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1552 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1553 CpuMpData
->SwitchBspFlag
= TRUE
;
1554 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1557 // Clear the BSP bit of MSR_IA32_APIC_BASE
1559 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1560 ApicBaseMsr
.Bits
.BSP
= 0;
1561 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1564 // Need to wakeUp AP (future BSP).
1566 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1568 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1571 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1573 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1574 ApicBaseMsr
.Bits
.BSP
= 1;
1575 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1578 // Wait for old BSP finished AP task
1580 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1584 CpuMpData
->SwitchBspFlag
= FALSE
;
1586 // Set old BSP enable state
1588 if (!EnableOldBSP
) {
1589 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1592 // Save new BSP number
1594 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1600 Worker function to let the caller enable or disable an AP from this point onward.
1601 This service may only be called from the BSP.
1603 @param[in] ProcessorNumber The handle number of AP.
1604 @param[in] EnableAP Specifies the new state for the processor for
1605 enabled, FALSE for disabled.
1606 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1607 the new health status of the AP.
1609 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1610 @retval others Failed to Enable/Disable AP.
1614 EnableDisableApWorker (
1615 IN UINTN ProcessorNumber
,
1616 IN BOOLEAN EnableAP
,
1617 IN UINT32
*HealthFlag OPTIONAL
1620 CPU_MP_DATA
*CpuMpData
;
1623 CpuMpData
= GetCpuMpData ();
1626 // Check whether caller processor is BSP
1628 MpInitLibWhoAmI (&CallerNumber
);
1629 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1630 return EFI_DEVICE_ERROR
;
1633 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1634 return EFI_INVALID_PARAMETER
;
1637 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1638 return EFI_NOT_FOUND
;
1642 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1644 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1647 if (HealthFlag
!= NULL
) {
1648 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1649 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1656 This return the handle number for the calling processor. This service may be
1657 called from the BSP and APs.
1659 @param[out] ProcessorNumber Pointer to the handle number of AP.
1660 The range is from 0 to the total number of
1661 logical processors minus 1. The total number of
1662 logical processors can be retrieved by
1663 MpInitLibGetNumberOfProcessors().
1665 @retval EFI_SUCCESS The current processor handle number was returned
1667 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1668 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1674 OUT UINTN
*ProcessorNumber
1677 CPU_MP_DATA
*CpuMpData
;
1679 if (ProcessorNumber
== NULL
) {
1680 return EFI_INVALID_PARAMETER
;
1683 CpuMpData
= GetCpuMpData ();
1685 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1689 Retrieves the number of logical processor in the platform and the number of
1690 those logical processors that are enabled on this boot. This service may only
1691 be called from the BSP.
1693 @param[out] NumberOfProcessors Pointer to the total number of logical
1694 processors in the system, including the BSP
1696 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1697 processors that exist in system, including
1700 @retval EFI_SUCCESS The number of logical processors and enabled
1701 logical processors was retrieved.
1702 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1703 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1705 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1710 MpInitLibGetNumberOfProcessors (
1711 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1712 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1715 CPU_MP_DATA
*CpuMpData
;
1717 UINTN ProcessorNumber
;
1718 UINTN EnabledProcessorNumber
;
1721 CpuMpData
= GetCpuMpData ();
1723 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1724 return EFI_INVALID_PARAMETER
;
1728 // Check whether caller processor is BSP
1730 MpInitLibWhoAmI (&CallerNumber
);
1731 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1732 return EFI_DEVICE_ERROR
;
1735 ProcessorNumber
= CpuMpData
->CpuCount
;
1736 EnabledProcessorNumber
= 0;
1737 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1738 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1739 EnabledProcessorNumber
++;
1743 if (NumberOfProcessors
!= NULL
) {
1744 *NumberOfProcessors
= ProcessorNumber
;
1746 if (NumberOfEnabledProcessors
!= NULL
) {
1747 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1755 Worker function to execute a caller provided function on all enabled APs.
1757 @param[in] Procedure A pointer to the function to be run on
1758 enabled APs of the system.
1759 @param[in] SingleThread If TRUE, then all the enabled APs execute
1760 the function specified by Procedure one by
1761 one, in ascending order of processor handle
1762 number. If FALSE, then all the enabled APs
1763 execute the function specified by Procedure
1765 @param[in] WaitEvent The event created by the caller with CreateEvent()
1767 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1768 APs to return from Procedure, either for
1769 blocking or non-blocking mode.
1770 @param[in] ProcedureArgument The parameter passed into Procedure for
1772 @param[out] FailedCpuList If all APs finish successfully, then its
1773 content is set to NULL. If not all APs
1774 finish before timeout expires, then its
1775 content is set to address of the buffer
1776 holding handle numbers of the failed APs.
1778 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1779 the timeout expired.
1780 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1782 @retval others Failed to Startup all APs.
1786 StartupAllAPsWorker (
1787 IN EFI_AP_PROCEDURE Procedure
,
1788 IN BOOLEAN SingleThread
,
1789 IN EFI_EVENT WaitEvent OPTIONAL
,
1790 IN UINTN TimeoutInMicroseconds
,
1791 IN VOID
*ProcedureArgument OPTIONAL
,
1792 OUT UINTN
**FailedCpuList OPTIONAL
1796 CPU_MP_DATA
*CpuMpData
;
1797 UINTN ProcessorCount
;
1798 UINTN ProcessorNumber
;
1800 CPU_AP_DATA
*CpuData
;
1801 BOOLEAN HasEnabledAp
;
1804 CpuMpData
= GetCpuMpData ();
1806 if (FailedCpuList
!= NULL
) {
1807 *FailedCpuList
= NULL
;
1810 if (CpuMpData
->CpuCount
== 1) {
1811 return EFI_NOT_STARTED
;
1814 if (Procedure
== NULL
) {
1815 return EFI_INVALID_PARAMETER
;
1819 // Check whether caller processor is BSP
1821 MpInitLibWhoAmI (&CallerNumber
);
1822 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1823 return EFI_DEVICE_ERROR
;
1829 CheckAndUpdateApsStatus ();
1831 ProcessorCount
= CpuMpData
->CpuCount
;
1832 HasEnabledAp
= FALSE
;
1834 // Check whether all enabled APs are idle.
1835 // If any enabled AP is not idle, return EFI_NOT_READY.
1837 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1838 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1839 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1840 ApState
= GetApState (CpuData
);
1841 if (ApState
!= CpuStateDisabled
) {
1842 HasEnabledAp
= TRUE
;
1843 if (ApState
!= CpuStateIdle
) {
1845 // If any enabled APs are busy, return EFI_NOT_READY.
1847 return EFI_NOT_READY
;
1853 if (!HasEnabledAp
) {
1855 // If no enabled AP exists, return EFI_NOT_STARTED.
1857 return EFI_NOT_STARTED
;
1860 CpuMpData
->StartCount
= 0;
1861 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1862 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1863 CpuData
->Waiting
= FALSE
;
1864 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1865 if (CpuData
->State
== CpuStateIdle
) {
1867 // Mark this processor as responsible for current calling.
1869 CpuData
->Waiting
= TRUE
;
1870 CpuMpData
->StartCount
++;
1875 CpuMpData
->Procedure
= Procedure
;
1876 CpuMpData
->ProcArguments
= ProcedureArgument
;
1877 CpuMpData
->SingleThread
= SingleThread
;
1878 CpuMpData
->FinishedCount
= 0;
1879 CpuMpData
->RunningCount
= 0;
1880 CpuMpData
->FailedCpuList
= FailedCpuList
;
1881 CpuMpData
->ExpectedTime
= CalculateTimeout (
1882 TimeoutInMicroseconds
,
1883 &CpuMpData
->CurrentTime
1885 CpuMpData
->TotalTime
= 0;
1886 CpuMpData
->WaitEvent
= WaitEvent
;
1888 if (!SingleThread
) {
1889 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1891 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1892 if (ProcessorNumber
== CallerNumber
) {
1895 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1896 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1902 Status
= EFI_SUCCESS
;
1903 if (WaitEvent
== NULL
) {
1905 Status
= CheckAllAPs ();
1906 } while (Status
== EFI_NOT_READY
);
1913 Worker function to let the caller get one enabled AP to execute a caller-provided
1916 @param[in] Procedure A pointer to the function to be run on
1917 enabled APs of the system.
1918 @param[in] ProcessorNumber The handle number of the AP.
1919 @param[in] WaitEvent The event created by the caller with CreateEvent()
1921 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1922 APs to return from Procedure, either for
1923 blocking or non-blocking mode.
1924 @param[in] ProcedureArgument The parameter passed into Procedure for
1926 @param[out] Finished If AP returns from Procedure before the
1927 timeout expires, its content is set to TRUE.
1928 Otherwise, the value is set to FALSE.
1930 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1931 the timeout expires.
1932 @retval others Failed to Startup AP.
1936 StartupThisAPWorker (
1937 IN EFI_AP_PROCEDURE Procedure
,
1938 IN UINTN ProcessorNumber
,
1939 IN EFI_EVENT WaitEvent OPTIONAL
,
1940 IN UINTN TimeoutInMicroseconds
,
1941 IN VOID
*ProcedureArgument OPTIONAL
,
1942 OUT BOOLEAN
*Finished OPTIONAL
1946 CPU_MP_DATA
*CpuMpData
;
1947 CPU_AP_DATA
*CpuData
;
1950 CpuMpData
= GetCpuMpData ();
1952 if (Finished
!= NULL
) {
1957 // Check whether caller processor is BSP
1959 MpInitLibWhoAmI (&CallerNumber
);
1960 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1961 return EFI_DEVICE_ERROR
;
1965 // Check whether processor with the handle specified by ProcessorNumber exists
1967 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1968 return EFI_NOT_FOUND
;
1972 // Check whether specified processor is BSP
1974 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1975 return EFI_INVALID_PARAMETER
;
1979 // Check parameter Procedure
1981 if (Procedure
== NULL
) {
1982 return EFI_INVALID_PARAMETER
;
1988 CheckAndUpdateApsStatus ();
1991 // Check whether specified AP is disabled
1993 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1994 return EFI_INVALID_PARAMETER
;
1998 // If WaitEvent is not NULL, execute in non-blocking mode.
1999 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2000 // CheckAPsStatus() will check completion and timeout periodically.
2002 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2003 CpuData
->WaitEvent
= WaitEvent
;
2004 CpuData
->Finished
= Finished
;
2005 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2006 CpuData
->TotalTime
= 0;
2008 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2011 // If WaitEvent is NULL, execute in blocking mode.
2012 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2014 Status
= EFI_SUCCESS
;
2015 if (WaitEvent
== NULL
) {
2017 Status
= CheckThisAP (ProcessorNumber
);
2018 } while (Status
== EFI_NOT_READY
);
2025 Get pointer to CPU MP Data structure from GUIDed HOB.
2027 @return The pointer to CPU MP Data structure.
2030 GetCpuMpDataFromGuidedHob (
2034 EFI_HOB_GUID_TYPE
*GuidHob
;
2036 CPU_MP_DATA
*CpuMpData
;
2039 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2040 if (GuidHob
!= NULL
) {
2041 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2042 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
2048 Get available system memory below 1MB by specified size.
2050 @param[in] CpuMpData The pointer to CPU MP Data structure.
2053 BackupAndPrepareWakeupBuffer(
2054 IN CPU_MP_DATA
*CpuMpData
2058 (VOID
*) CpuMpData
->BackupBuffer
,
2059 (VOID
*) CpuMpData
->WakeupBuffer
,
2060 CpuMpData
->BackupBufferSize
2063 (VOID
*) CpuMpData
->WakeupBuffer
,
2064 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
2065 CpuMpData
->AddressMap
.RendezvousFunnelSize
2070 Restore wakeup buffer data.
2072 @param[in] CpuMpData The pointer to CPU MP Data structure.
2075 RestoreWakeupBuffer(
2076 IN CPU_MP_DATA
*CpuMpData
2080 (VOID
*) CpuMpData
->WakeupBuffer
,
2081 (VOID
*) CpuMpData
->BackupBuffer
,
2082 CpuMpData
->BackupBufferSize