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 // Sync BSP's Control registers to APs
620 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
621 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
622 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
625 // Execute AP function if AP is ready
627 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
629 // Clear AP start-up signal when AP waken up
631 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
632 InterlockedCompareExchange32 (
633 (UINT32
*) ApStartupSignalBuffer
,
637 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
639 // Restore AP's volatile registers saved
641 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
644 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
645 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
646 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
647 if (Procedure
!= NULL
) {
648 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
650 // Enable source debugging on AP function
654 // Invoke AP function here
656 Procedure (Parameter
);
657 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
658 if (CpuMpData
->SwitchBspFlag
) {
660 // Re-get the processor number due to BSP/AP maybe exchange in AP function
662 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
663 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
664 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
665 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
666 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
668 if (CpuInfoInHob
[ProcessorNumber
].ApicId
!= GetApicId () ||
669 CpuInfoInHob
[ProcessorNumber
].InitialApicId
!= GetInitialApicId ()) {
670 if (CurrentApicMode
!= GetApicMode ()) {
672 // If APIC mode change happened during AP function execution,
673 // we do not support APIC ID value changed.
679 // Re-get the CPU APICID and Initial APICID if they are changed
681 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
682 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
687 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
692 // AP finished executing C code
694 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
695 InterlockedDecrement ((UINT32
*) &CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
);
698 // Place AP is specified loop mode
700 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
702 // Save AP volatile registers
704 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
706 // Place AP in HLT-loop
709 DisableInterrupts ();
715 DisableInterrupts ();
716 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
718 // Place AP in MWAIT-loop
720 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
721 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
723 // Check AP start-up signal again.
724 // If AP start-up signal is not set, place AP into
725 // the specified C-state
727 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
729 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
731 // Place AP in Run-loop
739 // If AP start-up signal is written, AP is waken up
740 // otherwise place AP in loop again
742 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
750 Wait for AP wakeup and write AP start-up signal till AP is waken up.
752 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
756 IN
volatile UINT32
*ApStartupSignalBuffer
760 // If AP is waken up, StartupApSignal should be cleared.
761 // Otherwise, write StartupApSignal again till AP waken up.
763 while (InterlockedCompareExchange32 (
764 (UINT32
*) ApStartupSignalBuffer
,
773 This function will fill the exchange info structure.
775 @param[in] CpuMpData Pointer to CPU MP Data
779 FillExchangeInfoData (
780 IN CPU_MP_DATA
*CpuMpData
783 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
785 IA32_SEGMENT_DESCRIPTOR
*Selector
;
787 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
788 ExchangeInfo
->Lock
= 0;
789 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
790 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
791 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
792 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
794 ExchangeInfo
->CodeSegment
= AsmReadCs ();
795 ExchangeInfo
->DataSegment
= AsmReadDs ();
797 ExchangeInfo
->Cr3
= AsmReadCr3 ();
799 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
800 ExchangeInfo
->ApIndex
= 0;
801 ExchangeInfo
->NumApsExecuting
= 0;
802 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
803 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
804 ExchangeInfo
->CpuMpData
= CpuMpData
;
806 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
808 ExchangeInfo
->InitializeFloatingPointUnitsAddress
= (UINTN
)InitializeFloatingPointUnits
;
811 // Get the BSP's data of GDT and IDT
813 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
814 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
817 // Find a 32-bit code segment
819 Selector
= (IA32_SEGMENT_DESCRIPTOR
*)ExchangeInfo
->GdtrProfile
.Base
;
820 Size
= ExchangeInfo
->GdtrProfile
.Limit
+ 1;
822 if (Selector
->Bits
.L
== 0 && Selector
->Bits
.Type
>= 8) {
823 ExchangeInfo
->ModeTransitionSegment
=
824 (UINT16
)((UINTN
)Selector
- ExchangeInfo
->GdtrProfile
.Base
);
828 Size
-= sizeof (IA32_SEGMENT_DESCRIPTOR
);
832 // Copy all 32-bit code and 64-bit code into memory with type of
833 // EfiBootServicesCode to avoid page fault if NX memory protection is enabled.
835 if (CpuMpData
->WakeupBufferHigh
!= 0) {
836 Size
= CpuMpData
->AddressMap
.RendezvousFunnelSize
-
837 CpuMpData
->AddressMap
.ModeTransitionOffset
;
839 (VOID
*)CpuMpData
->WakeupBufferHigh
,
840 CpuMpData
->AddressMap
.RendezvousFunnelAddress
+
841 CpuMpData
->AddressMap
.ModeTransitionOffset
,
845 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
;
846 ExchangeInfo
->ModeHighMemory
= (UINT32
)CpuMpData
->WakeupBufferHigh
+
847 (UINT32
)ExchangeInfo
->ModeOffset
-
848 (UINT32
)CpuMpData
->AddressMap
.ModeTransitionOffset
;
849 ExchangeInfo
->ModeHighSegment
= (UINT16
)ExchangeInfo
->CodeSegment
;
851 ExchangeInfo
->ModeTransitionMemory
= (UINT32
)
852 (ExchangeInfo
->BufferStart
+ CpuMpData
->AddressMap
.ModeTransitionOffset
);
857 Helper function that waits until the finished AP count reaches the specified
858 limit, or the specified timeout elapses (whichever comes first).
860 @param[in] CpuMpData Pointer to CPU MP Data.
861 @param[in] FinishedApLimit The number of finished APs to wait for.
862 @param[in] TimeLimit The number of microseconds to wait for.
865 TimedWaitForApFinish (
866 IN CPU_MP_DATA
*CpuMpData
,
867 IN UINT32 FinishedApLimit
,
872 Get available system memory below 1MB by specified size.
874 @param[in] CpuMpData The pointer to CPU MP Data structure.
877 BackupAndPrepareWakeupBuffer(
878 IN CPU_MP_DATA
*CpuMpData
882 (VOID
*) CpuMpData
->BackupBuffer
,
883 (VOID
*) CpuMpData
->WakeupBuffer
,
884 CpuMpData
->BackupBufferSize
887 (VOID
*) CpuMpData
->WakeupBuffer
,
888 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
889 CpuMpData
->AddressMap
.RendezvousFunnelSize
894 Restore wakeup buffer data.
896 @param[in] CpuMpData The pointer to CPU MP Data structure.
900 IN CPU_MP_DATA
*CpuMpData
904 (VOID
*) CpuMpData
->WakeupBuffer
,
905 (VOID
*) CpuMpData
->BackupBuffer
,
906 CpuMpData
->BackupBufferSize
911 Allocate reset vector buffer.
913 @param[in, out] CpuMpData The pointer to CPU MP Data structure.
916 AllocateResetVector (
917 IN OUT CPU_MP_DATA
*CpuMpData
920 UINTN ApResetVectorSize
;
922 if (CpuMpData
->WakeupBuffer
== (UINTN
) -1) {
923 ApResetVectorSize
= CpuMpData
->AddressMap
.RendezvousFunnelSize
+
924 sizeof (MP_CPU_EXCHANGE_INFO
);
926 CpuMpData
->WakeupBuffer
= GetWakeupBuffer (ApResetVectorSize
);
927 CpuMpData
->MpCpuExchangeInfo
= (MP_CPU_EXCHANGE_INFO
*) (UINTN
)
928 (CpuMpData
->WakeupBuffer
+ CpuMpData
->AddressMap
.RendezvousFunnelSize
);
929 CpuMpData
->WakeupBufferHigh
= GetModeTransitionBuffer (
930 CpuMpData
->AddressMap
.RendezvousFunnelSize
-
931 CpuMpData
->AddressMap
.ModeTransitionOffset
934 BackupAndPrepareWakeupBuffer (CpuMpData
);
938 Free AP reset vector buffer.
940 @param[in] CpuMpData The pointer to CPU MP Data structure.
944 IN CPU_MP_DATA
*CpuMpData
947 RestoreWakeupBuffer (CpuMpData
);
951 This function will be called by BSP to wakeup AP.
953 @param[in] CpuMpData Pointer to CPU MP Data
954 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
955 FALSE: Send IPI to AP by ApicId
956 @param[in] ProcessorNumber The handle number of specified processor
957 @param[in] Procedure The function to be invoked by AP
958 @param[in] ProcedureArgument The argument to be passed into AP function
962 IN CPU_MP_DATA
*CpuMpData
,
963 IN BOOLEAN Broadcast
,
964 IN UINTN ProcessorNumber
,
965 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
966 IN VOID
*ProcedureArgument OPTIONAL
969 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
971 CPU_AP_DATA
*CpuData
;
972 BOOLEAN ResetVectorRequired
;
973 CPU_INFO_IN_HOB
*CpuInfoInHob
;
975 CpuMpData
->FinishedCount
= 0;
976 ResetVectorRequired
= FALSE
;
978 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
979 CpuMpData
->InitFlag
!= ApInitDone
) {
980 ResetVectorRequired
= TRUE
;
981 AllocateResetVector (CpuMpData
);
982 FillExchangeInfoData (CpuMpData
);
983 SaveLocalApicTimerSetting (CpuMpData
);
984 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
986 // Get AP target C-state each time when waking up AP,
987 // for it maybe updated by platform again
989 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
992 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
995 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
996 if (Index
!= CpuMpData
->BspNumber
) {
997 CpuData
= &CpuMpData
->CpuData
[Index
];
998 CpuData
->ApFunction
= (UINTN
) Procedure
;
999 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1000 SetApState (CpuData
, CpuStateReady
);
1001 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
1002 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1006 if (ResetVectorRequired
) {
1010 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
1012 if (CpuMpData
->InitFlag
== ApInitConfig
) {
1014 // Here support two methods to collect AP count through adjust
1015 // PcdCpuApInitTimeOutInMicroSeconds values.
1017 // one way is set a value to just let the first AP to start the
1018 // initialization, then through the later while loop to wait all Aps
1019 // finsh the initialization.
1020 // The other way is set a value to let all APs finished the initialzation.
1021 // In this case, the later while loop is useless.
1023 TimedWaitForApFinish (
1025 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
1026 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
1029 while (CpuMpData
->MpCpuExchangeInfo
->NumApsExecuting
!= 0) {
1034 // Wait all APs waken up if this is not the 1st broadcast of SIPI
1036 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1037 CpuData
= &CpuMpData
->CpuData
[Index
];
1038 if (Index
!= CpuMpData
->BspNumber
) {
1039 WaitApWakeup (CpuData
->StartupApSignal
);
1044 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1045 CpuData
->ApFunction
= (UINTN
) Procedure
;
1046 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
1047 SetApState (CpuData
, CpuStateReady
);
1049 // Wakeup specified AP
1051 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
1052 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
1053 if (ResetVectorRequired
) {
1054 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1056 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1057 (UINT32
) ExchangeInfo
->BufferStart
1061 // Wait specified AP waken up
1063 WaitApWakeup (CpuData
->StartupApSignal
);
1066 if (ResetVectorRequired
) {
1067 FreeResetVector (CpuMpData
);
1072 Calculate timeout value and return the current performance counter value.
1074 Calculate the number of performance counter ticks required for a timeout.
1075 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1078 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
1079 @param[out] CurrentTime Returns the current value of the performance counter.
1081 @return Expected time stamp counter for timeout.
1082 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1088 IN UINTN TimeoutInMicroseconds
,
1089 OUT UINT64
*CurrentTime
1092 UINT64 TimeoutInSeconds
;
1093 UINT64 TimestampCounterFreq
;
1096 // Read the current value of the performance counter
1098 *CurrentTime
= GetPerformanceCounter ();
1101 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
1104 if (TimeoutInMicroseconds
== 0) {
1109 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
1112 TimestampCounterFreq
= GetPerformanceCounterProperties (NULL
, NULL
);
1115 // Check the potential overflow before calculate the number of ticks for the timeout value.
1117 if (DivU64x64Remainder (MAX_UINT64
, TimeoutInMicroseconds
, NULL
) < TimestampCounterFreq
) {
1119 // Convert microseconds into seconds if direct multiplication overflows
1121 TimeoutInSeconds
= DivU64x32 (TimeoutInMicroseconds
, 1000000);
1123 // Assertion if the final tick count exceeds MAX_UINT64
1125 ASSERT (DivU64x64Remainder (MAX_UINT64
, TimeoutInSeconds
, NULL
) >= TimestampCounterFreq
);
1126 return MultU64x64 (TimestampCounterFreq
, TimeoutInSeconds
);
1129 // No overflow case, multiply the return value with TimeoutInMicroseconds and then divide
1130 // it by 1,000,000, to get the number of ticks for the timeout value.
1134 TimestampCounterFreq
,
1135 TimeoutInMicroseconds
1143 Checks whether timeout expires.
1145 Check whether the number of elapsed performance counter ticks required for
1146 a timeout condition has been reached.
1147 If Timeout is zero, which means infinity, return value is always FALSE.
1149 @param[in, out] PreviousTime On input, the value of the performance counter
1150 when it was last read.
1151 On output, the current value of the performance
1153 @param[in] TotalTime The total amount of elapsed time in performance
1155 @param[in] Timeout The number of performance counter ticks required
1156 to reach a timeout condition.
1158 @retval TRUE A timeout condition has been reached.
1159 @retval FALSE A timeout condition has not been reached.
1164 IN OUT UINT64
*PreviousTime
,
1165 IN UINT64
*TotalTime
,
1178 GetPerformanceCounterProperties (&Start
, &End
);
1179 Cycle
= End
- Start
;
1184 CurrentTime
= GetPerformanceCounter();
1185 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
1192 *TotalTime
+= Delta
;
1193 *PreviousTime
= CurrentTime
;
1194 if (*TotalTime
> Timeout
) {
1201 Helper function that waits until the finished AP count reaches the specified
1202 limit, or the specified timeout elapses (whichever comes first).
1204 @param[in] CpuMpData Pointer to CPU MP Data.
1205 @param[in] FinishedApLimit The number of finished APs to wait for.
1206 @param[in] TimeLimit The number of microseconds to wait for.
1209 TimedWaitForApFinish (
1210 IN CPU_MP_DATA
*CpuMpData
,
1211 IN UINT32 FinishedApLimit
,
1216 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
1217 // "infinity", so check for (TimeLimit == 0) explicitly.
1219 if (TimeLimit
== 0) {
1223 CpuMpData
->TotalTime
= 0;
1224 CpuMpData
->ExpectedTime
= CalculateTimeout (
1226 &CpuMpData
->CurrentTime
1228 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
1230 &CpuMpData
->CurrentTime
,
1231 &CpuMpData
->TotalTime
,
1232 CpuMpData
->ExpectedTime
1237 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
1240 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
1243 DivU64x64Remainder (
1244 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
1245 GetPerformanceCounterProperties (NULL
, NULL
),
1253 Reset an AP to Idle state.
1255 Any task being executed by the AP will be aborted and the AP
1256 will be waiting for a new task in Wait-For-SIPI state.
1258 @param[in] ProcessorNumber The handle number of processor.
1261 ResetProcessorToIdleState (
1262 IN UINTN ProcessorNumber
1265 CPU_MP_DATA
*CpuMpData
;
1267 CpuMpData
= GetCpuMpData ();
1269 CpuMpData
->InitFlag
= ApInitReconfig
;
1270 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
1271 while (CpuMpData
->FinishedCount
< 1) {
1274 CpuMpData
->InitFlag
= ApInitDone
;
1276 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1280 Searches for the next waiting AP.
1282 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1284 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1286 @retval EFI_SUCCESS The next waiting AP has been found.
1287 @retval EFI_NOT_FOUND No waiting AP exists.
1291 GetNextWaitingProcessorNumber (
1292 OUT UINTN
*NextProcessorNumber
1295 UINTN ProcessorNumber
;
1296 CPU_MP_DATA
*CpuMpData
;
1298 CpuMpData
= GetCpuMpData ();
1300 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1301 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1302 *NextProcessorNumber
= ProcessorNumber
;
1307 return EFI_NOT_FOUND
;
1310 /** Checks status of specified AP.
1312 This function checks whether the specified AP has finished the task assigned
1313 by StartupThisAP(), and whether timeout expires.
1315 @param[in] ProcessorNumber The handle number of processor.
1317 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1318 @retval EFI_TIMEOUT The timeout expires.
1319 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1323 IN UINTN ProcessorNumber
1326 CPU_MP_DATA
*CpuMpData
;
1327 CPU_AP_DATA
*CpuData
;
1329 CpuMpData
= GetCpuMpData ();
1330 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1333 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1334 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1335 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1338 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1340 if (GetApState(CpuData
) == CpuStateFinished
) {
1341 if (CpuData
->Finished
!= NULL
) {
1342 *(CpuData
->Finished
) = TRUE
;
1344 SetApState (CpuData
, CpuStateIdle
);
1348 // If timeout expires for StartupThisAP(), report timeout.
1350 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1351 if (CpuData
->Finished
!= NULL
) {
1352 *(CpuData
->Finished
) = FALSE
;
1355 // Reset failed AP to idle state
1357 ResetProcessorToIdleState (ProcessorNumber
);
1362 return EFI_NOT_READY
;
1366 Checks status of all APs.
1368 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1369 and whether timeout expires.
1371 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1372 @retval EFI_TIMEOUT The timeout expires.
1373 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1380 UINTN ProcessorNumber
;
1381 UINTN NextProcessorNumber
;
1384 CPU_MP_DATA
*CpuMpData
;
1385 CPU_AP_DATA
*CpuData
;
1387 CpuMpData
= GetCpuMpData ();
1389 NextProcessorNumber
= 0;
1392 // Go through all APs that are responsible for the StartupAllAPs().
1394 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1395 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1399 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1401 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1402 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1403 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1405 if (GetApState(CpuData
) == CpuStateFinished
) {
1406 CpuMpData
->RunningCount
++;
1407 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1408 SetApState(CpuData
, CpuStateIdle
);
1411 // If in Single Thread mode, then search for the next waiting AP for execution.
1413 if (CpuMpData
->SingleThread
) {
1414 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1416 if (!EFI_ERROR (Status
)) {
1420 (UINT32
) NextProcessorNumber
,
1421 CpuMpData
->Procedure
,
1422 CpuMpData
->ProcArguments
1430 // If all APs finish, return EFI_SUCCESS.
1432 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1437 // If timeout expires, report timeout.
1440 &CpuMpData
->CurrentTime
,
1441 &CpuMpData
->TotalTime
,
1442 CpuMpData
->ExpectedTime
)
1445 // If FailedCpuList is not NULL, record all failed APs in it.
1447 if (CpuMpData
->FailedCpuList
!= NULL
) {
1448 *CpuMpData
->FailedCpuList
=
1449 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1450 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1454 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1456 // Check whether this processor is responsible for StartupAllAPs().
1458 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1460 // Reset failed APs to idle state
1462 ResetProcessorToIdleState (ProcessorNumber
);
1463 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1464 if (CpuMpData
->FailedCpuList
!= NULL
) {
1465 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1469 if (CpuMpData
->FailedCpuList
!= NULL
) {
1470 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1474 return EFI_NOT_READY
;
1478 MP Initialize Library initialization.
1480 This service will allocate AP reset vector and wakeup all APs to do APs
1483 This service must be invoked before all other MP Initialize Library
1484 service are invoked.
1486 @retval EFI_SUCCESS MP initialization succeeds.
1487 @retval Others MP initialization fails.
1492 MpInitLibInitialize (
1496 CPU_MP_DATA
*OldCpuMpData
;
1497 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1498 UINT32 MaxLogicalProcessorNumber
;
1500 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1502 UINT32 MonitorFilterSize
;
1505 CPU_MP_DATA
*CpuMpData
;
1507 UINT8
*MonitorBuffer
;
1509 UINTN ApResetVectorSize
;
1510 UINTN BackupBufferAddr
;
1512 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1513 if (OldCpuMpData
== NULL
) {
1514 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1516 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1518 ASSERT (MaxLogicalProcessorNumber
!= 0);
1520 AsmGetAddressMap (&AddressMap
);
1521 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1522 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1523 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1525 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1526 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1527 BufferSize
+= sizeof (CPU_MP_DATA
);
1528 BufferSize
+= ApResetVectorSize
;
1529 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1530 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1531 ASSERT (MpBuffer
!= NULL
);
1532 ZeroMem (MpBuffer
, BufferSize
);
1533 Buffer
= (UINTN
) MpBuffer
;
1535 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1536 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1537 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1538 CpuMpData
->Buffer
= Buffer
;
1539 CpuMpData
->CpuApStackSize
= ApStackSize
;
1540 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1541 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1542 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1543 CpuMpData
->CpuCount
= 1;
1544 CpuMpData
->BspNumber
= 0;
1545 CpuMpData
->WaitEvent
= NULL
;
1546 CpuMpData
->SwitchBspFlag
= FALSE
;
1547 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1548 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1549 CpuMpData
->MicrocodePatchAddress
= PcdGet64 (PcdCpuMicrocodePatchAddress
);
1550 CpuMpData
->MicrocodePatchRegionSize
= PcdGet64 (PcdCpuMicrocodePatchRegionSize
);
1551 InitializeSpinLock(&CpuMpData
->MpLock
);
1553 // Save BSP's Control registers to APs
1555 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1557 // Set BSP basic information
1559 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
+ ApStackSize
);
1561 // Save assembly code information
1563 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1565 // Finally set AP loop mode
1567 CpuMpData
->ApLoopMode
= ApLoopMode
;
1568 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1570 // Set up APs wakeup signal buffer
1572 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1573 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1574 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1577 // Load Microcode on BSP
1579 MicrocodeDetect (CpuMpData
);
1581 // Store BSP's MTRR setting
1583 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1585 // Enable the local APIC for Virtual Wire Mode.
1587 ProgramVirtualWireMode ();
1589 if (OldCpuMpData
== NULL
) {
1590 if (MaxLogicalProcessorNumber
> 1) {
1592 // Wakeup all APs and calculate the processor count in system
1594 CollectProcessorCount (CpuMpData
);
1598 // APs have been wakeup before, just get the CPU Information
1601 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1602 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1603 CpuMpData
->InitFlag
= ApInitReconfig
;
1604 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1605 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1606 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1607 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1608 if (CpuInfoInHob
[Index
].InitialApicId
>= 255 || Index
> 254) {
1609 CpuMpData
->X2ApicEnable
= TRUE
;
1611 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1612 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1614 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1615 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1616 sizeof (CPU_VOLATILE_REGISTERS
)
1619 if (MaxLogicalProcessorNumber
> 1) {
1621 // Wakeup APs to do some AP initialize sync
1623 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1625 // Wait for all APs finished initialization
1627 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1630 CpuMpData
->InitFlag
= ApInitDone
;
1631 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1632 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1638 // Initialize global data for MP support
1640 InitMpGlobalData (CpuMpData
);
1646 Gets detailed MP-related information on the requested processor at the
1647 instant this call is made. This service may only be called from the BSP.
1649 @param[in] ProcessorNumber The handle number of processor.
1650 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1651 the requested processor is deposited.
1652 @param[out] HealthData Return processor health data.
1654 @retval EFI_SUCCESS Processor information was returned.
1655 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1656 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1657 @retval EFI_NOT_FOUND The processor with the handle specified by
1658 ProcessorNumber does not exist in the platform.
1659 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1664 MpInitLibGetProcessorInfo (
1665 IN UINTN ProcessorNumber
,
1666 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1667 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1670 CPU_MP_DATA
*CpuMpData
;
1672 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1674 CpuMpData
= GetCpuMpData ();
1675 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1678 // Check whether caller processor is BSP
1680 MpInitLibWhoAmI (&CallerNumber
);
1681 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1682 return EFI_DEVICE_ERROR
;
1685 if (ProcessorInfoBuffer
== NULL
) {
1686 return EFI_INVALID_PARAMETER
;
1689 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1690 return EFI_NOT_FOUND
;
1693 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1694 ProcessorInfoBuffer
->StatusFlag
= 0;
1695 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1696 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1698 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1699 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1701 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1702 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1704 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1708 // Get processor location information
1710 GetProcessorLocationByApicId (
1711 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1712 &ProcessorInfoBuffer
->Location
.Package
,
1713 &ProcessorInfoBuffer
->Location
.Core
,
1714 &ProcessorInfoBuffer
->Location
.Thread
1717 if (HealthData
!= NULL
) {
1718 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1725 Worker function to switch the requested AP to be the BSP from that point onward.
1727 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1728 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1729 enabled AP. Otherwise, it will be disabled.
1731 @retval EFI_SUCCESS BSP successfully switched.
1732 @retval others Failed to switch BSP.
1737 IN UINTN ProcessorNumber
,
1738 IN BOOLEAN EnableOldBSP
1741 CPU_MP_DATA
*CpuMpData
;
1744 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1745 BOOLEAN OldInterruptState
;
1746 BOOLEAN OldTimerInterruptState
;
1749 // Save and Disable Local APIC timer interrupt
1751 OldTimerInterruptState
= GetApicTimerInterruptState ();
1752 DisableApicTimerInterrupt ();
1754 // Before send both BSP and AP to a procedure to exchange their roles,
1755 // interrupt must be disabled. This is because during the exchange role
1756 // process, 2 CPU may use 1 stack. If interrupt happens, the stack will
1757 // be corrupted, since interrupt return address will be pushed to stack
1760 OldInterruptState
= SaveAndDisableInterrupts ();
1763 // Mask LINT0 & LINT1 for the old BSP
1765 DisableLvtInterrupts ();
1767 CpuMpData
= GetCpuMpData ();
1770 // Check whether caller processor is BSP
1772 MpInitLibWhoAmI (&CallerNumber
);
1773 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1774 return EFI_DEVICE_ERROR
;
1777 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1778 return EFI_NOT_FOUND
;
1782 // Check whether specified AP is disabled
1784 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1785 if (State
== CpuStateDisabled
) {
1786 return EFI_INVALID_PARAMETER
;
1790 // Check whether ProcessorNumber specifies the current BSP
1792 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1793 return EFI_INVALID_PARAMETER
;
1797 // Check whether specified AP is busy
1799 if (State
== CpuStateBusy
) {
1800 return EFI_NOT_READY
;
1803 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1804 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1805 CpuMpData
->SwitchBspFlag
= TRUE
;
1806 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1809 // Clear the BSP bit of MSR_IA32_APIC_BASE
1811 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1812 ApicBaseMsr
.Bits
.BSP
= 0;
1813 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1816 // Need to wakeUp AP (future BSP).
1818 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1820 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1823 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1825 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1826 ApicBaseMsr
.Bits
.BSP
= 1;
1827 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1828 ProgramVirtualWireMode ();
1831 // Wait for old BSP finished AP task
1833 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1837 CpuMpData
->SwitchBspFlag
= FALSE
;
1839 // Set old BSP enable state
1841 if (!EnableOldBSP
) {
1842 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1844 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateIdle
);
1847 // Save new BSP number
1849 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1852 // Restore interrupt state.
1854 SetInterruptState (OldInterruptState
);
1856 if (OldTimerInterruptState
) {
1857 EnableApicTimerInterrupt ();
1864 Worker function to let the caller enable or disable an AP from this point onward.
1865 This service may only be called from the BSP.
1867 @param[in] ProcessorNumber The handle number of AP.
1868 @param[in] EnableAP Specifies the new state for the processor for
1869 enabled, FALSE for disabled.
1870 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1871 the new health status of the AP.
1873 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1874 @retval others Failed to Enable/Disable AP.
1878 EnableDisableApWorker (
1879 IN UINTN ProcessorNumber
,
1880 IN BOOLEAN EnableAP
,
1881 IN UINT32
*HealthFlag OPTIONAL
1884 CPU_MP_DATA
*CpuMpData
;
1887 CpuMpData
= GetCpuMpData ();
1890 // Check whether caller processor is BSP
1892 MpInitLibWhoAmI (&CallerNumber
);
1893 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1894 return EFI_DEVICE_ERROR
;
1897 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1898 return EFI_INVALID_PARAMETER
;
1901 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1902 return EFI_NOT_FOUND
;
1906 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1908 ResetProcessorToIdleState (ProcessorNumber
);
1911 if (HealthFlag
!= NULL
) {
1912 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1913 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1920 This return the handle number for the calling processor. This service may be
1921 called from the BSP and APs.
1923 @param[out] ProcessorNumber Pointer to the handle number of AP.
1924 The range is from 0 to the total number of
1925 logical processors minus 1. The total number of
1926 logical processors can be retrieved by
1927 MpInitLibGetNumberOfProcessors().
1929 @retval EFI_SUCCESS The current processor handle number was returned
1931 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1932 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1938 OUT UINTN
*ProcessorNumber
1941 CPU_MP_DATA
*CpuMpData
;
1943 if (ProcessorNumber
== NULL
) {
1944 return EFI_INVALID_PARAMETER
;
1947 CpuMpData
= GetCpuMpData ();
1949 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1953 Retrieves the number of logical processor in the platform and the number of
1954 those logical processors that are enabled on this boot. This service may only
1955 be called from the BSP.
1957 @param[out] NumberOfProcessors Pointer to the total number of logical
1958 processors in the system, including the BSP
1960 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1961 processors that exist in system, including
1964 @retval EFI_SUCCESS The number of logical processors and enabled
1965 logical processors was retrieved.
1966 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1967 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1969 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1974 MpInitLibGetNumberOfProcessors (
1975 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1976 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1979 CPU_MP_DATA
*CpuMpData
;
1981 UINTN ProcessorNumber
;
1982 UINTN EnabledProcessorNumber
;
1985 CpuMpData
= GetCpuMpData ();
1987 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1988 return EFI_INVALID_PARAMETER
;
1992 // Check whether caller processor is BSP
1994 MpInitLibWhoAmI (&CallerNumber
);
1995 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1996 return EFI_DEVICE_ERROR
;
1999 ProcessorNumber
= CpuMpData
->CpuCount
;
2000 EnabledProcessorNumber
= 0;
2001 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
2002 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
2003 EnabledProcessorNumber
++;
2007 if (NumberOfProcessors
!= NULL
) {
2008 *NumberOfProcessors
= ProcessorNumber
;
2010 if (NumberOfEnabledProcessors
!= NULL
) {
2011 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
2019 Worker function to execute a caller provided function on all enabled APs.
2021 @param[in] Procedure A pointer to the function to be run on
2022 enabled APs of the system.
2023 @param[in] SingleThread If TRUE, then all the enabled APs execute
2024 the function specified by Procedure one by
2025 one, in ascending order of processor handle
2026 number. If FALSE, then all the enabled APs
2027 execute the function specified by Procedure
2029 @param[in] WaitEvent The event created by the caller with CreateEvent()
2031 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2032 APs to return from Procedure, either for
2033 blocking or non-blocking mode.
2034 @param[in] ProcedureArgument The parameter passed into Procedure for
2036 @param[out] FailedCpuList If all APs finish successfully, then its
2037 content is set to NULL. If not all APs
2038 finish before timeout expires, then its
2039 content is set to address of the buffer
2040 holding handle numbers of the failed APs.
2042 @retval EFI_SUCCESS In blocking mode, all APs have finished before
2043 the timeout expired.
2044 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
2046 @retval others Failed to Startup all APs.
2050 StartupAllAPsWorker (
2051 IN EFI_AP_PROCEDURE Procedure
,
2052 IN BOOLEAN SingleThread
,
2053 IN EFI_EVENT WaitEvent OPTIONAL
,
2054 IN UINTN TimeoutInMicroseconds
,
2055 IN VOID
*ProcedureArgument OPTIONAL
,
2056 OUT UINTN
**FailedCpuList OPTIONAL
2060 CPU_MP_DATA
*CpuMpData
;
2061 UINTN ProcessorCount
;
2062 UINTN ProcessorNumber
;
2064 CPU_AP_DATA
*CpuData
;
2065 BOOLEAN HasEnabledAp
;
2068 CpuMpData
= GetCpuMpData ();
2070 if (FailedCpuList
!= NULL
) {
2071 *FailedCpuList
= NULL
;
2074 if (CpuMpData
->CpuCount
== 1) {
2075 return EFI_NOT_STARTED
;
2078 if (Procedure
== NULL
) {
2079 return EFI_INVALID_PARAMETER
;
2083 // Check whether caller processor is BSP
2085 MpInitLibWhoAmI (&CallerNumber
);
2086 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2087 return EFI_DEVICE_ERROR
;
2093 CheckAndUpdateApsStatus ();
2095 ProcessorCount
= CpuMpData
->CpuCount
;
2096 HasEnabledAp
= FALSE
;
2098 // Check whether all enabled APs are idle.
2099 // If any enabled AP is not idle, return EFI_NOT_READY.
2101 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2102 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2103 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2104 ApState
= GetApState (CpuData
);
2105 if (ApState
!= CpuStateDisabled
) {
2106 HasEnabledAp
= TRUE
;
2107 if (ApState
!= CpuStateIdle
) {
2109 // If any enabled APs are busy, return EFI_NOT_READY.
2111 return EFI_NOT_READY
;
2117 if (!HasEnabledAp
) {
2119 // If no enabled AP exists, return EFI_NOT_STARTED.
2121 return EFI_NOT_STARTED
;
2124 CpuMpData
->StartCount
= 0;
2125 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2126 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2127 CpuData
->Waiting
= FALSE
;
2128 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
2129 if (CpuData
->State
== CpuStateIdle
) {
2131 // Mark this processor as responsible for current calling.
2133 CpuData
->Waiting
= TRUE
;
2134 CpuMpData
->StartCount
++;
2139 CpuMpData
->Procedure
= Procedure
;
2140 CpuMpData
->ProcArguments
= ProcedureArgument
;
2141 CpuMpData
->SingleThread
= SingleThread
;
2142 CpuMpData
->FinishedCount
= 0;
2143 CpuMpData
->RunningCount
= 0;
2144 CpuMpData
->FailedCpuList
= FailedCpuList
;
2145 CpuMpData
->ExpectedTime
= CalculateTimeout (
2146 TimeoutInMicroseconds
,
2147 &CpuMpData
->CurrentTime
2149 CpuMpData
->TotalTime
= 0;
2150 CpuMpData
->WaitEvent
= WaitEvent
;
2152 if (!SingleThread
) {
2153 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
2155 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
2156 if (ProcessorNumber
== CallerNumber
) {
2159 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
2160 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2166 Status
= EFI_SUCCESS
;
2167 if (WaitEvent
== NULL
) {
2169 Status
= CheckAllAPs ();
2170 } while (Status
== EFI_NOT_READY
);
2177 Worker function to let the caller get one enabled AP to execute a caller-provided
2180 @param[in] Procedure A pointer to the function to be run on
2181 enabled APs of the system.
2182 @param[in] ProcessorNumber The handle number of the AP.
2183 @param[in] WaitEvent The event created by the caller with CreateEvent()
2185 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
2186 APs to return from Procedure, either for
2187 blocking or non-blocking mode.
2188 @param[in] ProcedureArgument The parameter passed into Procedure for
2190 @param[out] Finished If AP returns from Procedure before the
2191 timeout expires, its content is set to TRUE.
2192 Otherwise, the value is set to FALSE.
2194 @retval EFI_SUCCESS In blocking mode, specified AP finished before
2195 the timeout expires.
2196 @retval others Failed to Startup AP.
2200 StartupThisAPWorker (
2201 IN EFI_AP_PROCEDURE Procedure
,
2202 IN UINTN ProcessorNumber
,
2203 IN EFI_EVENT WaitEvent OPTIONAL
,
2204 IN UINTN TimeoutInMicroseconds
,
2205 IN VOID
*ProcedureArgument OPTIONAL
,
2206 OUT BOOLEAN
*Finished OPTIONAL
2210 CPU_MP_DATA
*CpuMpData
;
2211 CPU_AP_DATA
*CpuData
;
2214 CpuMpData
= GetCpuMpData ();
2216 if (Finished
!= NULL
) {
2221 // Check whether caller processor is BSP
2223 MpInitLibWhoAmI (&CallerNumber
);
2224 if (CallerNumber
!= CpuMpData
->BspNumber
) {
2225 return EFI_DEVICE_ERROR
;
2229 // Check whether processor with the handle specified by ProcessorNumber exists
2231 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
2232 return EFI_NOT_FOUND
;
2236 // Check whether specified processor is BSP
2238 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
2239 return EFI_INVALID_PARAMETER
;
2243 // Check parameter Procedure
2245 if (Procedure
== NULL
) {
2246 return EFI_INVALID_PARAMETER
;
2252 CheckAndUpdateApsStatus ();
2255 // Check whether specified AP is disabled
2257 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
2258 return EFI_INVALID_PARAMETER
;
2262 // If WaitEvent is not NULL, execute in non-blocking mode.
2263 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
2264 // CheckAPsStatus() will check completion and timeout periodically.
2266 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
2267 CpuData
->WaitEvent
= WaitEvent
;
2268 CpuData
->Finished
= Finished
;
2269 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
2270 CpuData
->TotalTime
= 0;
2272 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
2275 // If WaitEvent is NULL, execute in blocking mode.
2276 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
2278 Status
= EFI_SUCCESS
;
2279 if (WaitEvent
== NULL
) {
2281 Status
= CheckThisAP (ProcessorNumber
);
2282 } while (Status
== EFI_NOT_READY
);
2289 Get pointer to CPU MP Data structure from GUIDed HOB.
2291 @return The pointer to CPU MP Data structure.
2294 GetCpuMpDataFromGuidedHob (
2298 EFI_HOB_GUID_TYPE
*GuidHob
;
2300 CPU_MP_DATA
*CpuMpData
;
2303 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
2304 if (GuidHob
!= NULL
) {
2305 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
2306 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);