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[out] 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
429 @param[in] ApTopOfStack Top of AP stack
434 IN OUT CPU_MP_DATA
*CpuMpData
,
435 IN UINTN ProcessorNumber
,
437 IN UINT64 ApTopOfStack
440 CPU_INFO_IN_HOB
*CpuInfoInHob
;
442 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
443 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
444 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
445 CpuInfoInHob
[ProcessorNumber
].Health
= BistData
;
446 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= ApTopOfStack
;
448 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
449 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
= (BistData
== 0) ? TRUE
: FALSE
;
450 if (CpuInfoInHob
[ProcessorNumber
].InitialApicId
>= 0xFF) {
452 // Set x2APIC mode if there are any logical processor reporting
453 // an Initial APIC ID of 255 or greater.
455 AcquireSpinLock(&CpuMpData
->MpLock
);
456 CpuMpData
->X2ApicEnable
= TRUE
;
457 ReleaseSpinLock(&CpuMpData
->MpLock
);
460 InitializeSpinLock(&CpuMpData
->CpuData
[ProcessorNumber
].ApLock
);
461 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
465 This function will be called from AP reset code if BSP uses WakeUpAP.
467 @param[in] ExchangeInfo Pointer to the MP exchange info buffer
468 @param[in] NumApsExecuting Number of current executing AP
473 IN MP_CPU_EXCHANGE_INFO
*ExchangeInfo
,
474 IN UINTN NumApsExecuting
477 CPU_MP_DATA
*CpuMpData
;
478 UINTN ProcessorNumber
;
479 EFI_AP_PROCEDURE Procedure
;
482 volatile UINT32
*ApStartupSignalBuffer
;
483 CPU_INFO_IN_HOB
*CpuInfoInHob
;
487 // AP finished assembly code and begin to execute C code
489 CpuMpData
= ExchangeInfo
->CpuMpData
;
491 ProgramVirtualWireMode ();
494 if (CpuMpData
->InitFlag
== ApInitConfig
) {
498 InterlockedIncrement ((UINT32
*) &CpuMpData
->CpuCount
);
499 ProcessorNumber
= NumApsExecuting
;
501 // This is first time AP wakeup, get BIST information from AP stack
503 ApTopOfStack
= CpuMpData
->Buffer
+ (ProcessorNumber
+ 1) * CpuMpData
->CpuApStackSize
;
504 BistData
= *(UINT32
*) ((UINTN
) ApTopOfStack
- sizeof (UINTN
));
506 // Do some AP initialize sync
508 ApInitializeSync (CpuMpData
);
510 // Sync BSP's Control registers to APs
512 RestoreVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
, FALSE
);
513 InitializeApData (CpuMpData
, ProcessorNumber
, BistData
, ApTopOfStack
);
514 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
517 // Execute AP function if AP is ready
519 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
521 // Clear AP start-up signal when AP waken up
523 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
524 InterlockedCompareExchange32 (
525 (UINT32
*) ApStartupSignalBuffer
,
529 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
531 // Restore AP's volatile registers saved
533 RestoreVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
, TRUE
);
536 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateReady
) {
537 Procedure
= (EFI_AP_PROCEDURE
)CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
;
538 Parameter
= (VOID
*) CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
;
539 if (Procedure
!= NULL
) {
540 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateBusy
);
542 // Invoke AP function here
544 Procedure (Parameter
);
545 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
546 if (CpuMpData
->SwitchBspFlag
) {
548 // Re-get the processor number due to BSP/AP maybe exchange in AP function
550 GetProcessorNumber (CpuMpData
, &ProcessorNumber
);
551 CpuMpData
->CpuData
[ProcessorNumber
].ApFunction
= 0;
552 CpuMpData
->CpuData
[ProcessorNumber
].ApFunctionArgument
= 0;
553 ApStartupSignalBuffer
= CpuMpData
->CpuData
[ProcessorNumber
].StartupApSignal
;
554 CpuInfoInHob
[ProcessorNumber
].ApTopOfStack
= CpuInfoInHob
[CpuMpData
->NewBspNumber
].ApTopOfStack
;
557 // Re-get the CPU APICID and Initial APICID
559 CpuInfoInHob
[ProcessorNumber
].ApicId
= GetApicId ();
560 CpuInfoInHob
[ProcessorNumber
].InitialApicId
= GetInitialApicId ();
563 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateFinished
);
568 // AP finished executing C code
570 InterlockedIncrement ((UINT32
*) &CpuMpData
->FinishedCount
);
573 // Place AP is specified loop mode
575 if (CpuMpData
->ApLoopMode
== ApInHltLoop
) {
577 // Save AP volatile registers
579 SaveVolatileRegisters (&CpuMpData
->CpuData
[ProcessorNumber
].VolatileRegisters
);
581 // Place AP in HLT-loop
584 DisableInterrupts ();
590 DisableInterrupts ();
591 if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
593 // Place AP in MWAIT-loop
595 AsmMonitor ((UINTN
) ApStartupSignalBuffer
, 0, 0);
596 if (*ApStartupSignalBuffer
!= WAKEUP_AP_SIGNAL
) {
598 // Check AP start-up signal again.
599 // If AP start-up signal is not set, place AP into
600 // the specified C-state
602 AsmMwait (CpuMpData
->ApTargetCState
<< 4, 0);
604 } else if (CpuMpData
->ApLoopMode
== ApInRunLoop
) {
606 // Place AP in Run-loop
614 // If AP start-up signal is written, AP is waken up
615 // otherwise place AP in loop again
617 if (*ApStartupSignalBuffer
== WAKEUP_AP_SIGNAL
) {
625 Wait for AP wakeup and write AP start-up signal till AP is waken up.
627 @param[in] ApStartupSignalBuffer Pointer to AP wakeup signal
631 IN
volatile UINT32
*ApStartupSignalBuffer
635 // If AP is waken up, StartupApSignal should be cleared.
636 // Otherwise, write StartupApSignal again till AP waken up.
638 while (InterlockedCompareExchange32 (
639 (UINT32
*) ApStartupSignalBuffer
,
648 This function will fill the exchange info structure.
650 @param[in] CpuMpData Pointer to CPU MP Data
654 FillExchangeInfoData (
655 IN CPU_MP_DATA
*CpuMpData
658 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
660 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
661 ExchangeInfo
->Lock
= 0;
662 ExchangeInfo
->StackStart
= CpuMpData
->Buffer
;
663 ExchangeInfo
->StackSize
= CpuMpData
->CpuApStackSize
;
664 ExchangeInfo
->BufferStart
= CpuMpData
->WakeupBuffer
;
665 ExchangeInfo
->ModeOffset
= CpuMpData
->AddressMap
.ModeEntryOffset
;
667 ExchangeInfo
->CodeSegment
= AsmReadCs ();
668 ExchangeInfo
->DataSegment
= AsmReadDs ();
670 ExchangeInfo
->Cr3
= AsmReadCr3 ();
672 ExchangeInfo
->CFunction
= (UINTN
) ApWakeupFunction
;
673 ExchangeInfo
->NumApsExecuting
= 0;
674 ExchangeInfo
->InitFlag
= (UINTN
) CpuMpData
->InitFlag
;
675 ExchangeInfo
->CpuInfo
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
676 ExchangeInfo
->CpuMpData
= CpuMpData
;
678 ExchangeInfo
->EnableExecuteDisable
= IsBspExecuteDisableEnabled ();
681 // Get the BSP's data of GDT and IDT
683 AsmReadGdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->GdtrProfile
);
684 AsmReadIdtr ((IA32_DESCRIPTOR
*) &ExchangeInfo
->IdtrProfile
);
688 Helper function that waits until the finished AP count reaches the specified
689 limit, or the specified timeout elapses (whichever comes first).
691 @param[in] CpuMpData Pointer to CPU MP Data.
692 @param[in] FinishedApLimit The number of finished APs to wait for.
693 @param[in] TimeLimit The number of microseconds to wait for.
696 TimedWaitForApFinish (
697 IN CPU_MP_DATA
*CpuMpData
,
698 IN UINT32 FinishedApLimit
,
703 This function will be called by BSP to wakeup AP.
705 @param[in] CpuMpData Pointer to CPU MP Data
706 @param[in] Broadcast TRUE: Send broadcast IPI to all APs
707 FALSE: Send IPI to AP by ApicId
708 @param[in] ProcessorNumber The handle number of specified processor
709 @param[in] Procedure The function to be invoked by AP
710 @param[in] ProcedureArgument The argument to be passed into AP function
714 IN CPU_MP_DATA
*CpuMpData
,
715 IN BOOLEAN Broadcast
,
716 IN UINTN ProcessorNumber
,
717 IN EFI_AP_PROCEDURE Procedure
, OPTIONAL
718 IN VOID
*ProcedureArgument OPTIONAL
721 volatile MP_CPU_EXCHANGE_INFO
*ExchangeInfo
;
723 CPU_AP_DATA
*CpuData
;
724 BOOLEAN ResetVectorRequired
;
725 CPU_INFO_IN_HOB
*CpuInfoInHob
;
727 CpuMpData
->FinishedCount
= 0;
728 ResetVectorRequired
= FALSE
;
730 if (CpuMpData
->ApLoopMode
== ApInHltLoop
||
731 CpuMpData
->InitFlag
!= ApInitDone
) {
732 ResetVectorRequired
= TRUE
;
733 AllocateResetVector (CpuMpData
);
734 FillExchangeInfoData (CpuMpData
);
735 } else if (CpuMpData
->ApLoopMode
== ApInMwaitLoop
) {
737 // Get AP target C-state each time when waking up AP,
738 // for it maybe updated by platform again
740 CpuMpData
->ApTargetCState
= PcdGet8 (PcdCpuApTargetCstate
);
743 ExchangeInfo
= CpuMpData
->MpCpuExchangeInfo
;
746 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
747 if (Index
!= CpuMpData
->BspNumber
) {
748 CpuData
= &CpuMpData
->CpuData
[Index
];
749 CpuData
->ApFunction
= (UINTN
) Procedure
;
750 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
751 SetApState (CpuData
, CpuStateReady
);
752 if (CpuMpData
->InitFlag
!= ApInitConfig
) {
753 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
757 if (ResetVectorRequired
) {
761 SendInitSipiSipiAllExcludingSelf ((UINT32
) ExchangeInfo
->BufferStart
);
763 if (CpuMpData
->InitFlag
== ApInitConfig
) {
765 // Wait for all potential APs waken up in one specified period
767 TimedWaitForApFinish (
769 PcdGet32 (PcdCpuMaxLogicalProcessorNumber
) - 1,
770 PcdGet32 (PcdCpuApInitTimeOutInMicroSeconds
)
774 // Wait all APs waken up if this is not the 1st broadcast of SIPI
776 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
777 CpuData
= &CpuMpData
->CpuData
[Index
];
778 if (Index
!= CpuMpData
->BspNumber
) {
779 WaitApWakeup (CpuData
->StartupApSignal
);
784 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
785 CpuData
->ApFunction
= (UINTN
) Procedure
;
786 CpuData
->ApFunctionArgument
= (UINTN
) ProcedureArgument
;
787 SetApState (CpuData
, CpuStateReady
);
789 // Wakeup specified AP
791 ASSERT (CpuMpData
->InitFlag
!= ApInitConfig
);
792 *(UINT32
*) CpuData
->StartupApSignal
= WAKEUP_AP_SIGNAL
;
793 if (ResetVectorRequired
) {
794 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
796 CpuInfoInHob
[ProcessorNumber
].ApicId
,
797 (UINT32
) ExchangeInfo
->BufferStart
801 // Wait specified AP waken up
803 WaitApWakeup (CpuData
->StartupApSignal
);
806 if (ResetVectorRequired
) {
807 FreeResetVector (CpuMpData
);
812 Calculate timeout value and return the current performance counter value.
814 Calculate the number of performance counter ticks required for a timeout.
815 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
818 @param[in] TimeoutInMicroseconds Timeout value in microseconds.
819 @param[out] CurrentTime Returns the current value of the performance counter.
821 @return Expected time stamp counter for timeout.
822 If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
828 IN UINTN TimeoutInMicroseconds
,
829 OUT UINT64
*CurrentTime
833 // Read the current value of the performance counter
835 *CurrentTime
= GetPerformanceCounter ();
838 // If TimeoutInMicroseconds is 0, return value is also 0, which is recognized
841 if (TimeoutInMicroseconds
== 0) {
846 // GetPerformanceCounterProperties () returns the timestamp counter's frequency
847 // in Hz. So multiply the return value with TimeoutInMicroseconds and then divide
848 // it by 1,000,000, to get the number of ticks for the timeout value.
852 GetPerformanceCounterProperties (NULL
, NULL
),
853 TimeoutInMicroseconds
860 Checks whether timeout expires.
862 Check whether the number of elapsed performance counter ticks required for
863 a timeout condition has been reached.
864 If Timeout is zero, which means infinity, return value is always FALSE.
866 @param[in, out] PreviousTime On input, the value of the performance counter
867 when it was last read.
868 On output, the current value of the performance
870 @param[in] TotalTime The total amount of elapsed time in performance
872 @param[in] Timeout The number of performance counter ticks required
873 to reach a timeout condition.
875 @retval TRUE A timeout condition has been reached.
876 @retval FALSE A timeout condition has not been reached.
881 IN OUT UINT64
*PreviousTime
,
882 IN UINT64
*TotalTime
,
895 GetPerformanceCounterProperties (&Start
, &End
);
901 CurrentTime
= GetPerformanceCounter();
902 Delta
= (INT64
) (CurrentTime
- *PreviousTime
);
910 *PreviousTime
= CurrentTime
;
911 if (*TotalTime
> Timeout
) {
918 Helper function that waits until the finished AP count reaches the specified
919 limit, or the specified timeout elapses (whichever comes first).
921 @param[in] CpuMpData Pointer to CPU MP Data.
922 @param[in] FinishedApLimit The number of finished APs to wait for.
923 @param[in] TimeLimit The number of microseconds to wait for.
926 TimedWaitForApFinish (
927 IN CPU_MP_DATA
*CpuMpData
,
928 IN UINT32 FinishedApLimit
,
933 // CalculateTimeout() and CheckTimeout() consider a TimeLimit of 0
934 // "infinity", so check for (TimeLimit == 0) explicitly.
936 if (TimeLimit
== 0) {
940 CpuMpData
->TotalTime
= 0;
941 CpuMpData
->ExpectedTime
= CalculateTimeout (
943 &CpuMpData
->CurrentTime
945 while (CpuMpData
->FinishedCount
< FinishedApLimit
&&
947 &CpuMpData
->CurrentTime
,
948 &CpuMpData
->TotalTime
,
949 CpuMpData
->ExpectedTime
954 if (CpuMpData
->FinishedCount
>= FinishedApLimit
) {
957 "%a: reached FinishedApLimit=%u in %Lu microseconds\n",
961 MultU64x32 (CpuMpData
->TotalTime
, 1000000),
962 GetPerformanceCounterProperties (NULL
, NULL
),
970 Reset an AP to Idle state.
972 Any task being executed by the AP will be aborted and the AP
973 will be waiting for a new task in Wait-For-SIPI state.
975 @param[in] ProcessorNumber The handle number of processor.
978 ResetProcessorToIdleState (
979 IN UINTN ProcessorNumber
982 CPU_MP_DATA
*CpuMpData
;
984 CpuMpData
= GetCpuMpData ();
986 CpuMpData
->InitFlag
= ApInitReconfig
;
987 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, NULL
, NULL
);
988 while (CpuMpData
->FinishedCount
< 1) {
991 CpuMpData
->InitFlag
= ApInitDone
;
993 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
997 Searches for the next waiting AP.
999 Search for the next AP that is put in waiting state by single-threaded StartupAllAPs().
1001 @param[out] NextProcessorNumber Pointer to the processor number of the next waiting AP.
1003 @retval EFI_SUCCESS The next waiting AP has been found.
1004 @retval EFI_NOT_FOUND No waiting AP exists.
1008 GetNextWaitingProcessorNumber (
1009 OUT UINTN
*NextProcessorNumber
1012 UINTN ProcessorNumber
;
1013 CPU_MP_DATA
*CpuMpData
;
1015 CpuMpData
= GetCpuMpData ();
1017 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1018 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1019 *NextProcessorNumber
= ProcessorNumber
;
1024 return EFI_NOT_FOUND
;
1027 /** Checks status of specified AP.
1029 This function checks whether the specified AP has finished the task assigned
1030 by StartupThisAP(), and whether timeout expires.
1032 @param[in] ProcessorNumber The handle number of processor.
1034 @retval EFI_SUCCESS Specified AP has finished task assigned by StartupThisAPs().
1035 @retval EFI_TIMEOUT The timeout expires.
1036 @retval EFI_NOT_READY Specified AP has not finished task and timeout has not expired.
1040 IN UINTN ProcessorNumber
1043 CPU_MP_DATA
*CpuMpData
;
1044 CPU_AP_DATA
*CpuData
;
1046 CpuMpData
= GetCpuMpData ();
1047 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1050 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1051 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1052 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1055 // If the AP finishes for StartupThisAP(), return EFI_SUCCESS.
1057 if (GetApState(CpuData
) == CpuStateFinished
) {
1058 if (CpuData
->Finished
!= NULL
) {
1059 *(CpuData
->Finished
) = TRUE
;
1061 SetApState (CpuData
, CpuStateIdle
);
1065 // If timeout expires for StartupThisAP(), report timeout.
1067 if (CheckTimeout (&CpuData
->CurrentTime
, &CpuData
->TotalTime
, CpuData
->ExpectedTime
)) {
1068 if (CpuData
->Finished
!= NULL
) {
1069 *(CpuData
->Finished
) = FALSE
;
1072 // Reset failed AP to idle state
1074 ResetProcessorToIdleState (ProcessorNumber
);
1079 return EFI_NOT_READY
;
1083 Checks status of all APs.
1085 This function checks whether all APs have finished task assigned by StartupAllAPs(),
1086 and whether timeout expires.
1088 @retval EFI_SUCCESS All APs have finished task assigned by StartupAllAPs().
1089 @retval EFI_TIMEOUT The timeout expires.
1090 @retval EFI_NOT_READY APs have not finished task and timeout has not expired.
1097 UINTN ProcessorNumber
;
1098 UINTN NextProcessorNumber
;
1101 CPU_MP_DATA
*CpuMpData
;
1102 CPU_AP_DATA
*CpuData
;
1104 CpuMpData
= GetCpuMpData ();
1106 NextProcessorNumber
= 0;
1109 // Go through all APs that are responsible for the StartupAllAPs().
1111 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1112 if (!CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1116 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1118 // Check the CPU state of AP. If it is CpuStateFinished, then the AP has finished its task.
1119 // Only BSP and corresponding AP access this unit of CPU Data. This means the AP will not modify the
1120 // value of state after setting the it to CpuStateFinished, so BSP can safely make use of its value.
1122 if (GetApState(CpuData
) == CpuStateFinished
) {
1123 CpuMpData
->RunningCount
++;
1124 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1125 SetApState(CpuData
, CpuStateIdle
);
1128 // If in Single Thread mode, then search for the next waiting AP for execution.
1130 if (CpuMpData
->SingleThread
) {
1131 Status
= GetNextWaitingProcessorNumber (&NextProcessorNumber
);
1133 if (!EFI_ERROR (Status
)) {
1137 (UINT32
) NextProcessorNumber
,
1138 CpuMpData
->Procedure
,
1139 CpuMpData
->ProcArguments
1147 // If all APs finish, return EFI_SUCCESS.
1149 if (CpuMpData
->RunningCount
== CpuMpData
->StartCount
) {
1154 // If timeout expires, report timeout.
1157 &CpuMpData
->CurrentTime
,
1158 &CpuMpData
->TotalTime
,
1159 CpuMpData
->ExpectedTime
)
1162 // If FailedCpuList is not NULL, record all failed APs in it.
1164 if (CpuMpData
->FailedCpuList
!= NULL
) {
1165 *CpuMpData
->FailedCpuList
=
1166 AllocatePool ((CpuMpData
->StartCount
- CpuMpData
->FinishedCount
+ 1) * sizeof (UINTN
));
1167 ASSERT (*CpuMpData
->FailedCpuList
!= NULL
);
1171 for (ProcessorNumber
= 0; ProcessorNumber
< CpuMpData
->CpuCount
; ProcessorNumber
++) {
1173 // Check whether this processor is responsible for StartupAllAPs().
1175 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1177 // Reset failed APs to idle state
1179 ResetProcessorToIdleState (ProcessorNumber
);
1180 CpuMpData
->CpuData
[ProcessorNumber
].Waiting
= FALSE
;
1181 if (CpuMpData
->FailedCpuList
!= NULL
) {
1182 (*CpuMpData
->FailedCpuList
)[ListIndex
++] = ProcessorNumber
;
1186 if (CpuMpData
->FailedCpuList
!= NULL
) {
1187 (*CpuMpData
->FailedCpuList
)[ListIndex
] = END_OF_CPU_LIST
;
1191 return EFI_NOT_READY
;
1195 MP Initialize Library initialization.
1197 This service will allocate AP reset vector and wakeup all APs to do APs
1200 This service must be invoked before all other MP Initialize Library
1201 service are invoked.
1203 @retval EFI_SUCCESS MP initialization succeeds.
1204 @retval Others MP initialization fails.
1209 MpInitLibInitialize (
1213 CPU_MP_DATA
*OldCpuMpData
;
1214 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1215 UINT32 MaxLogicalProcessorNumber
;
1217 MP_ASSEMBLY_ADDRESS_MAP AddressMap
;
1219 UINT32 MonitorFilterSize
;
1222 CPU_MP_DATA
*CpuMpData
;
1224 UINT8
*MonitorBuffer
;
1226 UINTN ApResetVectorSize
;
1227 UINTN BackupBufferAddr
;
1229 OldCpuMpData
= GetCpuMpDataFromGuidedHob ();
1230 if (OldCpuMpData
== NULL
) {
1231 MaxLogicalProcessorNumber
= PcdGet32(PcdCpuMaxLogicalProcessorNumber
);
1233 MaxLogicalProcessorNumber
= OldCpuMpData
->CpuCount
;
1235 ASSERT (MaxLogicalProcessorNumber
!= 0);
1237 AsmGetAddressMap (&AddressMap
);
1238 ApResetVectorSize
= AddressMap
.RendezvousFunnelSize
+ sizeof (MP_CPU_EXCHANGE_INFO
);
1239 ApStackSize
= PcdGet32(PcdCpuApStackSize
);
1240 ApLoopMode
= GetApLoopMode (&MonitorFilterSize
);
1242 BufferSize
= ApStackSize
* MaxLogicalProcessorNumber
;
1243 BufferSize
+= MonitorFilterSize
* MaxLogicalProcessorNumber
;
1244 BufferSize
+= sizeof (CPU_MP_DATA
);
1245 BufferSize
+= ApResetVectorSize
;
1246 BufferSize
+= (sizeof (CPU_AP_DATA
) + sizeof (CPU_INFO_IN_HOB
))* MaxLogicalProcessorNumber
;
1247 MpBuffer
= AllocatePages (EFI_SIZE_TO_PAGES (BufferSize
));
1248 ASSERT (MpBuffer
!= NULL
);
1249 ZeroMem (MpBuffer
, BufferSize
);
1250 Buffer
= (UINTN
) MpBuffer
;
1252 MonitorBuffer
= (UINT8
*) (Buffer
+ ApStackSize
* MaxLogicalProcessorNumber
);
1253 BackupBufferAddr
= (UINTN
) MonitorBuffer
+ MonitorFilterSize
* MaxLogicalProcessorNumber
;
1254 CpuMpData
= (CPU_MP_DATA
*) (BackupBufferAddr
+ ApResetVectorSize
);
1255 CpuMpData
->Buffer
= Buffer
;
1256 CpuMpData
->CpuApStackSize
= ApStackSize
;
1257 CpuMpData
->BackupBuffer
= BackupBufferAddr
;
1258 CpuMpData
->BackupBufferSize
= ApResetVectorSize
;
1259 CpuMpData
->SaveRestoreFlag
= FALSE
;
1260 CpuMpData
->WakeupBuffer
= (UINTN
) -1;
1261 CpuMpData
->CpuCount
= 1;
1262 CpuMpData
->BspNumber
= 0;
1263 CpuMpData
->WaitEvent
= NULL
;
1264 CpuMpData
->SwitchBspFlag
= FALSE
;
1265 CpuMpData
->CpuData
= (CPU_AP_DATA
*) (CpuMpData
+ 1);
1266 CpuMpData
->CpuInfoInHob
= (UINT64
) (UINTN
) (CpuMpData
->CpuData
+ MaxLogicalProcessorNumber
);
1267 InitializeSpinLock(&CpuMpData
->MpLock
);
1269 // Save BSP's Control registers to APs
1271 SaveVolatileRegisters (&CpuMpData
->CpuData
[0].VolatileRegisters
);
1273 // Set BSP basic information
1275 InitializeApData (CpuMpData
, 0, 0, CpuMpData
->Buffer
);
1277 // Save assembly code information
1279 CopyMem (&CpuMpData
->AddressMap
, &AddressMap
, sizeof (MP_ASSEMBLY_ADDRESS_MAP
));
1281 // Finally set AP loop mode
1283 CpuMpData
->ApLoopMode
= ApLoopMode
;
1284 DEBUG ((DEBUG_INFO
, "AP Loop Mode is %d\n", CpuMpData
->ApLoopMode
));
1286 // Set up APs wakeup signal buffer
1288 for (Index
= 0; Index
< MaxLogicalProcessorNumber
; Index
++) {
1289 CpuMpData
->CpuData
[Index
].StartupApSignal
=
1290 (UINT32
*)(MonitorBuffer
+ MonitorFilterSize
* Index
);
1293 // Load Microcode on BSP
1295 MicrocodeDetect (CpuMpData
);
1297 // Store BSP's MTRR setting
1299 MtrrGetAllMtrrs (&CpuMpData
->MtrrTable
);
1301 if (OldCpuMpData
== NULL
) {
1302 if (MaxLogicalProcessorNumber
> 1) {
1304 // Wakeup all APs and calculate the processor count in system
1306 CollectProcessorCount (CpuMpData
);
1310 // APs have been wakeup before, just get the CPU Information
1313 CpuMpData
->CpuCount
= OldCpuMpData
->CpuCount
;
1314 CpuMpData
->BspNumber
= OldCpuMpData
->BspNumber
;
1315 CpuMpData
->InitFlag
= ApInitReconfig
;
1316 CpuMpData
->CpuInfoInHob
= OldCpuMpData
->CpuInfoInHob
;
1317 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1318 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1319 InitializeSpinLock(&CpuMpData
->CpuData
[Index
].ApLock
);
1320 if (CpuInfoInHob
[Index
].InitialApicId
>= 255) {
1321 CpuMpData
->X2ApicEnable
= TRUE
;
1323 CpuMpData
->CpuData
[Index
].CpuHealthy
= (CpuInfoInHob
[Index
].Health
== 0)? TRUE
:FALSE
;
1324 CpuMpData
->CpuData
[Index
].ApFunction
= 0;
1326 &CpuMpData
->CpuData
[Index
].VolatileRegisters
,
1327 &CpuMpData
->CpuData
[0].VolatileRegisters
,
1328 sizeof (CPU_VOLATILE_REGISTERS
)
1331 if (MaxLogicalProcessorNumber
> 1) {
1333 // Wakeup APs to do some AP initialize sync
1335 WakeUpAP (CpuMpData
, TRUE
, 0, ApInitializeSync
, CpuMpData
);
1337 // Wait for all APs finished initialization
1339 while (CpuMpData
->FinishedCount
< (CpuMpData
->CpuCount
- 1)) {
1342 CpuMpData
->InitFlag
= ApInitDone
;
1343 for (Index
= 0; Index
< CpuMpData
->CpuCount
; Index
++) {
1344 SetApState (&CpuMpData
->CpuData
[Index
], CpuStateIdle
);
1350 // Initialize global data for MP support
1352 InitMpGlobalData (CpuMpData
);
1358 Gets detailed MP-related information on the requested processor at the
1359 instant this call is made. This service may only be called from the BSP.
1361 @param[in] ProcessorNumber The handle number of processor.
1362 @param[out] ProcessorInfoBuffer A pointer to the buffer where information for
1363 the requested processor is deposited.
1364 @param[out] HealthData Return processor health data.
1366 @retval EFI_SUCCESS Processor information was returned.
1367 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1368 @retval EFI_INVALID_PARAMETER ProcessorInfoBuffer is NULL.
1369 @retval EFI_NOT_FOUND The processor with the handle specified by
1370 ProcessorNumber does not exist in the platform.
1371 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1376 MpInitLibGetProcessorInfo (
1377 IN UINTN ProcessorNumber
,
1378 OUT EFI_PROCESSOR_INFORMATION
*ProcessorInfoBuffer
,
1379 OUT EFI_HEALTH_FLAGS
*HealthData OPTIONAL
1382 CPU_MP_DATA
*CpuMpData
;
1384 CPU_INFO_IN_HOB
*CpuInfoInHob
;
1386 CpuMpData
= GetCpuMpData ();
1387 CpuInfoInHob
= (CPU_INFO_IN_HOB
*) (UINTN
) CpuMpData
->CpuInfoInHob
;
1390 // Check whether caller processor is BSP
1392 MpInitLibWhoAmI (&CallerNumber
);
1393 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1394 return EFI_DEVICE_ERROR
;
1397 if (ProcessorInfoBuffer
== NULL
) {
1398 return EFI_INVALID_PARAMETER
;
1401 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1402 return EFI_NOT_FOUND
;
1405 ProcessorInfoBuffer
->ProcessorId
= (UINT64
) CpuInfoInHob
[ProcessorNumber
].ApicId
;
1406 ProcessorInfoBuffer
->StatusFlag
= 0;
1407 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1408 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_AS_BSP_BIT
;
1410 if (CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
) {
1411 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_HEALTH_STATUS_BIT
;
1413 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1414 ProcessorInfoBuffer
->StatusFlag
&= ~PROCESSOR_ENABLED_BIT
;
1416 ProcessorInfoBuffer
->StatusFlag
|= PROCESSOR_ENABLED_BIT
;
1420 // Get processor location information
1422 GetProcessorLocationByApicId (
1423 CpuInfoInHob
[ProcessorNumber
].ApicId
,
1424 &ProcessorInfoBuffer
->Location
.Package
,
1425 &ProcessorInfoBuffer
->Location
.Core
,
1426 &ProcessorInfoBuffer
->Location
.Thread
1429 if (HealthData
!= NULL
) {
1430 HealthData
->Uint32
= CpuInfoInHob
[ProcessorNumber
].Health
;
1437 Worker function to switch the requested AP to be the BSP from that point onward.
1439 @param[in] ProcessorNumber The handle number of AP that is to become the new BSP.
1440 @param[in] EnableOldBSP If TRUE, then the old BSP will be listed as an
1441 enabled AP. Otherwise, it will be disabled.
1443 @retval EFI_SUCCESS BSP successfully switched.
1444 @retval others Failed to switch BSP.
1449 IN UINTN ProcessorNumber
,
1450 IN BOOLEAN EnableOldBSP
1453 CPU_MP_DATA
*CpuMpData
;
1456 MSR_IA32_APIC_BASE_REGISTER ApicBaseMsr
;
1458 CpuMpData
= GetCpuMpData ();
1461 // Check whether caller processor is BSP
1463 MpInitLibWhoAmI (&CallerNumber
);
1464 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1468 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1469 return EFI_NOT_FOUND
;
1473 // Check whether specified AP is disabled
1475 State
= GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]);
1476 if (State
== CpuStateDisabled
) {
1477 return EFI_INVALID_PARAMETER
;
1481 // Check whether ProcessorNumber specifies the current BSP
1483 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1484 return EFI_INVALID_PARAMETER
;
1488 // Check whether specified AP is busy
1490 if (State
== CpuStateBusy
) {
1491 return EFI_NOT_READY
;
1494 CpuMpData
->BSPInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1495 CpuMpData
->APInfo
.State
= CPU_SWITCH_STATE_IDLE
;
1496 CpuMpData
->SwitchBspFlag
= TRUE
;
1497 CpuMpData
->NewBspNumber
= ProcessorNumber
;
1500 // Clear the BSP bit of MSR_IA32_APIC_BASE
1502 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1503 ApicBaseMsr
.Bits
.BSP
= 0;
1504 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1507 // Need to wakeUp AP (future BSP).
1509 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, FutureBSPProc
, CpuMpData
);
1511 AsmExchangeRole (&CpuMpData
->BSPInfo
, &CpuMpData
->APInfo
);
1514 // Set the BSP bit of MSR_IA32_APIC_BASE on new BSP
1516 ApicBaseMsr
.Uint64
= AsmReadMsr64 (MSR_IA32_APIC_BASE
);
1517 ApicBaseMsr
.Bits
.BSP
= 1;
1518 AsmWriteMsr64 (MSR_IA32_APIC_BASE
, ApicBaseMsr
.Uint64
);
1521 // Wait for old BSP finished AP task
1523 while (GetApState (&CpuMpData
->CpuData
[CallerNumber
]) != CpuStateFinished
) {
1527 CpuMpData
->SwitchBspFlag
= FALSE
;
1529 // Set old BSP enable state
1531 if (!EnableOldBSP
) {
1532 SetApState (&CpuMpData
->CpuData
[CallerNumber
], CpuStateDisabled
);
1535 // Save new BSP number
1537 CpuMpData
->BspNumber
= (UINT32
) ProcessorNumber
;
1543 Worker function to let the caller enable or disable an AP from this point onward.
1544 This service may only be called from the BSP.
1546 @param[in] ProcessorNumber The handle number of AP.
1547 @param[in] EnableAP Specifies the new state for the processor for
1548 enabled, FALSE for disabled.
1549 @param[in] HealthFlag If not NULL, a pointer to a value that specifies
1550 the new health status of the AP.
1552 @retval EFI_SUCCESS The specified AP was enabled or disabled successfully.
1553 @retval others Failed to Enable/Disable AP.
1557 EnableDisableApWorker (
1558 IN UINTN ProcessorNumber
,
1559 IN BOOLEAN EnableAP
,
1560 IN UINT32
*HealthFlag OPTIONAL
1563 CPU_MP_DATA
*CpuMpData
;
1566 CpuMpData
= GetCpuMpData ();
1569 // Check whether caller processor is BSP
1571 MpInitLibWhoAmI (&CallerNumber
);
1572 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1573 return EFI_DEVICE_ERROR
;
1576 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1577 return EFI_INVALID_PARAMETER
;
1580 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1581 return EFI_NOT_FOUND
;
1585 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateDisabled
);
1587 SetApState (&CpuMpData
->CpuData
[ProcessorNumber
], CpuStateIdle
);
1590 if (HealthFlag
!= NULL
) {
1591 CpuMpData
->CpuData
[ProcessorNumber
].CpuHealthy
=
1592 (BOOLEAN
) ((*HealthFlag
& PROCESSOR_HEALTH_STATUS_BIT
) != 0);
1599 This return the handle number for the calling processor. This service may be
1600 called from the BSP and APs.
1602 @param[out] ProcessorNumber Pointer to the handle number of AP.
1603 The range is from 0 to the total number of
1604 logical processors minus 1. The total number of
1605 logical processors can be retrieved by
1606 MpInitLibGetNumberOfProcessors().
1608 @retval EFI_SUCCESS The current processor handle number was returned
1610 @retval EFI_INVALID_PARAMETER ProcessorNumber is NULL.
1611 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1617 OUT UINTN
*ProcessorNumber
1620 CPU_MP_DATA
*CpuMpData
;
1622 if (ProcessorNumber
== NULL
) {
1623 return EFI_INVALID_PARAMETER
;
1626 CpuMpData
= GetCpuMpData ();
1628 return GetProcessorNumber (CpuMpData
, ProcessorNumber
);
1632 Retrieves the number of logical processor in the platform and the number of
1633 those logical processors that are enabled on this boot. This service may only
1634 be called from the BSP.
1636 @param[out] NumberOfProcessors Pointer to the total number of logical
1637 processors in the system, including the BSP
1639 @param[out] NumberOfEnabledProcessors Pointer to the number of enabled logical
1640 processors that exist in system, including
1643 @retval EFI_SUCCESS The number of logical processors and enabled
1644 logical processors was retrieved.
1645 @retval EFI_DEVICE_ERROR The calling processor is an AP.
1646 @retval EFI_INVALID_PARAMETER NumberOfProcessors is NULL and NumberOfEnabledProcessors
1648 @retval EFI_NOT_READY MP Initialize Library is not initialized.
1653 MpInitLibGetNumberOfProcessors (
1654 OUT UINTN
*NumberOfProcessors
, OPTIONAL
1655 OUT UINTN
*NumberOfEnabledProcessors OPTIONAL
1658 CPU_MP_DATA
*CpuMpData
;
1660 UINTN ProcessorNumber
;
1661 UINTN EnabledProcessorNumber
;
1664 CpuMpData
= GetCpuMpData ();
1666 if ((NumberOfProcessors
== NULL
) && (NumberOfEnabledProcessors
== NULL
)) {
1667 return EFI_INVALID_PARAMETER
;
1671 // Check whether caller processor is BSP
1673 MpInitLibWhoAmI (&CallerNumber
);
1674 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1675 return EFI_DEVICE_ERROR
;
1678 ProcessorNumber
= CpuMpData
->CpuCount
;
1679 EnabledProcessorNumber
= 0;
1680 for (Index
= 0; Index
< ProcessorNumber
; Index
++) {
1681 if (GetApState (&CpuMpData
->CpuData
[Index
]) != CpuStateDisabled
) {
1682 EnabledProcessorNumber
++;
1686 if (NumberOfProcessors
!= NULL
) {
1687 *NumberOfProcessors
= ProcessorNumber
;
1689 if (NumberOfEnabledProcessors
!= NULL
) {
1690 *NumberOfEnabledProcessors
= EnabledProcessorNumber
;
1698 Worker function to execute a caller provided function on all enabled APs.
1700 @param[in] Procedure A pointer to the function to be run on
1701 enabled APs of the system.
1702 @param[in] SingleThread If TRUE, then all the enabled APs execute
1703 the function specified by Procedure one by
1704 one, in ascending order of processor handle
1705 number. If FALSE, then all the enabled APs
1706 execute the function specified by Procedure
1708 @param[in] WaitEvent The event created by the caller with CreateEvent()
1710 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1711 APs to return from Procedure, either for
1712 blocking or non-blocking mode.
1713 @param[in] ProcedureArgument The parameter passed into Procedure for
1715 @param[out] FailedCpuList If all APs finish successfully, then its
1716 content is set to NULL. If not all APs
1717 finish before timeout expires, then its
1718 content is set to address of the buffer
1719 holding handle numbers of the failed APs.
1721 @retval EFI_SUCCESS In blocking mode, all APs have finished before
1722 the timeout expired.
1723 @retval EFI_SUCCESS In non-blocking mode, function has been dispatched
1725 @retval others Failed to Startup all APs.
1729 StartupAllAPsWorker (
1730 IN EFI_AP_PROCEDURE Procedure
,
1731 IN BOOLEAN SingleThread
,
1732 IN EFI_EVENT WaitEvent OPTIONAL
,
1733 IN UINTN TimeoutInMicroseconds
,
1734 IN VOID
*ProcedureArgument OPTIONAL
,
1735 OUT UINTN
**FailedCpuList OPTIONAL
1739 CPU_MP_DATA
*CpuMpData
;
1740 UINTN ProcessorCount
;
1741 UINTN ProcessorNumber
;
1743 CPU_AP_DATA
*CpuData
;
1744 BOOLEAN HasEnabledAp
;
1747 CpuMpData
= GetCpuMpData ();
1749 if (FailedCpuList
!= NULL
) {
1750 *FailedCpuList
= NULL
;
1753 if (CpuMpData
->CpuCount
== 1) {
1754 return EFI_NOT_STARTED
;
1757 if (Procedure
== NULL
) {
1758 return EFI_INVALID_PARAMETER
;
1762 // Check whether caller processor is BSP
1764 MpInitLibWhoAmI (&CallerNumber
);
1765 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1766 return EFI_DEVICE_ERROR
;
1772 CheckAndUpdateApsStatus ();
1774 ProcessorCount
= CpuMpData
->CpuCount
;
1775 HasEnabledAp
= FALSE
;
1777 // Check whether all enabled APs are idle.
1778 // If any enabled AP is not idle, return EFI_NOT_READY.
1780 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1781 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1782 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1783 ApState
= GetApState (CpuData
);
1784 if (ApState
!= CpuStateDisabled
) {
1785 HasEnabledAp
= TRUE
;
1786 if (ApState
!= CpuStateIdle
) {
1788 // If any enabled APs are busy, return EFI_NOT_READY.
1790 return EFI_NOT_READY
;
1796 if (!HasEnabledAp
) {
1798 // If no enabled AP exists, return EFI_NOT_STARTED.
1800 return EFI_NOT_STARTED
;
1803 CpuMpData
->StartCount
= 0;
1804 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1805 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1806 CpuData
->Waiting
= FALSE
;
1807 if (ProcessorNumber
!= CpuMpData
->BspNumber
) {
1808 if (CpuData
->State
== CpuStateIdle
) {
1810 // Mark this processor as responsible for current calling.
1812 CpuData
->Waiting
= TRUE
;
1813 CpuMpData
->StartCount
++;
1818 CpuMpData
->Procedure
= Procedure
;
1819 CpuMpData
->ProcArguments
= ProcedureArgument
;
1820 CpuMpData
->SingleThread
= SingleThread
;
1821 CpuMpData
->FinishedCount
= 0;
1822 CpuMpData
->RunningCount
= 0;
1823 CpuMpData
->FailedCpuList
= FailedCpuList
;
1824 CpuMpData
->ExpectedTime
= CalculateTimeout (
1825 TimeoutInMicroseconds
,
1826 &CpuMpData
->CurrentTime
1828 CpuMpData
->TotalTime
= 0;
1829 CpuMpData
->WaitEvent
= WaitEvent
;
1831 if (!SingleThread
) {
1832 WakeUpAP (CpuMpData
, TRUE
, 0, Procedure
, ProcedureArgument
);
1834 for (ProcessorNumber
= 0; ProcessorNumber
< ProcessorCount
; ProcessorNumber
++) {
1835 if (ProcessorNumber
== CallerNumber
) {
1838 if (CpuMpData
->CpuData
[ProcessorNumber
].Waiting
) {
1839 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1845 Status
= EFI_SUCCESS
;
1846 if (WaitEvent
== NULL
) {
1848 Status
= CheckAllAPs ();
1849 } while (Status
== EFI_NOT_READY
);
1856 Worker function to let the caller get one enabled AP to execute a caller-provided
1859 @param[in] Procedure A pointer to the function to be run on
1860 enabled APs of the system.
1861 @param[in] ProcessorNumber The handle number of the AP.
1862 @param[in] WaitEvent The event created by the caller with CreateEvent()
1864 @param[in] TimeoutInMicroseconds Indicates the time limit in microseconds for
1865 APs to return from Procedure, either for
1866 blocking or non-blocking mode.
1867 @param[in] ProcedureArgument The parameter passed into Procedure for
1869 @param[out] Finished If AP returns from Procedure before the
1870 timeout expires, its content is set to TRUE.
1871 Otherwise, the value is set to FALSE.
1873 @retval EFI_SUCCESS In blocking mode, specified AP finished before
1874 the timeout expires.
1875 @retval others Failed to Startup AP.
1879 StartupThisAPWorker (
1880 IN EFI_AP_PROCEDURE Procedure
,
1881 IN UINTN ProcessorNumber
,
1882 IN EFI_EVENT WaitEvent OPTIONAL
,
1883 IN UINTN TimeoutInMicroseconds
,
1884 IN VOID
*ProcedureArgument OPTIONAL
,
1885 OUT BOOLEAN
*Finished OPTIONAL
1889 CPU_MP_DATA
*CpuMpData
;
1890 CPU_AP_DATA
*CpuData
;
1893 CpuMpData
= GetCpuMpData ();
1895 if (Finished
!= NULL
) {
1900 // Check whether caller processor is BSP
1902 MpInitLibWhoAmI (&CallerNumber
);
1903 if (CallerNumber
!= CpuMpData
->BspNumber
) {
1904 return EFI_DEVICE_ERROR
;
1908 // Check whether processor with the handle specified by ProcessorNumber exists
1910 if (ProcessorNumber
>= CpuMpData
->CpuCount
) {
1911 return EFI_NOT_FOUND
;
1915 // Check whether specified processor is BSP
1917 if (ProcessorNumber
== CpuMpData
->BspNumber
) {
1918 return EFI_INVALID_PARAMETER
;
1922 // Check parameter Procedure
1924 if (Procedure
== NULL
) {
1925 return EFI_INVALID_PARAMETER
;
1931 CheckAndUpdateApsStatus ();
1934 // Check whether specified AP is disabled
1936 if (GetApState (&CpuMpData
->CpuData
[ProcessorNumber
]) == CpuStateDisabled
) {
1937 return EFI_INVALID_PARAMETER
;
1941 // If WaitEvent is not NULL, execute in non-blocking mode.
1942 // BSP saves data for CheckAPsStatus(), and returns EFI_SUCCESS.
1943 // CheckAPsStatus() will check completion and timeout periodically.
1945 CpuData
= &CpuMpData
->CpuData
[ProcessorNumber
];
1946 CpuData
->WaitEvent
= WaitEvent
;
1947 CpuData
->Finished
= Finished
;
1948 CpuData
->ExpectedTime
= CalculateTimeout (TimeoutInMicroseconds
, &CpuData
->CurrentTime
);
1949 CpuData
->TotalTime
= 0;
1951 WakeUpAP (CpuMpData
, FALSE
, ProcessorNumber
, Procedure
, ProcedureArgument
);
1954 // If WaitEvent is NULL, execute in blocking mode.
1955 // BSP checks AP's state until it finishes or TimeoutInMicrosecsond expires.
1957 Status
= EFI_SUCCESS
;
1958 if (WaitEvent
== NULL
) {
1960 Status
= CheckThisAP (ProcessorNumber
);
1961 } while (Status
== EFI_NOT_READY
);
1968 Get pointer to CPU MP Data structure from GUIDed HOB.
1970 @return The pointer to CPU MP Data structure.
1973 GetCpuMpDataFromGuidedHob (
1977 EFI_HOB_GUID_TYPE
*GuidHob
;
1979 CPU_MP_DATA
*CpuMpData
;
1982 GuidHob
= GetFirstGuidHob (&mCpuInitMpLibHobGuid
);
1983 if (GuidHob
!= NULL
) {
1984 DataInHob
= GET_GUID_HOB_DATA (GuidHob
);
1985 CpuMpData
= (CPU_MP_DATA
*) (*(UINTN
*) DataInHob
);
1991 Get available system memory below 1MB by specified size.
1993 @param[in] CpuMpData The pointer to CPU MP Data structure.
1996 BackupAndPrepareWakeupBuffer(
1997 IN CPU_MP_DATA
*CpuMpData
2001 (VOID
*) CpuMpData
->BackupBuffer
,
2002 (VOID
*) CpuMpData
->WakeupBuffer
,
2003 CpuMpData
->BackupBufferSize
2006 (VOID
*) CpuMpData
->WakeupBuffer
,
2007 (VOID
*) CpuMpData
->AddressMap
.RendezvousFunnelAddress
,
2008 CpuMpData
->AddressMap
.RendezvousFunnelSize
2013 Restore wakeup buffer data.
2015 @param[in] CpuMpData The pointer to CPU MP Data structure.
2018 RestoreWakeupBuffer(
2019 IN CPU_MP_DATA
*CpuMpData
2023 (VOID
*) CpuMpData
->WakeupBuffer
,
2024 (VOID
*) CpuMpData
->BackupBuffer
,
2025 CpuMpData
->BackupBufferSize