2 CPU MP Initialize Library common functions.
4 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
17 EFI_GUID mCpuInitMpLibHobGuid
= CPU_INIT_MP_LIB_HOB_GUID
;
20 The function will check if BSP Execute Disable is enabled.
21 DxeIpl may have enabled Execute Disable for BSP,
22 APs need to get the status and sync up the settings.
24 @retval TRUE BSP Execute Disable is enabled.
25 @retval FALSE BSP Execute Disable is not enabled.
28 IsBspExecuteDisableEnabled (
33 CPUID_EXTENDED_CPU_SIG_EDX Edx
;
34 MSR_IA32_EFER_REGISTER EferMsr
;
38 AsmCpuid (CPUID_EXTENDED_FUNCTION
, &Eax
, NULL
, NULL
, NULL
);
39 if (Eax
>= CPUID_EXTENDED_CPU_SIG
) {
40 AsmCpuid (CPUID_EXTENDED_CPU_SIG
, NULL
, NULL
, NULL
, &Edx
.Uint32
);
43 // Bit 20: Execute Disable Bit available.
45 if (Edx
.Bits
.NX
!= 0) {
46 EferMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_EFER
);
49 // Bit 11: Execute Disable Bit enable.
51 if (EferMsr
.Bits
.NXE
!= 0) {
61 Worker function for SwitchBSP().
63 Worker function for SwitchBSP(), assigned to the AP which is intended
66 @param[in] Buffer Pointer to CPU MP Data
74 CPU_MP_DATA
*DataInHob
;
76 DataInHob
= (CPU_MP_DATA
*) Buffer
;
77 AsmExchangeRole (&DataInHob
->APInfo
, &DataInHob
->BSPInfo
);
81 Get the Application Processors state.
83 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
89 IN CPU_AP_DATA
*CpuData
92 return CpuData
->State
;
96 Set the Application Processors state.
98 @param[in] CpuData The pointer to CPU_AP_DATA of specified AP
99 @param[in] State The AP status
103 IN CPU_AP_DATA
*CpuData
,
107 AcquireSpinLock (&CpuData
->ApLock
);
108 CpuData
->State
= State
;
109 ReleaseSpinLock (&CpuData
->ApLock
);
113 Save the volatile registers required to be restored following INIT IPI.
115 @param[out] VolatileRegisters Returns buffer saved the volatile resisters
118 SaveVolatileRegisters (
119 OUT CPU_VOLATILE_REGISTERS
*VolatileRegisters
122 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
124 VolatileRegisters
->Cr0
= AsmReadCr0 ();
125 VolatileRegisters
->Cr3
= AsmReadCr3 ();
126 VolatileRegisters
->Cr4
= AsmReadCr4 ();
128 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
129 if (VersionInfoEdx
.Bits
.DE
!= 0) {
131 // If processor supports Debugging Extensions feature
132 // by CPUID.[EAX=01H]:EDX.BIT2
134 VolatileRegisters
->Dr0
= AsmReadDr0 ();
135 VolatileRegisters
->Dr1
= AsmReadDr1 ();
136 VolatileRegisters
->Dr2
= AsmReadDr2 ();
137 VolatileRegisters
->Dr3
= AsmReadDr3 ();
138 VolatileRegisters
->Dr6
= AsmReadDr6 ();
139 VolatileRegisters
->Dr7
= AsmReadDr7 ();
144 Restore the volatile registers following INIT IPI.
146 @param[in] VolatileRegisters Pointer to volatile resisters
147 @param[in] IsRestoreDr TRUE: Restore DRx if supported
148 FALSE: Do not restore DRx
151 RestoreVolatileRegisters (
152 IN CPU_VOLATILE_REGISTERS
*VolatileRegisters
,
153 IN BOOLEAN IsRestoreDr
156 CPUID_VERSION_INFO_EDX VersionInfoEdx
;
158 AsmWriteCr0 (VolatileRegisters
->Cr0
);
159 AsmWriteCr3 (VolatileRegisters
->Cr3
);
160 AsmWriteCr4 (VolatileRegisters
->Cr4
);
163 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, NULL
, &VersionInfoEdx
.Uint32
);
164 if (VersionInfoEdx
.Bits
.DE
!= 0) {
166 // If processor supports Debugging Extensions feature
167 // by CPUID.[EAX=01H]:EDX.BIT2
169 AsmWriteDr0 (VolatileRegisters
->Dr0
);
170 AsmWriteDr1 (VolatileRegisters
->Dr1
);
171 AsmWriteDr2 (VolatileRegisters
->Dr2
);
172 AsmWriteDr3 (VolatileRegisters
->Dr3
);
173 AsmWriteDr6 (VolatileRegisters
->Dr6
);
174 AsmWriteDr7 (VolatileRegisters
->Dr7
);
180 Detect whether Mwait-monitor feature is supported.
182 @retval TRUE Mwait-monitor feature is supported.
183 @retval FALSE Mwait-monitor feature is not supported.
190 CPUID_VERSION_INFO_ECX VersionInfoEcx
;
192 AsmCpuid (CPUID_VERSION_INFO
, NULL
, NULL
, &VersionInfoEcx
.Uint32
, NULL
);
193 return (VersionInfoEcx
.Bits
.MONITOR
== 1) ? TRUE
: FALSE
;
199 @param[out] MonitorFilterSize Returns the largest monitor-line size in bytes.
201 @return The AP loop mode.
205 OUT UINT32
*MonitorFilterSize
209 CPUID_MONITOR_MWAIT_EBX MonitorMwaitEbx
;
211 ASSERT (MonitorFilterSize
!= NULL
);
213 ApLoopMode
= PcdGet8 (PcdCpuApLoopMode
);
214 ASSERT (ApLoopMode
>= ApInHltLoop
&& ApLoopMode
<= ApInRunLoop
);
215 if (ApLoopMode
== ApInMwaitLoop
) {
216 if (!IsMwaitSupport ()) {
218 // If processor does not support MONITOR/MWAIT feature,
219 // force AP in Hlt-loop mode
221 ApLoopMode
= ApInHltLoop
;
225 if (ApLoopMode
!= ApInMwaitLoop
) {
226 *MonitorFilterSize
= sizeof (UINT32
);
229 // CPUID.[EAX=05H]:EBX.BIT0-15: Largest monitor-line size in bytes
230 // CPUID.[EAX=05H].EDX: C-states supported using MWAIT
232 AsmCpuid (CPUID_MONITOR_MWAIT
, NULL
, &MonitorMwaitEbx
.Uint32
, NULL
, NULL
);
233 *MonitorFilterSize
= MonitorMwaitEbx
.Bits
.LargestMonitorLineSize
;
240 Sort the APIC ID of all processors.
242 This function sorts the APIC ID of all processors so that processor number is
243 assigned in the ascending order of APIC ID which eases MP debugging.
245 @param[in] CpuMpData Pointer to PEI CPU MP Data
249 IN CPU_MP_DATA
*CpuMpData
258 CPU_INFO_IN_HOB
*CpuInfoInHob
;
260 ApCount
= CpuMpData
->CpuCount
- 1;
263 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
266 // Sort key is the hardware default APIC ID
268 ApicId
= CpuMpData
->CpuData
[Index1
].ApicId
;
269 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
270 if (ApicId
> CpuMpData
->CpuData
[Index2
].ApicId
) {
272 ApicId
= CpuMpData
->CpuData
[Index2
].ApicId
;
275 if (Index3
!= Index1
) {
276 CopyMem (&CpuData
, &CpuMpData
->CpuData
[Index3
], sizeof (CPU_AP_DATA
));
278 &CpuMpData
->CpuData
[Index3
],
279 &CpuMpData
->CpuData
[Index1
],
282 CopyMem (&CpuMpData
->CpuData
[Index1
], &CpuData
, sizeof (CPU_AP_DATA
));
287 // Get the processor number for the BSP
289 ApicId
= GetInitialApicId ();
290 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
291 if (CpuMpData
->CpuData
[Index1
].ApicId
== ApicId
) {
292 CpuMpData
->BspNumber
= (UINT32
) Index1
;
297 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
298 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
299 CpuInfoInHob
[Index1
].InitialApicId
= CpuMpData
->CpuData
[Index1
].InitialApicId
;
300 CpuInfoInHob
[Index1
].ApicId
= CpuMpData
->CpuData
[Index1
].ApicId
;
301 CpuInfoInHob
[Index1
].Health
= CpuMpData
->CpuData
[Index1
].Health
;
307 Enable x2APIC mode on APs.
309 @param[in, out] Buffer Pointer to private data buffer.
317 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
323 @param[in, out] Buffer Pointer to private data buffer.
331 CPU_MP_DATA
*CpuMpData
;
333 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
335 // Sync BSP's MTRR table to AP
337 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
339 // Load microcode on AP
341 MicrocodeDetect (CpuMpData
);
345 Find the current Processor number by APIC ID.
347 @param[in] CpuMpData Pointer to PEI CPU MP Data
348 @param[in] ProcessorNumber Return the pocessor number found
350 @retval EFI_SUCCESS ProcessorNumber is found and returned.
351 @retval EFI_NOT_FOUND ProcessorNumber is not found.
355 IN CPU_MP_DATA
*CpuMpData
,
356 OUT UINTN
*ProcessorNumber
359 UINTN TotalProcessorNumber
;
362 TotalProcessorNumber
= CpuMpData
->CpuCount
;
363 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
364 if (CpuMpData
->CpuData
[Index
].ApicId
== GetApicId ()) {
365 *ProcessorNumber
= Index
;
369 return EFI_NOT_FOUND
;
373 This function will get CPU count in the system.
375 @param[in] CpuMpData Pointer to PEI CPU MP Data
377 @return CPU count detected
380 CollectProcessorCount (
381 IN CPU_MP_DATA
*CpuMpData
385 // Send 1st broadcast IPI to APs to wakeup APs
387 CpuMpData
->InitFlag
= ApInitConfig
;
388 CpuMpData
->X2ApicEnable
= FALSE
;
389 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
);
390 CpuMpData
->InitFlag
= ApInitDone
;
391 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
393 // Wait for all APs finished the initialization
395 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
399 if (CpuMpData
->X2ApicEnable
) {
400 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
402 // Wakeup all APs to enable x2APIC mode
404 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
406 // Wait for all known APs finished
408 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
412 // Enable x2APIC on BSP
414 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
416 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
418 // Sort BSP/Aps by CPU APIC ID in ascending order
420 SortApicId (CpuMpData
);
422 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
424 return CpuMpData
->CpuCount
;
428 Initialize CPU AP Data when AP is wakeup at the first time.
430 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
431 @param[in] ProcessorNumber The handle number of processor
432 @param[in] BistData Processor BIST data
437 IN OUT CPU_MP_DATA
*CpuMpData
,
438 IN UINTN ProcessorNumber
,
442 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
443 CpuMpData
->CpuData
[ProcessorNumber
].Health
= BistData
;
444 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
445 CpuMpData
->CpuData
[ProcessorNumber
].ApicId
= GetApicId ();
446 CpuMpData
->CpuData
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
447 if (CpuMpData
->CpuData
[ProcessorNumber
].InitialApicId
>= 0xFF) {
449 // Set x2APIC mode if there are any logical processor reporting
450 // an Initial APIC ID of 255 or greater.
452 AcquireSpinLock(&CpuMpData
->MpLock
);
453 CpuMpData
->X2ApicEnable
= TRUE
;
454 ReleaseSpinLock(&CpuMpData
->MpLock
);
457 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
458 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
462 This function will be called from AP reset code if BSP uses WakeUpAP.
464 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
465 @param[in] NumApsExecuting Number of current executing AP
470 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
471 IN UINTN NumApsExecuting
474 CPU_MP_DATA
*CpuMpData
;
475 UINTN ProcessorNumber
;
476 EFI_AP_PROCEDURE Procedure
;
479 volatile UINT32
*ApStartupSignalBuffer
;
482 // AP finished assembly code and begin to execute C code
484 CpuMpData
= ExchangeInfo
->CpuMpData
;
486 ProgramVirtualWireMode ();
489 if (CpuMpData
->InitFlag
== ApInitConfig
) {
493 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
494 ProcessorNumber
= NumApsExecuting
;
496 // This is first time AP wakeup, get BIST information from AP stack
498 BistData
= *(UINT32
*) (CpuMpData
->Buffer
+ ProcessorNumber
* CpuMpData
->CpuApStackSize
- sizeof (UINTN
));
500 // Do some AP initialize sync
502 ApInitializeSync (CpuMpData
);
504 // Sync BSP's Control registers to APs
506 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
507 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
);
508 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
511 // Execute AP function if AP is ready
513 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
515 // Clear AP start-up signal when AP waken up
517 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
518 InterlockedCompareExchange32 (
519 (UINT32
*) ApStartupSignalBuffer
,
523 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
525 // Restore AP's volatile registers saved
527 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
530 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
531 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
532 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
533 if (Procedure
!= NULL
) {
534 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
536 // Invoke AP function here
538 Procedure (Parameter
);
539 if (CpuMpData
->SwitchBspFlag
) {
541 // Re-get the processor number due to BSP/AP maybe exchange in AP function
543 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
544 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
545 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
548 // Re-get the CPU APICID and Initial APICID
550 CpuMpData
->CpuData
[ProcessorNumber
].ApicId
= GetApicId ();
551 CpuMpData
->CpuData
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
554 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
559 // AP finished executing C code
561 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
564 // Place AP is specified loop mode
566 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
568 // Save AP volatile registers
570 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
572 // Place AP in HLT-loop
575 DisableInterrupts ();
581 DisableInterrupts ();
582 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
584 // Place AP in MWAIT-loop
586 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
587 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
589 // Check AP start-up signal again.
590 // If AP start-up signal is not set, place AP into
591 // the specified C-state
593 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
595 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
597 // Place AP in Run-loop
605 // If AP start-up signal is written, AP is waken up
606 // otherwise place AP in loop again
608 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
616 Wait for AP wakeup and write AP start-up signal till AP is waken up.
618 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
622 IN
volatile UINT32
*ApStartupSignalBuffer
626 // If AP is waken up, StartupApSignal should be cleared.
627 // Otherwise, write StartupApSignal again till AP waken up.
629 while (InterlockedCompareExchange32 (
630 (UINT32
*) ApStartupSignalBuffer
,
639 This function will fill the exchange info structure.
641 @param[in] CpuMpData Pointer to CPU MP Data
645 FillExchangeInfoData (
646 IN CPU_MP_DATA
*CpuMpData
649 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
651 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
652 ExchangeInfo
->Lock
= 0;
653 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
654 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
655 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
656 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
658 ExchangeInfo
->CodeSegment
= AsmReadCs ();
659 ExchangeInfo
->DataSegment
= AsmReadDs ();
661 ExchangeInfo
->Cr3
= AsmReadCr3 ();
663 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
664 ExchangeInfo
->NumApsExecuting
= 0;
665 ExchangeInfo
->CpuMpData
= CpuMpData
;
667 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
670 // Get the BSP's data of GDT and IDT
672 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
673 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
677 This function will be called by BSP to wakeup AP.
679 @param[in] CpuMpData Pointer to CPU MP Data
680 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
681 FALSE: Send IPI to AP by ApicId
682 @param[in] ProcessorNumber The handle number of specified processor
683 @param[in] Procedure The function to be invoked by AP
684 @param[in] ProcedureArgument The argument to be passed into AP function
688 IN CPU_MP_DATA
*CpuMpData
,
689 IN BOOLEAN Broadcast
,
690 IN UINTN ProcessorNumber
,
691 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
692 IN VOID
*ProcedureArgument OPTIONAL
695 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
697 CPU_AP_DATA
*CpuData
;
698 BOOLEAN ResetVectorRequired
;
700 CpuMpData
->FinishedCount
= 0;
701 ResetVectorRequired
= FALSE
;
703 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
704 CpuMpData
->InitFlag
!= ApInitDone
) {
705 ResetVectorRequired
= TRUE
;
706 AllocateResetVector (CpuMpData
);
707 FillExchangeInfoData (CpuMpData
);
708 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
710 // Get AP target C-state each time when waking up AP,
711 // for it maybe updated by platform again
713 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
716 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
719 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
720 if (Index
!= CpuMpData
->BspNumber
) {
721 CpuData
= &CpuMpData
->CpuData
[Index
];
722 CpuData
->ApFunction
= (UINTN
) Procedure
;
723 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
724 SetApState (CpuData
, CpuStateReady
);
725 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
726 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
730 if (ResetVectorRequired
) {
734 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
736 if (CpuMpData
->InitFlag
== ApInitConfig
) {
738 // Wait for all potential APs waken up in one specified period
740 MicroSecondDelay (PcdGet32(PcdCpuApInitTimeOutInMicroSeconds
));
743 // Wait all APs waken up if this is not the 1st broadcast of SIPI
745 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
746 CpuData
= &CpuMpData
->CpuData
[Index
];
747 if (Index
!= CpuMpData
->BspNumber
) {
748 WaitApWakeup (CpuData
->StartupApSignal
);
753 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
754 CpuData
->ApFunction
= (UINTN
) Procedure
;
755 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
756 SetApState (CpuData
, CpuStateReady
);
758 // Wakeup specified AP
760 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
761 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
762 if (ResetVectorRequired
) {
765 (UINT32
) ExchangeInfo
->BufferStart
769 // Wait specified AP waken up
771 WaitApWakeup (CpuData
->StartupApSignal
);
774 if (ResetVectorRequired
) {
775 FreeResetVector (CpuMpData
);
780 Calculate timeout value and return the current performance counter value.
782 Calculate the number of performance counter ticks required for a timeout.
783 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
786 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
787 @param[out] CurrentTime Returns the current value of the performance counter.
789 @return Expected time stamp counter for timeout.
790 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
796 IN UINTN TimeoutInMicroseconds
,
797 OUT UINT64
*CurrentTime
801 // Read the current value of the performance counter
803 *CurrentTime
= GetPerformanceCounter ();
806 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
809 if (TimeoutInMicroseconds
== 0) {
814 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
815 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
816 // it by 1,000,000, to get the number of ticks for the timeout value.
820 GetPerformanceCounterProperties (NULL
, NULL
),
821 TimeoutInMicroseconds
828 Checks whether timeout expires.
830 Check whether the number of elapsed performance counter ticks required for
831 a timeout condition has been reached.
832 If Timeout is zero, which means infinity, return value is always FALSE.
834 @param[in, out] PreviousTime On input, the value of the performance counter
835 when it was last read.
836 On output, the current value of the performance
838 @param[in] TotalTime The total amount of elapsed time in performance
840 @param[in] Timeout The number of performance counter ticks required
841 to reach a timeout condition.
843 @retval TRUE A timeout condition has been reached.
844 @retval FALSE A timeout condition has not been reached.
849 IN OUT UINT64
*PreviousTime
,
850 IN UINT64
*TotalTime
,
863 GetPerformanceCounterProperties (&Start
, &End
);
869 CurrentTime
= GetPerformanceCounter();
870 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
878 *PreviousTime
= CurrentTime
;
879 if (*TotalTime
> Timeout
) {
886 Reset an AP to Idle state.
888 Any task being executed by the AP will be aborted and the AP
889 will be waiting for a new task in Wait-For-SIPI state.
891 @param[in] ProcessorNumber The handle number of processor.
894 ResetProcessorToIdleState (
895 IN UINTN ProcessorNumber
898 CPU_MP_DATA
*CpuMpData
;
900 CpuMpData
= GetCpuMpData ();
902 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
904 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
908 Searches for the next waiting AP.
910 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
912 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
914 @retval EFI_SUCCESS The next waiting AP has been found.
915 @retval EFI_NOT_FOUND No waiting AP exists.
919 GetNextWaitingProcessorNumber (
920 OUT UINTN
*NextProcessorNumber
923 UINTN ProcessorNumber
;
924 CPU_MP_DATA
*CpuMpData
;
926 CpuMpData
= GetCpuMpData ();
928 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
929 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
930 *NextProcessorNumber
= ProcessorNumber
;
935 return EFI_NOT_FOUND
;
938 /** Checks status of specified AP.
940 This function checks whether the specified AP has finished the task assigned
941 by StartupThisAP(), and whether timeout expires.
943 @param[in] ProcessorNumber The handle number of processor.
945 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
946 @retval EFI_TIMEOUT The timeout expires.
947 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
951 IN UINTN ProcessorNumber
954 CPU_MP_DATA
*CpuMpData
;
955 CPU_AP_DATA
*CpuData
;
957 CpuMpData
= GetCpuMpData ();
958 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
961 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
962 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
963 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
966 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
968 if (GetApState(CpuData
) == CpuStateFinished
) {
969 if (CpuData
->Finished
!= NULL
) {
970 *(CpuData
->Finished
) = TRUE
;
972 SetApState (CpuData
, CpuStateIdle
);
976 // If timeout expires for StartupThisAP(), report timeout.
978 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
979 if (CpuData
->Finished
!= NULL
) {
980 *(CpuData
->Finished
) = FALSE
;
983 // Reset failed AP to idle state
985 ResetProcessorToIdleState (ProcessorNumber
);
990 return EFI_NOT_READY
;
994 Checks status of all APs.
996 This function checks whether all APs have finished task assigned by StartupAllAPs(),
997 and whether timeout expires.
999 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1000 @retval EFI_TIMEOUT The timeout expires.
1001 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1008 UINTN ProcessorNumber
;
1009 UINTN NextProcessorNumber
;
1012 CPU_MP_DATA
*CpuMpData
;
1013 CPU_AP_DATA
*CpuData
;
1015 CpuMpData
= GetCpuMpData ();
1017 NextProcessorNumber
= 0;
1020 // Go through all APs that are responsible for the StartupAllAPs().
1022 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1023 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1027 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1029 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1030 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1031 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1033 if (GetApState(CpuData
) == CpuStateFinished
) {
1034 CpuMpData
->RunningCount
++;
1035 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1036 SetApState(CpuData
, CpuStateIdle
);
1039 // If in Single Thread mode, then search for the next waiting AP for execution.
1041 if (CpuMpData
->SingleThread
) {
1042 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1044 if (!EFI_ERROR (Status
)) {
1048 (UINT32
) NextProcessorNumber
,
1049 CpuMpData
->Procedure
,
1050 CpuMpData
->ProcArguments
1058 // If all APs finish, return EFI_SUCCESS.
1060 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1065 // If timeout expires, report timeout.
1068 &CpuMpData
->CurrentTime
,
1069 &CpuMpData
->TotalTime
,
1070 CpuMpData
->ExpectedTime
)
1073 // If FailedCpuList is not NULL, record all failed APs in it.
1075 if (CpuMpData
->FailedCpuList
!= NULL
) {
1076 *CpuMpData
->FailedCpuList
=
1077 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1078 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1082 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1084 // Check whether this processor is responsible for StartupAllAPs().
1086 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1088 // Reset failed APs to idle state
1090 ResetProcessorToIdleState (ProcessorNumber
);
1091 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1092 if (CpuMpData
->FailedCpuList
!= NULL
) {
1093 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1097 if (CpuMpData
->FailedCpuList
!= NULL
) {
1098 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1102 return EFI_NOT_READY
;
1106 MP Initialize Library initialization.
1108 This service will allocate AP reset vector and wakeup all APs to do APs
1111 This service must be invoked before all other MP Initialize Library
1112 service are invoked.
1114 @retval EFI_SUCCESS MP initialization succeeds.
1115 @retval Others MP initialization fails.
1120 MpInitLibInitialize (
1124 CPU_MP_DATA
*OldCpuMpData
;
1125 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1126 UINT32 MaxLogicalProcessorNumber
;
1128 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1130 UINT32 MonitorFilterSize
;
1133 CPU_MP_DATA
*CpuMpData
;
1135 UINT8
*MonitorBuffer
;
1137 UINTN ApResetVectorSize
;
1138 UINTN BackupBufferAddr
;
1140 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1141 if (OldCpuMpData
== NULL
) {
1142 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1144 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1146 ASSERT (MaxLogicalProcessorNumber
!= 0);
1148 AsmGetAddressMap (&AddressMap
);
1149 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1150 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1151 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1153 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1154 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1155 BufferSize
+= sizeof (CPU_MP_DATA
);
1156 BufferSize
+= ApResetVectorSize
;
1157 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1158 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1159 ASSERT (MpBuffer
!= NULL
);
1160 ZeroMem (MpBuffer
, BufferSize
);
1161 Buffer
= (UINTN
) MpBuffer
;
1163 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1164 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1165 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1166 CpuMpData
->Buffer
= Buffer
;
1167 CpuMpData
->CpuApStackSize
= ApStackSize
;
1168 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1169 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1170 CpuMpData
->SaveRestoreFlag
= FALSE
;
1171 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1172 CpuMpData
->CpuCount
= 1;
1173 CpuMpData
->BspNumber
= 0;
1174 CpuMpData
->WaitEvent
= NULL
;
1175 CpuMpData
->SwitchBspFlag
= FALSE
;
1176 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1177 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1178 InitializeSpinLock(&CpuMpData
->MpLock
);
1180 // Save BSP's Control registers to APs
1182 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1184 // Set BSP basic information
1186 InitializeApData (CpuMpData
, 0, 0);
1188 // Save assembly code information
1190 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1192 // Finally set AP loop mode
1194 CpuMpData
->ApLoopMode
= ApLoopMode
;
1195 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1197 // Set up APs wakeup signal buffer
1199 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1200 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1201 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1204 // Load Microcode on BSP
1206 MicrocodeDetect (CpuMpData
);
1208 // Store BSP's MTRR setting
1210 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1212 if (OldCpuMpData
== NULL
) {
1213 if (MaxLogicalProcessorNumber
> 1) {
1215 // Wakeup all APs and calculate the processor count in system
1217 CollectProcessorCount (CpuMpData
);
1221 // APs have been wakeup before, just get the CPU Information
1224 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1225 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1226 CpuMpData
->InitFlag
= ApInitReconfig
;
1227 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) OldCpuMpData
->CpuInfoInHob
;
1228 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1229 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1230 CpuMpData
->CpuData
[Index
].ApicId
= CpuInfoInHob
[Index
].ApicId
;
1231 CpuMpData
->CpuData
[Index
].InitialApicId
= CpuInfoInHob
[Index
].InitialApicId
;
1232 if (CpuMpData
->CpuData
[Index
].InitialApicId
>= 255) {
1233 CpuMpData
->X2ApicEnable
= TRUE
;
1235 CpuMpData
->CpuData
[Index
].Health
= CpuInfoInHob
[Index
].Health
;
1236 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuMpData
->CpuData
[Index
].Health
== 0)? TRUE
:FALSE
;
1237 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1239 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1240 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1241 sizeof (CPU_VOLATILE_REGISTERS
)
1244 if (MaxLogicalProcessorNumber
> 1) {
1246 // Wakeup APs to do some AP initialize sync
1248 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1250 // Wait for all APs finished initialization
1252 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1255 CpuMpData
->InitFlag
= ApInitDone
;
1256 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1257 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1263 // Initialize global data for MP support
1265 InitMpGlobalData (CpuMpData
);
1271 Gets detailed MP-related information on the requested processor at the
1272 instant this call is made. This service may only be called from the BSP.
1274 @param[in] ProcessorNumber The handle number of processor.
1275 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1276 the requested processor is deposited.
1277 @param[out] HealthData Return processor health data.
1279 @retval EFI_SUCCESS Processor information was returned.
1280 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1281 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1282 @retval EFI_NOT_FOUND The processor with the handle specified by
1283 ProcessorNumber does not exist in the platform.
1284 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1289 MpInitLibGetProcessorInfo (
1290 IN UINTN ProcessorNumber
,
1291 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1292 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1295 CPU_MP_DATA
*CpuMpData
;
1298 CpuMpData
= GetCpuMpData ();
1301 // Check whether caller processor is BSP
1303 MpInitLibWhoAmI (&CallerNumber
);
1304 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1305 return EFI_DEVICE_ERROR
;
1308 if (ProcessorInfoBuffer
== NULL
) {
1309 return EFI_INVALID_PARAMETER
;
1312 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1313 return EFI_NOT_FOUND
;
1316 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuMpData
->CpuData
[ProcessorNumber
].ApicId
;
1317 ProcessorInfoBuffer
->StatusFlag
= 0;
1318 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1319 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1321 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1322 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1324 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1325 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1327 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1331 // Get processor location information
1333 GetProcessorLocationByApicId (
1334 CpuMpData
->CpuData
[ProcessorNumber
].ApicId
,
1335 &ProcessorInfoBuffer
->Location
.Package
,
1336 &ProcessorInfoBuffer
->Location
.Core
,
1337 &ProcessorInfoBuffer
->Location
.Thread
1340 if (HealthData
!= NULL
) {
1341 HealthData
->Uint32
= CpuMpData
->CpuData
[ProcessorNumber
].Health
;
1348 Worker function to switch the requested AP to be the BSP from that point onward.
1350 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1351 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1352 enabled AP. Otherwise, it will be disabled.
1354 @retval EFI_SUCCESS BSP successfully switched.
1355 @retval others Failed to switch BSP.
1360 IN UINTN ProcessorNumber
,
1361 IN BOOLEAN EnableOldBSP
1364 CPU_MP_DATA
*CpuMpData
;
1367 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1369 CpuMpData
= GetCpuMpData ();
1372 // Check whether caller processor is BSP
1374 MpInitLibWhoAmI (&CallerNumber
);
1375 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1379 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1380 return EFI_NOT_FOUND
;
1384 // Check whether specified AP is disabled
1386 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1387 if (State
== CpuStateDisabled
) {
1388 return EFI_INVALID_PARAMETER
;
1392 // Check whether ProcessorNumber specifies the current BSP
1394 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1395 return EFI_INVALID_PARAMETER
;
1399 // Check whether specified AP is busy
1401 if (State
== CpuStateBusy
) {
1402 return EFI_NOT_READY
;
1405 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1406 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1407 CpuMpData
->SwitchBspFlag
= TRUE
;
1410 // Clear the BSP bit of MSR_IA32_APIC_BASE
1412 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1413 ApicBaseMsr
.Bits
.BSP
= 0;
1414 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1417 // Need to wakeUp AP (future BSP).
1419 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1421 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1424 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1426 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1427 ApicBaseMsr
.Bits
.BSP
= 1;
1428 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1431 // Wait for old BSP finished AP task
1433 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1437 CpuMpData
->SwitchBspFlag
= FALSE
;
1439 // Set old BSP enable state
1441 if (!EnableOldBSP
) {
1442 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1445 // Save new BSP number
1447 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1453 Worker function to let the caller enable or disable an AP from this point onward.
1454 This service may only be called from the BSP.
1456 @param[in] ProcessorNumber The handle number of AP.
1457 @param[in] EnableAP Specifies the new state for the processor for
1458 enabled, FALSE for disabled.
1459 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1460 the new health status of the AP.
1462 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1463 @retval others Failed to Enable/Disable AP.
1467 EnableDisableApWorker (
1468 IN UINTN ProcessorNumber
,
1469 IN BOOLEAN EnableAP
,
1470 IN UINT32
*HealthFlag OPTIONAL
1473 CPU_MP_DATA
*CpuMpData
;
1476 CpuMpData
= GetCpuMpData ();
1479 // Check whether caller processor is BSP
1481 MpInitLibWhoAmI (&CallerNumber
);
1482 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1483 return EFI_DEVICE_ERROR
;
1486 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1487 return EFI_INVALID_PARAMETER
;
1490 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1491 return EFI_NOT_FOUND
;
1495 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1497 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1500 if (HealthFlag
!= NULL
) {
1501 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1502 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1509 This return the handle number for the calling processor. This service may be
1510 called from the BSP and APs.
1512 @param[out] ProcessorNumber Pointer to the handle number of AP.
1513 The range is from 0 to the total number of
1514 logical processors minus 1. The total number of
1515 logical processors can be retrieved by
1516 MpInitLibGetNumberOfProcessors().
1518 @retval EFI_SUCCESS The current processor handle number was returned
1520 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1521 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1527 OUT UINTN
*ProcessorNumber
1530 CPU_MP_DATA
*CpuMpData
;
1532 if (ProcessorNumber
== NULL
) {
1533 return EFI_INVALID_PARAMETER
;
1536 CpuMpData
= GetCpuMpData ();
1538 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1542 Retrieves the number of logical processor in the platform and the number of
1543 those logical processors that are enabled on this boot. This service may only
1544 be called from the BSP.
1546 @param[out] NumberOfProcessors Pointer to the total number of logical
1547 processors in the system, including the BSP
1549 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1550 processors that exist in system, including
1553 @retval EFI_SUCCESS The number of logical processors and enabled
1554 logical processors was retrieved.
1555 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1556 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1558 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1563 MpInitLibGetNumberOfProcessors (
1564 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1565 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1568 CPU_MP_DATA
*CpuMpData
;
1570 UINTN ProcessorNumber
;
1571 UINTN EnabledProcessorNumber
;
1574 CpuMpData
= GetCpuMpData ();
1576 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1577 return EFI_INVALID_PARAMETER
;
1581 // Check whether caller processor is BSP
1583 MpInitLibWhoAmI (&CallerNumber
);
1584 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1585 return EFI_DEVICE_ERROR
;
1588 ProcessorNumber
= CpuMpData
->CpuCount
;
1589 EnabledProcessorNumber
= 0;
1590 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1591 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1592 EnabledProcessorNumber
++;
1596 if (NumberOfProcessors
!= NULL
) {
1597 *NumberOfProcessors
= ProcessorNumber
;
1599 if (NumberOfEnabledProcessors
!= NULL
) {
1600 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1608 Worker function to execute a caller provided function on all enabled APs.
1610 @param[in] Procedure A pointer to the function to be run on
1611 enabled APs of the system.
1612 @param[in] SingleThread If TRUE, then all the enabled APs execute
1613 the function specified by Procedure one by
1614 one, in ascending order of processor handle
1615 number. If FALSE, then all the enabled APs
1616 execute the function specified by Procedure
1618 @param[in] WaitEvent The event created by the caller with CreateEvent()
1620 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
1621 APs to return from Procedure, either for
1622 blocking or non-blocking mode.
1623 @param[in] ProcedureArgument The parameter passed into Procedure for
1625 @param[out] FailedCpuList If all APs finish successfully, then its
1626 content is set to NULL. If not all APs
1627 finish before timeout expires, then its
1628 content is set to address of the buffer
1629 holding handle numbers of the failed APs.
1631 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1632 the timeout expired.
1633 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1635 @retval others Failed to Startup all APs.
1639 StartupAllAPsWorker (
1640 IN EFI_AP_PROCEDURE Procedure
,
1641 IN BOOLEAN SingleThread
,
1642 IN EFI_EVENT WaitEvent OPTIONAL
,
1643 IN UINTN TimeoutInMicroseconds
,
1644 IN VOID
*ProcedureArgument OPTIONAL
,
1645 OUT UINTN
**FailedCpuList OPTIONAL
1649 CPU_MP_DATA
*CpuMpData
;
1650 UINTN ProcessorCount
;
1651 UINTN ProcessorNumber
;
1653 CPU_AP_DATA
*CpuData
;
1654 BOOLEAN HasEnabledAp
;
1657 CpuMpData
= GetCpuMpData ();
1659 if (FailedCpuList
!= NULL
) {
1660 *FailedCpuList
= NULL
;
1663 if (CpuMpData
->CpuCount
== 1) {
1664 return EFI_NOT_STARTED
;
1667 if (Procedure
== NULL
) {
1668 return EFI_INVALID_PARAMETER
;
1672 // Check whether caller processor is BSP
1674 MpInitLibWhoAmI (&CallerNumber
);
1675 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1676 return EFI_DEVICE_ERROR
;
1682 CheckAndUpdateApsStatus ();
1684 ProcessorCount
= CpuMpData
->CpuCount
;
1685 HasEnabledAp
= FALSE
;
1687 // Check whether all enabled APs are idle.
1688 // If any enabled AP is not idle, return EFI_NOT_READY.
1690 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1691 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1692 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1693 ApState
= GetApState (CpuData
);
1694 if (ApState
!= CpuStateDisabled
) {
1695 HasEnabledAp
= TRUE
;
1696 if (ApState
!= CpuStateIdle
) {
1698 // If any enabled APs are busy, return EFI_NOT_READY.
1700 return EFI_NOT_READY
;
1706 if (!HasEnabledAp
) {
1708 // If no enabled AP exists, return EFI_NOT_STARTED.
1710 return EFI_NOT_STARTED
;
1713 CpuMpData
->StartCount
= 0;
1714 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1715 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1716 CpuData
->Waiting
= FALSE
;
1717 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1718 if (CpuData
->State
== CpuStateIdle
) {
1720 // Mark this processor as responsible for current calling.
1722 CpuData
->Waiting
= TRUE
;
1723 CpuMpData
->StartCount
++;
1728 CpuMpData
->Procedure
= Procedure
;
1729 CpuMpData
->ProcArguments
= ProcedureArgument
;
1730 CpuMpData
->SingleThread
= SingleThread
;
1731 CpuMpData
->FinishedCount
= 0;
1732 CpuMpData
->RunningCount
= 0;
1733 CpuMpData
->FailedCpuList
= FailedCpuList
;
1734 CpuMpData
->ExpectedTime
= CalculateTimeout (
1735 TimeoutInMicroseconds
,
1736 &CpuMpData
->CurrentTime
1738 CpuMpData
->TotalTime
= 0;
1739 CpuMpData
->WaitEvent
= WaitEvent
;
1741 if (!SingleThread
) {
1742 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1744 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1745 if (ProcessorNumber
== CallerNumber
) {
1748 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1749 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1755 Status
= EFI_SUCCESS
;
1756 if (WaitEvent
== NULL
) {
1758 Status
= CheckAllAPs ();
1759 } while (Status
== EFI_NOT_READY
);
1766 Worker function to let the caller get one enabled AP to execute a caller-provided
1769 @param[in] Procedure A pointer to the function to be run on
1770 enabled APs of the system.
1771 @param[in] ProcessorNumber The handle number of the AP.
1772 @param[in] WaitEvent The event created by the caller with CreateEvent()
1774 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
1775 APs to return from Procedure, either for
1776 blocking or non-blocking mode.
1777 @param[in] ProcedureArgument The parameter passed into Procedure for
1779 @param[out] Finished If AP returns from Procedure before the
1780 timeout expires, its content is set to TRUE.
1781 Otherwise, the value is set to FALSE.
1783 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1784 the timeout expires.
1785 @retval others Failed to Startup AP.
1789 StartupThisAPWorker (
1790 IN EFI_AP_PROCEDURE Procedure
,
1791 IN UINTN ProcessorNumber
,
1792 IN EFI_EVENT WaitEvent OPTIONAL
,
1793 IN UINTN TimeoutInMicroseconds
,
1794 IN VOID
*ProcedureArgument OPTIONAL
,
1795 OUT BOOLEAN
*Finished OPTIONAL
1799 CPU_MP_DATA
*CpuMpData
;
1800 CPU_AP_DATA
*CpuData
;
1803 CpuMpData
= GetCpuMpData ();
1805 if (Finished
!= NULL
) {
1810 // Check whether caller processor is BSP
1812 MpInitLibWhoAmI (&CallerNumber
);
1813 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1814 return EFI_DEVICE_ERROR
;
1818 // Check whether processor with the handle specified by ProcessorNumber exists
1820 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1821 return EFI_NOT_FOUND
;
1825 // Check whether specified processor is BSP
1827 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1828 return EFI_INVALID_PARAMETER
;
1832 // Check parameter Procedure
1834 if (Procedure
== NULL
) {
1835 return EFI_INVALID_PARAMETER
;
1841 CheckAndUpdateApsStatus ();
1844 // Check whether specified AP is disabled
1846 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1847 return EFI_INVALID_PARAMETER
;
1851 // If WaitEvent is not NULL, execute in non-blocking mode.
1852 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
1853 // CheckAPsStatus() will check completion and timeout periodically.
1855 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1856 CpuData
->WaitEvent
= WaitEvent
;
1857 CpuData
->Finished
= Finished
;
1858 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
1859 CpuData
->TotalTime
= 0;
1861 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1864 // If WaitEvent is NULL, execute in blocking mode.
1865 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
1867 Status
= EFI_SUCCESS
;
1868 if (WaitEvent
== NULL
) {
1870 Status
= CheckThisAP (ProcessorNumber
);
1871 } while (Status
== EFI_NOT_READY
);
1878 Get pointer to CPU MP Data structure from GUIDed HOB.
1880 @return The pointer to CPU MP Data structure.
1883 GetCpuMpDataFromGuidedHob (
1887 EFI_HOB_GUID_TYPE
*GuidHob
;
1889 CPU_MP_DATA
*CpuMpData
;
1892 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
1893 if (GuidHob
!= NULL
) {
1894 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
1895 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
1901 Get available system memory below 1MB by specified size.
1903 @param[in] CpuMpData The pointer to CPU MP Data structure.
1906 BackupAndPrepareWakeupBuffer(
1907 IN CPU_MP_DATA
*CpuMpData
1911 (VOID
*) CpuMpData
->BackupBuffer
,
1912 (VOID
*) CpuMpData
->WakeupBuffer
,
1913 CpuMpData
->BackupBufferSize
1916 (VOID
*) CpuMpData
->WakeupBuffer
,
1917 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
1918 CpuMpData
->AddressMap
.RendezvousFunnelSize
1923 Restore wakeup buffer data.
1925 @param[in] CpuMpData The pointer to CPU MP Data structure.
1928 RestoreWakeupBuffer(
1929 IN CPU_MP_DATA
*CpuMpData
1933 (VOID
*) CpuMpData
->WakeupBuffer
,
1934 (VOID
*) CpuMpData
->BackupBuffer
,
1935 CpuMpData
->BackupBufferSize