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
256 CPU_INFO_IN_HOB CpuInfo
;
258 CPU_INFO_IN_HOB
*CpuInfoInHob
;
260 ApCount
= CpuMpData
->CpuCount
- 1;
261 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
263 for (Index1
= 0; Index1
< ApCount
; Index1
++) {
266 // Sort key is the hardware default APIC ID
268 ApicId
= CpuInfoInHob
[Index1
].ApicId
;
269 for (Index2
= Index1
+ 1; Index2
<= ApCount
; Index2
++) {
270 if (ApicId
> CpuInfoInHob
[Index2
].ApicId
) {
272 ApicId
= CpuInfoInHob
[Index2
].ApicId
;
275 if (Index3
!= Index1
) {
276 CopyMem (&CpuInfo
, &CpuInfoInHob
[Index3
], sizeof (CPU_INFO_IN_HOB
));
278 &CpuInfoInHob
[Index3
],
279 &CpuInfoInHob
[Index1
],
280 sizeof (CPU_INFO_IN_HOB
)
282 CopyMem (&CpuInfoInHob
[Index1
], &CpuInfo
, sizeof (CPU_INFO_IN_HOB
));
287 // Get the processor number for the BSP
289 ApicId
= GetInitialApicId ();
290 for (Index1
= 0; Index1
< CpuMpData
->CpuCount
; Index1
++) {
291 if (CpuInfoInHob
[Index1
].ApicId
== ApicId
) {
292 CpuMpData
->BspNumber
= (UINT32
) Index1
;
300 Enable x2APIC mode on APs.
302 @param[in, out] Buffer Pointer to private data buffer.
310 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
316 @param[in, out] Buffer Pointer to private data buffer.
324 CPU_MP_DATA
*CpuMpData
;
326 CpuMpData
= (CPU_MP_DATA
*) Buffer
;
328 // Sync BSP's MTRR table to AP
330 MtrrSetAllMtrrs (&CpuMpData
->MtrrTable
);
332 // Load microcode on AP
334 MicrocodeDetect (CpuMpData
);
338 Find the current Processor number by APIC ID.
340 @param[in] CpuMpData Pointer to PEI CPU MP Data
341 @param[in] ProcessorNumber Return the pocessor number found
343 @retval EFI_SUCCESS ProcessorNumber is found and returned.
344 @retval EFI_NOT_FOUND ProcessorNumber is not found.
348 IN CPU_MP_DATA
*CpuMpData
,
349 OUT UINTN
*ProcessorNumber
352 UINTN TotalProcessorNumber
;
354 CPU_INFO_IN_HOB
*CpuInfoInHob
;
356 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
358 TotalProcessorNumber
= CpuMpData
->CpuCount
;
359 for (Index
= 0; Index
< TotalProcessorNumber
; Index
++) {
360 if (CpuInfoInHob
[Index
].ApicId
== GetApicId ()) {
361 *ProcessorNumber
= Index
;
365 return EFI_NOT_FOUND
;
369 This function will get CPU count in the system.
371 @param[in] CpuMpData Pointer to PEI CPU MP Data
373 @return CPU count detected
376 CollectProcessorCount (
377 IN CPU_MP_DATA
*CpuMpData
381 // Send 1st broadcast IPI to APs to wakeup APs
383 CpuMpData
->InitFlag
= ApInitConfig
;
384 CpuMpData
->X2ApicEnable
= FALSE
;
385 WakeUpAP (CpuMpData
, TRUE
, 0, NULL
, NULL
);
386 CpuMpData
->InitFlag
= ApInitDone
;
387 ASSERT (CpuMpData
->CpuCount
<= PcdGet32 (PcdCpuMaxLogicalProcessorNumber
));
389 // Wait for all APs finished the initialization
391 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
395 if (CpuMpData
->X2ApicEnable
) {
396 DEBUG ((DEBUG_INFO
, "Force x2APIC mode!\n"));
398 // Wakeup all APs to enable x2APIC mode
400 WakeUpAP (CpuMpData
, TRUE
, 0, ApFuncEnableX2Apic
, NULL
);
402 // Wait for all known APs finished
404 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
408 // Enable x2APIC on BSP
410 SetApicMode (LOCAL_APIC_MODE_X2APIC
);
412 DEBUG ((DEBUG_INFO
, "APIC MODE is %d\n", GetApicMode ()));
414 // Sort BSP/Aps by CPU APIC ID in ascending order
416 SortApicId (CpuMpData
);
418 DEBUG ((DEBUG_INFO
, "MpInitLib: Find %d processors in system.\n", CpuMpData
->CpuCount
));
420 return CpuMpData
->CpuCount
;
424 Initialize CPU AP Data when AP is wakeup at the first time.
426 @param[in, out] CpuMpData Pointer to PEI CPU MP Data
427 @param[in] ProcessorNumber The handle number of processor
428 @param[in] BistData Processor BIST data
433 IN OUT CPU_MP_DATA
*CpuMpData
,
434 IN UINTN ProcessorNumber
,
436 IN UINT64 ApTopOfStack
439 CPU_INFO_IN_HOB
*CpuInfoInHob
;
441 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
442 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
443 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
444 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
445 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
447 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
448 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
449 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
451 // Set x2APIC mode if there are any logical processor reporting
452 // an Initial APIC ID of 255 or greater.
454 AcquireSpinLock(&CpuMpData
->MpLock
);
455 CpuMpData
->X2ApicEnable
= TRUE
;
456 ReleaseSpinLock(&CpuMpData
->MpLock
);
459 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
460 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
464 This function will be called from AP reset code if BSP uses WakeUpAP.
466 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
467 @param[in] NumApsExecuting Number of current executing AP
472 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
473 IN UINTN NumApsExecuting
476 CPU_MP_DATA
*CpuMpData
;
477 UINTN ProcessorNumber
;
478 EFI_AP_PROCEDURE Procedure
;
481 volatile UINT32
*ApStartupSignalBuffer
;
482 CPU_INFO_IN_HOB
*CpuInfoInHob
;
486 // AP finished assembly code and begin to execute C code
488 CpuMpData
= ExchangeInfo
->CpuMpData
;
490 ProgramVirtualWireMode ();
493 if (CpuMpData
->InitFlag
== ApInitConfig
) {
497 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
498 ProcessorNumber
= NumApsExecuting
;
500 // This is first time AP wakeup, get BIST information from AP stack
502 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
503 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
505 // Do some AP initialize sync
507 ApInitializeSync (CpuMpData
);
509 // Sync BSP's Control registers to APs
511 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
512 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
513 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
516 // Execute AP function if AP is ready
518 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
520 // Clear AP start-up signal when AP waken up
522 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
523 InterlockedCompareExchange32 (
524 (UINT32
*) ApStartupSignalBuffer
,
528 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
530 // Restore AP's volatile registers saved
532 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
535 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
536 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
537 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
538 if (Procedure
!= NULL
) {
539 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
541 // Invoke AP function here
543 Procedure (Parameter
);
544 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
545 if (CpuMpData
->SwitchBspFlag
) {
547 // Re-get the processor number due to BSP/AP maybe exchange in AP function
549 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
550 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
551 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
552 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
553 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
556 // Re-get the CPU APICID and Initial APICID
558 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
559 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
562 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
567 // AP finished executing C code
569 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
572 // Place AP is specified loop mode
574 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
576 // Save AP volatile registers
578 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
580 // Place AP in HLT-loop
583 DisableInterrupts ();
589 DisableInterrupts ();
590 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
592 // Place AP in MWAIT-loop
594 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
595 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
597 // Check AP start-up signal again.
598 // If AP start-up signal is not set, place AP into
599 // the specified C-state
601 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
603 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
605 // Place AP in Run-loop
613 // If AP start-up signal is written, AP is waken up
614 // otherwise place AP in loop again
616 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
624 Wait for AP wakeup and write AP start-up signal till AP is waken up.
626 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
630 IN
volatile UINT32
*ApStartupSignalBuffer
634 // If AP is waken up, StartupApSignal should be cleared.
635 // Otherwise, write StartupApSignal again till AP waken up.
637 while (InterlockedCompareExchange32 (
638 (UINT32
*) ApStartupSignalBuffer
,
647 This function will fill the exchange info structure.
649 @param[in] CpuMpData Pointer to CPU MP Data
653 FillExchangeInfoData (
654 IN CPU_MP_DATA
*CpuMpData
657 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
659 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
660 ExchangeInfo
->Lock
= 0;
661 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
662 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
663 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
664 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
666 ExchangeInfo
->CodeSegment
= AsmReadCs ();
667 ExchangeInfo
->DataSegment
= AsmReadDs ();
669 ExchangeInfo
->Cr3
= AsmReadCr3 ();
671 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
672 ExchangeInfo
->NumApsExecuting
= 0;
673 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
674 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
675 ExchangeInfo
->CpuMpData
= CpuMpData
;
677 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
680 // Get the BSP's data of GDT and IDT
682 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
683 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
687 Helper function that waits until the finished AP count reaches the specified
688 limit, or the specified timeout elapses (whichever comes first).
690 @param[in] CpuMpData Pointer to CPU MP Data.
691 @param[in] FinishedApLimit The number of finished APs to wait for.
692 @param[in] TimeLimit The number of microseconds to wait for.
695 TimedWaitForApFinish (
696 IN CPU_MP_DATA
*CpuMpData
,
697 IN UINT32 FinishedApLimit
,
702 This function will be called by BSP to wakeup AP.
704 @param[in] CpuMpData Pointer to CPU MP Data
705 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
706 FALSE: Send IPI to AP by ApicId
707 @param[in] ProcessorNumber The handle number of specified processor
708 @param[in] Procedure The function to be invoked by AP
709 @param[in] ProcedureArgument The argument to be passed into AP function
713 IN CPU_MP_DATA
*CpuMpData
,
714 IN BOOLEAN Broadcast
,
715 IN UINTN ProcessorNumber
,
716 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
717 IN VOID
*ProcedureArgument OPTIONAL
720 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
722 CPU_AP_DATA
*CpuData
;
723 BOOLEAN ResetVectorRequired
;
724 CPU_INFO_IN_HOB
*CpuInfoInHob
;
726 CpuMpData
->FinishedCount
= 0;
727 ResetVectorRequired
= FALSE
;
729 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
730 CpuMpData
->InitFlag
!= ApInitDone
) {
731 ResetVectorRequired
= TRUE
;
732 AllocateResetVector (CpuMpData
);
733 FillExchangeInfoData (CpuMpData
);
734 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
736 // Get AP target C-state each time when waking up AP,
737 // for it maybe updated by platform again
739 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
742 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
745 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
746 if (Index
!= CpuMpData
->BspNumber
) {
747 CpuData
= &CpuMpData
->CpuData
[Index
];
748 CpuData
->ApFunction
= (UINTN
) Procedure
;
749 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
750 SetApState (CpuData
, CpuStateReady
);
751 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
752 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
756 if (ResetVectorRequired
) {
760 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
762 if (CpuMpData
->InitFlag
== ApInitConfig
) {
764 // Wait for all potential APs waken up in one specified period
766 TimedWaitForApFinish (
768 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
769 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
773 // Wait all APs waken up if this is not the 1st broadcast of SIPI
775 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
776 CpuData
= &CpuMpData
->CpuData
[Index
];
777 if (Index
!= CpuMpData
->BspNumber
) {
778 WaitApWakeup (CpuData
->StartupApSignal
);
783 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
784 CpuData
->ApFunction
= (UINTN
) Procedure
;
785 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
786 SetApState (CpuData
, CpuStateReady
);
788 // Wakeup specified AP
790 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
791 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
792 if (ResetVectorRequired
) {
793 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
795 CpuInfoInHob
[ProcessorNumber
].ApicId
,
796 (UINT32
) ExchangeInfo
->BufferStart
800 // Wait specified AP waken up
802 WaitApWakeup (CpuData
->StartupApSignal
);
805 if (ResetVectorRequired
) {
806 FreeResetVector (CpuMpData
);
811 Calculate timeout value and return the current performance counter value.
813 Calculate the number of performance counter ticks required for a timeout.
814 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
817 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
818 @param[out] CurrentTime Returns the current value of the performance counter.
820 @return Expected time stamp counter for timeout.
821 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
827 IN UINTN TimeoutInMicroseconds
,
828 OUT UINT64
*CurrentTime
832 // Read the current value of the performance counter
834 *CurrentTime
= GetPerformanceCounter ();
837 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
840 if (TimeoutInMicroseconds
== 0) {
845 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
846 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
847 // it by 1,000,000, to get the number of ticks for the timeout value.
851 GetPerformanceCounterProperties (NULL
, NULL
),
852 TimeoutInMicroseconds
859 Checks whether timeout expires.
861 Check whether the number of elapsed performance counter ticks required for
862 a timeout condition has been reached.
863 If Timeout is zero, which means infinity, return value is always FALSE.
865 @param[in, out] PreviousTime On input, the value of the performance counter
866 when it was last read.
867 On output, the current value of the performance
869 @param[in] TotalTime The total amount of elapsed time in performance
871 @param[in] Timeout The number of performance counter ticks required
872 to reach a timeout condition.
874 @retval TRUE A timeout condition has been reached.
875 @retval FALSE A timeout condition has not been reached.
880 IN OUT UINT64
*PreviousTime
,
881 IN UINT64
*TotalTime
,
894 GetPerformanceCounterProperties (&Start
, &End
);
900 CurrentTime
= GetPerformanceCounter();
901 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
909 *PreviousTime
= CurrentTime
;
910 if (*TotalTime
> Timeout
) {
917 Helper function that waits until the finished AP count reaches the specified
918 limit, or the specified timeout elapses (whichever comes first).
920 @param[in] CpuMpData Pointer to CPU MP Data.
921 @param[in] FinishedApLimit The number of finished APs to wait for.
922 @param[in] TimeLimit The number of microseconds to wait for.
925 TimedWaitForApFinish (
926 IN CPU_MP_DATA
*CpuMpData
,
927 IN UINT32 FinishedApLimit
,
932 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
933 // "infinity", so check for (TimeLimit == 0) explicitly.
935 if (TimeLimit
== 0) {
939 CpuMpData
->TotalTime
= 0;
940 CpuMpData
->ExpectedTime
= CalculateTimeout (
942 &CpuMpData
->CurrentTime
944 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
946 &CpuMpData
->CurrentTime
,
947 &CpuMpData
->TotalTime
,
948 CpuMpData
->ExpectedTime
953 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
956 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
960 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
961 GetPerformanceCounterProperties (NULL
, NULL
),
969 Reset an AP to Idle state.
971 Any task being executed by the AP will be aborted and the AP
972 will be waiting for a new task in Wait-For-SIPI state.
974 @param[in] ProcessorNumber The handle number of processor.
977 ResetProcessorToIdleState (
978 IN UINTN ProcessorNumber
981 CPU_MP_DATA
*CpuMpData
;
983 CpuMpData
= GetCpuMpData ();
985 CpuMpData
->InitFlag
= ApInitReconfig
;
986 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
987 while (CpuMpData
->FinishedCount
< 1) {
990 CpuMpData
->InitFlag
= ApInitDone
;
992 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
996 Searches for the next waiting AP.
998 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1000 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1002 @retval EFI_SUCCESS The next waiting AP has been found.
1003 @retval EFI_NOT_FOUND No waiting AP exists.
1007 GetNextWaitingProcessorNumber (
1008 OUT UINTN
*NextProcessorNumber
1011 UINTN ProcessorNumber
;
1012 CPU_MP_DATA
*CpuMpData
;
1014 CpuMpData
= GetCpuMpData ();
1016 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1017 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1018 *NextProcessorNumber
= ProcessorNumber
;
1023 return EFI_NOT_FOUND
;
1026 /** Checks status of specified AP.
1028 This function checks whether the specified AP has finished the task assigned
1029 by StartupThisAP(), and whether timeout expires.
1031 @param[in] ProcessorNumber The handle number of processor.
1033 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1034 @retval EFI_TIMEOUT The timeout expires.
1035 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1039 IN UINTN ProcessorNumber
1042 CPU_MP_DATA
*CpuMpData
;
1043 CPU_AP_DATA
*CpuData
;
1045 CpuMpData
= GetCpuMpData ();
1046 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1049 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1050 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1051 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1054 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1056 if (GetApState(CpuData
) == CpuStateFinished
) {
1057 if (CpuData
->Finished
!= NULL
) {
1058 *(CpuData
->Finished
) = TRUE
;
1060 SetApState (CpuData
, CpuStateIdle
);
1064 // If timeout expires for StartupThisAP(), report timeout.
1066 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1067 if (CpuData
->Finished
!= NULL
) {
1068 *(CpuData
->Finished
) = FALSE
;
1071 // Reset failed AP to idle state
1073 ResetProcessorToIdleState (ProcessorNumber
);
1078 return EFI_NOT_READY
;
1082 Checks status of all APs.
1084 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1085 and whether timeout expires.
1087 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1088 @retval EFI_TIMEOUT The timeout expires.
1089 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1096 UINTN ProcessorNumber
;
1097 UINTN NextProcessorNumber
;
1100 CPU_MP_DATA
*CpuMpData
;
1101 CPU_AP_DATA
*CpuData
;
1103 CpuMpData
= GetCpuMpData ();
1105 NextProcessorNumber
= 0;
1108 // Go through all APs that are responsible for the StartupAllAPs().
1110 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1111 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1115 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1117 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1118 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1119 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1121 if (GetApState(CpuData
) == CpuStateFinished
) {
1122 CpuMpData
->RunningCount
++;
1123 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1124 SetApState(CpuData
, CpuStateIdle
);
1127 // If in Single Thread mode, then search for the next waiting AP for execution.
1129 if (CpuMpData
->SingleThread
) {
1130 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1132 if (!EFI_ERROR (Status
)) {
1136 (UINT32
) NextProcessorNumber
,
1137 CpuMpData
->Procedure
,
1138 CpuMpData
->ProcArguments
1146 // If all APs finish, return EFI_SUCCESS.
1148 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1153 // If timeout expires, report timeout.
1156 &CpuMpData
->CurrentTime
,
1157 &CpuMpData
->TotalTime
,
1158 CpuMpData
->ExpectedTime
)
1161 // If FailedCpuList is not NULL, record all failed APs in it.
1163 if (CpuMpData
->FailedCpuList
!= NULL
) {
1164 *CpuMpData
->FailedCpuList
=
1165 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1166 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1170 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1172 // Check whether this processor is responsible for StartupAllAPs().
1174 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1176 // Reset failed APs to idle state
1178 ResetProcessorToIdleState (ProcessorNumber
);
1179 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1180 if (CpuMpData
->FailedCpuList
!= NULL
) {
1181 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1185 if (CpuMpData
->FailedCpuList
!= NULL
) {
1186 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1190 return EFI_NOT_READY
;
1194 MP Initialize Library initialization.
1196 This service will allocate AP reset vector and wakeup all APs to do APs
1199 This service must be invoked before all other MP Initialize Library
1200 service are invoked.
1202 @retval EFI_SUCCESS MP initialization succeeds.
1203 @retval Others MP initialization fails.
1208 MpInitLibInitialize (
1212 CPU_MP_DATA
*OldCpuMpData
;
1213 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1214 UINT32 MaxLogicalProcessorNumber
;
1216 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1218 UINT32 MonitorFilterSize
;
1221 CPU_MP_DATA
*CpuMpData
;
1223 UINT8
*MonitorBuffer
;
1225 UINTN ApResetVectorSize
;
1226 UINTN BackupBufferAddr
;
1228 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1229 if (OldCpuMpData
== NULL
) {
1230 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1232 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1234 ASSERT (MaxLogicalProcessorNumber
!= 0);
1236 AsmGetAddressMap (&AddressMap
);
1237 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1238 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1239 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1241 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1242 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1243 BufferSize
+= sizeof (CPU_MP_DATA
);
1244 BufferSize
+= ApResetVectorSize
;
1245 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1246 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1247 ASSERT (MpBuffer
!= NULL
);
1248 ZeroMem (MpBuffer
, BufferSize
);
1249 Buffer
= (UINTN
) MpBuffer
;
1251 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1252 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1253 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1254 CpuMpData
->Buffer
= Buffer
;
1255 CpuMpData
->CpuApStackSize
= ApStackSize
;
1256 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1257 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1258 CpuMpData
->SaveRestoreFlag
= FALSE
;
1259 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1260 CpuMpData
->CpuCount
= 1;
1261 CpuMpData
->BspNumber
= 0;
1262 CpuMpData
->WaitEvent
= NULL
;
1263 CpuMpData
->SwitchBspFlag
= FALSE
;
1264 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1265 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1266 InitializeSpinLock(&CpuMpData
->MpLock
);
1268 // Save BSP's Control registers to APs
1270 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1272 // Set BSP basic information
1274 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
);
1276 // Save assembly code information
1278 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1280 // Finally set AP loop mode
1282 CpuMpData
->ApLoopMode
= ApLoopMode
;
1283 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1285 // Set up APs wakeup signal buffer
1287 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1288 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1289 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1292 // Load Microcode on BSP
1294 MicrocodeDetect (CpuMpData
);
1296 // Store BSP's MTRR setting
1298 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1300 if (OldCpuMpData
== NULL
) {
1301 if (MaxLogicalProcessorNumber
> 1) {
1303 // Wakeup all APs and calculate the processor count in system
1305 CollectProcessorCount (CpuMpData
);
1309 // APs have been wakeup before, just get the CPU Information
1312 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1313 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1314 CpuMpData
->InitFlag
= ApInitReconfig
;
1315 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1316 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1317 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1318 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1319 if (CpuInfoInHob
[Index
].InitialApicId
>= 255) {
1320 CpuMpData
->X2ApicEnable
= TRUE
;
1322 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1323 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1325 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1326 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1327 sizeof (CPU_VOLATILE_REGISTERS
)
1330 if (MaxLogicalProcessorNumber
> 1) {
1332 // Wakeup APs to do some AP initialize sync
1334 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1336 // Wait for all APs finished initialization
1338 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1341 CpuMpData
->InitFlag
= ApInitDone
;
1342 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1343 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1349 // Initialize global data for MP support
1351 InitMpGlobalData (CpuMpData
);
1357 Gets detailed MP-related information on the requested processor at the
1358 instant this call is made. This service may only be called from the BSP.
1360 @param[in] ProcessorNumber The handle number of processor.
1361 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1362 the requested processor is deposited.
1363 @param[out] HealthData Return processor health data.
1365 @retval EFI_SUCCESS Processor information was returned.
1366 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1367 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1368 @retval EFI_NOT_FOUND The processor with the handle specified by
1369 ProcessorNumber does not exist in the platform.
1370 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1375 MpInitLibGetProcessorInfo (
1376 IN UINTN ProcessorNumber
,
1377 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1378 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1381 CPU_MP_DATA
*CpuMpData
;
1383 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1385 CpuMpData
= GetCpuMpData ();
1386 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1389 // Check whether caller processor is BSP
1391 MpInitLibWhoAmI (&CallerNumber
);
1392 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1393 return EFI_DEVICE_ERROR
;
1396 if (ProcessorInfoBuffer
== NULL
) {
1397 return EFI_INVALID_PARAMETER
;
1400 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1401 return EFI_NOT_FOUND
;
1404 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1405 ProcessorInfoBuffer
->StatusFlag
= 0;
1406 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1407 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1409 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1410 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1412 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1413 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1415 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1419 // Get processor location information
1421 GetProcessorLocationByApicId (
1422 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1423 &ProcessorInfoBuffer
->Location
.Package
,
1424 &ProcessorInfoBuffer
->Location
.Core
,
1425 &ProcessorInfoBuffer
->Location
.Thread
1428 if (HealthData
!= NULL
) {
1429 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1436 Worker function to switch the requested AP to be the BSP from that point onward.
1438 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1439 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1440 enabled AP. Otherwise, it will be disabled.
1442 @retval EFI_SUCCESS BSP successfully switched.
1443 @retval others Failed to switch BSP.
1448 IN UINTN ProcessorNumber
,
1449 IN BOOLEAN EnableOldBSP
1452 CPU_MP_DATA
*CpuMpData
;
1455 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1457 CpuMpData
= GetCpuMpData ();
1460 // Check whether caller processor is BSP
1462 MpInitLibWhoAmI (&CallerNumber
);
1463 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1467 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1468 return EFI_NOT_FOUND
;
1472 // Check whether specified AP is disabled
1474 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1475 if (State
== CpuStateDisabled
) {
1476 return EFI_INVALID_PARAMETER
;
1480 // Check whether ProcessorNumber specifies the current BSP
1482 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1483 return EFI_INVALID_PARAMETER
;
1487 // Check whether specified AP is busy
1489 if (State
== CpuStateBusy
) {
1490 return EFI_NOT_READY
;
1493 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1494 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1495 CpuMpData
->SwitchBspFlag
= TRUE
;
1496 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1499 // Clear the BSP bit of MSR_IA32_APIC_BASE
1501 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1502 ApicBaseMsr
.Bits
.BSP
= 0;
1503 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1506 // Need to wakeUp AP (future BSP).
1508 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1510 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1513 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1515 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1516 ApicBaseMsr
.Bits
.BSP
= 1;
1517 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1520 // Wait for old BSP finished AP task
1522 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1526 CpuMpData
->SwitchBspFlag
= FALSE
;
1528 // Set old BSP enable state
1530 if (!EnableOldBSP
) {
1531 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1534 // Save new BSP number
1536 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1542 Worker function to let the caller enable or disable an AP from this point onward.
1543 This service may only be called from the BSP.
1545 @param[in] ProcessorNumber The handle number of AP.
1546 @param[in] EnableAP Specifies the new state for the processor for
1547 enabled, FALSE for disabled.
1548 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1549 the new health status of the AP.
1551 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1552 @retval others Failed to Enable/Disable AP.
1556 EnableDisableApWorker (
1557 IN UINTN ProcessorNumber
,
1558 IN BOOLEAN EnableAP
,
1559 IN UINT32
*HealthFlag OPTIONAL
1562 CPU_MP_DATA
*CpuMpData
;
1565 CpuMpData
= GetCpuMpData ();
1568 // Check whether caller processor is BSP
1570 MpInitLibWhoAmI (&CallerNumber
);
1571 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1572 return EFI_DEVICE_ERROR
;
1575 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1576 return EFI_INVALID_PARAMETER
;
1579 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1580 return EFI_NOT_FOUND
;
1584 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1586 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1589 if (HealthFlag
!= NULL
) {
1590 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1591 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1598 This return the handle number for the calling processor. This service may be
1599 called from the BSP and APs.
1601 @param[out] ProcessorNumber Pointer to the handle number of AP.
1602 The range is from 0 to the total number of
1603 logical processors minus 1. The total number of
1604 logical processors can be retrieved by
1605 MpInitLibGetNumberOfProcessors().
1607 @retval EFI_SUCCESS The current processor handle number was returned
1609 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1610 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1616 OUT UINTN
*ProcessorNumber
1619 CPU_MP_DATA
*CpuMpData
;
1621 if (ProcessorNumber
== NULL
) {
1622 return EFI_INVALID_PARAMETER
;
1625 CpuMpData
= GetCpuMpData ();
1627 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1631 Retrieves the number of logical processor in the platform and the number of
1632 those logical processors that are enabled on this boot. This service may only
1633 be called from the BSP.
1635 @param[out] NumberOfProcessors Pointer to the total number of logical
1636 processors in the system, including the BSP
1638 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1639 processors that exist in system, including
1642 @retval EFI_SUCCESS The number of logical processors and enabled
1643 logical processors was retrieved.
1644 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1645 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1647 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1652 MpInitLibGetNumberOfProcessors (
1653 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1654 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1657 CPU_MP_DATA
*CpuMpData
;
1659 UINTN ProcessorNumber
;
1660 UINTN EnabledProcessorNumber
;
1663 CpuMpData
= GetCpuMpData ();
1665 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1666 return EFI_INVALID_PARAMETER
;
1670 // Check whether caller processor is BSP
1672 MpInitLibWhoAmI (&CallerNumber
);
1673 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1674 return EFI_DEVICE_ERROR
;
1677 ProcessorNumber
= CpuMpData
->CpuCount
;
1678 EnabledProcessorNumber
= 0;
1679 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1680 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1681 EnabledProcessorNumber
++;
1685 if (NumberOfProcessors
!= NULL
) {
1686 *NumberOfProcessors
= ProcessorNumber
;
1688 if (NumberOfEnabledProcessors
!= NULL
) {
1689 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1697 Worker function to execute a caller provided function on all enabled APs.
1699 @param[in] Procedure A pointer to the function to be run on
1700 enabled APs of the system.
1701 @param[in] SingleThread If TRUE, then all the enabled APs execute
1702 the function specified by Procedure one by
1703 one, in ascending order of processor handle
1704 number. If FALSE, then all the enabled APs
1705 execute the function specified by Procedure
1707 @param[in] WaitEvent The event created by the caller with CreateEvent()
1709 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
1710 APs to return from Procedure, either for
1711 blocking or non-blocking mode.
1712 @param[in] ProcedureArgument The parameter passed into Procedure for
1714 @param[out] FailedCpuList If all APs finish successfully, then its
1715 content is set to NULL. If not all APs
1716 finish before timeout expires, then its
1717 content is set to address of the buffer
1718 holding handle numbers of the failed APs.
1720 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1721 the timeout expired.
1722 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1724 @retval others Failed to Startup all APs.
1728 StartupAllAPsWorker (
1729 IN EFI_AP_PROCEDURE Procedure
,
1730 IN BOOLEAN SingleThread
,
1731 IN EFI_EVENT WaitEvent OPTIONAL
,
1732 IN UINTN TimeoutInMicroseconds
,
1733 IN VOID
*ProcedureArgument OPTIONAL
,
1734 OUT UINTN
**FailedCpuList OPTIONAL
1738 CPU_MP_DATA
*CpuMpData
;
1739 UINTN ProcessorCount
;
1740 UINTN ProcessorNumber
;
1742 CPU_AP_DATA
*CpuData
;
1743 BOOLEAN HasEnabledAp
;
1746 CpuMpData
= GetCpuMpData ();
1748 if (FailedCpuList
!= NULL
) {
1749 *FailedCpuList
= NULL
;
1752 if (CpuMpData
->CpuCount
== 1) {
1753 return EFI_NOT_STARTED
;
1756 if (Procedure
== NULL
) {
1757 return EFI_INVALID_PARAMETER
;
1761 // Check whether caller processor is BSP
1763 MpInitLibWhoAmI (&CallerNumber
);
1764 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1765 return EFI_DEVICE_ERROR
;
1771 CheckAndUpdateApsStatus ();
1773 ProcessorCount
= CpuMpData
->CpuCount
;
1774 HasEnabledAp
= FALSE
;
1776 // Check whether all enabled APs are idle.
1777 // If any enabled AP is not idle, return EFI_NOT_READY.
1779 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1780 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1781 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1782 ApState
= GetApState (CpuData
);
1783 if (ApState
!= CpuStateDisabled
) {
1784 HasEnabledAp
= TRUE
;
1785 if (ApState
!= CpuStateIdle
) {
1787 // If any enabled APs are busy, return EFI_NOT_READY.
1789 return EFI_NOT_READY
;
1795 if (!HasEnabledAp
) {
1797 // If no enabled AP exists, return EFI_NOT_STARTED.
1799 return EFI_NOT_STARTED
;
1802 CpuMpData
->StartCount
= 0;
1803 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1804 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1805 CpuData
->Waiting
= FALSE
;
1806 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1807 if (CpuData
->State
== CpuStateIdle
) {
1809 // Mark this processor as responsible for current calling.
1811 CpuData
->Waiting
= TRUE
;
1812 CpuMpData
->StartCount
++;
1817 CpuMpData
->Procedure
= Procedure
;
1818 CpuMpData
->ProcArguments
= ProcedureArgument
;
1819 CpuMpData
->SingleThread
= SingleThread
;
1820 CpuMpData
->FinishedCount
= 0;
1821 CpuMpData
->RunningCount
= 0;
1822 CpuMpData
->FailedCpuList
= FailedCpuList
;
1823 CpuMpData
->ExpectedTime
= CalculateTimeout (
1824 TimeoutInMicroseconds
,
1825 &CpuMpData
->CurrentTime
1827 CpuMpData
->TotalTime
= 0;
1828 CpuMpData
->WaitEvent
= WaitEvent
;
1830 if (!SingleThread
) {
1831 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1833 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1834 if (ProcessorNumber
== CallerNumber
) {
1837 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1838 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1844 Status
= EFI_SUCCESS
;
1845 if (WaitEvent
== NULL
) {
1847 Status
= CheckAllAPs ();
1848 } while (Status
== EFI_NOT_READY
);
1855 Worker function to let the caller get one enabled AP to execute a caller-provided
1858 @param[in] Procedure A pointer to the function to be run on
1859 enabled APs of the system.
1860 @param[in] ProcessorNumber The handle number of the AP.
1861 @param[in] WaitEvent The event created by the caller with CreateEvent()
1863 @param[in] TimeoutInMicrosecsond Indicates the time limit in microseconds for
1864 APs to return from Procedure, either for
1865 blocking or non-blocking mode.
1866 @param[in] ProcedureArgument The parameter passed into Procedure for
1868 @param[out] Finished If AP returns from Procedure before the
1869 timeout expires, its content is set to TRUE.
1870 Otherwise, the value is set to FALSE.
1872 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1873 the timeout expires.
1874 @retval others Failed to Startup AP.
1878 StartupThisAPWorker (
1879 IN EFI_AP_PROCEDURE Procedure
,
1880 IN UINTN ProcessorNumber
,
1881 IN EFI_EVENT WaitEvent OPTIONAL
,
1882 IN UINTN TimeoutInMicroseconds
,
1883 IN VOID
*ProcedureArgument OPTIONAL
,
1884 OUT BOOLEAN
*Finished OPTIONAL
1888 CPU_MP_DATA
*CpuMpData
;
1889 CPU_AP_DATA
*CpuData
;
1892 CpuMpData
= GetCpuMpData ();
1894 if (Finished
!= NULL
) {
1899 // Check whether caller processor is BSP
1901 MpInitLibWhoAmI (&CallerNumber
);
1902 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1903 return EFI_DEVICE_ERROR
;
1907 // Check whether processor with the handle specified by ProcessorNumber exists
1909 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1910 return EFI_NOT_FOUND
;
1914 // Check whether specified processor is BSP
1916 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1917 return EFI_INVALID_PARAMETER
;
1921 // Check parameter Procedure
1923 if (Procedure
== NULL
) {
1924 return EFI_INVALID_PARAMETER
;
1930 CheckAndUpdateApsStatus ();
1933 // Check whether specified AP is disabled
1935 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1936 return EFI_INVALID_PARAMETER
;
1940 // If WaitEvent is not NULL, execute in non-blocking mode.
1941 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
1942 // CheckAPsStatus() will check completion and timeout periodically.
1944 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1945 CpuData
->WaitEvent
= WaitEvent
;
1946 CpuData
->Finished
= Finished
;
1947 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
1948 CpuData
->TotalTime
= 0;
1950 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1953 // If WaitEvent is NULL, execute in blocking mode.
1954 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
1956 Status
= EFI_SUCCESS
;
1957 if (WaitEvent
== NULL
) {
1959 Status
= CheckThisAP (ProcessorNumber
);
1960 } while (Status
== EFI_NOT_READY
);
1967 Get pointer to CPU MP Data structure from GUIDed HOB.
1969 @return The pointer to CPU MP Data structure.
1972 GetCpuMpDataFromGuidedHob (
1976 EFI_HOB_GUID_TYPE
*GuidHob
;
1978 CPU_MP_DATA
*CpuMpData
;
1981 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
1982 if (GuidHob
!= NULL
) {
1983 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
1984 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
1990 Get available system memory below 1MB by specified size.
1992 @param[in] CpuMpData The pointer to CPU MP Data structure.
1995 BackupAndPrepareWakeupBuffer(
1996 IN CPU_MP_DATA
*CpuMpData
2000 (VOID
*) CpuMpData
->BackupBuffer
,
2001 (VOID
*) CpuMpData
->WakeupBuffer
,
2002 CpuMpData
->BackupBufferSize
2005 (VOID
*) CpuMpData
->WakeupBuffer
,
2006 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
2007 CpuMpData
->AddressMap
.RendezvousFunnelSize
2012 Restore wakeup buffer data.
2014 @param[in] CpuMpData The pointer to CPU MP Data structure.
2017 RestoreWakeupBuffer(
2018 IN CPU_MP_DATA
*CpuMpData
2022 (VOID
*) CpuMpData
->WakeupBuffer
,
2023 (VOID
*) CpuMpData
->BackupBuffer
,
2024 CpuMpData
->BackupBufferSize