2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016 - 2018, 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 ();
199 AsmReadGdtr (&VolatileRegisters
->Gdtr
);
200 AsmReadIdtr (&VolatileRegisters
->Idtr
);
201 VolatileRegisters
->Tr
= AsmReadTr ();
205 Restore the volatile registers following INIT IPI.
207 @param[in] VolatileRegisters Pointer to volatile resisters
208 @param[in] IsRestoreDr TRUE: Restore DRx if supported
209 FALSE: Do not restore DRx
212 RestoreVolatileRegisters (
213 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
214 IN BOOLEAN IsRestoreDr
217 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
218 IA32_TSS_DESCRIPTOR
*Tss
;
220 AsmWriteCr0 (VolatileRegisters
->Cr0
);
221 AsmWriteCr3 (VolatileRegisters
->Cr3
);
222 AsmWriteCr4 (VolatileRegisters
->Cr4
);
225 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
226 if (VersionInfoEdx
.Bits
.DE
!= 0) {
228 // If processor supports Debugging Extensions feature
229 // by CPUID.[EAX=01H]:EDX.BIT2
231 AsmWriteDr0 (VolatileRegisters
->Dr0
);
232 AsmWriteDr1 (VolatileRegisters
->Dr1
);
233 AsmWriteDr2 (VolatileRegisters
->Dr2
);
234 AsmWriteDr3 (VolatileRegisters
->Dr3
);
235 AsmWriteDr6 (VolatileRegisters
->Dr6
);
236 AsmWriteDr7 (VolatileRegisters
->Dr7
);
240 AsmWriteGdtr (&VolatileRegisters
->Gdtr
);
241 AsmWriteIdtr (&VolatileRegisters
->Idtr
);
242 if (VolatileRegisters
->Tr
!= 0 &&
243 VolatileRegisters
->Tr
< VolatileRegisters
->Gdtr
.Limit
) {
244 Tss
= (IA32_TSS_DESCRIPTOR
*)(VolatileRegisters
->Gdtr
.Base
+
245 VolatileRegisters
->Tr
);
246 if (Tss
->Bits
.P
== 1) {
247 Tss
->Bits
.Type
&= 0xD; // 1101 - Clear busy bit just in case
248 AsmWriteTr (VolatileRegisters
->Tr
);
254 Detect whether Mwait-monitor feature is supported.
256 @retval TRUE Mwait-monitor feature is supported.
257 @retval FALSE Mwait-monitor feature is not supported.
264 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
266 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
267 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
273 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
275 @return The AP loop mode.
279 OUT UINT32
*MonitorFilterSize
283 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
285 ASSERT (MonitorFilterSize
!= NULL
);
287 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
288 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
289 if (ApLoopMode
== ApInMwaitLoop
) {
290 if (!IsMwaitSupport ()) {
292 // If processor does not support MONITOR/MWAIT feature,
293 // force AP in Hlt-loop mode
295 ApLoopMode
= ApInHltLoop
;
299 if (ApLoopMode
!= ApInMwaitLoop
) {
300 *MonitorFilterSize
= sizeof (UINT32
);
303 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
304 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
306 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
307 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
314 Sort the APIC ID of all processors.
316 This function sorts the APIC ID of all processors so that processor number is
317 assigned in the ascending order of APIC ID which eases MP debugging.
319 @param[in] CpuMpData Pointer to PEI CPU MP Data
323 IN CPU_MP_DATA
*CpuMpData
330 CPU_INFO_IN_HOB CpuInfo
;
332 CPU_INFO_IN_HOB
*CpuInfoInHob
;
333 volatile UINT32
*StartupApSignal
;
335 ApCount
= CpuMpData
->CpuCount
- 1;
336 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
338 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
341 // Sort key is the hardware default APIC ID
343 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
344 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
345 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
347 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
350 if (Index3
!= Index1
) {
351 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
353 &CpuInfoInHob
[Index3
],
354 &CpuInfoInHob
[Index1
],
355 sizeof (CPU_INFO_IN_HOB
)
357 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
360 // Also exchange the StartupApSignal.
362 StartupApSignal
= CpuMpData
->CpuData
[Index3
].StartupApSignal
;
363 CpuMpData
->CpuData
[Index3
].StartupApSignal
=
364 CpuMpData
->CpuData
[Index1
].StartupApSignal
;
365 CpuMpData
->CpuData
[Index1
].StartupApSignal
= StartupApSignal
;
370 // Get the processor number for the BSP
372 ApicId
= GetInitialApicId ();
373 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
374 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
375 CpuMpData
->BspNumber
= (UINT32
) Index1
;
383 Enable x2APIC mode on APs.
385 @param[in, out] Buffer Pointer to private data buffer.
393 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
399 @param[in, out] Buffer Pointer to private data buffer.
407 CPU_MP_DATA
*CpuMpData
;
409 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
411 // Load microcode on AP
413 MicrocodeDetect (CpuMpData
);
415 // Sync BSP's MTRR table to AP
417 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
421 Find the current Processor number by APIC ID.
423 @param[in] CpuMpData Pointer to PEI CPU MP Data
424 @param[out] ProcessorNumber Return the pocessor number found
426 @retval EFI_SUCCESS ProcessorNumber is found and returned.
427 @retval EFI_NOT_FOUND ProcessorNumber is not found.
431 IN CPU_MP_DATA
*CpuMpData
,
432 OUT UINTN
*ProcessorNumber
435 UINTN TotalProcessorNumber
;
437 CPU_INFO_IN_HOB
*CpuInfoInHob
;
439 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
441 TotalProcessorNumber
= CpuMpData
->CpuCount
;
442 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
443 if (CpuInfoInHob
[Index
].ApicId
== GetApicId ()) {
444 *ProcessorNumber
= Index
;
448 return EFI_NOT_FOUND
;
452 This function will get CPU count in the system.
454 @param[in] CpuMpData Pointer to PEI CPU MP Data
456 @return CPU count detected
459 CollectProcessorCount (
460 IN CPU_MP_DATA
*CpuMpData
466 // Send 1st broadcast IPI to APs to wakeup APs
468 CpuMpData
->InitFlag
= ApInitConfig
;
469 CpuMpData
->X2ApicEnable
= FALSE
;
470 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
);
471 CpuMpData
->InitFlag
= ApInitDone
;
472 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
474 // Wait for all APs finished the initialization
476 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
480 if (CpuMpData
->CpuCount
> 255) {
482 // If there are more than 255 processor found, force to enable X2APIC
484 CpuMpData
->X2ApicEnable
= TRUE
;
486 if (CpuMpData
->X2ApicEnable
) {
487 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
489 // Wakeup all APs to enable x2APIC mode
491 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
493 // Wait for all known APs finished
495 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
499 // Enable x2APIC on BSP
501 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
503 // Set BSP/Aps state to IDLE
505 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
506 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
509 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
511 // Sort BSP/Aps by CPU APIC ID in ascending order
513 SortApicId (CpuMpData
);
515 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
517 return CpuMpData
->CpuCount
;
521 Initialize CPU AP Data when AP is wakeup at the first time.
523 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
524 @param[in] ProcessorNumber The handle number of processor
525 @param[in] BistData Processor BIST data
526 @param[in] ApTopOfStack Top of AP stack
531 IN OUT CPU_MP_DATA
*CpuMpData
,
532 IN UINTN ProcessorNumber
,
534 IN UINT64 ApTopOfStack
537 CPU_INFO_IN_HOB
*CpuInfoInHob
;
539 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
540 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
541 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
542 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
543 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
545 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
546 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
547 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
549 // Set x2APIC mode if there are any logical processor reporting
550 // an Initial APIC ID of 255 or greater.
552 AcquireSpinLock(&CpuMpData
->MpLock
);
553 CpuMpData
->X2ApicEnable
= TRUE
;
554 ReleaseSpinLock(&CpuMpData
->MpLock
);
557 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
558 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
562 This function will be called from AP reset code if BSP uses WakeUpAP.
564 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
565 @param[in] ApIndex Number of current executing AP
570 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
574 CPU_MP_DATA
*CpuMpData
;
575 UINTN ProcessorNumber
;
576 EFI_AP_PROCEDURE Procedure
;
579 volatile UINT32
*ApStartupSignalBuffer
;
580 CPU_INFO_IN_HOB
*CpuInfoInHob
;
582 UINTN CurrentApicMode
;
585 // AP finished assembly code and begin to execute C code
587 CpuMpData
= ExchangeInfo
->CpuMpData
;
590 // AP's local APIC settings will be lost after received INIT IPI
591 // We need to re-initialize them at here
593 ProgramVirtualWireMode ();
595 // Mask the LINT0 and LINT1 so that AP doesn't enter the system timer interrupt handler.
597 DisableLvtInterrupts ();
598 SyncLocalApicTimerSetting (CpuMpData
);
600 CurrentApicMode
= GetApicMode ();
602 if (CpuMpData
->InitFlag
== ApInitConfig
) {
606 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
607 ProcessorNumber
= ApIndex
;
609 // This is first time AP wakeup, get BIST information from AP stack
611 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
612 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
614 // Do some AP initialize sync
616 ApInitializeSync (CpuMpData
);
618 // CpuMpData->CpuData[0].VolatileRegisters is initialized based on BSP environment,
619 // to initialize AP in InitConfig path.
620 // NOTE: IDTR.BASE stored in CpuMpData->CpuData[0].VolatileRegisters points to a different IDT shared by all APs.
622 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
623 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
624 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
627 // Execute AP function if AP is ready
629 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
631 // Clear AP start-up signal when AP waken up
633 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
634 InterlockedCompareExchange32 (
635 (UINT32
*) ApStartupSignalBuffer
,
639 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
641 // Restore AP's volatile registers saved
643 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
646 // The CPU driver might not flush TLB for APs on spot after updating
647 // page attributes. AP in mwait loop mode needs to take care of it when
653 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
654 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
655 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
656 if (Procedure
!= NULL
) {
657 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
659 // Enable source debugging on AP function
663 // Invoke AP function here
665 Procedure (Parameter
);
666 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
667 if (CpuMpData
->SwitchBspFlag
) {
669 // Re-get the processor number due to BSP/AP maybe exchange in AP function
671 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
672 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
673 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
674 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
675 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
677 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
678 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
679 if (CurrentApicMode
!= GetApicMode ()) {
681 // If APIC mode change happened during AP function execution,
682 // we do not support APIC ID value changed.
688 // Re-get the CPU APICID and Initial APICID if they are changed
690 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
691 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
696 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
701 // AP finished executing C code
703 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
704 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
707 // Place AP is specified loop mode
709 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
711 // Save AP volatile registers
713 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
715 // Place AP in HLT-loop
718 DisableInterrupts ();
724 DisableInterrupts ();
725 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
727 // Place AP in MWAIT-loop
729 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
730 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
732 // Check AP start-up signal again.
733 // If AP start-up signal is not set, place AP into
734 // the specified C-state
736 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
738 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
740 // Place AP in Run-loop
748 // If AP start-up signal is written, AP is waken up
749 // otherwise place AP in loop again
751 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
759 Wait for AP wakeup and write AP start-up signal till AP is waken up.
761 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
765 IN
volatile UINT32
*ApStartupSignalBuffer
769 // If AP is waken up, StartupApSignal should be cleared.
770 // Otherwise, write StartupApSignal again till AP waken up.
772 while (InterlockedCompareExchange32 (
773 (UINT32
*) ApStartupSignalBuffer
,
782 This function will fill the exchange info structure.
784 @param[in] CpuMpData Pointer to CPU MP Data
788 FillExchangeInfoData (
789 IN CPU_MP_DATA
*CpuMpData
792 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
794 IA32_SEGMENT_DESCRIPTOR
*Selector
;
796 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
797 ExchangeInfo
->Lock
= 0;
798 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
799 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
800 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
801 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
803 ExchangeInfo
->CodeSegment
= AsmReadCs ();
804 ExchangeInfo
->DataSegment
= AsmReadDs ();
806 ExchangeInfo
->Cr3
= AsmReadCr3 ();
808 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
809 ExchangeInfo
->ApIndex
= 0;
810 ExchangeInfo
->NumApsExecuting
= 0;
811 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
812 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
813 ExchangeInfo
->CpuMpData
= CpuMpData
;
815 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
817 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
820 // Get the BSP's data of GDT and IDT
822 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
823 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
826 // Find a 32-bit code segment
828 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
829 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
831 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
832 ExchangeInfo
->ModeTransitionSegment
=
833 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
837 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
841 // Copy all 32-bit code and 64-bit code into memory with type of
842 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
844 if (CpuMpData
->WakeupBufferHigh
!= 0) {
845 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
846 CpuMpData
->AddressMap
.ModeTransitionOffset
;
848 (VOID
*)CpuMpData
->WakeupBufferHigh
,
849 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
850 CpuMpData
->AddressMap
.ModeTransitionOffset
,
854 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
856 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
857 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
860 ExchangeInfo
->ModeHighMemory
= ExchangeInfo
->ModeTransitionMemory
+
861 (UINT32
)ExchangeInfo
->ModeOffset
-
862 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
863 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
867 Helper function that waits until the finished AP count reaches the specified
868 limit, or the specified timeout elapses (whichever comes first).
870 @param[in] CpuMpData Pointer to CPU MP Data.
871 @param[in] FinishedApLimit The number of finished APs to wait for.
872 @param[in] TimeLimit The number of microseconds to wait for.
875 TimedWaitForApFinish (
876 IN CPU_MP_DATA
*CpuMpData
,
877 IN UINT32 FinishedApLimit
,
882 Get available system memory below 1MB by specified size.
884 @param[in] CpuMpData The pointer to CPU MP Data structure.
887 BackupAndPrepareWakeupBuffer(
888 IN CPU_MP_DATA
*CpuMpData
892 (VOID
*) CpuMpData
->BackupBuffer
,
893 (VOID
*) CpuMpData
->WakeupBuffer
,
894 CpuMpData
->BackupBufferSize
897 (VOID
*) CpuMpData
->WakeupBuffer
,
898 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
899 CpuMpData
->AddressMap
.RendezvousFunnelSize
904 Restore wakeup buffer data.
906 @param[in] CpuMpData The pointer to CPU MP Data structure.
910 IN CPU_MP_DATA
*CpuMpData
914 (VOID
*) CpuMpData
->WakeupBuffer
,
915 (VOID
*) CpuMpData
->BackupBuffer
,
916 CpuMpData
->BackupBufferSize
921 Allocate reset vector buffer.
923 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
926 AllocateResetVector (
927 IN OUT CPU_MP_DATA
*CpuMpData
930 UINTN ApResetVectorSize
;
932 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
933 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
934 sizeof (MP_CPU_EXCHANGE_INFO
);
936 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
937 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
938 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
939 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
940 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
941 CpuMpData
->AddressMap
.ModeTransitionOffset
944 BackupAndPrepareWakeupBuffer (CpuMpData
);
948 Free AP reset vector buffer.
950 @param[in] CpuMpData The pointer to CPU MP Data structure.
954 IN CPU_MP_DATA
*CpuMpData
957 RestoreWakeupBuffer (CpuMpData
);
961 This function will be called by BSP to wakeup AP.
963 @param[in] CpuMpData Pointer to CPU MP Data
964 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
965 FALSE: Send IPI to AP by ApicId
966 @param[in] ProcessorNumber The handle number of specified processor
967 @param[in] Procedure The function to be invoked by AP
968 @param[in] ProcedureArgument The argument to be passed into AP function
972 IN CPU_MP_DATA
*CpuMpData
,
973 IN BOOLEAN Broadcast
,
974 IN UINTN ProcessorNumber
,
975 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
976 IN VOID
*ProcedureArgument OPTIONAL
979 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
981 CPU_AP_DATA
*CpuData
;
982 BOOLEAN ResetVectorRequired
;
983 CPU_INFO_IN_HOB
*CpuInfoInHob
;
985 CpuMpData
->FinishedCount
= 0;
986 ResetVectorRequired
= FALSE
;
988 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
989 CpuMpData
->InitFlag
!= ApInitDone
) {
990 ResetVectorRequired
= TRUE
;
991 AllocateResetVector (CpuMpData
);
992 FillExchangeInfoData (CpuMpData
);
993 SaveLocalApicTimerSetting (CpuMpData
);
994 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
996 // Get AP target C-state each time when waking up AP,
997 // for it maybe updated by platform again
999 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
1002 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
1005 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1006 if (Index
!= CpuMpData
->BspNumber
) {
1007 CpuData
= &CpuMpData
->CpuData
[Index
];
1008 CpuData
->ApFunction
= (UINTN
) Procedure
;
1009 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1010 SetApState (CpuData
, CpuStateReady
);
1011 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1012 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1016 if (ResetVectorRequired
) {
1020 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1022 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1024 // Here support two methods to collect AP count through adjust
1025 // PcdCpuApInitTimeOutInMicroSeconds values.
1027 // one way is set a value to just let the first AP to start the
1028 // initialization, then through the later while loop to wait all Aps
1029 // finsh the initialization.
1030 // The other way is set a value to let all APs finished the initialzation.
1031 // In this case, the later while loop is useless.
1033 TimedWaitForApFinish (
1035 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1036 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1039 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1044 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1046 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1047 CpuData
= &CpuMpData
->CpuData
[Index
];
1048 if (Index
!= CpuMpData
->BspNumber
) {
1049 WaitApWakeup (CpuData
->StartupApSignal
);
1054 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1055 CpuData
->ApFunction
= (UINTN
) Procedure
;
1056 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1057 SetApState (CpuData
, CpuStateReady
);
1059 // Wakeup specified AP
1061 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1062 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1063 if (ResetVectorRequired
) {
1064 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1066 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1067 (UINT32
) ExchangeInfo
->BufferStart
1071 // Wait specified AP waken up
1073 WaitApWakeup (CpuData
->StartupApSignal
);
1076 if (ResetVectorRequired
) {
1077 FreeResetVector (CpuMpData
);
1082 Calculate timeout value and return the current performance counter value.
1084 Calculate the number of performance counter ticks required for a timeout.
1085 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1088 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1089 @param[out] CurrentTime Returns the current value of the performance counter.
1091 @return Expected time stamp counter for timeout.
1092 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1098 IN UINTN TimeoutInMicroseconds
,
1099 OUT UINT64
*CurrentTime
1102 UINT64 TimeoutInSeconds
;
1103 UINT64 TimestampCounterFreq
;
1106 // Read the current value of the performance counter
1108 *CurrentTime
= GetPerformanceCounter ();
1111 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1114 if (TimeoutInMicroseconds
== 0) {
1119 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1122 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1125 // Check the potential overflow before calculate the number of ticks for the timeout value.
1127 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1129 // Convert microseconds into seconds if direct multiplication overflows
1131 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1133 // Assertion if the final tick count exceeds MAX_UINT64
1135 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1136 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1139 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1140 // it by 1,000,000, to get the number of ticks for the timeout value.
1144 TimestampCounterFreq
,
1145 TimeoutInMicroseconds
1153 Checks whether timeout expires.
1155 Check whether the number of elapsed performance counter ticks required for
1156 a timeout condition has been reached.
1157 If Timeout is zero, which means infinity, return value is always FALSE.
1159 @param[in, out] PreviousTime On input, the value of the performance counter
1160 when it was last read.
1161 On output, the current value of the performance
1163 @param[in] TotalTime The total amount of elapsed time in performance
1165 @param[in] Timeout The number of performance counter ticks required
1166 to reach a timeout condition.
1168 @retval TRUE A timeout condition has been reached.
1169 @retval FALSE A timeout condition has not been reached.
1174 IN OUT UINT64
*PreviousTime
,
1175 IN UINT64
*TotalTime
,
1188 GetPerformanceCounterProperties (&Start
, &End
);
1189 Cycle
= End
- Start
;
1194 CurrentTime
= GetPerformanceCounter();
1195 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1202 *TotalTime
+= Delta
;
1203 *PreviousTime
= CurrentTime
;
1204 if (*TotalTime
> Timeout
) {
1211 Helper function that waits until the finished AP count reaches the specified
1212 limit, or the specified timeout elapses (whichever comes first).
1214 @param[in] CpuMpData Pointer to CPU MP Data.
1215 @param[in] FinishedApLimit The number of finished APs to wait for.
1216 @param[in] TimeLimit The number of microseconds to wait for.
1219 TimedWaitForApFinish (
1220 IN CPU_MP_DATA
*CpuMpData
,
1221 IN UINT32 FinishedApLimit
,
1226 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1227 // "infinity", so check for (TimeLimit == 0) explicitly.
1229 if (TimeLimit
== 0) {
1233 CpuMpData
->TotalTime
= 0;
1234 CpuMpData
->ExpectedTime
= CalculateTimeout (
1236 &CpuMpData
->CurrentTime
1238 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1240 &CpuMpData
->CurrentTime
,
1241 &CpuMpData
->TotalTime
,
1242 CpuMpData
->ExpectedTime
1247 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1250 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1253 DivU64x64Remainder (
1254 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1255 GetPerformanceCounterProperties (NULL
, NULL
),
1263 Reset an AP to Idle state.
1265 Any task being executed by the AP will be aborted and the AP
1266 will be waiting for a new task in Wait-For-SIPI state.
1268 @param[in] ProcessorNumber The handle number of processor.
1271 ResetProcessorToIdleState (
1272 IN UINTN ProcessorNumber
1275 CPU_MP_DATA
*CpuMpData
;
1277 CpuMpData
= GetCpuMpData ();
1279 CpuMpData
->InitFlag
= ApInitReconfig
;
1280 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
1281 while (CpuMpData
->FinishedCount
< 1) {
1284 CpuMpData
->InitFlag
= ApInitDone
;
1286 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1290 Searches for the next waiting AP.
1292 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1294 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1296 @retval EFI_SUCCESS The next waiting AP has been found.
1297 @retval EFI_NOT_FOUND No waiting AP exists.
1301 GetNextWaitingProcessorNumber (
1302 OUT UINTN
*NextProcessorNumber
1305 UINTN ProcessorNumber
;
1306 CPU_MP_DATA
*CpuMpData
;
1308 CpuMpData
= GetCpuMpData ();
1310 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1311 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1312 *NextProcessorNumber
= ProcessorNumber
;
1317 return EFI_NOT_FOUND
;
1320 /** Checks status of specified AP.
1322 This function checks whether the specified AP has finished the task assigned
1323 by StartupThisAP(), and whether timeout expires.
1325 @param[in] ProcessorNumber The handle number of processor.
1327 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1328 @retval EFI_TIMEOUT The timeout expires.
1329 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1333 IN UINTN ProcessorNumber
1336 CPU_MP_DATA
*CpuMpData
;
1337 CPU_AP_DATA
*CpuData
;
1339 CpuMpData
= GetCpuMpData ();
1340 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1343 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1344 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1345 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1348 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1350 if (GetApState(CpuData
) == CpuStateFinished
) {
1351 if (CpuData
->Finished
!= NULL
) {
1352 *(CpuData
->Finished
) = TRUE
;
1354 SetApState (CpuData
, CpuStateIdle
);
1358 // If timeout expires for StartupThisAP(), report timeout.
1360 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1361 if (CpuData
->Finished
!= NULL
) {
1362 *(CpuData
->Finished
) = FALSE
;
1365 // Reset failed AP to idle state
1367 ResetProcessorToIdleState (ProcessorNumber
);
1372 return EFI_NOT_READY
;
1376 Checks status of all APs.
1378 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1379 and whether timeout expires.
1381 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1382 @retval EFI_TIMEOUT The timeout expires.
1383 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1390 UINTN ProcessorNumber
;
1391 UINTN NextProcessorNumber
;
1394 CPU_MP_DATA
*CpuMpData
;
1395 CPU_AP_DATA
*CpuData
;
1397 CpuMpData
= GetCpuMpData ();
1399 NextProcessorNumber
= 0;
1402 // Go through all APs that are responsible for the StartupAllAPs().
1404 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1405 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1409 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1411 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1412 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1413 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1415 if (GetApState(CpuData
) == CpuStateFinished
) {
1416 CpuMpData
->RunningCount
++;
1417 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1418 SetApState(CpuData
, CpuStateIdle
);
1421 // If in Single Thread mode, then search for the next waiting AP for execution.
1423 if (CpuMpData
->SingleThread
) {
1424 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1426 if (!EFI_ERROR (Status
)) {
1430 (UINT32
) NextProcessorNumber
,
1431 CpuMpData
->Procedure
,
1432 CpuMpData
->ProcArguments
1440 // If all APs finish, return EFI_SUCCESS.
1442 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1447 // If timeout expires, report timeout.
1450 &CpuMpData
->CurrentTime
,
1451 &CpuMpData
->TotalTime
,
1452 CpuMpData
->ExpectedTime
)
1455 // If FailedCpuList is not NULL, record all failed APs in it.
1457 if (CpuMpData
->FailedCpuList
!= NULL
) {
1458 *CpuMpData
->FailedCpuList
=
1459 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1460 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1464 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1466 // Check whether this processor is responsible for StartupAllAPs().
1468 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1470 // Reset failed APs to idle state
1472 ResetProcessorToIdleState (ProcessorNumber
);
1473 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1474 if (CpuMpData
->FailedCpuList
!= NULL
) {
1475 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1479 if (CpuMpData
->FailedCpuList
!= NULL
) {
1480 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1484 return EFI_NOT_READY
;
1488 MP Initialize Library initialization.
1490 This service will allocate AP reset vector and wakeup all APs to do APs
1493 This service must be invoked before all other MP Initialize Library
1494 service are invoked.
1496 @retval EFI_SUCCESS MP initialization succeeds.
1497 @retval Others MP initialization fails.
1502 MpInitLibInitialize (
1506 CPU_MP_DATA
*OldCpuMpData
;
1507 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1508 UINT32 MaxLogicalProcessorNumber
;
1510 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1511 CPU_VOLATILE_REGISTERS VolatileRegisters
;
1513 UINT32 MonitorFilterSize
;
1516 CPU_MP_DATA
*CpuMpData
;
1518 UINT8
*MonitorBuffer
;
1520 UINTN ApResetVectorSize
;
1521 UINTN BackupBufferAddr
;
1524 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1525 if (OldCpuMpData
== NULL
) {
1526 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1528 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1530 ASSERT (MaxLogicalProcessorNumber
!= 0);
1532 AsmGetAddressMap (&AddressMap
);
1533 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1534 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1535 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1538 // Save BSP's Control registers for APs
1540 SaveVolatileRegisters (&VolatileRegisters
);
1542 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1543 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1544 BufferSize
+= ApResetVectorSize
;
1545 BufferSize
= ALIGN_VALUE (BufferSize
, 8);
1546 BufferSize
+= VolatileRegisters
.Idtr
.Limit
+ 1;
1547 BufferSize
+= sizeof (CPU_MP_DATA
);
1548 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1549 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1550 ASSERT (MpBuffer
!= NULL
);
1551 ZeroMem (MpBuffer
, BufferSize
);
1552 Buffer
= (UINTN
) MpBuffer
;
1555 // The layout of the Buffer is as below:
1557 // +--------------------+ <-- Buffer
1559 // +--------------------+ <-- MonitorBuffer
1560 // AP Monitor Filters (N)
1561 // +--------------------+ <-- BackupBufferAddr (CpuMpData->BackupBuffer)
1563 // +--------------------+
1565 // +--------------------+ <-- ApIdtBase (8-byte boundary)
1566 // AP IDT All APs share one separate IDT. So AP can get address of CPU_MP_DATA from IDT Base.
1567 // +--------------------+ <-- CpuMpData
1569 // +--------------------+ <-- CpuMpData->CpuData
1571 // +--------------------+ <-- CpuMpData->CpuInfoInHob
1572 // CPU_INFO_IN_HOB (N)
1573 // +--------------------+
1575 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1576 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1577 ApIdtBase
= ALIGN_VALUE (BackupBufferAddr
+ ApResetVectorSize
, 8);
1578 CpuMpData
= (CPU_MP_DATA
*) (ApIdtBase
+ VolatileRegisters
.Idtr
.Limit
+ 1);
1579 CpuMpData
->Buffer
= Buffer
;
1580 CpuMpData
->CpuApStackSize
= ApStackSize
;
1581 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1582 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1583 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1584 CpuMpData
->CpuCount
= 1;
1585 CpuMpData
->BspNumber
= 0;
1586 CpuMpData
->WaitEvent
= NULL
;
1587 CpuMpData
->SwitchBspFlag
= FALSE
;
1588 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1589 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1590 CpuMpData
->MicrocodePatchAddress
= PcdGet64 (PcdCpuMicrocodePatchAddress
);
1591 CpuMpData
->MicrocodePatchRegionSize
= PcdGet64 (PcdCpuMicrocodePatchRegionSize
);
1592 InitializeSpinLock(&CpuMpData
->MpLock
);
1595 // Make sure no memory usage outside of the allocated buffer.
1597 ASSERT ((CpuMpData
->CpuInfoInHob
+ sizeof (CPU_INFO_IN_HOB
) * MaxLogicalProcessorNumber
) ==
1598 Buffer
+ BufferSize
);
1601 // Duplicate BSP's IDT to APs.
1602 // All APs share one separate IDT. So AP can get the address of CpuMpData by using IDTR.BASE + IDTR.LIMIT + 1
1604 CopyMem ((VOID
*)ApIdtBase
, (VOID
*)VolatileRegisters
.Idtr
.Base
, VolatileRegisters
.Idtr
.Limit
+ 1);
1605 VolatileRegisters
.Idtr
.Base
= ApIdtBase
;
1606 CopyMem (&CpuMpData
->CpuData
[0].VolatileRegisters
, &VolatileRegisters
, sizeof (VolatileRegisters
));
1608 // Set BSP basic information
1610 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1612 // Save assembly code information
1614 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1616 // Finally set AP loop mode
1618 CpuMpData
->ApLoopMode
= ApLoopMode
;
1619 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1621 // Set up APs wakeup signal buffer
1623 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1624 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1625 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1628 // Load Microcode on BSP
1630 MicrocodeDetect (CpuMpData
);
1632 // Store BSP's MTRR setting
1634 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1636 // Enable the local APIC for Virtual Wire Mode.
1638 ProgramVirtualWireMode ();
1640 if (OldCpuMpData
== NULL
) {
1641 if (MaxLogicalProcessorNumber
> 1) {
1643 // Wakeup all APs and calculate the processor count in system
1645 CollectProcessorCount (CpuMpData
);
1649 // APs have been wakeup before, just get the CPU Information
1652 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1653 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1654 CpuMpData
->InitFlag
= ApInitReconfig
;
1655 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1656 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1657 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1658 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1659 if (CpuInfoInHob
[Index
].InitialApicId
>= 255 || Index
> 254) {
1660 CpuMpData
->X2ApicEnable
= TRUE
;
1662 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1663 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1664 CopyMem (&CpuMpData
->CpuData
[Index
].VolatileRegisters
, &VolatileRegisters
, sizeof (CPU_VOLATILE_REGISTERS
));
1666 if (MaxLogicalProcessorNumber
> 1) {
1668 // Wakeup APs to do some AP initialize sync
1670 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1672 // Wait for all APs finished initialization
1674 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1677 CpuMpData
->InitFlag
= ApInitDone
;
1678 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1679 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1685 // Initialize global data for MP support
1687 InitMpGlobalData (CpuMpData
);
1693 Gets detailed MP-related information on the requested processor at the
1694 instant this call is made. This service may only be called from the BSP.
1696 @param[in] ProcessorNumber The handle number of processor.
1697 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1698 the requested processor is deposited.
1699 @param[out] HealthData Return processor health data.
1701 @retval EFI_SUCCESS Processor information was returned.
1702 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1703 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1704 @retval EFI_NOT_FOUND The processor with the handle specified by
1705 ProcessorNumber does not exist in the platform.
1706 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1711 MpInitLibGetProcessorInfo (
1712 IN UINTN ProcessorNumber
,
1713 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1714 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1717 CPU_MP_DATA
*CpuMpData
;
1719 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1721 CpuMpData
= GetCpuMpData ();
1722 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1725 // Check whether caller processor is BSP
1727 MpInitLibWhoAmI (&CallerNumber
);
1728 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1729 return EFI_DEVICE_ERROR
;
1732 if (ProcessorInfoBuffer
== NULL
) {
1733 return EFI_INVALID_PARAMETER
;
1736 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1737 return EFI_NOT_FOUND
;
1740 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1741 ProcessorInfoBuffer
->StatusFlag
= 0;
1742 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1743 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1745 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1746 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1748 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1749 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1751 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1755 // Get processor location information
1757 GetProcessorLocationByApicId (
1758 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1759 &ProcessorInfoBuffer
->Location
.Package
,
1760 &ProcessorInfoBuffer
->Location
.Core
,
1761 &ProcessorInfoBuffer
->Location
.Thread
1764 if (HealthData
!= NULL
) {
1765 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1772 Worker function to switch the requested AP to be the BSP from that point onward.
1774 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1775 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1776 enabled AP. Otherwise, it will be disabled.
1778 @retval EFI_SUCCESS BSP successfully switched.
1779 @retval others Failed to switch BSP.
1784 IN UINTN ProcessorNumber
,
1785 IN BOOLEAN EnableOldBSP
1788 CPU_MP_DATA
*CpuMpData
;
1791 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1792 BOOLEAN OldInterruptState
;
1793 BOOLEAN OldTimerInterruptState
;
1796 // Save and Disable Local APIC timer interrupt
1798 OldTimerInterruptState
= GetApicTimerInterruptState ();
1799 DisableApicTimerInterrupt ();
1801 // Before send both BSP and AP to a procedure to exchange their roles,
1802 // interrupt must be disabled. This is because during the exchange role
1803 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1804 // be corrupted, since interrupt return address will be pushed to stack
1807 OldInterruptState
= SaveAndDisableInterrupts ();
1810 // Mask LINT0 & LINT1 for the old BSP
1812 DisableLvtInterrupts ();
1814 CpuMpData
= GetCpuMpData ();
1817 // Check whether caller processor is BSP
1819 MpInitLibWhoAmI (&CallerNumber
);
1820 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1821 return EFI_DEVICE_ERROR
;
1824 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1825 return EFI_NOT_FOUND
;
1829 // Check whether specified AP is disabled
1831 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1832 if (State
== CpuStateDisabled
) {
1833 return EFI_INVALID_PARAMETER
;
1837 // Check whether ProcessorNumber specifies the current BSP
1839 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1840 return EFI_INVALID_PARAMETER
;
1844 // Check whether specified AP is busy
1846 if (State
== CpuStateBusy
) {
1847 return EFI_NOT_READY
;
1850 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1851 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1852 CpuMpData
->SwitchBspFlag
= TRUE
;
1853 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1856 // Clear the BSP bit of MSR_IA32_APIC_BASE
1858 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1859 ApicBaseMsr
.Bits
.BSP
= 0;
1860 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1863 // Need to wakeUp AP (future BSP).
1865 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1867 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1870 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1872 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1873 ApicBaseMsr
.Bits
.BSP
= 1;
1874 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1875 ProgramVirtualWireMode ();
1878 // Wait for old BSP finished AP task
1880 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1884 CpuMpData
->SwitchBspFlag
= FALSE
;
1886 // Set old BSP enable state
1888 if (!EnableOldBSP
) {
1889 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1891 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1894 // Save new BSP number
1896 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1899 // Restore interrupt state.
1901 SetInterruptState (OldInterruptState
);
1903 if (OldTimerInterruptState
) {
1904 EnableApicTimerInterrupt ();
1911 Worker function to let the caller enable or disable an AP from this point onward.
1912 This service may only be called from the BSP.
1914 @param[in] ProcessorNumber The handle number of AP.
1915 @param[in] EnableAP Specifies the new state for the processor for
1916 enabled, FALSE for disabled.
1917 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1918 the new health status of the AP.
1920 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1921 @retval others Failed to Enable/Disable AP.
1925 EnableDisableApWorker (
1926 IN UINTN ProcessorNumber
,
1927 IN BOOLEAN EnableAP
,
1928 IN UINT32
*HealthFlag OPTIONAL
1931 CPU_MP_DATA
*CpuMpData
;
1934 CpuMpData
= GetCpuMpData ();
1937 // Check whether caller processor is BSP
1939 MpInitLibWhoAmI (&CallerNumber
);
1940 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1941 return EFI_DEVICE_ERROR
;
1944 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1945 return EFI_INVALID_PARAMETER
;
1948 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1949 return EFI_NOT_FOUND
;
1953 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1955 ResetProcessorToIdleState (ProcessorNumber
);
1958 if (HealthFlag
!= NULL
) {
1959 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1960 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1967 This return the handle number for the calling processor. This service may be
1968 called from the BSP and APs.
1970 @param[out] ProcessorNumber Pointer to the handle number of AP.
1971 The range is from 0 to the total number of
1972 logical processors minus 1. The total number of
1973 logical processors can be retrieved by
1974 MpInitLibGetNumberOfProcessors().
1976 @retval EFI_SUCCESS The current processor handle number was returned
1978 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1979 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1985 OUT UINTN
*ProcessorNumber
1988 CPU_MP_DATA
*CpuMpData
;
1990 if (ProcessorNumber
== NULL
) {
1991 return EFI_INVALID_PARAMETER
;
1994 CpuMpData
= GetCpuMpData ();
1996 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
2000 Retrieves the number of logical processor in the platform and the number of
2001 those logical processors that are enabled on this boot. This service may only
2002 be called from the BSP.
2004 @param[out] NumberOfProcessors Pointer to the total number of logical
2005 processors in the system, including the BSP
2007 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
2008 processors that exist in system, including
2011 @retval EFI_SUCCESS The number of logical processors and enabled
2012 logical processors was retrieved.
2013 @retval EFI_DEVICE_ERROR The calling processor is an AP.
2014 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
2016 @retval EFI_NOT_READY MP Initialize Library is not initialized.
2021 MpInitLibGetNumberOfProcessors (
2022 OUT UINTN
*NumberOfProcessors
, OPTIONAL
2023 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
2026 CPU_MP_DATA
*CpuMpData
;
2028 UINTN ProcessorNumber
;
2029 UINTN EnabledProcessorNumber
;
2032 CpuMpData
= GetCpuMpData ();
2034 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
2035 return EFI_INVALID_PARAMETER
;
2039 // Check whether caller processor is BSP
2041 MpInitLibWhoAmI (&CallerNumber
);
2042 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2043 return EFI_DEVICE_ERROR
;
2046 ProcessorNumber
= CpuMpData
->CpuCount
;
2047 EnabledProcessorNumber
= 0;
2048 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2049 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2050 EnabledProcessorNumber
++;
2054 if (NumberOfProcessors
!= NULL
) {
2055 *NumberOfProcessors
= ProcessorNumber
;
2057 if (NumberOfEnabledProcessors
!= NULL
) {
2058 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2066 Worker function to execute a caller provided function on all enabled APs.
2068 @param[in] Procedure A pointer to the function to be run on
2069 enabled APs of the system.
2070 @param[in] SingleThread If TRUE, then all the enabled APs execute
2071 the function specified by Procedure one by
2072 one, in ascending order of processor handle
2073 number. If FALSE, then all the enabled APs
2074 execute the function specified by Procedure
2076 @param[in] WaitEvent The event created by the caller with CreateEvent()
2078 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2079 APs to return from Procedure, either for
2080 blocking or non-blocking mode.
2081 @param[in] ProcedureArgument The parameter passed into Procedure for
2083 @param[out] FailedCpuList If all APs finish successfully, then its
2084 content is set to NULL. If not all APs
2085 finish before timeout expires, then its
2086 content is set to address of the buffer
2087 holding handle numbers of the failed APs.
2089 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2090 the timeout expired.
2091 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2093 @retval others Failed to Startup all APs.
2097 StartupAllAPsWorker (
2098 IN EFI_AP_PROCEDURE Procedure
,
2099 IN BOOLEAN SingleThread
,
2100 IN EFI_EVENT WaitEvent OPTIONAL
,
2101 IN UINTN TimeoutInMicroseconds
,
2102 IN VOID
*ProcedureArgument OPTIONAL
,
2103 OUT UINTN
**FailedCpuList OPTIONAL
2107 CPU_MP_DATA
*CpuMpData
;
2108 UINTN ProcessorCount
;
2109 UINTN ProcessorNumber
;
2111 CPU_AP_DATA
*CpuData
;
2112 BOOLEAN HasEnabledAp
;
2115 CpuMpData
= GetCpuMpData ();
2117 if (FailedCpuList
!= NULL
) {
2118 *FailedCpuList
= NULL
;
2121 if (CpuMpData
->CpuCount
== 1) {
2122 return EFI_NOT_STARTED
;
2125 if (Procedure
== NULL
) {
2126 return EFI_INVALID_PARAMETER
;
2130 // Check whether caller processor is BSP
2132 MpInitLibWhoAmI (&CallerNumber
);
2133 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2134 return EFI_DEVICE_ERROR
;
2140 CheckAndUpdateApsStatus ();
2142 ProcessorCount
= CpuMpData
->CpuCount
;
2143 HasEnabledAp
= FALSE
;
2145 // Check whether all enabled APs are idle.
2146 // If any enabled AP is not idle, return EFI_NOT_READY.
2148 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2149 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2150 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2151 ApState
= GetApState (CpuData
);
2152 if (ApState
!= CpuStateDisabled
) {
2153 HasEnabledAp
= TRUE
;
2154 if (ApState
!= CpuStateIdle
) {
2156 // If any enabled APs are busy, return EFI_NOT_READY.
2158 return EFI_NOT_READY
;
2164 if (!HasEnabledAp
) {
2166 // If no enabled AP exists, return EFI_NOT_STARTED.
2168 return EFI_NOT_STARTED
;
2171 CpuMpData
->StartCount
= 0;
2172 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2173 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2174 CpuData
->Waiting
= FALSE
;
2175 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2176 if (CpuData
->State
== CpuStateIdle
) {
2178 // Mark this processor as responsible for current calling.
2180 CpuData
->Waiting
= TRUE
;
2181 CpuMpData
->StartCount
++;
2186 CpuMpData
->Procedure
= Procedure
;
2187 CpuMpData
->ProcArguments
= ProcedureArgument
;
2188 CpuMpData
->SingleThread
= SingleThread
;
2189 CpuMpData
->FinishedCount
= 0;
2190 CpuMpData
->RunningCount
= 0;
2191 CpuMpData
->FailedCpuList
= FailedCpuList
;
2192 CpuMpData
->ExpectedTime
= CalculateTimeout (
2193 TimeoutInMicroseconds
,
2194 &CpuMpData
->CurrentTime
2196 CpuMpData
->TotalTime
= 0;
2197 CpuMpData
->WaitEvent
= WaitEvent
;
2199 if (!SingleThread
) {
2200 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
2202 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2203 if (ProcessorNumber
== CallerNumber
) {
2206 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2207 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2213 Status
= EFI_SUCCESS
;
2214 if (WaitEvent
== NULL
) {
2216 Status
= CheckAllAPs ();
2217 } while (Status
== EFI_NOT_READY
);
2224 Worker function to let the caller get one enabled AP to execute a caller-provided
2227 @param[in] Procedure A pointer to the function to be run on
2228 enabled APs of the system.
2229 @param[in] ProcessorNumber The handle number of the AP.
2230 @param[in] WaitEvent The event created by the caller with CreateEvent()
2232 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2233 APs to return from Procedure, either for
2234 blocking or non-blocking mode.
2235 @param[in] ProcedureArgument The parameter passed into Procedure for
2237 @param[out] Finished If AP returns from Procedure before the
2238 timeout expires, its content is set to TRUE.
2239 Otherwise, the value is set to FALSE.
2241 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2242 the timeout expires.
2243 @retval others Failed to Startup AP.
2247 StartupThisAPWorker (
2248 IN EFI_AP_PROCEDURE Procedure
,
2249 IN UINTN ProcessorNumber
,
2250 IN EFI_EVENT WaitEvent OPTIONAL
,
2251 IN UINTN TimeoutInMicroseconds
,
2252 IN VOID
*ProcedureArgument OPTIONAL
,
2253 OUT BOOLEAN
*Finished OPTIONAL
2257 CPU_MP_DATA
*CpuMpData
;
2258 CPU_AP_DATA
*CpuData
;
2261 CpuMpData
= GetCpuMpData ();
2263 if (Finished
!= NULL
) {
2268 // Check whether caller processor is BSP
2270 MpInitLibWhoAmI (&CallerNumber
);
2271 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2272 return EFI_DEVICE_ERROR
;
2276 // Check whether processor with the handle specified by ProcessorNumber exists
2278 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2279 return EFI_NOT_FOUND
;
2283 // Check whether specified processor is BSP
2285 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2286 return EFI_INVALID_PARAMETER
;
2290 // Check parameter Procedure
2292 if (Procedure
== NULL
) {
2293 return EFI_INVALID_PARAMETER
;
2299 CheckAndUpdateApsStatus ();
2302 // Check whether specified AP is disabled
2304 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2305 return EFI_INVALID_PARAMETER
;
2309 // If WaitEvent is not NULL, execute in non-blocking mode.
2310 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2311 // CheckAPsStatus() will check completion and timeout periodically.
2313 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2314 CpuData
->WaitEvent
= WaitEvent
;
2315 CpuData
->Finished
= Finished
;
2316 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2317 CpuData
->TotalTime
= 0;
2319 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2322 // If WaitEvent is NULL, execute in blocking mode.
2323 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2325 Status
= EFI_SUCCESS
;
2326 if (WaitEvent
== NULL
) {
2328 Status
= CheckThisAP (ProcessorNumber
);
2329 } while (Status
== EFI_NOT_READY
);
2336 Get pointer to CPU MP Data structure from GUIDed HOB.
2338 @return The pointer to CPU MP Data structure.
2341 GetCpuMpDataFromGuidedHob (
2345 EFI_HOB_GUID_TYPE
*GuidHob
;
2347 CPU_MP_DATA
*CpuMpData
;
2350 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2351 if (GuidHob
!= NULL
) {
2352 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2353 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);